Learn Python

For Loops in Python

What are For Loops?

For loops are a fundamental concept in programming that allow a sequence of instructions to be repeated multiple times. They automate repetitive tasks and iterate over a collection of items. A for loop consists of three essential components:

  1. Initialization: Sets the loop control variable to its initial value.
  2. Condition: Checked before each iteration to determine if the loop should continue or terminate.
  3. Increment/Decrement: Updates the loop control variable after each iteration.

Why Use For Loops in Python?

For loops are essential in Python programming for iterating over a sequence or an iterable object. They are used to repeat a set of statements a specific number of times, based on the elements in a sequence or collection. The basic structure includes an iterator variable, followed by the in keyword, and the sequence or iterable object to iterate through.

Example Use Cases

  • Calculating the square of each number in a list.
  • Accessing each key-value pair in a dictionary.

Benefits of Using For Loops

  1. Iteration Over Sequences: Iterate over lists, strings, tuples, or other iterable objects.
  2. Conciseness: Repeat a block of code efficiently, making it more readable and maintainable.
  3. Flexibility: Control the iteration process with break and continue statements.
  4. Simplicity: Easy to understand and implement, even for beginners.

Basic Syntax of a For Loop

The basic syntax involves three parts: initialization, condition, and increment.

Initialization

The loop variable is created or initialized to a specific value.

Condition

Checked before each iteration, it determines if the loop should continue or terminate.

Increment

Updates the loop variable after each iteration.

Example

for i in range(5):
    print(i)

Understanding the Basic Structure

The Range Function

Generates a sequence of numbers using start, stop, and step values.

Nested For Loops

Allows iteration over multiple lists or performing repetitive tasks within a loop.

Break and Continue Statements

  • break: Terminates the loop prematurely.
  • continue: Skips to the next iteration.

Else Statement with Loops

Executes a block of code when the loop completes without being interrupted by a break statement.

Enumerate Function

Facilitates iteration while keeping track of the index.

Defining a Loop Variable

A loop variable controls the iteration process, typically incremented or updated during each iteration.

Example

for i in range(5):    
		print(i)

Loop Iteration Over an Iterable Object

Example

Iterating over a set:

my_set = {1, 2, 3, 4, 5}
for item in my_set:    
		print(item)

Using the Range Function

Generates a sequence of numbers:

for i in range(1, 10, 2):    
		print(i)

Handling Sequences of Characters with a For Loop

Example

Iterating over a string:

my_string = "Hello, World!"
for char in my_string:    
		print(char)

Looping Through Lists and Iterables

Iterating Over a Python List

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

Using List Comprehension

Create a new list from an existing list:

old_list = [1, 2, 3, 4]
new_list = [x**2 for x in old_list]

Accessing Individual Items in a List

Using indexing:

fruits = ["apple", "banana", "orange"]
print(fruits[0])  # Output: apple

Looping Through an Iterable Object

Iterate over each element:

my_list = [1, 2, 3, 4, 5]
for element in my_list:    
		print(element) 

For loops in Python provide a powerful and flexible way to handle repetitive tasks, making your code efficient and readable.

Written by

Master Python by choosing your ideal learning course

View all courses

Create a free account to access the full topic

Sign up with Google
Sign up with Google
Sign up with JetBrains
Sign up with JetBrains
Sign up with Github
Sign up with GitHub
Coding thrill starts at Hyperskill
I've been using Hyperskill for five days now, and I absolutely love it compared to other platforms. The hands-on approach, where you learn by doing and solving problems, really accelerates the learning process.
Aryan Patil
Reviewed us on