Skip to content

Beginner's Guide For Loops in Python

Introduction A for loop is a fundamental programming construct that allows you to execute a block of code repeatedly for each item in a sequence (like a list,tuple, or string). This tutorial will guide you through the basics of for loops in Python, with code example and explanations. Table of Content 1.What is a For Loop? 2.Basic Syntax 3.Iterating Over a list 4.Using the range() Function 5.Nested For Loop 6.Common Use Cases 7.Practive Problem 8.Conslusion

What is a For Loop? A For Loop is used to interact over a sequence of elements, executing a block of code for each element. It simplifies the process of repeating actions, making your code more efficient and easier to read. Basic Syntax The basic syntax of a for loop in Python is: python Copy code for variable in iterable:

Code block to execute

variable: A temporary name for the curretn item from the iterable. interable: A sequence (like a list, tuple, or string) or any object that can return its elements one at a time. Interacting Over a list Let's start with an example of interacting over a list: pythonCopy code

List of fruits

fruits=['apple','banana','cherry']

For loop to interact over the list for fruit in fruits

print (fruit) Output: Copy Code apple banana cherry Explanation 1.The loop starts wiht the first item in the list ('apple'). 2.The variable fruit holds the value of the current item. 3.The code block (print(fruit)) executes for each fruit in the list. Using the range() Function The range() function is often used with for loops to generate a sequence of numbers. Here's how it works: pythonCopy code

For loop using range for i in range (5)

print (i) Output: 0 1 2 3 4 Key Points About range () range (n): Generates numbers from 0 to n-1. range (start, stop): Generates numbers from start to stop-1. range (start, stop, step): Generates numbers from start to stop-q, incrementing by step. Example: pythonCopy code

Using range with start, stop, and step for i in range (1,10,2)

print(i) Output: Copy Code 1 3 5 7 9 Nested For Loops You can also nest for loops to interact over multiple sequences. Here's an example: pythonCopy code

Nested for loop example

colors = ['red','green','blue'] objects = ['ball','car','house'] for color in colors: for obj in objects: print(f'{color}{obj}) Output: Copy Code red ball red car red house green ball green car green house blue ball bule car blue house Common Use Cases For loops are versatile and can be used in various scenarios: Processing Lists: Iterate through items to perform operations (e.g., filtering, transformations). Generating Sequences: Create sequence of numbers or letters. Date Analysis: Loop through datasets for computations. Practice Peoblems Here are some practice problems to reinforce your understanding of for loops: 1. Sum of Numbers: Write a program that sums the numbers from 1 to 10. 2. 3. pythonCopy Code 4. total = 0for i in range (1, 11): total += iprint (total) # Output shoule be 55 5. 6. Print Even Numbers: Write a program that prints all even numbers between 1 and 20. 7. 8. 9. for i in range (1, 21): if i % 2 == 0: print(i) 10. Conclusion For loops are essential for eaecuting code repeatedly in Python. They provide an efficient way to iterate over sequences and perform tasks without redundancy. As you practice using for loops, you'll discover their versatility in solving various programming problems.