0% found this document useful (0 votes)
16 views14 pages

4_Operators

The document provides an overview of various types of operators in Python, including arithmetic, relational, assignment, bitwise, logical, membership, and identity operators. Each operator type is explained with examples of their functions and usage in Python programming. Additionally, it highlights the distinction between the equality operator (==) and the identity operator (is).

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

4_Operators

The document provides an overview of various types of operators in Python, including arithmetic, relational, assignment, bitwise, logical, membership, and identity operators. Each operator type is explained with examples of their functions and usage in Python programming. Additionally, it highlights the distinction between the equality operator (==) and the identity operator (is).

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Operators in Python

Learning objective
• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Bitwise Operators
• Logical Operators
• Membership Operators
• Identity Operators
Operators
• Operators are special symbols in Python that carry out arithmetic
or logical computation.

• The value that the operator operates on is called the operand.

• Python language supports the following types of operators.


Arithmetic Operators
• These Operators are utilised to complete mathematical
operations such as addition, division, multiplication, etc.

• They can be worked on the essential data types including


numerical, Integers etc.

+, -, *, /, %, **, //
Relational Operators
• Relational operators are symbols that perform operations on data
and return a result as true or false depending on the comparison
conditions.

• Relational Operators are those that discover relationship between


the two operands provided to them.

• Relational operators or comparison operators are used to


compare between values.
>, <, >=, <=, ==, !=
Assignment Operators
• Assignment Operators are those that help in assigning a value to
the variable.

• var1 = 5 is a simple assignment operator that assigns the value 5


on the right to the variable var1 on the left.

• There are various compound operators in Python like a += 5 that


adds to the variable and later assigns the same.

• It is equivalent to var1 = var1 + 5.


Assignment Operators
Bitwise Operators
• Bitwise operator works on bits and performs bit by bit operation.

• Assume if a = 60; and b = 13; Now in binary format they will be as


follows:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
~a = 1100 0011
Logical Operators
• Logical Operators are utilised to complete Boolean tasks like
AND, OR etc.
• Logical Operators are used to perform certain logical operations
on values and variables.
• These are the special reserved keywords that carry out some
logical computations.
• The value the operator operates on is known as Operand.
• In Python, they are used on conditional statements (either True or
False), and as a result, they return Boolean only (True or False).
• They are used to combine conditional statements
Membership Operators
• Python’s membership operators test for membership in a
sequence, such as strings, lists, tuples, dictionary, set.
• There are two membership operators:
• in :Evaluates to true if it finds a variable in the specified sequence
and false otherwise.
• not in :Evaluates to true if it does not finds a variable in the
specified sequence and false otherwise.
Identity Operators
• Identity operators compare the memory locations of two objects
to see if they are the same object.
• There are two Identity operators
• is : Evaluates to true if the variables on either side of the operator
point to the same object and false otherwise.
• is not :Evaluates to false if the variables on either side of the
operator point to the same object and true otherwise.
Identity Operators

is Operator is not Operator

x = [10,15,30] x = [10,15,30]
y=x y=x
z = [10,15,30] z = [10,15,30]
print(x is y) print(x is not y)
print(x is z) print(x is not z)

print("Memory address x:", id(x))


print("Memory address y:", id(y))
print("Memory address z:", id(z))
How is == operator different from is operator?
• The == relational operator checks if the values of two variables are
equal, while is identity operator checks if they refer to the same object.

x = [10,20,30]
y = [10,20,30]
print(x == y) # same values
print(x is y) # different objects
print("Memory address x:", id(x))
print("Memory address y:", id(y))
You have Learnt:
• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Bitwise Operators
• Logical Operators
• Membership Operators
• Identity Operators

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy