4.5. Control Structures in Python: Decision Structures

Página 9

4.5 Control Structures in Python: Decision Structures

Control structures in Python are programming tools that allow the developer to manipulate the flow of execution of a program. They are fundamental to the creation of complex and dynamic systems, allowing the code to be executed in a conditional or repeated way. In this chapter, we will focus on decision structures, which are a crucial part of control structures.

Decision Frameworks

Decision structures, also known as conditional structures, allow code to execute based on certain conditions. In Python, the main decision structures are 'if', 'elif' and 'else'.

If

The 'if' is the most basic decision structure in Python. It allows a block of code to be executed if a certain condition is true. The 'if' syntax is as follows:

if condition:
    code_block

In this case, 'condition' is an expression that evaluates to true or false, and 'code_block' is the set of statements that will be executed if the condition is true.

Elif

The 'elif', which is an abbreviation of 'else if', is used to add more conditions to the 'if'. It allows a block of code to be executed if the 'if' condition is false and the 'elif' condition is true. The 'elif' syntax is as follows:

if condition1:
    code_block1
elif condition2:
    code_block2

In this case, 'condition1' and 'condition2' are expressions that evaluate to true or false, 'code_block1' is the set of instructions that will be executed if 'condition1' is true, and 'code_block2' is the set of statements that will be executed if 'condition1' is false and 'condition2' is true.

Else

The 'else' is used to execute a block of code if all the previous conditions are false. The 'else' syntax is as follows:

if condition1:
    code_block1
elif condition2:
    code_block2
else:
    code_block3

In this case, 'code_block3' is the set of instructions that will be executed if 'condition1' and 'condition2' are false.

Examples of Usage

Let's see an example of how these decision structures can be used in Python. Suppose we want to create a program that classifies a person as a minor, adult, or elderly based on their age:

age = 25

if age < 18:
    print('Minor')
elif age < 60:
    print('Adult')
else:
    print('Elderly')

In this example, the person will be classified as 'Minor' if their age is under 18, 'Adult' if their age is between 18 and 59, and 'Senior' if their age is 60 or older.

p>

In summary, decision structures are powerful tools that allow the flow of execution of a program to be dynamically controlled. They are essential for creating complex and flexible systems, and are a fundamental component of any programming language.

Now answer the exercise about the content:

What is the role of decision structures in Python?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

104.6. Control Structures in Python: Loops

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text