Published on

Python for Data Analysis | Day 14 Loops in Python | Beginners to Pro

Introduction

Python has become a widely recognized programming language, particularly among data professionals specializing in data science and analytics. Organizations around the globe leverage Python to extract valuable insights from their datasets, gaining a competitive edge over rivals. This article marks Day 14 of our Python learning journey here at Excel Island, where our objective is to master Python over the course of 100 days.

Progress Update

As of today, we have completed one course out of nine on our learning track. We are currently working on the second course, which consists of four chapters and one project. We have thoroughly covered the concept of loops in Python, and we're excited to start our first project soon.

Introduction to Loops

In our previous live streams, we have explored the basics of Python, including data types, variables, lists, and functions. We moved on to intermediate topics such as loops, specifically for loops and while loops. Loops are essential for iterating over data structures, allowing for the automation of repetitive tasks in our code.

Looping Over Dictionaries

When looping over dictionaries in Python, we can use the items() method to retrieve both keys and values.

for key, value in my_dict.items():
    print(f'The key is (key) and the value is (value)')

In this example, keys represent countries, and values denote their populations. It’s crucial to note that dictionaries are unordered collections, meaning the order of elements can vary.

Looping Over NumPy Arrays

Similarly, NumPy arrays can also be iterated through, and the method of iteration varies based on whether it's a 1D or 2D array.

  • For a 1D NumPy array, a simple loop will suffice:
for value in my_array:
    print(value)
  • For a 2D NumPy array, we utilize a function called nditer to navigate through each element:
import numpy as np

for value in np.nditer(my_2d_array):
    print(value)

Both loops will help in printing out all elements of the arrays efficiently.

Practical Application

We practiced looping by creating a loop for a dictionary containing European country names and their capitals. We printed results in a formatted manner for better clarity. We also demonstrated how to create a 2D NumPy array combining players' heights and weights and iterating over them effectively.

Here is an example of how to loop through a 2D array:

import numpy as np

combined_array = np.array([[height_1, weight_1], [height_2, weight_2]])
for value in np.nditer(combined_array):
    print(f'The value is: (value)')

Conclusion and Next Steps

We’ve accomplished a significant amount today, learning how to iterate through both dictionaries and NumPy arrays. Next time, we will dive deeper into data frame structures to practice looping over those as well.

Before we wrap up today's session, don’t forget to support the channel! Like, subscribe, and hit the notification bell to stay updated on our future content.


Keywords

Python, data analysis, loops, data structures, dictionary, NumPy, 1D array, 2D array, Jupyter Notebook, data frame.


FAQ

1. Why is Python popular among data professionals?
Python is favored because of its simplicity, versatility, and the extensive libraries available for data analysis, such as pandas and NumPy.

2. What are loops in Python used for?
Loops are used to iterate over data structures like lists, dictionaries, and arrays, allowing for efficient processing of elements.

3. How can I loop over a dictionary in Python?
You can use the items() method in a for loop to access both keys and values in a dictionary.

4. What is the difference between looping over a 1D and a 2D NumPy array?
A 1D array can be looped using a simple for loop, while a 2D array requires using the nditer function to properly access each element.

5. What should I expect in the next session?
In the upcoming session, we'll explore how to loop through data frame structures and apply these concepts practically in data analysis scenarios.