Home » Operators in Python

Operators in Python

Operators in Python

Python Operators

Python Operators are symbols that perform operations on variables and values. They are the building blocks of expressions and statements. Here are some commonly used operators:

Arithmetic Operators Python

Arithmetic operators are used to perform arithmetic/mathematical operations.

NameOperatorExample
Addition+a+b
Subtractiona-b
Multiplication*a*b
Division/a/b
Exponential**a**b
Modulus%a%b
Floor Division//a//b

Real-life Examples of Python Operators in Arithmetic

  • Arithmetic: Calculating total cost at a store.
  • Adding the prices of items in a shopping cart to find the total bill.
  • Calculating the total score of a student by adding individual scores in different subjects.
  • Determining the total cost of buying multiple items with the same price by multiplying the price by the quantity.
Examples
  • Addition (+): Adding two numbers together.
2 + 3 = 5
Python
  • Subtraction (-): Finding the difference between two numbers.
5 - 3 = 2
Python
  • Multiplication (*): Multiplying two numbers.
2 * 3 = 6
Python
  • Division (/): Dividing one number by another.
6 / 2 = 3
Python
  • Modulus (%): Finding the remainder after division.
7 % 3 = 1
Python

Assignment Operators in Python

These operators are used to assign values to variables.

NameEvaluated As
=a=b
+=a+=b    or    a=a+b
-=a-=b    or    a=a-b
*=a*=b    or    a=a*b
**=a**=b    or    a=a**b
/*a/=b    or    a=a/b
//=a//=b    or    a=a//b
%=a%=b    or    a=a%b
&=a&=b    or    a=a&b
=
^=a^=b    or    a=a^b
>>=a>>=b    or    a=a>>b
<<=a<<=b    or    a=a<<b

Examples

Assign (=): Assigns a value to a variable.

x = 5
Python

Add and assign (+=): Adds a value to a variable and assigns the result.

x += 2 # Equivalent to x = x + 2
Python

Bitwise Operators in Ptython

Bitwise operators are used to deal with binary operations.

NameOperatorExample
Bitwise ANDBitwise ANDa & b
Bitwise ORBitwise ORa
Bitwise NOTBitwise NOT~a
Bitwise XORBitwise XORa ^ b
Bitwise right shiftBitwise right shifta>>
Bitwise left shiftBitwise left shiftb<<

Comparison Operators in Python

These operators are used to compare values.

NameOperatorExample
Equal==a==b
Not Equal!=a!=b
Less Than<a>b
Greater Than>a<b
Less Than or Equal to<=a>=b
Greater Than or Equal to>=a<=b

Example of Python Comparison Operators

  • Equal to (==): Checks if two values are equal.
2 == 2 # True
Python
  • Not equal to (!=): Checks if two values are not equal.
2 != 3 # True
Python
  • Greater than (>): Checks if one value is greater than another.
5 > 3 # True
Python
  • Less than (<): Checks if one value is less than another.
2 < 4 # True
Python

Identity Operators in Python

NameExampleEvaluated As
isa is bReturns True if a and b are same
is nota is not bReturns True if a and b are not same

Logical Operators in Python

These operators are used to deal with logical operations.

NameOperatorExample
ANDanda=2 and b=3
ORora=2 or b=3
NOTnotNot(a=2 or b=3)

Example of Python Logical Operators

Logical Operators: Combine conditional statements.

  • AND (and): Returns True if both conditions are true.
(2 > 1) and (3 < 5) # True
Python
  • OR (or): Returns True if at least one condition is true.
(2 > 1) or (3 > 5) # True
Python
  • NOT (not): Returns the opposite of the condition.
not (2 > 1) # False
Python

Membership Operators

NameExampleEvaluated As
ina in bReturns True if a is present in given sequence or collection
not ina not in bReturns True if a is not present in given sequence or collection

Python Operators In Precedence

NameOperator
Parenthesis()
Exponential**
Complement, unary plus, unary minus~ , +,  –
Multiply, divide, modulus, floor division*,  /,  %,  //
Addition, subtraction+, –
Left shift and right shift operators<<, >>
Bitwise and&
Bitwise or and xor^,
Comparison operators<, >, >=, <=
Equality operators==, !=
Assignment operators=, %=, /=, //=, -=, +=, *= , **=
Identity operatorsis, is not
Membership operatorsin, not in
Logical operatorsand, or, not

Conclusion

Python operators are fundamental tools for performing various operations in programming. Understanding how to use them effectively is crucial for writing efficient and expressive code. With real-life examples and practice, you can master the use of operators in Python.

Frequently Asked Questions

Q1. What are operators in Python?

Ans: Operators are symbols used to perform operations on variables and values in Python.


Q2. Can I use operators with different data types?

Ans: Yes, Python is flexible with data types, but some operations may not be applicable to certain combinations of types.


Q3. Are there any other types of operators?

Ans: Yes, there are bitwise operators, membership operators, and identity operators, among others.