Professional Documents
Culture Documents
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.
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.
Output
# Addition
22 + 7
29
Output
# Subtraction
13 - 7
Output
# Multiplication
12 * 3
36
Output
Output
# power of
4 ** 2
16
Output
# Modulus
13 % 2
Output
# Integer Division
13 // 2
Output
5.0
Output
# Write the code
x = 10
x %= 5
print(x)
Output
Output
1000
Output
False
Output
Output
True
Output
False
Output
True
Output
False
Output
3.2.4 Logical Operators
True
Output
True
Output
False
Output
Output
Output
Output
Output
3.2.6 Identify Operators
True
Output
False
Output
True
Output
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
Output
Output
c. Declare a variable x and y with value 4 and 6. Find the value using bitwise
operator and, or values
Output
d. Write a code and declare variable names with values “Luis, “Peter”, “Iron”.
Check the output by using membership operators
Output