Python Fundamental — If Else Conditions

M. Rizky Maulana
3 min readApr 1, 2021

Yeaayyyyy today we are going to this article. But, before that you can check my previous article :

If Condition

If Condition is used to anticipate the conditions that occur when the program is running and determine what actions will be taken in accordance with the conditions.

In python there are several statements / conditions including if, else and elif. The if condition is used to execute code if the condition is True.

If the condition is false False then the if statement / condition will not be executed.

Below is an example of using an if condition in Python

The if condition is a condition that will be executed by the program if the value is true or TRUE

value = 8#If the condition is true / TRUE then the program will execute the command belowif (value> 7):   print (“Eight is Greater Than Seven”) #Condition True, Executed#If the condition is false / FALSE then the program will not execute the command belowif (value> 10):print (“Eight is greater than ten”) #Incorrect condition, so it will not be executed

From the example above, if the program is run, it will print the string “Eight Greater Than Seven” at the first if. In the second if statements are false, so the print command (“Eight is Greater Than Ten”) will not be executed.

If Else Conditions

If else conditions is not only used to determine what actions will be taken in accordance with the conditions, but is also used to determine what actions will be taken / executed if the conditions are not suitable.

In python there are several statements / conditions including if, else and elif The if condition is used to execute code if the condition is true.

The if else condition is a condition where if the statement is true, the code in if will be executed, but if it is false, it will execute the code in else.

Below is an example of using the if else condition in Python

#The if else condition is if the condition is TRUE it will be executed in if, but if it is FALSE then the code will be executed in elsevalue = 4#If the statement in if is TRUE then if will be executed, but if it is FALSE the code in else will be executed.if (value> 7):print (“Congratulations on your pass”)else:print (“Sorry You Didn’t Pass”)

In the example above, if the program is run it will print the string “Sorry you did not pass” because the if statement is False

Elif condition

If elif condition is a logical continuation / branching of the “if condition”. With elif we can code a program that will select a number of possibilities that can occur. Almost the same as the “else” condition, the difference is that the “elif” condition can be many and not just one.

Below is an example of using elif conditions in Python

#Examples of using elif conditionsthe_day = “Sunday”if (the_day == “Monday”):print (“I’m going to college”)elif (the_day == “Tuesday”):print (“I’m going to college”)elif (the_day == “Wednesday”):print (“I’m going to college”)elif (the_day == “Thursday”):print (“I’m going to college”)elif (the_day == “Friday”):print (“I’m going to college”)elif (the_day == “Saturday”):print (“I’m going to college”)elif (the_day == “Sunday”):print (“I’m going on vacation”)

In the example above, if the program is run it will print the string “I’m going on vacation”.

--

--