Range and Nested Loops

0
8كيلو بايت

Range Function (range())

The range() function is a built-in function in Python used to generate a sequence of numbers. It takes up to three arguments:

  • start (optional): The starting number of the sequence (inclusive). Defaults to 0.
  • stop (required): The number one greater than the last element in the sequence (exclusive).
  • step (optional): The increment between each number in the sequence. Defaults to 1.

Here's how it's used:

Python
# Generate numbers from 0 to 4 (excluding 4)
numbers = range(5)
print(list(numbers))  # Output: [0, 1, 2, 3]

# Generate numbers from 2 to 10 (excluding 10) with a step of 2
even_numbers = range(2, 10, 2)
print(list(even_numbers))  # Output: [2, 4, 6, 8]

Important Note: range() itself doesn't create a list, it creates a range object. To get a list of numbers from the range, you can convert it using list().

Nested Loops

Nested loops involve placing one loop inside another loop. The inner loop executes completely for each iteration of the outer loop. This allows you to iterate over elements in multi-dimensional data structures or perform operations on combinations of elements.

Example 1: Printing a Square Pattern

Python
for i in range(5):
    for j in range(5):
        print("*", end="")  # Print an asterisk without a newline
    print()  # Move to a new line after each inner loop completes

This code creates a nested loop. The outer loop (i) iterates 5 times. For each iteration of the outer loop, the inner loop (j) also iterates 5 times, printing an asterisk (*) each time. Since print() is called within the inner loop with end="", it prints 5 asterisks on the same line. The outer loop then moves to the next iteration, and the inner loop repeats, creating a 5x5 square of asterisks.

Example 2: Iterating Over a List of Lists

Python
data = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

for row in data:
    for element in row:
        print(element, end=" ")  # Print each element with a space
    print()  # Move to a new line after each row

This code iterates over a 2D list (data). The outer loop (row) iterates over each sub-list (row) in the data. The inner loop (element) iterates over the elements within each sub-list, printing each element with a space. Finally, a newline is printed after each row is processed.

Applications of Nested Loops:

  • Traversing multi-dimensional data structures (like matrices or grids).
  • Finding patterns or relationships between elements in data.
  • Performing calculations on combinations of elements.
  • Creating complex output formats (like tables or visualizations).
البحث
الأقسام
إقرأ المزيد
Chemistry
UCE CHEMISTRY PAPER 1 KAMTEC MOCK 2024
UCE CHEMISTRY PAPER 1KAMTEC MOCK 2024
بواسطة Landus Mumbere Expedito 2024-08-11 11:39:15 3 10كيلو بايت
Computer Programming
Dynamic Typing, Stubs, and Namespaces in Python
Here's a breakdown of these three concepts in Python: 1. Dynamic Typing: Python is a...
بواسطة Python for Everybody - Full University Python Course Code 2024-07-17 14:53:52 0 8كيلو بايت
Technology
Discover How Blockchain is Revolutionizing Data Security
Stay Ahead of the Curve with the Latest in Tech! Discover How Blockchain is Revolutionizing Data...
بواسطة ALAGAI AUGUSTEN 2024-07-23 17:21:45 0 16كيلو بايت
Technology
Microsoft Outlook 2016 Step by Step
Microsoft Outlook 2016
بواسطة Mpatswe Francis 2024-08-14 21:48:58 0 8كيلو بايت
Chemistry
A-LEVEL CHEMISTRY E.N RAMSDEN
A-LEVEL CHEMISTRY E.N RAMSDEN
بواسطة Landus Mumbere Expedito 2024-08-18 14:15:58 1 9كيلو بايت
Tebtalks https://tebtalks.com