Python Operations

Operator Description
+ addition
- subtraction
* multiplication
/ division
** exponentiation

Numeric Operations

6 + 5.7
11.7


15 - 7
8


4.5 * 4
18.0


2 ** 3
8


2.2 ** 5
51.53632000000002

Bool

True + True 
2


True * 4
4


False * 2 + True
1


False + 4
4

Str

'The monster under my bed' + ' is named Mike' 
'The monster under my bed is named Mike'


'The monster under my bed' - ' is named Mike' 
TypeError: unsupported operand type(s) for -: 'str' and 'str'

Detailed traceback: 
  File "<string>", line 1, in <module>


'The monster under my bed' / ' is named Mike' 
TypeError: unsupported operand type(s) for /: 'str' and 'str'

Detailed traceback: 
  File "<string>", line 1, in <module>
'The monster under my bed' + 1200
TypeError: can only concatenate str (not "int") to str

Detailed traceback: 
  File "<string>", line 1, in <module>


'The monster under my bed' + str(1200)
'The monster under my bed1200'


'The monster under my bed' * 3
'The monster under my bedThe monster under my bedThe monster under my bed'

List

list1 = [1, 2.0, 3, 4.5] + ['nine', 'ten', 'eleven', 'twelve']
list1
[1, 2.0, 3, 4.5, 'nine', 'ten', 'eleven', 'twelve']


[1, 2.0, 3, 4.5] - [3, 5, 2, 1]
TypeError: unsupported operand type(s) for -: 'list' and 'list'

Detailed traceback: 
  File "<string>", line 1, in <module>


[1, 2.0, 3, 4.5] * [5, 6, 7, 8]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[15], line 1
----> 1 [1, 2.0, 3, 4.5] * [5, 6, 7, 8]

TypeError: can't multiply sequence by non-int of type 'list'

Boolean Operators

Operator Description
x == y is x equal to y?
x != y is x not equal to y?
x > y is x greater than y?
x >= y is x greater than or equal to y?
x < y is x less than y?
x <= y is x less than or equal to y?
x is y is x the same object as y?
x and y are x and y both true?
x or y is at least one of x and y true?
not x is x false?
'dogs' == 'cats'
False


6 < 7
True


(6 < 7) and ('dogs' == 'cats')
False


(6 < 7) or ('dogs' == 'cats')
True
not ('dogs' == 'cats')
True


not  (6 < 7)
False

Let’s apply what we learned!