In this lecture, we’ll learn about the Python For Loop. After Learning Python While Loop We’ll start with the basics and gradually explore more advanced topics. The “for” loop is a powerful tool for repeating tasks, and we’ll show you how to use it effectively. Through easy-to-understand examples and hands-on exercises, you’ll become confident in using the “for” loop to solve various problems.
Python For Loop:
A For Loop is a control flow statement that iterates over a sequence (such as a list, tuple, string, or range) and executes a block of code for each element in the sequence. The basic syntax is:
for item in sequence:
# code to be executed for each item in the sequence
The loop continues until all items in the sequence have been processed.
Program 1: Counting with a For Loop
# Title: Square Numbers
for i in range(1, 6):
square = i ** 2
print(f"Number: {i}, Square: {square}")
Explanation:
- This program uses a for loop to iterate over a range of numbers from 1 to 5 (inclusive).
- Inside the loop, it calculates the square of each number (i ** 2) and prints both the original number and its square.
Conditionals within a Loop:
Similar to a “while” loop, you can use “if” statements inside a “for” loop to introduce conditional logic. This allows you to execute specific code based on conditions during each iteration of the loop.
for item in sequence:
# code to be executed for each item in the sequence
if some_condition:
# code to be executed when some_condition is true
else:
# code to be executed when some_condition is false
Counting Using For Loop:
A “for” loop is commonly used for counting, especially when iterating over a range of numbers. For example:
for i in range(1, 6):
print(i)
This loop will print the numbers 1 through 5.
“in” Operator with For Loop:
The “in” operator is often used with a “for” loop to iterate over the elements of a sequence. It checks if a particular element is present in the sequence and, if true, executes the code inside the loop.
my_list = [1, 2, 3, 4, 5]
for item in my_list:
print(item)
This loop iterates over each element in the list my_list, printing each item during each iteration.
Program 2: Conditional Filtering with For Loop
# Title: Even Numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
if num % 2 == 0:
print(f"Even Number: {num}")
Explanation:
- This program initializes a list of numbers.
- The for loop iterates over each number in the list.
- Inside the loop, there’s an “if” statement checking if the number is even (num % 2 == 0).
- If the number is even, it prints a message indicating that it is an even number.
Python For Loop: Problem Examples
Here are 10 problem examples involving for loops, conditionals within a loop, counting using a for loop, and the use of the “in” operator:
1: Use a for loop to print the numbers from 1 to 5.
2: Modify the previous program to skip printing multiples of 3.
3: Develop a program utilizing a for loop to count down from 10 to 1.
4: Employ a for loop to calculate and print the sum of the first 5 even numbers.
5: Create a program with a for loop to check if each character in a given word is a vowel.
6: Print the squares of numbers from 1 to 5 using a for loop.
7: Check whether a given number is prime using a for loop and conditionals.
8: Utilize a for loop and conditionals to print only the odd numbers from a given list of numbers.
9: Count and print the occurrences of a specific character in a given string using a for loop.
10: Verify the presence of a given element in a list using a for loop and the “in” operator.
These exercises encompass various aspects of for loops, conditionals within loops, counting, and the “in” operator, promoting active practice in Python programming.