4.1. Control Structures in Python: Conditional Structures

Página 5

4.1. Control Structures in Python: Conditional Structures

Control structures in Python are the components that control the flow of execution of a program. They are fundamental in programming, as they allow the programmer to control the flow of execution of a program based on certain conditions or loops. In this chapter, we're going to focus on one of the main control structures in Python: conditional structures.

Introduction to Conditional Structures

Conditional structures, also known as decision structures, are used to make decisions based on specific conditions. They allow a program to execute different blocks of code depending on whether one or more conditions are true or false. Conditional structures in Python are implemented using the keywords 'if', 'elif' and 'else'.

'if' Conditional Structure

The 'if' conditional structure is the simplest and is used to execute a block of code if a specific condition is true. For example:

age = 20
if age >= 18:
    print("You are of legal age.")

In this example, the code block inside the 'if' will be executed if the variable 'age' is greater than or equal to 18. If the 'age' is less than 18, the code block will be ignored.

>

'else' Conditional Structure

The 'else' conditional structure is used in conjunction with 'if' to execute a block of code if the 'if' condition is false. For example:

age = 16
if age >= 18:
    print("You are of legal age.")
else:
    print("You are a minor.")

In this example, if 'age' is less than 18, the code block inside 'else' will be executed.

'elif' Conditional Structure

The 'elif' conditional structure is an abbreviation of 'else if' and is used to add more conditions to the 'if'. For example:

age = 20
if age < 18:
    print("You are a minor.")
elif age >= 18 and age < 60:
    print("You are an adult.")
else:
    print("You are old.")

In this example, if 'age' is less than 18, the first message will be printed. If 'age' is greater than or equal to 18 and less than 60, the second message will be printed. If 'age' is greater than or equal to 60, the third message will be printed.

Conditional statements are a crucial part of Python programming. They allow programs to make decisions and execute different blocks of code based on specific conditions. Understanding how to use the 'if', 'else' and 'elif' conditional structures is essential for any Python programmer.

In the next section, we'll explore another important control structure in Python: loops. So stay tuned and keep learning!

Now answer the exercise about the content:

What is the role of conditional statements in Python programming?

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

You missed! Try again.

Next page of the Free Ebook:

64.2. Control Structures in Python: Repeating Structures

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