#7: Python if...else Statement for Decision Making | Python for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys i'm puneet from programmers and welcome back to this series on python in the last video we learned about comparison operators these comparison operators evaluate to either true or false in this video we'll do something interesting we will learn to create programs that execute different code based on the comparison of two values if the comparison is true we will do one set of tasks and if the comparison is false we will do another set of tasks this is called decision making in programming now let's see how we can implement decision making in python in python we use the if statement to create programs that can make decisions let's start with its syntax so on my screen you can see the syntax of the if statement the if statement evaluates the test condition which is a boolean expression if the test condition is true the body of the if statement is executed otherwise it is kept from execution notice the spaces before the statement here this indentation is used to differentiate between the body of the if statement it doesn't matter how many spaces or tabs we use for indentation however we need to be consistent with it generally it's considered a good practice to use four spaces for indentation now that we know the syntax of the if statement let's try a working example suppose you're a university student and to pass the examination you need to score 50 or more if you score 50 or more we will print you pass the examination let's see how we can implement this program so first i'll take the input from the user as score equals integer input enter your score then i'll say if score greater than equals 50 print you have passed your exams let me add another print statement i say print congratulations let me run this program and here in the score i'll enter something like 75 as you can see you have passed your exam and congratulations are printed to the screen when your score is 75 as in this example our test conditions score greater than or equals 50 is true and that is why these two statements are executed let me run this program again this time instead of 75 i'll enter something like 35 so this time nothing is printed to the screen this is because since score is 35 35 greater than equals 50 is false and the body of the if statement is kept from execution our program is working correctly but it's not printing anything when the score is less than 50. we might want to print something like sorry you failed your exam during this case so i will add another if statement i'll say if score less than 50 print sorry you have failed your exam and we can see that when i press run and enter 35 then it says sorry you have failure exam this is because again this condition is false and this is skipped but this condition is true and this code is executed i have this code from our last section i have removed the code that takes the user input and instead i've hard coded the value of score so that we can focus on the logic of the if statement in this program we have used two if statements to perform two different tasks we know the student pass the exam if the score is greater than or equals 50. if the criteria is not met we know the student failed the exam in such cases instead of writing the second if statement with the condition we can use the else clause let's first look at the syntax of the if statement with the else clause on your screen you can see how it looks what happens here is that if our test condition is true these statements are executed and if it's false the statements inside the else clause are executed now getting back to our code to print whether the student passed or failed the exam in this program we know for sure that if the score is not greater than or equal to 50 then the student is failing the exam so instead of writing the second if statement we can replace the second if with else so here i'll remove this if and i'll write else and a colon here let me read this code in plain english if score is greater than or equal to 50 then print you have passed your exams and print congratulations else print sorry you have failed your exam the else clause catches everything that's not covered in the if's condition now when i run this code you can see that sorry you have failed your exam is printed before moving to the next section of the video i'd like to mention that the program is team has created an app that allows you to learn python from your phone the app contains bit size lessons that are easy to understand a built-in interpreter so that you can run python on your phone quizzes and many more features the app is available on both ios and android the links are in the video description below the if statement with the else clause allows us to make a choice from two different options but sometimes we need to make a choice from more than two options in those cases we can use the elif clause with the test condition let's see its syntax first the if statement checks the condition inside the if statement or the test condition 1 if it is true then this block of code is executed and statements 2 and statements 3 are skipped however if the first test condition is false the control of the program jumps to the second test condition if this test condition is true its body is executed and other statements are skipped if both test condition 1 and test condition 2 are false finally the else body is executed if necessary we can add as many alif clauses as we want for our program to work and among all those alternatives only a single block of code is executed now we know how the alif clause works let's get back to our code to check whether the student passed a failed exam here this score variable stores the marks obtained by the student so it shouldn't be greater than 100 or a negative number if score is above 100 our test condition is true and you have passed our exam and congratulations is printed even though the score is invalid logically and if score is a negative number sorry you have failed your exam is printed because our test condition is false again score shouldn't be less than zero we need to fix this we can fix this by adding a condition to check if the score is valid or not at the beginning only then we will check if the student passed or failed the exam so in my code i'll make the changes let's try for 105 and here i'll add a condition i'll say if score greater than 100 or score less than 0 then print score is invalid and then i'll turn this if into an alif and now i have put that condition to check if the score is valid or not now if score is greater than 100 or score is less than zero then score is invalid should be printed to the screen as long as score is a valid number our program should work correctly now if you remember this r is a logical operator which we discussed in the last video this test condition is true if either this part or score greater than 100 is true or this part score less than 0 is true as you can see the syntax of the if statement is pretty simple the harder part is the logic behind the test conditions and you will get better at creating test conditions with practice also be sure to check our video on comparison and logical operators that are used to create these test conditions i posted the link in the description below we have covered a lot of material in this video it's time for you to practice what we learned here is one programming challenge for you to solve now can you create a program to check whether a number is positive or negative or zero to create this program create a variable named number and assign a float value to it based on the user input then using a if statement check if the number variable is positive or negative or zero if the number is positive print the number is positive if the number is negative print the number is negative and if the number is zero print the number is zero you will find the answer to this question in our github repository i posted the link in the description below since this video is already very long we will not explore nested effects in this video if you are interested you can find more information about the if statement in our website programmers.com along with numerous examples i posted the link in the description below before i end this video here's a recap of what we learned today the if statement is used to create programs that can make decisions the if statement evaluates a test condition if the test condition is true it executes the if body but if the test condition is false the if body is escaped from execution to specify the body of the if statement we use indentation in python the if statement can have an optional else clause the else clause is executed if the test condition is false if you need to make a choice from more than two alternatives you can add an optional alif clause with another test condition that's it for this video i hope you learned something if you're just watching the video without writing any code i highly encourage you to try the programs in this video on your own the only way you can be a good programmer is by trying by the way you can find all the programs from this video on github i posted the link in the description below feel free to copy the programs and edit them as you please and if you have any questions and feedback use the comment section below in the next video we will learn about loops to repeat a block of code and it's going to be pretty interesting join me in this video series and let's explore the exciting world of programming together if you like this video hit the like button now and also don't forget to subscribe to our channel and ring that bell icon so that you don't miss the next video thanks for watching and i'll see you in the next one happy programming
Info
Channel: Programiz
Views: 54,189
Rating: 4.9400544 out of 5
Keywords: python, python programming, programiz, python if else, decision making
Id: 497MClrekMY
Channel Id: undefined
Length: 11min 27sec (687 seconds)
Published: Wed Sep 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.