You are on page 1of 27

QUAID-I-AZAM

UNIVERSITY
Department of Physics

PY-101 Fundamentals of Programming Languages

Lecture 4: Decision Structures

Aftab Alam
cs101.qau@gmail.com

4/7/2023 1
Lecture 1: Introduction

PRESENTATION AGENDA
• Operators
• Booleans
• Relational,
• Logical,
• Membership,
• Identity Operators
• Decision Structures
• One-way Decision (If Statement)
• Two-way Decisions (If Else Statement)
• Multi-Way Decisions (IF...ELIF...ELSE Statement)

2
Operators

3
PY101 Operators
Comparison Operators

• There are six comparison (or relational) operators in Python.


• A relational operator is a programming language construct or operator that
tests or defines some kind of relation between two variables.

Python Mathematics Meaning


< < Less than
<= ≤ Less than or equal to
== = Equal to
>= ≥ Greater than or equal to
> > Greater than
!= ≠ Not equal to

4
PY101 Operators
Conditions and Booleans

• Conditions may compare either numbers or strings

• Strings are compared lexicographically, meaning that they


are compared character by character according to their
Unicode values
• E.g., “Bbbb” is less than “aaaa” since “B” precedes “a”
• (all uppercase Latin letters come before lowercase
equivalents)

• A condition is actually a Boolean expression,


• which produces a value of either
• True (the condition holds) or
• False (the condition does not hold)

5
PY101 Operators
Conditions and Booleans

• Comparison operators can be chained


>>> 1 < 2 < 3 >>> 3 < x < 2


True False
>>> x = 2 >>> 2 == x < 4
>>> 1 < x < 2 True
False >>> y = 4
>>> 1 < x < 2 < 3 < 4 < 5 >>> 1 < x <= 2 < y
False True
>>> 1 < x <= 2 >> 1 != 2
True True

6
PY101 Operators
Logical Operators

• We can also use logical operators with our conditions


Operator Description
not x Evaluates to the opposite- i.e., if x is False, the result will be True and vice versa
x and y Evaluates to True if both, x and y are True
x or y Evaluates to True if either x is True or y is True

• Examples

