Arithmetic Operators
Arithmetic Operators
Operators
print(7 + 3)
a = 7
b = 3
c = a + b
print(c)
challenge
Incrementing Variables
Incrementing a variable means to change the value of a variable by a set
amount. You will most often have a counting variable, which means you
will increment by 1.
a = 0
a = a + 1
print(a)
How to Read a = a + 1
The variable a appears twice on the same line of code. But each instance of
a refers to something different.
How to Read a = a + 1
The += Operator
Incrementing is a common task for programmers. Many programming
languages have developed a shorthand for a = a + 1 because of this. a +=
1 does the same thing as a = a + 1.
a = 0
b = 0
a = a + 1
b += 1
print(a)
print(b)
challenge
Type Casting
Type casting (or type conversion) is when you change the data type of a
variable.
a = 3
print(type(a))
a = str(a)
print(type(a))
challenge
a = 5
b = "3"
print(a + b)
Data read from the keyboard or a file is always stored as a string. If you
want to use this data, you will need to know how to convert it to the proper
data type.
String Concatenation
String Concatenation
String concatenation is the act of combining two strings together. This is
done with the + operator.
a = "This is an "
b = "example string"
c = a + b
print(c)
a = "This is an "
b = "example string"
a += b
print(a)
challenge
Subtraction
a = 10
b = 3
c = a - b
print(c)
challenge
Subtracting a Boolean?
In Python, boolean value are more than just true and false. False has the
numerical value of 0, while true has the numerical value of 1. This is why
doing math with a boolean does not give you an error message.
The -= Operator
Decrementing is the opposite of incrementing. Like +=, there is a shorthand
for decrementing a variable - -=.
a = 10
b = 3
a -= b
print(a)
Division
Division in Python is done with the / operator
a = 25
b = 5
print(a / b)
challenge
a = 25
b = 5
a /= b
print(a)
Reminder
Floor Division
Normal division in Python always returns a float. If you want a whole
number, use floor division (//). Floor division does not round up, nor
round down. It removes the decimal value from the answer.
Floor Division
a = 5
b = 2
print(a // b)
challenge
a = 5
b = 2
print(int(a // b))
Multiplication
Multiplication
Python uses the * operator for multiplication.
a = 5
b = 10
print (a * b)
challenge
Reminder
a = 3
b = "Hello!"
print(a * b)
challenge
Powers
Python uses the ** operator for powers (or exponents). So 2 ** 2 would be
two to the second power.
a = 2 ** 2
print(a)
challenge
Square Root
The square root of 4 can be calculated as 4 raised to the power of . In
Python, this is written as 4 ** 0.5.
Python does have a sqrt function, but it requires you to import the math
library. Libraries will be covered in a later lesson.
import math
square_root = math.sqrt(4)
print(square_root)
square_root = 4 ** 0.5
print(square_root)
Order of Operations
Order of Operations
Python uses the PEMDAS method for determining order of operations.
PEMDAS
a = 2
b = 3
c = 4
result = 3 * a ** 3 / (b + 5) + c
print(result)
Explanation
Mental Math
5 + 7 - 10 * 3 / 0.5
Solution
Step 1: 10 * 3 = 30
Solution: -48.0
(5 * 8) - 7 ** 2 - (-1 * 18)
Solution
Step 1: 5 * 8 = 40
Step 2: -1 * 18 = -18
Step 3: 7 ** 2 = 49
Step 4: 40 - 49 = -9
Step 5: -9 - -18 = 9
Solution: 9
9 / 3 + (100 ** 0.5) - 3
Solution
Modulo
Modulo is the mathematical operation that performs division but returns
the remainder. The modulo operator is %. The modulo operation happens
during the multiplication and division step of the order of operations.
Modulo
modulo = 5 % 2
print(modulo)
Order of Operations
Modulo is treated like multiplication or division, and is performed in a left
to right manner.
challenge