Python Crash Course #5 - Control Flow
Skills:
Prompt Craft80%Advanced Prompting70%Prompt Systems Engineering60%Agent Foundations50%Tool Use & Function Calling50%
Key Takeaways
This video series covers the basics of Python, focusing on control flow, conditional statements, and loops, using tools like if statements, while loops, and for loops to manage code execution.
Full Transcript
in this section we are going to cover how to change the flow of the code in Python for example imagine you have some code that you only want to run if a certain value is true or maybe you want to repeat I.E Loop a code as long as the condition is true finally maybe you want to run some code for every entry inside of a container like a list or a tupo for all of that we're going to cover some logic in this video and the most important thing we have to learn about is a boot value this is a new data type that can only be true or false there's no other value possible the easiest way to create it is with comparison operators like equal to smaller than or greater than notice for this one we are using two equal signs to check for equality since a single equal sign assigns a value to a variable let's play around with all of that first of all just to have some basic idea of what's going on once again we are back in the code and I want to create create a new python file let's call it flow. Pi then I can go into full screen again and now I want to create a basic Boolean value the easiest way of doing that is I want to print some kind of comparison let's say 5 is equal to 5 if I run that code we getting the output true which seems like the correct result for this one however if I change this to a four then we're getting f because five is different from four however what you could be checking is if five is greater than four then we get true once again so the result of this operation would be a Boolean value which we can use for example imagine we only want to run some code if this kind of condition is true for that we have an if statement which is going to look for a Boolean value for example we could add in five is great greater than four which would be true and now pay close attention if I press enter python indents the line by one Tab and this is really important let me add one line for now I want to print correct result with the print statement being indented this indentation is really important because it tells python that this print belongs to this if statement if it wasn't indented python would considered as a general part of the code meaning it would run regardless of the result of the if statement as a matter of fact let me add another print statement that is not indented so print let's say test if I now run the code we can see the correct result this is what we get from this print statement and then we get test which is the second print statement the first print statement the one with correct result only run because this if condition is true if it wasn't true let's say we can change this to 5 is equal to four and run the code again then we cannot see the print correct result because the if statement is false however we can see test because this one is not indented so it's not part of the if statement so I hope that makes sense but if you get the basic idea you understand basic if statements we add the if keyword then we add some kind of Boolean value this can either be a Boolean value directly so if you add if false this is never going to run and if you add if true it is always going to run but most of the time you have some kind of comparison let's say five is greater than four and then it would be running on top of that what you can also do is add an else statement let's say we can have print correct result and print incorrect result if I now run the code we're getting the the correct result because the first if statement ran however if we change the condition let's say three is greater than four and run this again we getting the incorrect result since the condition was false we are running the else statement and that way we're getting the incorrect result and once again the print statement for incorrect result is indented so python knows it's part of the else bit what you can also do is set an L if statement the way it works is we first of all check if a condition is true at the moment this one would not be correct then we can check for another condition let's say 2 is greater than 1 and if that is the case we need to double colon again we can print let's say some other result which in this case would run because two is indeed greater than one however if that wasn't the case we would go to else let me run all of this we're getting some other result because for the El L if statement the condition is true if it wasn't let's say 0 is greater than one we would simply end up with the incorrect result you can expand this system quite a bit for example you could have multiple L if statements let me duplicate it and add it like so we can for example check if 0 is equal to0 and this one would be true so we are getting some other result the if statement would end on this bit you can also chain different parts together inside of an if statement meaning we only want to for example run this L if statement if 0 is zero and let's say five is greater than one only if both of these values are true then we are going to run this L if statement and something like this could be done for every single if statement also what you could be doing inside of any of these if statements add another nested if statement I could add if and then add another condition and you can be quite Fancy with your conditions for example what you could be checking is the length of some list with one two and three if that list is greater than two then we want to do something else let's say print list is long enough and now if I run the code again we get some other result because this condition and this condition is correct so we are printing this bit and then we are running the other if statement and we are getting to this print statement I think the logic for all of this is fairly straightforward there isn't that much to it the main thing you have to understand is that you always need an if statement then a condition and then you want to indent whatever code you want to run by one tab the code you can add is basically whatever you want could even be another if statement or some function or method or list or dictionary literally whatever you want python is super flexible and well with that we have if that is basically all you have to know about them next up we have a while loop this one is going to run a code as long as a certain condition is true and to work with all of that I am going to comment out all the things we have done for the if statement for example what we could be doing is first of all I want to have some kind of counter which is starting at zero and then I'm going to use the while keyword this one like if wants to have some kind of condition for example let's say that the counter is smaller than 10 or to use a comparison we haven't used yet smaller or equal to 10 if that is the case we want to print let's say we want to print the counter and after that we want to increase the counter by one now if I run this again we are getting numbers from zero all the way to 10 and then the wire Loop stops we are checking this condition and as long as counter is below or equal to 10 we are running the code inside of it which is going to print the number and then increase cter by one which is going to run 10 times from zero all the way to 10 and then it stops or rather we go to the line after the while loop and there we can print while loop has finished now let me print it again and we get at the end while loop has finished once again the initation is really important this print statement and this counter are part of the while loop because of the indentation indenting things in Python is fundamental that way you know a line of code belongs to an if statement a while loop or later to something like a function or a class also what you could be doing in there is add an if statement so if if counter is equal to 5 then I want to print I don't know counter is five if I now run this again we can see on number five counter is five and if I had spelled this correctly this would also look much better but anyway you know it's working perfectly well and that is basically all you have to know about while Loops they honestly not that common in Python also you really want to be careful with them because if you up this wire Loop is going to run forever and might crash your code for example if you forgot to add this counter plus one let me comment it out actually and now run the code it is going to run forever we are getting a zero eternally we are never going to reach this print statement or any code we had after this while loop if you ever ended up in an infinite loading screen this is what might have happened so be careful when you're using while Loops you always want to have some kind of Escape condition which in our case is the counter plus equal 1 but that's about it while Loops aren't that complicated finally we have a for Loop a for Loop is similar to a while loop except that this one is only working for every single entry of some kind of container let me create a test list which is going to be a list with 1 2 3 4 and five the way of for Loop works is with the four keyword and then we need a certain kind of name let me for now say 4X in test list what is going to happen is that python is going to go through every single item inside of the test list meaning 1 two three and so on in our case and then on every single Loop it is going to store that value inside of this X or whatever you name the value basically we are creating a variable for the duration of this for Loop and storing the current item we are looking at inside of this variable that way we can easily use it inside of this for Loop for example we can print X if I run the code now we are getting the values from 1 to five exactly what I have specified inside of the list also if you are only seeing zeros in the terminal then the wire Loop is still running for you to end that one simply press on the trash can symbol that way you ending the code and then run everything again it should work just fine cool but in our case we have a simple for Loop where we can execute some code for every single item inside of any kind of container this is going to work with a list a tuple a set and a dictionary in fact let me create a set really quick with 1 2 3 and three and once again we're getting the values from 1 to 3 remember a set ignores any duplicate values then we can have a tuple with 1 2 3 and three and then we're getting all of those values and if we have a dictionary with a couple of key value pairs let's say one and two then we have three and four and we have five and six if I now run it we only get 1 three and five which are all of the keys of the dictionary which is not necessarily what you want to get for that inside of a for Loop especially for a dictionary you have a couple of methods that are super useful those would be keys and don't forget to call this one and this one would only return the keys of the dictionary meaning if I run this again we're getting the same outcome because this is the default value however what you can be doing is values that way you're getting only the values and on top of that there's one more method called items and this one is returning both meaning we're getting a tupal with the key value Pairs and then we have access to the entirety of the dictionary inside of one for Loop but other than that you can still run whatever code you want inside of the for Loop in that regard it's very similar compared to while or if so not that much new stuff to learn for example what you could be doing for this x you could use indexing where we only want to get the first item then I can print this again and now we are back to only getting the keys of the dictionary I suppose there's one more thing that you do want to be aware of with a for Loop and that is that at the moment we are always getting a key value pair inside of a tupo meaning we have a tupo with always two items since this is a bit annoying to work with with a single variable python has a nice feature where if you know the length of the two bus you're getting returned or more generally if you have any kind of data structure where you know the amount of items inside of the container then you can unpack the items right away so in my case I know I always get the key and a value k andv for short I can unpack both of them right away inside of the for Loop that way inside of the loop I can get access to the values much more easily I can print K and V and the result would be that I have access to the values individually especially for more complex for Loops this kind of system can be really handy to work with but all right with that we have the three major tools that you can use to control the flow of the code and also you can combine them now before we go to an exercise there's one more really important concept that I want to cover so far we only use the basic comparison operators but but in Python you have another concept which is called truthy or falsy which are the result of an automatic conversion of any data type to a Boolean so what does that mean well basically in Python every empty container every string without a letter and every zero is going to evaluate to false everything else is going to be truy if you add a zero into an if statement python needs to figure out what it's going to do and for that we have truthy and falsey where a zero inside of an if statement is going to be a false value or it's going to be the equivalent of a false value let's play around with that really quick back in the code I want to comment out the four Loop and instead add a comment for TR Fe and falsey basically imagine you have the case where if zero and then you want to print I don't know something doesn't really matter this might seem weird but if you have zero and I run the code we don't get anything because zero is what python considers to be faulty it would evaluate to equivalent of a false which for python means that we are not running the if statement however if you change the zero to a one and run all of this again we are getting something because a one in Python is truthy meaning if you put it inside of an if statement the code is going to run to organize all of this a bit better let me change this something to Tru V and then add an else statement for falsey that way we can very easily tell if a value is truthy or falsey for example if you add an empty list so a list without any value and run the code again we getting faaly because a list without a value is going to evaluate to false however if you add any kind of value let's say a one then we are getting truthy because any kind of container with any value is going to be truthy finally the last thing is you could create a string without any values this one would be faly however as long as you have any kind of content even a single letter then you are going to get to truthy the logic is going to become intuitive quite fast the main thing you have to remember is that the only values that are becoming faulty are empty containers strings without letters and the number zero everything else will be truthy you only really have to learn about three special cases everything else is going to be true and for completion sake there are a couple of functions that return either true or false any and all are the most common ones both of those want to have some kind of container as an argument and if all of the values for example for the all function are true or at the very least true fee then it is going to return true as a result for the any function it is going to return true if any value inside of the container is a true or truthy value those are ultimately fairly simple functions so I don't think we have to practice them instead I want to do an exercise I want you guys to create a list with the values from one to5 and then run some code for every single item of this list if the value is two then print the value two if it is not then print the value is not two finally if the value is five run a while loop to print last item six times pause the video now and try to figure this one out back in the code I want to comment out the truthy and faly bit and instead add an exercise part for this one first of all we will need an X S size list 1 two 3 four and five and that's all we need this could also be a tupo and I realized on the slide I have used parenthesis instead of square brackets but the difference isn't going to matter next up I want to create a for Loop let's say for X in exercise list and then I can print the x value and now if I run the code we're getting the values from 1 to five that's a really good start after that in the indentation of the four loop I want to check if x is equal to 2 and if that is the case I want to print the value is 2 however if that is not the case else I want to print the value is not two let's run this one again and we get the value is not two the value is two and that is only happening if the value is inde two and other than that we get the same outcome which is the El statement finally we have to create a while loop to print last item six times for that we will need the while keyword along with some kind of condition the condition in this case is going to be let's call it the exercise counter which is going to start at zero and I want to check if the exercise counter is smaller or equal than six and if that is the case I want to print last item really important we have to increase exercise counter by one otherwise this wire Loop is going to run forever and now if we running all of this again we are getting something weird we start with the value is not two which is this if statement after that we get last item a couple of times and after that we get the value is two and then the value is not two multiple times we never see last item again so what happened here and once you think about it it actually makes a lot of sense we start with the for Loop and then on the first for Loop we have X being one so we are printing the value is not two this is what we have seen if I go up a bit on this line after that we are running the while loop where we are checking if the exercise counter is smaller than six if that is the case we are printing last item and then increasing the counter by one on every single run of the while loop the issue is that we are running all of this on the first iteration of the four Loop meaning after the first four Loop the exercise counter is going to have a Val value that is greater than six meaning this one is not going to run anymore after the first run of the for loop as a consequence we cannot see it anymore and it's not going to have an impact the thing we actually want to do if we want to have last item at the end of the for Loop is we have to make sure that the wire Loop is not indented that way we are getting the value is not two the value is two for every single one of the items and then we get last item after the for Loop this might have been a little bit tricky but I hope you understand the logic it's a really good way to practice indentation but anyway with all of this we have a lot of control over the execution of our code
Original Description
In this Python crash course tutorial series, you'll learn all the basics of Python from the ground up.
🚀🥷🏼Get access to the Python Crash Course on Net Ninja Pro:
https://netninja.dev/p/python-crash-course
🔥🥷🏼 Access Premium Courses with a Net Ninja Pro subscription:
https://netninja.dev/p/net-ninja-pro/
🔗🥷🏼 Check out Clear Code for more of his own tutorials:
https://www.youtube.com/c/ClearCode
🔗🥷🏼 VS Code - https://code.visualstudio.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
How to build a faceless YouTube channel with AI in 30 days
Dev.to AI
10 small AI wins that quietly give you your afternoon back
Dev.to AI
Stop Starting With AI Tools. Start With the Workflow.
Medium · AI
AI Can Deploy Your Infrastructure in Seconds. That’s Exactly What Worries Me.
Medium · AI
🎓
Tutor Explanation
DeepCamp AI