Arithmetic Operators in Python
Arithmetic operators are used to perform mathematical calculations. Python supports basic operations such as addition, subtraction, multiplication, division, as well as special operations like exponentiation or modulus.
Example - arithmetic operators
a = 10
b = 3
print(a + b) # Addition → 13
print(a - b) # Subtraction → 7
print(a * b) # Multiplication → 30
print(a / b) # Division → 3.333...
print(a // b) # Integer division → 3
print(a % b) # Modulus → 1
print(a ** b) # Exponentiation → 1000
//returns only the integer part of the result.%is useful for checking parity:x % 2 == 0means x is even.**is the exponentiation operator:2 ** 3→ 8.
Logical and Comparison Operators in Python
Logical and comparison operators are used to check conditions and to build expressions that return boolean
values (True or False). They are essential in conditional statements and loops.
Comparison Operators
Compare two values and return True or False.
x = 10
y = 5
print(x == y) # Equality → False
print(x != y) # Inequality → True
print(x > y) # Greater than → True
print(x < y) # Less than → False
print(x >= y) # Greater than or equal → True
print(x <= y) # Less than or equal → False
Logical Operators
Combine boolean expressions.
a = True
b = False
print(a and b) # True and False → False
print(a or b) # True or False → True
print(not a) # Negation of True → False
andreturns True only if both conditions are true.orreturns True if at least one of the conditions is true.notreverses the value:not True→ False.
Assignment Operators in Python
Assignment operators are used to store a value in a variable. Besides the simple operator =,
Python also provides compound operators that combine assignment with a mathematical operation.
Example - assignment operators
x = 10 # Simple assignment
x += 5 # Equivalent to: x = x + 5 → x becomes 15
x -= 3 # Equivalent to: x = x - 3 → x becomes 12
x *= 2 # Equivalent to: x = x * 2 → x becomes 24
x /= 4 # Equivalent to: x = x / 4 → x becomes 6.0
x %= 5 # Equivalent to: x = x % 5 → x becomes 1.0
x **= 3 # Equivalent to: x = x ** 3 → x becomes 1.0
- Compound operators are useful for writing code more concisely and clearly.
- They can also be used with data types like
strorlist, depending on context. - After an operation with
/, the result is always float, even if the division is exact.
Operators on Strings and Lists in Python
Python allows using operators on data types such as strings (str) and
lists (list). These operators are intuitive and easy to use.
String Operators (str)
text1 = "Hello"
text2 = "John"
print(text1 + " " + text2) # Concatenation → "Hello John"
print(text1 * 3) # Repetition → "HelloHelloHello"
print("lu" in text1) # Content check → True
List Operators (list)
fruits = ["apples", "pears"]
vegetables = ["carrots", "potatoes"]
print(fruits + vegetables) # Concatenation → ["apples", "pears", "carrots", "potatoes"]
print(fruits * 2) # Repetition → ["apples", "pears", "apples", "pears"]
print("pears" in fruits) # Content check → True
- The
+operator joins two strings or two lists. - The
*operator repeats the content multiple times. inchecks if an element exists in a string or a list.
Set Operators in Python
Sets (set) are unordered collections of unique elements. Python provides special operators to work
with sets: union, intersection, difference, and symmetric difference.
Example - union (|)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2) # Union → {1, 2, 3, 4, 5}
Example - intersection (&)
print(set1 & set2) # Intersection → {3}
Example - difference (-)
print(set1 - set2) # Difference → {1, 2}
print(set2 - set1) # Difference → {4, 5}
Example - symmetric difference (^)
print(set1 ^ set2) # Elements that appear in only one set → {1, 2, 4, 5}
- Sets automatically remove duplicate values.
- Set operators are very useful for data filtering and quick comparisons.
- You can also use equivalent methods:
set1.union(set2),set1.intersection(set2), etc.
Operator Precedence in Python
When an expression contains multiple operators, Python follows a set of rules to decide the order in which they are evaluated. This is called operator precedence. If you want to control the order, use parentheses.
Example - without parentheses
result = 2 + 3 * 4
print(result) # Displays: 14 - multiplication has precedence
Example - with parentheses
result = (2 + 3) * 4
print(result) # Displays: 20 - addition is performed first
Operator order (from highest to lowest precedence)
- 1. Parentheses:
( ) - 2. Power:
** - 3. Unary signs:
+x,-x,not x - 4. Multiplication, division, remainder:
*,/,//,% - 5. Addition and subtraction:
+,- - 6. Comparison:
==,!=,>,<,>=,<= - 7. Logical operators:
and,or,not - 8. Assignment:
=,+=,*=, etc.
- Use parentheses for clarity, especially in complex expressions.
- Python follows standard mathematical order, but can evaluate expressions left to right.
- Testing in the interpreter helps you understand how expressions behave.
