Python - Control Structures
Conditionals are a fundamental part of programming. In this
presentation, we will explore the basics of Python conditionals with
plenty of examples to help you understand how they work.
by Anik Acharjee
Simple if statement
Example 1 Example 2 Example 3
Using the simple if statement Checking if you have the Determining if you have
to control traffic light. necessary tickets to attend a enough money to eat at your
music concert. favorite restaurant.
If-else statement
Example 1 Example 2 Example 3
Use the if-else statement Checking if a movie is Checking for duplicates in
to check if you have worthy of your time based a data set using if-else
enough money to buy a on its ratings and genre. conditionals.
new gadget.
Elif statement
Example 2
Determining the grade of a student
depending on their scores - A, B, C, D, or F.
1 2 3
Example 1 Example 3
Determining what you can do for fun Deciding which mode of transport is most
depending on the weather - sunny, cloudy, economical for a long-distance trip - train,
rainy. plane or bus.
Nested if statement
Example 1 Example 2 Example 3
Determining the name of a Checking if a soldier is a Checking if an input word is
baby animal depending on its veteran or a rookie based on available in the language
type. the number of wars they have translation app's database.
participated in.
Iteration Control (Looping)
Example 1 Example 2 Example 3
Printing the prime Printing the Fibonacci Checking the length of a
numbers from a given list, series up to a certain string, using a for loop.
using a for loop. number, using a while
loop.
Break statement
Example 2
Checking for a specific number in a random
list, using a for loop and break statement.
1 2 3
Example 1 Example 3
Printing the first ten even numbers using a Printing the first occurrence of a string in a
while loop and break statement. list, using a for loop and break statement.
Continue statement
Example 1 Example 2 Example 3
Checking how many hours you Checking which students have Determining which musical
have worked in a week, passed or failed in a class, notes were hit correctly,
ignoring the breaks. skipping the absentees. ignoring any wrong notes.
Pass statement
1 Example 1 2 Example 2 3 Example 3
Creating an empty Setting up a framework Providing structures for
function and adding it for a class and adding future error handling
later. the methods later. codes.