pds first unit module 2 MODULE FOR ppt.pptx

bmit1 10 views 26 slides Sep 25, 2024
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

PDS


Slide Content

Sequential Statements

Python Basics: Introduction - Features – Execution Environment – Indentation – Comments - BasicElements: Data Types– Variables – Input/Output Statements – format() – Sequential – Basics of Conditionals – Selection (Conditional): Simple if – if else – If elif else – Nested if – Loops: for – for else – while - while else – Selection (Unconditional): break – continue – pass – Nested Loops – Functions: - Introduction to Functions, inbuilt functions, user defined functions, passing parameters, return values, recursion, Lambda functions

Decision Making or Conditional Statements Decision making is about deciding the order of execution of statements based on certain conditions. if statements if- else statements if- elif ladder Nested if statements

Python if statement if statement is a conditional statement in Python used to determine whether a block of code will be executed. If the program finds the condition defined in the if statement true, it will execute the code block inside the if statement. Syntax: if ( expression ): Statement 1 Statement 2 . Statement n

Example: a = 20 b = 20 if a == b: print("a and b are equal") print("If block ended")

if- else statements if- else statement checks the expression and executes the if block when the expression is True otherwise it will execute the else block of code. syntax if( expression ): Statement else: Statement

Example: number1 = 20 number2 = 30 if number1 >= number2: print("number 1 is greater than number 2") else: print("number 2 is greater than number 1")

Python if- elif ladder elif keyword to chain multiple conditions one after another. With elif ladder, we can make complex decision- making statements. Syntax if ( expression1 ): statement elif (expression2 ) : statement elif(expression3 ): statement . . else: statement

Example: price = 100 if price > 100: print("price is greater than 100") if price == 100: print("price is 100") if price < 100: print("price is less than 100")

Nested if statement Nested if statement. Syntax: statements is an if statement inside another if if (expression): if (expression): Statement of nested if else : Statement of nested if else Statement of outer if Statement outside if block

Example: num1 = int (input()) num2 = int (input()) if num1 >= num2: if num1 == num2: print(f'{num1} and {num2} are equal') else: print(f'{num1} is greater than {num2}') else: print(f'{num1} is smaller than {num2}')

Looping A loop statement allows us to execute a statement or group of statements multiple times until the condition is satisfied Types: while loop While loop else for loop for loop else Nested loops

while loop while loop is used to execute a block of statements repeatedly until a given condition is satisfied . And when the condition becomes false, the line immediately after the loop in the program is executed . Syntax: while expression: statement(s)

Example: count = 0 while count < 3: count = count + 1 print("Hello")

while loop else The statements in the else block will be executed after all iterations are completed . The program exits the loop only after the else block is executed.

Example x = 0 while x < 5: x += 1 print( f"iteration no {x} in while loop") else: print("else block in loop") print("Out of loop") iteration no 1 in for loop iteration no 2 in for loop iteration no 3 in for loop iteration no 4 in for loop iteration no 5 in for loop else block in loop Out of loop

For loop For loops are used for sequential traversal. For example: traversing a list or string or array etc. for iterator_var in sequence: statements(s)

Example: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)

For loop else The statements in the else block will be executed after all iterations are completed. The program exits the loop only after the else block is executed.

For Loop for x in range(5):     print("iteration no {} in for loop".format (x+1)) else:     print("else block in loop") print("Out of loop") iteration no 1 in for loop iteration no 2 in for loop iteration no 3 in for loop iteration no 4 in for loop iteration no 5 in for loop else block in loop Out of loop

Python nested loop A nested loop is a loop inside the body of the outer loop. The inner or outer loop can be any type, such as a while loop or for loop. For example, the outer for loop can contain a while loop and vice versa. Syntax: for element in sequence : for element in sequence: body of inner for loop body of outer for loop

Example: for i in range(1, 11): # nested loop # to iterate from 1 to 10 for j in range(1, 11): # print multiplication print(i * j, end=' ') print()

Loop control statements Loop control statements are used to change the flow of execution These can be used if you wish to skip an iteration or stop the execution. The three types of loop control statements are: break statement continue statement pass statement

Break statement Based on the given condition, the break statement stops the execution and brings the control out of the loop. for num in range(1, 11): if num == 5: break else: print(num)

Continue statement Continue statement is used to skip the current iteration when the condition is met and allows the loop to continue with the next iteration for num in range(1, 11): if num == 6: continue print(num)

pass Pass statement is used when we want to do nothing when the condition is met. It doesn’t skip or stop the execution, it just passes to the next iteration for num in range(1, 11): if num == 6: Pass print(num)