Python Programming Day 4 PDF
Document Details
Uploaded by PlushWolf8519
NIELIT
Tags
Summary
This document is a set of lecture notes on Python programming. It covers for loops, while loops, the range function, break and continue statements, and provides examples.
Full Transcript
NATIONAL [Unit INSTITUTE 1: Introduction OF ELECTRONICS to Web Design] Course:AND INFORMATION NIELIT TECHNOLOGY...
NATIONAL [Unit INSTITUTE 1: Introduction OF ELECTRONICS to Web Design] Course:AND INFORMATION NIELIT TECHNOLOGY ‘O’ Level (IT) Sumit Complex, A-1/9, Vibhuti Khand, Module: GomtiWeb M2-R5: Nagar, Lucknow, Designing & Publishing 1 File and Printer sharing in Windows NT Environment Python Programming Day 4 [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 2 Index Why Loop? Python for Loop Python range() function Syntax of range() function Example of range() function For loop Flow Chart The break Statement The continue Statement For-else loop The while Loop While-else loop Nested Loop Assignments [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 3 Why Loop? To perform repetitive tasks efficiently without manually writing the same code multiple times. Instead of duplicating code, you can loop through sequences. There are two loops that exists in python: for loop: It allows you to execute a block of code repeatedly for a fixed number of times. while loop: it is used to execute a block of code until a certain condition is met. [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 4 Python for Loop for loop iterates over the elements of sequence in order. In each iteration, the body of the loop is executed. Note: Iterables: Objects that can be iterated over, such as lists, tuples, strings, dictionaries, and ranges. Before using for Loop Let’s First read concept of Range & in. [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 5 Python range() function range() is a built-in function of Python. It is used when a user needs to perform an action for a specific number of times. The range() function is used to generate a sequence of numbers. range() is commonly used in for looping hence, knowledge of same is key aspect when dealing with any kind of Python code. Most common use of range() function in Python is to iterate sequence type (List, string etc.. ) with for and while loop. Note: ‘in’ is a Membership operator that is used to test if a sequence is presented in an object or not. [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 6 Syntax of range() Function: Syntax: range(start, stop, step) start: integer starting from which the sequence of integers is to be returned stop: integer before which the sequence of integers is to be returned. The range of integers end at (stop – 1). step: integer value which determines the increment between each integer in the sequence [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 7 Example of range() I. for i in range(5): print(i) II. for i in range(3, 8): print(i) III. for i in range(10, 0, -2): print(i) IV. for i in range(2, 10, 2): print(i) [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 8 For Loop Flow Chart [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 9 Syntax of for Loop for variable in iterable: # code to be executed for: This keyword indicates the start of a for loop. variable: This is a temporary variable that will hold each element from the iterable during each iteration of the loop. in: This keyword separates the variable from the iterable. iterable: This is any object that can be iterated over, such as a list, tuple, string, dictionary, or a range object. For loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation. [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 10 The for Loop The for loop work with in Membership operators. Example1: for letter in 'Python’: print(f"Current letter: {letter}") Example2: fruits = ['apple', 'banana', 'mango’] for index in range(len(fruits)): print(f"At index {index}, fruit is {fruits[index]}") [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 11 Example: Example 3: fruits = ['apple', 'banana', 'mango’] for fruit in fruits: print(f"Current fruit: {fruit}") Example 4: person = {'name': 'John', 'age': 25, 'city': 'New York’} for key, value in person.items(): print(f"{key}: {value}") [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 12 The break Statement With the break statement we can stop the loop even if the while condition is true: [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 13 Break statement [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 14 Break Statement Example 1: for i in range(10): if i == 5: print("Loop interrupted at", i) break print(i) [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 15 Break statement Example 2: fruits = ['apple', 'banana', 'mango', 'grape', 'orange’] for fruit in fruits: if fruit == 'mango': print("Mango found, stopping the loop") break print(f"Checking fruit: {fruit}") [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 16 The continue Statement With the continue statement we can stop the current iteration of the loop, and continue with the next: [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 17 Continue flow control [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 18 Continue statement Example 1: for i in range(1, 10): if i % 2 != 0: continue # Skip odd numbers print(i) Example 2: fruits = ['apple', 'banana', 'mango', 'grape', 'orange’] for fruit in fruits: if fruit == 'mango': continue # Skip 'mango' print(fruit) [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 19 Continue statement Example 3: count = 0 while count < 5: count += 1 if count == 3: continue # Skip printing when count is 3 print(f"Count: {count}") Example 4: words = ['hello', '', 'world', 'python', ‘’] for word in words: if word == '': continue # Skip empty strings print(word) [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 20 For-Else Loop The for-else construct in Python allows you to execute an else block after the for loop finishes only if the loop completes without hitting a break statement. Example: numbers = [1, 2, 3, 4, 5] for num in numbers: print(num) else: print("Loop completed without break.") [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 21 The while Loop The while loop is also known as a pre-tested loop. A while loop allows a part of the code to be executed as long as the given condition is true. Syntax while condition: statements When the program control reaches the while loop, the condition is checked. If the condition is true, the block of code under it is executed. Remember to indent all statements under the loop equally. After that, the condition is checked again. This continues until the condition becomes false. Then, the first statement, if any, after the loop is executed. [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 22 The while Loop Flow Chart [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 23 The while Loop With the while loop we can execute a set of statements as long as a condition is true. Example1:-The while loop decreases the value of count until it reaches 0: count = 5 while count > 0: print(f"Countdown: {count}") count -= 1 # Decrease the value of count by 1 each time print("Liftoff!") Example2:- The loop keeps asking the user for a password until the correct one is entered. password = "" while password != "python123": password = input("Enter the password: ") print("Access granted!") [Unit 1: Introduction to Web Design] Course: NIELIT ‘O’ Level (IT) Module: M2-R5: Web Designing & Publishing 24 The while loop Example 3:-The loop calculates the sum of numbers from 1 to 5. total = 0 number = 1 while number