Python if,elif, else and while loop - learnit

Home Top Ad

Post Top Ad

Sunday, June 7, 2020

Python if,elif, else and while loop

Python if,elif, else and while loop

Python IF, ELIF, and ELSE Statements

While writing code in any language, you will have to control the flow of your program. This is generally the case when there is decision making involved - you will want to execute a certain line of codes if a condition is satisfied, and a different set of code incase it is not. In Python, you have the if, elif and the else statements for this purpose. In this tutorial, you will work with an example to learn about the simple if statement and gradually move on to if-else and then the if-elif-else statements. You will also learn about nesting and see an nested if example. Lets get started……

IF Statment

Syntax

IF(Condtition)

    Indented Statment Block

The block of lines indented the same amount after the colon (:) will be executed whenever the condition is TRUE.

For example, lets say you are recording the score for a certain course. The Average is 100, with 50 points for the theoritical work and 50 for practical. You want to display an Congratulations Result is Pass

Example

Average=50

IF(Average>50):

      print("Congratulations Result is Pass")

Single test: if-else Statment

The if-else statement is used to code the same way you would use it in the English language.

syntax

IF(Condition):

    Indented statement block for when condition is TRUE

else:

    Indented statement block for when condition is False

Example

Average=50

IF(Average>50):

    print("Congratulation Result is Pass)

else:

    print("Result is Fall)

if-elif-else Statment

The syntax followed by the if-else-if statement is:

IF(Conditional)

    Indented statement block for Condition1

elif(Conditional)

    Indented statement block for Condition2

else:

    Alternate statement block if all condition check above fails

Example

Average=50

IF(Average>=50)

    print("Congratulation Result is E")

elif(Average>=60)

    print("Congratulation Result is D")

elif(Average>=70)

    print("Congratulation Result is C")

elif(Average>=80)

    print("Congratulation Result is B")

elif(Average>=90)

    print("Congratulation Result is A")

else:

    print("Result F")


loops in python

Python programming language provides following types of loops to handle looping requirements. Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.

While Loop

In python, while loop is used to execute a block of statements repeatedly until a given a condition is satisfied. And when the condition becomes false, the line immediately after the loop in program is executed.

Syntax

while expression

    statement(s)

Example

Count=0

while(Count<3):

    count=count+1

    print("Hello Reanits")

Output

Hello Reanits

Hello Reanits

Hello Reanits

Using else statement with while loops: As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed.


The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.


Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad