Loops

Loops let you repeat a block of code multiple times without writing it out again and again. This is essential in data science, where you'll frequently need to process every row of a dataset or repeat a calculation until a condition is met.

The for Loop

A for loop iterates over a sequence, such as a list, string, or range of numbers.

scores = [72, 85, 90, 66]

for score in scores:
    print(score)

The while Loop

A while loop continues to run as long as its condition remains true.

count = 0

while count < 5:
    print(count)
    count += 1
In real data science projects, loops are often replaced with faster, vectorized operations from libraries like Pandas and NumPy, but understanding how loops work is essential before learning those optimizations.

Coming Up

With loops covered, the final topic in this section looks at command line arguments, which let you pass input into a Python script directly from the terminal.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course