2 INTRO Number
2 INTRO Number
March 1, 2024
1 Integer
• Add (+), Subtract (-), Multiply (*), and Divide (/)
[ ]: print(3 + 2)
print(3 - 2)
print(3 * 2)
print(3 / 2)
5
1
6
1.5
• Two multiplication symbols (**) to represent “Exponentiation”
[ ]: print(3 ** 2)
print(5 ** 2)
print(5 ** 3)
9
25
125
• Double slash (//) to represent “Floor division”
[ ]: print(3 // 2)
print(5 // 2)
print(5 // 3)
1
2
1
1
• One percent symbol (%) to represent “Modulus”
[ ]: print(3 % 2)
print(5 % 2)
print(5 % 3)
1
1
2
• Use parentheses to modify the order of operations
[ ]: print(2 + 3 * 4)
print((2 + 3) * 4)
14
20
2 Floats
Any number with a decimal point is “Float”.
0.30000000000000004
0.1
0.020000000000000004
2.0
How to solve “Floating Point Approximation - 0.1 + 0.2 != 0.3”?
• Further reading - Floating Point Math
print(round(2.675, 2))
print(round(Decimal("2.675"), 2))
0.3
0.3
2.67
2.68
2
3 Integers and Floats
When you divide any two numbers, you’ll always get a float.
[ ]: print(4/2)
print(1 + 2.0)
print(2 * 3.0)
print(3.0 ** 2)
2.0
3.0
6.0
9.0
4 Underscores in Numbers
You can group digits using underscores to make large numbers more readable.
[ ]: universe_age = 14_000_000_000
print(universe_age)
14000000000
5 Multiple Assignment
Need to separate the variable names with commas, and do the same with the values.
As long as the number of values matches the number of variables, Python will match them up
correctly.
[ ]: x, y, z = 0, 1, 0
print(f"x = {x}, y = {y}, z = {z}")
x = 0, y = 1, z = 0
6 Constanst
When you want to treat a variable as a constant in your code, write the name of the variable in all
capital letters.
[ ]: CONSTANST = 5000
References:
• Eric Matthes - Python Crash Course-No Starch Press (2023)