---
description: Python Operators are used to perform operations on values and variables. Learn different python operators like Arithmetic, Logical, Comparison, Assignment, Bitwise etc. with example.
title: Operators in Python &#8211; Logical, Arithmetic, Comparison
image: https://www.guru99.com/images/python-operators.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Python Operators are special symbols and keywords that perform operations on values and variables. The examples below explain arithmetic, comparison, assignment, logical, membership, and identity operators, along with operator precedence, using clear and runnable code for each operator type.

- 🧮 **Arithmetic Operators:** Perform calculations such as addition, subtraction, multiplication, division, modulus, and exponentiation on numeric operands.
- ⚖️ **Comparison Operators:** Compare two values and return True or False based on the relation, using symbols like ==, !=, >, and <.
- 📝 **Assignment Operators:** Assign a right-side value to a left-side variable, including compound forms such as += and \*=.
- 🔗 **Logical Operators:** Combine conditional statements using and, or, and not to evaluate Boolean logic.
- 🔍 **Membership and Identity:** Test membership with in and not in, and compare object identity with is and is not.
- 🔢 **Operator Precedence:** Determines the order in which operators evaluate, which parentheses can override for clarity.

Read More

![Operators in Python](https://www.guru99.com/images/python-operators.png)

## What are Operators in Python?

**Operators in Python** are special symbols or keywords used to perform operations on values and variables, which are known as operands. They let you carry out calculations, compare values, assign data, and test conditions. Python provides several types of operators: arithmetic, comparison, assignment, logical, membership, and identity operators. Each operator is represented by a keyword or a special character.

## Arithmetic Operators

Arithmetic Operators perform various arithmetic calculations like addition, subtraction, multiplication, division, %modulus, exponent, etc. There are various methods for arithmetic calculation in Python like you can use the eval function, declare variable & calculate, or call functions.

**Example**: For arithmetic operators we will take simple example of addition where we will add two-digit 4+5=9

```
x= 4
y= 5
print(x + y)
```

Similarly, you can use other arithmetic operators like for multiplication(\*), division (/), substraction (-), etc.

## Comparison Operators

**Comparison Operators In Python** compares the values on either side of the operand and determines the relation between them. It is also referred to as relational operators. Various comparison operators in python are ( ==, != , <>, >,<=, etc.)

**Example**: For comparison operators we will compare the value of x to the value of y and print the result in true or false. Here in example, our value of x = 4 which is smaller than y = 5, so when we print the value as x>y, it actually compares the value of x to y and since it is not correct, it returns false.

```
x = 4
y = 5
print(('x > y  is',x>y))
```

Likewise, you can try other comparison operators (x < y, x==y, x!=y, etc.)

## Python Assignment Operators

**Assignment Operators** in **Python** are used for assigning the value of the right operand to the left operand. Various assignment operators used in Python are (+=, – = , \*=, /= , etc.).

**Example**: Python assignment operators is simply to assign the value, for example

```
num1 = 4
num2 = 5
print(("Line 1 - Value of num1 : ", num1))
print(("Line 2 - Value of num2 : ", num2))
```

**Example of compound assignment operator**

We can also use a compound assignment operator, where you can add, subtract, multiply right operand to left and assign addition (or any other arithmetic function) to the left operand.

- Step 1: Assign value to num1 and num2
- Step 2: Add value of num1 and num2 (4+5=9)
- Step 3: To this result add num1 to the output of Step 2 ( 9+4)
- Step 4: It will print the final result as 13

```
num1 = 4
num2 = 5
res = num1 + num2
res += num1
print(("Line 1 - Result of + is ", res))
```

**Don't Miss:**

- [Python Tutorial for Beginners (Free PDF)](https://www.guru99.com/python-tutorials.html)
- [Python Counter in Collections with Example](https://www.guru99.com/python-counter-collections-example.html)
- [Yield in Python Tutorial: Generator & Yield vs Return Example](https://www.guru99.com/python-yield-return-generator.html)
- [Python map() Function with EXAMPLES](https://www.guru99.com/python-map-function.html)

## Logical Operators

Logical operators in Python are used for [conditional statements](https://www.guru99.com/if-loop-python-conditional-structures.html) are true or false. Logical operators in Python are AND, OR and NOT. For logical operators following condition are applied.

- For AND operator – It returns TRUE if both the operands (right side and left side) are true
- For OR operator- It returns TRUE if either of the operand (right side or left side) is true
- For NOT operator- returns TRUE if operand is false

**Example**: Here in example we get true or false based on the value of a and b

```
a = True
b = False
print(('a and b is',a and b))
print(('a or b is',a or b))
print(('not a is',not a))
```

## Membership Operators

These operators test for membership in a sequence such as lists, strings or tuples. There are two membership operators that are used in Python. (in, not in). It gives the result based on the variable present in specified sequence or string

**Example**: For example here we check whether the value of x=4 and value of y=8 is available in list or not, by using **in** and **not in** operators.

```
x = 4
y = 8
list = [1, 2, 3, 4, 5 ];
if ( x in list ):
   print("Line 1 - x is available in the given list")
else:
   print("Line 1 - x is not available in the given list")
if ( y not in list ):
   print("Line 2 - y is not available in the given list")
else:
   print("Line 2 - y is available in the given list")
```

- Declare the value for x and y
- Declare the value of list
- Use the “in” operator in code with if statement to check the value of x existing in the list and print the result accordingly
- Use the “not in” operator in code with if statement to check the value of y exist in the list and print the result accordingly
- Run the code- When the code run it gives the desired output

## Identity Operators

**Identity Operators in Python** are used to compare the memory location of two objects. The two identity operators used in Python are (is, is not).

- Operator is: It returns true if two variables point the same object and false otherwise
- Operator is not: It returns false if two variables point the same object and true otherwise

Following operands are in decreasing order of precedence.

Operators in the same box evaluate left to right

| Operators (Decreasing order of precedence) | Meaning |
| --- | --- |
| \*\* | Exponent |
| \*, /, //, % | Multiplication, Division, Floor division, Modulus |
| +, – | Addition, Subtraction |
| <= < > >= | Comparison operators |
| = %= /= //= -= += \*= \*\*= | Assignment Operators |
| is is not | Identity operators |
| in not in | Membership operators |
| not or and | Logical operators |

**Example**:

```
x = 20
y = 20
if ( x is y ):
	print("x & y  SAME identity")
y=30
if ( x is not y ):
	print("x & y have DIFFERENT identity")
```

- Declare the value for variable x and y
- Use the operator “is” in code to check if value of x is same as y
- Next we use the operator “is not” in code if value of x is not same as y
- Run the code- The output of the result is as expected

## Operator Precedence

The operator precedence determines which operators need to be evaluated first. To avoid ambiguity in values, precedence operators are necessary. Just like in normal multiplication method, multiplication has a higher precedence than addition. For example in 3+ 4\*5, the answer is 23, to change the order of precedence we use a parentheses (3+4)\*5, now the answer is 35. Precedence operator used in Python are (unary + – \~, \*\*, \* / %, + – , &) etc.

```
v = 4
w = 5
x = 8
y = 2
z = 0
z = (v+w) * x / y;
print("Value of (v+w) * x/ y is ",  z)
```

- Declare the value of variable v,w…z
- Now apply the formula and run the code
- The code will execute and calculate the variable with higher precedence and will give the output

### Python 2 Example

Above examples are Python 3 codes, if you want to use Python 2, please consider following codes

```
#Arithmetic Operators
x= 4
y= 5
print x + y

#Comparison Operators
x = 4
y = 5
print('x > y  is',x>y)

#Assignment Operators
num1 = 4
num2 = 5
print ("Line 1 - Value of num1 : ", num1)
print ("Line 2 - Value of num2 : ", num2)

#compound assignment operator
num1 = 4
num2 = 5
res = num1 + num2
res += num1
print ("Line 1 - Result of + is ", res)

#Logical Operators
a = True
b = False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)

#Membership Operators
x = 4
y = 8
list = [1, 2, 3, 4, 5 ];
if ( x in list ):
   print "Line 1 - x is available in the given list"
else:
   print "Line 1 - x is not available in the given list"
if ( y not in list ):
   print "Line 2 - y is not available in the given list"
else:
   print "Line 2 - y is available in the given list"

#Identity Operators
x = 20
y = 20
if ( x is y ):
	print "x & y  SAME identity"
y=30
if ( x is not y ):
	print "x & y have DIFFERENT identity"

#Operator precedence
v = 4
w = 5
x = 8
y = 2
z = 0
z = (v+w) * x / y;
print "Value of (v+w) * x/ y is ",  z
```

This code is editable. Click Run to Execute  

  

## FAQs

🔢 What is the difference between = and == in Python?

The single = is an assignment operator that stores a value in a variable. The double == is a comparison operator that checks whether two values are equal and returns True or False.

➗ What do the // and \*\* operators do in Python?

The // operator performs floor division, dividing two numbers and rounding the result down to the nearest whole number. The \*\* operator raises a number to a power, so 2 \*\* 3 returns 8.

🔀 What is the difference between logical and bitwise operators?

Logical operators (and, or, not) work on Boolean True or False values. Bitwise operators (&, |, ^, \~, <<, >>) work on the individual bits of integers, performing operations at the binary level.

🧩 What are the main types of operators in Python?

Python provides seven operator categories: arithmetic, comparison (relational), assignment, logical, bitwise, membership, and identity operators. Each type performs a distinct operation on operands such as numbers, variables, or sequences.

🐍 What is the walrus operator (:=) in Python?

The walrus operator :=, added in Python 3.8, assigns a value to a variable as part of a larger expression. It is useful inside while loops and list comprehensions, reducing repeated function calls.

🤖 How can AI help me choose the right Python operator?

AI assistants can read your goal in plain language, suggest the correct operator, and explain the difference between similar ones such as is and ==. They also generate example code you can test immediately.

🧠 Can AI detect operator precedence mistakes in my code?

Yes. AI tools can spot precedence errors, explain why an expression evaluates unexpectedly, and suggest parentheses to fix the order. This helps beginners avoid subtle bugs in arithmetic and logical expressions.

🚀 Can GitHub Copilot and AI tools generate Python operator code automatically?

Yes. GitHub Copilot autocompletes operator expressions from a comment or function name, generating arithmetic, comparison, and logical code. Agentic AI tools go further, refactoring operators across a whole file and flagging precedence issues automatically.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-operators.png","url":"https://www.guru99.com/images/python-operators.png","width":"700","height":"250","caption":"Python Operators","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/python-operators-complete-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/python-operators-complete-tutorial.html","name":"Operators in Python &#8211; Logical, Arithmetic, Comparison"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/python-operators-complete-tutorial.html#webpage","url":"https://www.guru99.com/python-operators-complete-tutorial.html","name":"Operators in Python &#8211; Logical, Arithmetic, Comparison","dateModified":"2026-10-07T13:07:08+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-operators.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/python-operators-complete-tutorial.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Operators in Python &#8211; Logical, Arithmetic, Comparison","description":"Python Operators are used to perform operations on values and variables. Learn different python operators like Arithmetic, Logical, Comparison, Assignment, Bitwise etc. with example.","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-07T13:07:08+05:30","image":{"@id":"https://www.guru99.com/images/python-operators.png"},"copyrightYear":"2026","name":"Operators in Python &#8211; Logical, Arithmetic, Comparison","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between = and == in Python?","acceptedAnswer":{"@type":"Answer","text":"The single = is an assignment operator that stores a value in a variable. The double == is a comparison operator that checks whether two values are equal and returns True or False."}},{"@type":"Question","name":"What do the // and ** operators do in Python?","acceptedAnswer":{"@type":"Answer","text":"The // operator performs floor division, dividing two numbers and rounding the result down to the nearest whole number. The ** operator raises a number to a power, so 2 ** 3 returns 8."}},{"@type":"Question","name":"What is the difference between logical and bitwise operators?","acceptedAnswer":{"@type":"Answer","text":"Logical operators (and, or, not) work on Boolean True or False values. Bitwise operators (&amp;, |, ^, ~, &gt;) work on the individual bits of integers, performing operations at the binary level."}},{"@type":"Question","name":"What are the main types of operators in Python?","acceptedAnswer":{"@type":"Answer","text":"Python provides seven operator categories: arithmetic, comparison (relational), assignment, logical, bitwise, membership, and identity operators. Each type performs a distinct operation on operands such as numbers, variables, or sequences."}},{"@type":"Question","name":"What is the walrus operator (:=) in Python?","acceptedAnswer":{"@type":"Answer","text":"The walrus operator :=, added in Python 3.8, assigns a value to a variable as part of a larger expression. It is useful inside while loops and list comprehensions, reducing repeated function calls."}},{"@type":"Question","name":"How can AI help me choose the right Python operator?","acceptedAnswer":{"@type":"Answer","text":"AI assistants can read your goal in plain language, suggest the correct operator, and explain the difference between similar ones such as is and ==. They also generate example code you can test immediately."}},{"@type":"Question","name":"Can AI detect operator precedence mistakes in my code?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI tools can spot precedence errors, explain why an expression evaluates unexpectedly, and suggest parentheses to fix the order. This helps beginners avoid subtle bugs in arithmetic and logical expressions."}},{"@type":"Question","name":"Can GitHub Copilot and AI tools generate Python operator code automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot autocompletes operator expressions from a comment or function name, generating arithmetic, comparison, and logical code. Agentic AI tools go further, refactoring operators across a whole file and flagging precedence issues automatically."}}]}],"@id":"https://www.guru99.com/python-operators-complete-tutorial.html#schema-1139226","isPartOf":{"@id":"https://www.guru99.com/python-operators-complete-tutorial.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/python-operators-complete-tutorial.html#webpage"}}]}
```
