Python Crash Course #7 - Tools

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·2y ago

Key Takeaways

The video covers various Python tools and techniques, including F strings, single line if statements, list comprehensions, and Lambda functions, to efficiently process data and create complex logic.

Full Transcript

hello there in this tutorial we are going to cover a couple of Handy Tools in Python specifically we are going to talk about how to add values into Strings using F strings then we talk about single line if statements besides that we have list comprehensions and finally we are going to talk about Lambda functions and understanding them is going to help you a lot to master the language first of all here's what we have written in the last video I want to create a new python file with tools. piy after that we can get started with f strings I want to have some user information and this is going to be a string that should be output to a user where we have something like Bob is 10 years old in this string Bob and 10 years old should be dynamic or to be a bit more specific I want to have two more variables where we have user user name for example this could be Bob and where we have a user age which could be 10 those two values should be what informs the string for the final output and F strings make this process incredibly easy although before we start with that let's outline the bad way of approaching that which I am going to store in another variable let's call this one the bad approach since you can concatenate strings by adding them together it is quite easy to combine various strings for example we could have the username and then add another string with is 10 years old if I now print the bad approach and run the code we get Bob is 10 years old you can see here Bob is is written together because in the string there should be one more space if I now run this again we we get Bob is 10 years old if we change Bob to I don't know let's say Anna then we are getting Anna is 10 years old this is working and to refine this further we could turn this string into two and then inject via a plus symbol the user age in there as well like so although now if I run out of this we are getting an error that we can only concatenate a string not an INT to a string essentially what we are doing here is is we have a string a string a number and a string the number is what python is really unhappy about although that we can fix all we have to do is turn the user AG into a string and then all of this is going to work just fine also I should remove this 10 years old then all of this is going to look much better and there we go now we have NR is 10 years old and we can customize NR and the age the problem is that this this kind of approach is really annoying to work with let me just add a comment this is the bad approach just to make sure you understand instead what is a much better way of approaching this is to use an F string and the idea is before you're creating the string you write the letter F on its own and then python enables a few extra things for a string they are all accessed via curly brackets inside of the string and basically you can add whatever information you want to add this could be another variable like username or it could be some kind of calculation for example you could have 10 + 20 and then I want to print the user information run all of this and we get anner is 30 years old we have gotten a variable and we have gotten a calculation inside of a string do make sure that you have the F though if you don't include it you can see it already all we get is username in curly brackets and the calculation in curly brackets only because of the F string does python calculate these values in our case we want to use the user name and the user age and now if I run all of this we get Anna is 10 years old and if I change these values to let's say 16 and I don't know Paul then we get Paul is 16 years old and I hope you can see that this kind of approach is significantly better compared to this bad approach not not only is it shorter it's also much more readable next up we have single line if statements for the example for this one I want to reuse user age and let's say we have a user that is 15 years old and we are running some kind of system where we have to determine if they use as an adult or a child which means we would want to check if user age is let's say under 18 then the user status is going to be a child if that is not the case else we want to set the user status to an adult and depending which status they get they get different kind of privileges or different kinds of abilities doesn't really matter for the example in this case all we have to do is print the user status we get child if I change the user age to something like 20 run all of this again we're getting adult a fairly simple if statement as a matter of fact this kind of if statement is so simple dedicating four lines to it is basically Overkill we could do this much more easily and for that we have single line if statements those are basically if statements for very simple checks the way you are using them is you are still going to create a variable and then you want to assign a value let's say the default value for this one could be adult but this value we only want to get if a certain condition is true in now our case we want to assign this if the user age is greater or equal to 18 after that we can add or rather we have to add an El statement if this condition is false what do we want to do in our case we want to assign the value child and with that we have the exact same logic compared to what we have done on these four lines meaning I can comment them out run the code again and we once again get adult if I change the age back to let's say 10 and run this again we're getting child the logic still works perfectly fine what makes this kind of logic even more powerful is that you can combine it quite easily with f strings for example if I copied the F string from the earlier example and then print it now for this one we don't have a username right now let's create one really quick we have a username with Anna then we have a user Ag and now we want to add one more bit of information let's say the per person is e and then we want to dynamically generate information now in there we could add user status if I run the code we getting Anna is 10 years old the person is a child so that is a pretty good start but what we can also do is simply get the logic that we have used for the user status and paste it in directly into the curly brackets if I run the code again we once again get the exact same outcome that way you can have really powerful print statements and insert pretty much whatever kind of information you want and I suppose just to finish off this bit let me add lowercase letters for all of this run it again then it feels a bit cleaner also we should add a DOT at the end but you get the idea after that if I comment out everything we can talk about list comprehension one of the most powerful Tools in all of python basically imagine we want to create a simple simple list simple list and this list should be a list with the values from 0 to 10 meaning 0 1 2 3 4 5 6 7 8 9 10 typing it out for a simple list is fairly doable but once again imagine you have a much larger and more complex project and you have to generate 10,000 lines of code you couldn't just type that out now you can tell python to generate values for example what we could be doing is create a for Loop for I in range and then we can create 10 values or to use the range function a bit more extensively we want to go from zero to 10 and then go at a step size of one inside of this for Loop we want to get this simple list and append the I value at the end of all of this we can print this simple list run the code and we're getting the numbers from 0 to 9 remember inside of the range function like with slicing we only go up to the number but we're not including it if you want to go to 10 we have to go up to 11 but not included and there we go we are getting the numbers from 0 to 10 that way we are dynamically generating all of these numbers but this isn't really a good way of doing it because in Python there's a much better way which is called list comprehension basically what you would be doing is you are a for Loop inside of a list on the line where you are creating that list we are still going to write for I in range and then you can Define the range so in this case 0 11 and 1 although if I now run all of this we are getting an error that this is invalid syntax there's one more bit that we have to add in our case we have to add an i at the beginning if I now run all of this we are once again getting the same numbers so what happened here the four part I think is fairly straightforward we simply have a for Loop like the one we have just created effectively I is going to get the values from 0o up to 10 these values are going to be 0 1 2 and so on all the way up to 10 the important difference now is this I this is the part where you can customize the logic essentially whatever you're getting returned from this this bit before the four part is going to be returned to the list on the first four loop we're going to return a zero on the second one a one on the third one a two and so on and this is then in the end what will be stored inside of the simple list for example what we could be doing is multiply all of these values by two run this again and now we get 024 and so on we have essentially cycled through all of the numbers from 0 to 10 and then multiplied the value by two and after that we have appended the value to this list that's all that's happening in here especially for this initial bit you can write really complex logic there's pretty much no end to what you can do I suppose the one limitation is that all of your code has to be on a single line but there is a lot of space for what you can write on a single line although there's actually quite a bit more what you can do for example you could add a whole second for Loop where we could have something like 4 J in and I want to cycle through a tall with the values a b and c if I now run the code we're getting a whole bunch more values because essentially we have two four Loops to understand a bit better what's going on here in the beginning we are still able to customize the logic and I want to create an F string where we have first of all the J part the stuff that we are getting from the second part and then we can add the I part if I now run this again you can see we have a0 b0 and C 0 meaning we are first running this four Loop and there the first value is going to be zero after that we are running the other four Loop where we are getting the values a b and c because of that we're getting a0 B 0 and C 0 after that we're going to the second part where we have A1 B1 and C1 meaning for the I Loop we got to the value of one what you can also do is add an if statement at the end for example I only want to append a value if J is equal to I don't know let's say a if I now run this again we only get the a values because B and C is never going to be the case I suppose not the most use fful example but you get the idea this kind of list comprehension you can make incredibly powerful although at some point you want to keep in mind that if this thing becomes too complex it's going to be really hard to read and then a simple for Loop where you have multiple lines might be much more readable this is a thing that comes with practice and at some point you're going to get an intuition of which one is better but to get started being able to read at the very least basic list comprehensions is really important also there are other kinds of comprehensions in Python you could for example write a dictionary comprehension the logic for that isn't all too different but I want to keep this video at least somewhat focused as a consequence let me comment all of this out and then we come to the final part which is going to be Lambda functions these are once again where we have the basic python functionality which is a function but now we want to write a function that is so simple that it doesn't really Merit a whole function we want to keep the entirety of the logic on a single line of code for example let's create a function where we simply want to double a value this one is going to need one parameter for the number that we want to double and the only thing that this function is going to do it is going to return the number multiplied by two with that if I print double value and insert let's say 10 I can now run all of this and we getting 20 we have doubled the value although this is way too much code for something so simple what you would rather want to do is to have all of the logic on a single line of code once again the way to approach that and let me comment out the original function we want to create a simple variable name let's call this once again double value and now we want to assign it a Lambda function and a Lambda function is basically a more primitive function that is constrained to a single line of code the way it works is you are still going to give it a parameter although this one now is simply going to be a single variable name could also be multiple if you separate them by a comma but in our case we only have a single variable let's call this one num to stay consistent with the previous function and then we can add the actual operation in our case we want to get num and multiply it by two what you want to be aware of is that this value the result of our operation is going to be returned automatically which means now I can run this again and we once again get the very same outcome meaning this function and this function do the same thing the logic is basically equivalent although the second one is much shorter and I would say a bit easier to read although you might be argued that you could simply do the operation right away and you would be correct this is so simple that you don't even need a function in the first place however sometimes there are cases where you absolutely need a function there are two major cases actually where this is going to become important the first case is that some functions want a function as an argument there sort function is the most famous One let's say we have a random list where we have some random values doesn't really matter what it is the important thing is that we want to sort this list for that we can use sort also I should organize all of this the print function should be right after this the important thing in our case is we want to create a sorted list and for that we will use sorted which wants to have two arguments but at the minimum we will need a single one we could simply add the random list in there and then print the sorted list and get the sorted list from the lowest to the highest value and there we get the result this is what working perfectly fine because by default sorted is sorting a list by their numerical value and we're starting from the lowest and going to the highest value so this function by itself is quite straightforward however now imagine that we have another case where we have a list that contains a couple of two buils where we for example have Anna and Anna has a default age of 25 then we have a another tup with another person Paul which has age of 40 and then we have another user I don't know Lisa who has an age of 10 and we want to sort these T buils by the age of the users if I now run the code again we are getting some weird Behavior where we get the age of 25 10 and 40 which is not in the correct order I think what python here is trying to do is to sort by the first letter of the name where we go from a l and P but this isn't what we want to do for that we can Define another argument inside of the sorted function and this has to be a named function where we can specify a key and the key has to be a function the way all of this is going to work is sorted is going to go through every single item of the list we are providing and then it passes the value into whatever function we are providing for the key this could be a normal function or it could be a Lambda function and for that to work IA function has to have a parameter in this case let's say the user Tuple and then whatever value is going to be returned is going to be assigned to this tual or whatever value we provided and then python or rather the sorted function is going to sort them by this numerical value so in our case we want to look at the user Tuple and then use indexing to get the first value which would be the AG if I now run out of this again we are getting the users sorted by AG and just to go over this one really quickly because it can be a bit confusing we are using this sorted function and pass in the random list meaning we have three toils that we want to look at and python is going to pass every single one of them into the Lambda function the Lambda function in turn is going to look at the user Tuple and one which would be the age of these users which is then the value it is going to return to the sorted function and sorted is then going to sort them from the lowest to the highest age which is exactly what we wanted this is one of the most common ways to use a Lambda function where you simply pass it into another function as an argument to for example sort a list that is slightly more complicated what you also see fairly often is that when you have a guey so a graphical user interface where user can press a button very often pressing a button should trigger a function but the function it triggers is very simple meaning there you see Lambda functions fairly often as well although this isn't an example that I can demonstrate quickly here I did however make an introduction to tkinter in there I am using Lambda functions all the time so check this one out if you are interested but anyway with that we have covered at all of the basic tools that I wanted to cover which means I can comment out all of this and then we can do one exercise what I want you guys to do is to create a list comprehension for a battleship board this one should be a list that starts at A1 and ends at E5 with all of the values in between I want to have the letters from A to e and the numbers from 1 to 5 also to make this a bit more difficult if the value is C3 then simply remove that part so pause the video now and try to implement this one on your own back in the code I want to add an exercise comment where we want to create a battleship board the associated value for that is going to be a list comprehension and right after that I want to print the result so battleship board and if I run the code right now we getting an empty list that is the expected result so not a bad start now we have to figure out what kind of list comprehension we have to create and I want to start with a letter so let's say letter for letter in a tupal with a b c d and finally e if I run all of this we are getting the letters from A to e By the way later on I will show you how to create the entire alphabet from from code so you don't have to write all of this by hand next up we will need the second for Loop where we have for Num in and this should be range where we want to start at one and then go all the way up two but not include six you could also add a step size but since one is the default we can simply keep this one as it is and with that I can run all of this again and we are getting the same letter multiple times not a bad start but not what I want the reason for that is that we at the moment only return the letter but this has to be replaced with an F string where we want to get the letter along with the number now if we run this we are getting the values from A1 all the way to E5 the last thing we have to work on is that if the value is C3 we want to get rid of that value and for that we can add an if statement if and I want to copy the F string if that value is equal to C3 then we would only get C3 meaning if I run this we are only getting C3 in our case we only want to return that value if the letter is not C3 and for that we need the exclamation mark along with the equal sign this is the operator for a difference we only want to get the value if the value is not C3 that way if I run the code we are getting the values from C1 C2 and then go to C4 we do not have a C3 which is exactly what we wanted and that is the exercise a pretty chunky list comprehension although in practice they can get more complex but this is usually what you see as an average and all right with that I hope you get some useful python tools

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches Python beginners how to use various tools and techniques, such as F strings, single line if statements, list comprehensions, and Lambda functions, to efficiently process data and create complex logic. By following the steps and using the provided tools, viewers can improve their Python skills and create more efficient code.

Key Takeaways
  1. Create a new Python file with tools.py
  2. Use F strings to add variables and calculations into strings
  3. Reuse user age without changing the code
  4. Use single line if statements to simplify conditional logic
  5. Create a list using list comprehension
  6. Use multiple loops in list comprehension
  7. Add conditional statements to list comprehension
  8. Multiply values and append them to a list using list comprehension
  9. Create F strings and add values to a list using list comprehension
💡 List comprehensions can be used to create complex logic on a single line, making code more efficient and readable.

Related Reads

Up next
How I Use AI to Write Facebook Ad Scripts That Actually Scale
Nick Theriot
Watch →