Python If Statements for Data Science / Data Analysis - P.2
Key Takeaways
The video covers the basics of if statements in Python, including their structure, conditional statements, and use cases in data analysis, utilizing tools like Jupyter Notebook and Python's built-in functions such as input and in statements. It demonstrates how to apply if statements for conditional logic, comparison operators, and logical operators, and how to create simple chatbots based on user input.
Full Transcript
what is up data nerds i'm luke and welcome to my channel where i make data visualization easy in this video today we're going to be going over if statements and if statements are really important especially in data analysis for carrying out certain actions if a condition are met and in order to evaluate those conditions we need to use certain operators so we're in addition to if statements we're going to be going over operators that compare comparison or identity and understanding what those are used for inside of if statements at the end of this we're going to wrap it up all together and we're going to have a comprehensive problem that we're going to solve that applies all of the concepts that we learned in this video so with that let's jump right in so here we are in my mac we have a jupiter notebook launched and we're going to be looking at first if statements okay so for the if statement the basic structure of an if statement is shown here in orange but it will start with an if and then from there you have some sort of condition that condition has to be evaluated to provide some sort of answer whether true or false otherwise it won't work then from there you have your colon and then the next line is indented so with python you indented four spaces so everything under this if if the condition is met will execute and then you'll put the code it could be multiple lines if necessary but it must be indented to fall under this let's look at a very simple example so in this we have an if statement then we have a condition i just specified true and we'll expand upon this later but i just want to keep it simple for now so if true we would expect that this code underneath will execute from here so if i run shift enter it will it will execute so it will print this this will print underneath it now if we continue just to show a further example if say we evaluate the condition to false we would expect the condition underneath it so this code statement will not execute however this statement after it is not indented so therefore it does not fall within the if statement so we would expect it to print so whenever i press command enter or sorry shift enter we see hey but this will because only this line actually printed because it falls outside of the if statement the next portion to look at an if statement that can be included is an else so if this condition is not met maybe we want some code to still execute even if that condition is not met and that's when we use the else statement and that's going to be on the same line as the if but then the code following the else must be indented in order to fall under so let's look at a simple example hey if the first condition is executed here to false yep we don't expect this to print but then the else we would expect this to print so pressing shift enter yeah just like expected this evaluated to false so the else statement executed moving on to the final portion to keep in mind when using if statements is an elif that is short for else if if you're coming from other languages and that is just saying hey if this condition is met you'd follow the if statement and then if you had another condition you'd use lf and for this you can use zero or as many as you want different lf conditions and the first elif to meet the true condition will execute and then from there you will exit this control flow statement um if none of these conditions are met then the else statement will execute in this case so let's do a simple example yeah so for this we have uh we would expect this first lf to execute because it is true and then we don't expect this second one to execute because we exited the loop so if we run this shift enter um yeah we got the but this will from this line and this was skipped even though it was true because um we executed the first one so this is all great and now we understand the basics of if statements but we're not going to be using this false and true we need to actually understand how to make conditions that we can use in our code so let's look at operators now the first operator to look at is a com is the comparison operators and we'll just do a simple comparison operator first so let's compare zero and see if it equals zero um pressing shift enter we see true okay so that's that's what we would put we could put this inside of an if statement if we wanted um to evaluate a true or false condition and just to show the point hey we would expect that 0 does not um it's not equal to 1. so this is false in this case one thing to note a sort of a rookie mistake is to make sure that you're using the double equal sign so if you're doing the single equal sign that's an assignment operator and in this case you'll get an error but that you can't assign to a literal but in other cases you may not get this kind of error message depending on how you framed it for other comparison operators we can do things like above we did equal we could do not equal we could do greater than less than could do greater than or equal to or less than or equal to and all of these whenever we go to execute we'd expect to get these conditions but those are just some other different comparison operators so let's look at these comparison operators in action within an if statement so in this case let's say we assign a name to a variable name and we want to evaluate it and so in this case we can use the comparison operator equal to to see if it equals a certain value and then it will evaluate this condition whether true or not so in this case hey the name is dwight so we would expect that for the first if it's going to skip this for the lf we would meet this condition it would be true and then we print get out of here dwight so running shift enter yeah as expected uh we met true on this and we get get out of here dwight looking next at logical operators this is used to combine conditional statements so say you had two different statements that you're trying to evaluate whether they're true or false and get a combined condition of whether true or false this is what we're going to be using either and or or to solve this problem so for this for and both must be true to get a true and for or one must be true to get a true and to show this in action let's do true and true if i press enter yes i would expect to get true because both of these must be true to get it now if i had a single false in here and false and run this i'm going to get a false similarly with the or statement if we did a true or false i would expect to get true because just one of these conditions are met whereas if we had a false or false we're going to get a false because neither one of these are true so therefore it's a false and then a more complex example would be this one in this case where you have multiple condition uh in here so false or true and then true and not false so first let's pick this apart so we can use this not operator which we learned about before to basically invert the sign so this is going to make this true in this case the next portion is the and and or have similar to an order of operations so this true and not false is going to get evaluated first so true and true i would expect this to be true and since this evaluates to true we're going to have false or true and because we have a true on this side we would expect to get true so running this we get true moving into other operators we have uh these are operators that are not as frequently used but i think you should still have an idea about their use so membership as uh as is in the name we're trying to see if something is a part of something so something simple that we use a lot we can use list or tuple so we're gonna use tuple in this case so we had this tuple of the the female cast and we wanted to see if a certain value was in it we can use the in statement to see hey is female is phyllis in female cast so i can run this and see that hey phyllis is actually not in the female cast and just sort of to show this point further we could also do is uh michael not in a female cast and yeah it's true michael is not in female cast the last other operator to think of is the identity operator and this is used to test object identity don't confuse this with a comparator operator so the using the double equal signs this is used to compare the identity and let's just show this and what do i mean by identity so if we did a simple example one is one okay this is true one is one and we have a built-in function within python to see hey i wanna see what the identity is so if we did id of one and just to show let's also do i do have another one and i do shift enter this is the a unique value and number assigned to one and for both of these these ids are similar so therefore the if statement evaluates as true let's move moving to a more complex example though say we had these two lists and you can see they look identical right they have the same values within it so i want to say hey a is a b and i do it gets false and you may wonder well these are the same have the same list with inside of it but if we looked at the identity of a and also the identity of b we can see that these are actually two different lists and they do not equal each other a better option if we wanted to like just to reinforce the point we'd want to use the double equal sign to be a comparator if we wanted to see if the lists were equal to each other and in that case we would get true so that is a quick overview of if statements and also operators such as comparison operators or identity operators and now we're going to combine this all and go into a practice problem so let's check it out for the application we're going to be building a bot if you will that is going to be using if statements to evaluate certain answers based on the input so for this we want a user to input what their favorite character is of the office we're going to evaluate it and then depending on what the value is we're going to output a certain print statement so we do have some rules with this so the name that they list must be in the cast members list if it's michael jim or pam we're going to say the bot's going to say hey this is also my favorite character if it's dwight or angela we're going to say this is not my favorite character and then for anyone else we're just going to say hey this is a great character looking at the list itself i have in here already the cast members list and this has all the different major characters from the office so before we get started as always i want to look at what is the approach i'm going to take for this so the first thing is i want the user to input the name next i want to evaluate if it is within the list if it's not in the list i want to [Music] i want to basically say i want to have them re-enter the name however if it is in the list i want to then start to go into its own if statement to evaluate um what the name is to have a certain output okay so that's the approach that we're going to be taking for this one thing that i realized that you may not understand yet is inputs so how would you get a user to input so we actually have a built-in function within python and just to show this in action we can say hey name equal to input and whenever i run this um it gives you this popup up here and you can input something so say i want to put my name so shift enter okay so now it takes this name in uh into and is assigned to name so if i go to name and press shift enter we can see that the name is now luke other thing to note we can put within here uh some sort of um text saying what we want the user to input so let's do what so we have this text statement now uh what's your favorite character the office so do shift enter and it pops up hey what is your favorite character at the office and we'll do dwight shift enter and now if we evaluate this name condition we would expect to see dwight so we have this first portion complete now so the user inputs the name we have this built into it now we want to evaluate if the name is in the list so we can do the uh in statement to evaluate so we'll do an if name in and what is the name of this list it's cast members so if name in cast members print and we'll just keep it simple now uh name in cast members and this is just something you can keep in mind should keep in mind whenever you are building anything you should routinely test it throughout the process as you're building it to make sure that it's executing as you expect so remember name is dwight isn't stored in there so i would expect this to have or i need to input the name so we'll put dwight and do enter and yep got name in cast members because dwight is in here where is it right there however we want them to re-enter the name if it's not in the list so we're just going to make this a little bit add a little bit more complexity to it so if the name is not in cast members we want to re-enter the name if not in the list so if name not in cast members you can put the input statement again so i'll just copy this command c place this command v and then we'll just put this uh a statement warning that that wasn't in the list and so yeah so now we have this entered um i'll do a shift enter and who's your favorite character office let's put me luke and if i press enter it will say hey name not in list re-enter the name and then if i enter dwight and press enter and then evaluate the name pressing shift enter yep dwight's in there okay so now we're on to the final portion and that's we want to do an if statement depended on the the name that's inputted and we have these conditions above and so for this we want to move into that so so we have this if statement saying hey if the name is not in the cast member's input so then we want an else statement and we want to have an enclosing if statement so the first one is if name is equal to michael and what other names do we have up here so if names michael jim or pam say is also my favorite character i'm gonna type that real quick okay so in this case we're doing the if so if the name is equal to michael or the name is equal to jim or the name is equal to pam we want to then print out we're going to print out hey the name and then plus is my uh favorite character and so now like always we should just run this for troubleshooting purposes so we'll do a shift enter what is your favorite character the office i'll input michael press shift enter and yeah we got michael is my favorite character so now let's add these other conditions so we want hey if dwight or angela say it's not my favorite character else say is a great character um i'm gonna go ahead and put this in real quick okay so i went and put this in and the logic i did for this uh as we have previously discussed hey if the michael jim or pam this is my favorite character then what i did is if hey if it's dwight or angela if it's not dwight or angela sorry um i wanted to print hey this is a great character else whenever it would obviously meet the conditions of dwight and angela we would output dwight or angela is not my favorite character so all right let's try this out shift enter and what is your favorite character let's try dwight now and press enter and it would say hey dwight is not my character if we try this again with somebody else on the list and press we can run this and let's say let's try toby and press enter it meets this condition this lf condition right here so hey toby is a great character and it meets this condition because remember hey it's uh the name is not one of these but it's not equal to one of these so therefore we're in this condition so there you have it we have the uh the chatbot uh built and to interact with the user using if and else statements and the different comparator operators bam so there you have it we covered if statements then we also moved into different operators such as comparison operators and logical operators to be used with inside of an if statement to evaluate certain conditions in the next video we're going to be going over loops so understanding for loops and also while loops and then we're going to apply what we learned from these loops to actually go in and read a csv and do some data analysis on the contents of the csv so as always smash that like button if you enjoyed this video also drop us a comment down below if you have any questions relating to if statements and if you're finding this video tutorial series interesting please consider subscribing and with that we'll see you in the next episode
Original Description
Python Fundamentals Course (DataCamp) 👉🏼 https://lukeb.co/PythonBasicsDataCamp
Data Analyst Track w/ Python (DataCamp) 👉🏼 https://lukeb.co/PythonAnalystDataCamp
(My recommended courses that I took to learn Python!)
This video provides an overview of if statements and how they are used by data scientist / data analysts. Additionally, we cover operators used within if statements such as: Comparison (==, !=), Logical (and, or, not), Identity (is), and Membership (in). At the end we build a simple chatbot with what we have learned, to provide feedback on characters from 'The Office'.
Also, if curious, here is more on operator precedence 👉🏼 https://docs.python.org/3/reference/expressions.html#operator-precedence
00:00 Introduction
00:46 If Statement - Basic if statement
02:19 If Statement - else condition
02:59 If Statement - elif condition
04:16 Comparison Operators - ==, !=
06:13 Logical Operators - and, or, not
08:15 Membership Operators - in
09:11 Identity Operators - is
11:05 Build a chatbot with if statements
19:14 Conclusion
Playlist (Course) Material 👉🏼 https://github.com/lukebarousse/Python_Intro
Basics of Python playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT8zTx9XO9jFf4PQ0iwBWfXB
Setting up Python on your computer playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT9udgCeqZpS4HKF6uIzra3r
Recommended Certificates/Courses (Affiliate Links)
==================================
DataCamp:
Python 👉🏼 https://lukeb.co/PythonBasicsDataCamp
Power BI 👉🏼 https://lukeb.co/PowerBIDataCamp
Tableau 👉🏼 https://lukeb.co/TableauDataCamp
R 👉🏼 https://lukeb.co/RDataCamp
Data Analyst w/ Python 👉🏼 https://lukeb.co/PythonAnalystDataCamp
DataCamp Subscription (Monthly $25USD) 👉🏼 https://lukeb.co/DataCampSub
Coursera:
Google Data Analytics Certificate (BEGINNERS START HERE) 👉🏼 https://lukeb.co/GoogleCert
Python for Everybody 👉🏼 https://lukeb.co/PythonForEverybody
SQL 👉🏼 https://lukeb.co/PostgresSQLUofM
Coursera Plus Subs
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Luke Barousse · Luke Barousse · 18 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
▶
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Connect Google Sheets to Tableau & Joining Data - Tableau Tutorial P.1
Luke Barousse
How To Use Tableau Desktop Controls - Tableau Tutorial P.2
Luke Barousse
Dimensions Vs Measures (Blue Vs Green Data) - Tableau Tutorial P.3
Luke Barousse
Create Stacked Bar Chart (and any other visuals EASILY!) w/ Show Me! - Tableau Tutorial P.4
Luke Barousse
Conditional Format Tables in Tableau (Like Excel!) - Tableau Tutorial P.5
Luke Barousse
Calculated Fields in Tableau (Formulas & IF Statements) - Tableau Tutorial P.6
Luke Barousse
Parameters (Create & Use in Calculated Fields and/or Visuals) - Tableau Tutorial P.7
Luke Barousse
Totals, Average Lines, & Trend Lines (Analytics Pane) - Tableau Tutorial P.8
Luke Barousse
How To Create a Dashboard - Tableau Tutorial P.9
Luke Barousse
Upload your dashboard to Tableau Public - Tableau Tutorial P.10
Luke Barousse
Install Python for Data Science on Mac & Windows (PC) with Anaconda - P.1
Luke Barousse
How to run Python for Data Science - Editors vs IDEs - P.2
Luke Barousse
Install VS Code with Python for Data Science / Data Analysis - P.3
Luke Barousse
Understanding Virtual Environments for Data Science / Data Analysis - P.4
Luke Barousse
Using VS Code with Python for Data Science / Data Analysis - P.5
Luke Barousse
Python for Data Science / Analysis ft. 'The Office' Dataset - P.0
Luke Barousse
Python Objects frequently used in Data Science / Data Analysis - P.1
Luke Barousse
Python If Statements for Data Science / Data Analysis - P.2
Luke Barousse
Python For & While Loops for Data Science / Data Analysis - P.3
Luke Barousse
Python List Comprehension for Data Science / Data Analysis - P.4
Luke Barousse
Python Functions for Data Science / Data Analysis - P.5
Luke Barousse
Lambda Functions for Data Science / Data Analysis - Python P.6
Luke Barousse
How NOT to learn Python for Data Science
Luke Barousse
What is Business Intelligence (BI)? 📊😅
Luke Barousse
Top 3️⃣ Technical Skills for Business Intelligence 📚📊
Luke Barousse
Top Non-technical Skills for Business Intelligence 📊👨🏼💻
Luke Barousse
M1 vs Intel Mac for Data Science
Luke Barousse
M1 vs Intel Mac for Excel 📈👨🏼💻
Luke Barousse
M1 vs Intel Mac for Python 🐍👨🏼💻
Luke Barousse
M1 vs Intel Mac for Business Intelligence Tools 💻📊
Luke Barousse
M1 Macbook Air vs Pro (8 vs 16 GB) for Data Science
Luke Barousse
Python for M1 Mac vs Intel (SPOILER: M1 is 2x faster)
Luke Barousse
Data Analyst's WFH Setup & Upgrades
Luke Barousse
Windows on the M1 Mac - What are your options?
Luke Barousse
Install your favorite Windows app on M1 Mac - ft. Parallels
Luke Barousse
Data Science shortcuts for Mac
Luke Barousse
Day in the life of a data analyst
Luke Barousse
Power BI vs Tableau - Best BI Tool
Luke Barousse
Mac Vs PC - BEST for Data Science
Luke Barousse
Data Scientist vs Data Analyst (funny!)
Luke Barousse
Become a DATA ANALYST with NO degree?!? The Google Data Analytics Professional Certificate
Luke Barousse
Certificates vs Degree for Data Analysts (ft. Google Data Analytics Professional Certificate)
Luke Barousse
Google vs IBM Data Analyst Certificate - BEST Certificate for Data Analysts
Luke Barousse
Python Vs R (funny!)
Luke Barousse
THIS got me my job as a Data Analyst - My portfolio tip
Luke Barousse
I used Python to Count my Bike Jumps!
Luke Barousse
Standout as a Data Analyst with THIS TOOL
Luke Barousse
STOP using Spreadsheets for Everything!
Luke Barousse
Transition into Data Science - My Tips & Story
Luke Barousse
Get a JOB w/ Google Data Analytics Certificate?!? (ft. Certificate Holders)
Luke Barousse
Staying Motivated in Data Science
Luke Barousse
Data Science - Expectation vs Reality (funny!) - ft. @KenJee_ds
Luke Barousse
Get NOTICED in Data Science!!! (3 types of GREAT projects)
Luke Barousse
Use THIS to showcase EXPERIENCE in Data Science
Luke Barousse
How to show EXPERIENCE... when you have NONE?!?
Luke Barousse
Learn PYTHON to be a DATA ANALYST?!? (or is R enough...)
Luke Barousse
The BIGGEST MISTAKE when starting a data project!
Luke Barousse
Top Jobs in Data Science
Luke Barousse
How to get Data Analytics side jobs - NEW LinkedIn Feature
Luke Barousse
Building a bot to scrape job data… How NOT to collect data
Luke Barousse
More on: Prompting Basics
View skill →Related AI Lessons
Chapters (10)
Introduction
0:46
If Statement - Basic if statement
2:19
If Statement - else condition
2:59
If Statement - elif condition
4:16
Comparison Operators - ==, !=
6:13
Logical Operators - and, or, not
8:15
Membership Operators - in
9:11
Identity Operators - is
11:05
Build a chatbot with if statements
19:14
Conclusion
🎓
Tutor Explanation
DeepCamp AI