Control Statements in Python
| Site: | Dr. B.R. Ambedkar Open University Online Learning Portal |
| Course: | Basics of Programming with Python |
| Book: | Control Statements in Python |
| Printed by: | Guest user |
| Date: | Saturday, 30 May 2026, 1:57 AM |
1. If Statement
The Python if executes a block of statements conditionally, based on a Boolean expression.
Syntax:
if expression : statement_1 statement_2 ....
In this case, expression specifies the conditions which are based on Boolean expression. When a Boolean expression is evaluated it produces either a value of true or false. If it evaluates to True, the indented block of statements is executed. This block of code is called an "if block."
Example:
x = 5
if x > 3:
print("x is greater than 3")
"Python if elif else" by w3resource is licensed under CC BY-SA 4.0
2. if-else Statement
In Python, the if..else statement has two blocks: one for when the condition is True and another for when the condition is False.
Syntax:
if expression :
statement_1
statement_2
....
else :
statement_3
statement_4
....
If the expression is True, the statements after if are executed; otherwise, the statements after else are executed. In the following example the program will print the second print statement as the value of a is 10.
Output:
Value of a is 10
"Python if elif else" by w3resource is licensed under CC BY-SA 4.0
3. if-else if - else Statement
When there are multiple conditions to check, you can use the elif clause (short for "else if"). Python evaluates each condition one by one until it finds one that is True. If none are True, the else block is executed.
Syntax:
if expression1 :
statement_1
statement_2
....
elif expression2 :
statement_3
statement_4
....
elif expression3 :
statement_5
statement_6
....................
else :
statement_7
statement_8
In the above case Python evaluates each expression (i.e. the condition) one by one and if a true condition is found the statement(s) block under that expression will be executed. If no true condition is found the statement(s) block under else will be executed. In the following example, we have applied if, series of elif and else to get the type of a variable.
Output:
Type of the variable is Complex
"Python if elif else" by w3resource is licensed under CC BY-SA 4.0
4. while loop
Loops are used to repeatedly execute a block of program statements. The basic loop structure in Python is while loop. Here is the syntax.
Syntax:
statement_1
statement_2
....
The while loop runs as long as the expression (condition) evaluates to True and execute the program block. The condition is checked every time at the beginning of the loop and the first time when the expression evaluates to False, the loop stops without executing any remaining statement(s). The following example prints the digits 0 to 4 as we set the condition x < 5.
Output:
0 1 2 3 4
One thing we should remember that a while loop tests its condition before the body of the loop (block of program statements) is executed. If the initial test returns false, the body is not executed at all. For example the following code never prints out anything since before executing the condition evaluates to false.
5. for loop
The next type of loop in Python is the for loop. Unlike in most languages, for requires some __iterable__ object like a Set or List to work.
The output:
The output looks very familiar, but the program code looks different. The first line uses the range function. The range function uses two arguments like this range(start,finish). start is the first number that is produced. finish is one larger than the last number. Note that this program could have been done in a shorter way:
Here are some examples to show what happens with the range function:
Remark: for the current version of Python, we need to pass the range function to list() to actually print a list. For instance, we need to use list(range(1,10)) to get [1, 2, 3, 4, 5, 6, 7, 8, 9].
Another way to use the range() function in a for loop is to supply only one argument:
The above code acts exactly the same as:
with 0 implied as the starting point. The output is
"For Loops" by en.wikibooks.org is licensed under CC BY-SA 4.0
6. break Statement (Keyword)
There can be times when you want to prematurely end a while statement. The break statement can be used to accomplish this. The break statement can be used to prematurely end the while statement that it's nested in. This means it will end only one while statement, not all of them. An example is given below.
The above example demonstrates that a while loop can be ended, even if it is True. You should also note from the example that the break statement doesn't require a colon (:), which means new indentation isn't needed. Putting a colon at the end of break will cause an error; the omission of the colon isn't optional, it's mandatory.
"The Break Statement" by wikiversity is licensed under CC BY-SA 4.0
7. Continue Statement (Keyword)
Although the break statement comes in handy, the continue statement is just as useful. Unlike the break statement, the continue statement will stop the current execution of code and go back to the beginning of the while statement. This can be used to skip part of the code and not end the loop at the same time. The continue statement is used just like the break statement. An example is given below.
Although the example could have easily omitted the continue, it demonstrates how it works. The continue statement is used to reduce the amount of indenting and nesting used; it would be easier to check five conditions and use a continue if they fail, than to nest five if statements. This can reduce the amount of typing needed and, as a result, will increase readability and code clarity.
"The Continue Statement" by wikiversity is licensed under CC BY-SA 4.0