Python command line

to enter: $python3 to exit: exit() to exit

to create a python file use extension .py to execute:

while in python command line (have done the steps above)

myfile.py

while outside of the python command line (have not done the steps above)

python3 myfile.py

Indentation

indicates a code block print() must be indented to be recognized as part of the if statement number of spaces in the indentation must be >1 and must be consistent for the whole code block

#correct
if 5 > 2:
  print("Five is greater than two")
  
#incorrect
if 5 > 2:
print("Five is greater than two")

#correct
if 0 < 3:
  print("duh")
  print("of course zero is less than 3")

#incorrect
if 0 > 3:
  print("nope")
    print("this indentation is inconsistent")

Variables

assign with =

x = 22
#python is case sensative, using a capitol X will create a different variable
X = "Capitol X"
#X will not overwrite x

cow = "Holy Cow!"
#note single or double quates can be used the above is equivalent to 
#cow = 'Holy Cow!'

print(x)
## 22
print(X)
## Capitol X
print(cow)
## Holy Cow!

Variable types (classes)

to get a variable type (class):

print(type(x))
## <class 'int'>
print(type(cow))
## <class 'str'>

Variable names

must start with a letter (e.g. a) or an underscore _

#a few examples
myvar = "works"
_myvar = "also works"
myvar2 = "works 2"

####Camel Case

#lowerUpperUpperUpper etc
myVariableName = "Camel"

####Snake Case

#word_underscore_word_underscore
my_variable_name = "Snake"

Above x is an integer and y is a string automatically. To specify the data type use casting

Casting

x = str(3)    # x will be the string '3'
y = int(3)    # y will be the integer 3
z = float(3)  # z will be 3.0 (contains a decimal)

Multiple Variables

a, b, c = "Alligator", "Baboon", "Crocadile" 
print(a)
## Alligator
print(b)
## Baboon
print(c)
## Crocadile

Unpack lists or tuples

yum = ["mango", "strawberries", "pineapple"]
m, s, p = yum
print(m)
## mango
print(s)
## strawberries
print(p)
## pineapple

Can also print the whole list

print(m, s, p)
## mango strawberries pineapple

To keep the words from runnign together add spaces to the original assignments

yum = ["mango ", "strawberries ", "pineapple "]
m, s, p = yum
print(m, s, p)
## mango  strawberries  pineapple

Comments

#will not be executed

"""
create a string literal for 
easy multiline comments
python will ignore this
because it wasn't assigned to a variable
"""
## "\ncreate a string literal for \neasy multiline comments\npython will ignore this\nbecause it wasn't assigned to a variable\n"

Operators