Python if else Statement

In these articles, you will learn about python if else statement from basic to advance. If you are new in python here is a link for python variable and datatype feel free to visit these articles. Here we are learning about if, else, elif, nested if etc. Before dive into python if-else statement, we have to knowledge about Boolean expression. Here we start with a Boolean expression.

Boolean Expression

Boolean expression is those which is return either True or False. By the following example, the term is clear. The operator ==, use for comparing two operands and as a result, produce either true or false. True if they are same and false if they aren’t. Following code show boolean expression in python.

boolean operation
boolean operation

Logical Operation

Logical operation is the most popular term in a programming language. Generally, these are logical operation are, and, or & not. The meaning of this operation is very similar to its meaning in English. Following are some example of logical operations. Following code show logical operation in python.

logical operation

So the concept of boolean expression and logical operation is very important for a conditional statement. In other words, the conditional statement is those which are using for the condition apply. The simplest form of a conditional statement is if statement. This is executed only if the operation is true.

Python If Statement

The word If itself a keyword. So expression after if the statement is call condition. End of the statement is indicating by clone (:) character. After that the next after if the statement is intended.

flow chart of if statement

If the logical operation is true then the code inside if the statement is executing otherwise it’s skipping. After skip, if statement automatically then else statements is executed. The syntax of the if-else statement is here.
If condition:
Statement1
Statement2
.
.
.
Statement n

If there is no statement inside if statement so you can use pass keyword for empty implementation. Following code shows a simple if-else statement.

a=6
b=5
if a>b:
    print('a is greater than b')

Python else Statement

Else statement is the second part of if statement. If the logical statement of if a statement is false then else statement is executed. There is no logical operation in else statement. The end of else statement is indicated by clone(:) similar as if statement. The Syntax of if-else statement is here.

If condition:
Statement1
Statement2
.
.
.
Statement n

else:
Statement1
Statement2
.
.
.
Statement n

Following diagram clear about if and else statement.

Flow Chart of if-else statement
a=2
if a>10:
    print('a is greater than 10')
else:
    print('a is less than 10')

Python Nested If Else Statement

collection of one or more operation is called nested if-else. The new term appears in the nested case which is called elif. It’s similar to if statement but only come after if statement. The syntax of elif is meanwhile similar to if statement. To clarify that It has also logical operation. If a logical operation is true then code inside elif is execute otherwise skip this condition. The syntax looks like this.

if Condition:
Statement1
Statement2
.
.
.
Statement n


elif Condition:
Statement1
Statement2
.
.
.
Statement n


elif Condition:
Statement1
Statement2
.
.
.
Statement n


else:
Statement1
Statement2
.
.
.
Statement n

Likewise flowchart for nested statement is given below.

python nested if else statement

The condition checks from top to button approach. If the first condition is false then goes to next. But is first is true the code inside the first condition is executed an so on. Following code show chain condition.

a=2
b=2
if a>b:
    print('a is greater than b')
elif a<b:
    print('b is greater than a')
else:
    print('a and b are equal')

We can also create condition inside the condition which is a little difficult to understand. Following diagram shows this condition in visualizes form.

python if else statement
Nested inside

Following code are use to demonstrate the flow chart.

a=2
b=3
if a==b:
    print("a and b are equal")
else:
    if a>b:
        print('a is greater than b')whatapp 
    else:
        print('b is greater than a')

Error

The error occurs while programming conditional statement. General commonly seen error is given below and also their solution is discussing here. This two error is commonly seen while codding on a conditional statement.

1. Syntax Error

This error is occurred because of python interpreter does not understand our code. In a conditional statement, this error may occur if you forget to clone(:) at end of the statement. Also, this error is seen if the conditional statement is not in a good format. The syntax error looks like this. The arrow indicates an error occurrence point. The rectangle indicates that error message.

Syntax error

2. Indentation error

This error occurs if you forget to give tab inside the if-else block. In python, there is no bracket are used in code.in addition, python interpreter code inside if-else is understanding by a tab. The indentation error looks like this. The arrow indicates an error occurrence point. The rectangle indicates that error message.

Indentation error

Project

Here we are implementing all the concept learn from this and the previous article does simple console project. Overview of this project is to take input from the user. Check whether user input number or not. If user input user then prompt message contain only support number. And if user input correct is in correct format then check this number is even or odd. If the number is even then shown the message you enter even number. Otherwise, show you enter odd number. The syntax for our project is here.

  1. Input from the user
  2. convert the input to an integer
  3. if the input is number
    1. if number%2==0
      number is even
    2. if number%2!=0
      number is odd
  4. if the input is a string
    show(‘enter correct format’)
  5. end program
raw_text=input('Enter number')
try:
    number=int(raw_text)
except:
    number=-1
if number>0:
    if number%2==0:
        print('your number is even')
    else:
        print('your number is odd')
else:
    print('enter correct format')

Conclusion

In conclusion in this article, we are learning about python if-else statement. Conditional Statement is very important for every programming language. Compare to other programming languages python conditional statement is very easy. if you want to guide for machine learning project free of cost here is a link for face mask detection project.
Ok, this is the end of the article I hope you you can get a good lesson what I deliver in this article. I ask forgiveness for any word and behave which are not to be. Thank you for your kind and attention guys. Stay tuned for the next article. If you have any question regarding this article please feel free to comment below.

Leave a Reply