Week5(Operators & Conditionals)
Week5(Operators & Conditionals)
Information Technology
12/28/2024
Python Operators
Operators are used to perform operations on variables and values.
E.g., Consider the expression 4 + 5. Here, 4 and 5 are called
operands and + is called the operator.
Python divides the operators in the following groups
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
2 12/28/2024
Arithmetic Operators
3 12/28/2024
Arithmetic Operators
4 12/28/2024
Example
5 12/28/2024
Assignment Operators
Assignment operators are used to assign values to variables.
6 12/28/2024
Assignment Operators..
7 12/28/2024
Comparison Operators
Comparison operators are used to compare two values. It
returns either True or False according to the condition
8 12/28/2024
Comparison Operators..
9 12/28/2024
Example
10 12/28/2024
Logical Operators
Logical operators are used to combine conditional
statements. It returns either True or False according to the
condition
Marks Grade
80 or above A
Between 60 and 80 B
Between 40 and 60 C
Less than 40 F
11 12/28/2024
Logical Operators..
12 12/28/2024
Example
13 12/28/2024
Bitwise operators
In Python, bitwise operators are performed on bit by bit,
hence the name bitwise operators. Then the result is
returned in decimal format.
14 12/28/2024
Bitwise operators..
Let's first understand representation of number in bits.
128 64 32 16 8 4 2 1
60 0 0 1 1 1 1 0 0
13 0 0 0 0 1 1 0 1
60&13 0 0 0 0 1 1 0 0
12
15 12/28/2024
Bitwise operators..
16 12/28/2024
Decision Making Statements
making.
17 12/28/2024
Operation of if Statement
18
If Statement Syntax
20 12/28/2024
Scenario 2: Which lines will be shown on Console
21 12/28/2024
Scenario 3: Which lines will be shown on Console
22 12/28/2024
Scenario: Shopping Mall
Amount Discount
Less than 1000 No discount
greater than equal to 1000 25%
greater than equal to 5000 50%
23 12/28/2024
Scenario: Shopping Mall..
24 12/28/2024
Scenario: Shopping Mall..
25 12/28/2024
Scenario: Shopping Mall..
26 12/28/2024
Scenario: Shopping Mall..
27 12/28/2024
Scenario: Shopping Mall..
28 12/28/2024
Operation of if else statement
29
Check if the number is positive or negative
30
Given 2 numbers Check which number is greater
31
if elif else
The elif allows us to check for multiple
expressions.
32 12/28/2024
Scenario: Shopping Mall..
33 12/28/2024
Exercise: Conditional Statements and Operators
Write a Python program to determine if a person qualifies for
a club membership discount. The criteria are:
• The person must be under 21 years old or over 60 years old.
• Alternatively, if the person is a student, they also qualify for a
discount
34 12/28/2024