Python: Decision


To control the way the program executes. Sometimes we only want certain codes to run when a specific condition is met. Python use if statements to control flow that depends on conditions.

  1. Introduction
  2. if statement
  3. if-else statement
  4. if-elif statement
  5. Nested If
  6. Ternary

Introduction

To implement decision-making in a program, Python has an if statement, if-else statement, and if-elif statement.

if statement

The simplest form of the if statement. It executes a block of code only if a condition is correct.

if statement flow chart, drawn using WPS Presentation

The syntax:

if condition:
    statement(s)

Condition is an expression that returns a boolean value. the statement(s) executes if the condition is True. After the condition, there is a colon sign (:) ending the first line.

Python does not use curly brackets to indicate blocks. The statements are indented to make them part of the if statement block.

aString = "greet"

if aString == "greet":
    name = input("What is your name? ")
    print("Hello, how are you", name)

Output (User input is written in bold)

What is your name? Cuber
Hello, how are you Cuber
> 

if the condition is False, the statements are skipped and exit the if statement to the next line code.

if-else statement

To specify a statement to execute if the condition is true and another group statement if the expression is false.

The syntax:

if condition:
     statement1(s)
else:
     statement2(s)

if the condition is True statement1(s) will execute. And if the expression is False, statement2(s) will execute.

There is a colon sign (:) after the condition and after the else keyword. The statements are indented to indicate block.

The following example checks the user’s weather information and gives appropriate messages according to weather conditions.

weather =input("Enter weather ")

if weather.lower() == "raining":
    print("Ride bus")
else:
    print("Take a Walk")

Output

Enter weather sunny
Take a Walk
>

The program prompts users to enter weather conditions. The user enters ‘sunny’ and presses enter. the if statement checks if the user entered “raining”. the lower() function makes sure the letter case is the same. The condition returns false, so display “Take a walk”.

if-elif statement

to execute different statements under multiple conditions.

The syntax:

if condition1:
    statement(s)1
elif condition2:
    statement(s)2
else:
    statement(s)3

Each condition is checked from top to down until a condition evaluates to True. If a condition turns out to be true, the statements of that condition are executed. The rest of the condition will not be checked.

If all the condition is False, the statement block written at the else-portion will execute.

If else-portion is not written, the if statement simply exits without any action if all conditions are False.

This type of if statement can have any number elif clause.

score = 67

if score > 80:
    grade = 'A'
elif score > 70:
    grade = 'B'
elif score > 50:
    grade = 'C'
elif score > 40:
    grade = 'D'
else:
    grade = 'F'

print("Score: ", score)
print("Grade: ", grade)

Output

Score: 67
Grade: C
>

Nested If

We can write an if statement inside of another if statement. This is known as nested if statement.

if a > 0:
    print("a is positive")
else:
   if score < 0:
   		print("a is negative")
	else:
		print("a is zero")

Ternary

This ternary operator is written very differently from Java ternary operator (?:). It is a shorter version of the if-else statement. It returns expression1 if the condition is True, otherwise returns expression2.

The Syntax

value1 if condition else value2

For example, if b > 5 is True, then return and assign 2.5 to variable res. Otherwise, if b > 5 is False, there return and assign 1.8 to variable res.

aValue = 5.6
result = 2.5 if aValue > 5 else 1.8

print("The returned value is ", result) 

Output

The returned value is 2.5
>

Here, aValue is 5.6, so the condition aValue > 5 is True. This means the ternary statement returns 2.5 and assigns it to a variable called result.

This ternary statement in the example above is equivalent to following an if-else statement:

b = 5.6

if b > 5:
    res = 2.5
else:
    res = 1.8
    
print("The returned value is ", res)


Leave a comment

Design a site like this with WordPress.com
Get started