Sunday, December 3, 2023

Day 4: Loops and Iterations

Chapter 1: Introduction to Loops

Loops are a fundamental concept in programming that allows you to repeat a certain block of code multiple times. In Python, there are two main types of loops: for and while.

The for Loop

The for loop is used for iterating over a sequence (that is either a list, tuple, dictionary, string, or range):

        
# for loop example
for item in iterable:
    # code to be executed for each item
        
    

For example:

        
# Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
        
    

The while Loop

The while loop continues to execute a block of code as long as the specified condition is True:

        
# while loop example
while condition:
    # code to be executed while the condition is True
        
    

For example:

        
# Using while loop to countdown
countdown = 5
while countdown > 0:
    print(countdown)
    countdown -= 1
        
    

Understanding loops is crucial for efficiently handling repetitive tasks in your programs.

Chapter 2: Loop Control Statements

Python provides several loop control statements to modify the execution of loops. These include break, continue, and else clauses in loops.

The break Statement

The break statement is used to exit the loop prematurely, regardless of the loop's normal termination condition:

        
# Using break statement
for item in iterable:
    if condition:
        break
        
    

The continue Statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration:

        
# Using continue statement
for item in iterable:
    if condition:
        continue
        # code here will be skipped for the current iteration
        
    

The else Clause in Loops

Python allows an else clause in loops. The code inside the else block is executed when the loop condition becomes False, unless the loop was terminated by a break statement:

        
# Using else clause in loops
for item in iterable:
    # code to be executed for each item
else:
    # code to be executed when the loop condition becomes False
        
    

Loop control statements provide flexibility and additional control over the flow of your loops.

Chapter 3: Nested Loops

In Python, you can have loops inside loops, known as nested loops. This is useful when dealing with multi-dimensional data structures or when repetitive actions need to be performed within each iteration of an outer loop.

        
# Nested loops example
for outer_item in outer_iterable:
    for inner_item in inner_iterable:
        # code to be executed for each inner item, within each outer iteration
        
    

For example:

        
# Multiplication table using nested loops
for i in range(1, 11):
    for j in range(1, 11):
        print(i * j, end="\t")
    print()  # Move to the next line for the next outer iteration
        
    

Understanding nested loops is essential for handling complex scenarios in programming.

Chapter 4: Putting It All Together - A Looping Example

Let's apply our knowledge of loops to a practical example. Suppose we want to find the factorial of a given number using a for loop.

        
# Finding factorial using a for loop
num = 5
factorial = 1

for i in range(1, num + 1):
    factorial *= i

print(f"The factorial of {num} is {factorial}")
        
    

This example demonstrates how a for loop can be used to calculate the factorial of a number. Stay tuned for Day 5, where we'll explore Functions and Modularity in Python.

If you have any questions or need further clarification, feel free to ask.

No comments:

Post a Comment

scala project to support JDK 17

Compiling my Scala project with JDK 17. status: the project once used sbt version 1.2.8 and scala 2.12.8, and targets JDK 11. it works fin...