If you have knowledge of other programming languages, then you would know the importance of conditional statements. Conditional statements are required for taking decisions. Whenever we operate the software, our choice of action decides the next step. A simple example is booking an air ticket. If you want to travel in economy class, you will be presented a different package, whereas if you opt for business class you will be given a different package. So, let’s take a look at the conditional statements available in python.
-An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
if (expression): code1
print ‘I am greater than b’ else:
print ‘I am greater than a’
Output :I am greater than a
-To check multiple conditions and run the block of code for the condition that returns ‘true’
if expression1: code1
elif expression2: code2
elif expression3: code3
print ‘I am greater than b’ elif (b>a):
print ‘I am greater than a’ elif (a==b):
print ‘Hey!! we both are equal’
Output: Hey!! we both are equal
- A simple if statement can be used without any else or else if. Usage of simple if statement means that the lines of code should be executed only if the condition is true.
if condition : execute the code
print ‘I am greater than b’ print ‘Good Bye’
Output: Good Bye
Enroll for Instructor Led Live PYTHON TRAINING
Get Updates on Tech posts, Interview & Certification questions and training schedules