Python Loops Explained Here is How to Master Them - StrataScratch
Python Loops Explained Here is How to Master Them - StrataScratch
Pricing
Written by:
Nathan Rosidi
Author Bio
Main Topics
Share
Follow
for loop
while loop
count = 0
while count < 5:
print(count)
count += 1
One can iterate over any sequence type with a for loop
in Python. For example, the most common sequence
type in data science is a list of data points.
print(temperatures_fahrenheit)
while condition:
# code to execute
temperature = next(temperature_stream)
while temperature != "Stop":
print(f"Received temperature: {temperature}")
temperature = next(temperature_stream)
You can use a nested while loop if you have more than
one condition inside each other. It might look
complicated. However, the application of it is
straightforward, which you can understand from the
following example.
try:
batch = next(data_stream)
while batch != "Stop":
print(f"Processing {batch}")
data_point = next(data_stream)
while data_point != "End":
print(f" Data point: {data_point}")
data_point = next(data_stream)
batch = next(data_stream)
except StopIteration:
print("Finished processing all batches.")