0% found this document useful (0 votes)
14 views28 pages

Operators in Python (Part-1)

The document provides an overview of operators in Python, including their classifications such as arithmetic, relational, assignment, logical, membership, and identity operators. It explains the use of arithmetic operators with examples, discusses operator precedence and associativity, and introduces type conversion methods including implicit and explicit conversions. Additionally, it includes exercises and questions to reinforce understanding of the concepts presented.

Uploaded by

manya.mangal123
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)
14 views28 pages

Operators in Python (Part-1)

The document provides an overview of operators in Python, including their classifications such as arithmetic, relational, assignment, logical, membership, and identity operators. It explains the use of arithmetic operators with examples, discusses operator precedence and associativity, and introduces type conversion methods including implicit and explicit conversions. Additionally, it includes exercises and questions to reinforce understanding of the concepts presented.

Uploaded by

manya.mangal123
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/ 28

OPERATORS IN PYTHON

XI – COMPUTER SCIENCE

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


WHAT ARE OPERATORS

Consider the
Operators are the expression
constructs which 4+5=9
can manipulate the 4 and 5 are called
value of operands. operands and + is
called operator
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
PYTHON OPEREATOR CLASSIFICATION

ARITHMETIC RELATIONAL ASSIGNMEN LOGICAL MEMBERSHI IDENTITY


OPERATORS OPERATORS T OPERATORS P OPERATORS
OPERATORS OPERATORS

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


PYTHON ARITHMETIC OPERATORS

* / //
+ -
MULTIPLICATI FLOAT INTEGER
ADDITION SUBTRACTION ON DIVISION DIVISION

+
% ** -
Unary
REMAINDER EXPONENT Unary Minus
Operator +

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


WORKABLE EXAMPLE OF ARITHMETIC OPERATORS
a = 9
b = 4
add = a + b
sub = a - b
mul = a * b
div1 = a / b Output:
div2 = a // b
mod = a % b 13
p = a ** b 5
print(add) 36
print(sub) 2.25
print(mul) 2
print(div1)
1
print(div2)
6561
print(mod)
print(p)
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR -6561
print(-p)
COMPLEX EXPRESSION WITH ARITHMETIC OPERATORS

For an expression - It follows PEMDAS to evaluate


2+9**3*4 Precedence
expressions.
table of
Python uses Arithmetic (Parenthesis, Exponentiation,
Precedence Table to Multiplication, Division,
Operators Addition and Subtraction)
resolve the
operators

** (Exponenet) + - (Unary) *,/,//,% + - (Binary)

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


EXAMPLE OF PRECEDENCE

Example 1 Example 3
a=20
#Precedence of * is more
x=7+3*2 b=2
#Precedence of ** is more than unary -
print(x) print(-a**b)
#Output : 13 #Output : -400

Example 2 Example 4
a = 20 a=20
b = 10 b=2
#Precedence of ** is more than /
print(400**1/2)
c = 15
d=5
e=0 #Precedence of ** is more
print(a/b**e) #Output : -200.0
#Output: 20.0 (Division
DEEPSHIKHA SETHI____CLASS XI __________AISresults
MAYUR VIHARin
float type)
TO DO ACTIVITY (FIND THE OUTPUT)

a=200
b=20
c=10
d=0 Output:
e=4
f=15.0 2.0
print(a/b//e) 201
print(a+b**d) 52.5
print((a+b-c)/(d+e)) 52
print((a+b-c)//(d+e)) 13.0
print(a//f) -3000.0
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
print(-a*f) -202.0
print(-a-b/c)
OPERATORS WITH SAME PRECEDENCE

 What happens if the expressions are

9/5*4
9//5/5
9**4**2
9+5-2

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


ASSOCIATIVITY

 Associativity is the order in which an expression is


evaluated that has multiple operators of the same
precedence.

#Order is left to right # Output 3


print(5 * 2 // 3)
# //
print(5 * (2 Brackets
3)) have higher precedence #Output 0

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


RIGHT TO LEFT ASSOCIATIVITY

print(2 ** 3 ** 2)

# ** operator has Right to left associativity


#Output 512

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


PROGRAMMING EXERCISE

FIND THE AREA OF A TRIANGLE


USING HERON’S FORMULA
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
PROGRAMMING EXERCISE

FIND THE ROOTS OF


QUADRATIC EQUATION
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
PROGRAMMING EXERCISE

FIND THE IDEAL DENOMINATION OF


CURRENCY
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
LET’S REVISE

1) The value of the expressions 4/(3*(2-1)) and 4/3*(2-1) is


the same. True or False
2) Evaluate the expression given below if A = 16 and B =
15.
A % B // A
3) What is the value of the following expression?
2+4.00, 2**4.0
4) What will be the output of the following Python
expression?
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR

