Python - if, else, elif Statements (With Examples)

Python - if, else, elif conditions

In this tutorial, we are going to learn about the python if..else..elif statement with the help of examples and am using pycharm to execute the command and create some decision-making programs.


The if statement in Python allows you to specify a block of code to be executed if a certain condition is satisfied. Here we use the statement and give value to the output return when a certain condition is met and extract the block code.


Read also : student registration form a python with database

 

For example:


In school, the student gets results based on the marks. if the student marks get above 35 marks, the output is a pass and if the student gets below 35 marks, the output is a fail.

same process as the Assigning in grades systems like A,B,C,D,E based on the marks obtained by a student.

In Python , there are three types of forms in if...else statement

  • if statement
  • if.. else statement
  • if ...elif...else statement 

 

Python if statement



student = 45
# Here the check if number is greater than 35
if student > 35:
  print("student is pass ")


In the above example python code, we have created student marks variable named as student. the variable is student = 45 ,if the condition is true inside the if statement is executed.In case the condition is false , the if statement is skipped.

The if statement in Python allows you to specify a block of code to be executed if a certain condition is satisfied.


Syntax:



if condition:
    # block of statement


For Example:



student = 45
if student > 35:
  print("student is pass ")


This will print "student is pass" because the conditioned student > 35 is satisfied.

You can also use an if statement in combination with an else statement to specify a block of code to be executed if the condition is satisfied, and a different block of code to be executed if the condition is not satisfied.

Python If..Else statement:


In the above of code, if...else the statement is used to execute a block of code and has two alternatives code executed.

Syntax:



if condition:
    # block of code is True if condition executed

else:
    # block of code is false  if condition executed


For Example:



student = 35

if student >= 35:
    print("student is pass")

else:
    print('student is fail')

print('Above given statement is passed')


This will also print "student is Fail" because the conditioned student > 35 is not satisfied. You can also use an if statement in combination with one or more elif (short for "else if") statements to specify multiple conditions and the corresponding code to be executed if any of those conditions are satisfied.

Python If ...Elif...Else statement:


if we need to make a Multiple choice between more than two alternatives, then we use the if...elif...else statement.

Syntax:



if condition1:
    #  block 1

elif condition2:
    # block 2

else: 
    # block 3


For Example:



student = 0

if student >= 35:
    print("student is pass")

elif student == 0:
    print('student is absent')
else:
    print('student is fail')

print('Above given statement is passed')


This will print "student is Absent" because the condition student ==0 is not satisfied.

student = 65


In this case, the student's mark is 65 so, the condition is greater than 35 and the print output is "student is pass" because the condition is satisfied.

student = 12


In this case, the student's mark is 12 so, so the condition greater than 35 is not true so, so the condition is skipped the if condition, and the print directly else output "student is fails".

It's important to note that the else and elif statements are optional, and you can use as many elif statements as you like. It's also worth noting that the conditions in an if statement is evaluated in the order they are written, and the first one that is satisfied will trigger the corresponding code block to be executed.

Overall, the if, if else, and if elif else statements are powerful tools for controlling the flow of your program's execution based on different conditions. They allow you to write code that can adapt and behave differently based on the data it is given.

Previous Post Next Post