print (type(3), 3) # integer
print (type(2.5), 2.5) # float
print (type('apples'), 'apples') # string
print (type(True), True) # boolean
print (type('3'), '3') # string
print (type('2.5'), '2.5') # string
In [3]:
print (type(3), 3) # integer
print (type(2.5), 2.5) # float
print (type('apples'), 'apples') # string
print (type(True), True) # boolean
print (type('3'), '3') # string
print (type('2.5'), '2.5') # string
Each datatype has a number of operations that are valid.
Here are some commmon operators, more will be covered as we progress in the course.
print ('1 + 2 = ', 1 + 2) # Addition
print ('2 - 1 = ', 2 - 1) # Subtraction
print ('2 * 5 = ', 2 * 5) # Multiplication
print ('6 / 2 = ', 6 / 2) # Division
print ('7 // 2 = ', 7 // 2) # Floor/Integer division
print ('2 ** 3 = ', 2 ** 3) # Exponent
print ('16 % 3 = ', 16 % 3) # Division
print ('(1 + 3) * (4 + 5) = ', (1 + 3) * (4 + 5)) # parenthesis
print ('1 + 3 * 4 + 5 = ', 1 + 3 * 4 + 5) # PEMDAS
Parenthesis can help make statements less ambigous, this is a good thing
In [18]:
print ('1 + 2 = ', 1 + 2) # Addition
print ('2 - 1 = ', 2 - 1) # Subtraction
print ('2 * 5 = ', 2 * 5) # Multiplication
print ('6 / 2 = ', 6 / 2) # Division
print ('7 // 3.5 = ', 7 // 2) # Floor/Integer division
print ('2 ** 3 = ', 2 ** 3) # Exponent
print ('16 % 3 = ', 16 % 3) # Division
print ('(1 + 3) * (4 + 5) = ', (1 + 3) * (4 + 5)) # parenthesis
print ('1 + 3 * 4 + 5 = ', 1 + 3 * 4 + 5) # PEMDAS
print("there are {} apples".format(5 + 6 // 2))
print("'Ban' + 'ana' = ", 'Ban' + 'ana')
print("'Ba' * 5 = ", 'Ba' * 5)
Nesting quotation marks '"' or "'" will treat the quote inside the other quote as being a character
In [6]:
print("'Ban' + 'ana' = ", 'Ban' + 'ana')
print("'Ba' * 5 = ", 'Ba' * 5)
Everything in Python is an Object (More on this later)
The following code demonstrates a couple of string operators
print("'Banana'.upper() = ", 'Banana'.upper())
print("'banana'.isupper() = ", 'banana'.isupper())
print("'BANANA'.isupper() = ", 'BANANA'.isupper())
print("'124'.isdigit() = ", '124'.isdigit()) # note that this is a string
print("'Banana'.swapcase() = ", 'Banana'.swapcase())
In [7]:
print("'Banana'.upper() = ", 'Banana'.upper())
print("'banana'.isupper() = ", 'banana'.isupper())
print("'BANANA'.isupper() = ", 'BANANA'.isupper())
print("'124'.isdigit() = ", '124'.isdigit()) # note that this is a string
print("'Banana'.swapcase() = ", 'Banana'.swapcase())
Python Documentation on methods - explain documentation - how to read help
help ('banana'.upper)
In [8]:
help ('banana'.upper)
In [9]:
int_one = 5
int_two = 10
result = int_one + int_two
print (result)
In [19]:
num1 = "5"
if num1.is_digit():
num1 = int(num1)
print("5" + 5)
Variables can start with a character or underscore
Variables cannot start with a number or special character
number = 1 # correct
_number = 1 # correct
!number = 1 # incorrect
number 1 = 1 # incorrect
1number = 1 # incorrect
In [13]:
number = 1 # correct
_number = 1 # correct
!number = 1 # incorrect
number 1 = 1 # incorrect
# 1number = 1 # incorrect
capital i (I), lowercase l (L) and the number 1 look the same in some fonts, capital O and 0 also look the same in some fonts
This is a style guide which dictates how your code should look.
This helps programs to look similar, which makes them easier to understand for anyone who also uses the styleguide
Here is the link to the PEP8 style guide : https://www.python.org/dev/peps/pep-0008/
These are words that are used by python and cannot be used as variable names
Reserved | Words | |||
---|---|---|---|---|
and | del | from | not | while |
as | elif | global | or | with |
assert | else | if | pass | yield |
break | except | import | class | |
exec | in | raise | continue | finally |
is | return | def | for | lambda |
try |
This will cause an error
lambda = 5
In [14]:
lambda = 5
Immutable example
animal = "dog"
print (id(animal), animal)
animal = "cat"
print (id(animal), animal) #animal is a new object, previous one is deleted
Mutable example
numbers = [1,2,3] # this is a list ( more on this later)
print (id(numbers), numbers)
numbers.append(4)
print (id(numbers), numbers) #numbers object is the same
In [21]:
numbers = [1,2,3] # this is a list ( more on this later)
print (id(numbers), numbers)
numbers.append(4)
print (id(numbers), numbers) #numbers object is the same
String methods do not change the original value. Instead a new string is created by the method
fruit = 'banana'
fruit.upper()
print (fruit) # fruit is still lowercase
The return type of a method is shown by the help function.
help (fruit.upper)
In [26]:
x = 5
y = x + 4
print(x, y)
In order to use the result of the string operation we need to store it in a variable
fruit = 'banana'
fruit.upper()
print (fruit) # fruit is still lowercase
In [ ]:
Variables can be reused, simply assign the result of the operation to the original variable
fruit = 'banana'
fruit = fruit.upper() # assign fruit to the result of the upper() method
print ('fruit is now', fruit)
In [ ]:
A boolean expressions is an expression that will either evaluate to True or False.
Operator | Expression | Comparison |
---|---|---|
== | x == y | is x equal to y? |
!= | x != y | is x not equal to y? |
< | x < y | is x less than y? |
> | x > y | is x greater than y? |
<= | x <= y | is x less than or equal to y? |
>= | x >= y | is x greater than or equal to y? |
is | x is y | is x, y? |
x = 5
y = 10
print (x,'==',y,'\t:', x == y)
print (x,'!=',y,'\t:', x != y)
print (x,'<',y,' \t:', x < y)
print (x,'>',y,' \t:', x > y)
print (x,'<=',y,'\t:', x <= y)
print (x,'>=',y,'\t:', x >= y)
print (x,'is',y,'\t:', x is y)
print ('type(x == y) : \t', type(x==y))
In [31]:
x = 5
y = 10
print (x,'==',y,'\t:', x == y)
print (x,'!=',y,'\t:', x != y)
print (x,'<',y,' \t:', x < y)
print (x,'>',y,' \t:', x > y)
print (x,'<=',y,'\t:', x <= y)
print (x,'>=',y,'\t:', x >= y)
print (x,'is',y,'\t:', x is y)
print ('type(x == y) : \t', type(x==y))
The following code evaluates 2 boolean expressions
Using the 2 boolean expressions 2 logical expressions are evaluated
num = 5
print ('positive_even :', num)
print ('\tBoolean Expressions')
print ('\t\t', num, '% 2 == 0 \t\t\t:', num%2 == 0)
print ('\t\t', num, '> 0 \t\t\t\t:', num > 0)
print ('\tLogical Expressions')
print ('\t\t', num, '% 2 == 0 and',num,'> 0 \t\t:', num%2 == 0 and num > 0)
print ('\t\t', num, '% 2 == 0 or',num,'> 0 \t\t:', num%2 == 0 or num > 0)
In [33]:
num = -6
print ('positive_even :', num)
print ('\tBoolean Expressions')
print ('\t\t', num, '% 2 == 0 \t\t\t:', num%2 == 0)
print ('\t\t', num, '> 0 \t\t\t\t:', num > 0)
print ('\tLogical Expressions')
print ('\t\t', num, '% 2 == 0 and',num,'> 0 \t\t:', num%2 == 0 and num > 0)
print ('\t\t', num, '% 2 == 0 or',num,'> 0 \t\t:', num%2 == 0 or num > 0)
In order to write usefull programs you need to be able to control which statements are executed. This is achieved using ~if~ statements.
if statements have the following format
if <expression>:
<statement block>
Note that the statement block is indented. This is how Python seperates code that should be executed by the if statement and code that is executed regardless of the statement.
In the following code statement1 and statement2 are only executed if the expressin is true. statement3 will be executed regardless of the if statement.
if <expression>:
statement1
statement2
statement3
A few word on indentation. Python does not mind how your code is indented as long as it is consistant. The accepted convention is to use a single tab or 4 whitespaces. But anything will work as long as you are consistant.
if True:
print ('Hello') # 4 spaces
print ('Hello') # 1 tab (this is bad. Use one of the other)
The following code will result in an error as the spaces are inconsistant
if True:
print ('Hello') # 4 spaces
print ('Hello') # 5 spaces
In [36]:
if True:
print ('Hello') # 4 spaces
print ('Hello') # 5 spaces
In [41]:
disease = True
x = -1
if disease == True:
if x > 0 and x <= 4:
print('give drug A')
elif x > 7 and x <= 10:
print('give drug B')
else:
print('no drug')
Allows your programs to be dynamic.
Methods
Usage
variable_to_hold_input = input('message to show user')
print (variable_to_hold_input)
In [45]:
variable_to_hold_input = input('message to show user : ')
print (type(variable_to_hold_input))
Lets build a calculator
pseudocode
Note
In [ ]: