If statements in Python are easy (if, elif, else) 🤔

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this topic I'm going to explain if statements and if statement is used to do some code only if some condition we set is true else we could do something else it's a basic form of decision making if it's true we do something if it's not true we don't do it let's ask a user for their age age equals input enter your age I will type cast the input as an integer depending on what the user's age is we can do one of a few things let's pretend that the user would like to sign up for a credit card but in order to do so their age needs to be greater than or equal to 18. well we can check that to use an if statement type if then some condition what would we like to check let's check to see if the user's age is greater than or equal to 18 then add a colon then hit enter any code underneath the if statement should be indented make sure to pay attention to that because that's easy to miss if the user's age is greater than or equal to 18 let's print you are now signed up if I were to run this code I'll type in my age I'll type 21 hit enter this statement is true therefore we will execute any code found within the if statement you are now signed up what if this condition was not true let's say my age is 13. well nothing happens if the condition we check is instead false we skip over this code if you need to take a different course of action you could add an else statement if this is true do this else we can do something else let's print a different message you must be 18 plus to sign up I'll type in my age again I'll say that I'm 13. hit enter you must be 18 plus to sign up that's basically an if statement do some code only if some condition is true else you could do something else entirely it's a basic form of decision making the else statement is kind of like a last resort we can check more than one condition before reaching the else statement we can add an else if statement which we just shortened to e-l-i-f meaning else if else if let's check if age is less than zero then we'll print a different message you haven't been born yet now if I run this code I'll say that my age is negative one this condition is false we skip this code this condition is true therefore we will execute this code and we skip the else statement you haven't been born yet let's add another elsif statement you can add as many elsif statements as you want let's check to see if somebody's age is greater than or equal to 100 we'll print a different message let's print you are too old to sign up if I were to say my age is 111 years old well it states you are now signed up the reason that we didn't reach this part of our elsif statement that's because this condition is still technically true you do need to pay attention to your order of if and else if statements if I want to be sure that nobody over 100 is signing up I should probably move this to the beginning if age is greater than or equal to 100 then else if age is greater than or equal to 18 we'll do something else and to your age I am 111 years old you are too old to sign up so those are if statements if some condition is true do something else if you can check something else if no above conditions are true you could do something else entirely it's kind of like the default here's another example we'll ask a user if they would like some food response equals input would you like food we'll have the user type in y for yes or n for no if our response now to check to see if two values are equal you would use double equals if the response is equal to y then we will print have some food the doubles equal sign is the comparison operator it will check to see if two values are equal you don't want one equals because that's the assignment operator python in this case thinks we're attempting to assign the character y to response so for comparisons use double equals else we can print no food for you so would you like food I'll type why have some food let's try it again I'll type no and for no no food for you here's a third example we'll have a user type in their name name equals input enter your name if our name is equal to an empty string that means they didn't type in anything so let's yell at the user you did not type in your name else we will print using an F string hello whatever the name is enter your name I'm just going to hit enter you did not type in your name let's run this again I'll type in my name and we have executed the else statement this time hello bro so one important thing that you should know is the use of boolean's width if statements suppose we have some Boolean variable named for sale I'll set this to be true now using an if statement you can just use the Boolean variable in place of a condition because a condition would evaluate to be true or false we could just say if for sale if that's true then let's print this item is for sale else we will print this item is not for sale for sale is set to be true this item is for sale if this variable were false well then the item is not for sale let's try a different variable how about online if online the user is online else the user is offline so the user is offline I'll change the Boolean to true the user is online so with if statements you can either write a condition or you could use a Boolean all right everybody so those are if statements do some code only if some condition is true else you can do something else it's a basic form of decision making and those are if statements in Python
Info
Channel: Bro Code
Views: 115,660
Rating: undefined out of 5
Keywords: control flow, conditional statements, if elif else, conditional statements in python, elif, elif capítulo, python if, python if else statement, if statements python, python if else, python if statement, if else statements, python elif, if else statement in python, if statement in python, if statement, if else, python tutorial, python tutorial for beginners, learn python, how to use if else statements, conditionals, conditional statements python
Id: FvMPfrgGeKs
Channel Id: undefined
Length: 8min 20sec (500 seconds)
Published: Wed Oct 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.