Programming and Computational Thinking

How do you use a statement in Python?

How do you use a statement in Python?

This post answers the question how do you use a statement in Python.  Python offers possibility to use several most common for Python statements that are used for different purposes.

The main statements are:

If statement, having the following structure

if expression1 : result1

elseif expression2 : result2

else expression3 : result3

Code example for if statement is published here in our github repository. If statement can have several elseif expressions, or may not have any elseif or else expressions. If the expression is true, the result is executed. if the expression is false – the result is not executed.

While statement is usually used for repeating execution of certain result as soon as expression is true. When the expression is false, loop terminates.  While statement has the following structure:

while expression : result1

else : result2

It means that if that result1 executes while expression is true. When it is not true the result2 executes.

For statement in Python is slightly different to for statements in other programming languages. In Python case for statement can iterate elements of a sequence iterating a certain iteration_list, and the result1 is executed. The For statement have the following structure:

for iteration_list in sequence : result1

else: result2

Very important built-in Python function that can be used in programming is range function.  Arguments of the range function are integers and it has the following structure: range(start, stop[,step]). It works by the formula range_element[i]=start+step*i  and iterates when i0 then range_element[i]<stop. If i0, then range_element[i]>stop.

The example code for this built-in function is published in our github repository.

Educational content can also be reached via Reddit community r/ElectronicsEasy.