4 ConditionalStatements
4 ConditionalStatements
4 ConditionalStatements
Sokratis Sofianopoulos
What are conditional statements
• In computer science, conditional statements are
features of a programming language, which perform
different computations or actions depending on
whether a programmer-specified Boolean condition
evaluates to true or false
• The control flow of the program changes based on
some condition
2
Arithmetic operators used as Boolean operators
3
Assignment vs. Boolean evaluation
•x=2
• print(x == 2) # prints out True
• print(x == 3) # prints out False
• print(x < 3) # prints out True
4
Simple if Statements
• General Syntax
if condition :
indentedStatementBlock
• If the condition is true, then do the indented statements
• If the condition is not true, then skip the indented statements
• Example (using input from the user):
age = int(input("How old are you? ")) #Python doesn't evaluate the data type, you have to
explicitly convert to int
if age < 17:
print("You must be older than 17 to vote.")
print(“Program flow continues here…")
5
if-else Statements
• general Python if-else syntax
if condition :
indentedStatementBlockForTrueCondition
else:
indentedStatementBlockForFalseCondition
• There are two indented blocks:
• The first is right after the if heading and is executed when the condition in the if
heading is true
• The second is right after the else statement, and is only executed when the original
condition is false
• In an if-else statement exactly one of two possible indented blocks is executed
6
if-else Statement example
7
if-elif Statements
• General syntax
if condition1 :
indentedStatementBlockForTrueCondition1
elif condition2 :
indentedStatementBlockForFirstTrueCondition2
elif condition3 :
indentedStatementBlockForFirstTrueCondition3
else:
indentedStatementBlockForEachConditionFalse
• Exactly one of the indented blocks is executed → the one corresponding to the 1st True condition
• If all conditions are False, it is the block after the final else line
8
Exercise
• Write a program called sign.py that asks the user for an integer number
• Print out which category the number is in:
• 'positive'
• 'negative'
• 'zero'
9
Boolean operators
10
The not operator
11
Boolean operators and/or example
day = "Friday"
date = 13
if day == "Friday" and date == 13:
print("Today is Friday the 13th! Good luck.")
if (day == "Tuesday" or day == "Friday") and date==13:
print(" Today is the 13th and it is either Friday or Tuesday! Good luck.")
12
Chaining comparison operators
13
Membership operators: in and not in
14
Identity operator: is and is not
15
Exercise
• Write a Python program to calculate a dog's age in dog's years using the
following formula:
• For the first two human years, a dog year is equal to 10.5 human years
• After that, each dog year equals 4 human years
• You can assign value to the variable of the dog's age by asking user input
with the following code:
dog_h_age = int(input("Enter the age of the dog in human years: "))
• If the user enter a negative number or zero, just print an appropriate error
message
16
Solution
17
Lazy evaluation
know as well as short-circuit evaluations
18
What is lazy evaluation
def x():
print('x')
return False
def y():
print('y')
return False
21