Professional Documents
Culture Documents
UNIVERSITY
Department of Physics
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
4
PY101 Operators
Conditions and Booleans
5
PY101 Operators
Conditions and Booleans
6
PY101 Operators
Logical Operators
• 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?
• True or False?
• True or False?
15
PY101 Decision Structures
16
PY101 Decision Structures
Example: Temperature Warnings
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
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)
if condition:
# body of if statement
19
PY101 Decision Structures
One-Way Decisions (IF-STATEMENT)
• Example
number = 10
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')
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')
26
Thanks
27