Branching in Python : If ,Else and Elif - MyPythonGuru

Jobs Search Portal and Learning point for Python,Data Science,AI,ML, Cloud and latest technologies.

Follow us on Facebook

Post Top Ad

Your Ad Spot

Monday, July 15, 2019

Branching in Python : If ,Else and Elif


Branching

 

There are different output from different input in branching.

 In an if statement , if the statement is True  your program will run some predefined tasks, but if the statement is False the program will ignore the task.

For example, Consider The adult movie theater. Where only adults are allowed over age 18.
Use the condition statements learned before as the conditions need to be checked in the if statement.
Explanation Of If Statement:
  • It contains a word If, any condition statement, and a colon at the end. 
  • Start your tasks which need to be executed under this condition in a new line with an indent. 
  • The lines of code after the colon and with an indent will only be executed when the if statement is True. The tasks will end when the line of code does not contain the indent.

In the case below, the tasks executed print(“welcome to the club”) only occurs if the variable age is greater than 18 is a True case because this line of code has the indent. However, the execution of print(“Enjoy the show”) will not be influenced by the if statement.
# If statement example

age = 19
#age = 18

#expression that can be true or false
if age > 18:
   
    #within an indent, we have the expression that is run if the condition is true
    print("welcome to the club" )

#The statements after the if statement will run regardless if the condition is true or false
print("enjoy the show")

Output: Welcome to the club
Enjoy the show


The else Statement:   

  • Ø  The else statement runs a block of code if none of the conditions are True before this else statement.
  • Ø  If the user is 17 they cannot go to the Adult Club, but they can go to the  Gaming Zone.
  • Ø  The syntax of the else statement is similar as the syntax of the if statement, as else :. Notice that, there is no condition statement for else.
  •  
  • Ø  Try changing the values of age to see what happens:


# Else statement example

age = 18
# age = 19

if age > 18:
    print("welcome to the show" )
else:
    print("go to gaming zone" )
   
print("enjoy the show")

Explanation :
  • §  when the age is 17, we set the variable age to 17, and this corresponds to the individual attending the Game Zone.
  • §  when the individual is over 18, in this case 19, and the individual is granted access to the concert.
Elif Statement:

§  elif statement, short for else if,
§  allows us to check additional conditions if the condition statements before it are False.
§  If the condition for the elif statement is True, the alternate expressions will be run.

§ Consider the below example, where if the individual is 18 they will go to the Dance Club instead of attending the adult show or gaming zone. The person of 18 years of age enters the area, and as they are not older than 18 they can not see adult show, but as they are 18 years of age, they will go to the dance club. 

age = 18
if age > 18:
    print("Welcome to the club" )
elif age == 18:
    print("go to dance club")
else:
    print("go to gaming zone" )
   
print("enjoy the show")


No comments:

Post Top Ad

Your Ad Spot