

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python code examples and explanations of the if, if-else, if-elif, and nested if statements. It covers how to test variables for different conditions and print corresponding outputs.
Typology: Slides
1 / 2
This page cannot be seen from the preview
Don't miss anything!


In [1]: a=
In [2]: if a>0: print("Positive") else: print("Negative")
Positive
In [5]: x = int(input()) if x>0: print("Positive") elif x<0: print("Negative") else: print("Zero")
Negative
In [6]: day = int(input()) if day == 1: print("Monday") elif day == 2: print("Tuesday") elif day == 3: print("Wednesday") elif day == 4: print("Thursday") elif day == 5: print("Friday") elif day == 6: print("Saturday") else: print("Sunday")
Thursday
In [7]: x = int(input()) y = int(input()) if x>y: print("X is greater than Y") else: print("Y is greater than X")
5 9 Y is greater than X
In [11]: x = int(input()) y = int(input()) z = int(input()) if x>y: if x>z: print("X is the greatest") else: print("Z is the greatest") else: if y>z: print("Y is the greatest") else: print("Z is the greatest")
5 8 Z is the greatest