Conditional Statements in Python

44 minutes ago 2
Club.noww.in

From the previous tutorials in this series, you now have quite a bit of Python code under your belt. Everything you have seen so far has consisted of sequential execution, in which statements are always performed one after the next, in exactly the order specified.

But the world is often more complicated than that. Frequently, a program needs to skip over some statements, execute a series of statements repetitively, or choose between alternate sets of statements to execute.

That is where control structures come in. A control structure directs the order of execution of the statements in a program (referred to as the program’s control flow).

Here’s what you’ll learn in this tutorial: You’ll encounter your first Python control structure, the if statement.

In the real world, we commonly must evaluate information around us and then choose one course of action or another based on what we observe:

If the weather is nice, then I’ll mow the lawn. (It’s implied that if the weather isn’t nice, then I won’t mow the lawn.)

In a Python program, the if statement is how you perform this sort of decision-making. It allows for conditional execution of a statement or group of statements based on the value of an expression.

The outline of this tutorial is as follows:

  • First, you’ll get a quick overview of the if statement in its simplest form.
  • Next, using the if statement as a model, you’ll see why control structures require some mechanism for grouping statements together into compound statements or blocks. You’ll learn how this is done in Python.
  • Lastly, you’ll tie it all together and learn how to write complex decision-making code.

Ready? Here we go!

Take the Quiz: Test your knowledge with our interactive “Python Conditional Statements” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python Conditional Statements

Test your understanding of Python conditional statements. You'll practice using if, elif, and else to control the flow of your programs.

Introduction to the if Statement

We’ll start by looking at the most basic type of if statement. In its simplest form, it looks like this:

Language: Python
if <expr>: <statement>

In the form shown above:

  • <expr> is an expression evaluated in a Boolean context, as discussed in the section on Boolean Operators in the Operators and Expressions in Python tutorial.
  • <statement> is a valid Python statement, which must be indented. (You will see why very soon.)

If <expr> is true (evaluates to a value that is “truthy“), then <statement> is executed. If <expr> is false, then <statement> is skipped over and not executed.

Note that the colon (:) following <expr> is required. Some programming languages require <expr> to be enclosed in parentheses, but Python does not.

Here are several examples of this type of if statement:

Language: Python
>>> x = 0 >>> y = 5 >>> if x < y: # Truthy ... print('yes') ... yes >>> if y < x: # Falsy ... print('yes') ... >>> if x: # Falsy ... print('yes') ... >>> if y: # Truthy ... print('yes') ... yes >>> if x or y: # Truthy ... print('yes') ... yes >>> if x and y: # Falsy ... print('yes') ... >>> if 'aul' in 'grault': # Truthy ... print('yes') ... yes >>> if 'quux' in ['foo', 'bar', 'baz']: # Falsy ... print('yes') ...

Grouping Statements: Indentation and Blocks

So far, so good.

Read the full article at https://realpython.com/python-conditional-statements/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Read Entire Article