Operators are special symbols that perform operations on values and variables. Understanding them is key to performing calculations, making decisions, and assigning values in your programs.
Python supports several types of operators:
- Arithmetic Operators These are used for mathematical calculations:
+: Addition-: Subtraction*: Multiplication/: Division (always returns a float)%: Modulus (returns the remainder of a division)**: Exponentiation (power)//: Floor division (returns the integer part of the division, rounds down)
pythona = 10 b = 3 print(f"a + b = {a + b}") # Output: 13 print(f"a - b = {a - b}") # Output: 7 print(f"a * b = {a * b}") # Output: 30 print(f"a / b = {a / b}") # Output: 3.3333333333333335 (float division) print(f"a % b = {a % b}") # Output: 1 (remainder of 10 / 3) print(f"a ** b = {a ** b}") # Output: 1000 (10 to the power of 3) print(f"a // b = {a // b}") # Output: 3 (floor division, discards fractional part)
- Comparison (Relational) Operators
These operators compare two values and return a Boolean result (
TrueorFalse).
==: Equal to!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
pythonx = 10 y = 12 print(f"x == y: {x == y}") # Output: False print(f"x != y: {x != y}") # Output: True print(f"x > y: {x > y}") # Output: False print(f"x < y: {x < y}") # Output: True print(f"x >= y: {x >= y}") # Output: False print(f"x <= y: {x <= y}") # Output: True
- Logical Operators These operators combine conditional statements (Boolean expressions).
and: ReturnsTrueif both statements areTrue.or: ReturnsTrueif at least one statement isTrue.not: Reverses the logical state of its operand.
pythonp = True q = False r = 5 s = 10 print(f"p and q: {p and q}") # Output: False print(f"p or q: {p or q}") # Output: True print(f"not p: {not p}") # Output: False print(f"r < 10 and s > 5: {r < 10 and s > 5}") # Output: True (5<10 is True, 10>5 is True) print(f"r > 10 or s > 5: {r > 10 or s > 5}") # Output: True (5>10 is False, 10>5 is True)
- Assignment Operators
These operators are used to assign values to variables. The simple assignment operator is
=. Compound assignment operators combine an arithmetic operation with assignment.
=: Assign value+=: Add and assign (x += 5isx = x + 5)-=: Subtract and assign*=: Multiply and assign/=: Divide and assign%=: Modulus and assign**=: Exponentiate and assign//=: Floor divide and assign
pythonnum = 10 print(f"Initial num: {num}") # Output: Initial num: 10 num += 5 # num = num + 5 print(f"num after += 5: {num}") # Output: num after += 5: 15 num *= 2 # num = num * 2 print(f"num after *= 2: {num}") # Output: num after *= 2: 30 num /= 3 # num = num / 3 print(f"num after /= 3: {num}") # Output: num after /= 3: 10.0 num %= 4 # num = num % 4 print(f"num after %= 4: {num}") # Output: num after %= 4: 2.0
Operator Precedence
Just like in mathematics, operators have an order of precedence. For example, multiplication and division are performed before addition and subtraction. Parentheses () can be used to override the default precedence.
pythonresult1 = 5 + 2 * 3 # Multiplication first: 5 + 6 = 11 result2 = (5 + 2) * 3 # Parentheses first: 7 * 3 = 21 print(f"Result 1: {result1}") # Output: 11 print(f"Result 2: {result2}") # Output: 21
Understanding and correctly applying these operators is fundamental to writing functional and logical Python programs. They allow you to process data, make comparisons, and control program flow.
Key Takeaways:
- Arithmetic operators perform mathematical calculations.
- Comparison operators (
==,!=,<,>) returnTrueorFalse. - Logical operators (
and,or,not) combine or modify Boolean expressions. - Assignment operators (
=,+=,-=, etc.) assign values to variables. - Operator precedence determines the order of operations; use parentheses to clarify or change it.