4.7. Control Structures in Python: If-else

Página 11

4.7 Control Structures in Python: If-else

The Python programming language, like many others, provides control structures that allow developers to manipulate the flow of execution of a program. Among these structures, if-else conditional statements stand out. They are essential for building systems with Python and Django, as they allow the program to make decisions based on certain conditions.

What is the If-else structure in Python?

In Python programming, the if-else statement is used to execute a block of code if a specific condition is true. If the condition is false, a different block of code is executed. The basic structure of if-else in Python is as follows:

if condition:
    # block of code to be executed if condition is true
else:
    # block of code to be executed if condition is false

How does the If-else structure work?

When Python encounters an if statement, it evaluates the condition enclosed in parentheses. If the condition is true (that is, evaluates to True), Python executes the block of code that immediately follows the if statement. If the condition is false (that is, evaluates to False), Python skips the block of code after the if statement and executes the block of code after the else statement.

Example usage of If-else structure

Suppose we are creating a sales system and we want to apply a 10% discount for purchases over R$100. We can use an if-else statement to implement this logic:

purchase_value = 150

if purchase_value > 100:
    purchase_value = purchase_value * 0.9
    print("Discount applied. Final purchase amount: ", purchase_value)
else:
    print("Final purchase amount: ", purchase_value)

If the purchase amount is greater than $100, Python executes the block of code after the if statement, applying the discount and printing the final purchase amount. If the purchase amount is less than or equal to $100, Python executes the block of code after the else statement, printing only the final purchase amount.

Importance of the If-else structure in creating systems

The if-else statement is a powerful tool when building systems with Python and Django. It allows developers to create programs that can make decisions and adapt to different situations. For example, in a user management system, you can use if-else statements to check whether a user has permission to access certain functionality.

Conclusion

The if-else control structure is a fundamental feature in Python programming. It allows programs to make decisions and execute different blocks of code based on certain conditions. Mastering the use of if-else statements is crucial for anyone wanting to build robust and flexible systems with Python and Django.

Now answer the exercise about the content:

What is the function of the if-else control structure in the Python programming language?

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

You missed! Try again.

Next page of the Free Ebook:

124.8. Control Structures in Python: While

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