■ Python Lab Manual – Operators
■ Objective: To understand and apply different types of operators in Python including Arithmetic,
Relational, Logical, Assignment.
■ 1. Theory
Operators are special symbols used to perform operations on variables and values.
A. Arithmetic Operators
Used to perform basic mathematical operations.
Operator Description Example Output
+ Addition 5+3 8
- Subtraction 5-3 2
* Multiplication 5*3 15
/ Division 5/2 2.5
// Floor Division 5 // 2 2
% Modulus 5%2 1
** Exponentiation 2** 3 8
B. Relational (Comparison) Operators
Used to compare two values.
Operator Description Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5>3 True
< Less than 5<3 False
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 3 <= 5 True
C. Logical Operators
Used to combine conditional statements.
Operator Description Example Output
and True if both conditions are True (x > 5 and y > 5) True
or True if one condition is True (x > 5 or y < 5) True
not Reverses the result not(x > 5) False
D. Assignment Operators
Used to assign values to variables.
= x=5 Assigns value 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 **= 2 x = x ** 2
Python Operator Precedence (Simple Order – Highest to Lowest)
3. Lab Exercises
1. Write a Python program to input two numbers and perform all
arithmetic operations.
2. Check if a number is positive and less than 100 using logical
operators.
3. Demonstrate use of assignment operators in a small program.
4. Combine arithmetic and logical operators. x=7, y=3, z=10.
5. Write a Python program to demonstrate operator precedence —
that is, which operator is evaluated first when an expression has
multiple operators.
6. Write a Python program to input marks obtained by a student in
three subjects — Math, Science, and English.
The program checks whether the student has passed in all subjects.
A student passes only if they score 40 or more marks in each
subject.
If the condition is satisfied, the program displays “Student Passed ”,
otherwise it displays “Student Failed ”.
7. Write a Python program to create a simple calculator that
performs basic arithmetic operations.
The program takes two numbers as input and an operator (+, -, *,
or /).
Based on the operator entered by the user, the program performs the
corresponding operation:
Addition if the operator is +
Subtraction if the operator is -
Multiplication if the operator is *
Division if the operator is /
If the user enters an invalid operator, the program displays “Invalid
operator!”.