Back to Blog
Jan 18, 20233 min read

Operators in Python

Python
Operators in Python

Operators in Python are special symbols or characters that perform specific operations on one or more operands (values or variables). They are used to perform operations such as arithmetic calculations, comparisons, and logical evaluations.

There are several types of operators in Python, including:

  1. Arithmetic operators: +, -, *, /, %, ** (exponentiation), // (floor division)
  2. Comparison operators: ==, !=, >, <, >=, <=
  3. Logical operators: and, or, not
  4. Assignment operators: =, +=, -=, *=, /=, %=, **=, //=
  5. Membership operators: in, not in
  6. Identity operators: is, is not

For example, the + operator is used to add two numbers, the == operator is used to check if two values are equal, and the and operator is used to check if both of the two conditions are true.

It’s important to note that operators have different precedence levels, meaning that some operators are executed before others. Parentheses can be used to change the order of precedence.

For example:

x = 5
y = 3

Arithmetic operators:

# Arithmetic operators
print(x + y) # 8
print(x - y) # 2
print(x * y) # 15
print(x / y) # 1.666666...
print(x % y) # 2
print(x ** y) # 125
print(x // y) # 1

Comparison operators:

# Comparison operators
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False

Logical operators:

# Logical operators
print(x > 0 and y > 0) # True
print(x > 0 or y > 0) # True
print(not(x > 0)) # False

Assignment operators:

# Assignment operators
x += y  # x = x + y
print(x) # 8
x -= y  # x = x - y
print(x) # 5
x *= y  # x = x * y
print(x) # 15
x /= y  # x = x / y
print(x) # 5.0
x %= y  # x = x % y
print(x) # 2.0
x **= y  # x = x ** y
print(x) # 8.0
x //= y  # x = x // y
print(x) # 2.0

Membership operators:

# Membership operators
numbers = [1, 2, 3, 4, 5]
print(x in numbers) # True
print(x not in numbers) # False

Identity operators:

# Identity operators
y = x
print(x is y) # True
print(x is not y) # False

Note that the above examples are just a few examples of the usage of operators. There are many more ways to use them in your code.

If you Want to Learn Python, You can watch my ultimate Python Course on My Youtube Channel.

You can join there as well to share your Queries and suggestions. Facebook Facebook Group: https://web.facebook.com/groups/890525732087988/?mibextid=HsNCOg

Thanks For Reading.

You can Also Follow Me on My Social Media Platforms:

  1. Facebook
  2. Youtube
  3. Twitter & Instagram
  4. GitHub & Replit
  5. Upwork

💬 Got questions? Ask me anything!