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

lecture 8

The document provides an overview of various operators in Python, including comparison, assignment, logical, bitwise, membership, and identity operators. It includes examples demonstrating the usage and output of these operators. Each operator is explained with its function and corresponding code snippets to illustrate their application.

Uploaded by

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

lecture 8

The document provides an overview of various operators in Python, including comparison, assignment, logical, bitwise, membership, and identity operators. It includes examples demonstrating the usage and output of these operators. Each operator is explained with its function and corresponding code snippets to illustrate their application.

Uploaded by

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

Example of comparison operators

Examples Output
a=10
b=5
print("a>b=>",a>b) a>b=> True
print("a>b=>",a<b) a>b=> False
print("a==b=>",a==b) a==b=> False
print("a!=b=>",a!=b) a!=b=> True
print("a>=b=>",a<=b) a>=b=> False
print("a>=b=>",a>=b) a>=b=> True
Assignment Operators

Assignment operators
are used in Python to
assign values to
variables ..
Operator Description Examples
= Assigns values from right side c = a + b assigns value of
operands to left side a + b into c
operand .
+= Add AND It adds right operand to the left c += a is equivalent to c =
operand and assign the result to c+a
left operand.
-= Subtract AND It subtracts right operand from c -= a is equivalent to c =
the left operand and assign the c-a
result to left operand .
*= Multiply AND It multiplies right operand with c *= a is equivalent to c =
the left operand and assign the c*a
result to left operand.
/= Divide AND It divides left operand with the c /= a is equivalent to c =
right operand and assign the c / ac /= a is equivalent
result to left operand. to c = c / a
%= Modulus AND It takes modulus using two c %= a is equivalent to c =
operands and assign the result to c%a
left operand.
Operator Description Examples

**= Exponent AND Performs exponential (power) c **= a is equivalent to c


calculation on operators and = c ** a
assign value to the left
operand.

//= Floor Division It performs floor division on c //= a is equivalent to c


operators and assign value to = c // a
the left operand.
Examples of Assignment operator.

Examples Output
a = 21
b = 10
c=0
c=a+b
print("Line 1 - Value of c is ", c) Line 1 - Value of c is 31
c += a Line 2 - Value of c is 52
print("Line 2 - Value of c is ", c) Line 3 - Value of c is 1092
c *= a
print("Line 3 - Value of c is ", c)
c /= a
Example Output

print("Line 4 - Value of c is ", c) Line 4 - Value of c is 52.0

c=2

c %= a

print("Line 5 - Value of c is ", c) Line 5 - Value of c is 2

c **= a

print("Line 6 - Value of c is ", c) Line 6 - Value of c is 2097152

c //= a

print("Line 7 - Value of c is ", c) Line 7 - Value of c is 99864


Logical operators

Logical operators are the


and, or, not operators.
Operator Meaning Examples
And True if both the operands are true. X and y
Or True if either the operands are X and y
true.
not True if operand is false Not x

Example Output
a = True
b = False
print('a and b is',a and b) x and y is False
print('a or b is',a or b) x or y is True
print('not a is',not a) not x is False
Bitwise Operators:

A bitwise operation operates on one or


more bit patterns at the level of
individual bits.
 Example: Let x = 10 (0000 1010 in
binary) and y = 4 (0000 0100 in binary)
Operator Meaning Example

& Bitwise AND X & y=0 (0000 0000)

| Bitwise OR X | y = 14 (0000 1110)

~ Bitwise NOT ~x=-11 (1111 0101)

^ Bitwise XOR X ^ y = 14 (0000 1110)

>> Bitwise right shift X>>2 =2 (0000 0010)

<< Bitwise left shift X<< 2 = 40 (0010 1000)


Examples Output

a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c=0
c = a & b; # 12 = 0000 1100
print "Line 1 - Value of c is ", c Line 1 - Value of c is 12
c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c Line 2 - Value of c is 61
c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c Line 3 - Value of c is 49
c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c Line 4 - Value of c is -61
c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c Line 5 - Value of c is 240
c = a >> 2; # 15 = 0000 1111
print "Line 6 - Value of c is ", c Line 6 - Value of c is 15
Membership Operators

 Evaluates to find a value or a variable


is in the specified sequence of string,
list, tuple, dictionary or not.
 Let, x=[5,3,6,4,1]. To check particular
item in list or not, in and not in
operators are used.
Operator Meaning Example
In True if value/ variable is found in 5 in x
sequence.
Not in True if value/ variable is not found 5 not in x
in sequence

Example: x=[5,3,6,4,1]
>>> 5 in x
True
>>> 5 not in x
False
Identity Operator

They are used to check if


two values (or variables) are
located on the same part of
the memory.
Operator Meaning Example

Is True if operands are identical( refer to X is true.


the same object)

Is not True if operands are not identical( X is not true.


refer to the same object)
 Example Output
False
True

 x=5
 y=5
 x2 = 'Hello’
 y2 = 'Hello’
 print(x1 is not y1)
 print(x2 is y2)

You might also like