In this lesson, we will learn about control structures in Python and how to use them to control the flow of your program.
In Python, there are three main control structures that you can use: if
statements, for
loops, and while
loops.
If statements
An if
statement allows you to execute a block of code only if a certain condition is met.
Here is an example of how to use an if
statement:
age = 30
if age >= 18:
print('You are old enough to vote.')
In this example, the if
statement checks if the value of the age
variable is greater than or equal to 18. If it is, the code block within the if
statement is executed and the message 'Youare old enough to vote.' is printed to the console. If the condition is not met, the code block is skipped.
You can also use the else
keyword to specify a block of code that should be executed if the condition is not met.
Here is an example of how to use an if
statement with an else
clause:
age = 15
if age >= 18:
print('You are old enough to vote.')
else:
print('You are not old enough to vote.')
In this example, the if
statement checks if the value of the age
variable is greater than or equal to 18. If it is, the code block within the if
statement is executed and the message 'You are old enough to vote.' is printed to the console. If the condition is not met, the code block within the else
clause is executed and the message 'You are not old enough to vote.' is printed to the console.
You can also use the elif
keyword (short for 'else if') to specify additional conditions that should be checked.
Here is an example of how to use an if
statement with multiple elif
clauses:
age = 15
if age >= 18:
print('You are old enough to vote.')
elif age >= 16:
print('You are old enough to drive.')
elif age >= 13:
print('You are old enough to watch rated PG movies.')
else:
print('You are not old enough to do any of the above.')
In this example, the if
statement checks if the value of the age
variable is greater than or equal to 18. If it is, the code block within the first if
statement is executed and the message 'You are old enough to vote.' is printed to the console. If the condition is not met, the elif
clauses are checked in order. If the condition of one of the elif
clauses is met, the corresponding code block is executed and the appropriate message is printed to the console. If none of the conditions are met, the code block within the else
clause is executed and the message 'You are not old enough to do any of the above.' is printed to the console.
For loops
A for
loop allows you to execute a block of code multiple times, based on a sequence of items.
Here is an example of how to use a for
loop:
fruits = ['apple', 'banana', 'orange', 'mango']
for fruit in fruits:
print(fruit)
In this example, the for
loop iterates over the list of fruits and prints each fruit to the console.
You can also use the range()
function to create a sequence of numbers to iterate over.
Here is an example of how to use a for
loop with the range()
function:
for i in range(5):
print(i)
This will print the numbers 0 through 4 to the console.You can also specify a start and end value for the range()
function, as well as a step value.
Here is an example of how to use the range()
function with start, end, and step values:
for i in range(2, 10, 2):
This will print the numbers 2, 4, 6, 8 to the console.
While loops
A while
loop allows you to execute a block of code multiple times as long as a certain condition is met.
Here is an example of how to use a while
loop:
i = 0
while i < 5:
print(i)
i += 1
In this example, the while
loop will print the numbers 0 through 4 to the console. The i += 1
statement is used to increment the value of i
by 1 after each iteration of the loop.
It's important to make sure that the condition of the while
loop will eventually become False
, otherwise the loop will run indefinitely and you will have an infinite loop.
Breaking out of loops
Sometimes, you may want to break out of a loop early if a certain condition is met. In Python, you can use the break
statement to break out of a loop.
Here is an example of how to use the break
statement:
for i in range(10):
if i == 5:
break
print(i)
In this example, the for
loop will iterate over the numbers 0 through 9. When the value of i
is 5, the break
statement is executed and the loop is terminated. This will print the numbers 0 through 4 to the console.
Continuing loop iterations
Sometimes, you may want to skip the rest of the current iteration of a loop and move on to the next one. In Python, you can use the continue
statement to do this.
Here is an example of how to use the continue
statement:
for i in range(10):
if i % 2 == 1: # if i is odd
continue
print(i)
In this example, the for
loop will iterate over the numbers 0 through 9. If the value of i
is odd, the continue
statement is executed and the loop continues to the next iteration. This will print the numbers 0, 2, 4, 6, 8 to the console.