Python - Decision Making

Decision making statements are the statements which is used to take decision(action) according to the condition.

Decision making is a structure of multiple expressions which produce either true or false. You need to determine which action to take at the time of condition True or False.

if condition:
    print("Statement which you want to print when condition is true")
    print("Another statement")
else:
    print("Statement which you want to print when condition is false")
    print("Another statement")

Python does not use braces{} to indicate blocks of code for defining flow control, functions, and classes. Blocks of code are denoted by line indentation, all statements within the block must be indented the same amount of spaces.

In Python all continuous lines indented with the same number of spaces would form a block

Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.

Multi-Line Statements

Statements in Python typically end with a new line. Python, however, allows the use of the line continuation character (\) to denote that the line should continue.

a = '1' \
    + '2' \
    + '3' 
print (a)

The statements contained within the [], {}, or () brackets do not need to use the line continuation character.

If - Elif - Else

if-elif-else statement is a block of given conditions out of which will execute only one which is true

Example 1: Input number maximum 5, it will print in words.

a = int(input("Enter number"))
if a==1:
    print("One")
elif a==2:
    print("Two")
elif a==3:
    print("Three")
elif a==4:
    print("Four")
elif a==5:
    print("Five")
else:
    print("Invalid! Try Again")

Exercise: Python Decision Making