Programmer Coding

Operators in Python

Python Operators: An Overview

Do you find yourself wondering what to do with Python programming in particular all the time? Have you been attempting to comprehend concepts related to operators inside the Python Certification training software language, but you’ve been overwhelmed by all of its peculiarities? Have no fear—we’ve got your back. We can provide Python operators, operators in Python styles, operators in Python, and operators in Python priority in this Python tutorial.

What are Operators in Python?

Operators are special characters or phrases that operate on values and Python variables in the programming language Python. They provide the foundation for expressions that are used to perform calculations and alter statistics. There are multiple operators in Python, each having a distinct purpose.

Types of Python Operators

Python language supports various types of operators, which are:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Assignment Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

Python Arithmetic Operators

Python math operators are typically used to perform mathematical operations including addition, subtraction, multiplication, and division.

they are compatible with integers, variables, and expressions.

further to the same old arithmetic operators, there are operators for modulus, exponentiation, and ground division.

Operator Description Example Result
+ Addition 5 + 3 8
Subtraction 5 – 3 2
* Multiplication 5 * 3 15
/ Division 6 / 3 2.0
% Modulus 6 % 4 2
** Exponentiation 2 ** 3 8
// Floor Division 7 // 3 2

Example 

a = 50

b = 80

# Addition

print("a + b:", a + b)

# Subtraction

print("a - b:", a - b)

# Multiplication

print("a * b:", a * b)

# Division

print("a / b:", a / b)

# Modulus

print("a % b:", a % b)

# Exponentiation

print("a ** b:", a ** b)

# Floor Division