print(4.00/(2.0+2.0))
LET’S REVISE

5) What will be the value of X in the following Python


expression?
X = 2+9*((3*12)-8)/10

6) What will be the output of the following Python


expression?
24//6%3, 24//4//2

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


The process of
converting the value
TYPE of one data type
CONVERSION (integer, string, float,
etc.) to another data
PYTHON type is called type
conversion.

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


1 2
IMPLICIT TYPE EXPLICITY TYPE TYPES OF TYPE
CONVERSION CONVERSION CONVERSION
(TYPE (TYPE CASTING)
PROMOTION or
Coercion)

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


IMPLICITY TYPE CONVERSION

In Implicit type conversion, Python automatically


converts one data type to another data type.

This process doesn't need any user involvement.

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


IMPLICIT TYPE CONVERSION (EXAMPLE)

num_int = 123
num_flo = 1.23 OUTPUT:
num_bool=True
a=num_int+num_flo
124.23 <class 'float'>
b=num_int+num_bool
print(a,type(a)) 124 <class 'int'>
print(b,type(b))
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
NOT ALL TYPES ARE PROMOTED
OUTPUT : TypeError

num_int = 123 Data type of num_int: <class 'int'>


num_str = "456" Data type of num_str: <class 'str'>
Traceback (most recent call last):
File "main.py", line 7, in <module>
print(num_int+num_str)
print("Data type of TypeError: unsupported operand type(s)
num_int:",type(num_int)) for +: 'int' and 'str'
print("Data type of
num_str:",type(num_str))

print(num_int+num_str)
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
We use the
predefined
In Explicit Type Conversion,
functions
users convert the data type
like int(), float(), s
of an object to required
tr(), etc to
data type.
perform explicit
type conversion.

TYPE CASTING / EXPLICIT TYPE CONVERSION


DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
TYPE CASTING EXAMPLES

Taking numeric input from user


R=int(input(‘Enter the number’)) # Explicit
conversion from string to integer

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


TYPE CASTING EXAMPLES

num_int = 123 Output:


num_str = "456"
print(num_int+int(num_str)) 579
print(str(num_int)+num_str) 123456

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


TO DO ACTIVITY (TYPE CASTING) –FIND THE OUTPUT

a=122.0
Output:
b=22
c=a//b
5.0
print(c)
5.0 3
c=str(c)
print(c,len(c))

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


TO DO ACTIVITY ( ANSWER THE QUESTIONS)

What will be the value of the following Python expression?


float(4+int(2.39)%2)

Which of the following expressions is an example of type


conversion?
a) 4.0 + float(3)
b) 5.3 + 6.3
c) 5.0 + 3
d) 3 + 7
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR
TO DO ACTIVITY ( ANSWER THE QUESTIONS)

Which of the following expressions results in an error?


a) float(‘10’)
b) int(‘10’)
c) float(’10.8’)
d) int(’10.8’)

DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR


RECAP
 What are Operators
 Type of operators
 Arithmetic operator consists of +,-,*,%,/,//,**
 Precedence of operators with PEDMAS rule
 Associativity is the order in which an expression is evaluated that
has multiple operators of the same precedence.
 Type Conversion: The process of converting the value of one data
type (integer, string, float, etc.) to another data type.
 Two types of conversions: Implicit(Type promotion) and Explicit (Type
Casting)
DEEPSHIKHA SETHI____CLASS XI __________AIS MAYUR VIHAR

You might also like