Python Control Structures
Python Control Structures
by Anik Acharjee
Simple if statement
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
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
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
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