LGT1041: INTRODUCTION TO AIDA IN BUSINESS
WEEK 3
PYTHON PROGRAMMING 2
1
DIFFERENCE BETWEEN RETURN AND PRINT
[Link]
2
COMPARISON/RELATIONAL OPERATORS
Operator Meaning
x==y x is equal to y
x!=y x is not equal to y
x>y x is bigger than y
x<y x is smaller than y
x>=y x is bigger than or equal to y
x<=y x is smaller than or equal to y
3
WHAT IS THE OUTPUT? (TRUE OR FALSE)
x=6
y=9
print(x<y)
print(x>y)
print(x<y and x>y)
print (x<y or x>y)
4
WHAT IS THE OUTPUT?
def can_run_for_president(age):
"""Can someone of the given age run for president in the US?"""
# The US Constitution says you must be at least 35 years old
return age >= 35
help(can_run_for_president)
print("Can a 19-year-old run for president?", can_run_for_president(19))
print("Can a 45-year-old run for president?", can_run_for_president(45))
5
WHAT IS THE OUTPUT?
def can_run_for_president(age, is_natural_born_citizen):
"""Can someone of the given age and citizenship status run for president in the US?"""
# The US Constitution says you must be a natural born citizen and at least 35 years old
return age >= 35 and is_natural_born_citizen
print(can_run_for_president(19,True))
print(can_run_for_president(55, False))
print(can_run_for_president(55,True))
6
CONDITIONAL EXECUTION
■ Think about this simple situation
– The exam passing mark is 40 Yes
X>=40
– You want to check if a student
passes the exam
– If the student passes, print the No
Print(“pass”)
message “pass”
■ We need to use the if statement
7
WHAT IS THE OUTPUT? (IF, ELSE)
x=60
if x>=40:
print('Pass')
8
ALTERNATIVE EXECUTION
■ Think about this situation Yes
No
– The exam passing mark is 40 X>=40
– You want to check if a
student passes the exam
– If the student passes, print Print(“fail”) Print(“pass”)
the message “pass”
– If the student fails, print
the message “fail”
■ We need to use the if-then-else statement
45
WHAT IS THE OUTPUT? (IF, ELSE)
x=30
if x>=40:
print('Pass')
else:
print('Fail')
10
CHAINED CONDITIONS
Yes
X>=90
■ Think about this situation
– The exam passing mark is 40
No
– If the student gets >=90 marks, Print(“distinction”)
print the message “distinction”
– If the student gets >=40 and <90 X>=40
marks, print the message “pass” Yes
– If the student fails, print the
message “fail” No Print(“pass”)
■ There are more than two possibilities
Print(“fail”)
■ Else if statement
11
WHAT IS THE OUTPUT? (IF, ELIF, ELSE)
x=95
if x>=90:
print('Distinction')
elif x>=40:
print('Pass')
else:
print('Fail')
12
WHAT IS THE OUTPUT? (IF, ELIF, ELSE)
x=95
if x>=90:
print('Distinction')
if x>=40:
print('Pass')
else:
print('Fail')
13
USER INPUT
x=input('what is your exam mark?')
x=float(x)
if x>=90:
print('Distinction')
elif x>=40:
print('Pass')
else:
print('Fail')
14
WHAT IS THE OUTPUT? (IF, ELIF, ELSE)
def inspect(x):
if x == 0:
print(x, "is zero")
elif x > 0:
print(x, "is positive")
elif x < 0:
print(x, "is negative")
else:
print(x, "is unlike anything I've ever seen...")
inspect(0)
inspect(-15)
15
You should be able to do Kaggle Lesson 3
16