If - Else Statement

If ... Else Construct in Python

The if - else structure used for condition control in programming is used in Python language as follows.

a = 50

b = 60

if a == b :

     print("Numbers Equal")

In the above example, a and b variables are assigned values, and then the two variables are compared within the if structure . It was checked whether the two variables are equal, and if the result is True, "Numbers Equal" is written on the screen.

One point is very important here. In Python it is the indentation of the lines that determines the scope. For example, in C#, the boundary of the if block is determined by curly braces. In Python, the boundaries of if and else blocks are determined by line indentation.

In the above example, if the print line is written without indentation, an error will occur. We can use Tab key for indentation.

Also, don't forget to put : at the end of the if line.

If the condition specified in the If line does not hold, that is, when it gives a False result, we use the else part if there are actions to be taken.

a = 50

b = 60

if a == b :

     print("Numbers Equal")

else :

     print( "Different Numbers")

In the above example, if the result of the condition in the if line is True, the first block runs and "Numbers Equal" is written on the screen. If the result of the condition is False, the else block runs and "Numbers Different" is written on the screen.

When the condition is true, the else part is skipped and not processed. If the result of the condition statement is False, the operation or operations in the else part are performed.

elif is used when more than one condition needs to be checked sequentially . (The elif command is called else if in many programming languages.)

if a < 0 :

     print( "Number is Negative")

elif a == 0:

     print( "Number Zero" )

else :

     print( "Number Positive")

Here, the conditions are checked in order from top to bottom, and if there is a matching condition, that block is executed. If no condition is met, the else part is executed.

Else part is optional. If there is no else part and no conditions are met, then no action will be taken.

We can check multiple conditions using multiple elfs.

if a < 0 :

     print("Invalid Input . ")

elif a < 50:

     print( "Your grade: 1" )

elif a < 60:

     print( "Your grade: 2" )

elif a < 70:

     print( "Your grade: 3" )

elif a < 85:

     print( "Your grade: 4" )

elif a <= 100:

     print( "Your grade: 5" )

else :

     print( "Invalid Input")

In the above example, the condition in the if line will be checked first. If the result of the expression here is True, the other blocks will not be looked at at all.

If the result of the condition in the if line is False, the conditions in the elif lines will be checked in order. If these conditions are met, that block will be executed and the others will be skipped.

If no condition is met, the else block will be executed.

If - Else Examples

Finding out if an integer variable is odd or even:

if a % 2 == 0:

     print("Number is Even")

else:

     print("Number is odd")

Program that prints "Numbers are Positive" if all 3 variables are positive, otherwise does nothing:

if a>=0 and b>=0 and c>=0:

     print("Numbers are Positive")

The program that finds the largest among 3 numeric variables:

if x>=y and x>=z:

     print("The first number is the largest . ")

elif y>=x and y>=z:

     print("The second number is the largest")

else:

     print("The third number is the largest")

 

python if else usage, python if else structure, if in python, what is python elif, python elif usage and examples, python if else examples

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 611 times.

Online Users: 390



python-if-else-statement