Control Structures - Data Analysis
Advance your Python skills with my second free tutorial on Control Structures! Learn if statements, loops, and more, paving the way to a comprehensive premium project. This is the second of four essential lessons in my beginner-friendly Python series.
Lesson 2 / 4 (Plus 5th Premium Final Project)
If you landed here without doing the first of the lessons in this Data Analysis. I recommend heading to that lesson if you want to follow fully. Of course you are welcome to do this lesson if you want to know more about Control Structures.
Introduction
Welcome to the second lesson in my free Python series! In this tutorial, we will explore control structures in Python, which are crucial for making decisions and controlling the flow of your program. You'll learn about conditional statements (if
, elif
, else
) and loops (for
, while
), enabling your programs to handle different conditions and repeat tasks efficiently. This lesson is a stepping stone towards the final premium project.
The purpose of this tutorial series "Data Analysis" is to learn in a step by step way some fundamental Python principles so you can create a simple program later to perform data analysis.
1. Understanding if
Statements
if
statements let your program execute certain code only when specific conditions are met.
- Basic
if
Statement:
age = 18
if age >= 18:
print("You are an adult.")
What does it do: This code checks if age
is greater than or equal to 18. If true, it prints a message.
2. Using elif
and else
elif
(else if) and else
extend if
statements for multiple conditions.
- Example:
age = 16
if age >= 18:
print("You are an adult.")
elif age < 18 and age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
What does it do: This code prints different messages based on the value of age
.
3. Introduction to Loops
Loops are used for repeating tasks. Python offers for
and while
loops.
- Using a
for
Loop:
for i in range(5):
print("Iteration", i)
What does it do: The for
loop runs the print statement five times, with i
taking values from 0 to 4.
- Using a
while
Loop:
count = 0
while count < 5:
print("Count is", count)
count += 1
What does it do: The while
loop repeatedly prints and increments count
until it reaches 5.
Try changing the number from 5 to another number, say for example: 10. Watch what the code does. See how each of them behave. You can also consider how these two functions can be used.
A good way to view these two functions would be a video game.
Imagine a game where a player must open a set number of treasure chests. Typical of missions or achievements where you need to earn or complete a task a certain number of times.
- For Loop Purpose: In this game, a
for
loop can be used to repeat an action a specific number of times. For instance, if there are 5 treasure chests to open, the loop can run 5 times, once for each chest.
This loop will result in the game automatically opening each of the 5 chests in turn and displaying a message about the found treasure. The loop knows exactly how many chests there are, so it stops automatically after the 5th chest.
Next consider a game where a player must continue to defeat enemies until their health points (HP) drop to zero.
- While Loop Purpose: In this scenario, a
while
loop is useful because the number of enemies the player has to defeat isn't predetermined. The loop continues as long as the player's health is above zero.
The while
loop keeps the player fighting enemies in each iteration. After each enemy attack, the player's HP decreases. The loop checks the player's HP at the beginning of each iteration and continues the battle until the HP falls to zero or below, at which point the loop stops.
- For Loop: Best when the number of iterations is known in advance (a fixed number of treasure chests).
- While Loop: Ideal for situations where the loop must continue until a certain condition changes (player's HP drops to zero).
In both cases, loops help create repetitive actions in the game, making the gameplay more dynamic and automated.
4. Conclusion and Next Steps
Nice job! You've learned how to use control structures in Python, allowing your programs to make decisions and repeat actions. These concepts are foundational for any programmer. Up next, we'll delve into functions in Python, another critical building block for your programming skills and the upcoming premium project.
Support Educational Content
All tips to contribute to time and services to provide free and easy to follow content for students and beginners in Programming, Networking, Technology and Homelabs.
Donate