Loops for Beginners (with python code)

 

Understanding Loops in Python

Loops are a fundamental concept in programming that allow us to repeat a block of code multiple times. They are useful for tasks that involve repetitive actions.

1. Main Points About Loops

  • For Loop: Used to iterate over a sequence (such as a list, tuple, dictionary, set, or string) and execute a block of code for each item in the sequence.
  • While Loop: Repeats a block of code as long as a specified condition is true.
  • Break Statement: Exits the loop prematurely if a certain condition is met.
  • Continue Statement: Skips the current iteration and moves to the next iteration of the loop.

2. Creating a Math Table Using a Simple For Loop

Let's create a simple math table using a for loop in Python. This example will print the multiplication table for the number 5.

Python Code

    
    # Multiplication table for number 5
    num = 5
    for i in range(1, 11):
        print(num * i)
    
    

Output

    
    5
    10
    15
    20
    25
    30
    35
    40
    45
    50
    
    

Explanation

The code uses a for loop to iterate through numbers 1 to 10. For each iteration, it multiplies the number 5 by the current iteration number (i) and prints the result.

Box Diagram

    
    +-------------------------+
    | num = 5                 |
    +-------------------------+
    | for i in range(1, 11):  |
    |     +-------------------+
    |     | print(num * i)    |
    +-------------------------+
    
    

3. Creating Math Tables from 1 to 10 Using Nested Loops

Now let's create math tables from 1 to 10 using nested loops. This example will generate a 10x10 table of multiplication results.

Python Code

    
    # Multiplication tables from 1 to 10
    for i in range(1, 11):
        for j in range(1, 11):
            print(i * j, end=' ')
        print()
    
    

Output

    
    1 2 3 4 5 6 7 8 9 10 
    2 4 6 8 10 12 14 16 18 20 
    3 6 9 12 15 18 21 24 27 30 
    4 8 12 16 20 24 28 32 36 40 
    5 10 15 20 25 30 35 40 45 50 
    6 12 18 24 30 36 42 48 54 60 
    7 14 21 28 35 42 49 56 63 70 
    8 16 24 32 40 48 56 64 72 80 
    9 18 27 36 45 54 63 72 81 90 
    10 20 30 40 50 60 70 80 90 100 
    
    

Explanation

The code uses two nested for loops. The outer loop iterates through numbers 1 to 10 (i), and the inner loop iterates through numbers 1 to 10 (j). For each combination of i and j, it multiplies the two numbers and prints the result. The end=' ' parameter in the print function ensures that the results are printed on the same line, and the inner loop prints a newline character after each inner loop completes.

Box Diagram

    
    +------------------------------+
    | for i in range(1, 11):       |
    |     +------------------------+
    |     | for j in range(1, 11): |
    |     |     +------------------+
    |     |     | print(i * j, end=' ') |
    |     +------------------------+
    |     | print()               |
    +------------------------------+
    
    

4. Looping Through Lists and Dictionaries

Loops are also useful for iterating through lists and dictionaries. Here are examples of how to loop through these data structures.

Looping Through a List

Let's loop through a list of numbers and print each number.

Python Code

    
    numbers = [1, 2, 3, 4, 5]
    for num in numbers:
        print(num)
    
    

Output

    
    1
    2
    3
    4
    5
    
    

Box Diagram

    
    +---------------------+
    | numbers = [1, 2, 3, 4, 5] |
    +---------------------+
    | for num in numbers: |
    |     +---------------+
    |     | print(num)    |
    +---------------------+
    
    

Looping Through a Dictionary

Let's loop through a dictionary and print each key-value pair.

Python Code

    
    student_scores = {"Alice": 85, "Bob": 92, "Charlie": 78}
    for student, score in student_scores.items():
        print(student, score)
    
    

Output

    
    Alice 85
    Bob 92
    Charlie 78
    
    

Box Diagram

    
    +--------------------------------------+
    | student_scores = {"Alice": 85, "Bob": 92, "Charlie": 78} |
    +--------------------------------------+
    | for student, score in student_scores.items(): |
    |     +----------------------------------------+
    |     | print(student, score)                  |
    +--------------------------------------+

Support Our Efforts and Earn Together ðŸš€

Visit https://parucodes.github.io/ today and start your journey to becoming a fast, accurate, and confident touch typist.

If you find our website useful and want to support us, consider joining the exciting world of Bitcoin mining on your mobile phone. Follow this link: Mine PI Bitcoin and use my username prarthanadp as your invitation code. With the referral code prarthanadp, you'll receive a special referral bonus.

Thank you for your support! Let's grow and earn together! 🌟

 

Comments

Popular posts from this blog

Recursion examples for Beginners (step by step code execution walkthrough with python code)

Handling hierarchical Data using Dictionaries for beginners (with python code)

Word Search in Maze using Depth First Search (with python code and step by step code execution walkthrough)