Python Coding Day 12 | Python Lists and Loops Explained

Python Lists and Loops Explained

Watch the lesson tutorial  ðŸ”»


1. For Loop with Lists

The for loop is the most common way to iterate over the elements of a sequence, such as a list. It executes the code block once for each item in the list.

CodeExplanation
vehicles = ["Car", "Bike", "Bus"]This line creates a list named vehicles containing three string elements.
for vehicle in vehicles:This is the start of the for loop. For each item in the vehicles list, it temporarily assigns that item to the variable vehicle.
print(vehicle)Inside the loop, this line prints the current element ("Car", then "Bike", then "Bus").
print("I like " + vehicle)This line uses string concatenation to print a personalized sentence for the current vehicle.

Practical Example:

This pattern is useful for processing every item in a dataset, like printing a report for every user in a list or applying a function to every file in a directory.

Python
vehicles = ["Car", "Bike", "Bus"]
for vehicle in vehicles:
    print(vehicle)
    print("I like " + vehicle)

Output:

Car
I like Car
Bike
I like Bike
Bus
I like Bus

2. For Loop with Range

The built-in range() function generates a sequence of numbers, which is often used in for loops to control how many times the loop runs. It's especially useful when you need to repeat an action a specific number of times.

Example 1: range(stop) or range(start, stop)

CodeExplanation
for count in range(5, 20):This loop uses range(start, stop). It generates numbers starting at 5 (inclusive) and goes up to but not including 20 (exclusive). The numbers generated are $5, 6, 7, \dots, 19$.
print(count)Prints the current number in the sequence.

Practical Example (Counting Inventory):

Python
for count in range(5, 20):
    print(count)

Example 2: range(start, stop)

CodeExplanation
for value in range(50, 61):This loop generates numbers starting at 50 (inclusive) and goes up to but not including 61 (exclusive). The numbers generated are $50, 51, \dots, 60$.
print(value)Prints the current number.

Practical Example (A sequence of years):

Python
for value in range(50, 61):
    print(value)

Example 3: range(start, stop, step)

CodeExplanation
for step in range(2, 22, 4):This loop uses the third argument to define a step (or increment). It starts at 2, goes up to (but not including) 22, and increases by 4 in each iteration.
print(step)Prints the current number in the sequence: $2, 6, 10, 14, 18$.

Practical Example (Processing data every 4th entry):

Python
for step in range(2, 22, 4):
    print(step)

3. Calculating the total of numbers from 1 to 50

This is a classic example of using a for loop to perform an accumulation or running total.

CodeExplanation
sum_value = 0Initializes a variable to store the running total. It must start at 0 so the first number added is correct.
for num in range(1, 51):This loop iterates through the numbers from 1 up to 50 (since 51 is the exclusive stop value). The current number is stored in num.
sum_value += numThis is a shorthand operator equivalent to sum_value = sum_value + num. In each loop, the current value of num (1, then 2, then 3, and so on) is added to the running total stored in sum_value.
print("Total:", sum_value)After the loop is complete (all numbers from 1 to 50 have been added), this line prints the final result.

Practical Example:

This technique is fundamental for tasks like calculating the total cost of items in a shopping cart, aggregating sales data, or finding the sum of sensor readings.

Python
sum_value = 0
for num in range(1, 51):
    sum_value += num

print("Total:", sum_value)

Output:

Total: 1275

- by Chirana Nimnaka

Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python