Free Ebook cover System creation course with Python and Django complete

System creation course with Python and Django complete

New course

176 pages

Control Structures in Python: Decision Structures

Capítulo 9

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

Decision structures in Python, such as if, elif, and else, allow code execution based on specific conditions. They enable conditional flow control, executing certain blocks only if particular conditions are met. This functionality is essential for creating complex, dynamic programs where different pathways are taken based on varying conditions.

Next chapter

Control Structures in Python: Loops

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.