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
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")
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!
to get a variable type (class):
print(type(x))
## <class 'int'>
print(type(cow))
## <class 'str'>
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
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)
a, b, c = "Alligator", "Baboon", "Crocadile"
print(a)
## Alligator
print(b)
## Baboon
print(c)
## Crocadile
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