You are on page 1of 16

Introduction to Python

Operators in Python
Hands-on Exercise
Index
1 Objectives ............................................................................................ 3
2 Overview .............................................................................................. 4
3 Guided Exercise .................................................................................... 5
3.1 Operators and Operands ............................................................................ 5
3.2 Python Operators......................................................................................... 5
3.2.1 Arithmetic Operators ....................................................................... 6
3.2.2 Assignment Operators ..................................................................... 7
3.2.3 Comparison Operators .................................................................... 8
3.2.4 Logical Operators........................................................................... 10
3.2.5 Bitwise Operators ........................................................................... 10
3.2.6 Identify Operators .......................................................................... 12
3.2.7 Membership Operators .................................................................. 12
4 Practice Exercise ................................................................................. 14
1 Objectives
After completing the hands-on exercise, you will be able to:
– Describe operators and operands
– Explain the syntax and usage of various operators available in Python, such
as:
o Arithmetic Operators
o Assignment Operators
o Comparison Operators
o Logical Operators
o Bitwise Operators
o Identity Operators
o Membership Operators
2 Overview
This document provides information about various operators found in Python, along
with guided and practice exercises to help you understand the topic better.

Estimated time needed: 15 minutes


3 Guided Exercise
3.1 Operators and Operands

Python operators are the special symbols used to execute operations on variables
and values. The values used by the operator to calculate are called operands.

# Write the code below


x=5
y=3
x+y

Output

3.2 Python Operators

The various operators available in Python are:

Let us now look at some exercises using operators.


3.2.1 Arithmetic Operators

# Addition
22 + 7

29

Output

# Subtraction
13 - 7

Output

# Multiplication
12 * 3

36

Output

# Division (result is in float)


12 / 4

Output
# power of
4 ** 2

16

Output

# Modulus
13 % 2

Output

# Integer Division
13 // 2

Output

3.2.2 Assignment Operators

# Write the code


x = 10
x /= 2
print(x)

5.0

Output
# Write the code
x = 10
x %= 5
print(x)

Output

# Write the code


x = 10
x //= 5
print(x)

Output

# Write the code


x = 10
x **= 3
print(x)

1000

Output

3.2.3 Comparison Operators

# Write the code


# > Greater than
5>7

False

Output

# Write the code


# < less than
5<7
True

Output

# Write the code


# == Equal to
5 == 5

True

Output

# Write the code


# != Not equal to
5 != 5

False

Output

# Write the code


# >= Greater than equal to
5 >= 3

True

Output

# Write the code


# <= Less than equal to
5 <= 3

False

Output
3.2.4 Logical Operators

# Write the code


# and operator
x=6
print(x > 4 and x < 10)

True

Output

# Write the code


# and operator
x=6
print(x > 4 or x < 5)

True

Output

# Write the code


# and operator
x=6
print(not(x > 4 and x < 10))

False

Output

3.2.5 Bitwise Operators


Let’s take an example in this case we will use bitwise operator on two variables a
and b having values 7 and 5 respectively. First will convert 7 into bitwise operator
taking a base of 2.
7=111(22 + 21 + 20 = 7)
5=101(22 + 0 + 20 = 5)
Now we will apply ‘&’ operator bitwise so the output will be 5.
# Write the code
# and operator 111 7
a=5 101 5
b=7 101 5
a&b

Output

# Write the code


# or operator 111 7
a=5 101 5
b=7 111 7
a|b

Output

# Write the code


# xor operator 111 7
a=5 101 5
b=7 010 2
a^b

Output

# Write the code


# shift a by b bits 3 >> 2 = 0
3 >> 2 0011 0000

Output
3.2.6 Identify Operators

# Write the code


# is
x = “park”
x is “park”

True

Output

# Write the code


# is not operator
x = “park”
x is not “park”

False

Output

3.2.7 Membership Operators

# Write the code


# in operator
x = [“park”,”Alen”]
print(“Alen” in x)

True

Output

# Write the code


# not in operator
x = [“park”,”Alen”]
print(“Sara” not in x)

True

Output
# Write the code
# not in operator
x = [“park”,”Alen”]
print(“park” not in x)

False

Output
4 Practice Exercise
a. Write a code to find the cube of 5 using assignment operator

# Write the code below

Output

b. Write a code to find the integer division of 32 by 5

# Write your code below

Output

c. Declare a variable x and y with value 4 and 6. Find the value using bitwise
operator and, or values

# Write your code below

Output
d. Write a code and declare variable names with values “Luis, “Peter”, “Iron”.
Check the output by using membership operators

# Write your code below

Output

You have completed Operators in Python hands-on exercises in Introduction to


Python.
Copyright © 2021 Accenture
All rights reserved.
Accenture and its logo are trademarks of Accenture.

You might also like