0% found this document useful (0 votes)
20 views39 pages

Session 3 - Operators - Operands - Types, Examples, Usage

Python

Uploaded by

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

Session 3 - Operators - Operands - Types, Examples, Usage

Python

Uploaded by

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

Python

Operations
Session-03
Python divides the operators in the following groups:

● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Identity operators
● Membership operators
● Bitwise operators
Arithmetic Operators
Python allows us to do all sort of arithmetic operations on numbers. You can do operations
on numbers directly, example:

a = 5+4

or, operations on variables containing numbers

a=5

b=6

c = a+b
Addition of 2 numbers
You can add 2 numbers by using a '+' sign between the 2 numbers that you want to
add.

add = num1 + num2

Example

a=100
b=3

add = a + b

print(add)
Subtraction of 2 numbers

You can subtract 2 numbers by using a '-' sign between the 2 numbers that you want to
subtract.

sub = num1 - num2

Example:

sub = a-b
print(sub)
Multiplication of 2 numbers
You can multiply 2 numbers by using a '*' sign between the 2 numbers that you want to multiply.

mul = num1 * num2

Example

mul = a*b
print(mul)
Division of 2 numbers
You can divide 2 numbers by using a '/' sign between the 2 numbers that you want to divide.

div = num1 / num2

Example :

div= a/b
print(div)
Modulus of 2 numbers
Modulus is the remainder you get when you divide 2 numbers.

You can find the modulus of 2 numbers by using a '%' sign between the 2
numbers.
mod = num1 % num2

Example :

mod = a%b

print(mod)
Floor division of 2 numbers
Floor division gives us the quotient of the division between 2 numbers. You can floor divide 2
numbers by using a '//' sign between the 2 numbers.

quotient = num1//num2

Example :

quotient= a//b

print(quotient)
Exponentiation of 2 numbers
You can do x raised to the power y in python by using a '**' sign between the 2 numbers.
exp = num1 ** num2

Example :

exp= a**b

print(exp)

Note

When you have multiple operations, python operates using the standard PODMAS /
BODMAS rule
Python Assignment Operators
Assignment operators are used to assign values to variables:

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
1) Assign: This operator is used to assign the value of the right side of the expression to the left side
operand.
Syntax:
x = y + z

# Assigning values using


# Assignment Operator

a = 3
b = 5

c = a + b

# Output
print(c)
2) Add and Assign: This operator is used to add the right side operand with the left
side operand and then assigning the result to the left operand.
Syntax:
x += y

a = 3
b = 5

# a = a + b
a += b# a -= b , *= , /= ,%= //=

# Output
print(a)
7) Divide (floor) and Assign: This operator is used to divide the left operand with
the right operand and then assigning the result(floor) to the left operand.
Syntax:
x //= y

a = 3
b = 5

# a = a // b
a //= b

# Output
print(a)

Output :0
8) Exponent and Assign: This operator is used to calculate the exponent(raise
power) value using operands and then assigning the result to the left operand.
Syntax:
x **= y

a = 3
b = 5

# a = a ** b
a **= b

# Output
print(a)

Output :243
9) Bitwise AND and Assign: This operator is used to perform Bitwise AND on both
operands and then assigning the result to the left operand.
Syntax:
x &= y

a = 3
b = 5

# a = a & b
a &= b

# Output
print(a)

Output :1
10) Bitwise OR and Assign: This operator is used to perform Bitwise OR on the
operands and then assigning result to the left operand.
Syntax:
x |= y

a = 3
b = 5

# a = a | b
a |= b

# Output
print(a)

Output :7
11) Bitwise XOR and Assign: This operator is used to perform Bitwise XOR on the
operands and then assigning result to the left operand.
Syntax:
x ^= y

a = 3
b = 5

# a = a ^ b
a ^= b

# Output
print(a)

Output :6
12) Bitwise Right Shift and Assign: This operator is used to perform Bitwise right
shift on the operands and then assigning result to the left operand.
Syntax:
x >>= y

a = 3
b = 5

# a = a >> b
a >>= b

# Output
print(a)

Output :0
Let’s take a number 14.

Binary representation of 14 is 00001110 (for the sake of clarity let’s write it using 8 bit)

14 = (00001110) 2

Then 14 >> 1 will shift the binary sequence by 1 position to the right side.

Like,

Pictorial Explanation
13) Bitwise Left Shift and Assign: This operator is used to perform Bitwise left
shift on the operands and then assigning result to the left operand.
Syntax:
x <<= y

a = 3
b = 5

# a = a << b
a <<= b

# Output
print(a)

