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.
1# Addition2x = 5 + 3 # Result: 834# Subtraction5y = 10 - 4 # Result: 667# Multiplication8z = 3 * 4 # Result: 12910# Division11a = 15 / 3 # Result: 5.0 (always returns float)1213# Floor Division14b = 17 // 3 # Result: 5 (discards decimal part)1516# Modulus (remainder)17c = 17 % 3 # Result: 21819# Exponentiation20d = 2 ** 3 # Result: 8 (2 raised to power 3)2122print(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).
1# Equal to2print(5 == 5) # True34# Not equal to5print(5 != 3) # True67# Greater than8print(7 > 3) # True910# Less than11print(2 < 8) # True1213# Greater than or equal to14print(5 >= 5) # True1516# Less than or equal to17print(4 <= 3) # False1819# Compare strings20print("hello" == "hello") # True21print("hello" < "world") # True (alphabetical comparison)
Logical Operators
Logical operators combine conditional statements.
1# and operator2x = 53print(x > 0 and x < 10) # True (both conditions are true)45# or operator6y = 157print(y < 0 or y > 10) # True (at least one condition is true)89# not operator10z = True11print(not z) # False (inverts the boolean value)1213# Complex conditions14age = 2515has_license = True16can_drive = age >= 18 and has_license17print(f"Can drive: {can_drive}") # True
Assignment Operators
Assignment operators assign values to variables, often combining assignment with arithmetic.
1# Simple assignment2x = 1034# Add and assign5x += 5 # Same as: x = x + 56print(x) # 1578# Subtract and assign9x -= 3 # Same as: x = x - 310print(x) # 121112# Multiply and assign13x *= 2 # Same as: x = x * 214print(x) # 241516# Divide and assign17x /= 4 # Same as: x = x / 418print(x) # 6.0
Identity Operators
Identity operators compare object identities.
1# is operator2x = [1, 2, 3]3y = [1, 2, 3]4z = x56print(x is z) # True (same object)7print(x is y) # False (different objects)8print(x == y) # True (same value)910# is not operator11print(x is not y) # True (different objects)
Membership Operators
Membership operators test if a value is found in a sequence.
1# in operator2fruits = ["apple", "banana", "orange"]3print("apple" in fruits) # True4print("grape" in fruits) # False56# not in operator7print("grape" not in fruits) # True89# String membership10text = "Hello, World!"11print("Hello" in text) # True12print("Python" in text) # False
Operator Precedence
Operators follow a precedence order, similar to mathematical operations.
1# Example of operator precedence2result = 2 + 3 * 4 # Multiplication happens first3print(result) # 1445# Use parentheses to control order6result = (2 + 3) * 4 # Addition happens first7print(result) # 2089# Complex expression10x = 511y = 312z = 213result = x + y * z ** 2 - (x + y) / z14print(result) # 17.0
🎯 Practice Exercise
Try creating expressions that combine different operators:
1# Your turn! Try these exercises:23# 1. Calculate the area of a circle (π * r²)4radius = 55pi = 3.146area = # Your code here78# 2. Check if a number is even and positive9number = 4210is_even_and_positive = # Your code here1112# 3. Calculate the average of three numbers13a = 1014b = 1515c = 2016average = # Your code here
Related Tutorials
Learn about if statements, loops, and control structures.
Learn moreCreate reusable code blocks with functions.
Learn moreWork with sequences in Python.
Learn more