Python Conditional Statements PDF
Document Details
Uploaded by FreshestNitrogen
Tags
Summary
These lecture notes detail conditional statements in Python programming. They cover if, else, if-elif-else statements and provide examples. The material also includes exercises for practice. The notes aim to be accessible and build programming knowledge.
Full Transcript
INTRODUCTION TO Python Part 3: Conditional Statements in Python At the end of this lecture, you should be able to learn about: Conditional statements in Python using if, i- else, if-elif-else Conditional - if The if statement of Python is similar to that of oth...
INTRODUCTION TO Python Part 3: Conditional Statements in Python At the end of this lecture, you should be able to learn about: Conditional statements in Python using if, i- else, if-elif-else Conditional - if The if statement of Python is similar to that of other programming languages. The if statement contains a logical expression using which data is compared, and a decision is made based on the result of the comparison (whether it is TRUE or FALSE). 3 Conditional - if The if statement in Python Programming has simple structure: if expression: statement(s) if expression contains condition All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. 4 Conditional – if Example: x=5 if x==5: print(x, ‘is equal to 5’) if x>10: print (x, ‘is greater than 10’) 5 Conditional –if (indentation concept) x=5 print (‘Before 5’) if x==5: Yes print(‘is 5’) x==5? print(‘is still 5’) print(‘is 5’) print(‘the third 5’) print(‘afterwards 5’) No print(‘is still 5’) print(‘before 6’) print(‘the third 5’) if x==6: print(‘is 6’) print(‘is still 6’) print(‘the third 6’) Exercise 1 Write a program that ask user an integer. If the integer is greater or equal to 2, display to user that they have entered a positive integer. 7 Conditional – if-else An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value. The else statement is an optional statement and there could be at most only one else statement following an if. Else does not contain a condition. 8 Conditional – if-else In if-else statements: if (Test condition): # If TRUE then these statements will be executed True statements else: # If FALSE then these statements will be executed False statements 9 Conditional – if - else 10 Exercise 2 marks = int(input(”Enter Your Subject Marks: ")) if marks >= 50: print(" Congratulations ") print(" You cleared the subject ") else: print(" You Failed") print(" Better Luck Next Time") Question: What would be the output for marks = 29? For marks = 50, what is/are the TRUE statements based on the above script? Which line will be executed for marks = 40? 11 Conditional – if – elif - else Elif combinations of else and if The elif statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions evaluates to true. Like the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if. elif statements contain conditions. 12 Conditional – if – elif - else if (condition 1): statements 1 elif (condition 2): statements 2 elif (condition 3): statements 3........... elif (condition n): statements n else: default statements 13 Conditional – if – elif - else 14 Exercise 3 age = input("How old are you? : ”) What is the if int(age)