Lesson 5 - Conditional Statements
Lesson 5 - Conditional Statements
Hi All,
I start the lesson with an example
You need a drink but based on some condition like color, you ask shop keeper if you have white drink please give me 2 and if you don't have white give me any 1.
So, in above example you use conditions. Similarly in programs we use condition to sort out the results.
Another example is finding the grade based on your percentage.
If percentage is between 90 - 100 your grade is A+ and so on.
Syntax:
The keyword 'if' is used for condition with conditional operators
Example
a = 20
b = 10
if a > b:
print("a is greater than b")
Now you observe in above example if a is greater than b than it prints "a is greater than b" but if not than it didn't print anything. To handle this use case we use else
if a > b:
print("a is greater than b")
else:
print("a is not greater than b")
For some cases we have multiple conditions to check for example do some tasks based on week days. So, to handle this use case we use multiple if else using the keyword elif
day = Monday
if day == "Monday":
print("Do some coding")
elif day == "Tuesday":
print("Do some riding")
else:
print("Do something else")
You can use if, else and if elif else statements also inside the if statements. This is called nested if-else.
Keyword Pass
Always try to follow the standard programming practices. So, if we have a condition where we don't want to perform any action we can use keyword pass
Example
a = 10
if a != 10:
pass
else:
# do some code here
Hope you understand the concepts and syntax. Please let me know if you have any concern and I am glad to reply to your comment.
See you in Lesson 6 :-) Happy coding...
Comments
Post a Comment