A Professional’s Tutorial to Python If + While Loop

Control flow, a fundamental Python term and concept

Adam Ross Nelson
6 min readSep 17, 2023

--

For an overview of these tutorials, click here (or click the image).

Welcome to forth, in a series of tutorials that teach beginner Python specifically for aspiring data scientists. For an overview of these tutorials, click here.

Introduction

Imagine planning a shopping trip to your favorite supermarket. Every aisle and shelf represents potential decisions. With a well-prepared shopping list, you have a clear plan: pick up some tomatoes, then bread, followed by milk.

However, when you reach the dairy section, you find that your preferred brand of milk is out of stock. Now, you have to make a decision on the spot: choose another brand, skip the milk, or maybe check another store.

In programming, particularly in Python, flow control works much like this shopping scenario. Using statements like if and while, your code can make decisions (“make decisions”) on the fly. The code can adjust to different conditions, and ensure that your program’s “shopping trip” is both efficient and adaptable to unexpected scenarios.

Side note: Don’t forget the fruit loops! 🥣

Understanding how to control the flow of your code while it’s running is akin to making your…

--

--

Adam Ross Nelson

Ask about my free career course. I mentor new (💫) and aspiring data scientists enter (🚪) and level up (📈) in the field.