0% found this document useful (0 votes)
5 views34 pages

Week5 (Operators & Conditionals)

Uploaded by

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

Week5 (Operators & Conditionals)

Uploaded by

rahidsaleemcool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

108443 – Introduction to

Information Technology

Week-5(Operators & Conditionals)

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

 Arithmetic operators are used with numeric values to


perform common mathematical operations

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

if marks of student > 80 (then give him/her A grade)


if marks of student > 60 and if marks of student < 80 (then give him/her B grade)
What will be problem if we right this second condition as if marks of student > 60
if marks of student > 40 and if marks of student < 60 (then give him/her C grade)
if marks of student < 40 (then give him/her F grade)

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

 Decision making is required when we want to execute a code

only if a certain condition is satisfied.

 The if, elif, else statement is used in Python for decision

making.

17 12/28/2024
Operation of if Statement

18
If Statement Syntax

 Here, the program evaluates the test expression and


will execute statement(s) only if the test expression is
True.

 If the test expression is False, the statement(s) is not


executed.

 In Python, the body of the if statement is indicated by


the indentation. The body starts with an indentation
and the first un-indented line marks the end.

 Python interprets non-zero values as True. None and


0 are interpreted as False.
19
Scenario 1: Check whether number is POSITIVE or not

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%

Write a Program for following


1. Suppose amount = 100
2. Only print the % discount to be given
3. Now check you code for amount = 500, 1100, 2500, 5500

23 12/28/2024
Scenario: Shopping Mall..

24 12/28/2024
Scenario: Shopping Mall..

25 12/28/2024
Scenario: Shopping Mall..

Let's Solve this Issue…

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.

 If the condition for if is False, it checks the


condition of the next elif block and so on.

 If all the conditions are False, the body of else


is executed.

 Only one block among the several if...elif...else


blocks is executed according to the condition.

 The if block can have only one else block. But


it can have multiple elif blocks.

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

You might also like