Adding, Subtracting, Multiplication, Division and More!
Integers: Numbers without a decimal part.
5, 10, -2, 0, -210
Floats: Numbers with a decimal point
1.0, 1.2, 1.2345, -3.45, -3.0, 5.0001
You can perform all the basic operations in Python
#Addition
2+3
#Subtraction
3-2
#Multiplication
3*2
#Division
3/2
5
1
6
1.5
You can also use two asterisks (*) to represent exponents and parenthesis to group terms.
#Exponentiation - 3^2
3**2
#Exponentiation - 3^3
3**3
#Order of Operations
2+3*4
#Grouping
(2+3)*4
9
27
14
20
Adding, Multiplying, Subtracting and Dividing Floating Point Numbers behave like you would expect.
#Addition
0.1+0.1
0.2+0.2
#Multiplication
2*0.1
2*0.2
0.2
0.4
0.2
0.4
You might sometimes get a lot of decimal places and a random number at the end
#Addition
0.2+0.1
#Multiplication
3*0.1
0.30000000000000004
0.30000000000000004
Performing operations with integers and floats gives a varying type of results.
#Division - Int / Int
4/2
#Addition - Int + float
1+2.0
#Multiplication - Int * float
2*3.0
#Exponentiation - float^Int
3.0**2
2.0
3.0
6.0
9.0
Here is a quick rundown of the expected result of math operations
In Python you can assign the value of a number to a variable.
You can even separate place values with an underscore to make the numbers easier to read!
#You can use underscores to separate place values.
universe_age = 14_000_000_000
print(universe_age)
14000000000
You can assign multiple values to variables in one line to make your code shorter. Usually you initialize variables that are related.
# Assign multiple values in a line
x,y,z = 1,2,3
print(x)
print(y)
print(z)
1
2
3
Constants are value that do not change during the entire program. We want to have the habit of using variable names in ALL CAPS.
# Define a constant in ALL CAPS
PI = 3.141592
print(PI)
3.141592