Statement Result
(3*4 > 10) and (3+4 < 10) True
(3*4 < 10) or (3+4 < 10) True
not ((3*4 < 10) or (3+4 < 10)) False
(3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (5 + 4 > 10) True
7
PY101 Operators
Membership Operators

• Membership operators are used to test if a sequence is presented in an object: IN and Not in

Operator Description
x in sequence Evaluates to True if x is found in the given sequence (e.g., string)
x not in sequence Evaluates to True if x is NOT found in the given sequence (e.g., string)

Examples
Statement Result
“15-110” in “15-110 is a lot of fun!” True
“Java” not in “15-110 uses Python to illustrate computing principles” True
1 in “15-110” ERROR
“1” in “15-110” True 8
PY101 Operators
Identity Operators

• Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:

Operator Description
x is y Evaluates to True if x and y point to the same object
x is not y Evaluates to True if x and y do not point to the same object

Examples

Statement Result
“15-110” in “15-110 is a lot of fun!” True
“Java” not in “15-110 uses Python to illustrate computing principles” True
1 in “15-110” ERROR
“1” in “15-110” True 9
PY101 Operators
• Identity Operators

• True or False?

>>> x = "cmu"
>>> y = "cmu"
>>> x is y
True
>>> z = x
>>> z is y
True
>>>
PY101 Operators
• Identity Operators

• True or False?

>>> x = "cmu"
>>> y = "cmu"
>>> x is y
True
>>> z = x
>>> z is y
True
>>>
PY101 Operators
• Identity Operators

• True or False?

>>> x = "cmu" >>> a = 5


>>> y = "cmu" >>> if (type(a) is int):
>>> x is y ... print("true")
True ... else:
>>> z = x ... print("false")
>>> z is y ...
True true
>>> >>>
PY101 Operators
• Identity Operators

• True or False?

>>> x = "cmu" >>> a = 5 >>> b = 5.4


>>> y = "cmu" >>> if (type(a) is int): >>> if (type(b) is not int):
>>> x is y ... print("true") ... print("true")
True ... else: ... else:
>>> z = x ... print("false") ... print("false")
>>> z is y ... ...
True true true
>>> >>> >>>
PY101 Operators
• Identity Operators

• True or False?

>>> x = "cmu" >>> a = 5 >>> b = 5.4


>>> y = "cmu" >>> if (type(a) is int): >>> if (type(b) is not int):
>>> x is y ... print("true") ... print("true")
True ... else: ... else:
>>> z = x ... print("false") ... print("false")
>>> z is y ... ...
True true true
>>> >>> >>>
Decision Structures
• One-way Decision (If Statement)
• Two-way Decisions (If Else Statement)
• Multi-Way Decisions (IF...ELIF...ELSE Statement)

15
PY101 Decision Structures

• So far, we have mostly viewed computer programs as sequences of


instructions that are interpreted one after the other

• Sequencing is a fundamental concept of programming, but alone it is not


sufficient to solve every problem

• Often it is necessary to alter the sequential flow of a program to suit the


needs of a particular situation

• We will study decision structures, which are statements that allow a


program to execute different sequences of instructions for different cases

16
PY101 Decision Structures
Example: Temperature Warnings

• Let us consider again our Celsius to Fahrenheit temperature program

def main():
celsius = eval(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit")
main()

• How can we enhance this program to print a suitable warning when the
temperature is extreme (say, over 90 degrees F, it deserves a heat warning, and
under 30 it deserves a cold warning)?

17
PY101 Decision Structures
Example: Temperature Warnings

• Enhanced Celsius to Fahrenheit temperature program

def main():
celsius = eval(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit")
if fahrenheit > 90:
print(“It is really hot outside. Be careful!”)
if fahrenehit < 30:
print(“Be sure to dress warmly!”)

main()

18
PY101 Decision Structures
One-Way Decisions (IF-STATEMENT)

• We applied the Python if statement to implement a one-way decision,


• If Statement Syntax

if condition:
# body of if statement

• The if statement evaluates condition.


• If condition is evaluated to True, the code inside the body of if is executed.
• If condition is evaluated to False, the code inside the body of if is skipped.

19
PY101 Decision Structures
One-Way Decisions (IF-STATEMENT)

• Example

number = 10

# check if number is greater than 0


if number > 0:
print('Number is positive.')

print('The if statement is easy')

20
PY101 Decision Structures
Two-Way Decisions (IF-ELSE STATEMENT)

• if...else Statement
• Notice how we attached an else clause onto an if clause to come up with what we refer to as a two-way
decision
• Syntax
if condition:
# block of code if condition is True
else:
# block of code if condition is False

21
PY101 Decision Structures
Two-Way Decisions (IF-ELSE STATEMENT)

• Example

number = 10

if number > 0:
print('Positive number')

else:
print('Negative number')

print('This statement is always executed')

22
PY101 Decision Structures
Two-Way Decisions (IF-ELSE STATEMENT)

• Example

23
PY101 Decision Structures
Multi-Way Decisions (IF...ELIF...ELSE Statement)

• The if...else statement is used to execute a block of code among two alternatives.
• However, if we need to make a choice between more than two alternatives, then we use
the if...elif...else statement.

• Syntax

if condition1:
# code block 1

elif condition2:
# code block 2

else:
# code block 3

24
PY101 Decision Structures
Multi-Way Decisions (IF...ELIF...ELSE Statement)

• Example

25
PY101 Decision Structures
Multi-Way Decisions (IF...ELIF...ELSE Statement)

• Example

number = 0

if number > 0:
print("Positive number")

elif number == 0:
print('Zero')
else:
print('Negative number')

print('This statement is always executed')

26
Thanks

27

You might also like