This Python Operators lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
Arithmetic operators calculate. Comparison operators test. Logical operators and, or, and not combine tests.
and stops when the left side is false. or stops when the left side is true. That order matters if the right side calls a function.
Example 1: Start here
Prefer a clear comparison over a long chain of operators.
age = 21 # a whole number
city = "Chennai" # text
print(age >= 18 and city == "Chennai") # and needs both tests; or needs one
print(not city) # not flips True and False
Example 2: Arithmetic
These four symbols are the everyday arithmetic operators. Division is the slash.
print(10 + 3) # the math runs, then print shows the result
print(10 - 3) # the math runs, then print shows the result
print(10 * 3) # the math runs, then print shows the result
Example 3: Comparison
== asks if two values are equal. != asks if they are different.
print(10 > 3) # a comparison is True or False
print(10 == 10) # a comparison is True or False
print(10 != 3) # a comparison is True or False
Example 4: Add and store
+= adds to the current value and stores the result.
seats = 20 # a whole number
seats += 4 # update the stored value
print(seats)
