Skip to main content
Practice

Using Loops to Execute Code Multiple Times

In programming, there are often instances where you need to perform the same task multiple times.

For example, you may need to retrieve values from a large dataset with thousands of entries or continuously perform tasks until a specific condition is met.

Instead of writing the code multiple times, you can use loops to handle these tasks efficiently.

In this lesson, we will explore what loops are and how to utilize them in Python.


Iterable Types

In Python, iterable types refer to objects that can be sequentially accessed in a loop.

Examples of iterable types include strings, which are composed of multiple characters, lists, which are collections of elements enclosed in square brackets, and tuples, which are immutable collections of elements.

Loops can access these iterable types and process each element according to the unique code within the loop.

For instance, in the example for item in fruits: on the practice screen, the loop accesses each element (apple, banana, cherry) in the defined fruits list ["apple", "banana", "cherry"] and executes the unique loop code print(item).


Using Loops in Python

In Python, loops are used to handle repetitive tasks primarily with for loops and while loops.

From the next lesson onwards, we will learn how to use for and while loops with examples.

Try typing the for loop example on the practice screen, and let's dive deep into the details of for loops in the next lesson :)

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.