Output :96
Bitwise Left shift operator is used to shift the binary sequence to the left side by specified position.

Example
Let’s take a number 14.

Binary representation of 14 is 00001110 (for the sake of clarity let’s write it using 8 bit)

14 = (00001110) 2

Then 14 << 1 will shift the binary sequence 1 position to the left side.

Like,
Python Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example


== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
```
Equal:

x=5
y=3

print(x == y)

# returns False because 5 is not equal to 3


Python Logical Operators
Logical operators are used to combine conditional statements:

Operator Description Example


and Returns True if both statements are true x < 5 and x <
10
or Returns True if one of the statements is true x < 5 or
x<4
not Reverse the result, returns False if the result is true not(x < 5 and x
< 10)
# Python program to demonstrate
# logical and operator

a = 10
b = 10
c = -10

if a > 0 and b > 0:


print("The numbers are greater than 0")

if a > 0 and b > 0 and c > 0:


print("The numbers are greater than 0")
else:
print("Atleast one number is not greater than 0")
# Python program to demonstrate
# logical or operator

a = 10
b = -10
c = 0

if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")

if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
# Python program to demonstrate
# logical not operator

a = 10

if not a:
print("This will return True")

if not (a%3 == 0 or a%5 == 0):


print("10 is not divisible by either 3 or 5")
else:
print("10 is divisible by either 3 or 5")
Python Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if
they are actually the same object, with the same memory location:

Operator Description Example


is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y
Returns True if both variables are the same object
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is z)

# returns True because z is the same object as x

print(x is y)

# returns False because x is not the same object as y, even if they have the same
content

print(x == y)

# to demonstrate the difference betweeen "is" and "==": this comparison returns
True because x is equal to y
Returns True if both variables are not the same object
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is not z)

# returns False because z is the same object as x

print(x is not y)

# returns True because x is not the same object as y, even if they have the same content

print(x != y)

# to demonstrate the difference betweeen "is not" and "!=": this comparison returns
False because x is equal to y
Python Membership Operators
Membership operators are used to test if a sequence is presented in an object:

Operator Description
Example
in Returns True if a sequence with the specified value is present in
the object x in y
not in Returns True if a sequence with the specified value is not present
in the object x not in y
In Returns True if a sequence with the specified value is present in the object

x = ["apple", "banana"]

print("banana" in x)

# returns True because a sequence with the value "banana" is in the list
Not in
Returns True if a sequence with the specified value is not present in the object

x = ["apple", "banana"]

print("pineapple" not in x)

# returns True because a sequence with the value "pineapple" is not in the list
In Class Exercises :

● Nidhi and Shubh are two students in the same grade. They have a total of 3 subjects in the school. After
examination, they want to check whether their total marks are equal or not. So to help them write a
program in python to take marks as input from two students. Print the overall score of both the students.
Also, print "true" if they have an equal score or print "false" on the screen.
● Hint: Take the input for marks and then use the “==“ operator to print the answer.

● Varun is using a stopwatch to calculate his running speed. The stopwatch shows the time taken in
seconds. He wants to convert it into minutes and seconds. Write a program to help him with this.

● For input :
● Enter the number of seconds: 125
● Output :
● >> 2 minutes and 5 seconds
In class exercise

● Varun measures his running speed in kilometers per hour. For a better understanding, he wants to
know his speed in meters per second as well. But he is facing a problem in calculating his speed in
meters per second. Write a program for Varun where he can enter the speed in kilometers per hour
and he can get the speed in meters per second.
● Hint: divide the speed value by 3.6
Homework questions
● Nidhi loves to travel to different countries. She is now in a country where the temperature is measured
in Fahrenheit and she is not able to understand it in a better way. To help her in this situation, write a
program to convert temperature from Fahrenheit to celsius.
● Hint: (0°C × 9/5) + 32 = 32°F

● In a school, the teacher asks students to tell whether the given number on the board is odd or even.
Students were taking a lot of time to find out the answer. But Chirag realized that even and odd
depends only on the last digit of the number. So write a program in python to take a number as input
and then print only the last digit of the number. Also, discuss whether the given number is odd or even.
If given number is even then print True on the screen and if the given number is odd then print False
on the screen.
● Hint: Take the number as input and then use “%10” to extract the last digit of the given number.
Homework questions

● Shubham and his two more friends decided to share their chocolates equally among them. But they
couldn't divide chocolates equally as they need a few more chocolates. Write a program to take three
inputs for the number of chocolates and then print how many more chocolates they need to buy to
divide it equally.
Thank you

End of session - 3

You might also like