1. For Loop
Purpose: The for loop is designed for iterating over a sequence (like lists, tuples, dictionaries, sets, or strings) or a range of numbers. It is particularly useful when you know the number of iterations in advance or when you want to go through all items in a collection.
How It Works:...
1. For Loop
Purpose: The for loop is designed for iterating over a sequence (like lists, tuples, dictionaries, sets, or strings) or a range of numbers. It is particularly useful when you know the number of iterations in advance or when you want to go through all items in a collection.
How It Works:
The loop assigns each item from the sequence to a variable in each iteration.
It executes the block of code within the loop for every item until it has gone through the entire sequence.
Use Cases:
Iterating through Lists: Useful when you want to process or display each item in a list.
Working with Strings: Great for manipulating or examining characters in a string.
Generating Sequences: Combined with functions like range(), it allows you to create loops that run a specified number of times.
2. While Loop
Purpose: The while loop is used for repeated execution as long as a given condition remains true. It's useful when the number of iterations is not known beforehand and depends on a condition being met.
How It Works:
The loop evaluates the condition before executing the block of code. If the condition is true, it executes the code block; if false, it exits the loop.
It continues to check the condition before each iteration, which can lead to infinite loops if the condition never becomes false.
Use Cases:
User Input: Useful for scenarios where you want to continue prompting the user until they provide valid input.
Counting: Can be used for counting or iterating until a certain condition is met.
Resource Management: Often employed in scenarios where the process needs to run until specific resources are exhausted or conditions change.
Control Flow Statements in Loops
Control flow statements enhance the functionality of loops, allowing for greater control over how and when loops execute:
Break Statement:
Function: Exits the loop immediately, regardless of the loop's condition.
Use Case: Useful for terminating a loop when a specific condition is met, such as finding a target value or handling an error.
Continue Statement:
Function: Skips the rest of the code inside the loop for the current iteration and jumps to the next iteration.
Use Case: Useful for skipping over certain values that do not meet criteria without terminating the entire loop.
Pass Statement:
Function: Does nothing and acts as a placeholder.
Use Case: Useful in situations where syntactically a statement is required, but no action is needed (e.g., in an empty loop or function definition).
Summary
Loops in Python are fundamental for executing repetitive tasks efficiently. They can automate processes, handle collections of data, and manage control flow based on specific conditions. Understanding how to use for and while loops, along with control statements like break, continue, and pass, enables developers to write more dynamic and efficient code, enhancing program functionality and user interaction.
Size: 4.22 MB
Language: en
Added: Oct 02, 2024
Slides: 34 pages
Slide Content
Prepared by: Jahanara Taznina Orin
For loop: The for loop iterates over a given sequence (it may be a list, tuple, or string). itself allows for this. Syntax of for Loop for val in sequence: Body of for
break statement: With the break statement we can stop the loop even if it is true.
While loop: With the while loop we can execute a set of statements as long as a condition is true. It requires to define an indexing variable. Syntax of while Loop in Python while test_expression : Body of while
break statement: With the break statement we can stop the loop even if it is true.