Reading in English Week 3 Day 2
Theme: Conditional if
These constructions allow to condition the execution of one or several sentence blocks to
the fulfillment of one or several conditions.
Conditional sentences if. The if control structure allows a program to execute some
instructions when a condition is fulfilled and its syntax is the following:
if condition:
Sentence
here are the orders that are executed if the condition is true
Block
and which can occupy several lines
The execution of this construction is as follows:
The condition is always evaluated.
- If the result is True, the sentence block is executed
- If the result is False, the sentence block is not executed.
The first line contains the condition to be evaluated and is a logical expression. This line
must always end with a colon (:).
Then comes the block of commands that are executed when the condition is met (ie when
the condition is true). It is important to note that this block must be indented, since Python
uses the indentation to recognize the lines that form a block of instructions. The indent that
is usually used in Python is four spaces, but you can use more or less spaces. When you
type a colon (:) at the end of a line, the editor will automatically indent the following lines. To
end a block, simply go back to the beginning of the line.
“Formando líderes para la construcción de un nuevo país en paz”
Universidad de Pamplona
Pamplona - Norte de Santander - Colombia 1
Tels: (7) 5685303 - 5685304 - 5685305 - Fax: 5682750
SC-CER96940 www.unipamplona.edu.co
Forks if - else. The if - else control structure allows a program to execute some instructions
when a condition is met and other instructions when that condition is not met. Its syntax is
the following:
if condition:
Sentences
here are the orders that are executed if the condition is true block 1
and which can occupy several lines
else:
and here are the orders that are executed if the condition is Sentences
false and that can also occupy several lines block 2
The execution of this construction is as follows:
The condition is always evaluated.
- If the result is True, only the block of sentences 1 is executed.
- If the result is False, only the block of sentences 2 is executed.
The first line contains the condition to be evaluated. This line should always end with a colon
(:).
Then comes the block of orders that are executed when the condition is met (i.e. when the
condition is true). It is important to note that this block must be indented, since Python uses
the indentation to recognize the lines that form a command block. The indent that is usually
used in Python is four spaces, but you can use more or less spaces. When you type a colon
(:) at the end of a line, IDLE will automatically indent the following lines. To end a block,
simply go back to the beginning of the line.
“Formando líderes para la construcción de un nuevo país en paz”
Universidad de Pamplona
Pamplona - Norte de Santander - Colombia 2
Tels: (7) 5685303 - 5685304 - 5685305 - Fax: 5682750
SC-CER96940 www.unipamplona.edu.co
Then comes the line with the command else, which tells Python that the next block must be
executed when the condition is not met (ie when it is false). This line should also always end
with a colon (:). The line with the else command should not include anything else but the
else and the colon. Last is the indented instruction block that corresponds to the else.
Alternatives if - elif - else. The if - else construction can be extended by adding the elif
instruction.
The control structure if - elif - else, allows chaining several conditions. elif is a contraction of
else if. The syntax of this construction is as follows:
if condition_1:
block 1
elif condition_2:
block 2
else:
block 3
- If condition 1 is met, block 1 is executed.
- If condition 1 is not met but condition 2 is met, block 2 is executed.
- If neither condition 1 nor condition 2 are met, block 3 is executed.
You can write as many elif blocks as you need. The else block (which is optional) is executed
if none of the above conditions are met.
Reference: Bartolomé Sintes Marco – Introducción a la Programación con Python
“Formando líderes para la construcción de un nuevo país en paz”
Universidad de Pamplona
Pamplona - Norte de Santander - Colombia 3
Tels: (7) 5685303 - 5685304 - 5685305 - Fax: 5682750
SC-CER96940 www.unipamplona.edu.co