Discover how operators work in Python, allowing you to perform calculations, comparisons, and logical operations with ease.
Operators in Python
Operators in Python are special symbols used to perform operations on variables and values. They help you carry out mathematical calculations, compare values, and make decisions in your code.
Quick Quiz: Operators Basics
What do operators do in Python?
1. Arithmetic Operators
Arithmetic operators are used for basic mathematical operations:
+
(Addition): Adds two values.-
(Subtraction): Subtracts the second value from the first.*
(Multiplication): Multiplies two values./
(Division): Divides the first value by the second.//
(Floor Division): Divides and returns the whole number part only.%
(Modulus): Returns the remainder of a division.**
(Exponentiation): Raises the first value to the power of the second.
# Examples of arithmetic operators
x = 10
y = 3
print(x + y) # Output: 13 (Addition)
print(x - y) # Output: 7 (Subtraction)
print(x * y) # Output: 30 (Multiplication)
print(x / y) # Output: 3.333... (Division)
print(x // y) # Output: 3 (Floor Division)
print(x % y) # Output: 1 (Modulus)
print(x ** y) # Output: 1000 (Exponentiation)
Arithmetic Operators Quiz
What is the output of 11 % 4
?
Which operator gives the whole number part of division?
2. Comparison Operators
Comparison operators compare two values and return a boolean result (True
or False
):
==
: Checks if values are equal.!=
: Checks if values are not equal.>
: Checks if the first value is greater than the second.<
: Checks if the first value is less than the second.>=
: Checks if the first value is greater than or equal to the second.<=
: Checks if the first value is less than or equal to the second.
# Examples of comparison operators
a = 5
b = 10
print(a == b) # Output: False (Equal)
print(a != b) # Output: True (Not equal)
print(a > b) # Output: False (Greater than)
print(a < b) # Output: True (Less than)
print(a >= b) # Output: False (Greater than or equal to)
print(a <= b) # Output: True (Less than or equal to)
Comparison Operators Quiz
What does 7 != 7
evaluate to?
Which operator checks "greater than or equal to"?
3. Logical Operators
Logical operators combine multiple conditions and return True
or False
:
and
: ReturnsTrue
if both conditions are true.or
: ReturnsTrue
if at least one condition is true.not
: Returns the opposite of the condition's truth value.
# Examples of logical operators
x = 5
y = 3
print(x > 2 and y < 5) # Output: True (Both conditions are true)
print(x > 10 or y < 5) # Output: True (One condition is true)
print(not (x > 10)) # Output: True (Reverses the false condition)
Logical Operators Quiz
What is the output of not (True and False)
?
Which operator returns True if either condition is true?
4. Assignment Operators
Assignment operators are used to assign values to variables. They can also perform arithmetic operations and update the variable's value:
=
: Assigns a value to a variable.+=
: Adds and assigns (x += 2
is the same asx = x + 2
).-=
: Subtracts and assigns.*=
: Multiplies and assigns./=
: Divides and assigns.%=
: Modulus and assigns.**=
: Exponentiates and assigns.//=
: Floor divides and assigns.
# Examples of assignment operators
x = 5
x += 3 # Same as x = x + 3
print(x) # Output: 8
x *= 2
print(x) # Output: 16
x -= 5
print(x) # Output: 11
Assignment Operators Quiz
What is the value of x
after x = 10; x /= 2
?
Which operator combines addition and assignment?