Python Programming PDF

Summary

This Jupyter notebook provides examples of conditional statements (if, if-else, elif, and nested if statements), along with loops in Python. Examples include finding the smallest number amongst the three inputs, displaying a message if the number is even, and a variety of examples using loops, ranges, and other programming elements.

Full Transcript

10/31/23, 5:47 PM Unit_2_VHA - Jupyter Notebook unit-2 Objectives There are situations in our lives when we need to make a decision and take the next steps acco...

10/31/23, 5:47 PM Unit_2_VHA - Jupyter Notebook unit-2 Objectives There are situations in our lives when we need to make a decision and take the next steps accordingly. Similar situations arise in a programming language, and we need to make decisions here as well. Based on the decision, we execute the next block of code. if statement if..else statement elif statements Nested if statements if statement In this, the statement starts with an if reserved word. The condition in the statement is a Boolean expression that determines whether or not the body will be executed. A colon must follow the condition. The block is defined as one or more statements that are executed when the condition is true. The statement in the if block is a Boolean expression, which determines whether or not the block of statements will be executed. 1 Syntax: 2 if expression: 3 statement(s) In : 1 n= int(input("enter value: ")) 2 if n%2==0: 3 print("number is even") enter value: 5 In : 1 n= int(input("enter value: ")) 2 if n%2==0: 3 print("number is even") enter value: 10 number is even if..else statement With if else statements, if is the reserved word, and the code block under it is executed if its boolean expression evaluates to TRUE; otherwise, the code block under the else statement is executed. Syntax: localhost:8888/notebooks/T1-PYTHON1/Unit_2_VHA.ipynb 1/15 10/31/23, 5:47 PM Unit_2_VHA - Jupyter Notebook if expression: statement(s) else: statement(s) In : 1 n= int(input("enter value: ")) 2 if n%2==0: 3 print("number is even") 4 else: 5 print("number is odd") enter value: 1 number is odd elif statements The elif statement helps you evaluate multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Syntax: if expression1: statement(s) elif expression2: statement(s) elif expression3: statement(s) else: statement(s) In : 1 n=int(input("enter value: ")) 2 if n>0: 3 print("posative value") 4 elif n

Use Quizgecko on...
Browser
Browser