Basics of working with Loops and Data Structures

Introduction

In the last tutorial, you learned about Python’s core data structures: lists, tuples, and dictionaries. Now it’s time to combine them with loops to process collections of data. This is where Python becomes truly powerful — you can automate repetitive tasks, analyse datasets, and build practical mini-projects.

 

Step 1: Looping Through Lists

Lists are often processed with for loops.

Code:

numbers = [10, 20, 30, 40]

for num in numbers:

    print(num * 2)

 

Output:

20

40

60

80



Each item in the list is accessed one by one, and you can perform operations on it.

 

Step 2: Looping Through Tuples

Tuples work the same way, but since they’re immutable, you usually just read values.

Code:

coordinates = (2, 4, 6)

for point in coordinates:

    print(point)

 

Output:

2

4

6


 

Step 3: Looping Through Dictionaries

Dictionaries are slightly different because they store key-value pairs.

student = {

    "name": "Deepak",

    "age": 30,

    "course": "Python"

}

 

for key, value in student.items():

    print(key, ":", value)

 

Output:

name : Deepak

age : 30

course : Python

 


 .items() gives both the key and value at once.

 


Step 4: Nested Loops with Data Structures

You can use nested loops to process complex data.

students = [

    {"name": "Alice", "age": 22},

    {"name": "Bob", "age": 25},

    {"name": "Charlie", "age": 23}

]

 

for student in students:

    for key, value in student.items():

        print(key, ":", value)

    print("---")

 

Output:

name : Alice

age : 22

---

name : Bob

age : 25

---

name : Charlie

age : 23

---

 


 

Step 5: Building a Mini Project — Contact Book

Let’s create a simple contact book using a dictionary and loops.

Code:

contacts = {

    "Alice": "123-456-7890",

    "Bob": "987-654-3210",

    "Charlie": "555-666-7777"

}

 

print("Contact List:")

for name, phone in contacts.items():

    print(f"{name}: {phone}")

 

Output:

Contact List:

Alice: 123-456-7890

Bob: 987-654-3210

Charlie: 555-666-7777

 

This is a practical example of how loops and dictionaries work together.

 



Mini Exercise

Try this challenge:

·       Create a list of 5 numbers.

·       Use a loop to calculate the square of each number.

·       Store the results in a dictionary where the key is the number and the value is its square.

·       Print the dictionary.

 

Hint:

numbers = [1, 2, 3, 4, 5]

squares = {}

 

for num in numbers:

    squares[num] = num ** 2

 

print(squares)

 

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

 




 

Conclusion

In this tutorial, you learned how to:

·       Loop through lists, tuples, and dictionaries

·       Use nested loops for complex data

·       Build a simple contact book project

·       Combine loops and data structures for practical tasks

 

Loops + data structures are the engine of automation in Python. With these tools, you can process large datasets, build applications, and create efficient workflows.

In the next tutorial, we’ll explore Python modules and libraries — how to import and use external code to expand your programs.