Mathematical Operations¶
Computers are good at math becasue they don’t get bored and almost never make mistakes. Math and other operations may be affected by the data type. Here are some key operations:
Symbol 
Meaning 
Example(s) 


Addition (for numerical types) Concatenation (for string types) 


Subtraction 


Division 


Multiplication 


Modulus (a.k.a. the remainder) 


Less than (comparison) 


Greater than (comparison) 


Less than or equal 


Greater than or equal 


Equal to 

Plus: Addition and Concatenation¶
The plus sign +
does two things. It adds two numbers and it concatenates two strings. Concatenation is just a fancy work for putting strings together. Try the code in the cells to see how the plus operator works. Put this code in the cell below to see how +
simply adds numbers:
print(10 + 5)
[ ]:
Now see what happens when you add two string literals together with this code:
print("Hello" + "World")
[ ]:
Not all things can be added together. When you mix types the +
operator raises an error. Try this:
print("one" + 1)
[ ]:
Multiplication: Sometimes Funny¶
The multiplication operator works just as you would expect on numbers. For example:
print(23.2 * 10)
[ ]:
But multiplication also works on strings by multiplying the string some number of times. For example:
print("Fun" * 1000)
[ ]:
Boolean Operators¶
Some operations result in True
or False
. They are key to asking questions using an if
statement, but we’ll talk more about that later. Printint the result of a boolean operation yields the words “True” or “False” which is convenient and I will ask you do to for assignments. For example enter these print statements:
print("Is 5 less than 3?", 5 < 3)
print("Is 5 greater than 3?", 5 > 3)
print("Is 5 greater than or equal to 3?", 5 >= 3)
print("Is 'Python' equal to 'C++'?", 'Python' == 'C++')
print("Is 'Python' equal to 'Python?", 'Python' == 'Python')
[ ]:
Order of Operations¶
Operations are applied in a specific order. Python uses the same order as many other programming languages. You can remember it using the word PEDMAS:
Parenthesis (this is done first)
Exponents
Multiplication
Division
Addition
Subtraction (this is done last)
Multiplication is higher than addition, so it is done first. For example, in this equation the multiplication happens first, then the addition.
[ ]:
print(100 + 5 * 10)
If you want to change the order of operations you can add parenthesis (
and )
to the equation.
[ ]:
print((100 + 5) * 10)