codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

13 / 68 topics
7Python Data Types Overview8Python Numbers & Math9Python Type Conversion (Casting)10Python Booleans & None11Python Strings & Methods12Python String Formatting13Python Operators & Precedence
Tutorials/Python Programming/Python Operators & Precedence
🐍Python Programming

Python Operators & Precedence

Updated 2026-05-15
30 min read

Python Operators & Precedence

In this tutorial, you'll explore the different types of operators available in Python, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. Understanding these operators is crucial for performing operations on data and controlling the flow of your programs. Additionally, we'll delve into operator precedence to ensure that expressions are evaluated correctly.

Introduction

Operators in Python allow you to perform various operations on variables and values. They can be categorized into several types:

  • Arithmetic Operators: Used for basic mathematical operations.
  • Assignment Operators: Used to assign values to variables.
  • Comparison Operators: Used to compare two values.
  • Logical Operators: Used to combine conditional statements.
  • Identity Operators: Used to compare the memory locations of objects.
  • Membership Operators: Used to check if a sequence is present in an object.
  • Bitwise Operators: Used to perform bitwise operations on integers.

Understanding these operators and their precedence will help you write more effective and efficient Python code.

Arithmetic Operators

Arithmetic operators are used for basic mathematical operations such as addition, subtraction, multiplication, division, modulus, exponentiation, and floor division.

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division5 / 3 = 1.6667
%Modulus5 % 3 = 2
**Exponentiation5 ** 3 = 125
//Floor Division5 // 3 = 1
arithmetic_operations.py
1a = 5
2b = 3
3
4print("Addition:", a + b)
5print("Subtraction:", a - b)
6print("Multiplication:", a * b)
7print("Division:", a / b)
8print("Modulus:", a % b)
9print("Exponentiation:", a ** b)
10print("Floor Division:", a // b)
Output
Addition: 8
Subtraction: 2
Multiplication: 15
Division: 1.6666666666666667
Modulus: 2
Exponentiation: 125
Floor Division: 1

Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations and then assign the result back to the variable.

OperatorExampleEquivalent To
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
**=x **= 3x = x ** 3
//=x //= 3x = x // 3
assignment_operations.py
1x = 5
2print("Initial value of x:", x)
3
4x += 3
5print("After x += 3:", x)
6
7x -= 2
8print("After x -= 2:", x)
9
10x *= 4
11print("After x *= 4:", x)
12
13x /= 2
14print("After x /= 2:", x)
15
16x %= 5
17print("After x %= 5:", x)
18
19x **= 3
20print("After x **= 3:", x)
21
22x //= 2
23print("After x //= 2:", x)
Output
Initial value of x: 5
After x += 3: 8
After x -= 2: 6
After x *= 4: 24
After x /= 2: 12.0
After x %= 5: 2.0
After x **= 3: 8.0
After x //= 2: 4.0

Comparison Operators

Comparison operators are used to compare two values and return a Boolean result (True or False). These operators are essential for decision-making in your programs.

OperatorDescriptionExample
==Equal5 == 3 returns False
!=Not equal5 != 3 returns True
>Greater than5 > 3 returns True
<Less than5 < 3 returns False
>=Greater than or equal5 >= 3 returns True
<=Less than or equal5 <= 3 returns False
comparison_operations.py
1a = 5
2b = 3
3
4print("a == b:", a == b)
5print("a != b:", a != b)
6print("a > b:", a > b)
7print("a < b:", a < b)
8print("a >= b:", a >= b)
9print("a <= b:", a <= b)
Output
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False

Logical Operators

Logical operators are used to combine conditional statements. They return a Boolean result based on the conditions provided.

OperatorDescriptionExample
andReturns True if both statements are true(5 > 3) and (3 < 10) returns True
orReturns True if at least one of the statements is true(5 > 3) or (3 > 10) returns True
notReverse the result, returns False if the result is truenot (5 > 3) returns False
logical_operations.py
1x = 5
2y = 3
3
4print("x > y and y < 10:", x > y and y < 10)
5print("x > y or y > 10:", x > y or y > 10)
6print("not (x > y):", not (x > y))
Output
x > y and y < 10: True
x > y or y > 10: True
not (x > y): False

Identity Operators

Identity operators are used to compare the memory locations of two objects. They check if both variables point to the same object in memory.

OperatorDescriptionExample
isReturns True if both variables point to the same objecta is b returns True if a and b point to the same object
is notReturns True if both variables do not point to the same objecta is not b returns True if a and b do not point to the same object
identity_operations.py
1a = [1, 2, 3]
2b = a
3c = [1, 2, 3]
4
5print("a is b:", a is b)
6print("a is c:", a is c)
7print("a is not c:", a is not c)
Output
a is b: True
a is c: False
a is not c: True

Membership Operators

Membership operators are used to test if a sequence (string, list, tuple, etc.) contains a specified value.

OperatorDescriptionExample
inReturns True if a sequence contains the specified value'a' in 'apple' returns True
not inReturns True if a sequence does not contain the specified value'b' not in 'apple' returns True
membership_operations.py
1text = "Hello, world!"
2
3print("'o' in text:", 'o' in text)
4print("'z' not in text:", 'z' not in text)
5
6numbers = [1, 2, 3, 4, 5]
7print("3 in numbers:", 3 in numbers)
8print("6 not in numbers:", 6 not in numbers)
Output
'o' in text: True
'z' not in text: True
3 in numbers: True
6 not in numbers: True

Bitwise Operators

Bitwise operators are used to perform operations on individual bits of binary numbers. These operators are less commonly used but can be powerful for certain tasks.

OperatorDescriptionExample
&Bitwise AND5 & 3 = 1
|Bitwise OR5 | 3 = 7
^Bitwise XOR5 ^ 3 = 6
~Bitwise NOT~5 = -6
<<Bitwise Left Shift5 << 1 = 10
>>Bitwise Right Shift5 >> 1 = 2
bitwise_operations.py
1a = 5
2b = 3
3
4print("Bitwise AND:", a & b)
5print("Bitwise OR:", a | b)
6print("Bitwise XOR:", a ^ b)
7print("Bitwise NOT:", ~a)
8print("Bitwise Left Shift:", a << 1)
9print("Bitwise Right Shift:", a >> 1)
Output
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Bitwise Left Shift: 10
Bitwise Right Shift: 2

Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before those with lower precedence.

Here is a table of operator precedence in Python, from highest to lowest:

Operator TypeOperators
Parentheses()
Exponentiation**
Bitwise NOT~
Unary plus and minus+x, -x
Multiplication, Division, Floor Division, Modulus*, /, //, %
Addition, Subtraction+, -
Bitwise Shift<<, >>
Bitwise AND&
Bitwise XOR^
Bitwise OR|
Comparison==, !=, >, <, >=, <=
Identityis, is not
Membershipin, not in
Logical NOTnot
Logical ANDand
Logical ORor
operator_precedence.py
1result = 10 + 5 * 2 - 3 ** 2
2print("Result:", result)
Output
Result: 14

Tip

Understanding operator precedence is crucial to ensure that your expressions are evaluated as intended. You can use parentheses () to override the default precedence and control the order of operations.

Practical Example

Let's create a practical example that combines several operators to perform a simple calculation and decision-making process.

practical_example.py
1# Calculate the total cost including tax
2price = 100.0
3tax_rate = 0.08
4
5total_cost = price + (price * tax_rate)
6
7# Check if the total cost is within a budget
8budget = 110.0
9
10if total_cost <= budget:
11 print("Within budget")
12else:
13 print("Over budget")
14
15# Use bitwise operators to check permissions
16read_permission = True
17write_permission = False
18
19has_permissions = read_permission and write_permission
20
21print("Has read and write permissions:", has_permissions)
Output
Within budget
Has read and write permissions: False

Summary

In this tutorial, you learned about various types of operators in Python:

  • Arithmetic Operators: For basic mathematical operations.
  • Assignment Operators: To assign values to variables.
  • Comparison Operators: To compare two values.
  • Logical Operators: To combine conditional statements.
  • Identity Operators: To compare the memory locations of objects.
  • Membership Operators: To check if a sequence is present in an object.
  • Bitwise Operators: To perform bitwise operations on integers.

You also learned about operator precedence, which determines the order in which operators are evaluated in an expression. Understanding these concepts will help you write more effective and efficient Python code.

What's Next?

In the next tutorial, we'll explore control flow statements using the if...else statement. This will allow you to make decisions based on conditions in your programs. Stay tuned!

Terminal
$ python3 practical_example.py
Output
Within budget
Has read and write permissions: False

PreviousPython String FormattingNext Python if...else Statement

Recommended Gear

Python String FormattingPython if...else Statement