If Else & Elif Conditionals In Python | Python Tutorial for Beginners | Python Programming Language

Veröffentlicht am: 08 November 2022
auf dem Kanal: Tanmay Panchal
764
41

My full course is on Udemy. Here is a link to Enroll - https://www.udemy.com/course/python-b...

If Statements
As shown in the earlier example, conditional statements are run by using the If statement.
A condition is checked in the if section. If it is true, the code runs. But when the code is run otherwise, nothing happens.
See the example given on the screen to understand how to use the if statement. It shows that if the value of x is greater than zero, python will print “x is positive”; otherwise, nothing will happen.

If…Else Statements
A second form of the IF statement is an alternative execution in which there are two possibilities in the condition that determines which one gets executed. This is called the IF…ELSE statement.

In the given example, a value of twenty is assigned to the age variable. Now in the if statement, it’s being checked whether the age is equal to the given value of twenty or not. If this condition is true, then python will print “age is 20 years” else python will print “age is not 20”.

You can also use greater than and greater than equal to operators with the if…else statements. In the given example, you can check if the age is greater than eighteen. In case it’s true, python will print “person is adult”, otherwise, it will print “person is not adult”.

If…Else If Statements
Sometimes, there are more than two possibilities and you will need more than two branches. One way to express such computation is a chained conditional. This is also called the if..elif statements. Elif is an abbreviation of else…if.

Again, exactly one branch will be executed. There is no limit on the number of elif statements.

To understand it better, consider the given example
Create variable named marks and assigned a value of ninety-five. Now, for different grades, the result will depend on a value stored in the marks variable. In the highlighted example, if the mark is greater than ninety, then the grade is A. Now if the mark is not greater than ninety, then the second if condition which is elif marks greater than or equal to eighty will be evaluated. In this case, python will print a B grade. Similarly, if marks are less than eighty, the next elif condition will be evaluated to check if the marks are greater than or equal to sixty.
As mentioned earlier, there can be any number of elif statements.

If…Else If…Else Statements
The next type of conditional statement is if…elif and else combined. If none of the conditions is met, then else block is executed.
Notice the same example of marks. If marks are less than sixty, then the else statement will be executed.

#python #pythonprogramming #pythontutorial


Auf dieser Seite können Sie das Online-Video If Else & Elif Conditionals In Python | Python Tutorial for Beginners | Python Programming Language mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Tanmay Panchal 08 November 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 764 Mal angesehen und es wurde von 41 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!