Python Code Example: multi-branches

In Python there are also multiple branches represented by the elif statement

The if-conditions are evaluated in sequence until one of them is true. Then finally the code section belonging to this condition is executed and the multiple branching handling is finished. The else-part is only executed if none of the conditions is true.

LineDescription
1Prompts the user to enter a number. This is stored in the variable firstNumber
2Prompts the user to enter a number. This is stored in the variable secondNumber
4 – 5If the value of the variable firstNumber is greater than the value of the variable secondNumber, the string “value_first_variable > value_second_variable” is output
6 – 7If firstNumber is smaller than secondNumber, the string “value_first_variable < value_second_variable” is output
8 – 9Otherwise the string “value_first_variable = value_second_variable” is output
firstNumber = input("Please enter first number: ")
secondNumber = input("Please enter second number: ")

if (firstNumber > secondNumber):
    print(firstNumber + " > " + secondNumber)
elif (firstNumber < secondNumber):
    print(firstNumber + " < " + secondNumber)
else:
    print(firstNumber + " = " + secondNumber)
Output
Please enter first number: 5
Please enter second number: 8
5 < 8