Conditional Statements in Python

There are a lot of scenarios comes at the time of programming where we need to executed particular lines of code if and only if it meets some requirements. On that type of scenarios we need to put some conditions so the program can perform accordingly. For example let's say if we need to write a code which tells you whether you are eligible for voting or not by judging your age. So the condition is, if your age is above 18 then you can vote otherwise you cannot vote. So if we write it in a general way - 

If person Age > 18 then He/She can Vote, otherwise He/She cannot Vote.

So now let's see how to give these type of conditions in Python.


If-else Statement

 To do that we use if-else Statement. If statement only executes when the condition written with the if statement is true otherwise the compiler ignores the if statement along with the codes written inside the if-statement.
Else statement will execute if and only if all the previous conditions are false, basically it means if all conditions are false then execute the code written inside the else statement. Else-Statement doesn't have any conditions.

Let's see the syntax of if-else statement in Python.

Syntax- 

if (condition):
    //body
else:
    //body

After that let's solve the above problem with the help of if-else Statement.

Age = int(input('Enter your Age: '))
if Age > 18:
    print('You can eligible to vote')
else:
    print('You are not eligible to vote')

[NOTE:
1> 
In python you don't need to use ' ( ) ' on the time of giving the conditions.
2> ' if ' statement can exist without 'else' but for every 'else' statement there should be a ' if ' statement. ]


Elif Statement

 ' elif ' stands for " else-if " which is used when you want to check another conditions if the previous conditions are false. We can use as many elif statements as we want to, but it should be written after the if-statement. Let's take an example to understand it better.

A user need to input a value between 0-100. If the value is lesser than 20 then print "Hello", if it is lesser than 50 then print "Coders", if it is lesser than 80 then print "Welcome" and if it is greater than 80 then print "Here".

a=int(input("Enter a number between 0 to 100: "))
if a < 20:
    print('Hello')
elif a < 50:
    print('Coders')
elif a < 80:
    print('Welcome')
else:
    print('Here')

If the user inputs [a = 30] then according to the conditions, both elif statements are true. But the first statement (either if or elif) which is true will be executed and other statements will be ignored by the compiler. Therefore the output will be- 

OUTPUT - Coders

Now let's see if we use if-statement only instead of using the elif statement then what will be the output.

a=int(input("Enter a number between 0 to 100: "))
if a < 20:
    print('Hello')
if a < 50:
    print('Coders')
if a < 80:
    print('Welcome')
else:
    print('Here')

OUTPUT - Coders
                    Welcome

So we have seen that if we are using the elif statement then whenever the condition will be true then the compiler will ignore the other conditional statements. But if we are using only if-statement then even the previous conditions are true, the compiler will execute all the conditional statements present.


And & Or

Now let's learn how to check more than one condition in the conditional statements. There are two keywords in Python that we use to do that.

  • And Keyword - We use 'And' keyword when we want to execute the code only if both the given conditions are true, if anyone of the condition is false then that statement will not be executed.
    For example -

    If the input value is between 0-9 then print 'One Digit', if it is between 10-99 then print 'Two Digits', if it is between 100-999 then print 'Three Digits' otherwise print 'Unknown'.

value = int(input('Enter: '))
if value >= 0 and value < 10:
    print('One Digit')
elif value >=10 and value < 99:
    print('Two Digits')
elif value >=100 and value < 999:
    print('Three Digits')
else:
    print('Unknown')


  • Or Keyword - We use 'Or' keyword when we want to execute the code when anyone of the given condition is true. if both the conditions are false then only that statement will not be executed.
    For example - 

    If the user enter 'pro' or 'coder' or 'mega' then print 'ProCoderMega' and if the user enters 'blogger' or 'unit' then print 'BloggerUnit' otherwise print 'Unknown'.
value = input('Enter: ')
if value == 'pro' or value == 'coder' or value == 'mega':
    print('ProCoderMega')
elif value == 'blogger' or value == 'unit':
    print('Blogger Unit')
else:
    print('Unknown')

[NOTE: If you want to not execute the conditional statement when the condition is true then we can use a keyword 'pass'. For example:- if a > b: pass ]


Nested If-Statement

If-else statement inside an if statement is known as 'Nested If-Statement'. We use Nested if statement when we need to check condition to the conditions i.e, when the condition is true then we need to check other conditions that are within that conditional statement. Let understand it by an example -

val1 = input('Enter: ')
if val1 == 'I':
    val2 = input('Enter: ')
    if val2 == 'Love':
        print('I Love Python')
    elif val2 == 'code':
        print('I code in Python')
    else:
        pass
else:
    print('Unknown')

 
[NOTE:
If there is only one line of code within the if-statement then we can write it like below-

if condition : code
]


For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments