Search Header Logo
Python operators

Python operators

Assessment

Presentation

Arts

2nd Grade

Practice Problem

Easy

Created by

s tobgyal

Used 25+ times

FREE Resource

25 Slides • 1 Question

1

Python operators

media

2

media

3

Operators
~special symbols or keywords that perform operations on data values (operands).

~The values that an operator acts on are called operands

Example:
a=20# = operator-----a --> operand
b=30
print(a+b)-->Operator???? operand????

Operators

  1. Arithmetic operators

  2. Assignment operators

  3. Comparison operators

  4. Logical operators

  5. Membership operators

  6. Identity operators

  7. Bitwise operators

4

Importance of operators:
​~To express computations and manipulate data.

~To control the flow of a program based on conditions.

~To improve efficiency and performance.

~To enable data manipulation and transformation.

~To enhance code readability and expressiveness.

~To facilitate algorithm design and implementation.

5

~Essential for calculations in programs, from simple sums to complex algorithms.

Arithmetic/mathematical operators

6

​Example:
x=50
y=7

media

7

Example 1:

x=100
y=3
print(x%y)

output?????

% (Modulus):

~ Divides left hand operand by right hand operand and returns remainder

8

​** (Exponent):
~  Performs exponential (power) calculation on operators..

Example:
x=2
y=5
z=x**y #x raised to the power of y
#same as 2*2*2*2*2
print(z)
Output?????

// (floor division):
~  The division of operands where the result is the quotient in which the digits after the decimal point are removed.
# Example:
x=10

y=3

z=x//y

print(z)

9

​ hands on practice......

x = 7

y = 3

SL_No

Expression statement

Answer 

Remarks

1

x + y

 

Sum of x and y

2

x - y

 

 

3

x * y

 

 

4

x / y

 

 

5

x // y

 

 

6

x % y

 

 

7

x** y

 

 

10

Comparison operators:
~Also known as relational operators

~are used to compare two values

~Returns true or false based on the validity of the comparison

media

11

 

x = 7

y = 3

SL_No

Comparison statement

Result

1

x==y

 

2

x!=y

 

3

x>y

 

4

x<y

 

5

x>=y

 

6

x<=y

 

12

media

13

​Assignment Operators:
~used to assign values to variables
~allow us to store and update data in variables efficiently

media

14

Hands on practice:

use x and y to calculate for the given assignment operators:
x=20
y=3

1. x+=y
2.x-=y
3. x*=y
4. x%=y
5. x/=y
6. x//=y
7. x**=y

15

Open Ended

  • Write down the differences between the following operators with an example each:

  1. = and ==

  2. / and //

  3. * and **

16

​ Answer:

= operator

== operator

= is an assignment operator and used to assign value to  a variable.

is an equal operator and is used to check equality between two operands.

/ operator

// operator

is a regular division and returns the remainder as the float data type.

is a floor division and returns the remainder as the int data type.

* operator

** operator

is used for regular multiplication.

is used for represent a number in the power form.

17

are used to combine conditions (True/False values). They return either True or False.

​logical operators

18

 

Operator

Description

Example

Result

and

Returns True if both statements are true

6 > 7 and 6 > 3

False

or

Returns True if one of the statement is true

6 > 7 or 6 > 3

True

not

Reverse the result, returns false if the result is true

not (6 > 7)

True

​Logical Operators Example

19

media

20

media

21

 

x, y, z = 10, 15, 7

x > y

y > z

x > y and
y > z

x > y
or y > z

not x > y

not y > z

 

 

 

 

 

 

​Activity

22


Identity operator:
are used to check whether two objects or values refer to the same object in memory.

  • They evaluate the identity of the operands rather than their values.

  • There are two identity operators:

  • is 

  • is not


Example:
x=20
y=20
print(x is y)

media

23

is not:
~Returns True if both variables are not the same object
Example:

x = 20

y = 20
print(x is not y)


Output????

24

Membership operators:
are used to test whether a value or an object is a member of a sequence or a collection.

  • They evaluate the membership of the operands.

  • in 

  • not in

Example:
x = [2,4,6]

print( 2 in x)

return true....
Example:
x = [2,4,6]

print( 2 not in x)

Output????

25

media

Try it.....

26

media

Python operators

media

Show answer

Auto Play

Slide 1 / 26

SLIDE