print("a // b:", a // b)
Output

a + b: 130

a – b: -30

a * b: 4000

a / b: 0.625

a % b: 50

a ** b: 78886090522101180541172856528278622967320643510902300477027893066406250000000000000000000000000000000000

a // b: 0

Python Comparison Operators

  • To compare two values, Python comparison operators are needed.
  • Based on the comparison, they produce a Boolean value (True or False).
Operator Description Example Result
== Equal 5 == 5 True
!= Not equal 5 != 3 True
> Greater than 5 > 3 True
< Less than 5 < 3 False
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 5 <= 3 False

Example

# Define two variables

a = 10

b = 20

# Equal to (==)

print("a == b:", a == b)

# Not equal to (!=)

print("a != b:", a != b)

# Greater than (>)

print("a > b:", a > b)

# Less than (<)

print("a < b:", a < b)

# Greater than or equal to (>=)

print("a >= b:", a >= b)

# Less than or equal to (<=)

print("a <= b:", a <= b)

Output

a == b: False

a != b: True

a > b: True

a < b: False

a >= b: True

a <= b: False

Python Assignment Operators

Python mission operators are used to assign values to variables in Python.

The unmarried identical image (=) is the maximum essential mission operator.

It assigns the cost at the operator’s right facet to the variable at the operator’s left aspect.

Operator Example Equivalent to
+= x += 3 x = x + 3
-= y -= 2 y = y – 2
*= z *= 4 z = z * 4
/= w /= 5 w = w / 5
%= m %= 2 m = m % 2
**= n **= 3 n = n ** 3
//= p //= 2 p = p // 2

 

Example

# Assign

a = 10

print("a after assignment:", a)

# Add and assign

a += 5

print("a after add and assign:", a)

# Subtract and assign

a -= 5

print("a after subtract and assign:", a)

# Multiply and assign

a *= 5

print("a after multiply and assign:", a)

# Divide and assign

a /= 5

print("a after divide and assign:", a)

# Modulus and assign

a %= 3

print("a after modulus and assign:", a)

# Floor divide and assign

a //= 2

print("a after floor divide and assign:", a)

# Exponentiate and assign

a **= 3

print("a after exponentiate and assign:", a)

Output

a after assignment: 10

a after add and assign: 15

a after subtract and assign: 10

a after multiply and assign: 50

a after divide and assign: 10.0

a after modulus and assign: 1.0

a after floor divide and assign: 0.0

a after exponentiate and assign: 0.0

Python Bitwise Operators

Python bitwise operators execute operations on character bits of binary integers.

They paintings with integer binary representations, acting logical operations on each bit place.

Python consists of diverse bitwise operators, such as AND (&), OR (|), now not (), XOR (), left shift (), and proper shift (>>).

Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise NOT ~a
<< Left Shift a << n
>> Right Shift a >> n

Example

# Define two variables

a = 60  # Binary: 0011 1100

b = 13  # Binary: 0000 1101

# Bitwise AND (&)

print("a & b:", a & b)  # Output: 12 (Binary: 0000 1100)

# Bitwise OR (|)

print("a | b:", a | b)  # Output: 61 (Binary: 0011 1101)

# Bitwise XOR (^)

print("a ^ b:", a ^ b)  # Output: 49 (Binary: 0011 0001

# Bitwise NOT (~)

print("~a:", ~a)        # Output: -61

# Bitwise Left Shift (<<)

n = 2

print("a << n:", a << n)  # Output: 240 (Binary: 1111 0000)

# Bitwise Right Shift (>>)

print("a >> n:", a >> n)  # Output: 15 (Binary: 0000 1111)

Python Logical Operators

Python logical operators are used to compose Boolean expressions and evaluate their truth values.

they may be required for the advent of conditional statements in addition to for dealing with the glide of execution in packages.

Python has three simple logical operators: AND, OR, and not.

Operator Description Example
and Logical AND x and y
or Logical OR x or y
not Logical NOT not x

Example

# Define boolean variables

a = True

b = False

# Logical AND (and)

print("a and b:", a and b)  

# Logical OR (or)

print("a or b:", a or b)    

# Logical NOT (not)

print("not a:", not a)

Python Membership Operators

Python club operators are used to determine whether or no longer a positive cost occurs within a sequence.

They make it easy to determine the membership of factors in numerous information structures along with lists, tuples, sets, and strings.

Python has two primary membership operators: the in and not in operators.

Example

# Define the sequence

friends = "Ramkrishna Vishnu Singh Shubham Kashyap Mausam Mishra Satyam Keshri Akash Raz"

# Check if "Vishnu" is present in the sequence

print("Vishnu" in friends)      

# Check if "John" is present in the sequence

print("John" in friends)     

# Check if "Rohan" is not present in the sequence

print("Rohan" not in friends)  

# Check if " Satyam Keshri " is not present in the sequence

print("Satyam Keshri"  in friends)  

# Check if " Jitendra Sharma " is not present in the sequence

print("Jitendra Sharma" not in friends)

Output

True

False

True

True

True

Python Identity Operators

Python identification operators are used to compare  items’ memory addresses as opposed to their values.

If the two gadgets refer to the equal reminiscence address, they compare to authentic; in any other case, they evaluate to false.

Python includes  identification operators: the is and isn’t operators.

Example

# Define the list of friends

friends = ["Ramkrishna", "Vishnu Singh", "Shubham", "Kashyap", "Mausam Mishra", "Satyam", "Keshri", "Akash", "Raz"]

# Create a new list referencing the same object as friends

friends_copy = friends

# Check if friends and friends_copy refer to the same object

print("friends is friends_copy:", friends is friends_copy) 

# Create another list with the same values as friends

friends_new = ["Ramkrishna", "Vishnu Singh", "Shubham", "Kashyap", "Mausam Mishra", "Satyam", "Keshri", "Akash", "Raz"]

# Check if friends and friends_new have the same values

print("friends == friends_new:", friends == friends_new) 

# Check if friends and friends_new refer to different objects

print("friends is friends_new:", friends is friends_new)

Output

friends is friends_copy: True

friends == friends_new: True

friends is friends_new: False

Python Operators Precedence

An expression in python includes variables, operators, values, etc. while the Python interpreter encounters any expression containing numerous operations, all operators get evaluated consistent with an ordered hierarchy, referred to as operator precedence.

Operator Description
Parentheses ()
Exponentiation **
Unary plus, minus +x, -x
Multiplication, division, modulo *, /, //, %
Addition, subtraction +, –
Bitwise shifts <<, >>
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Comparison ==, !=, >, >=, <, <=
Logical NOT not
Logical AND and
Logical OR or
Assignment =, +=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, <<=, >>=

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top