Python Operators
Python Operators
Key Term:
Adding strings together is
called concatenation
(con - cat - en - ay - shun).
Python has four data types:
• String (text or alphanumeric information);
• Integer (a whole number, a number which is not a fraction);
• Float (a number which can be a fraction);
• Boolean (can only be one of two values, True or False).
Type Casting
In programming we sometimes need to change the data type
of information. This is known as type casting.
Evaluate each expression and tell whether it's true or false.
3<2 FALSE
5>8 FALSE
3>=3 TRUE
4 == 4 TRUE
7 != 3 TRUE
Python: Operators
In This Lesson
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Arithmetic Operators
Arithmetic Operators
- Subtraction x-y 3
* Multiplication x*y 10
Example
Operator Name Output
(x = 5, y = 2)
% Modulus x%y 1
** Exponentiation x ** y 25
// Multiplication x//y 2
Example
x = 5
y = 2
print("x + y = ",x + y) x+y= 7
print("x - y = ",x - y) x-y= 3
print("x * y = ",x * y) x * y = 10
print("x / y = ",x / y) x / y = 2.5
print("x % y = ",x % y) x%y= 1
print("x ** y = ",x ** y) x ** y = 25
print("x // y = ",x // y) x // y = 2
Example
print(5 + 2) 7
print(5 - 2) 3
print(5 * 2) 10
print(5 / 2) 2.5
print(5 % 2) 1
print(5 ** 2) 25
print(5 // 2) 2
Assignment Operators
Assignment Operators
x = True
y = False
print(x and y) False
print(x or y) True
print(not x) False
Example
x = 3
y = 7
print(x > 4 and y < 10) False
print(x > 4 or y < 5) False
print(not(x > 2)) False