111763 Views
85221 Views
83639 Views
51774 Views
49949 Views
48472 Views
Obsidian - the best tool for Makers
10 Projects for your Raspberry Pi Pico
Raspberry Pi Telegraf Setup with Docker
Setting Up Dynamic DNS on a Raspberry Pi for Self-Hosting
Raspberry Pi WordPress Setup with Docker
Raspberry Pi WireGuard VPN Setup with Docker
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
20% Percent Complete
By Kevin McAleer, 2 Minutes
In this lesson, we’ll explore how to control the flow of your Python programs using conditional statements (if, elif, else), loops (for, while), and functions.
if
elif
else
for
while
functions
In Python, we use if, elif (else if), and else to control conditional flows. They help the program to make decisions based on certain conditions.
age = 20 if age < 13: print("You are a kid.") elif age < 20: print("You are a teenager.") else: print("You are an adult.")
Python has two types of loops - for and while.
for i in range(5): print(i)
counter = 0 while counter < 5: print(counter) counter += 1
Functions are reusable pieces of code. They only run when called and can receive parameters and return data.
# Defining a function def greet(name): return f"Hello, {name}!" # Calling a function message = greet("Alice") print(message) # Prints "Hello, Alice!"
In this lesson, you’ve learned about controlling the flow of your Python programs using conditional statements, loops, and functions. Understanding control flow is crucial in writing dynamic and flexible Python programs.
< Previous Next >