0% found this document useful (0 votes)
13 views

Unit 1 Lesson 10

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

Unit 1 Lesson 10

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

Unit 1(Lesson10)

Assignment Operators, Bitwise Operators


Assignment Operator
• An assignment operator assigns the value on the right side of the
assignment operator to the variable on the left side.
• The right side of the operator can be an operand or an expression
that results in a value.
• a = 52
Assignment Operator
Python programming language supports the following assignment operators:
= Assigns values from right side operands c = a + b assigns value of a + b into c
to left side operand

+= Adds right operand to the left operand and c += a, equivalent to c = c + a


assign the result to left operand

-= Subtracts right operand from the left c -= a, equivalent to c = c - a


operand and assign the result to left operand

*= Multiplies right operand with the left and c *= a, equivalent to c = c * a


assign the result to left opera
which of the following is correct output for
given expression?
x=20

x*=5 will print ?

a)20
b)100
c)5
d None of these
/= Divides left operand with the right and c /= a, equivalent to c = c / a
assign the result to left operand

**= Performs exponential calculation on left and right operand c **= a, equivalent to c = c ** a
and assign the result to left operand

%= Performs modulo division of left and right operands and c %= a, equivalent to c = c % a


assign the result to left operand

//= Performs floor division on left and right operands and c //= a, equivalent to c = c // a
assign the result to left operand
Python allows simple syntax for assigning same value to multiple variables as
follows (chained assignment):

x=y=z=12
12 is assigned to x,y and z

Similarly, you can assign multiple values to multiple variables in a single


statement:
a,b,c=12,45.67,”naman”
A will get value 12,b will get 45.67 and c will get “naman”
Bitwise operators
• 1) Bitwise AND operator:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)

a & b = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise operators
• 2) Bitwise OR operator:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)

a | b = 1010
|
0100
= 1110
= 14 (Decimal)
Bitwise operators
• Bitwise XOR:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)

a ^ b = 1010
^
0100
= 1110
= 14 (Decimal)
Bitwise shift operator:

• These operators are used to shift the bits of a number left or right
thereby multiplying or dividing the number by two respectively. They
can be used when we have to multiply or divide a number by two.
Bitwise operators
• Bitwise right shift operator: Shifts the bits of the number to the right
and fills 0 on voids left as a result.

Example 1:
a = 10 = 0000 1010 (Binary)
a >> 1 = 0000 0101 = 5
Bitwise operators
• Bitwise left shift operator: Shifts the bits of the number to the left and
fills 0 on voids right as a result.

Example 1:
a = 5 = 0000 0101 (Binary)
a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20
Problem:

Take two integers x and y as inputs from the console using input() function. For
each bitwise operator ( >> , <<, &, |, ~, and ^ ), print to the console, the result of
applying these operators on the two input integers as shown in the example.

Sample Input and Output:

Enter an integer value: 52


Enter an integer value: 20
52 >> 20 is ?
52 << 20 is ?
52 & 20 is ?
52 | 20 is ?
~ 52 is ?
52 ^ 20 is ?
Solution:

Enter an integer value: 52


Enter an integer value: 20
52 >> 20 is 0
52 << 20 is 54525952
52 & 20 is 20
52 | 20 is 52
~ 52 is -53
52 ^ 20 is 32
Problem:

Enter an integer value: 12


Enter an integer value: 3
12 >> 3 is ?
12 << 3 is ?
Solution:
Enter an integer value: 12
Enter an integer value: 3
12 >> 3 is 1
12 << 3 is 96

You might also like