Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Unit-1_PythonOperators

The document provides an overview of Python operators, categorizing them into various types such as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each type is explained with examples demonstrating their usage and functionality. Additionally, it discusses operator precedence and associativity, illustrating how expressions are evaluated in Python.

Uploaded by

shantanu270805
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit-1_PythonOperators

The document provides an overview of Python operators, categorizing them into various types such as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each type is explained with examples demonstrating their usage and functionality. Additionally, it discusses operator precedence and associativity, illustrating how expressions are evaluated in Python.

Uploaded by

shantanu270805
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Python Operators

● The operator can be defined as a symbol which is responsible for a


particular operation between two operands.
● Operators are the pillars of a program on which the logic is built in a
specific programming language.
Operators

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

● Arithmetic operators are used to perform arithmetic operations between two


operands.
● It includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder),
//(floor division), and exponent (**) operators.
● Example of Arithmetic operators:-
● a=20
● b=10
● c=a+b
● Print(c)
Python Operators
Comparison operator
● The name itself is explaining that this operator is used to compare different things or
values with one another.
● In Python, the Comparison operator is used to analyze either side of the values and
decides the relation between them.
● Comparison operator is also termed as a relational operator because it explains the
connection between them.
Comparison operator
● Comparison operators are used to comparing the value of the two operands
and returns Boolean true or false accordingly. Example a=10, b=20
Assignment Operators
● The assignment operators are used to assign the value of the right
expression to the left operand. For example: a=10, b=20
Logical Operators
● In Python, Logical operators are used on conditional statements (either True
or False). They perform Logical AND, Logical OR, and Logical NOT
operations.
Bitwise Operators
● The bitwise operators perform bit by bit operation on the values of the two
operands.
Bitwise Operators
It performs logical AND operation on the integer value after converting an integer
to a binary value and gives the result as a decimal value it returns true only if
both operands are true otherwise it returns false
Example of Bitwise AND operators:-
a=7
b=4
c=5
Print(a&b)
Print(a&c)
Print(b&c)
Bitwise Operators
● Bitwise or operators:-
It performs logical OR operation on the integer value after converting integer value
to binary value and gives the result a decimal value . It returns false only if both
operands are true otherwise it returns true.
Example of Bitwise OR operators:-
a=7
b=4
c=5
print(a | b)
print(a | c)
print(b| c)
Bitwise Operators

● Bitwise XOR operators:-


It performs logical XOR operation on the binary value of a integer and gives the
result as a decimal value.
Example of a Bitwise XOR operators:-
a=7
b=4
c=5
print( a ^ c)
print(b ^ c)
Bitwise Operators

● Bitwise 1’s complement ~:-


● It performs 1’s complements operation it invert each bit of binary value and
returns the bitwise negation of a value as a result.
Example of Bitwise 1’st complements:-
a=7
b=4
c=3
print(~a, ~b, ~c)
Bitwise Operators

● Bitwise left-shift << operators:-


The left-shift << operators performs a shifting bit of a value by a given number of the
place and fills 0’s to new positions.
Example of Bitwise left-shift << operators:-

print(4 << 2)
print(5 << 3)
Bitwise Operators

● Bitwise right-shift >>


The left-shift >> operator performs sifting a bit of value to the right by a given
Number of places.

Example of Bitwise right-shift >> operators:-

print(4 >> 2)
print(5 >> 2)
Membership operators

● Membership operators:- python membership operators are used to check for


membership of objects in sequence ,
● such as string , list , tuple. It checks whether the given value or variable is
present in a given sequence.
● If present it will return true else false.
● Python there are two membership operators IN and NOT IN
Membership operators

● IN operator:- it returns a result as true. If it finds a given object in the


sequence. Otherwise it return false.
● Example of IN OPERATORS:-
list =[11,15,21,29,50,70]
num=15
If num in list:
print(“number in present”)
else:
print(“number not in present”)
Membership operators

● NOT IN OPERATOR:-it returns true if the object is not present in a given sequence.
● Otherwise it return false.
● Example of NOT In OPERATOR:-
Tuple =(11,15,21,29,50,70)
num=15
If num not in tuple:
print(“number is present”)
else:
print(“number is not present”)
IDENTITY OPERATORS

● Identity operators check whether the value of two variable is the same or
not.
● This operator is known as reference - quality operators.
● Because the identity operators compares value according to two variable
memory addresses
● Python has 2 identity operators is and is not
IDENTITY OPERATORS

● IS OPERATOR:- this operator return Boolean True or False.


● It return true if the memory address first value is equal to the second value
otherwise it return false.
● Example of is operators:-
x=10
y=11
z=10
print(x is y)
print(x is z)
IDENTITY OPERATORS
● IS NOT OPERATORS: the is not operators return Boolean value either true
or false.
● It return true if the first value is not equal to the second value
● Otherwise it return false.
Example of is not operator:
x=10
y=11
z=10
print(x is not y)
print(x is not z)
Precedence and Associativity of Operators in
Python
Precedence :
Precedence is the order in which different operators in an expression are evaluated.
Operators with higher precedence are executed before those with lower precedence, which
ensures that expressions are processed according to the rules of operator priority.

Associativity:

Associativity defines the order in which operators of the same precedence level are
evaluated in an expression. It determines whether operations are performed from left to
right or right to left.
Example:

1. exp= 100 + 200 / 10 - 3 * 10


print(exp)

2. result = (4 + 6) * (2 ** 3) / 5 - 7 % 3
print(result)

3. result = not (True and False) or (False or True)


print(result)

4. result = 5.0 * 2 + 3.0 ** 2 - 8.0 / 4


print(result)

5. result = 8 * 3 + 2 - 12 // 4 ** 2 // (5 % 3) + 1 - 6 - 2 * 1 + 1 ** 2
print(result)
Answers:

1.90.0
2.15.0
3.True
4.17.0
5.20
Que 6:
Que:7
Que:8

You might also like