Python (PyHacks) - Conditional Logic - IF..ELIF..ELSE statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to pi hacks today we're going to be looking at conditional logic and we're going to be looking at the if else and else if statements okay so i'll take you through and we'll launch straight into this so i'm going to open my spider code editor just a little thing around operators just two things we're going to explore here so equality needs to be plast so if you you're evaluating two statements the left and the right side of the argument you need to have an equality so not equals so double double equality there to perform that evaluation for not equals it's um this and not equal to so exclamation mark and equals symbol we're going to explore the other operators as we go along doing the code examples so i'm going to use an example of me going for a job and i've said i've got six years coding experience and the role needs five yep so i'm going to say i'm going to evaluate this in an if statement if my code experience is greater than the role code experience and remember indentation is key in python so if anything's going wrong with the code it's probably because you've not indented right so i'll check that first if anything other than that you've misspelled something wrong or remember python's case sensitive as well great you've got the job so we know that that's going to return true because there's 6 and a value of 5 6 is greater than 5 therefore that becomes true under boolean algebra boolean logic should i say so it's going to say great you've got the job okay so that's a simple if statement simplest statement so let's have multiple conditions that we're checking for so we're going to use the elif function here so i'm going to say if my role code experience is greater than my coding experience so we know this is not true because the role experience only wants five that's not greater than six so this is going to return false and here we're going to say we're going to create a variable called code deficit and what this is going to do is take the absolute value of code exp take away subtract roll code experience so create a variable in memory and i'm going to print with replacement fields sorry you need replacement field years more experience to get a coding role here i'm going to use the format operator at the end to pass in this code def variable they've created here okay that's going to handle that print and i'm going to use the alif pass some parameters into that alif so i'm going to want to do something with the alif statement now role code experience is less than equal to code experience print wow you know how to code again corny as hell so if i do that and run that line that's going to evaluate to true because we know that the role code experience is less than my code experience and it's not equal to that if i then change that to six it's still evaluate true because actually my my experience matches with the role code experience so now if i want to change it the other way around and say that i've only got two years current experience one year one year current experience and i hit f9 on that there we go to my code and experience as well more than the coding experiences that's needed okay so we're now going to perhaps just amp this up and i'll probably just copy a previous example that i've done for time so i'm going to throw in this else statement i'll take you through this so i've got this code experience and roll code experience these are the same so create the code def code deficit and i passed it to the replacement field and then i'm again putting that same alif statement in there all i'm adding to this function is if it doesn't evaluate and it will always evaluate they'll always it'll always be between those values but if for some reason they're passing in something like an incorrect data type etc it won't know how to evaluate this so it's always good to put an else statement on the back of it just so there is so it will print something or do something at the end of your uh your code block it could even just be like breaking out the function or terminating the specific program that you're working on or closing a database connection whatever you're trying to achieve with that okay so that's how you do if if else so else if statements and then adding an else at the end for our code experience example so if i wanted to do this in shorthand i'm just going to do an if else statement here so let's do it shorthand so a bit like list comprehensions you start with the thing that you want to try and do at the start so i'm just going to print your coding experience rocks okay and here's where i put my condition so as before you had the print inside the condition here you've got the print before the condition if code experience is greater than roll code experience it will return true else print come back come back when you have more experience obviously hiring decisions aren't usually made just on these facts but uh there we go so if i wanted to run that now i couldn't experience rocks because it does you know six years and they want two you're four years more advanced than their required minimum okay so there's a couple of other important concepts that i need to talk to you about today those useful concepts are the and and or operators so exclusive and exclusive and stuff like that what am i talking about so the and keyword so if you want two conditions so you're checking for two conditions returning true this is when you'd use the and so i'll give you an example of this and we'll create a few variables here we're going to do the code experience example from before roll code experience so what the role requires four years what i've got what job experience i've got more than that just 24 years god 24 years and the role only wants five years experience in a specific area say in terms of coding so there we go we're going to set those variables in memory and then we're going to go if we're going to i'm not going to add a if else in this one i'm just doing a simple if statement just for clarity if code experience is greater than role code experience and there's the and so i'm saying actually i want it to evaluate two conditions we know at the moment that actually code experience is higher than raw code experience so that's going to evaluate this side to true so let's do put another check in there and if both conditions don't mat um pass then you'll get a false and it'll print the else part of the condition so and job experience is greater than role job experience so i know this is going to pass because they're both greater than so joe experience is 24 and the role requires five years experience so i know that that's greater than so it's going to pass both conditions because both return true if one returned true and the other returned false then this is where you'd have an else catch to say there is a variance one of the conditions conditions caused the and statement to fail because both need to be true or both need to be false but one can't be true once which can't be true and the other switch be false so print so this is going to pass because we know that they're both true so your coding experience is what we're looking for so when i run this okay coding experience support so the else condition then how can i make this not work so for some reason the job wanted 50 years experience that's now caused this part of it to fail so you can see that one of the conditions caused the and statement to fail because we know that actually our my job experience now doesn't return true this now returns false so because this is true but this one's false this is this and's gonna fail because it's looking for two trues or two falses not one or the other let's change this back so it works and now you'll get the first statement because both conditions are true okay so let's move on to the or keyword then how we use the all so here i'm going to say if code experience is greater than role code experience which we know is true or job experience is greater than role job experience tab auto completely helps as well print you have the right balance of experience experience and coding skill something like that okay else let's put an else in this time to catch the if it doesn't work print that all works because the first condition returned true and only this was evaluated that's a big long statement so explain what that means so what an all statement is going to do it's going to look to see if this is true or this is true and it's going to return one or the other um it doesn't return both it's not exclusive like this one it's going to return one side and it'll return true if that is true but say both of the conditions failed so let's make that 40 and that 50 and i'm going to reset these variables in memory you can see that now all the variables have changed if i run this again now this is going to fail because actually both conditions are wrong both ors fail so that fails so that's false and that's false in terms of the operator my role experience my job experience are now not greater than what's required from the role so we get this failure again if i change this back you'll see what happens again the weirdness of the or statement it's going to work again because actually it's now said although this one's false the right side of the equation is true so actually joe experience is greater than real job experience so actually albeit you might not have the current experience we're going to give you the job based on your job and role experience because that's greater than that so in the difference between an and and an or is an and needs both of the things to return either true or false together so it's evaluating two conditions whereas the or is just evaluating one or the other or one or the other as they say around my neck of the woods right nested if then so why would we want to use a nested if we want to check for multiple things and i'm going to do it as a nested hierarchy as well so you can just see how to construct them so i think i've got a code and experience of i don't know five years just as another example i'm going to say if code experience is greater than 2 let's say greater than equal to because we're not used that much then print candidate for junior developer okay then we're going to say if because we're doing it inside of this experience is greater than equal to five print candidates for developer okay if code experience is greater than let's say seven years print senior developer and then we're going to handle the else but this is where indentations key we're going to put the else at the end so anything else that's not caught in this statement will then print this is outside of the evaluation scope or something like that so you can actually find it doesn't know what to do if say zero i'll show you that in a minute so if i press f9 because this is encapsulating inside it's going to look to see if the code experience is greater than 2. that's going to return true so i'm going to say is it greater than equal to 5 well yeah it is actually greater than or equal to because it's equal to 5 that's going to work so it's looking from five onwards so i'm going to print back candidate for developer and if you had seven years so eight greater than seven so it'll be eight years experience i'll get all three of these printed out so sometimes you want to evaluate multiple conditions in this hierarchy okay you want to check for multiple things so multiple matches if you're you're testing in your code and your data to try and create multiple tags this is the way that you do it so you can see now for this particular one variable i've now got three values that i can work with so say my say i don't know my current experience is one year so instead of printing this gobbledygook i'll just say you don't have enough experience okay so if i prep that it's true because one year is not greater than equal to two so it's always going to be you don't have enough experience if i set this to zero and you don't have enough experience if i set it to minus 40 it definitely doesn't know what to do with that still the number but it's in the negative direction that's why the importance of why you'd have an else statement because it just handles anything else that's not within your conditions okay and there's one last thing to explore and then we've concluded our kind of little little dive into uh if else if an else statements using shorthand in python and the importance of the and or keywords use the and a lot to be honest when you're doing comparisons between dates so it normally looks something like this if my start if my current date is greater than equal to my sorry my start date is greater than equal to date and my end date is less than equal to not meant to be data it's meant to be date so what i'm doing is saying actually return all the subset of the data when you're filtering from the start date that's greater than equal to the date that i'm i'm putting in so if i'm inputting a date and the end date is less than i've done a or operator there an over a variable condition to assign um if the start date is greater than equal to date and the end date is less than equal to the date then do something and we brought back an if condition and print and store in a database or whatever one last thing to do so actually that's a good example to say i've not quite finished with this statement i've not quite worked it out yet if you're still working on something python you can just use the word pass to make it evaluate obviously this is not gonna evaluate because there isn't a date at the moment so we could just say date equals two and i'm just going to use a year 2017. so date will work there you want to print those start date oh yes start date's not defined either so you need your start date defined working as well start date is let's say 2010. we should say start year really and end year is equal to 2021. okay and if start date is greater than equal to date and end date is less than equal to pass should work now there so although it's not doing anything it it now works it's now got an equation that can it it can work with and i'm not getting any errors when i'm running this script i'm probably getting a pilint uh issue around i've got a undeclared statement here it's not quite working i wonder if i name end yeah it's not working then it's me being flaky and date so you can see that actually that's now worked i mean that getting no issues there so the pass the pass statement is another way to just make your code work while you're working on an equation it's really handy really really handy when you you know you want to you're building classes or class modules from the scratch and you haven't quite figured out all the abstract classes or what you'd need in an object-orientated programming way or if you're creating a function uh you don't quite know all the parameters or you've not worked out the logic internally you create the function initially and just use pass as long as the variables are present you can see these are global variables so they can operate on those so i'll get into things like functions of function creation global variables later on just got this problem here that need to get rid of and then that's good to go so i hope you found that really useful um a second get an issue with my screen it's on f11 that's why so smooth i hope you found that really interesting um like say please watch again for my next tutorial please subscribe if you like these tutorials drop us a comment um store the repository on github do whatever you want to but yeah it's been great uh taking you through this today and just drop me some feedback alright guys stay safe
Info
Channel: Hutsons-Hacks
Views: 19
Rating: undefined out of 5
Keywords: Python, Python Programming, IF statements, Conditional Logic, Boolan, Statistics, Coding
Id: ko9ZiDDl3kI
Channel Id: undefined
Length: 22min 0sec (1320 seconds)
Published: Fri Jul 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.