0% found this document useful (0 votes)
3 views10 pages

Operators in Python

The lecture covers Python operators, which are symbols that perform operations on variables and values, including arithmetic, comparison, logical, assignment, and membership operators. It provides examples and explanations for each type of operator, common mistakes students make, and includes classroom activities and homework assignments. The conclusion emphasizes the importance of operators in programming for calculations and decision-making.

Uploaded by

badhwar.hina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Operators in Python

The lecture covers Python operators, which are symbols that perform operations on variables and values, including arithmetic, comparison, logical, assignment, and membership operators. It provides examples and explanations for each type of operator, common mistakes students make, and includes classroom activities and homework assignments. The conclusion emphasizes the importance of operators in programming for calculations and decision-making.

Uploaded by

badhwar.hina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Operators Lecture

Introduction

Good morning students.

Today we will learn about Operators in Python.

Operators are special symbols that perform operations on variables and values.

For example:

a = 5
b = 3

print(a + b)

Here + is an operator.

It adds two numbers.

Definition of Operators
An operator is a symbol used to perform mathematical or logical operations on data.

Examples:

• +
• -
• *
• /
• ==
• and

Types of Operators in Python


Python mainly has:
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Assignment Operators
5. Membership Operators

1. Arithmetic Operators
These operators are used for mathematical calculations.

List of Arithmetic Operators


Operator Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

// Floor Division

** Power

Addition Operator +

Example:

a = 10
b = 5

print(a + b)

Output:

15
Subtraction Operator -
print(10 - 5)

Output:

Multiplication Operator *
print(4 * 3)

Output:

12

Division Operator /
print(10 / 2)

Output:

5.0

Explain:

• Division always gives a decimal value in Python.

Modulus Operator %

This gives the remainder.

print(10 % 3)

Output:

Explanation:

• 10 divided by 3 leaves remainder 1.

Use:
• Odd/even programs

Floor Division //
print(10 // 3)

Output:

It removes the decimal part.

Exponent Operator **
print(2 ** 3)

Output:

Explanation:

• 2³ = 8

2. Comparison Operators
These compare two values.

The answer is always:

• True
• False
Comparison Operators Table
Operator Meaning

== Equal to

!= Not equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

Examples
print(5 == 5)

Output:

True

print(5 > 3)

Output:

True

print(5 < 3)

Output:

False

3. Logical Operators
These are used to combine conditions.
Operator Meaning

and Both conditions must be true

or Any one condition true

not Opposite result

AND Operator
age = 20

print(age > 18 and age < 30)

Output:

True

OR Operator
print(5 > 10 or 7 > 5)

Output:

True

NOT Operator
print(not True)

Output:

False

4. Assignment Operators
Used to assign values.

Operator Example

= x = 5
Operator Example

+= x += 2

-= x -= 2

*= x *= 2

Example
x = 5

x += 3

print(x)

Output:

5. Membership Operators
Used to check whether a value exists in a sequence.

Operator Meaning

in Present

not in Not present

Example
fruits = ["apple", "banana", "mango"]

print("apple" in fruits)

Output:

True
Real Life Examples
Example 1 — Even/Odd Number
num = int(input("Enter a number: "))

if num % 2 == 0:
print("Even")
else:
print("Odd")

Example 2 — Voting Eligibility


age = int(input("Enter your age: "))

if age >= 18:


print("Eligible to vote")
else:
print("Not eligible")

Common Mistakes Students Make


Mistake 1
print(5 = 5)

Wrong because:

• = is assignment
• == is comparison

Correct:

print(5 == 5)

Mistake 2
print(10 / 3)

Students may expect 3.

Actual output:

3.333333
Use // for whole number division.

Classroom Activities
Activity 1

Predict the output:

print(5 + 2 * 3)

Answer:

• Multiplication first
• Output = 11

Activity 2

Write a program to:

• Input two numbers


• Print:
o Addition
o Subtraction
o Multiplication
o Division

Homework
Q1

Write a program to find remainder of two numbers.

Q2

Write a program to check whether a student passed or failed.


Q3

Write a program to calculate square of a number.

Conclusion
Today we learned:

• Arithmetic operators
• Comparison operators
• Logical operators
• Assignment operators
• Membership operators

Operators are the basic tools used in programming to perform calculations and make decisions.

Thank you students.

You might also like