Progress0%

4 of 20 topics completed

Python Operators and Expressions

In this tutorial, you'll learn about different types of operators in Python and how to use them to create expressions. Operators are special symbols that perform operations on variables and values.

Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

Python
1# Addition
2x = 5 + 3 # Result: 8
3
4# Subtraction
5y = 10 - 4 # Result: 6
6
7# Multiplication
8z = 3 * 4 # Result: 12
9
10# Division
11a = 15 / 3 # Result: 5.0 (always returns float)
12
13# Floor Division
14b = 17 // 3 # Result: 5 (discards decimal part)
15
16# Modulus (remainder)
17c = 17 % 3 # Result: 2
18
19# Exponentiation
20d = 2 ** 3 # Result: 8 (2 raised to power 3)
21
22print(f"x: {x}, y: {y}, z: {z}")
23print(f"a: {a}, b: {b}, c: {c}, d: {d}")

Comparison Operators

Comparison operators compare values and return boolean results (True or False).

Python
1# Equal to
2print(5 == 5) # True
3
4# Not equal to
5print(5 != 3) # True
6
7# Greater than
8print(7 > 3) # True
9
10# Less than
11print(2 < 8) # True
12
13# Greater than or equal to
14print(5 >= 5) # True
15
16# Less than or equal to
17print(4 <= 3) # False
18
19# Compare strings
20print("hello" == "hello") # True
21print("hello" < "world") # True (alphabetical comparison)

Logical Operators

Logical operators combine conditional statements.

Python
1# and operator
2x = 5
3print(x > 0 and x < 10) # True (both conditions are true)
4
5# or operator
6y = 15
7print(y < 0 or y > 10) # True (at least one condition is true)
8
9# not operator
10z = True
11print(not z) # False (inverts the boolean value)
12
13# Complex conditions
14age = 25
15has_license = True
16can_drive = age >= 18 and has_license
17print(f"Can drive: {can_drive}") # True

Assignment Operators

Assignment operators assign values to variables, often combining assignment with arithmetic.

Python
1# Simple assignment
2x = 10
3
4# Add and assign
5x += 5 # Same as: x = x + 5
6print(x) # 15
7
8# Subtract and assign
9x -= 3 # Same as: x = x - 3
10print(x) # 12
11
12# Multiply and assign
13x *= 2 # Same as: x = x * 2
14print(x) # 24
15
16# Divide and assign
17x /= 4 # Same as: x = x / 4
18print(x) # 6.0

Identity Operators

Identity operators compare object identities.

Python
1# is operator
2x = [1, 2, 3]
3y = [1, 2, 3]
4z = x
5
6print(x is z) # True (same object)
7print(x is y) # False (different objects)
8print(x == y) # True (same value)
9
10# is not operator
11print(x is not y) # True (different objects)

Membership Operators

Membership operators test if a value is found in a sequence.

Python
1# in operator
2fruits = ["apple", "banana", "orange"]
3print("apple" in fruits) # True
4print("grape" in fruits) # False
5
6# not in operator
7print("grape" not in fruits) # True
8
9# String membership
10text = "Hello, World!"
11print("Hello" in text) # True
12print("Python" in text) # False

Operator Precedence

Operators follow a precedence order, similar to mathematical operations.

Python
1# Example of operator precedence
2result = 2 + 3 * 4 # Multiplication happens first
3print(result) # 14
4
5# Use parentheses to control order
6result = (2 + 3) * 4 # Addition happens first
7print(result) # 20
8
9# Complex expression
10x = 5
11y = 3
12z = 2
13result = x + y * z ** 2 - (x + y) / z
14print(result) # 17.0

🎯 Practice Exercise

Try creating expressions that combine different operators:

Python
1# Your turn! Try these exercises:
2
3# 1. Calculate the area of a circle (π * r²)
4radius = 5
5pi = 3.14
6area = # Your code here
7
8# 2. Check if a number is even and positive
9number = 42
10is_even_and_positive = # Your code here
11
12# 3. Calculate the average of three numbers
13a = 10
14b = 15
15c = 20
16average = # Your code here

Related Tutorials

Learn about if statements, loops, and control structures.

Learn more

Create reusable code blocks with functions.

Learn more

Work with sequences in Python.

Learn more