Python Tutorial for Beginners – Full Course in 12 Hours (2022)
- June 3, 2024
- Posted by: MainInstructor
- Category: BASIC C Data Science Go Java JavaScript Node Python React Ruby SQL Web Development
Video Title: Python Tutorial for Beginners – Full Course in 12 Hours (2022)
welcome to the best Python tutorial in the world they’ll take you from a complete beginner to not so bad this tutorial will cover so many things you’re going to be building amazing projects all with python as a beginner so you’re going to be building an app with like a counter app we’re gonna go ahead and build guess the number game a YouTube app that’s gonna queer YouTube get the data back show it on the screen rock paper scissors who doesn’t like rock paper scissors and we’re also going to be building a random dog generator app which is going to be awesome you’ll be able to click on the dogs generate the dogs it’ll remember you logging in it’ll remember you logging out there’ll be a leaderboard of who generated the most dogs I mean oh my gosh all this with python while all of that is fun first we need to get our fundamentals so the first thing this course is going to start with is clear cut beautiful amazing fundamentals okay mastering basic concepts starting from variables to going to conditionals going to Loops for Loops diction lists you name it list comprehensions all of that stuff we’re going to cover if you’ve never coded before a day in your life you’re absolutely in the right place if you’re even intermediate I promise there’s going to be things in here that I’m going to show you they’re going to blow your mind ways to refactor your code or List comprehensions Lambda functions all the stuff is available for you if you’re Advanced there might be still some tidbits that are there that you’ll be able to pick up with that said hey if you don’t know me I’m rafikaze the CEO and founder of clever programmer I’ve helped teach millions of people how to code and now my goal is to help you learn how to code and become a software developer alright now if you want to follow along with me into this course you’re gonna need to scroll down below and click the link in the description join.replet.com CP Dash python open that up and Fork this repository if you don’t Fork you won’t be able to write any of your code or do anything with this okay and the reason why I’m having you use this Rebel is so you don’t have to install python you can just get it up and running without having to do anything everything will be here like the images and the assets you’ll need your project links for your exercises all the notes will be here for you so everything that you need to be able to get access to you’ll have it in here without doing anything and then once you’re done it’ll bring you to a page like this in this main.py file is where you’re going to write all of your code and I mean all of it everything goes in here okay this quasi.py file that’s here this is all the code I will be writing in the video and you’ll be following along with me and so you can go at any time to this quasi.py here and you’ll get access to and be able to see all the code that I’ve written so that way if you’re worried about missing notes or anything don’t be because everything is already there and so you’re not going to miss anything this readme you should be seeing this over here in the if you select markdown at the top here okay and this is pretty important and this will have all of your Prime projects here available for you these projects you won’t be jumping into them now but you’ll be jumping into them once you master certain Concepts that shows you that you’re going to be building this app right here and it tells you hey these are the concepts pure python Concepts that are going to be covered so make sure before you get to this project you master all of these Concepts that I have listed out here okay and then it tells you hey don’t worry about the web development Concepts you won’t need to know these uh before you can start building this all of these applications you build will be already online and deployed which means that you could just send it off to anybody anywhere and they can play with these projects that you build any project that you go to will have this project link where you can write your code it’ll have this demo link where you can go ahead and demo your apps and then it’ll have a solution link as well so you could click and view the solution and all the notes we’re going to take throughout this will be made available to you here as well in this markdown if you ever want to go to it again just hit markdown and then just scroll down and so like if I’m talking about dictionaries those will be listed out here conditionals Etc with that said we’re going to start from complete complete complete beginner stuff okay so don’t stress Don’t Worry Don’t let this file overwhelm you this is more to just give you a road map and what’s happening we’re gonna start from baby steps I’m Gonna Hold Your Hand every single step of the way and it’s gonna be a lot of fun with that said I hope you’re excited before we get into the video do me a huge favor smash that like button turn it blue turn it white I don’t care what color you’re turning make sure it’s filled and it has color and you’ve smashed it that’s the most important part and as a way to thank you here’s a picture of a baby a koala bear and a banana spinning all of it at the same time oh too much all good I don’t care I’m here to help you learn how to code and if we have to do some things like criminal activities like have fun once in a while for God’s sakes we will freaking do that with that said let’s go ahead get started and start coding this bad boy up right now now before we start foreign it up here’s what I want you to do we have turned this course into a bite-sized Drip by drip netflix-like binge-worthy course content and in a course portal it is completely free the only idea behind it is that you the student will have a great experience going through it if you want to be a part of that the link is in the description below click that link it’ll take you to a page where you can pop in your email and it will give you access to this exact python course it’s going to be the same material as is on YouTube and everything’s gonna be all nicely in one place so I recommend you go ahead click that button below and sign up for it again it’s completely free enjoy just giving extra resources so you can be a better developer before we started diving deep into coding and starting to write every single line of code you’ve chosen python let’s just talk about why and what your career as a software developer or just a person learning python could look like why doesn’t matter so I think let’s get the context around python first and then we’ll get to the coding part if you want to get to the coding part right away and you don’t care you just want me to stop yapping and shut up you can just skip this part time stamps are below you could skim and do whatever you want it’s a free country with that said I’m gonna kind of go into why should you choose python I think python is one of the best ways to start learning how to code I started about 10 years ago coding and my first language of choice was Python and the opportunities you get by becoming a python developer are absolutely insane because first of all it’s one of the most popular programming languages and there are insane number of job openings with python you could look on indeed.com or really any other online platform so the popularity of the language is also huge if you look on stack Overflow it’s one of the top most wanted and learned programming languages in the world and when it comes to people’s salaries people are making sixty thousand seventy thousand hundred thousand dollars a year plus now what type of jobs do people do lot of people with python do data analysts type of job data science type of job I mean of course there are a lot of web developers with python too because you can learn different Frameworks okay like flask or Django Etc and you could do web development as well so there’s a lot that you could do with python and your career could look great whether you want to learn it as an entrepreneur or a student in high school or you’re just trying to get through college man or just watching these videos so you could pass your exams I mean either any of those options is totally fine now the way I structure this course you have a little bit of insight going into it is I’ve designed it as a practical step-by-step guide where I hand hold you and we learn together but then we do exercises together and then we do big projects together and the whole idea is how can you learn in a practical way so tomorrow you could take this knowledge and go get a job okay now you won’t get a job right after taking this course but if you synthesize this material and then add a lot of supplementary material and spend some time this will set you on a path to being able to get a job as a software developer freelance with it have a full-time job or maybe you want to have your own business or start a SAS whatever your case may be but my goal in this course just to give you a taste of python and build amazing projects you can understand and just fall in love with that said let’s just get into it boom all right welcome to the ultimate Python tutorial now how I’m going to teach you is almost like we’re walking step by step and learning side by side together kind of like how I did with my JavaScript tutorial if you watched that earlier so let’s just get started okay like we’re buddies and we’re doing this and learning this together so the first thing that I want to tell you about is something called comments and comments are basically anything that’s ignored by the computer so anything that we write with a hashtag computer will just ignore that line the next thing we’re going to talk about are variables how do variables work in Python well if I actually do print hello world okay first let’s just start off with this and I hit run on the right hand side over here you’re gonna see a hello world so this allows us to print out whatever we’re doing into here for example if you did not say print and you just said hello world like this it wouldn’t actually show anything to the console so we want to put it in print and then put quotes like this around it now what the heck are variables and how do they work well let’s say I want to print my name Rafe Kazi now every time I want to print my name maybe I want to print it again well I’ll have to write print I’ll have to write rough it cozy and then do this again and then do this again using this ton of work but what if I just created a variable that said name and set that to rafikaze and make sure you put these single quotes or double quotes either should be fine and what if I did this and then I said print name and now if I command enter or click run there it’s going to print out my name okay on the right hand side and now if I change the name I only have to change it in one place not on every single line let’s change the name to clever programmer and now I’ll run this and there you go you could see that that name is being printed out because of line 9 right this is referring to that variable over there if I have another variable like age is 25 I can now also say print age and when we run this you’ll see that it actually says the age right over there now here are a couple of things you can and can’t do with variables that you should just know about so for example I can’t name a variable starting with the number three name equals quasi and now if I try to print three name and run this I’m going to get an error okay and it says hey what tech is going on invalid syntax that means you wrote something that’s not right so when we’re naming variables make sure that they start with a letter most of the times and then you should be good to go now other things I could do is I could say full underscore name and now we can do print full name and let’s run this and now it reads out our full name underscores are allowed in Python whenever you’re writing multiple words you separate it out with underscores this is snake casing okay so you keep everything usually lower and then you separate things by underscores and kind of one of the last things I’m going to touch on for now with variables is you could have two variables being saved in one go so I could say with comma height is equal to 400 by 500. what this does is it actually sets the value of 400 inside of width and then it saves a value of 500 inside of height and now to prove that out we can go ahead and say print with and if I go ahead here and say print height and when I run this you’re going to see that we get a 400 over here 500 over there that’s how naming multiple variables in one go Works outside of this we’re good with variables so we’re going to keep going okay now let’s talk about what could we do with this stuff what is something that we could actually do with python well there’s something called input that I actually really like and what input does is when you have input it’s going to ask you to put in something right here as the user so we can make our names a little bit more Dynamic now and make the name dependent on the user or whatever the user wants the name to be let’s say we create a variable called your name and then inside of this variable your name we’ll say input lease enter your name so your name is going to store the value that the user types in okay it’s going to ask for the input and it’s going to give this as a prompt so watch what happens when I run it on the right hand side here you’ll see please enter your name so I’ll actually say let’s say I will say Andrew let’s hit enter now it was stored Andrew ended up being stored inside of your name but we need to show that so how do we show that what have you learned so far where you can actually go ahead and show that to me so print and what would you print okay take three seconds right now three two one you would print your underscore name right if you want to show it out on the screen so let’s run this now and I will say cozy and boom look at that it says qazi and if we want to make a little greeting thing that can greet greet us we can we can say a high plus your name so like just like this hi plus your name so let’s give that a try and we’ll say qazi okay now it says hi Kazi look at that that is beautiful if you don’t want some of these lines to run that are the previously there like that and that just comment it out or what you could do is highlight it and hold command and press slash I’m pressing this button right here this slash or hold Ctrl and press slash if you’re on Windows let’s go see if we can do other things like can I give two inputs and then we can sum them up so let’s try it so let’s say num one is equal to input enter a number okay and then we’ll copy that and say num2 Okay so this will give us two numbers and then it’ll print out those two numbers so let’s try this so we’ll say num one comma num two let’s run this and we’ll say 10 and we’ll say 20. okay so 10 and 20 got printed out that means we were able to take that value from the user and then show it out onto the screen now what I want to do is something a little bit more interesting so I want to add these two numbers together that the user is giving me well if I want to add them together wouldn’t that be really simple can’t I just do num1 plus num2 right because that’s what the user is giving me and so I should just add them together and if let’s say the user gave me 10 plus 20 this will become 10 and this will become 20 and well 10 plus 20 well that is a 30 and so that should print out 30 onto the screen right well let’s take a look at what happened so I’ll run this and then I’ll give 10 and 20 and what should it return if I add 10 to 20 what do you get 30 right let’s hit run and look at that I instead get 10 20. what is happening here so when you add two things together two strings together it adds them up and mumbles and jumbles them so this is a good point to talk about strings versus numbers so there are different data types inside of python on and there’s there’s a bunch of data types so you have strings as a data type numbers is a data type numbers is not a really data type but integer is you know float these are what we just refer to as numbers and then there’s other data types like lists and python there are dictionaries but some of this stuff is a little bit more advanced like lists dictionaries we’ll talk about these later for now what I want to just talk about is I want to talk about the num difference between strings and numbers I’m just going to write some examples of strings hello is a string cookie anything within these single quotes or really double quotes is a string you’re telling the computer literally this it literally this and literally that so when I add them together if I ever added this together the computer would give me back hello cookie as one string so what’s happening right now is we’re storing a number our numbers as a string data type so it’s saying taking a string of one string of two string of 10 rather and string of 20 and is returning a string of 10 20 because it’s just combining them because that’s what it knows what to do with strings so when you input a number it doesn’t think of it as a number it actually stores this as a as a string so what we want python to do is we want to tell python hey convert that into an integer okay a number in other words that I could actually use and when you add numbers together to actually add up rather than just get concatenated and squished together so what I want to do here is this num one that I get I want to call the function int on it so I’ll store it as an integer or let me just show you something simpler even in the console so let’s say I have five and you can see it has quotes around it this is a string but if I call this function called int and I pass 5 to it okay as a string it will Typecast it and turn it into five so that’s actually what happens so what we want to do here is store for the input that we get from the user as an integer so I can take this entire thing and wrap it inside of int or I can call Int right here either way should work now let’s run this and we’ll do 10 20 and you can see that it actually added it up to a 30 because it said hey turn it into an integer the number one which is 10 turn the second number into an integer which is 20 integer 10 plus integer 20 that gets you a 30 and then that gets output to the screen does that make sense okay great now let’s end this section off by making a little baby app you know every every single thing we do I like to actually make it step by step like a project or some kind of app that we make even if it’s like a baby or a really really small app so let’s do it so what we want to do is we want to make a tip calculator app in this case Okay so this is going to be something that’s very simple what we’re going to do is let’s comment out this stuff right here here so I’ll commented out you know I can comment down basically uh everything up until now that’s fine so in my tip calculator app what I want to do is uh let’s say that I went to a restaurant to eat some food and let’s say the food was a hundred dollars well if that food was a hundred dollars and I want to give a 20 tip you know the question would be how much am I paying in total if you’re quick at math you’ll realize this is 120 dollars right because this is actually the food amount like 100 and this is the tip amount twenty percent tip okay and then 20 of 100 is twenty dollars you might know that in your head but how do we actually do that in calculation so let’s go ahead and make this right so let’s set a food amount to be a hundred dollars let’s represent a tip percentage to be you know let’s say 20 percent and and then we could divide this by 100 or or just make it 0.2 because this represents 20 as well so we could you know since it’s a percentage let’s go ahead and just do 0.2 or 20 divided by 100 okay and these are some math operators which we’ll talk about more later but if I just go ahead and show you in the console here if I take 100 and I divide it by 20 I get 5. if I take 20 divided by 100 okay I got 0.2 this is like division what we learn in like second or third grade all right and now math operators well there’s uh some interesting ones two inside of python so for example I could actually go ahead and say hundred two division signs and 20 well you’ll be like well this doesn’t really look that much difference but watch this if I do 20 and I do two division signs and I do 100 I get zero that’s interesting so what this basically does is it says hey don’t give me anything that has any kind of remainder like just give me things like divided you know if something divides completely then show it to me otherwise I don’t care I don’t need a floating or a decimal point as a division let’s say I take 135 divided by 2 it gives me back a 67. okay whereas if I did it the regular way I would get a 67.5 so this will actually divide and then round it down okay so when I round that down I get 67 so you’ll get whole numbers this is sometimes like actually helpful because if you want to just quickly round down the thing and you don’t want to do like all kinds of stuff and Typecast a thing and make it all this this simple way okay so in terms of math operators we’ll talk more about these later as well but some simple math operators that we have are like the plus sign you have the minus sign you have the division sign multiplication so you can multiply things right you have exponents so if you do 2 to the power of 2 you’ll get 4 and if you do 2 to the power of 4 you’ll get 16 right so a regular exponents you have uh divide and get a whole number this one and then you know kind of the last one that I’ll just leave you with for now is is a modulo so this just gives you what the remainder is so 20 modulo four there’s no remainder so you get zero but 20 modulo 3 you get a remainder right because 20 if you divide it by three you will go up to 18 but then you can’t go beyond 18 and then you’ll have remainder of two so I’ll leave the modulo here as well okay so these are some of the math operators that’s like good for you to kind of know and we’re just going to be using the division and maybe the multiplication one when we’re doing our tip percentage okay and with tip percentage Yeah we do care about the float number so that’s why we’re just going to use a single division sign we don’t need it to be a whole number we need it to be a actual number that it’s like you know it could be afloat because we are expecting a 0.2 so anything that’s a decimal like this right if I go 0.2 it’s a float number if I go one or two or three these are all integers okay and this is a float float is just another way of saying decimal numbers in uh encoding most of the time so whenever you float think some number that’s a zero point something so this is representing 0.2 right so I have my amount this is 20 as a tip okay and then what’s the tip amount so tip amount in this case would be a hundred multiplied by that so if I have a hundred multiplied by 0.2 that’s a twenty dollar tip so that’s the actual tip of mail so it’s food amount times tip percentage and that’s my tip amount we got the tip amount right which is food amount multiplied by the percentage so let’s just go ahead and print out the tip Mount and let’s just see if it’s um if it’s what we expect it to be so I’m going to hit command enter and run this it’s saying percentage aha so it’s tip underscore percentage right not just percentage and that’s what the error is telling me so errors are your friends don’t freak out just read them and usually when you’re reading errors you don’t have to read like the top part that much you can read this part but I’ll also kind of make you familiar with errors okay we’re not going to go crazy into like reading errors but in a simple way like if we’re looking at this error here it says Hey percentage is not defined so that was simple because we never had a variable called percentage we had tip underscore percentage so that makes sense so name error right and then the next thing it says is Trace back it says the error is in main.py which makes sense because this file is called main.py and it tells you that the error is happening on Line 39 this is Line 39 and it tells you that it’s happening specifically somewhere here okay so it really really makes your life simple once you can kind of read it and not freak out so much about it and let’s keep going so let me run this here and let’s see and it calculates a tip amount correctly so look tip amount is twenty dollars well now my question to you is how are you going to get the total okay we have the you know total if I had to put total so first of all you should try and see if you can get the total here so you can just pause the video and give that a try I’ll give you five seconds five four three two one so what is total total is really just two things right it’s food amount plus tip amount that’s what a total is right it’s how much did you pay for the food and how much was the tip so in order to turn this and translate it into code all we say is we say total is equal to food amount plus tip amount and now let’s print out total and let’s take a look and over here we have 120 okay 120 dollars okay now we’re seeing 120 and that’s cool right but what we would really like is if the 120 looked like uh you know 120 dollars instead of 120 just some number so I want to put a dollar sign here so there’s a few ways we could do this okay so one way I could do is I could put a dollar sign here and then I could say plus and this is called string concatenation and it’s giving us an error so let’s see what the error is so error says error can only concatenate string to string so it’s basically saying this total here is of type float because it’s a decimal right it’s 120.0 so that means this is a decimal which means that this is a float in coding terms okay so it’s basically saying hey you’re trying to combine a string this string over here with a float Tomatoes Tomatoes tomatoes and oranges you know what can’t compare oranges and apples together what’s going on here what we could do is we could turn this into a string the total into a string so I could just call Str and wrap it in this function Str you’ll learn more about functions later but for now all you need to know is if I take a number like a 100 and I call STR on it you’ll see they’ll put these quotes around it which means that it successfully has turned it from an integer type to a string type likewise if I take a hundred like this and I call string on that it will also turn it into a string type as a float okay so this will work so let’s try this and there we go that looks a lot better right a hundred and twenty dollars boom we’re getting our answer and it looks clean okay so that’s that’s kind of how we want to do this when I said we’re gonna make a baby app well let’s make it interactive and then we will actually be done with this little project and then we’ll keep moving on so right now it’s hard we’ve hard-coded data food amount is 100. the tip percentage is also hard coded but what if you were at a restaurant and you actually took this file and you tried to run it and you tried to get it to help you well it won’t really help you with any tip calculation or or really how much the total total bill is because it’s just running it for a hundred dollars and twenty percent tip what if in reality you were sitting at a restaurant right now and maybe your food cost twenty four dollars and maybe your tip was 10 what you will have to come to your app and then change the hard-coded numbers that’s not how users are going to use this right it’s going to be very frustrating experience if you do that so we’re going to change this to an input so we’re going to say enter food amount okay and we’re going to say dollars and then here I’m not going to change this 100 but I’m gonna change this part okay so we’re going to make this a dynamic part we’re going to say enter your tip percentage so the user is going to put in their tip percentage and let’s say the user says 20 we’ll just put a 20 here and then 20 divided by 100 will give you a 20 tip or 0.2 all right and uh the next thing that we want to do is remember this is going to be given to us as what data type I’ll give you five seconds five four three two one food amount is going to be stored as a string but what do we want it to be stored as because if we start as a string it’s not going to do the right things that we wanted to do right you’re you’re going to try to add a string to a string and you’re going to do 10 plus 20 and you’re gonna get 10 20 instead of 30. so how do we force this to become in integer okay or a number so for example the user is gonna put in some number like hey the food costs fifty dollars so it’ll be a string and then what we want to do is turn it from a string data type because it’s a string data type two an actual 50 either an integer or a float let’s go ahead I think would float and the reason is because the user could say it’s fifty dollars and fifty cents right so that’s why I think a float would actually be a better representative of this and so we will say float and wrap this whole thing in float and then we’ll wrap this here and Float but not the whole thing just this part right here so now boom and let’s run this so let’s say the food amount is a hundred dollars and let’s say our tip percentage is 20 and we get a hundred and twenty dollars boom oh look at that that is working the only thing I don’t like about this right now is a percentage and the 20 look too close to each other so I’m gonna put a colon here I’m gonna put a space here and I’m gonna put a percent sign percent sign here to represent that we’re asking for a percentage let’s run this again food amount I will say it’s fifty dollars tip percentage let’s say it’s 20 and let’s give this a try and boom look at that it tells us that is sixty dollars total that we owe uh now if we wanted to make it prettier I’ll show you a little sneak peek into something called string formatting and there’s something really cool in Python so what if I wanted to let the user know which one is the tip and which one is the total and which one is the tip percentage and all of that all here in a nice interface that shows everything up front Okay so for example let’s do that right so it should say total is 60 60 or whatever right it should say tip amount is let’s say twenty dollars or whatever so if you want to do that type of string formatting let’s do it like this so I’m going to say print and I’m going to say f and then we’re just going to go ahead and write out what we’re looking for so I’m going to say tip amount and I’ll put a colon and then I’ll put these curly braces means I can now write python code inside of this so the python code all right is tip amount now when I run this and let’s say tip amount tip percentage is 10 so 10 percent of ten dollars tip amount is one and now let’s go ahead and put a dollar sign here so it actually looks like one dollar a hundred to percentage is ten you can see tip amount is ten dollars beautiful now let’s go ahead and copy paste this again and instead of tip amount now what I want to say is I want to say food amount okay and we could put that above this and let’s go ahead and say food amount and let’s run this and let’s see what we get so we’ll say food amount is a hundred dollars tip percentage is 10 so you can see food amount is ten dollars tip amount is ten dollars or a hundred dollars and ten dollars okay and then the left for the last one let’s do the same thing except I’m gonna say total amount and what are you gonna say in here we’re gonna replace this with the five four three two one we’re gonna replace this with the total okay now let’s run this a hundred dollars and ten percent boom look at that and we don’t need this final line anymore and if you want to make it like a little bit cooler whatever you could add emojis if you if you can download an emoji app I have rocket on Mac and that’s really easy I just press colon and then I press right and then it allows me to do this it’ll look you can make this look really nice so let’s put total amount food amount can be it chicken food so let’s run this now 110 and we can see the total amount is a hundred and ten dollars and it looks you know a little pretty now if we want to make this look even nicer we can actually go ahead and just add these at the top and then we could take this and add this at the bottom now if I run this we get a hundred ten percent tip and you can see food amount is hundred dollars to of amount is ten dollars and total amount is a hundred and ten dollars maybe I wanna add a new line in between total amount and tip amount so what you could do is you could print and go backslash n this encoding means a new line so it’s going to just put a new line over here uh tip amount let’s just put a scale for tip I guess so at least everything has an emoji and a little cleaner food amount is a hundred dollars to percentage is ten percent there we go food amount tip amount and then the overall total amount that looks very clean if we want to add some new lines here as well if you want to add multiple new lines we could do this so let’s run this again there we go we’re getting our food percentage and total amount so this string formatting stuff is very very useful it’s really helpful and it can make your thing look pretty but more importantly it is very easy to write because you’re just writing out everything you want to write and then right when you want to write python code you just put it in curly braces and you’re good to go okay it’s as simple as that so there you go we have made our first little app with Pythons if you got to hear congratulations you’re doing great with that said we’re gonna keep going and we’re gonna keep learning I love your beautiful face I’ll see you in a second all right now the next thing is going to be a pretty important thing let’s talk about Boolean okay this is another data type it’s called Boolean this is very very important it basically anytime you hear if then else all that logic which is really programming at its heart because most things that you write with programming pretty much almost always have some kind of if then statements going on okay very important this is the heart of Boolean so like for example let’s just talk about the real life where are where do we use this well let me ask you this if you have zero dollars in your bank account can you take out a thousand dollars you can’t why not the reason why you can’t take out your money is literally there’s a piece of code that says if withdrawal amount is greater than and the balance don’t allow withdrawal there’s a there’s like a code line written there okay that literally says that okay so it reads like English too and in Python it’ll probably look like something like this okay it’d be like if the withdrawal amount is greater than the balance don’t allow withdrawal or we might just say return or just like don’t do anything but if it’s not then allow withdrawal okay so let’s say we go here else I can say allow withdrawal so I hope that this conversationally just makes sense to you like if I’m just chatting with you like a buddy right or we’re at a bar and I’m just explaining to see like this I hope it makes sense before we get formal and I start trying to explain to you in like at a very specific level that’s what that means hey the withdrawal amount is greater than the balance don’t allow it otherwise allow it okay so that’s a simple example of where it happens well there’s probably a lot of other places it happens as well like if the user hits accelerator accelerate the car if the use user hits break break the car imagine if you did if the if you hit accelerator and it break your car or if you hit the brake in an accelerated your car that would be catastrophic there’d be so many accidents and there would be like so many debts right and why would that happen that would happen because somebody wrote somebody just programmed it incorrectly so that’s why this if else stuff is very awesome and it’s also very important because it’s at the heart of what we do as programmers so let’s make like a little baby app so we’ll make like a baby weather app and um let’s comment out this entire thing with with the tip calculator we don’t really need to worry about that right now so for example if I run my code I should see nothing right now it should be empty and basically what this should be say is hey what’s the weather like so I’ll give you a premise for this app it should tell you what to have if it’s raining outside it should tell you you know grab an umbrella otherwise grab your sunglasses so that’s pretty much what the app should do it should be a very simple one and that’s what I should say Okay so let’s say we have weather here and the weather will say is rain okay so if it’s rain then it should say grounded umbrella so here’s how we’re gonna write our if statement so we’re going to say if weather is rain if this is true print umbrella else otherwise print sunglasses okay I’ll use emojis if you don’t have emojis feel free to just write this stuff out and let’s go ahead and print this and let me ask you a question before we even run this and before you ask me why are we putting two equal signs and what is this colon about and what’s happening here if you just followed along with me and you just did what I did and you hit enter here and you just wrote this and your indentation and the whole thing looks exactly like mine and you hit print or run what do you think is going to print with your gut feeling before we even run this for the first time do you think it’s going to say rain or do you think it’s gonna say sunglasses it’s checking for of the weather is rain and we have set the weather to rain so what would be your guess I’ll give you five seconds five four three two one okay it would be the umbrella you see that right there now why are we getting now why are we getting an umbrella so let’s break it down at a further level and let’s talk about Boolean which we didn’t talk about yet so booleans are very simple they’re either true or false that’s the data type that’s all it contains it’s either true or false so a Boolean can either be true or false that’s it those are the only two values it could have so for example if I said hey it’s five greater than 10 and we’ll also talk about comparison operators I’ll just make a little note of it comparison operators and notice that I got back false okay because 5 is not greater than ten what if I said 5 is equal to 5. it says true now the reason why we put two equals lines two equal signs is a comparison operator because if you did one equal sign that is an assignment operator so if I did five equals five it’s going to give an error it says you can’t assign the value 5 to a literal to a five you can assign five to you can say a equals five you could do that but you can’t do five equals five that doesn’t make sense but you could check if 5 is the same as five you can compare these two values and I will tell you if they are the same or they are not in this case they are so I’m going to say true is 10 the same as 5. what do you think is going to say it’s going to say false okay it’s 5 less than 25 yes it is it’s 5 less than or equal to 25 yes it is so let’s write some of these comparison operators so we have less than or equal less than we have greater than we have less than or equal to we have greater than or equal to so these are all the different assign operators that we have inside of python so now going back to this over here what’s Happening Here is if I say if true so if you have an if statement where you have a true and we’ve just hard coded it to be true you will always get back the if statement value not the else value okay so if I make this a true right there watch what’s going to happen when I run this I’m going to get back an umbrella but if I make this it falls what do you think I’m gonna get do you think I’m still going to get back an umbrella or do you think I’m gonna get back sunglasses very good if you said sunglasses let’s run this and I get back sunglasses because if this is false the code says do not run this line of code run the else line of code when I say if weather is rain what I’m really saying is if this variable here has the value rain in it check if that is the case if that’s the case take this whole thing and then it’ll turn it to True okay so literally it plugs in rain here and then it goes is rain equal equal to rain and then it goes yes it is true and since that evaluates are true it ends up running this line of code now watch what happens if I said Sunny what do you think is going to happen do you think it’s going to print out this umbrella right over here or do you think is going to print out the sunglasses I’ll give you five seconds five four three two one let’s run it and boom look at that I got my sunglasses so that’s how that works now in order to make this kind of an interactive app let’s instead of hard-coded weather data let’s take input from the user so at this point you should know how to do this and you should be able to do this part okay so I’ll give you five seconds pause the video and do it on your own five four three two one all right let’s do it together if you got it great if you did it that’s okay we’ll do it together no problem so I’m gonna go ahead and I’m gonna say input because that’s how I take user data I don’t need to Typecast it to anything because it’s already a string so input is fine so I’m going to say input and I’m going to say how is the weather question mark all right and we’re going to just do put a space there let’s go there how is the weather and I will say it is sunny and I will get back this Emoji right here and let’s run it again and now it’ll say rain and it’ll give me back the rain Emoji right there okay so that tells me that my app is working it can already take in user data and figure out what to do with it it can ask the user the weather and once the user tells it how the weather is it can print out rain or umbrella so what I’d recommend is you play around with this okay and try this so let’s chain this up a little bit more so let’s take this if else condition a little bit farther okay so now let’s do something interesting right so we’re gonna go ahead and say else if so we’re going to add elseif whether is cloudy then you know uh where a hat or something I don’t know why you would wear a hat but maybe a hoodie so do we have a hoodie or a jacket we don’t have a jacket let’s just put clouds okay just print out a cloud that’s it that’s perfect else give me these sunglasses so now watch what happens when I run this if I say rain it’ll give me the umbrella if I run this again if I choose cloudy it’s actually going to give me the cloud if it’s neither of those so if it’s neither of those I’ll just say ha or I’ll say thunderstorm it’ll give me sunglasses so I’ll give you a little challenge right now add another else if condition and you could chain as many else of conditions as you want and one thing I need to tell you about python is this if statement how you write it you put this colon you hit enter this indentation in Python is important if you don’t have this indentation this code will break it will not work so python uses white space and it’s really important as a part of it to make sure that you indent things correctly if your code ever breaks is most likely all you’ll have to do is go back and hit enter like this and you should be good so go ahead write another else if condition which should account for thunderstorm so if it’s thunderstorming it should print out a thunder icon or something so see if you could try that so I’ll give you five seconds to pause it and give it a try five four three two one all right let’s go ahead and do this together so the last thing that I’m going to say here is else if weather and we’re going to say it’s thunderstorm okay and then then we’ll say print and let’s see if there’s thunderstorm there is not let’s say lightning okay there we go we’ll say lightning with rain and thunderstorm okay so there we go and now let’s run it we’ll say whether it’s thunderstorm and boom we’re getting this icon right here so we could chain these and we could make this multiple so let’s do for example another exercise what if I wanna check out my you know grades so I’m gonna go ahead comment this out and I’m going to check out my grade so you know or score so let’s say I got a score of in this case I’ll give you some notes so let’s say anything you know anything that’s 90 anything that’s not greater than a 90 this will be considered an A anything greater than an 80 will be considered a b anything greater than a 70 will be considered a c anything greater than a 60 will be considered a d and then anything less than 60 will be considered an F so anything less than 60 will be an F so see if you could try this right now and create a if else if chained that could represent this okay so if I give you a score that’s above a 90 it should say an a it print an a if I give you a score of between 80 and 90 should be a b uh C Etc so see if you could give that a try I’ll give you five seconds to pause the video and then you should give this a full try on your own five four three two one all right I hope you pause it and you try to give it a try but regardless of what however it went let’s go ahead and do this together so I’m going to create a score so we’ll just start with some kind of score we’re going to say if score is greater than 90 print a right and then we’re going to say else if score is greater than 80 print B okay and actually let me give you a better parameter because if it’s 80 that’s also a b okay so I want if the score is 80 it’s also a b this would only account for 81 something higher than an 80. so what we’re going to do is score is greater than or equal to 80 greater than or equal to 90 greater than or equal to greater than or equal to okay and then this is actually less than 69 so this is fine because we want you know 59.9 to be an f and then I’m going to say else if score is so we’re going to to say greater than or equal to and we can say else if score is less than 60 print F let’s run this and you can see that it says I got an F now let’s change the score to be an 80. let’s run this you could see it gave me a b and then let’s change this over here to be in 90 and boom you can see I got an A now what if it was a 95 let’s run this and it’s still an a what if it was an 87 let’s run this and you could see that I got a B okay so with that logic you know we can fill the rest of it out if you weren’t able to do it now you should definitely be able to do it so let’s go ahead and we’re going to say else if score is greater than or equal to 70 I’m going to say print C and then we’re going to say else if score is greater than or equal to 60 let’s say print D and then what we could also also say is we could say else so I can actually remove all of this and I can be pretty clever here and say print F because that’s the only other case left because we have a case that’s higher than 90 and then we have all the way down to a case that’s greater than 60. so the only time an else would happen is if it was something less than 60. that’s the only time right because otherwise it would be caught by this guy right here okay because that would mean it’s higher than 90. so anything less than a 60 is an F so we could just be clean and write it like this now let’s go ahead and try a few different numbers so 87 and I got a B let’s try 93 what do I get a 103 maybe I I scored so well I got above 100 for this yeah so that’s pretty good let’s go ahead and try you know 67 and that’s a d that’s very good let’s try maybe 53 53 what what do I get here boom and I got an F okay so not good I failed the class okay this was my score in math every math class that was my score so this is not too far from the truth anyway so I hope you got some good practice with how if else if else conditions work and what Boolean really means because everything here at the end of the day is very simple this is either the word true or it’s either the word either the type false okay and this is a very specific data type true or false and how I can prove to you that these are data types this is Boolean data type so for example if I uh call type on the number five it’ll tell me that is of class integer if I call type on 5.2 it will tell me that it’s of type float if I call it on type on hello it’ll tell me it’s a string so see if you can call type on false and what do you get and false is like this with a capital f okay so give that a try five four three two one what did you discover right let’s look at it false and it’s of class Bool and if I go type true it’s of class Bool so booleans those are the only two Boolean values that exist true or false and now you know if we want to take it even further we can actually chain values together as well okay so for example let’s say we wanted to either have we wanted to show either a passing grade or a failing grade okay so if we wanted to just do that and we didn’t have this A B C D minus stuff okay or a b c d uh F stuff we just had you passed and so if you pass it just said P okay or maybe just pass and if you failed it just said failed that’s it so how could I do that well I wanted to be where if you got you know within this range let’s just say this is a passing range and anything less than that just doesn’t work so I could actually say that I could say if score and let’s say I set the score here 80 for now if if score is greater than or equal to 60 and to make this example even better let’s say you have pass failing and then you have like super pass meaning the the student got let’s say uh you know superpass is when they get greater than a hundred okay so they just really crushed it let’s say I start with the passing grade condition so the passing grade condition I’ll say if the score is greater than or equal to 60 and score is less than a hundred if it’s or less than or equal to a hundred because we’re defining superpass to be something that’s greater than 100 okay so 101 would be like you pass Super past it so we could actually even say this we can actually write multiple booleans here so for example I could say true and false and I get false so when you write with the Boolean gate logic or Boolean logic both of these statements have to be true whenever you have an and both statements have to be true or all statements have to be true in order for you to get a true so true and true and in other words like 5 is greater than six okay five five uh let’s say 10 is greater than five and ten is greater than six since both of these statements are true you’re going to get back true but if you said 10 is greater than 5 and 10 is less than 6 since this statement is a false here this whole thing is going to evaluate to false and not work which means that the only way that the code is going to run is if it’s if the score is in between these numbers if it’s greater than 100 or it’s less than a 60 it will break and it won’t work and we’re just going to go ahead here and say passing grade so let’s go ahead and run this and you can see that I’m getting passing grade now another cool thing and kind of important thing I want to show you in Python is there’s another way to write it which is really nice and it’s called pythonic okay so very pythonic way of writing it it just means you’re writing very clean code that is pretty language specific can’t really write in JavaScript and also this code is very stylistic in Python okay it’s a nice way of writing it so I’ll give you an example so there’s multiple ways of writing the same statement another way to write it is you could say if score so since we’re really saying score in between these two numbers there’s a nicer way to write it you could actually say and this looks nicer and reads nicer in my opinion and you don’t need to write a bunch of and statements and you could say print pass and grade and to prove that it’s working I’ll comment out this part and run you’ll still see that it says passing grade okay I’ll run it again just so you can see it and I’ll choose 59 as a score and it won’t say anything and then I’ll choose 103 and it won’t say anything it’s detecting that it’s out of that range and right when I put it in that range it will show up and say passing grade this is really cool this allows you to capture in between array change okay and this is the same thing as writing this guy over here same exact thing so you could chain your booleans with an and statement and then the last thing I want to show you is you could also change your booleans with an or statement like this okay so you if you have a true or false you’re going to get back a true so an or statement only one of them has to be true so for example it’s like hey what time did you eat today well I either ate at 3 P.M or I ate at 10 pm I’m not technically wrong okay I am telling you the truth one of them is correct so here it’s giving me a true so if I have 3 is greater than 5 or 5 is less than three okay or we can say five is greater than three it’s going to give me a true because here 5 is greater than 3. so since this is true and this is false it’s going to Value to true so all that confusion aside okay I can also write ORS so I can say if score is less than 60 or score is greater than 100 I can say you either failed or you passed or you super passed okay so now let’s choose the score and make the score 103 and let’s see what it says it says you either failed or you super pass yep and if I choose 59 you either failed or you super passed that’s correct and if I choose a 63 it’ll it’ll do this passing grade okay so you can see that this or statement works and it catches two things it’s catching two you know it’s caching below the boundary and then above the boundary so this or is super helpful too and I just want you to be aware of that or statement that it that it exists okay so this is a range there’s an or way of doing this okay and then there’s an and way of doing this I hope this was like not too confusing but I wanted to show you all these things because as we start to build up and add more layers of complexity it’s really going to help you because these tools are going to come in handy and then you’ll be able to write more pythonic code so this here I’m sorry if I didn’t explain this correctly but this here is a very pythonic way of writing it this over here is like a regular way of writing it this is how somebody who’s maybe coming from another programming language would write it somebody who’s been in the python ecosystem knows that they can write it like this and this looks way way cleaner just these are the three examples that you should look at and these will be helpful so if you want to take note of this you know this is available for you the whole time this will be available to you the entire time and on the left hand side I have quasi.py right that’s the file I am going to be coding in okay quasi.py just to be very clear main.py is the file you’re going to be coding in and if you don’t want any of my lines to run just go ahead and comment this out by the time you’re watching this this might be they’re commented out or deleted already so I wouldn’t worry about it too much but my file is quasi.py okay you’re going to write in main.py and follow along there so there you go just wanted to give you a little crash course on if else conditions because I think this will be valuable for you with that said let’s keep on rocking and rolling all right you are doing great so far now what we’re gonna do is we’re just gonna comment out everything else that we don’t need so make sure it’s commented out and if at any time you look over here and you see things changed don’t worry because I’m gonna keep adding things and adding more exercises and projects for you guys inside of this course all you need to worry about is keep coding inside of main okay main.py and since I’m cozy I’m just going to be coding in kazi.py so at any time you can go into cosita py you should have all the notes and everything because that’s where I’m going to be doing all of my work now with that said let’s keep rocking and rolling let’s work on understanding and learning how functions work inside of python so in order to define a function I will do something along the lines of death okay d e f which is Define a function and then say I’m gonna call it say my name like this and first we’ll just call this function and then we’ll see what happens okay and this is just going to say hi rafae qazi it doesn’t need to even say hi I’ll just be like this now if I run this piece of code you’ll notice that nothing actually happened why am I not actually seeing anything in this case now the reason why I’m not seeing anything is it’s just like I defining a variable and if you don’t call that variable it doesn’t really do anything so for example say I said name is Rafi qazi well now I need to call that variable right if I need to do this so let’s just say here name is John how would I call this here if I run this it doesn’t actually output John to the screen if I want to call it I do print name and when I run this it will actually say John but the difference between a function is when you call it it’s very similar to a variable but when you call a function you have to put these open close parentheses in front of it okay so it’s very similar to a variable name except you got to do this and once you do this it runs everything that’s inside of here so I’m going to hit run and you can see it says say my name okay we’re going to get rid of this John thing here now what if I wanted it to say more names okay so let’s say John and then let’s go here and let’s say Cara and let’s say here and say sam and if I run this it’ll run all of those lines of code but if I comment this part out right here it will say nothing so whenever I uncomment this this entire chunk of code is going to run that’s the cool thing about a function it’ll allows us to run big chunks of code at one time now I’m going to show you something else so what I want to do here is I want to make another function and this one will be more Dynamic so we’ll call it say my name two okay so we’re going to make our function a little bit Dynamic so it’s going to basically take in a user input I’m going to say print rafikaze okay and let’s just call our function print say my name to and then make sure we call it open close parens and I don’t have to actually say print here because I’m already printing from the function itself so there we go I got Rafa quasi in this case what I want to actually say is I want this part to be dynamic so I want this to be name dynamically but it says this is not defined so what I’m going to do is this function is going to take in an argument and that argument is going to be name so it’s going to take in a name and whatever name the user gives it print is going to put that name right here and then call the print function and then it’s going to print it out onto the screen all right so I’m going to go ahead and say say my name and I will say Cosi and let’s run it and boom you’re going to get cozy I can say Rafe and boom it’s going to run it so you can see how uh it can run your full name like that now we’re gonna step it up a notch so I’m gonna comment this out because I don’t need to run this function right now so we’re going to comment this out I’m going to make a greeting function and this will take in your name okay so we can do some string formatting here so I’m going to say hey and then we’ll say name okay or exclamation mark and let’s go ahead and print greeting now or not print but let’s just say greeting and we will say name is qazi and let’s run this and you can see that it says hey qazi and I can even put a little Emoji here to make it more fun and you could see it says Hey Kazi now let’s put exclamation mark at the end here that’ll look nicer I think now what if we want to make not only the name Dynamic but also the greeting Dynamic meaning that we should be able to say the uh the person’s name is qazi so greed Causey and greet them with a specific greeting so we can actually do that thing okay so it’ll take in a greet and then it’ll take in the name so now I can actually go ahead and give it two arguments okay and the first argument I’ll give it is aloha and the second argument is the name so I’m giving this function greeting two arguments that’s how we say it and then we want to make this dynamic as well so what should I replace this hay with so I get greet over here I’ll give you five seconds to think about it five four three two one all right so we’re gonna go ahead and change this with greet so now greeting is dynamic and the name is dynamic and let’s go ahead and run this and we get Aloha qazi okay so this is an example of multiple arguments okay inside of a function this has one argument inside a function uh this here an example of no arguments so these are some simple examples of of functions okay and if we want to add some documentation to our function so they’re more readable we can actually do that as well so I’ll show you something it’s called triple quotes inside of these triple quotes I can say greeting ticks and two arguments green and name and it greets the user and then I can actually even give an example of how to call this function so I can say grading and then I’ll take in Aloha and then a name like this and then actually actually even show what that will output so I can show that it will actually output this string right here so that way if a user is about to use your function greeting they can look at it and they can go oh man I really understand what this code does it takes in two arguments greet a name and it creates the user this is how I would call it so this is a great example and this is what I’m expecting it will return turn so you don’t always have to do this but documentation is an important and valuable skill that if you actually learn now you’ll be way more advanced ahead of so many people because people shy away from it and run from it and if you just spend like three minutes or two minutes often times you could document most things very quickly and it’ll help and save so much time and really help out other your fellow developers you know because you’re going to get a job as a developer one day and so if you can write code that’s very helpful like this your chances of getting a job will be so much higher because everybody wants a developer that is actually helpful to other people and isn’t just some nerd that sits in a basement writes code and then when things break just doesn’t communicate and nobody else knows how to handle that person’s code because everybody hates that person so this will actually help you with that okay so our greeting function is looking good to me we can go ahead and comment this out for now now one other concept I want to show you is the concept of default arguments so I can say what if the user doesn’t give a greeting what if that happens you know they don’t put in anything for this so there is something you could do for default arguments it’s a it’s a pretty nice trick and I’ll show you how we can do this so what we’re going to do is the argument we know we’re going to need a default argument for let’s go ahead and put that at the end so I’m going to say comma and we’ll do greet just like this okay so name comma greet and the greeting that we’ll actually give here is Aloha let’s say aloha by default if you don’t put in anything for greet it’ll say aloha by default so let’s go ahead and give this a try I’m going to uncomment out this greeting and I’m going to hit command enter to run it and look at that I got back Aloha qazi because by default it assumed we had Aloha happening okay now if I went ahead and gave a specific greeting and I said hello and we ran this you’ll see now it actually says hello Aussie so it’s a very nice way of having default arguments like this because it makes your code a lot more user friendly because if a developer it you know forgets to put this or a user doesn’t put a certain piece of information you can still let them go to the next page you know so an example of that would be if you made a fitness app and you asked the user for their body weights you know their body mass index their height Etc on a page but the user like didn’t put in any of the information they should still be able to hit next and go to another page where maybe they can log their calories but if you didn’t have these default arguments it would like force them to just stay on that page and just be like no you have to give me your body weight your body mass index your height and your gender all of it otherwise I can’t let you go forward so default arguments are really really nice now one other really cool thing I want to show you is something called named arguments okay so for example if if I’m looking at this you know this gets pretty confusing because if I’m looking at this I don’t know which place is for argument and I have no idea which place is for greeting like where what goes where I have no idea where does my where does my grading go and where does my name go right so is this the name and is this the greeting or is it the other way around so right now we’re doing something called positional arguments so we know that since name is in the first position or the zeroth position I’m going to put my name in this earth position and since the greetings in the first position I’m gonna not put this but I’m gonna put a greeting like high okay so we know that but this is a concept known as positional arguments but what we actually care about are named arguments because that’s a really nice way of doing it you don’t always have to do this but this will make your life a lot easier in the future what I’m going to say instead is I’m going to do a greeting and I’m gonna say name is qazi comma great is high let’s just say so now it’s very nice because I can actually even change the order of this right so I can actually order it this way because it’s greeting so now it doesn’t really matter where I call it let’s run this and I should see high qazi look at that so the order of these actually does not matter anymore because it’s a named argument so I really like named Arguments for functions you don’t always have to use them but it’s very handy and then when you’re calling this function and you’re looking at it later and you haven’t looked at your definition of a function in a long time it’ll be very clear what the heck is happening for you in the future okay so for now we’ll just keep things simple and we’ll just call our functions using positional arguments all right so positional arguments okay and then we’ll say name and then greet but I’m going to comment this out for now because I don’t need this all right let’s go ahead and now we’ll create another function so with functions because there are such a life blood of coding there’s pretty much no app that you’ll write that won’t have functions very unlikely unless you’re writing some simple Python scripts if you master functions you’re going to be a great coder so let’s do a lot of practices with functions so let’s go ahead and create a function it’ll be called sum and it’ll take in two arguments two numbers and what this function will do okay at a very simple level sum if I call it with like one and two it should return three okay so takes two integers returns there’s some that’s it if you want to take this documentation and you want to add it in the body you can do that as well and so now it makes it very clear what you’re about to write as your code okay that’s that’s what I like about having some documentation like this what it’ll look like so what I want to do is for this function I just want to go ahead and say return a plus b okay so if I’m taking in two numbers a a b then I just want to return their sum so let’s go ahead here let’s put this out here for now okay and let’s just take a look at this the reason I’m showing it to you like this is because I want you to start getting used to seeing functions if you see this in there that might make it bloated it might confuse you for now but in future when you’re writing your own functions I highly recommend you do the documentation within it like this so boom here we got our sum function and then when we return we just return a plus b so this is a function it returns these two numbers now let’s go ahead and call our sum function and I’ll say sum 1 comma two here’s what’s going to happen watch what happens when I run it nothing got output to the console because this is a return and not a print so for this function and most functions you’re going to return the statement because once you return it you could actually do something with it so for example I’ll show you what I mean so I could print this if I want to and now I’ll actually get a printed answer three but what’s cool about return is I could actually create a variable called num and now the result of sum is stored inside of it and now I could go ahead and print num so I could use this value later that’s returned so you could see num was printed out and is three however if I replace this with print you’ll notice we’ll have a problem here if I replace it with print it prints it out right but then num it stores the value none inside of that like there’s nothing because when I call sum here says something with print and then nothing gets stored here so in other words it’s broken and not useful outside of the function okay so that’s why return is a good way to do it all right so we’re going to go ahead and I’m just going to say print for now so this function is working now I want to show you something so what you could actually do is you could say from Main import this function sum that you just wrote so we can play with it in the Shell now since I’m not in the main file like you might be I’m in this file called quasi.py I’m going to go ahead and I’m going to replace main with cozy and I’m going to say quasi import sum so now I actually have access to the function sum inside of my command line which is really nice and now I can go ahead and use this so I can say sum 1 comma two and Watch What Happens boom I get back three let’s try sum again and I’ll say five comma 20 27 boom I get 32 so you could see this function sum works every single time and I like this interactive terminal because you could play with this right you could do a lot of things with it like for example what if I do sum of 5 comma 27 plus 10 well this will evaluate to 32 and then this will add a 10 to that and then these will get added together and boom you get 42. this allows you to play test some ideas and do everything very very quickly so that’s why I like playing in the console there’s a lot of playing we can do here and then here you kind of write your clean you know finished pieces of code you know and then because you wanna you you can run them once whereas on the console like you can just keep hitting enter keep running it and it’s easier alright so there you go that’s how we do our sum function now what I want to do is I’m going to comment this out and I want to take a step backwards and so this way we can get a little bit more practice I’m going to take a step backwards and I want to convert some of the previous stuff we had written and turn that into functions so for example this food calculator app that we had written earlier I’m going to copy this entire thing command C or control C on Windows and I’m gonna come over here I’m gonna paste it and my task is I need to convert this into functions all right so that’s what I need to do so when I’m looking at this the first thing I want to do is what so let me ask you this whenever you’re about to create a function what three letters do you write I’ll give you five seconds to figure that out five four three two one yes you write the word def okay so we’re going to say def Define this allows us to define a function now what’s the name of this function going to be well what will this entire piece of code do it will calculate the food total so wouldn’t that be a good name for it calculate food total perfect so we’re done with the name now we need to know what are the inputs that this function takes I can actually see these inputs right over here it takes in one input right here and second input right here so these over here are going to be the input that it needs these two so see if you can try to make them the arguments for this function by yourself use this as a hint I’ll give you three seconds three two one you could pause the video you could try this by yourself as well so you get some practice so food amount we’re going to take in his argument and tip percentage we’re going to take in as an argument so those are two things this function is going to take as an argument and then what we’re going to do because those need to be given we can’t just calculate that right there’s no way so we need to know hey what the tip is at this restaurant or what the food total is we have to wait for our server to give us that now we need to calculate the tip the actual tip percentage and then we need to calculate the tip amount so once I have the tip percentage which is right here I can turn it into let’s just call it a variable like tip percent okay so we take the tip and we divide it by 100 or we could even get rid of this because we know that this is our hip right this is this thing right here so if we get 20 then this is going to be 0.2 which is going to reflect 20 so this is our tip percentage now we need to get our tip amount while tip amount is going to be food amount multiplied by the tip percentage so we take this and we multiply this okay you could put this in parentheses if you want and this will be tip amount and let’s go ahead and just print tip amount for now and usually printing when you’re creating a function is often used for debugging seeing where your code is at seeing if things are working as you expect them to or not so now we’re just going to go ahead and call this function so I’m going to say calculate food total I’m going to give it a hundred dollars and a 10 tip and let’s see if it calculates the tip amount correctly so the tip amount should be ten dollars let’s go ahead and run this and we did get back ten dollars now the last thing we’re missing is the total amount well that’s going to be whatever the food amount is plus the tip amount that’s what that is and since we’re using the word amount everywhere we can just replace it also everywhere and this this case and just simplify our life and then the code will look simpler total is Food Plus tip okay it looks nicer and easier now we need to make sure that we actually return this because you see it’ll tell you hey you’re not using this okay so we’re going to return this in a second but first let’s go ahead and just print it out and let’s see if we get back what we’re expecting and you can see that I’m getting back total and it says 110 right there what I can do is I can just return total because that’s all this function needs to do and we can delete these other lines of code I click that button over here because it will style the code and make it look pretty alright there we go let’s run this and we’re getting back nothing because we’re just returning and we’re not printing so when you return what happens is this will evaluate to that number so if I did 100 comma tenant this will give me back let’s say a hundred and ten dollars is my total amount and then print will actually go ahead print it out and show it onto the screen so let’s go ahead and give this a try and boom lo and behold you can see that I got back a hundred and ten so calculate food total is now working exactly like we expected okay so there we go we’ve taken that blob of code and turned it into a nice function and now if we you know commented out the nice thing is that our code will still be fine we just have to comment it out and then this entire block won’t run anymore so we could just kind of ignore this block but it’s nice to be able to calculate food tip like this now you can use named arguments because you’re like hey which one is the tip and which one is the food it’s kind of confusing right so what we could do is we could say food is you know 100 and then we could say tip percentage is 20 all right and now we could call it with the named argument and of course we’ll have to print that result and you could see that I get back 120 and then I think we did something else with food if I’m not mistaken we made food look really nice so we could certainly do that here as well we could make food look really nice just copy that piece of code and if you want it to kind of have that like prettiness action going on which is not totally necessary in this case I’ll uncomment this and then just make sure you indent everything so I’m going to hit delete and hit enter okay and sometimes this happens with python which is really annoying so delete delete bring it up to a new line delete bring it up to a new line delete delete hit enter delete enter delete enter delete enter and yeah this this I’m not gonna lie is a little bit annoying because it needs to be indented in a specific way and when it’s not indented in that specific way it’ll give you errors and you will be like what did I do I didn’t do anything wrong they’ll just be like nope you your mistake was just being born in this world all right now the error we’re getting is it says food amount is not defined so that’s because we called it food and we call it a tip rename it correctly and there we go food amount so it’s printing out exactly like how we wanted it to now let’s get a little bit more practice in with functions so I want you to give this a try yourself see if you could turn this weather thing into a function so I’ll give you a hint so first let’s comment this out and so we’ll say Define and I guess we’ll call this function weather to Emoji because you give it a weather and then it tells you what emoji that is represented okay so we’re going to say weather to Emoji okay so this is what the function is going to be called I’ll leave the body empty for now because that’s what you’re going to be writing and then for documentation we’ll we’ll say this okay we’ll say whether to move OG will and they’ll take in some arguments I’ll leave this blank too but you have to kind of fill that part in and let’s write it like pythonic like this so we’ll say Emoji takes in one argument as a string okay so we know that whatever the user gives us is going to be a string and let’s just say we’re expecting expected uh values from user is just going to be like rain thunderstorm all right so these are the expected values and our expected inputs okay so they’re going to be either of them and then it’s going to be like this so if we call weather to Emoji function with let’s say rain well we should actually get back is that umbrella okay if we call this function with not rain let’s say thunderstorm we should get back cloud with thunderstorm just like this and then if we call this with the weather is sunny outside then it should just give us sunglasses and if you want to be super accurate this is it returns a string so this is like me being super accurate and you can see that this documentation is already probably very very helpful for you because you could see okay this is exactly how I’m going to be calling this function this is exactly what it should be returning okay I think I can understand that so you where I wrote pass you have to replace that with you know the body of the function which means you have to replace that with the correct code so that when I call whether to pass function and I give it an argument like rain it does indeed return this and when I give it the arm when I say thunderstorm it does return this so you have to figure that part out so I’m going to give you about five seconds here to pause and then try to do this on your own and then we’ll try to do this together five four three two one all right let’s do this together so if you got to this part very good and if you give it a try and you got it beautiful job if you didn’t it’s okay we’re gonna do this together so now what I want to do here is I want to say what I had initially said right so I can I can I don’t need to go through the entire logic again I could copy paste I could copy paste this and let’s uncomment this and then let’s just make sure that everything is indented properly so I’ll I’ll just bring it back up and hit enter so because there’s something called Scopes which is really important to understand in programming so I’ll just take these comments away for a second just so you could see it so this is a function so everything underneath this function will be indented showing that it’s part of this function right over here then every time we have a if condition anything that’s part of that if condition will be indented like this print statement to show that it’s part of the if statement so this entire thing belongs to this weather Emoji function okay that’s why it’s indented okay this print is indented because part of this if this print is indented because part of this lsif but keep in mind else if is not inside of this if okay so keep that in mind it’s very very important and neither is this else and now if I want to get outside of this function I have to make sure I unindent all the way out and then write something so you know I am outside right now but if I wrote something at this level I am still inside weather to Emoji function okay so indentation indentation makes a huge difference in Python all right so just remember that so actually let’s let me go bring my comments back because those were actually quite helpful okay so there’s my logic now for weather weather is actually going to be passed in automatically let me put this outside of here okay and let’s do it like this just so when I’m going through the code with you guys it’s not super confusing because the big that big blob like just makes it look too intimidating so the actual logic of the function here we’re just going to put in weather so it’s going to take in a weather that’s the input and then it’s gonna see if it’s rain it’s gonna do this if it’s cloud it’s going to do this if it’s thunderstorms going to do this otherwise it’s going to print sunny and Bob’s your uncle all right I have to indent and undent all this stuff so I’ll just do that real quick so you can see copy paste copying and pasting usually in Python it’s not a good idea just try to write stuff from scratch and then you won’t have these weird indentation problems because even a difference between a tab for example if I write a tab you see how a tab like is that versus four spaces or two or spaces so a tab looks like it’s really just two spaces but it’s not it’s a whole different character and so if you’re copying pasting copying and pasting code from somewhere where the writer of the code was using tabs and then but you were using spaces in all of your code then that will break and that that copy and paste will not work as well so what I recommend is like put the code next to so you can see it and then write it from scratch and then you’ll be fine all right so whether the Emoji I think it’s pretty much done if weather is rain whatever so let’s go ahead and try calling it now so I’ll say weather to 2 to emoji and I’m going to give it rained and let’s see what happens so let’s run this and boom look at that I got an umbrella let’s go ahead and import this now and here so from qazi you should do from Main and I’ll say import weather to Emoji so I’m going to import this function that I just made and I’ll just say whether to Emoji I hit Tab and tab allows me to auto complete it and I’ll say rain and then I’ll hit up and go back and delete the inside of the parentheses and I’ll say cloudy and I’ll go up and I’ll say thunder storm and you can see that it works every single time and if I went here and I said hailing it gives me sunny so every single time if it’s not any of these three I’ll get back Sunny so this code over here right Maps perfectly to this output right over here and if you actually look in our documentation well our documentation here looks very similar to what we’re seeing out inside of the console okay so this is just multiple ways for you to understand and get back to you know that conclusion but I’m showing you a little bit more advanced stuff than some people might but I think it’s helpful for you and it’ll it’ll really prepare you for the future so let’s comment out whether to emoji and then let’s keep moving forward now one other cool thing I kind of want to show you is uh it’s a little bit more advanced but some of the stuff that we’re documenting out here we could do something called type hinting where it allows you to essentially integrate documenting and coding at the same time I don’t want you to be doing this yet but I just want to expose you to it so if you ever see it you’re not confused by it and you know what it means and just know that it’s there to help you and not to confuse you so for example we were saying you know we’re we’re writing all the stuff here and telling the user hey it takes in one argument as a string right but then the user is probably thinking well does this function return anything well then we’ll have to write okay this function returns nothing or what we saw earlier turns none so so you know the user has to actually like read all this and try to understand this so we can actually make this simpler for the user so with this weather we know it’s going to be a string so we can actually do colon Str and Str is a built-in if I go to the console and I write like some random variable it’s going to say hey it’s undefined but if I write Str it’s going to actually say oh that’s class strength and then there’s another built-in right which is none which just is like hey this is none okay like nothing that’s what that means so what I could do is not only could I tell the code that we’re expecting a string so when you’re reading this you’re like ah I get it I’m expecting a string here for weather you could also do this and say none which if somebody was reading your code they would immediately know oh weather is a Str whether it’s a string and this function actually returns none ah okay so um I shouldn’t be expecting a return statement here so it’s very nice to kind of bake in documentation into your code like this because it’s just very helpful and this is a brand new new feature that was enabled after python 3.5 plus and you know so it’s really nice because if you use typescript or any of those other types of languages they have they have this type of stuff there and what I like is like you could use it or you could not use it and your code would still work so for example if I run my code here and I run this I’m still going to go ahead and get my umbrella nothing really changed and you might be like why would I ever write more lines of code well same reason why you write more documentation or documentation in general is so your code is cleaner and easier to read and something for you to understand is 80 to 90 percent of your time as a developer will be used to read code okay about 80 to 90 percent of your time only about say 10 or 20 percent of your time will actually be used right in code okay so for example why why do I mean that if you were building an app and you built your own app and it took you like six months or nine months or a year to make you’re not every single day going to be coming back to that app and like writing so much more in code a lot of your time is going to be spent reading the code understanding what you’ve done and then maybe adding a new feature and then if you want to add a new feature you’ll you’ll need to go back and look at again what you’ve done and then see if you could add a new feature because adding a new feature wouldn’t just be as simple as copy and pasting lines copying and pasting lines you’ll have to understand all the work that you’ve done previously so the cognitive load of if you just make it messy and leave it jumbled and don’t have any documentation or anything it’s going to be a nightmare even for you to understand what you did and to catch up with everything so for now that you’re a beginner you don’t need to worry about any of this you can just be like screw docs screw type hinting I don’t give a I’m trying to write a simple code fair I agree with you honestly to this date that’s how I write a lot of my code but if you want to get hired and have higher chances of getting hired and write code it’s very pleasant to understand and read because again most of your code and other developers on your team will be reading your code this is the way to go go and if you pick up these habits now it’ll just it’ll make you so awesome to work with for future so just a little note there just a little rant there on it I just thought I’ll leave you with this okay so this is called type hinting so I’ll leave a little comment here saying type hinting okay that’s what that’s that’s what that is so let’s keep on moving forward now let’s go ahead and type in some of our other functions okay so for example we created some so why don’t you try to type hint sum and if you have multiple arguments you can just repeat this process right here for the multiple arguments as well okay so give this a try and see if you could type in some I’ll give you about five seconds five four three two one if you want to type in sum you can actually go ahead and say int it’s a built-in so if I write int here it’s a built in okay int and I’m expecting an integer here as well okay but in the docs I had integers so I’m I’m if I’m reading reading this code now I go okay I’m expecting it two integers to be coming through but then what are we returning well we’re also actually returning a see if you could fill this out here what does this return once this get evaluated so if I had a one here and a 2 here this will become a three and I’ll return to three three is what data type you’re right if you got this okay this is I’m gonna be returning an INT so now when I’m reading this without even looking at anything else I just know okay I’m getting integers I’m returning an integer very simple but initially very confusing to look at and then when you get used to it then it’s simple and then you’re like oh actually this is super helpful it saves me a lot of time and effort like for example you know if you had food well what the heck is if I wanted to convert this into type hinting right what’s Happening Here How can I convert this into taipant well I’m getting back I’m putting in a food but is food a like you know it’s food am I expecting a string like chicken or am I expecting a food amount like a number well how would I know that it was an amount also can I get back an amount like 50 or what about 52.5 live so I can also get floats well float you know is like a super class of integer because floats contain integers right so I can actually go here and I can say float it’s a built-in data type food is float and type uh tip percentage is it could also be a float okay but in this case we could just say integer okay we’re expecting back to get back into your 15 20 not many people release it 25.5 percent is what I want to tip and then what are we returning while we’re returning a number which is you know could be a hundred and twenty dollars and 20 cents so it could be a float so I could actually just say float so now when I look at this function I just go okay I’m expecting some kind of number here some kind of number here and I’m returning some kind of number okay so some math stuff going on here all right so that’s how I would type hint both of those now if I call sum and we run this I should still get some without any problems so it didn’t really do quote unquote anything it just made your code a lot more read edible and it makes other developers not want to slap you to be honest so great so whether to Emoji is now good you’ve gotten some practice with type hinting named arguments default arguments calling functions turning your previous code into functions all of this is a these are actually very valuable skills and it’s a lot to synthesize in a very short amount of time so I’m going through this but I expect that you’re watching this over days and weeks so I hope that this can register in your brain now before we move forward I want to give you an exercise it’s time for an exercise put some cheesy music or something here throw some cheese on the screen all right great so time for an exercise all right get ready get ready we’re gonna try to keep this exercise as simple as humanly possible and we’ll start off with something something basic it’s not that basic so don’t get excited it’s actually a little tricky but um I I believe in you I believe venue I believe you could do this so I have this exercises folder right here you can go in here and do the exercise okay so I’ll go into exercises and we’ll go into this exercise here bigger guy this is going to be your first exercise all right so now I’m inside of bigger guy so this is how it works so I go here and you read The Prompt make sure you read everything that’s there don’t just not read because that’s mean we spent so much time and effort making this for you making it easy here’s the prompt write a function bigger guide that takes in two numbers and returns a bigger one make sure to return to use return and not print in your function you could write print in your function while you are let’s say working on it but you know in future make sure to uh return it but I’m Sorry by that I mean that at the end once you’re done make sure you return it okay like don’t just leave all the prints there but don’t return so like while you’re debugging and working on it’s pretty normal to use print at the end of it you should have a return majority of the times on all your functions one other important note that I want to leave you with is and this will come in handy later so I’ll say important right here and it’ll say Do not forget to import this into main.py alright so I’ll talk about this in a second this is going to be a very important point because if you don’t do this and I don’t tell you about this you won’t be able to run your bigger guy function and then you’ll just be depressed alright so let’s just talk about the function first real quick okay and what’s happening here so given two numbers return the bigger one so I want to be able to call bigger guy just like this bigger guy takes in two arguments two comma three and it Returns the bigger one three okay so that’s an example for you if I had obviously 10 then it will return 10 right here okay it won’t return the smaller Guy where it says pass is where you’re going to write your solution where you’re going to write the code okay I have written pass here because I’m not just going to give you the solution it’s an exercise now if you want to actually call this function here’s how you’re going to do it you’re going to go inside of your main.py and I’m going to show you how to import stuff because it’s going to be important for me it says import quasi because because I’m still working on this file for you this line shouldn’t even be there and it doesn’t matter so I’m just going to comment this out for now because it’s not necessary okay you could just if you feel comfortable you can even delete this but it’s not going to be there most likely uh now we’re going to say from exercises dot bigger guy so I’m gonna go dot bigger guy so by doing this I have so here imagine like we’re we’re on this walk and this journey together right so when I do exercises it takes me into this folder right here boom and it just opens it then we’re gonna do the dot bigger guy it goes into this file and it opens it now inside of this file I have pretty much only one function and it’s called bigger guy as well so I can actually say import bigger guy and that’ll get me access to that function but since I’m so nice and I want to make life even simpler for you you can actually just say import exercises dot bigger guy and that’s it okay yes this is going to remain yellow but leave it it’s not important it’s not important so let’s go back into our bigger guy right here okay and if you want to run this let’s do this okay so I’ll just say print and I’ll say banana Anna here and then I’ll say print bigger guy and then give it two arguments so we’ll just give it one and two for now and I’ll run I get a banana printed out to the screen so without confusing you too much if you want this to run you’ll need to have this imported in the main file if you don’t have it imported in the main file and you run it you get back nothing NADA nobody’s there nobody’s home okay because what’s happening is the only file your python is running right now is this main.py file as the only file that’s being ran none of the other files are being ran so you have to import and then you can actually run that file okay so import allows you to do that here is another way you can run that file okay and this is the way you could run it on your local computer anywhere as well so say you want to run this file bigger guy and let’s just say you forget this import method or you just don’t like it I don’t know whatever the reason you can run it from your shell okay so go to your shell you can hit enter right and and this it should tell you’re in this folder whatever your folder is called CD into exercises okay so the command line stuff is valuable and so I’m I think it’d actually be cool if you learned this because if you’re doing python you’re gonna be doing something with the command line so CD into exercises and all this means is I’m I’ve changed directory and now I’m inside of this exercises folder and then simply do python bigger and then you could just hit tab it’ll auto complete it okay and there we go so the banana was printed out to the screen it ran that file so those are the two different ways of running this file I think this is a much easier way because once you set it up this way you can just keep working on your code you just say print cookie and then just hit command enter and everything will run okay you’re getting the cookie and you’re getting the banana everything is working perfectly so in my opinion this is a much easier way now once you’re sure that you have written this function and you’ve written it completely correctly and just to give you hints you’re going to have some you’re gonna have to do some if um else conditions here and to give you even further hint you’re probably gonna have to do only one if and only one else and your logic is going to be you know basically like hey if this is greater than this one then return this one otherwise return the other one okay so I’ve kind of given you the logic now you just have to turn it into code and we’ll code it together if you don’t get it which is totally fine so that’s kind of the logic right there and if you’re sure that you did it right I have man I’m so nice I have also written tests for you which means my tests will run and test if your code is working or not and if it’s broken it’ll catch it and tell you so what you could do is you could go into the shell and once you’re inside of the shell just make sure to go to the home directory so let’s just say you were in exercises just do CD space dot dot that will take you out and back One Directory so now that you’re in the home directory whatever of your project just go ahead and do PI test if you run Pi test it’ll run and it’ll tell you if your function is correct or not and here it’s saying uh nope this function was not correct we ran the test it says hey we ran this a bigger guy one comma two and where it was supposed to return to it actually didn’t it actually returned none and that doesn’t seem right now I’m hiding the solution from you because I just solved the problem if you have done it correctly and you do PI test it should basically say green okay um and it should give you uh test all exercises okay and it should give you a like a green check mark for this now if you just do PI tests right on your computer you’re going to get something ugly that’s gonna have red red marks and stuff like that and I don’t want you to have that because it’s testing every single test that I’ve written inside of test all exercises so it’s actually saying that you passed for the bigger guy one but you failed for some other ones right so one failed and it says one passed you can see the green here passed but what I want you to do is don’t test the entire file in all the functions right so if I actually look in the test file I want you to just test the bigger guy function so I’ve written these tests you don’t have to worry about them but I’m just checking if you know if I call it with one and two does it return two if I call it 10 and 20 it should return 20. so I’ve written these tests don’t touch anything in the test file okay so just get out of this test file I’m just showing this to you so you’re not like scared what this is yeah what’s going on here but I’m also testing other functions in there so if you’re doing Pi test it’ll test this guy and it’ll test every other exercise you’ve done as well so we don’t want to do that we want to test only your uh bigger guide bigger guy okay so let’s go back and here’s what we’re going to do I’m going to clear this okay and then we’re simply going to go ahead and say I’m going to say Pi test do Dash K and I’m just going to say test bigger guy that’s it so now for bigger guy I did pass successfully for the other now if you want to test other functions you obviously just give the names of whatever the other functions are and make sure you don’t change function names and things so go ahead and give this a try I really hope that you get it I think you will and I’m just going to set it back up to how you should get it okay so it’s just going to be like this is going to be pass boom okay and then yeah that’s it so replace this pass with the correct code run this test it out and yeah and if it’s successful then it should pass essentially all of the tests and then you should be good to go with that said good luck I’m rooting for you all right welcome back so I hope you gave it a shot and made tremendous amount of progress I really hope you were able to actually fully solve it but no worries uh if you didn’t let’s do it together and even if you did hey maybe you’ll get to learn something so here’s the situation right I have to return one of whichever one is the bigger one well I’m gonna check and I’m gonna say hey is a greater than b okay so I’m going to do that check first so let’s actually go ahead into our console let’s just test something so I’ll set a num A5 and B I’ll set it to 10. and I’ll just say hey is a greater than b and I get false okay so I’m like okay so far so good I like using my console as I’m writing my code because very interactive I can see what’s happening and then come back and add it to my code okay and now I can actually even write here if a is greater than b then you know just give me back a just like print a whatever so if a is greater than b let’s see give me a else give me b so let’s see what this does I ran this line this code and if you actually see I got back 10 which is actually exactly what I’m supposed to get right in that case so I’m going to say if it’s a is greater than b return a else return B okay so basically what I’m saying is you know just do do this little chakaruski over here and if it’s if this number is bigger then return it otherwise if the other number is bigger then return that yeah that’s it so let’s return run this and we we can try running this function and see if it gives us what we wanted to give us so I’ll say bigger guy and I’ll call this with one comma two let’s run this and I get back to 2 which is completely correct let’s go five it should choose five now so it’s looking correct I can also import it as a console if I want I can say from exercises dot bigger guy import bigger guy and then I can say bigger guy give it two comma four so I’m just showing you multiple ways you can access this function and play around with it and then I’m gonna go ahead in my uh test and I’ll say test okay and um let’s just leave a little note here and then we’ll say to to test your code this is the command that you’ll write Pi test Pi test k and then you know bigger guy whatever so I’ll remove this yeah this is going to be the code that you write for this one right it’s a pie test and then I’ll say bigger guy shell okay there we go so now it’s very clear what’s happening all right let’s run this and boom there you go it says all tests have actually been passed all right there you go so if you got this congratulations if you didn’t all good they’re going to be plenty of more exercises so don’t lose hope and let’s keep going all right welcome back you’re doing great so far all right so now let’s get out of this function okay hopefully you got this one correct let’s go back into your main but I’m gonna go into my Kazi because that’s where I’m writing all my code but you should be writing it in your main okay so just ignore cozy so let’s go over here now I want to introduce you to our friend Lambda okay it’s a great friend you’re gonna love him or love her she’s awesome but in order to learn about Lambda you know we need to just get the technical definition out of the way and it is basically allows you to create an anonymous function uh boring boring boring definition but it’s a lot more fun in real life so let’s just use it without doing too much talking here’s an example so remember when we had our sum function right so it was a comma B and then we said return a plus b well with Lambda we can actually make it a lot cooler for example I could say sum and I could say Lambda okay and I can say a comma B and I can say a plus b so this is let’s just call this sum two so basically over here this is a this is a whole function okay how do I know because I can actually use it I could say print sum to and let’s give it one comma two and it’s telling us what the inputs are it’s an anonymous function right here all right so I’m going to hit run and right when I hit run boom look at that I get back three now I could change this to let’s say five comma ten let’s run this and I get back 15. so you can see that this is a really nice way of writing the same piece of code and it just puts it in one line and honestly I really like looking at it like this and in functional programming if you’re doing JavaScript or whatever you come from that ecosystem yeah a lot of functional programming will look like this okay JavaScript has its error functions but a lot of those Concepts came from like Python and functional programming languages like this synonymous functions are really nice now you could use you could create this function without a name and use it or you could do it here so either way will work but yeah that’s the definition now I want to introduce you to this concept of implicit return so notice that you actually didn’t even have to say return here here because in Lambda functions you can only have one expression instead of multiple expressions and that’s expression expression it thinks you’re returning okay so when I run it you get back 15 and I don’t even need a return statement so that’s nice because it looks cleaner now how does Lambda work so Lambda you say Lambda and then you put ARG one you put Arc two however many arcs you have right whatever and then colon argone plus R2 or whatever like however you want to use them this is where the arguments go and then after the colon is where your expression goes okay so we just say expression all right so you know if I want to make my greet function I can say take greed comma name and then just go high I can go great cozy now my grid function has turned into a Lambda and I can run this okay so my bad name so now let’s do print and we’ll call this great and now I’ll say great and we’ll say aloha and I will say qazi and let’s run this and boom we got that okay so you can see it it works it’s easy to read and everything is just in one line now there will be more we’ll talk about later about lambdas but for now I just wanted you to be intro to them and not be scared of them because you’ll see them a lot so you know become friends with Lambda that’s a good idea and also I wanted you to get familiar with the concept of implicit returns right they’re returning here you’re returning here um implicit returns now another thing I want to intro you to is called testing so testing your code how do you test your code you might have heard of something called unit test or whatever we’re not going to get too deep into it but I’m just going to show you how to write basic basic basic basic basic tests for example we’ve already defined our sum function right so for example if I do print let’s comment out both of these lines here so let’s say I write my function sum 1 comma 2 I should get back 3 and it should print it out over there it does beautiful how do I test this so how I test this is I can actually go ahead and say assert that sum of 1 and 2 is equal to what can you guess I mean it’s pretty obvious right sum of 1 and 2 should return what three right so I will write the statement assert that sum of one comma two is equal to 3. so when I run this and if I don’t get any errors that means everything worked and our our code works great now imagine you went to your sum function so let’s see if we can find our sum function let’s say we want your sum function and you did something that may be broken maybe you like instead hit a minus here and maybe you did that by mistake and then you just like moved along if you didn’t have this a search statement you would run your code and it would seem like everything else is fine but that sum function might be causing so much so many errors in your app but watch this if I run this now boom it just immediately catches and it says Hey assertion error assert someone comma two equals three and that’s not the case here okay and I can even leave a little helpful message and it’s like someone comma 2 does not return three like it should and I I can put a little X here or whatever and you run this and I could see hey sum 1 comma two does not return three like it should so now there’s like a more helpful error message and you can and assert allows you to do that so what I like to do sometimes when I’m coding is I’ll just have a bunch of assert statements like some you know and and I want a really rigorously test this like does it work on negative numbers like negative 20 comma negative uh 20. so negative 20 plus positive 20 should give me back zero so I want to assert that this is zero okay so let’s go back to our sum function let’s fix it so I’ll say a plus b okay let’s run it again and now you can see that all tests pass okay so I can say print some function in all tests passed and I I’m just doing this one for just show you don’t have to ever like do that okay but I’m just doing this so we could see it cleanly okay some functions all of the tests pass because if it broke at any of these points it would just stop running right there it wouldn’t go any further that’s what assert does now what I would probably do you know as a next step is I would not just leave this code outside by itself but I would put it inside of a function so I’d say test sum so that’d be like hey test my sum function and all it would do is it would just have these assertions so I’m gonna delete delete hit enter delete delete hit enter and maybe a couple of more assertions like make sure 20 plus 20 is 40 make sure you know 560 plus 780 is 560 plus 780 is thirteen one three four zero okay so yeah just make sure sure that those match so now I need to and I’ll put this print statement inside of that function as well okay now if we don’t call that function obviously when I run my code nothing will happen because I’m not calling it so I’ll just say test sum and we’ll run this and boom look at that it says all test pass in this case we have four tests I’ll just say four or four and then we’ll run this and we’ll say hey all of the four tests passed so sometimes that’s actually a really good way for you to actually write your code because okay you know what what your functions should return right so for example let’s say we want to write a test for calculate food total before we even really write the code for it so I’ll say test calculate food total and I’ll do this and I’ll just hit assert calculate uh when I call that function calculate food total yeah and isn’t that what I called it oh okay I see I called it like this uh camel case so I’ll just say calculate food total and this is really helpful because documentation just showed up from our code so the first argument is food so I’ll just say hey the food amount is a float right it’s telling us which is very helpful and it says the second argument human is tip percentage and that’s an integer so that’s also very helpful so I know okay 20 so if I have a hundred dollars and I have twenty percent tip the total amount should actually return 122 um 120 okay so that’s what it should return so let’s go ahead and test calculate food total and I’ll call this and let’s run it and let’s see if we get an error we did not get any error let’s say if we said 125 boom now we get an assertion error so that’s what’s nice because you could write these tests and it gives you a very clear idea of how your function is going to look if there are any edge cases you’ll be able to catch them right here for example what if we had a decimal percentages that people were giving it like would that still work and what would happen if they were giving this would that still work and what would what would the result of that be so you could test that you know you could test what if people give me you know a negative number because for your apps when you’re making them for users you’ll have to also write these tests right imagine somebody who’s using Instagram right now well the developers have to test what if the user like hits the back button on certain case or like tries to delete somebody else’s comment are they allowed to do that can somebody you know create a new comment are they allowed to do that is that working because what if all of a sudden they added a new feature but then that broke the ability to create comments they want to be able to catch that right away without you know with waiting for a really long time to find that out so they have all these test cases and then as soon as that function breaks or you do something where it doesn’t work these test cases will actually catch it let’s go ahead and do test calculate food total and so like imagine I went to my test calculate food total and I was like I’m just gonna like change around a couple of things and I’ll try uh just doing divide divide by 100 I’m sure it won’t make a difference let’s go ahead and boom We like caught that there’s an error right away that it doesn’t work it’s saying assertion error right it caught it right away so let’s go ahead get rid of this let’s run it and now boom you can see there are no assertion errors anymore and our problem is solved okay so this assert thing I really like using it it’s very simple to use I love how simple python makes it so you know get in the habit of also testing your own code and this is a very light and easy way to do it and then later on if you ever want to write unit tests I have an example of unit tests here for you and I’m I’ve written some unit tests in here okay so uh that’s that’s the way to do it so far you’re doing great let’s keep on going all right now let’s go forward and we’re going to talk about probably one of my most favorite things that makes programming honestly programming and it is arrays okay a raise which in Python we actually call lists okay so if you hear arrays they’re the same thing as lists okay so lists are one of my favorite thing things in just programming lists allow you to do a lot of stuff but let’s just start off with simple things so let me ask you this question right so far if you want to hold multiple items in one variable how are you going to do that so let’s say I want to give you like you know I want to say hold 10 numbers in one variable how are you going to do that you’re you’re not right because if you say x is equal to or you say numbers is equal to how are you going to hold 10 numbers because if you do 10 comma 10 well that won’t work that won’t do what you wanted to do you could try 10 10 10 10 but that also won’t work because that’s an error it’s like okay what the heck is even that if you try to put them all together well that’s just one big number that’s not 10 different numbers lists are amazing for that now where are lists used well if you’ve ever been to Netflix here’s an example of an of a list here the list of users and for every user show the user so this is an example of a list and where it gets used if I go inside here all of this this is a list okay list of movies so it’s the first movie this is the second movie third movie fourth movie fifth movie and multiple things are being stored without lists this would not be possible lists are erased it would not be possible I’m gonna create an array or a list of fruits and in this list I’m gonna have different fruits I’ll have a cookie I mean that’s not really a fruit I’ll have an apple I’ll have a pear I’ll have a ice cream that’s not a fruit what other fruits are there blueberry there we go I’ll have a banana so I got a bunch of fruits here and yeah my my pie flakes is freaking out pie flakes is just really really just saying hey like it usually tells you about styling but it’s freaking out here because I think it’s like I don’t understand this blueberry and this stuff is okay now I can print fruits so let’s go ahead and run this okay it actually caught it correctly it said hey I don’t know what this blueberries thing is so let’s change the blueberries to an orange let’s give this a try and let’s see if it knows yes it looks like it knows you you don’t have to use these emojis I just like to use these emojis because I’m sure you’re bored with all the other boring programming tutorials and so this would make your life a little bit easier it’s something more fun to look at than just numbers so here are my fruits that I have now here’s the cool thing I’m going to show you a couple of things with lists so lists have different type of methods that you can use and if you ever hear me saying word like methods versus functions here’s really what that means anything that I do like this if I do print or whatever that is a function okay this is an example of a function but anything I do that requires me to do a DOT so with with the list I’ll probably show you like list dot uh list dot append if you ever do this dot a pen thing so append would be a method whereas print would be a function so just some terminology stuff that you know could be helpful for you so let’s go ahead and print fruits and you know of course when I when I print fruits it shows me the fruits right over there and now I’ll show you some methods that you could have so for example what if I wanted to add more fruits like let’s say I want to add another orange well what I can actually do is I can say fruit dot append and I can append another orange and then let’s go ahead and print fruits again so Watch What Happens look at the first array right this is before adding the orange and then look at the second array this was after adding the orange and now you can see that I’ve added that orange okay and it’s a pen because you’re adding to the end that’s why it’s a pent and there you go I got myself another orange and so you try a pending a strawberry and see if you could do that and if you can’t if you don’t have emojis just use the word strawberry so give that a try I’ll give you five seconds five four three two one all right let’s do it together so I’m gonna go ahead and I’m gonna say fruit start append and I’ll put a strawberry here and then we’ll print this out again let’s run this and now I should see yes without this it’s four items originally then I added the orange and then I added the strawberry and you can see all of them are actually added correctly how they’re supposed to be added there you go that’s your buzzword of the day a pen now let’s look at indexing what if I want to get the zeroth item right there and maybe needing I want to get this apple what if I want to get that orange how will I get that orange out of that area so I can do print fruits right that I could do and I’ll comment out uh some of these fruits right here actually let’s leave it uncommented for now just like okay I can print out fruits but how do I get that specific fruit out of there well what I could actually do is something called indexing so if I show you a raise arrays have indices okay so the zeroth item is going to have the indices of zero meaning this apple here is going to have the indices of zero then the second item is going to have one because in programming numbers usually start from zero okay and then one instead of one and two so in this case we have the pair so one is going to be the pair then it is Orange right there so let’s go ahead and give that a try now so I’m going to go ahead and do fruits of zero and let’s run this and look at that I got my Apple okay I’ll go ahead and comment out all this other stuff for now and let’s look I got my Apple okay let’s go ahead and see how we get our pair and you can see I got my pair let’s go ahead and try fruits of two and I got my orange okay so I think you’re getting the idea right now why don’t you go ahead and try to get that banana I’ll give you five seconds five four three two one all right so I hope that you figured out how to get the banana but it’ll be very simple right we’re just gonna do fruits of three and if I run this I should get back that banana right over there okay so so far so good I’m able to index it and get back things now here’s a cool way to get the last item so it’s like how do you get how to always get the last item in an array that might be a great question for you to think about okay maybe that’s a problem you could try to solve one nice way we could always get the last item of an array is we could go backwards so python allows us to actually index backwards as well which I love because JavaScript and those things don’t have that so it’s like when you’re working with lists my God it’s annoying to not be able to go backwards so here’s how we can do it instead of saying fruits of three I could actually go fruits of negative one and fruits of negative one you’ll notice actually gives me this banana right over there and this means that even if I change the size of it meaning I keep appending let’s say I keep appending fruits here okay and then let’s comment out all these other ones so I’m changing the size of it and I’m appending like an orange and a strawberry so it needs to give me dynamically the strawberry let’s see if it could do that I’ll run this and boom look at that it actually gave me back a strawberry so it doesn’t matter how many things I’m adding or not adding right it will figure out in its own way so comment this out now comment it out now let’s say I add a cookie here and when I run this fruits of negative one is still going to get me to cook you okay so even though the size of this keeps changing this starts from right to left whereas this starts from left to right okay so right to left so negative one okay in other words this will be negative one and like I’ll zoom in so you can see it better that’ll be negative one that’ll be negative two that’ll be negative three okay but if you if you’re going this is if you’re going right to left okay but if you’re going left to right then this will be zero and this will be one okay and then this will be two this will be three and this will be four and I think you get the idea okay so in my opinion it’s much nicer in or uh in Python and way easier to be able to get like the last item of an array and the first item of an array so that’s how uh this method append works and that’s how uh indexing works and you can actually like pull values out of an array now the other thing I want to show you when it comes to arrays is something called slicing so this is a very valuable thing to learn so I’ll say so it says slicing when you have slicing in an array that means what if I want a chunk of this array from here to here or what if I want just these three elements from an array so how do I slice this array earlier I gave give you an example of Netflix right if we go back to Netflix say all of these movies every single one is in some big giant array but I just want to slice and give you in this in this list right I just want to slice and give you every trending now movie so maybe I’ll go through that let’s say like every single movies in this giant list every single movie well I might want to slice it and just get you the trending movies or I might just want to slice it from here to here and get you you know award-winning shows or what or yeah right so for example boom boom like if I don’t want to get you everything so that’s worth slicing can come in and actually help us all right so if I go here and I go fruits of zero through two let’s see what happens so I’ll go ahead and print this and then let’s comment this out and let’s run this okay and you could see that I got two fruits so I went from zero to two now it goes up two but not until okay so it’s like the first one you know first argue first one is inclusive and the second number uh is exclusive second exclusive so kind of annoying I understand but it stops right before two meaning that if you did zero through two what it would really get you is 0 and 1. okay so that’s what it’s going to get used to two now if I did zero three zero through three it’ll get me this now go ahead and get me all the fruits but not including the cookie because that’s not a fruit so get me from here to here what are you gonna how are you gonna do this so print that out right here give that a try so five four three two one so how we would do that it would be very simple I’ll just say zero through four and let’s run this and that gets me all the way up until there except for the cookie all right now another thing that’s very valuable is I want to always know at any given time what’s the length of my array so that’s something you very commonly have to find out whenever you’re working with a raise so in Python there’s something called Len and when I call it you can see it’s a built-in function that comes with python and so say I have this this fruits array so I’ll just copy this and I’ll say fruits is equal to that okay so I’m doing it in the console just so it’s easier for you to follow along and you could interactively do that too and now I could actually go ahead and say Len of fruits like this so watch what happens when I run this it’s going to go ahead and tell me that the length of this array is four okay so length is a very important function it will allow you to you know always get the full length of the array so if I asked you hey create a function that can like you know always get from the start all the way to the second last item so basically regardless of the size of the array it gets from the start to the second last but then doesn’t get the last one right how would you do that well you’ll need to actually know the length of the array so then what you could say is hey go from zero to the length of the array minus one okay so you could go hey go from zero right here right there to the length of the array minus one okay so in other words I could say print fruits from zero to land of fruits minus one because this is going to be 4 minus one is going to be three let’s go ahead and give that a try and there you go that worked it didn’t get me the cookie so if I comment these out as well and I run this you can see it works it doesn’t get me to cookie so it goes from the start all the way to the second last at the end so slicing is a very valuable thing and what’s really cool about slicing is you could also slice strings as well so if I have a string like this even though we’re not talking about strings but slicing is like an array method or a list method it could still be valuable for this so if I have a string like hi my name is cozy okay I can slice this and go zero to ten and if I run this it’ll get me the first 10 characters right hi my name is whatever if I go 20 it will get me the did I nail it like no I didn’t okay good it’s freaking out so if you go above the maximum amount it’ll just max out so you could go zero through eighteen that’ll give you the whole thing so you could slice this too what if I wanted this last character right there where it says I what if I wanted that well I could just go and go ahead and go negative one here now one other thing I want to show you with the raise that’s very nice is how you can actually reverse it in a really easy way so I go here and this is something also new as well so if I go ahead and print uh fruits right and let’s go ahead and comment this out and comment this out so if I go ahead and print out fruits and I go give me zero through let’s say a five and I run this it’ll get me every single fruit right and obviously this is part of the fruit too apparently cookie is a fruit I bet you didn’t know that so if it does that um I can I can also reverse it so I’ll show you something cool so there’s also a step so this says the first zero right so if I was to make this out so I have this I have I have this right and I have zero excuse my bad handwriting colon five whoops colon one so one when I have this right this means from okay let’s go make that bigger this over here means two let’s make that bigger too okay and then this over here means step so step by just like step by one by one character which is the default okay so if I run this now you should just get fruits this regular array but what if I wanted to skip and get you every other fruit meaning I wanted to get you apple and I want to get you an orange and the cookie but nothing in between well I could actually step by two and I could do that okay and I could get boom like I’m so you see how I’m like skipping over every single two now what I can also do is I can go backwards as well so I can this allows me to step forward and if I go negative one this allows me to step backwards so if I run this now um I’ll I’ll I can’t have this be zero colon five okay because the from and two are reversed so the trick here is if you don’t specify this at all and you don’t specify this at all you just do colon colon you can actually do colon colon negative one and this will actually reverse reverse the array and you could actually reverse strings like this as well with that same technique so if I change that to negative one here it’ll go backwards instead of you know if I change that to positive one that’ll go forwards okay so arrays our lists have a lot of tricky methods and you know and functions you can call on it and you know the slicing business but it’s very valuable for you to learn because it’s going to be super super helpful as we start to get to the later projects because you’re going to need all of this stuff you know when we’re doing bigger projects or if we’re building a game or anything like that now some other stuff that’s actually helpful for you to know as well with lists is that list could hold different types of data types so for example I could have numbers and strings in here so I could say hi and I could have a float like 8.5 I could have integers hell I could have other arrays inside of it or other lists inside of it like another list of numbers so I could have all kinds of stuff inside of a list lists are cool with it lists don’t have any problem with that so you can see that’s the output right when you run it you should get this see I was able to reverse that list as well so yeah you can I’m just used to saying array my bad list so yeah you can always you list can hold any type of data type I think that’s a helpful thing for you to know and then there are a lot of other methods that lists have we won’t go into all of them today but I left you with some of them like an append method right and just kind of different functionality you could do with it you could call Len on it and get the length of the list you could call you know you could slice it so I wanted to just give you certain pieces that would be valuable so you can see right here right there’s tons of different methods append is one of the ones I showed you there’s insert which allows you to insert an item into the list anywhere there’s clear which will clear all the items in a list there is count the count items and things of that nature you have removed so you can remove items from a list sort allows you to sort the list reverse allows you to reverse the list yeah pop will allow you to basically remove the last element of the list and then return a new list which is pretty interesting it has really nice use cases and then of course index as well so if you put a cookie it’ll tell you what the index of that cookie is or the you know banana what’s the index where is that specifically located so there’s a ton of different methods I just wanted to show you a few of them and with that said we’re gonna move on and we’re gonna start learning about something really fun called dictionaries and python let’s get to it all right now let’s move on and talk about dictionaries so I will comment this out dictionary is a very powerful concept inside of python let’s start with visual examples and now make our life a lot easier all right so say we have this person here which is me hi I’m qazi and we have this person and the person name is qazi and person’s shirt is black right this is the shirt that we’re wearing so in Python so we’re going to represent this with a dictionary so if you just look at it by example you’ll kind of get it very quickly so first thing I’m going to do is I’m just going to say person and then whenever you’re creating a dictionary you’re gonna create these curly braces okay all right and they have something we call key value pairs so for example if we’re looking at this guy right over here this part right here would be what we call a key and then this part right over here is what we would call a value okay so that’s what that means key values it’s like if I try to get that person’s shirt I get the value black back okay and if I try to get this person’s name I get the value cozy back let’s go and leave that there so you could see it and now we’re going to go ahead and we’re gonna say okay so it’s basically in a key value format so the first key we have in this case is our name so I’m going to say name and let’s put it in quotes like this and then the value is cozy okay so that’s key this is key number one so this right here now already matches that okay so I can go ahead and say person and I can say person show me the person’s name and we can print this and now I’ll run this and you can see in the console I just got back cozy how did I just get back cozy by doing this okay so whenever you do this you call your dictionary and then you give it which key you’re looking for and in this case we’re looking for this name key then it’s going to spit back out the value now if you want to add another key to this another key value pair so basically if we want to add the information about the shirt we just put a comma here and then now we can say shirt is black and now why don’t you go ahead and print out the shirt and it’s going to be in the same format right over here okay exactly the same you’re just going to replace this here instead of name you’re going to put in something else and then run it and see if you can get this color black back so I’ll give you five seconds five four three two one so go ahead and pause it and try it and if you’ve already done it great job and let’s do it together if you if you didn’t get it no worries we’ll just do this together right now I’m gonna say person and I’m just gonna go ahead and say shirt and this right here should spit back out black okay so let’s go ahead and run that so you can see cozy and then we get the shirt color back black so both of this is working now what I want you to do is add another key value pair see if you can add another key that says laptop is an apple okay so this is your task right now so it should be the laptop is Apple okay so Apple should be the value and laptop should be the key so give that a shot and see if you can add another key value pair in here I’ll give you five seconds to pause five four three two one one all right so let’s go ahead and give that a try now so I’m gonna go ahead and say comma and I’m just going to say laptop is an apple and now we can go ahead and print that out as well I can say laptop is an apple okay boom there we go now normally when people are writing dictionaries most of the times how they make it look and if you’re coming from another language this is an object or a map so if you’re coming from Java or JavaScript you know it’s called an object there just like lists are called arrays so this is how it’ll normally look because sometimes if you’re making it run really long it can look confusing so how we like to do it is like this each key value pair represented on its own line and then open close like this so it’s very stylistic and it looks good so styling your code is a thing in coding and if you can make your code look nicer then that’s great okay so people are going to love you for and other developers are going to love you for it so like reading this you know wouldn’t you agree that this is reading this is much easier like okay key value key value key value then reading this right that that looks much more confusing so that’s why we like to do it like this and this should still run if I run it I still get all my all of my stuff here is getting printed out so there you go now what we can do is we can create a function that you know that takes this information and then maybe just gives you a little greeting okay so like this is like our Advanced greeting thing okay or just maybe introduces who this person is so let’s let’s create this function and I’m going to say Define and we will call this introducer and what the introducer will do is let’s say we just put this person into our introducer and then it’s going to go ahead and say hi my name is blank my shirt I am wearing a black shirt oh not black but like a blank shirt and the laptop I use to code is n blank okay so we’ll put some emojis to make it fun so this is going to be the high part it’s going to be the shirt part and this is going to be there we go so now if I run introducer and I say print introducer oops I get an error and this person is not defined introducer oh yeah I got a call introducer like this my bad okay and then also let’s comment all of this out because that’s what’s complaining because we just took the definition of person away from there and you could see okay and we don’t need uh the print statement there for now you can see it says hi my name is blank I’m wearing a blank shirt and the laptop I use to code is n blank okay is a if you’re you know isn’t Microsoft or is an apple all right and so now instead of these blanks we’re going to actually put in these specific values so the way to do that is put a f here so this enables string formatting and then I’m going to basically say here name and here we can go ahead and say shirt and then here we can go ahead and say laptop but this won’t work right why will this not work because we don’t have access to actual name and shirt and laptop variables if I run this I’m going to get an error well we do have access is person so remember we gotta actually call it like this so replace this name here with this so give that a try and see if you can actually make that happen I’ll give you five seconds here to give it a try or pause the video five four three two one okay now let’s do it together I’m going to say person and then we’ll say name just like that and then we’re going to say person and we’ll say shirt just like that and then here we’ll say person and then we’ll say laptop just like that now let’s run it okay what’s the error that we’re having here it’s complaining about invalid syntax so let’s see okay so the problem that it’s actually having is if you look here it says Hey person is not defined so the problem that we’re having facing here is we haven’t put this print inside of our introducer function so that’s where I’m having an error so let’s go back and hit enter and enter and now it’s actually inside of this introducer okay so it’s indented right here so now let’s go ahead and run introducer all right so this syntax error is basically complaining because we have single quotes here but then like also single quotes here so what’s actually happening is that this single quote is looking for another single quote to end so when it sees this it goes oh that’s the string and so it thinks this is it because it’s looking for the last one and then it wants to end it here so what we need to do is leave either this as single quotes and then double quote all of these or do it the other way meaning make this double quote and then leave everything else single quote and that would solve the problem so let’s go ahead and just make these double quotes because that’s much easier to do I’ll make this double quote right here and run this and there we go I see hi my name is qazi I’m wearing a black shirt and the laptop I used to code is an apple okay there we go so our introducer function now works uh pretty correctly now we can even go further and add this person’s you know let’s add let’s add assets so let’s say that qazi is you know has about a hundred dollars in his bank account okay so assets and then for debt we’ll say let’s say Kazi has fifty dollars worth of debt and then what we want to do is so now we have assets and debt so that’s that’s cool we added that but what if I wanted to add something that could actually automatically calculate kazi’s net worth so if I did net worth now there’s no way to actually set assets minus debt here because these when we’re creating this these aren’t defined yet so if I run this this wouldn’t do any it’ll be like wait I don’t understand what assets or what debt is so here we want to create a function that can call so essentially like imagine I had a function right imagine at a function you know we’ll just we’ll call it uh net worth and then I did and I said hey grab the this person object okay or this person dictionary and then basically do grab their assets and then subtract that by their debt okay and then return this that’s the so like if we want to create this well there’s something really cool that you could do inside of dictionaries as well you could actually create functions inside of dictionaries so in other words you’re kind of creating a method because you’re gonna have to use a DOT something but yeah so it’s just kind of it’s a really cool hacky thing that you know it’s a little bit Advanced so I I don’t imagine you’re going to be using this right away but in the future this is something you could use so I would I would say just have this up your sleeve Right In Your Arsenal so check this out right this is pretty cool so what I can say is I can actually create a function here now I can’t create a function like this because whenever you’re doing def you have to Define you have to create that function on its own line it can’t be it can’t already have stuff behind it and then you could do def it won’t work so remember how we can create inline functions our friend it starts with an L see if you could remember that yes that’s right our friend Lambda this is where our friend Lambda comes in so I’m going to create a function on the Fly here an anonymous function we don’t need to give it a name because I mean I guess in this case in case the name would be net worth and what I want to do here is I want to say essentially this and I don’t even need to write a return because remember lamb does have an implicit return so net worth is a function that does this so now I can actually say and let’s continue this right and my net worth is and here’s how I have to call this function okay so this is an interesting part so I’m just going to show you separately here first so if I want to call that function this one right here I gotta say person net worth and that’ll get me access to the function but anytime I have to call a function remember I got an open close parentheses after that and that will actually call this guy right over here so I’m gonna just cut this because I know I’m gonna need this and my net worth is and here we’re going to go ahead and pop that in and then we’re going to run our introducer function so let’s run this and there we go it says hi my name is qazi I’m wearing a black shirt and the laptop I used to code is an apple and my net worth is 50. and then if you want to make it look prettier put it you could put a dollar sign here you could say USD here or whatever your currency is of currency of choice and then you could put you know a money bag emoji here right and now when I run this boom I could also put all of this on a new line so every single thing starts on a new line right boom boom new line new line and now it reads like this it looks a little bit cleaner so it goes let’s run this it goes right hi my name is qazi I’m wearing a black shirt and the laptop is the code is an apple and my net worth is fifty dollars okay that’s just to make it look slightly pretty well there’s a lot of stuff you could do with dictionaries including you could put functions in there as values uh you could have you know you could have fruits in here so you know or maybe I could say favorite fruits right and I can actually go ahead and put all my favorite fruits so let’s say I love I don’t really love apples but that’s fine I do like bananas so let’s actually put an orange here and so you can see that it could even hold an array right as long as you call a favorite fruits so I’ll go ahead and say my favorite favorite fruits are and then I’m gonna go ahead and say person and then we’re gonna put favorite fruits okay let’s run this okay invalid syntax so let’s just fix whatever error we got so it’s saying we’re having ah because we probably didn’t put a comma here right comma here and then let’s run this and there we go hi my name is qazi I’m Moringa black shirt the laptop I use to code is an apple my net worth is 50 USD my favorite fruits are the following yeah there’s many different ways to do this these are some of the ways that I like to use my dictionaries you know you could you could hold so much information in here and like for example a phone number would be a great thing to put in here so for example a what’s your phone right so whenever you think of a CRM think about your iPhone all right so you have your iPhone right and now this iPhone that you have stores all kinds of information so it’s like you have a contact by the name of qazi and then you’ll have their phone number and then you’ll have their you know maybe their address you know their their first name their last name and uh yeah all of that information that’s actually being stored on the back and it’s actually being stored as a dictionary all of that okay because hey here’s a here’s the thing here’s what’s actually being stored in it and dictionaries are perfect for that because what you do right is you don’t necessarily no con no my phone number or most people’s phone numbers on your phone but if you actually just type in quasi it gives you that entire object okay that has all the information like kazi’s phone number kazi’s address gives you all of that so think of it like it’s a key value pair so you search for qazi and boom you get all the things you need so that’s why it’s very valuable and it’s it’s very helpful and you know you could think of Google search like that too where you search a thing and then Google shows you some results so this here is an example of a key value pair so I just typed in Marathon right I’m getting all of these Keys essentially and so imagine the search term is a key okay so search term is our key in this case and then all the results that we get back think of that as the value so think of that as a big giant array that we get back we have Marathon Staffing that’s the key and then the value is this array of like results so it’s like it’ll show us you know website one website to website three website four Etc you get the idea right so those are already stored inside of Google’s database and it’s literally like a key value pair so now I’ve actually click Staffing Marathon Staffing and I’m going to get all these results right here and it’s telling me okay this is these are all the websites that I get so key value pairs are pretty much everywhere and everything that you do you know and like you could store a phone number and phone numbers are generally stored as a string so I could say one two three three four five six right there we go so there we go right so put a comma here and now you could get this person’s phone number as well okay so this is a good introduction to an object or a dictionary a few important things for you to know as in the differences between let’s say lists and Python and dictionaries is when you’re trying to get an item out of a list you use this index so you go hey get me the fruit from position zero right here we go and we get this item so let’s say I want to get this item right here I’m going to go ahead and get this item with this index and the index here is zero right so zero one two three Etc that’s how that those are the names of those right that’s how I get access to them and there’s actually something really nice you could do with dictionaries it’s called um dot value so I’ll give you an example so I’ll say print person dot value so there’s some like methods as well that dictionaries have they’re really nice so if I do this I’ll get access to all of the values let me comment out that function that just makes it look a little confusing so I’m going to comment out the function for a second and I will remove the net worth call from here for just a second you don’t need to follow along with that and here I have my values right these are all my addict values which is I have uh you know cozy black apple boom boom so these are all the values likewise I can also get all of the keys so this is another important method to remember about dictionaries so all my keys are like name shirt laptop phone number assets that favorite fruits so I’m looking at the values here and if I was just to look at the values so let’s go ahead and print it out and I’m just going to call list on it so it literally looks like a list like a pure good old list right right there and if we looked at this well the value of cozy in a list would be zero but in a dictionary or sorry not the value but like the index or the location of this isn’t like zero in this case it would be name and if I do name I’ll get access to it here in order to get access to this I will put in color instead of one so in a in a list you would do zero and you would do one in an object in a dictionary you don’t do that you just put their keys so that’s kind of the nicer part about it now some other things that are that are not going to be crazy vital to you right now but some information that is useful for you to know about dictionaries versus lists is lit lists are ordered okay so they fall follow a specific order dictionaries are not ordered starting in Python 3.8 plus anything that’s 3.8 and moving forward dictionaries are ordered and it is a built-in thing that comes into uh python most other language languages objects are never ordered and uh the in dictionaries in Python used to not be ordered but it is so cool I’m like actually nerding out over and I’m so excited there are definitely ordered now so I can give you like a quick example so let’s just make things and we’ll say health points of this fighter or whatever is you know 100 and maybe the name of this user is uh quasi so I can Define oh let me put uh quotes around this all right I have I have things here okay now what I can do is I could go things and I could actually just reverse it so I could I could call reversed on this and let’s go ahead and call list here and do this and when I call it you could see that name and health are now reversed the order of insertion was held first and named second right and now when it reverse this it just gives me this okay if I do list things it goes Health common name so yeah I mean this is so cool the order is kept and why I really like this is like when you get to the more advanced Parts later on and and you know when we’re coding you’re going to get to these parts where you know if we have to sort data we’ll usually have to take it out of a dictionary put it into an array and then like do some jiggery pokery with it because arrays are the only things that keep order or lists are the only things that keep order but like man this is so nice so it’s going to make your life a lot smoother yeah dictionaries are now honestly probably one of my most favorite things I want to use them even more so it’s like you get the benefits of a list plus a dictionary so I’m excited um as you can tell so yeah huge update and uh thought you guys should know that yeah this is massive and then one other thing before we go on to the next data types for you to learn inside of python lists and dictionaries lists and dictionaries are mutable so these are mutable and mutable just means changeable okay so you could change it you could change the values you could delete the values you could update the value so that’s another thing that I I think is going to be actually important to show you is in a dictionary how to update the value of a certain key value pair because I’ve shown you how to get a key value right how to get the shirt or how to get certain things but I’m now going to show you I’m just going to put the net worth back here real quick boom there you go let’s just run it let’s make sure our net worth is coming out fine oh yeah and let’s comment this yep and then let’s uncomment this sound for now so I showed you right but now I have to show you how to assign an update values so let’s say that we Define qazi’s shirt as black here but maybe today is a new day and cause he decided to change his shirt thank God no more smelly cozy we’re gonna go ahead and say shirt and I’m gonna go ahead and change the shirt color to Blue okay if I actually do that okay or maybe let’s make it orange so there’s every letter is different not because blue is B and L and it could look similar so let’s go ahead and send my shirt color I change it to Orange so let’s run this and now you could see here that it says kanji’s wearing an orange shirt even though we started it off as a black shirt so using that you could update a user’s you know their laptop you can update their phone number you could update their assets you could update their favorite fruits Etc so now I want you to go ahead and give this a try try changing let’s say the assets to a thousand dollars a thousand essentially okay so give that a try right underneath here it’ll be very similar to this but the key you’ll have is assets and then you’ll choose a value of one thousand so I’ll give you five seconds here five four three two one let’s do it together and I’m gonna grab assets and I will change assets to one thousand okay let’s run this and you can see my net worth is now nine hundred and fifty dollars right because a thousand dollars minus fifty dollars should get you nine hundred and fifty dollars so in terms of dictionaries I think this is good now let’s move on to other data types all right so now let’s go ahead comment out introducer so when you run your code you should see nothing essentially okay and let’s go down here and we’ll talk about tuples okay that’s another data type inside of python and they’re just like anything you put in these parents okay so for example a tuple just looks like parents I’ll I’ll put it right next to it so you could see for example if I had numbers and I said one comma two okay now this is a tuple I can print it out print numbers okay so let’s run this and boom and now I can also print out their data type so I could say print type numbers okay just like that now I could see that it’s it’s type Tuple class is Tuple now one thing I can’t do is tuples are immutable meaning you cannot change change their data at all so I can still get access so I could go numbers of zero and they’ll still get me that should get me one so there we go so I got the first item from that Tuple right so I can I can index it but I can’t actually go I can’t go numbers of zero to ten like this is something you could actually do in a list we are not going to be able to change it so let’s try running this and we get an error and the error basically says Tuple object does not support item assignment all that means is you can’t update items inside of tuples okay now tuples are used in Python quite often and they usually represent things that you can’t change and so you’ll oftentimes see these array of tuples of maybe like key value Pairs and that just represents hey these are two things that you can’t and should not change anyway so this is helpful when you’re dealing with data that needs to be secure and data that throughout your app when you’re building you shouldn’t really change honestly as far as tuples are concerned that’s all kind of you what you really need to know there are some other stuff we could do with it later but let’s not go too much more into detail on tuples let’s move on to the next data set that is one of my favorites and it exists in Python let’s go all right now let’s move on to one of my favorite data types sets they are awesome so I’m going to put that right over here and these are very very powerful I mean you learned sets you’re gonna be a pretty awesome human being okay so let’s go ahead and comment out all of this stuff don’t need uh need it anymore so I’m gonna make a comment I’m gonna learn about sets okay what’s nice about this is what if I told you hey there are two um they’re two you know let’s say databases okay database one and it has certain users emails okay or certain numbers so I’ll just say one two three four and then there’s this other one let’s actually make it a little bit more fun this is too dry so let’s say uh there’s a database of programming languages okay or we have some data of programming languages we’ll call this one and we’ll say this is two okay and then we’re gonna go ahead and say that instead of these numbers over here right we have programming languages so we have Ruby and python and JavaScript so maybe this is like right and then here we have Ruby and let’s say somebody really likes um SQL and then there’s a Java and then JavaScript okay now let me ask you this what if I wanted to create a variable called programming languages unique okay or just programming languages right and that should only be uh the unique languages okay so unique languages how can I pull the unique languages out of it can you do that can you I mean you could give it a try but it’s going to be really difficult for you to be able to do this because right now you don’t really have the tools for this right so how could you do this how could you merge these two arrays so that all the duplicates are removed so they’re no duplicates right AKA no duplicates normally yeah that’s a process it’s not that easy and then you gotta write some piece of code that can do it but I will introduce you to something called sets Okay and sets are awesome because they are Made for This their notation is Curly braces okay kind of like a dictionary and they are used for unique elements getting unique elements we’re getting unique stuff okay if that looks you know stuff is less intimidating than uh elements so I could actually throw all of these into a set and it will know what to do with it okay now first thing we’re going to do is I’m going to show you how to concatenate two arrays okay just so you can see kind of how that works so let’s call this list one and let’s call this list two okay so there’s certain things uh special keywords so special keywords hmm and they are special keywords are like you know lists is a special keyword length Max Min these are like built-in functions and methods that Java that python gives you that um that have special meaning so when you’re creating variable names try not to like call it list because I’ll overwrite this list that python comes with okay and uh set is another one is another one so there’s like a bunch of special ones and so try not to use them most of the times okay so that’s why I’m just going to say list one list two and then we’re going to say list one plus list two and if you actually do this python is so awesome they’ll actually take both of these and they’ll concatenate them together okay let’s add them together and let’s go ahead and print programming languages and I’ll hit enter and you could see that I have a concatenated list with you know Ruby python JavaScript Ruby SQL Java JavaScript right but this is still not uniques so now I’ll show you how set works okay so set works like this um you you take let’s just let’s just call set and it’s like most raw form right so and I said two comma let’s just say this and we’ll um we’ll do we’ll just print it out so if I print this out and I’ll comment this out for now if I print this out you’ll notice that I get one comma two right but if I actually do one comma two comma two and I call this notice that I don’t get the other two right and so like the duplicates are not there at all another thing you should know or is that sets are not subscriptable which means that I can’t get the zeroth element of a set okay if I do that it will give me an error it says set object is not subscriptable okay so you can’t like index or find things because it doesn’t store them with an index or a key value pair okay so there’s no get the index of zero get the index of one it’s either in the set or it’s not in the set okay so we could do things like that so for example let’s say I have uh I’ll just call this things okay and I’ll make this a little bit bigger and zoom in here so you can see better so let’s I have things right and I have one comma two comma two if I do things it’ll only show me one comma two now I’ll say two in things it’ll give me back true yes it is so in Python you have these special things called in which is a special keyword and it allows you to ask a question is the thing you’re looking for inside of what you’re where you’re looking so it’s two in things yes it is but it’s three in things well no it’s not so I get back false this is very helpful to know if something exists or not and sets are the best data structure one of the best data structures to look through um really really quickly okay so um so it just will not hold any of those values right now let’s go back to sets and let’s look at what happens if I do the following so let’s look here and I’m going to go ahead and say I have a set but I’m going to call it on an error I’m going to call it on an array okay so let’s give this a try now you’ll see this does not work and it actually gives an error so in this situation it’ll be helpful to actually call or Constructor and our function set so let’s go ahead and do set and do it like this okay and boom it has turned that array into a set okay now let’s go ahead and give it an array with duplicate items like this and call this function and you’ll notice that it’s giving me one comma two back which means that it automatically removed all of the duplicates so remember how would list one plus list two we’re already getting back our list of programming languages right like if I run this and I copy this first line all right so just make sure you copy that entire list right and then I’m going to say uh things let’s just call it things and I’m going to paste it okay so now if I do things I should see this array with or this list with duplicate iTunes right all these duplicate items JavaScript JavaScript Ruby Ruby but what what can I do to make them all unique you know this you know all the pieces of the puzzle already okay so give it a try see if you could try this okay I’ll give you five seconds here to give this a try on your own five four three two one and here we go I’m going to call my function set on this right and then I’m just going to pass it things and let’s see what happens boom look at that how freaking beautiful right so that’s all I need to do there I already know I’m concatenating that’s what I need to do right over here right because I already or sorry this line right there because I already know I’m concatenating these two because once you concatenate these two you get that big array right there and on that array we just call this function set right here so we’re going to do that we’ll just take this we’ll wrap this entire thing in and we’ll call the set function on it and once you do that you get back this beautiful right over here set that has unique values so let’s go ahead and call set on this okay and I’m going to run it okay and uh let’s comment this out and run this boom look at that so nice right python SQL Java JavaScript Ruby I’m getting back all of the programming languages uh that I wanted right all of this okay so but all the unique stuff right because we don’t want to duplicate so imagine you were Google and you wanted to have unique search queries so think about it like this let’s go to google.com and I’m just going to type in how to now how many people do you think have asked this question how to restart iPhone how to lose weight fast don’t you think it’s probably millions of people that have typed this search in how to lose weight fast or at least thousands well how come I am only seeing this one time does that mean only one person ever has searched that you already know the answer is not true for that right the answer is millions or thousands of people have searched for it so if that’s the case why am I only seeing it once Google is doing something very similar to a set and it’s going it’s taking all of the users queries right it’s making this giant list of every single query ever right and then there’s probably duplicates that exist here so lose weight fast okay another person typing in lose weight fast and that gets stored in that array another person types and lose weight fast that gets stored in the array however Google then goes and goes I’m gonna call me a set on this bad boy right and then once it does that well then you just get one query that says lose wait fast that’s it okay instead of all these other like million queries right so if we if if Google finds duplicates it it reduces them and it probably uses something like set okay they probably use something different but you get the idea okay I’m just trying to make this easy for you to understand all right don’t shoot me if I’m not completely 100 000 accurate on some of these things some of it is for entertainment okay would you rather be bored you’d rather be entertained but kind of like it okay kind of like this it works so there you go you’re understanding Google Now you can pretty much ready to create Google so you can see why I like sets so much any time you um and let me emphasize the T when I say that set okay it sounds like something else if I don’t say the T in there so I like sets a lot and their ability to give you unique data is unparalleled it’s beautiful you can’t do that that will list that easily you can’t do that with anything else that easily so I like them so anytime you want have to you don’t care about an or uh you don’t care they are ordered but anytime you don’t care about you know getting specific indices from here and you just want to check boom just pop it into a set right and uh if you just want a list of unique items and now I can actually even see this so I don’t have things defined here that’s fine so I could actually even do you know on sets I can ask hey um is SQL in programming languages and what do you think is going to say do you think it’s gonna say true or false I’ll give you three seconds for that three two one let’s see what you said I hope you said true because yes it is inside uh but what about the programming language go is that inside so what do you think I’m gonna get back for that true you can just be true or false three two one I’m gonna get a false in this case because go is not inside of programming languages but SQL is right so this is going to be true and this is going to be false okay with the capital F right there so there you go there you have sets there are really cool okay and I hope you enjoyed them and with that said let’s keep on rocking and rolling now let’s just go ahead and do one little exercise with sets before we move on to newer things I’m gonna go ahead and I will comment out all of this and we will create a new function and uh okay and so this way you can keep practicing functions as well okay so I’m going to create a function and it’s job let me actually write its documentation first and then you see if you could try to do this okay so I want to be able to call this function like this I want to call this function unique and unique should take in an array and so if I give it an array like this okay or let’s say I give it an array like this Ruby Ruby excuse that sound that is my focus app telling me hey you’re done but I’m not done I gotta keep going Ruby Ruby and python okay so when I call Unique on this I should get back what I should get back is Ruby and python okay and you know what let’s return it as a list okay so I’ll just show you like if I have a set here and it’s like one comma two one two I can call list on this and lists will convert it back into a list okay so let’s just say we we want to call Unique on a list and get the unique elements back um and yeah so that’s how it should work okay and the function should be called unique so um you know create a function unique that takes in a list okay and returns only unique items okay so that’s that’s what it does in English and then this is how you would write it this is how you would write it and call it and this is what it would return okay so I hope that this is giving you enough to write your own function so I would say take five seconds to pause this video and then go ahead and try this on your own so five four three two one all right let’s do this together if you haven’t done it yourself yet so the first thing I’m going to say is I’m going to define a function okay and this is going to be called unique and then unique will take in you know let’s say we’ll just say languages and what we’ll do is we’ll return by just calling set on those languages okay and let’s just give this a try right now so I’m going to go ahead and print and I’m going to say unique and then we’ll give unique our languages so let’s actually call it just like this I’ll copy this exact line and I will paste it okay so we’re calling unique with Ruby Ruby python and then let’s see what it says so let’s run it and it does say that we’re getting back Ruby and python so it does make it unique right that’s perfect however it’s giving me back a set and not a list you see those curly braces at the end there that represents that it’s a set okay and if it makes sense it doesn’t have key value pairs if it had key value pairs that would be a dictionary but this is a set now I want to call list on that okay so this returns this here this part that I’m highlighting that returns this guy and then if you wrap this guy and you call set on this or sorry list on this like that then that will actually convert this into a list uh into a list okay with with the items in there okay hopefully that makes sense so that’s why I wrapped it with the list let’s run this and you can see I get Ruby comma python there we go now what if we wanted to take it even further what if we wanted to use Lambda and uh convert it into or actually on honestly for this you don’t even you could use Lambda or you could just make it a one-liner like this okay but if I wanted to use Lambda how could I do it with Lambda well see if you could convert this function into a Lambda function okay so unique into Lambda give that a try I’ll give you five seconds five four three two one all right let’s do it together so I’m gonna do some callbacks and then that way we’re practicing the other stuff we learned too right we’re not just forgetting about it so I’m gonna say unique is equal to and we’ll say Lambda okay and it takes in languages so we’ll just say it takes in a list okay or languages and uh let’s set languages just like that I there’s an implicit return in here okay so now I’ll comment this out and we’ll run this and you can see I’m still getting back the correct result even though I commented this line completely out it’s working okay so there’s both ways of writing it for this function honestly I’d probably prefer to write it like this because there’s no real reason to use Lambda here okay um it’s kind of just slightly you know over a kill in this case but uh yeah so you can use that right there and you should be good and you could just call Unique anywhere you want and you’ll be good okay so I’m gonna go ahead and comment this out and we’ll move forward all right now let’s talk about one of the most fun things with programming and this is also without this programming really wouldn’t be programming okay so I had to do my due diligence and show you all the data types that exist in Python because that’s pretty important but this is my most favorite thing let’s talk about it and we got Loops okay Loop D doopy dupes bam all right these are loop-dee-doops that there are very very helpful in programming okay so I’m gonna talk about them now now if you have an array like the fruits one that we created long time ago long time ago copy and comment it out because I don’t need it up there and I’ll put it here okay and I don’t need all these other different data types because that’s just hella confusing now what if I asked you hey can you print out each of these and just show me them there one by one okay so for example what does that mean so it’s like hey print out fruits of zero I want you to print out all of them okay so that should be on its own line then get me the second one okay and then give me so yeah go ahead do this and do it manually so make this array make this list put whatever you want and it doesn’t need to be fruits as long as the string it should be fine and then just like print every single one of them out okay so right now we’re not using a loop this is the old school way of doing it manual way of doing it and look at how much fun you have to hear three go here four go here one two three four five I actually don’t need more than that one two three four five there we go let’s run this and you can see all of the items are being printed right what if I told you hey also make sure that they have like a uh you know it says fruit and then it like shows it so for example it says fruit like this all right and then it shows uh it shows that okay I don’t need a space here I can just run this so what if I said like make sure that every single one of them says fruit before it well go ahead do this manually right now okay go ahead and do this manually right now so put that here put that here put that here put that here okay now let’s run it and now you got it’s saying fruit for every single one of them okay now what if I also told you print its position out as well and let’s do that manually so we’re going to go ahead and say if uh you know what’s that thing’s index so I’m going to say 0 here when I say one here I’m going to say 2 here I’m going to say 3 here and four here right and then when we run this we can see fruit we could see all the fruits and then we could see their index okay so you could see that this is this was a tedious process not too fun and if this list keeps getting bigger and bigger oh man can you imagine the amount of manual work we’re gonna have to do is gone I’ll be insane okay it’s gonna be off to charts it’s gonna be off the GBS so if we don’t want to do all that manual work there is a solution thank you Lord and the solution is writing Loops so I’ll show you how to write a for loop I can say four fruit in Fritz print fruit and I’m gonna go ahead grab all of these lines and comment them out and run this and you can see I just got back all my fruits so that was so much easier and all by saying for fruit in fruits that’s it just this here okay so what does this mean for fruit and fruits well what’s actually happening is this fruit right here this is one arbitrary item and this is the actual list okay so this refers to this list over here and this is an arbitrary variable we could call it whatever we want I could call this X and print X and if I run this this will still work okay as long as these guys match up right that’s what’s important and so I’m basically saying hey grab this fruits array and then for each item in there give each item a name so we’ll just give this item a name fruit and the first time it’s looping through so how a for Loop works is it goes through give it a variable name of fruit okay so it goes through and the first time I’ll pick the apple and then it’ll go ahead and then it’ll go ahead and then it’ll go ahead and print that fruit out so we’ll go ahead and go okay uh it’s apple and then it’ll pop Apple up onto the screen then it’s going to go a second time and what’s going to happen is this will become fruit right over here okay I’m going to make this a little bit bigger so it’s easier for you to see this will become fruit and then it will go here and it will print it out okay then what’s going to happen the next time is it’s going to Loop through and the orange will become the new fruit right and let’s get rid of this guy right here okay and then it will go ahead and make orange the new fruit and then orange will get printed out to the screen then it will go ahead the next time it’ll Loop through it’ll make banana the new fruit okay and this will become the fruit and it will print out banana and then last but not least it’s gonna go ahead and do that one more time it’s going to make this the variable fruit and then it’s going to go ahead and print that out okay and after it does this is going to go ahead and just stop the loop because it’s going to realize I cannot go any further so it’s time for me to end the loop and then I will end it okay and then this guy over here is done so on and the code level what’s happening is these two lines will just keep running it’ll it’ll go down here it’ll be like run this then come back up then it’ll come back here and run this then it’ll come back up again and it’ll come down here and run this again then it’ll come back up then it’ll come back down here and run this again until there are no more items left and the the loop is completely done okay so that is how looping works and anytime you start to see code like this it just means somebody doesn’t know how to write a for Loop because a lot of repetitive stuff you could automate it and make your life really easy now what if I did ask you the same thing and make sure it says the word fruit before each of these fruits well you could just do that in one line right now you could just go ahead and say see if you can even try it yourself you could go ahead and just say fruit and just like this okay and run it and boom look at that it just put fruit with every single one because that’s what you asked it to do right so if we went through this iteration again you know and kind of like what the process happened but the first time this was the variable fruit right and it it said hey this is fruit and so imagine you have that Apple here right so imagine you have that Apple here and it says print the word fruit and then the actual Apple emoji and show it on on the screen and then it says go ahead and do that then it says go ahead and do that again then it says go ahead do that again and it says go ahead and do that again do that again and then you’re done okay so it goes ahead and just like keeps looping through that okay so all of those lines we got it into one now what if I actually also wanted to say the index so I wanted to say that this is the zeroth index and this is on the first index and so on and so forth just like how we did earlier how could we do that so check this out there’s something really cool that we could do in Python so I’m going to copy this list here and I’m going to put this here so you could you could see this uh better and we could interact with this better so let’s go ahead and uh zoom in here right there okay so now check this out I could call fruits obviously right you already know that but I could actually call this function enumerate and I can call this function on fruits and watch what it does let’s actually wrap it inside of lists so you can actually see it and watch what it does it actually gives you a list of tuples remember we talked about tuples and lists of tuples it gives you a list of tuples and it says that and the list of tuples basically says the first item this is item number one so remember in list you could also have different types of datas right you could have Tuple as a data type inside of a list as well so this is my first item and the first item says hey uh position of zero and it’s an apple okay so I have a tuple zero comma Apple uh string Apple okay and the second Tuple says position is one so one comma pair two comma orange three comma banana four comma so all we’re doing here is we’re just getting the position of all of these okay and we’re putting them in pairs the position of all of these right and we’re putting them in like these pairs and Tuple is a perfect data structure to use here because you don’t want a user to go here and be able to update the position of the um of the Apple because apple is always going to remain in zero okay and the pair is going to remind in one so you want it to be not updatable so this is a list of tuples and usually for storing pairs of information like it’s really good so enumerate gives me this so with the numerate I have access to when now I can Loop through and I have access to my emojis apple and my my fruits but I also have access to their positions okay so watch this I can go over here and call this enumerate function and I know that’ll give it’ll be giving me this type of uh you know pair pairs so it’s like zero comma the fruit one comma fruit so since I know uh let’s actually just run this and let’s see what we get so if I run this you could see I’m getting back fruit and I’m getting back the position and the actual fruit name right the actual fruit itself so zero comma Apple One comma pair two comma orange Etc now since I know this is a tuple so let’s go ahead and create a tuple just so you could see visualize this as well and I’ll say apple since I know it’s a tuple I can index a tuple right if I do thing of zero I’ll get back the zero that zero and if I do thing of one I’ll get back the Apple so if I want to get the Apple here I just do think fruit of one right so if I run this you could see I’m getting all the fruits okay so in other words this right here just means fruits okay now if I go okay so uh my bad so let’s just write that that right there is fruits okay and now next one I’ll say fruit of zebra okay and run this and now you can see I’m getting back all my indexes right my indices so this right over here right is the index this over here is the fruit itself so this is a fruit and this is the index now wouldn’t it be a lot cleaner If instead of writing fruit of one and fruit of zero we could just write the actual word you know fruit here and then we could actually write just index here like that would look so much cleaner you know so it’s like it’s easy to read that code well there is a way we could do that so there’s something called unpacking okay so I’ll give you an example so let’s say I have a zero here so let’s let’s go again and say I have a 0 here and I have a apple here okay now if I set this to one variable then when I obviously call that one variable like thing it’ll just refer to that Tuple so for example if I do thing it’ll refer to that Tuple however if I do two variables here so since that a thing I actually say index and then I say fruit now if I print index it’ll give me zero and if I print fruit it’ll give me this Apple so this is uh this is called the Tuple unpacking okay so it’s a little bit of more advanced concept but hey you should just learn it right Tuple unpacking okay and then there’s like all other you can unpack an array like that list like this too you can unpack other stuff but all that means it’s complicated but sounds complicated but we’re just saying hey we just like to store these in separate variables like index and fruit So then whenever you refer to index and fruit it’ll be this and then that Tuple just evaporated in this case is just gone like the actual parentheses and the Tuple object right it’s gone so I could do that here since I know that I’m going to be getting back this structure right here I could just rename it because this is what I’m going to be getting here let me just rename it I can say hey get me index comma fruit okay and now if I for example print out the index you could see I just get back the indices and if I print fruit you can see I get back the fruits okay so I’m just going to go ahead and say a fruit comma index all right there we go so this solution right over here you can see is much much much cleaner right and it um it gets us what we want but it’s just overall a much cleaner way of looping through this and getting everything that we want so you won’t all of us have to use enumerate but if in any situation you need access to your index index enumerate is going to be the way to go but the more most common way you’re going to write the loop is just going to be for fruit and fruits print fruit this is going to be the most common way you’re going to be doing things with it okay and you’re not always just going to be printing you’re obviously going to be doing stuff with it right you’re going to be uh returning thing you’re going to be uh not returning it’s not a function but if you had a function you’d return it you can keep adding items to that array with the for Loop so there’s a lot that you could do with it but this is a very basic example of you know a loop now let’s say I wanted to add 10 more apples to this fruits array how could I do that well what’s the default way we would do that it would do fruits dot append so let’s do it let’s go ahead and try this in the console right now so the normal way to do it would be let’s say I have I’m going to copy my fruits list over and paste it and I will say fruits dot append and let’s say I add another apple and let’s print out fruits again and you could see that another Apple was added well what if I want to add another Apple well then I just do that line again I’m pressing up up on my keyboard to get back my most recently input commands so if you press up on your keyboard I’ll hit enter and then I’ll get fruits right we’ll go fruits append print it out and you could see that with that technique I have successfully added a lot of apples so to order to add 10 apples I would have to keep appending many many many many many times I’ll need to append it 10 times obviously right let’s go ahead and do that manually now right so how am I going to do that I’m going to say fruits.apent and I’m going to say apple right and then I have to do this 10 times so let’s go ahead and do it one more time I think that’s 10 so let’s go one two three four five six seven eight nine ten okay that’s one extra now I can go ahead and do print fruits okay I can go ahead and print fruits and when I run this you can see that I have 10 apples added here to this list so that was a kind of an annoying way to add this many um apples to this list so how could we add them using a loop well there’s a really cool way we could do this I’m going to show you something called range so range if I do range of 50 okay and let’s do range of 10. and if you just run it you won’t really see anything it’ll just arrange like that but if I convert it to a list you can see that it just created a list on the Fly for me that goes from 0 through 10 okay but not including 10 just up to 10. so that’s what range does so this is a really nice way so for example if you had a list so I let’s say I said for I in right and I said print I this Loop will run how many times let me ask you that question how many times will this Loop run Loops will always run the same amount of times as there are items in the array that you’re running it on or the list that you’re running it on okay or whatever you’re running it on so in this case there are one two three four five items so this Loop will run five times if there were six items it would run six times if there were seven items it would run seven times you get the idea right and um and I will just be the number in this case so I will be one the first time I will be two the second time I will be three you could also just call this number so it’s not like stupid looking variable so if I run that it should print out one two three four five and I’ll comment out a bunch of the other stuff so it’s like less confusing let’s run this so I see one two three four five here okay now what if I want now another way to say this here all this is doing is saying hey run this thing five times that’s what this is saying okay run this piece of code five times so the nicer way to do this is I can just say Range Five so this will also run five times okay you can see that one two three four five right ran five times now what I want to do and I don’t even really need access to this variable so I could just call it a underscore and I can just say haha now print out haha five times because I I’m just skipping underscores basically I’m just saying uh hey I’m not really using that variable so I’m just I’m not even gonna really store it in something I’m just going to store an underscore most programmers know that if you see underscore you’re not gonna like you’re not gonna like print underscore you’re not gonna call it you usually just ignore it and so because I don’t really need access to the elements that are inside of this list so I can I’m just printing haha five times so this let me this piece of code right here is saying do this five times that’s all this is saying okay this being whatever I’m writing here it’s going to repeat that five times Well if you have this piece of knowledge how could you add 10 pieces of fruits here okay add 10 uh apples to the fruits list how could I do that how could I add it to the fruits list see if you could do this yourself and you should be able to do this yourself because you have all the pieces here to do it okay you need this line you’ll need some of these lines right so try to add it yourself I’ll give you about five seconds five four three two one hopefully you took that time to pause the video and try it yourself pause it now if you need it and uh we’ll do this together now so I’m gonna go ahead now I’m gonna say four don’t need access to this and range 10 fruits dot append Apple and I’m gonna get rid of all of these here and if you wanna see it in action you could even print out the fruits array 10 times so you could see it so let’s run this and you could see the array let me let me comment the hahasa and you could see the array getting bigger and bigger each time because it’s adding an apple to it right adding an apple to it right here right here right another apple right here another apple right here another apple right here another one right there another one right there there and then you get this okay so if I now remove this print from the loop because I don’t want to print it every single time I just want to print it once the loop is done so I’m going to put it outside of the loop and I’m just going to say print fruits and let’s run this now and you can see it added you know 10 apples at the end here okay so that’s how for loops work okay that’s that’s how they work hopefully that was a good intro for you into for Loops there’s a there’s gonna be a lot more let’s comment out everything there’s gonna be a lot more that we’re gonna talk about when it comes to for Loops because they’re pretty much going to be used everywhere okay so there’s a lot to cover with them there’s gonna be a ton of exercises we’re gonna do with lists and Loops Etc we’re going to do them soon enough uh first I want to introduce you to another type of loop as well which you won’t really use that much but it’s good to be aware of it so when you see it you know what it does and there will be times you might actually need it okay so with that said let’s go ahead and learn while loops all right now let’s talk about my friends while loops there we go all right while Loops are fun but they can be very crazy and they can be very dangerous meaning you can very easily write an infinite Loop um and so yeah you don’t want to do that okay so watch out for this I’m going to say that as the first thing because you are going to write a loop that’ll never end what I like about for Loops is they automatically end and you don’t have to have an end condition that’s nice this not so nice all right so I’ll show you an example of a while loop okay so let’s just go ahead and say here we have uh Kazi and let’s say we have Kazi and he’s currently sitting okay that’s what cause he’s doing he’s chilling and he’s sitting all right now I’ll say while qazi is not so that’s another cool thing so it’s like um so that’s how we would kind of say it in English right Kazi is not so we’ll say uh is qazi standing and we’ll do a check and it should say true and we’ll says qazi sitting okay um and sorry I’m I did not mean to just have meant to put two equal signs but that’s fine let’s just say cause these settings let’s say Kazi is sitting let’s just redefine it and I’ll say I’ll check is Causey sitting it should give me a true and I’ll say is qazi uh standing and it should give me a false because we have defined cause you to be sitting so now I can write a while loop very easily I could say while cozy okay is not so there’s also not okay this is a very important operator okay so for example I can say quasi is standing that’ll give me false right but if I say cos is not standing that should give me a true just like that okay so I’ll say while quasi is not standing we will say and when you hit enter and it shows the three dots like that I’m in the console right now by the way okay so just writing the console I’ll give one space here okay or we give two spaces that’s fine as long as you indent some amount and I’ll say print get your fat butt up okay let’s there now I’m not going to hit enter yet but this is a while loop and it’ll automatically start running so it’s like while kazi’s not standing print get your fat butt up and you’ll notice that right now cause he’s not standing right and so there’s never a condition where qazi will be standing that means this Loop will keep running for ever AKA infinite Loop and now I’ll hit enter and I’ll hit enter again and watch this it just keeps on keeping on right it just keeps on running over and over and over and over and over again I mean this has ran thousands of times to stop this hold control and then press C on Windows probably hold something and press C control probably or option uh not you guys don’t have option uh something alt okay just look it up and that should kill your command line so if we were to write that in our program our program we just keep looping just like this infinitely and that’s not what we want we don’t want to create an infinite Loop okay let me just see the status of qazi and it says cause he’s sitting so what I’ll do instead is I’ll say maybe after 10 times it’s printed out we can say causes then standing after 10 times okay so in order to do that okay I’ll need to teach you how to create a breaking break condition to end a while loop okay so let’s set a counter and we will say the counter is zero okay and then when that counter Hits 10 we will break out of our Loop so let’s try this I’ll say while counter is less than 10. and we’ll say put a space here and we’ll say print counter okay and then what we’re gonna do in the second line here before hitting enter one more time is we’re going to go ahead and we’re gonna increment counter so I’m going to say counter is equal to whatever counter previously was plus one so I have defined counter here as zero so this is incrementing it by one so we’re taking this definition of counter at zero and then I’m redefining counter here to be whatever it previously was plus one so if you follow the logic of this Loop what’s happening is the first time I Loop through this it says while counter is less than 10. so I go while zero is less than 10 that statement evaluates to True since that evaluates to true because it needs a conditional one set of values to true it runs the entire while loop block so it prints out zero so then it spits out boom you get zero here and then it redefines counter to be whatever it previously was plus one which means the new definition of counter is gonna be one okay then the loop runs the second time and it goes while one is less than ten okay so the second time it runs it goes one is less than 10. okay that’s what it checks so let me actually write this together so while one is less than 10 this evaluates to a true okay and then it runs again what the heck happened there okay this evaluates to a true all right and then it prints out one and then it sets the counter to whatever it previously was in this case it was previously one uh and then one plus one is going to be 2. okay now counter is set to two okay and then this so the second time we Loop through this is two the third time we Loop through it’s three the fourth time fifth times six times seven times eight times ninth time and then when we go the tenth time through the loop what actually happens is this statement over here is not true anymore it goes while 10 is less than 10. let me ask you a question is 10 less than 10 no 10 is not less than 10. so that statement becomes false and when stat becomes false everything underneath it does not run so it does not run the block of code and it finally comes to a stop so let’s try this and I’ll hit enter and you could see it ran from zero through nine and then boom went through nine right so that’s how a while loop works now I’m gonna zoom out of here okay and then we’re gonna go ahead and write this down into our uh into here okay so now I’m going to say cozy let’s set him to sitting okay and um while cozy is not standing we will tell we’ll keep telling him get your fat butt up and then another loop another loop we wrote was this counter one and then we said while counter is less than 10 so this will run 10 times but not because of that but because we’re in an increment counter counter equals counter plus one now another way to write it which is very common is we’ll see counter plus equals one okay that’s the same way of writing that statement and then here’s another writing that same statement we can say counter plus plus they’re both the same statement and I can prove to you in the command line over here let’s say we have count we set it to zero if I do count plus plus count plus plus oh actually never mind I take that back python does not have that so let’s try count equals zero and let’s go count plus equals one and now let’s go count boom we get back one okay so ignore that I said that that was wrong okay and count because in JavaScript which I’ve been using a lot of lately right that has plus plus and other languages like Java itself has it and I’m sure C plus plus and other languages probably have it too so count plus equals one will increment it by one and so will count equals count plus one and now I’m gonna go count it’s two now okay let’s go count plus equals one let’s print count we get back three and you get the idea so that’s how you can increment it so we’re going to go ahead and increment it like this counter Plus equal one okay and then print counter and uh I’m gonna comment this line out so it doesn’t put me in in an infinite Loop I’ll comment all of that out and let’s run this okay and you could see I get zero one two three four five six seven eight nine okay I get all the way up to nine so that’s how while while loops work and that’s how you have to end them so make sure you always have a statement that allows you to break them majority of the times though you won’t really need to use while loops and I tend to avoid them as much as I can because it makes code look not as clean uh not pretty there are some times you need to use them like if we’re creating a game and that game just needs to be running infinitely long meaning the user should be able to play it the entire time yeah that you’ll probably have to put in a while loop so there’s certain situations where a while loop does make sense but most situations like this one I would just do with a for Loop it’d be much easier to do this with the for Loop okay but this pattern here will be important like setting a variable outside of the loop and then incrementing it through the loop uh that pattern is important so don’t forget that and don’t make this mistake of putting the counter inside of the loop because you keep setting it to zero over and over and over again so just max it’ll increment to two is one because you’ll increment it to one then you’ll come down and you’ll set it back to zero then it’ll do zero plus one and you’ll get one go against zero one so you wanna put the initial one outside of the loop outside the scope of the loop and then you just keep incrementing that one you want to repeat the amount of times you’re incrementing it by one you don’t want to repeat the amount of times you’re setting it to zero you only want to set it to zero once okay that’s basically what I’m trying to say all right so there you go those are that’s an intro to while loops with that said I’m gonna go ahead and comment this out and we’ll move on to doing some interesting fun exercises so let’s go ahead and do that now all right let’s do some practice exercises with everything that we’ve learned so let’s just start with lists okay so what if I have numbers okay and I want to double these numbers so basically what if I want you to write something that when I call it let’s say I call it double and I give it this array here okay it returns Back to Me 2 4 6. eight and ten okay so if I call that it should double everything up and spit that back out to me how could I do that how could I create something that does that well let’s go ahead and let’s create uh a function and we’ll call it double okay and let’s say it takes in some argument numbers okay I’ll remove this numbers here for now because you should immediately know that this is a function right and this is a function call and it’s taking in an array okay or a list rather so I could give it a list type so let’s actually see is it list or is it list yeah it lists with a lower case is going to be fine so I could do add type hinting in here as well and I could say that it returns a list okay so double is the numbers takes in a list Returns the list you don’t need to add in the type hinting but let’s just practice everything that we’ve learned so far okay it’s not going to really change the code in any way if you remove this it’s it’s not right the code is going to remain the same but I like to just leave this here for now okay and now in order to double this list of numbers that we’re getting well how can we do that so what I want you to do is try see if you could give this a try and do this yourself you have most of the puzzle pieces to be able to do this yourself it would be a little challenging to do it said we had difficulty of six or seven right now because there’ll be a lot that you’d have to figure out okay but take about five seconds right now to pause the video five four three two one all right great let’s do this together so the first thing I’m gonna do is I’m gonna create a um an empty array here that’s gonna that’s gonna store all of my values okay and now the pattern I’m going to use is I’m going to keep appending to this list okay so I’m going to make this list here and I’m going to keep appending to this list right here so let’s write our Loop and I will say for numbers for number and numbers for now let’s just say print number okay if I run my code nothing should come out right nothing should work here hope make sure everything that you’ve written so far is commented out and now I’ll call this function double and we’ll give it an array so I’ll say one two three and we’ll give it a list one two three and you could see it’s printing them out so I’m calling the function and the function is working as expected great okay this function I’m happy that this function is working as I want it to work now what I want to do is I wanna append it to this array so let’s try that so I’m going to say result dot append and I’ll just keep adding those numbers so let’s go ahead and do print result and the result remember I’m printing it outside of the loop if I put it inside of the loop it’ll print it out every single time right it’s a pending so one the first time okay so if I do this and I run this the first time you’ll see was one then it was one comma two then one two three okay if I put it in the most accurate place I would be right here and you could see it starts off as empty then it goes to one and it goes to one comma two right and then we will print the final one so let’s run this you can see it starts off empty then prints out then appends one to that array then it appends a two to data rate then it depends a three to that list right so again I’m going to be using arrays interchangeably right because I’ve used a lot of JavaScript so lists whenever I say array just think list okay so I have this result list and I just keep appending to that list again and again well if I want to double the numbers at this point it’s very simple I just append double that number okay and so let’s do this you should see this so like first iteration I go through is empty then the first iteration I go through I append this okay I appended two then the next iteration I go through I append A4 and then I append a six and every single time it just keeps doubling okay now what I want to do at the end is I don’t need it to say print and here I just needed to say return so I can say return result okay return result and now if I run this you’ll see nothing so we will have to do print and now we will see the final result it’s two four six okay so now it’s working so this is how you can uh double a list okay if I’ve given you a list you can double it this is a very common pattern so remember this pattern and like practice it and write this code and write it again from scratch but this is how it works you have a result you you create a empty array then you keep appending to that array and then you you keep a pen you create an empty list you keep appending to that list and then you return that list okay that’s how that works so very common pattern okay and let me actually even write it down very common pattern create empty list Loop through and append to that list return that list okay this is a very common um pattern I don’t know if it has a name or not but this is but this is a common pattern so just remember that this is something that you do okay so just take note of it memorize it this you’re gonna be doing a lot of this in your code okay so it’s like if you wanna you know if I go back to Netflix and we have a list of movies here we have a list of movies right so maybe I have a list that’s called trending now trending now list right and the start that list is empty trending now and then I keep looping through and I keep putting items inside of that so I keep looping through just like for a movie like if I go to Netflix here right the loop that’s written here and the trending now is pretty much like very simple it’s it’s basically for movie in trending movies and then it’d be like print or output somehow movie okay and then it like shows you each of those movies it just Loops through and makes puts this here Loops through puts this here Loops through put this here Loop screw puts this here so that’s effectively what’s happening inside of that Loop oops well we’re already shooting the Python tutorial but thank you for the reminder appreciate it so yeah you want to Loop through and you keep uh pending and that’s what’s even on Netflix did they make a list they keep appending through to that list then they append all the trending now movies and then they put it in the list and then show it okay so that’s yeah long um explanation of this but I think I just want to drill that point home in in multiple different ways now there are a lot of other cool things that lists allow us to do too for example before there were there’s a method there’s some methods that we have that we could call on strings okay and uh lists can help us with that so I’ll give you an example let’s create a new function so I’m going to comment out this print call right here and we’ll create a new function and I’ll call this function I’ll just write the documentation for it real quick and it’ll be count Words count Words okay this is this is how I’m going to call the function and this function I’m going to give it a string like hi my name is qazi okay or let’s remove any kind of special characters for now and so I’ll just say hi my name is qazi and when I call this function what it should return to me is it should say one two three four five so there’s five words should return a five so count Words function should count Words should count um if given a string should count and return number of words okay so that’s the function count Words that’s what it should do so I know it takes in a string and then it returns a number so if I want to create this here’s what I’m going to do I’m going to create a function we’ll call it count Words it will take in some input from the user so we’ll say you know phrase from the user and based on that phrase that phrase will be a string so I can just put that in the documentation so phrase is going to be a string okay uh just a little note so you could create a type hint here if you want you could go it’ll be a phrase will be a string and then we return an integer okay so you could do that but I’m going to remove this one okay so I’m just going to keep it like a basic one right now all right and let’s go ahead and just print out the phrase for now let’s call this function and I’ll say print I don’t need to say print here I’ll just say count Words and I’ll say hi my name is qazi and let’s run this and you should just see hi my name is Kazi here for now right there right so that’s good that’s that’s really good for now the next thing I want to do is I want to find out well how long how how many words are here how do I find that out so check this out there’s something really cool that we could do with strings so I’m going to zoom in here so you could see better and there’s something awesome that we could actually do with strings okay and what I could do with strings is I could go hi my name and you could call this method it’s called dot split okay and if I do a DOT split on a string it will actually split that string based on um empty spaces so wherever it sees empty spaces it just removes them and and then it turns the rest of it into a list like that okay for example imagine I had high my name is okay or imagine I have a CSV file like an Excel file maybe if you’ve ever seen one like that and it says age comma gender comma height comma sex right if I if I have this well and I and I have it as a long string if I want to get a gender height sex I want to get all those headers and attributes and store it in a list I can if I call Dot split here well that won’t work because it’s still putting the commas in here and it’s just giving me back one string that’s not what I want so what I could do is I could split it by commas I could tell it splitted by commas once I do once I do that it actually removes the columns from the string so then I have a gender height sex and as four separate attributes so it’s four separate properties right and that’s what I that’s more of what I want so split is a really cool method and when you call it on a string that has spaces what you’re doing is you’re basically saying splitted by empty spaces just like this when I run this this will work now notice that this line over here dot split and then dot split with empty spaces Returns the same result interesting right they’re returning the same result right here because by default if you actually don’t pass anything into this dot split method on a string by default it will it will put this I’ll assume you’re you’re asking for an empty string Okay so we know that we could call the function Len to find out the length of our list right so if I can actually take these words and I can split them well then I can actually find out the length of my string right or that phrase that I passed it so zoom out here so see if you could actually see if you could give that a try all right and see if you could make this function work I think at this point you have enough information to be able to put this together if not that’s all good too so I’ll give you five seconds to pause five four three two one time all right let’s go ahead and do this together if you were able to do it great if you’re not that’s okay too we’ll now do this together so here’s what I want to do I’m gonna take my phrase and I’m gonna split it okay and then let’s just print uh let’s print [Music] um this out and I’ll remove this here so let’s run this and when I give it High see this line over here it’s saying hi my name is uh cozy right and boom it’s showing me all this stuff there and everything is split boom hi my name is cozy so I already know well then what can I do actually well I can actually call the function Len on that so for example phrase.split gives me that and then what I’ll do essentially is I’ll just call the function I’ll wrap this and I’ll just call the function Len on it that’s what I’m doing here so I’m just taking this right boom because that’s where that’s going right there and then I’m calling the function Len on it so it’ll just give me the length of that okay so let’s go ahead and run this and there we go count Words is five so count Words now I can remove this return uh this print and write return right there and I could actually turn it into a one line solution so let’s go ahead and print and you can see count Words is a very simple one-liner solution and um yeah I can I can type in anything here cookies cream cream soda legendary okay so if I keep giving it words it keeps counting it and accurately so that’s that’s how count Words works if you were able to get it great if not that’s all right and uh hopefully that gave you some more exercises with like and and methods that you could actually use with lists now I want to show you something very very very helpful to help you visualize for Loops or any type of Loops in general and this is a debugger that’s actually built into replit and this is fantastic this is a fantastic debugger okay so debugging is a very useful skill and it’ll help you a lot so what I’m going to do is um go ahead and uncomment this line out where we were printing double okay and let’s comment let’s comment this out as well okay and what I’ll do is I’ll put a break point here so if you see this blue thing that I just added right next to it you can do that by clicking here and unclicking here okay so just click right there and then get that blue point to show up okay so once you do that I’m going to go ahead and click this that’s the debugger get that icon right there so let’s go ahead and click the debugger and now you could see that it’s currently saying uh let me make this let me do this so maybe the debugger looks a little bit bigger okay so there we go so it tells you the breaking point is on line 395. right it’s this right here because that’s where we’re actually calling this double function from so it’s a good idea to put the breaking point there now if I hit run on this it runs from right here okay and then there’s already a couple of things here so it knows about special variables and it knows about function variables so special variables don’t touch them that just ignore them they come with python so ignore this function variables you can pretty much ignore that too but all this is saying is hey since you defined all of these functions and we didn’t comment them out right we have a ton of functions defined but just not commented out so if I keep going up you’ll see introducer and all kinds of functions at the top that are that are defined and just not commented out right so let’s go back down so that’s what it’s noticing so ignore that too for now okay ignore that too now hit this next step right over here Next Step so the first line of the code what it’s saying is we are given numbers and numbers are one two three because the user passed it in so it already knows that this numbers over here is one two three at the moment okay that’s all it knows because it’s on that line right now now let’s run another line so it’s on this result line now now when I run the result line notice that it now knows a result is a variable that’s instantiated as an empty empty list now it’s telling me that it’s going to run this line over here so let’s hit next on this next step and now it says hey I know what numbers is and I know what result is already but now I know that number is an integer and it’s one right it even knows their data types lists lists and integer so it goes hey I know what number is at this point because the first at the first Loop number is going to be a one then hit next again okay and now it can tell you hey I just ran this line over here and this is what happened result is a list now that list is not empty anymore it has two inside of it okay great I’m going to run line 390 again that’s why it’s grayed out right so let’s run line 390 again boom and it says Hey number is now two run it again and it goes boom I just added four to the result okay let’s run it one more time it goes hey I know that number is three so number is 3 here so it’s going to be basically doing 3 multiplied by two and it’s going to take six and append it to result and so after this line This result over here should have a six so let’s give that a try and boom you can see that we have that six right there okay let’s run it again and now it’s going to that line return result finally so let’s go ahead and run it again right it did that and then now it’s going to the print line and it just printed out okay so that was a full step-by-step line by line exactly everything that happened and uh yeah so debugger can be very helpful uh in order for you to like find out what your code is doing okay so I I would highly recommend any part specialty loops and then to turn it off just hit this stop okay so you can turn it off but for visualizing Loops very very very helpful okay very helpful all right now we’ll do some more practice still with lists because this is going to be key okay so we’re going to just keep sharpening this sword now let’s say that I have um okay so let’s say this right if I call a function uh and we’ll call it some list okay if I call this function and I give it one two three it should return back to me is six so what’s happening here I’m summing up this entire list okay create a function that can that given a list of numbers it can return their sum so we’re going to go ahead and create this function how do we do that how would we do something along those lines so think about this there was a pattern there’s a couple of patterns we’ve used in the past so I would say that if you give this a try right now it’d be of a difficulty seven out of a ten and you could do this because you have all the tools available so what I’m going to tell you is that it’s gonna use this counter pattern that we had created earlier it’s going to use similar to this pattern right here okay where we did Count plus equals one but instead of count plus equals one you’re going to do plus equals something else so you could give that a try I’ll give you five seconds it’d be challenging and then we’ll do this together five four three two one okay let’s go ahead and do this together now so obviously I Know This Much the function is going to be called some list and it’s going to take in some numbers okay now I know that that’s going to be a list data type and I can further type hinted to say list of integers okay so again we don’t need to do this and I know it’ll return an integer okay this much I know about this function so I can get rid of it for now just so I can keep this simple all right so some list is going to take it numbers and it’s going to sum up all of the numbers and then return them so the first thing I’m going to do is I’m going to uh say count and I’m going to set it to zero then what I’ll do is I’ll Loop through these numbers okay so I’ll say for number in numbers and let’s actually comment this out for now and let’s actually try this manually first I think that’ll really help you so let’s try how would we do this manually if I gave you numbers and they were one two three and four how would you do this manually well you’d first go through and you would do you know numbers uh uh you would create you know account and you set it to zero and then you’d basically go through and you say okay I’m going to take counts and I’m gonna plus equals it by numbers of zero okay so that by doing that I’m incrementing count by that right so for example let’s go ahead and put it in the console here so we could visualize this better so I’m going to set count to zero and I’ll set numbers to this okay now I’ll increment it by the first element in that array I’ll increment count and let’s go ahead and see count so count is now one now I’ll increment count by the second element in the right which is the two now count should be three then I’ll increment count by the third element in the array which is the three show the count should be six and then I’ll increment it by the fourth element in the list and then let’s go ahead count and you could see count says 10. okay that’s the pattern that I would have so in effect I’ll be writing this and this and this multiple times copying pasting doing it by hand and then I’ll print out my my account and that will be the the sum right okay so let’s do it in a loop now so we’ll set for number in numbers and we’ll say count and we’ll just increment it by the number and then at the end we’ll just say return count okay now let’s go ahead and give this a try so I’m going to go ahead and say print some list and we’ll give it one two three and let’s see what happens boom there you go that worked Let’s uh comment out any other print statements we have let’s comment this out right here let’s run it again you can see we’re getting back six now what if I put a 4 in there as well comma 4 that should get me to a 10. and yep I see a six and I see a 10. so my sum function is working correctly my sum list function so let’s go here so that was yet another practice for you to do okay and we’re gonna do you know I would say let’s at least do one more practice okay so this is good practice for us but loops and loops and lists combined there’s so much you could do with coding with that so that’s why you want to keep drilling those practice skills and getting better at it okay now I’m going to comment out both of these and now what we’ll do is we’ll say We’ll create a find Max okay and find Max if you try to do it yourself that would be a challenge of 9 or 10 difficulty because you don’t necessarily have full on you kind of have the tools to do this but it’s it’s but it’s a difficult one if you want to do it by yourself it’ll be a pretty difficult one okay so we want to make this one fine Max that finds a Max from a list so for example if I give it a list of one two three four it finds this so let’s go ahead and create comments for this I’m going to say find Max okay and then I will give it one five ten three if I do that it should return 10 that’s what find Max should do okay well how are we how on Earth are we going to make a function that can go ahead and do this so if you want to give it a try pause the video now give it a try and otherwise let’s keep going okay so I’m going to go ahead and create a function it’s going to obviously be called find Max okay find Max and then it’s going to take in you know a list of integers and I don’t need to type hints okay but I’ll just go here it’s going to take in a list of numbers you could add in type hinting after you’re done writing your entire function that way it doesn’t take up cognitive load mentally from you but it makes your code more readable when looking at it later it’s going to take in numbers and it’s going to find the max well how I want to do this is I want to set the current Max okay and I’ll set the current Max to the first item inside of that list okay so if I was to do this by hand I’d probably have current Max and I’ll set that set that to the first element in that list right so if you gave me a list and you said hey Kazi you know here are the numbers and you said there are 1 5 10 comma three just like that well I would probably set the current Max to 1. okay or in other words I would set it to numbers of zero okay the first element of that list and then what I would do is I would go because we’re assuming this is a really large list right because if it was a really really large list you couldn’t just you wouldn’t just be able to eyeball it is what I’m saying Okay so let’s say this was like a really large list and there was like some some Max number in there you wouldn’t just be able to eyeball it’d be too hard okay so I’m gonna command Z this and make this back to the small list that it was right because that’s usually the real data you’re working with real data is like massive right here we’re just using some dummy variables dummy lists making them small so we can conceptually understand what’s happening okay so I would have this and I’ll set the current Max to this then what I would do is I would just go through each of these items and I would compare this and this and if I found out that this item was greater than this item this number right here I would replace current Max with this new current Max and then I would compare right then you would compare this and this and if this was greater than the current Max uh then I would set this to the current Max and then I would compare this against the current Max and if this was greater than the current Max and it set at the great current Max otherwise I would just Skip and ignore and keep moving okay that’s the pattern so basically I would check you know uh through the through the loop I would say hey is oh let’s just start with that so I’ll say is um and I’m just writing this in English for now one greater than current Max and then it would say the answer is no so then I just continue moving and I would say is in the second Loop I’d say is 5 greater than current Max what I’m writing here is called pseudocode it’s helpful to write code in English like this because you can before you end up writing actual code you can just visualize what the heck is happening and then you could syntactically turn it into actual code and it’s an easy way of doing it it’s five greater than current Max the answer is yes since the answer is yes I’ll say current Max is now equal to five okay then I’ll go through again I’ll say current Max then I’ll say is 10 greater than current Max and since the answer is yes I will say current Max is equal to 10. okay here we’ll just say go to next number okay that’s what’s happening here so if it’s no go to next number and then I’ll go is three the last item in the loop greater than current max if that’s that’s a no okay well then just um and the loop because we know we’re at the last number and it will automatically end the loop so that’s really the algorithm that we would write now I’m going to go ahead and comment all of this out so the pseudo code is there and we can use the pseudo code to create our real code okay all right so let’s go boom and I will say for number and numbers if number greater than current Max well then set the current Max to the number to whatever number is and that’s really it I don’t really need to do anything else and then once we’re done with the loop just return current Max okay and um yeah let’s give it a try because that’s essentially the logic so let’s go ahead and say find Max and I’ll give it one two three ten Seventeen four six five six let’s run this and look at that it found the current Max so damn damn fast right now if I copy this and I just paste it a couple of times and we make we put a current Max like here of 19. let’s see if we could find it it found it instantly right it found it really really fast okay now I’ll make it smaller there we go and boom yeah it works every single time so that’s how you would write it okay so again you saw that we had to work with lists here we had to do loops we have to do if else conditions so we had a for loop with the if else condition so that’s probably something new you know that you might not have seen before so the the reason why you want to set it to the first element and you don’t want to set current Max to be zero or something like that is if you did set it to zero and I tricked you and I just gave you like all negative numbers your when I run find Max you’re going to return back a zero but like there is no zero in this list right so you want to set it as the first element of that list so you so instead I should have gone back like you know negative one because that’s the biggest number out of all of these other numbers here so that’s why numbers of zero is the way to do this okay so hopefully that gave you some solid practice with using lists all right so now since we did a little bit of practice with lists let’s do some practice with dictionaries okay so dictionary practice um and just again just a friendly reminder you should be coding the entire time in maine.py since I’m Kazi I’m coding in here and at any time if you need notes or everything you’re probably writing or will write is all in here okay everything that I’m writing will be made available to you in quasi.py so you could just go there and get access to all of this code okay just in case you couldn’t follow along or anything for that reason okay so with dictionary um with dictionary practice what we’re going to do is we’re going to create a function and we’ll call this word frequency and what this will do is it will take in a string so let’s say I say I love Batman because I am Batman so weird string weird phrase to say so I’ll take in a phrase which will be of type string and you’ll notice that it is repeating certain words uh certain words a few times so let’s go here and which this should return is a is a dictionary and that dictionary should basically say the word I should be the key okay I and it should say since it I happens twice it should say two times and then since love happens only one time it should say love is one since Batman happened twice it should go Batman two and then I is already done so if we ignore that and then m happens once okay so if you call word frequency on this I love Batman because I am Batman this is what it should actually return so what this is doing is it’s telling you the frequency of uh whatever it is you’re doing okay so let’s go ahead and create this function so we’re going to say Define word frequency frequency and it’ll take in a phrase okay and then it’ll give us this guy over here now the first thing I notice when I do this is I imagine usually with the string by itself there’s not really much you could do with it so I would want to turn this into a list that’s probably the first thing I’m thinking okay so let’s go ahead and do that so I’ll do phrase and it’s all of these words are separated by a space right so I can actually just do phrase phrase dot split by empty space or just remove this and by default it’s actually putting that and let’s go ahead and print this all right and we’ll call this function now word frequency frequency and the phrase will give it is this I love Batman because I am Batman so let’s give it that okay and let’s run our code and comment everything else out in the meantime Okay so yeah you see it turned it into a array or a list right and we have these inside of the list now this is where a dictionary is going to be really helpful to keep track of all of this because dictionary can have only unique keys so if I appears twice in a dictionary it’ll only appear once so what I want to do is here’s how I would do it if I kind of did it manually okay so I’d kind of create a dictionary I’d create an empty dictionary okay so I’ll just say result or something like that and then I would Loop through this list over here okay which is which has all the words in it and I’d say something along the lines of like okay if I get I um the first question I’ll ask is is I in result okay I’ll look inside of this dictionary and see if if it’s already there if I is not end result because they’ll say no in the start then what we’ll do is I’ll say okay set create a new key value pair okay and we’ll say set the key value to I and sorry set the key to I and the value to zero okay so basically in other words I’m gonna do this I and I’ll say zero because it didn’t exist so this is this is just Loop Number One okay Loop one then we’ll Loop through uh the second time right we’ll go again we’ll go to the second iteration of the for Loop and we’ll be on this word love because we’ll do something like forward in words you know or for word and phrase or whatever this whatever we’re going to call this list over here so for word and words let’s just say the first time word is going to be I the second time the word is going to be love so I’m going to say is love in result the answer is no well then we’re just going to follow the same logic and we’ll say set the key to I and value to zero okay and I’ll remove this and because it’s the same thing so like set the key to I and value 2 or in this case we’ll set the key to love and value to zero because this is and I apologize the value should be one because this represents that it’s been found one time so there we go one so it’s like hey I’ve this is this I has been found one time so I’ll set it to one not zero then we’ll loop again so I’ll copy paste this format and we’ll loop again the third time and we’ll find the word Batman is Batman and the result the answer is no since the answer is no we’ll set that to one this is the third Loop then we’ll Loop through the fourth time and we’ll find the word because and since this is a no we’ll set the key to and value to one let’s Loop through again and now we get to kind of the interesting part and we get to the I is I in result the answer is yes and so now we won’t set a new key because the key is already there we’ll say increment the value of of result of um I buy one okay so we’ll increment the result increment the value of that key okay or in in English I’ll say of this key of the I key by 1. okay then we’ll Loop through it again we’ll what will we find now you help me with this okay what will we find what’s the next word I love Batman because I and what’s the word we’re going to use here am right so this is going to be m then is m in this result dictionary yes or no it’s not um and by the way I wasn’t updating this dictionary but this dictionary would be getting updated the entire time right so so result to one and then we’ll set um Batman to one as well right after this and then four would do we’ll say because whoops we’ll say because and it’s because I don’t want this running uh along the entire line I will do this so you could see key value pairs on every single line so I said because we’ll set that to one and then it’ll bring us to I again and we’ll say I because I and now we’ll increment the value of this I to 2 instead of actually adding a new key okay and then we’ll say am and am is that in the result uh dictionary yes or no no right it’s not so it’s we’re going to say no we’re going to increment the value of uh we’re not going to increment the value we’re going to set the key to uh to M and value to 1. okay so we’re going to go ahead in here we’re going to say key I’m sorry not key but M and we’ll say one okay and then the last Loop that we’re gonna do is going to be the increment Loops I’ll copy this Loop seven and is Batman and result the answer is yes increment the value of Batman Key by one so since we’re going to do that we won’t add a new key we’ll go to the existing Batman and we’ll increment it and we’ll put it as two okay so that’s how we would do it manually if we were doing this all right now let’s go ahead and turn all of this into code so first up I’m going to comment this entire thing out and then we’re going to go ahead and write the code so I’m going to say Define and I think I have my function already here so I’ll actually just okay let’s uh let’s write up top let’s write up top because Bunch everything is already there so let’s write our function here okay we’re going to follow that same logic so in the start I’m going to create an empty dictionary okay it’s just going to be empty there’s going to be nothing in there right now um and if you create empty squigglies you might think this is a set but no this is going to be a dictionary and you can check it here you can say type of empty squigglies and you will see these curly braces or squigglys or whatever you want to call it class is dictionary so it assumes it’s a dictionary instead of an empty set okay and that’s what we want we want a dictionary okay so um result is this for now then I’m going to Loop through okay we’ll say this split is just words right because I get back a list of all the words and I’ll say for word and words if word in result if the word is already in result in that case what I want you to do is incremented by one okay or in other words if we follow our logic I’ll say if the word is not in result then I want you to set the key okay so how we’re going to set the key and I’m going to say result and we’ll create a new key and the key will be titled that specific word and also set the key to one otherwise else if it is inside a result if it is found well then we’ll just increment result um buy one okay and then at the end let’s go ahead and return result okay and uh let’s go ahead and run this and of course we forgot to call Print here that’s why it didn’t show so let’s go ahead and print and run this all right there we go we’re getting back exactly this right over here you can see I is two loves one Batman is two because oh my bad should have said because one right because is one and then m is one right there okay so this works perfectly so hopefully this was not too overwhelming or too confusing because we’re just building on top of you know essentially every single step that we had previously done now if we want to make the input uh Dynamic for the user we could go ahead and say word frequency and then let’s say that it takes an input from a user please enter your phrase okay now if I run this and I comment this out it’s going to ask me right here please enter your phrase and let’s go ahead and practice this so I’ll I’ll go ahead and enter my phrase here and I’ll say I like to eat cookies boom I like to eat cookies right let’s run let’s run it again let’s say cookies cookies cookies cookies I am a bookie bookie bookie bookie let’s run that hit enter and cookies it happened three times um cookies with a comma happen one time right because we haven’t we haven’t taught it yet how to care about special characters like a comma so that is fine because cookies comma is as far as I’m concerned it’s a brand new word I am a right bookie right bookie showed up four times so very nice very nice it works perfectly so you can make it Dynamic by asking it the input um from the user okay so that’s uh that’s it for now in terms of practicing with dictionaries now we’re going to go ahead and move on to some really big boy and big girl stuff which is going to be higher order functions and then after that we’re going to step it up a notch and it’s gonna be awesome okay I’m not even gonna tell you it’s gonna be a secret you’re gonna be in for a surprise so with that said I’m really excited can’t wait to start talking to you about this stuff all right I hope you’re ready to talk about higher order functions this is scary scary stuff but don’t worry it’s not that scary actually we’ll walk through it together okay um so cool let’s go ahead and comment out every single line so when you run your code it should be empty nothing should be uh printing out okay now I’m going to talk about this higher order functions okay big big words but what what are we really Gonna Learn here and what we’re gonna talk about I’m just gonna show you two for now map and filter okay because those are really the only two ones you’ll kind of need um and then after that I’ll show you something and you won’t even need this anymore okay but this is gonna be the building block for the next thing that we’re gonna do so let’s just start off with this so remember that function that we wrote that doubled things so we wrote this function and uh you could double right you call Double on one two three and you would get back one sorry two four six well here’s a nicer way of doing it we can actually use the map method okay and whenever you run this map function what’s nice about this map function is at the end it will always return a brand new array now if I want to use map to make this happen over here I need to give map a function okay and what map does is map goes and gets this guy and gets this guy and gets this guy and it has the power to get this guy and do something with him get this guy do something with him get get this guy and do something with him and this is something you would normally write a for Loop for and loop through and do all the stuff map you can kind of do it in one line and I want to call map right and I want to be able to call it and map is a function and it takes in another function as an input so you’re going to put another function in here okay so for example I’ll put let’s say I get I create a function you know our function double uh does what it goes through if I look at it uh it goes through and it doubles the numbers okay so let’s go and create a function here so you could see it and I’ll just say double number so this function is going to be a lot simpler it’s going to expect some kind of number and it will return that number multiplied by two okay that’s it so I’m gonna go ahead and give the double number function to map so it’s taking in a function okay so I’m not giving a function call like this I am literally giving it the definition of that function okay for example if I did from qazi you can do from import from Main import blah blah or you could just do import double number so I’ll do from causing import double number okay so I had some errors in my thing so I just refreshed everything and uh yeah now if you if I run it from quasi like for some reason sometimes I’ll give an error so just like run I guess double number first if you’re getting an error where it’s not recognizing what it is so just like run it like this first and once you run it like this now try doing from cozy in your case from Main from qazi import double number okay if you’re in Maine you could probably also just do import double number that should work too and now if I do double number and I call something call um call it with two I get back four so like I can see that double number function works as expected so what I want to do is I want to apply this double number function on everything on this one here on this two here and on this three here okay in place update these in place and then return like a new array so basically I’m not going to be changing the actual list that the user gave me but I’m going to be making the copy of that list and then you know essentially changing that you could think of it like that okay so that way the actual data that the user gave doesn’t doesn’t get lost okay so I’m going to say map double number give it that and then also now it needs an array so I’ll give it an array of one two three okay now if I tried to print this you’re gonna see that I’m going to get something a little weird it says Hey map object at blah blah blah blah bloop okay so it’s an iterable object and in order to show it and have it reveal itself in other words we’re gonna have to call list on it so list will turn it into a list and you can see boom that worked we’ve doubled everything right two four six so we gave it a function and we gave it a list okay so we just said hey we gave it a double number function and we gave it a list and said do that to this list and now we could essentially write that entire thing in one line and if I actually if we go and look at our double number right and I’m going to put it underneath this and I’ll just comment everything out honestly this over here looks a lot more complicated than this over here because it’s one line okay so if we were to turn double this into a function so if I said define you know um and we call this yeah you don’t even need to turn it into a function right that’s the whole point because you could just write it in one line it wouldn’t really make sense to even honestly make it into a function you should just call map right there call whatever you need to do and do it right there now here’s another reason why I like it you can create this double number function on the fly without even creating this function here so in other words I can actually do this okay I can say instead of this I can actually make a function on the Fly and I can say Lambda numb and just take that num and multiply it by two so return that so this is now the same exact thing as this okay we’re taking in an input num number and then we’re taking num number and multiplying it by two just like that multiplying it by two right that’s it and now if I actually run this you’ll see I’ll get two lists of two four six one from this and one from this okay so I can actually look I don’t even need to use this anymore and I’ll comment this out and there you go this is a nice little one-liner that we just did on the Fly okay well what if I wanted to make a function that squared everything what if I wanted to make it so instead of it going doubling 246 we squared it so you know 1 squared 1 squared is going to be one two squared is going to be 4 3 squared okay is going to be 9 right so I should get back one four and nine how do I do that well that’s very simple we go here and we just replace this with a square we’re just going to square that result that number okay so we’re going to go through I’m going to Loop through get one I’m going to square it I’m gonna go get two okay map is already writing the loop for you that’s a nice part that’s why you you’re not the one writing the loop map is already doing that for you whenever you see map you’re essentially mapping through the entire list okay then I’ll go through two and square two and then I’ll go through three and square three and then I’ll just get one four nine in place of whatever is there right now okay like that let’s run it and you can see I got 149 Okay so let’s go ahead higher order functions right this is the example of map very nice very very useful okay now I’m going to show you another one that is very useful and we call this one this is filter okay filter is another beautiful one which you can use very commonly so for example let’s say I have a list of numbers so I have a list of numbers and I have one two three four five six seven eight nine ten eleven twelfth okay now let’s say I only want to get the even numbers watch how easy that is to write it I can actually say map and I write my Lambda here and I say I’m going to take in a number okay and then for and I’m going to say oh not sorry not map we’re going to write filter okay I’m gonna filter and filter where the number does not equal um or or we’ll do this so how do you get let’s let’s just show you the logic here first so I’m going to send numbers one two three four five six okay and let’s say I want to get all the even numbers from this numbers list right here well how I’ll do that I’ll zoom in so you can see actually let’s a get away from there okay so how do you check something is an odd number how to check for an odd or sorry even number let’s check for an even number all right what I’ll do is I’ll say two modulo 2 should have a remainder of zero so if I take the number two and I divided by the number two it should have a module of zero yes okay let’s take four and do modulo two and check what I get back let’s take six and do modulo two meaning like all these numbers divided by two have no remainder so every even number every even number divided by two has no remainder is basically what we’re saying that’s um that’s a definition of an even number okay that’s that’s an even number now an odd number will be you know like five so let’s say I try five modular two equal zero and it’ll say false okay so here let’s say I want only the even numbers well I’ll just pass in this this condition so to this filter I’m just going to say grab the number and just make sure that the modulo 2 is 0 if this is true then pop it into the new list filter also returns a list filter is a function and it returns list okay and a new list so I’m going to apply this and I’m gonna give it my numbers okay let me zoom out here so you can see everything filter I’m going to give it my numbers list right here okay and then in order to show this I obviously have to wrap it and list and in order to show that onto the console I need to print it okay so let’s go ahead and print this and look at that wow right like we were able to create this filter so easily if you had to manually do it that would take a lot of work and you could try to do it manually write your own Loop and all of that but yeah filter already automatically starts looping so whenever I’m doing filter I’m thinking I’m already looping in my mind I know I’m looping through every single item right but then the condition is like only put the thing in the new list if it matches this condition right over here so I go through here and I say his number divisible by two nope okay we throw that throw it away then we go to this and we say hey is this divisible by two yes it is so we throw it in the final result list then we go to three and we go nope four check five and six check seven and eight check nine and ten check right check and then we just keep dropping those and that’s how we’re getting all those guys in our list so that’s how that works and you know the basically the code here is a nice one liner I’m super zoomed in right but if I’m on my computer and I’m just zoomed out I’m just zoomed in so you can see better right but on my computer this is how I see it so it’s like that’s a smooth one-liner with plenty of more space to write right so that’s how you will filter so that’s enough for an introduction of higher order functions and now I’ll show you something that’s going to be even better and it’s like the most epic thing um and that is called list comprehensions they’re much easier to write than all this filter and um you know all this map stuff much much much easier and they will pretty much replace their powers and and give you new brand new powers okay and and you can write you can replace the functionality of map and filter with them and they’re just much easier to write and smoother so let’s go ahead and learn about list comprehensions now all right my friends now I want to talk to you about list comprehensions and how they work okay so I’m gonna go ahead grab that same list here numbers okay one two three four five and watch how cool it is and how I can do it with a list comprehension so I can say um let’s say I want to use this filter one right over here right I first thing I have to do when I’m doing a list comprehension is I have to make a list just like this okay so make a list and then I will say number for number in numbers okay um and then I’ll write my if statement if number is divisible by two okay and let’s go ahead and just print this and boom look at that that is already working okay it’s giving me the all the even numbers and now I’ll kind of explain what I’ve done so this is a regular for Loop four number and numbers this part you know already okay oh and then I say only get me the number if the number is divisible by 2. and then it asks how should I build the list that I returned to you um just give me number okay for each of those numbers just give it back to me so for example this filter would only allow two okay just give me two back great this filter would only allow four in the next Loop okay just give me back number and so it just keeps appending it boom boom boom all right keep sharing a new list out of that and then we return it now what if I wanted to double all these numbers I just do this and that doubles every number okay oh that is doubling actually all the even numbers so okay let me give you an example okay so this is the uh I’ll write a comment filter and give me only even numbers okay and then here I’m gonna go ahead and remove this statement here okay and I’ll say for number and numbers here’s what I want you to do with the number number multiplied by two for number and numbers okay so this basically means Loop through the numbers list call that variable you know whichever variable you’re iterating over number and then the new list how I want you to make it is a makeup make it up of these elements number multiplied by two so if I print this out you’ll see it has doubled uh all of those numbers right there okay and honestly in my mind this looks way easier than writing map and filter and then wrapping it in a list and Lambda and all kinds of crazy stuff this just looks like you’re writing a for Loop right that’s the power of list comprehension so that was filtering and then this is you just essentially did a map map double numbers okay so this is an example of a map this is example of a filter and um yeah that’s why I didn’t spend too much time teaching you these because you’ll kind of never have to use those again okay map and filter because list comprehensions are Kink and you can just use them for everything what if I wanted to get odd numbers to give that a try okay so get odd numbers give that a try here I’ll give you five seconds five four five seconds to pause okay now five seconds to solve it so take five seconds to pause and solve it five four three two one all right let’s go ahead and do this together and I would just simply say okay I’ll let me make an array first so if I first make my array or list and then I’m gonna say four number and numbers because that’s my Loop so let me just write that get that out of the way okay so this is my Loop right here and then what do I want to do I want to get only odd numbers okay so only get the number if number modula 2 does not equal zero so if it’s like if uh if basically there is a remainder in other words okay and I’m going to say once you get that number then all I want you to do is just give me that number back I don’t want you to double that number I don’t want you to Triple that number and just give me that number back and let’s print this okay okay and let’s go ahead and comment out all of this so we can see very clearly where our result is there we go one three five seven nine eleven so I got back all of my odd numbers here now if I told you uh you know give me all of the odd numbers cubed okay so that’s like crazy I wouldn’t ask you to do that that’s crazy but you would just do this and that will give you all the odd numbers cubed so this will get you all the odd numbers this is going to cube all of them and then that there you go okay so really really crazy fancy stuff that you could do with this and not just this right again if I take you to the Netflix example it’s like if I have you know this giant list I want to just filter out where the name is cozy so it’ll just filter that out or if I have like every single movie data and I only want like comedies I could filter this out this entire database out and I could filter where there’s only like the the title is comedy or they have comedy in their attribute and it can filter that out so there are very real world applications of where you could use this right you could filter out obviously with strings as well you could filter it out filter for anything floats whatever is in your list you could do it so list comprehensions are absolutely amazing and python you could also do 2D you know array two-dimensional list comprehensions too uh which we won’t get into because two-dimensional lists are kind of a little bit more advanced than we need to get right now because we’re just trying to Learn Python right now right and and master the basics so there you go uh I hope you enjoyed learning about list comprehensions as much as I enjoy teaching it they are so freaking badass they’re so cool and they’re very very easy to write right this is an example of doubling the numbers and it was so easy to do so I enjoyed them I hope you enjoyed them um as well and um last thing I want to leave you with is just there’s a couple of special keywords that I think you should just learn how to use and I won’t take too much time on this but I do want to just share them with you so I’ll do this here I’ll just zoom in here a little bit so you can see very clearly let’s go ahead and delete all of this and so for example python comes with something called sum so if I actually do sum one two three it can automatically just sum it up for you so you don’t actually have to write a function that sums okay so that’s one really nice one there’s obviously Len which you’re already aware of okay and then there’s also Max so you don’t have to write your own Max function it can Max can do that for you 10 five seven and boom there you go you get back ten so you could see Max automatically will always pick out the max number there’s also Min which is often times also very helpful so I’ll say 50 uh negative 7 337. let’s run it minimum the smallest number is negative seven and boom you can see that I get back negative seven Okay so very nice these are some built-in functions some lend Max Min okay I’ll just put them here for you and I’ll comment these out so you have access to them and uh it doesn’t know what that is I’ll just remove it okay I’ll just put these three so if you’re looking at it later it just looks like nice little notes for you that I’ve left Okay so special built in functions with python okay there we go awesome so list comprehensions plus some special built-in functions plus a little kiss for you your beautiful face and with that said let’s just keep rocking and rolling what’s up guys welcome back so I’ve added a couple of nice things for you let me go ahead and show you so go ahead back to your um course and right over here you’re going to see markdown so click on markdown and in your markdown you’re going to see all of this nice stuff that I’ve added for you it’s gonna say welcome to the best Python tutorial in the world by clever programmer and here I’ll list out exercises projects and any future projects that we’ll be creating we’ll go here okay and the Order of this might change by the time you are watching this tutorial because we’re constantly making changes making improvements seeing like where you might fall into a trap or what we’re constantly changing this so for example here’s a list of apps that you’re going to have so this is going to show you hey here the apps okay now here’s an example uh so for example this counter app that you’re going to build it’s going to be a really nice app they’re going to build with flask now if I actually scroll down over here it’s going to tell you hey before you start uh this project make sure you’ve mastered the following pure python Concepts and it will tell you hey it’s okay if you don’t know web development Concepts we’ll learn these together so for this counter app we’re going to be covering the following concepts web development and pure python related Concepts so web development for this project is going to have these three concepts in there routing get request database you don’t need to know these These are okay these are new we’re going to learn it together so you don’t have to know those however before you enter this project you will need to know these Concepts dictionaries named arguments incrementing decrementing and functions now if you have made it to this part of the course you know all of these already if you didn’t skip any sections you have been familiarized with dictionaries you have learned named arguments which is where you call a function and any given arguments a sum you can say a equals one and b equals two okay so those are like named arguments you have learned increment in decrementing which is no num plus equals one or you know or like a cookie hello uh plus equals one so it’s like incrementing uh uh a key in a dictionary right so you have learned those Concepts you have learned functions and how to call functions so all pure python concepts are good which means that you are eligible to go ahead onto this project now another thing you’re going to see in this project is going to see project live demo and solution all right so you’re going to see these three things the project means this is the exercise project where you’re going to write your code this isn’t going to be the complete project so this is going to be like you coding this kind of from scratch okay so that’s going to be that uh the live demo is going to be obviously the demo of the app working so for example if I hit this plus button right here you can see my calendar app is incrementing right and when I hit this um when I hit this minus button here you can see my app decrements right so how do we do that how do we create an app like that with python so you’re going to create that and this app is deployed onto the internet which is super nice about Rebel right replit allows you to deploy apps with a click of a button which means you will be good to you’ll be able to show all of these python projects you make to everybody else and then there’s going to be a solution section right there’s going to be a solution section and this section is going to have the link to the full solution all the code which I don’t I recommend that you go to the solution once you feel like you can’t make progress on the project and in this course we’re gonna go through the project together all right we’re going to cover every single thing we’re going to walk through it step by step together so you should have no problem completing this project portion but if for any reason you’re stuck you always have the solution file you can just go to that the next app you’re going to be making in this course or one of the apps also you’re going to be making is this random dog generator right this will have what the app is what concepts you’ll need to learn right I now have some additional notes for you as well like you know know hey this is how dictionaries work so you can scroll down and see those and so this is just a snapshot from what we have done already and so it’s like okay if you create dictionaries and you call the function boom this is what it will do or if you want to remember hey how conditionals work here’s like a little cheat sheet for how if then statements work and you can come and always check this or you know if you forget for Loops you can always see these examples that I’ve like written out in a very clear way because I know in notes here it’s not always so clear where the examples are but here they’re very clear here’s Loops here’s working examples of Loops right and then how do you find frequency of words in a phrase boom some more examples right so this will constantly be evolving and more notes more exercises more projects are going to be going there but yeah I just wanted to kind of give you the lay of the land I hope that this makes sense and I hope that this is helpful for you all right with that said what we’re gonna be doing is working on our first python project I’m so excited to do this with you so let’s go ahead and do this one together now we have this app that we’re going to be building together the counter app and it looks gorgeous and it’s going to cover quite a bit of python Concepts and we’re gonna do this slowly and one step at a time so it’s not overwhelming and I’ve tried my best to make it as easy and simple to understand for you as possible so what you’re going to go ahead and do is click this project link over here because we don’t want to see the live demo right now and we don’t want to see the solution right now so let’s go ahead choose a project so once you click this project it’s going to bring you to this page over here okay and then all you’re going to want to do is hit Fork Rebel so you hit Fork Ripple and then if you haven’t you obviously have an account I’m on Incognito but you’re just gonna go to that Rebel okay because uh once you Fork it it’ll allow you to edit code so you must hit Fork very important okay so go ahead Fork that ruffle CP counter app okay and then you could call it whatever you want you could change it okay so there you go now in here we’re going to be coding now here’s how this app is going to work so once you go here and if you hit run or whatever this is what your app should be showing you home page right there okay so that’s what it’s showing you and before I do anything else with this app I’ll just walk you through what’s inside and what’s happening in this app okay so so you’re not overwhelmed now there’s only one file you need to worry about okay there’s no other files that you need to worry about right now so that’s the only file right there that you have to worry about it’s the first thing I want you to notice the second thing I want you to notice is here okay we only have eight lines of code and I know there’s some stuff that you have not seen in that’s in here before and we’ll walk through that one step at a time okay so let’s just walk through line by line okay the first two lines I don’t want you to worry too much about but they are just importing stuff to help you out so you’ve seen me use a little bit of importing in Python not too much of it but all we’re doing here is we’re just asking some other files for help that’s why we’re doing import okay so there’s a counter app that I’ve created for you which I have hidden from you right now okay and that has some helpful functions that you’ll need to complete this project so your home page app show and DB and I’ll show you those later okay but these are these are there to help you that’s what’s happening in those lines so you can pretty much ignore these for now let’s go to line eight okay what’s happening in line eight over here this line is simply running the app all right and it’s running the app on Port 81 or whatever which honestly even all these details you can ignore I don’t even pay attention to this I just see this it runs my app if you remove this line your app doesn’t run okay so it’s simple as that this is to help you this is to run the app so that’s it so far now it brings us to kind of the interesting part of our code which is this part over here okay so the thing you’ve probably never seen before is this guy right there okay what’s this at sign and app.rout all this is saying okay I’ll I’ll open this in a new page so you should probably do that too all this is saying is that when I hit my home page meaning I don’t have a path here or about or anything like that in my URL when I hit my home page do the following run this function home okay so for example I could also say only when I hit the banana page uh give me a banana like this okay now make sure you stop and start your app again so you’ll have to do this pretty often you’ll have to stop your server and run it again because if you don’t it’s not going to show the new changes now if you go to your app and you refresh it’s going to give you an error and you could just go slash banana and if I go slash banana now you can see that it’s actually giving me back a banana right so that’s how this works this is just saying where this is happening so we’re just going to leave it on slash our home page and now on our home page we’ll say give us a banana so I’m going to hit command enter you could also do that too that can help restart your app if you do command enter and there we go I can see my banana is now on the home page so without going too deep into it I just want you to understand what it does at a simple level it’s just saying yeah on the home page buddy show me this banana that’s all okay now this function name here you could call it whatever you want it doesn’t really matter I could literally call it this right and if I start my code it’ll still um it’ll still give me that banana okay so this function name is not that important it’s just important that it has a function name or at least that there is a function okay that’s what’s important so that’s what’s important okay the function name itself is not that important here so I’m just choosing it to call home because it’s like when you go to the home page this slash route run my home function that’s all okay now there’s this other part that’s happening which is doing uh which is doing this uh which is showing this banana part over here now this is HTML so we are returning HTML here or we’re writing HTML here uh which means that if you have not seen HTML don’t worry we’ll do a whole video talking about a lot about HTML and what it does and everything like that okay um because if we’re doing some projects with web development with python you’ll have to know HTML and honestly let’s be honest like regardless of any type of development you’re doing especially in 2022 or after 2022 in some Modern year 2023 2024 whenever you’re watching this come on like you have to know the web an HTML regardless of what you’re doing if you want to become a data scientist python engineer it doesn’t matter so for example I can write H2 which is a heading tag and I can say hello and and close the tag with the slash right and if I run this you’re going to get this like big sized hello if I say H1 I’m going to get an even bigger size hello right so back to my point of what I was saying with python and with python and essentially web development like you have to know it whether you’re trying to become a data scientist uh you know a a web developer a data analyst whatever you’re trying to do with python you’re going to be dealing with the web either you’re going to be scraping the web pulling information from the Web building an app that’s on the web interfacing with an app all of that is gonna there’s gonna be something there okay so it’s better if you learn it um and we’re just going to learn some basics of it right now I’m going to show you very bare minimum but most important is like you’re getting some HTML here okay so that’s what’s happening here now what I’ve done is I’ve created a nice little function for you that allows you to kind of show a much nicer looking page because right now if you look at this page it’s pretty ugly right but we can also style this page and do things within this page to make it look better so for example I could even style it in here and I could say style and I’ll put double quotes here since I have single quotes out here and for my style I’ll zoom in a little bit here so you could see better I’ll say background Dash color colon and I’ll say let’s say red okay and I’ll hit command enter and Watch What Happens it’ll restart the server and you’ll get all of red stuff here okay so if we did a lot of styling and a lot of HTML stuff here we could make a nice looking page so I’m gonna help skip you a step and I’m going to take you a step forward really fast what I’ve done is I’ve created this nice function called show and the show function basically shows you whatever you give it so I could you know again give it some ugly looking HTML like this and if I restart my server um hopefully it’s going to show you some kind of stuff so we’re going to call our nice helper that we created show and inside of this show it takes two things but we can also sometimes leave it blank so right now what I’ll just do is I’ll give it home page okay so this is a home page that we created and so I’m gonna give it that that home nice looking home page and let’s see if it can show that nice looking home page onto the screen so I’m going to go ahead and restart and hit command enter on this and let’s see what happens boom there you go all right so just like that right our app is already up and kind of running and if you drag it down it’ll kind of normalize it but if you want to see it in the best view possible just open up a new tab here so you could see it kind of in the best view now if you hit any of these buttons like for example plus the app will break and if you hit minus the app will break so what we want to do is we want to connect this plus button and this minus button so that they actually speak with something okay because right now there’s nobody to speak to and I hit plus you’ll notice it takes me to the slash increment page but our code doesn’t know what to do with the slash increment page right like if I zoom in over here right it says slash increment paths our code knows what to do at this path when we go to the home page with the the slash or whatever it knows what to do but when we go when I hit plus sign and I go to this increment page our page has no idea how to react what to do it doesn’t know so think about this if in order to handle what happens at the Home Route we need to do a slash like this this means root right the home page if I want to handle the increment path aka the slash increment path right so let’s let’s let me put some notes here so if this guy over here is root this is our like rude or our home page right then if we want to go to our increment path wouldn’t that be just increment right so if that would just be increment then we need to create another route what we have on line seven that handles that so what I want you to do right now is just write one line of code for me please that should be pretty much this much in length okay and it should be very very similar to this line but it should basically handle what uh handle what happens or at least uh mention what happens when we go to that route okay so it should be very similar to this and see if you can write that I’ll give you about five seconds okay if you don’t that’s okay this will give you five four three two one all right let’s do it together so all I’m going to say is I’m going to say app.route slash increment so now I know that I’m I’m going to be handling my slash increment route and all I need to do it to be to be technical in Python these things with the at sign are called decorators they’re a little bit of magic so that’s why we’re not going to go too deep into them but in this case it allows us to handle what happens as at this route now right underneath it what we need to do is write a function so here’s what I want you to do before I even do this because this is just a python part so I want you to write listen to me very carefully so you can turn this into code write a function called increment that Returns the word increment that’s all I want you to do let’s write a word increment that returns a string increment that’s it so take five seconds try to do that this is pure python this is no web development right now okay five four three two one so I hope you took that time to pause and give this a try let’s do it together now so I’m gonna go ahead and do increments okay and I will say return increment okay so that’s it that’s all I’m doing here now let’s go back to let’s restart the server always make sure to restart the server it’s very important it’s a gotcha you’re going to fall for a lot so don’t fall for it hit command enter so your server restarts and go here and by the way if you see all this red stuff at the bottom here right behind me they’re not errors it’s just flask sending you red messages to scare you I have no idea why I wish they did it but yeah so okay now let’s go back to our app let’s first go to our home page okay now we’re on our homepage now when you click this plus button it’s going to route you to the slash increment page so let’s go ahead and click this bullets man we’re handling what happens at the increment page Isn’t that cool that is awesome we’re already handling what happens at the increment pitch okay um and and we could we could put whatever we want here right we could put a plus button we could put increment word whatever we want so all good we’re handling what happens now what we need to do when somebody clicks increment is we need to update a number okay so for example if you click increment then a function should run that actually increments a number right that’s what should happen that’s why the user clicked the plus button so that big giant number could go up by one that’s the only reason they did it right so we need to add that functionality okay so we need to have imagine there’s some number okay so imagine we just had a variable number which we don’t right now imagine we had a variable number we would do plus equals one or Imagine This uh imagine we had this imagine we had some fake magical database and we said database equals okay and it was a it was a dictionary like this and it said number and the number was Zero okay now let me ask you a question how would you get access to this value right over here zero how could we get this and increment this number by one how could we do that so if you remember from your previous lessons with python if you’re going through the full python course is how do you access a key inside of a dictionary think about that how do we do that how do we access a key and or a value inside of a dictionary well it’s a dictionary right so the first thing I’m going to do is I’m just going to say database and grab the number okay and then what I could just do is I could say you know let’s just first print it out and let’s just make sure that we we have access to this now anything that I print is gonna come in here to the console so I’m going to make myself a little bit smaller so you can see this console right over here okay so I’m going to make this a little bit bigger and we’re going to run this app again and then all I’m going to do is I’m going to hit this plus button and then once I hit it you’re gonna see what it’s going to say at the bottom here and you can see it actually saying print zero okay now here’s the cool thing our show function is really really smart so what we could do to our show function is we could also give it um it it also like if we look at our show function definition right show function definition or let’s say we call our show function here okay I don’t know why it’s not popping up the documentation our show function what it does is it takes in a page and the second thing it takes in is a number okay so it’s a page and number so for page I want to give it the home page so that’s what we have done essentially here okay and for the number if you don’t give it a number it’s going to assume that you want a zero but before we even do that let’s just do this first so when they click the page we don’t want them to come to this ugly looking page we still want them to come to the main page right because if we go to the actual solution file which is this one right over here you can come back to the course and you can hit solution right if we come back to the solution file right here and we go ahead and look at the app here okay so not the solution file sorry I meant to say if we look at the the demo of the app live demo if I hit plus it goes plus but it brings me back to the same home page right so the page has not changed so what that basically means in my mind is make sure the page is the same like we’re not changing the page at all actually so bring me back to the same page right so let’s just give that a try let’s say that so now when we hit the plus button it should bring us back to the same page and we should still see print coming up here which means that when I when I hit this button it’s still running this function which is a great sign so let’s hit the plus button or sorry let’s restart our server and let’s hit the plus button and watch what happened I’m still on this home page yet it’s still printing that database number zero so that’s a very very good sign right that’s that’s effectively what we want that’s exactly what we want so now what we want to do is the show page takes two things it’s very very smart so show input show takes in this function show takes in which page you want to show and it also takes in a number as a second argument so it can show this page and if you give it number it knows how to update this number based on the number you give it that’s the cool thing about this function show so say that I give it a number like 57. okay let’s restart our app so I’ll hit command enter and let’s see what happens okay let’s hit something plus and you could see that it’s showing 57. right why did it show 57 right now because I told it when I hit increment show exactly 57 so every time I hit increment it’ll show that okay we still haven’t done anything with decrement so if you hit it obviously it’s going to do this because we haven’t handled that routing yet so we’ll do that next so what I want to do is I clearly can’t hard code 57 here right that’s not a great solution but what I could tell it to do is I could say hey um what I’m gonna say is the number is this database one example so what we want to do is we want to take this database number and we want to increment it how can we increment a database number by one do you remember that from any of the lessons that we’ve done can you think about how to do that I’ll give you five seconds five four three two one so you could have used these five seconds to pause the video but if you’re not if you can’t think about it to increment a number we just do plus equals one so when I do plus equals one that will increment this number here so let’s just go ahead and verify that what we’re saying is true so let’s print out our database here okay and um when I hit let’s do command enter so it’s it runs our app again and then I’m just going to hit the plus button and when I do you can see every time I hit the plus button you could see that the database number is going up so it’s five and six and there’s a problem with when you’re trying to do this within replica sometimes within this iframe uh it will refresh twice so just go and open it up in a new tab hit uh start or you hit refresh and then hit the plus button hit the plus button and actually let’s just restart the app okay go back to the home page now hit the plus button edit again oh wait um yeah so you could see the database is now at 10 and now it’s at 11. so you could see that it just keeps going up and up and up and up and up and up so what we want to do then is we don’t want to give this a hard-coded 57 right if we don’t want to give it a hard coded 57 what do we want to give it can you think about that so see if you could think about what goes in there and I’ll give you a hint it has to do with the database okay that’s what we want to give our function and remember these are named arguments right number equals page equals these are named arguments not positional arguments to take about five seconds five four three two one all right so let’s do this together what goes in here is the database number okay that’s the number that we want to put in there so now let’s go ahead and reload this app and uh let’s go to a new browser and let’s hit uh refresh let’s hit plus plus plus plus wow look at that right every single time it just keeps incrementing I mean isn’t that beautiful every single time wow what happens when I refresh the app goes up goes up because I keep going to that increment path right what happens if I go back to the home ah it goes back to zero now what happens if I hit plus it goes back to 23. interesting so why is that going to zero that’s interesting so what we want to do here is since we’re we have this database number it’s it we’re giving it to our increment but let me ask you this shouldn’t our home route have access to that too because right now the problem is when we go to the Home Route if you look at our definition of Home Route home has no idea about the number because we’re not passing any number into here so it has no information show has no information about that so when show has no information about that what show likes to do is show the number zero so let me ask you this how could we pass show this number over here I’ll give you a hint it’s with pretty much the exact same line of code but in here okay so think about that take about four seconds to pause the video and give that a try Yourself four three two one let’s go ahead and do this now so I’m gonna give it the same number I’m gonna say database number so this is a not a real database right it’s a fake database that we just created kind of on the Fly so let’s restart our server let’s hit command enter let’s go right over here let’s refresh this one and I’ll hit Plus and I’ll hit plus and I’ll hit plus and I’ll hit plus so that’s very good but let’s go back to our home route so I’m just going to go back to our home route and our home routes that’s four isn’t that great I’ll hit five six seven eight that is beautiful right that is awesome I can keep going keep going keep going keep going there’s only one problem it stores the data when I’m refreshing so that’s not a problem it’s very good if I go back to the home page it still remembers that I’m at number 16. that’s very very good but if I restart the server and we go back to the app and we go back to the home page guess what it starts at zero well what if we wanted to make a counter that remembers forever so meaning if you hit it 20 times and then tomorrow your grandma used your app and hit it 20 times and then the day after that your paw your daddy use your app and hit this counter 40 times to count his steps every second he walked it should be at like a 60 or 80 or 100 it should remember all of that and it should never ever forget right how cool would that be if your app has data that it can record even when you kill the server and start it again and it remembers it forever so you know because this app is already online right you could link this to people already but it would be much cooler if it could remember forever so like how are many people use this app let’s just say you show it to like 10 or 20 of your friends they keep hitting plus plus plus and you see the counter is at like three thousand or five thousand right because it clicked it so many times so that would be cool and then every time you come back to the app from any browser anywhere at any time it’ll still remember so for that what we actually need to do is we need to use a real database and I’ll show you how easy it is to use a real database remember when we were using this fake database right over here and we said it has something called number and the number is set to zero well let me tell you something that DB the word that I have over there for you this DB right over here that’s literally this guys it’s literally this exact dictionary so like imagine you just change this word from database to DB and you can use it the exact same way the only thing is that this DB that I’m giving you is replits database which means it’s persistent which means that regardless of when you use this app it’ll forever have permanently stored that data kind of like a memory card when you played video games and it saved data forever so that’s all you need to know which means that you can really get you can comment down this line and I’ll just change it to DB don’t run this line because that’d be terrible that’ll break your app but just know that DB looks literally like this okay I’ll put a Emoji so even if you commented this line out you won’t mess it up right so just know that DB looks just like this so then in order to increment the DB number it’s that easy you just change that word with DB and you change this with DB and you change this with DB and DB and that’s it you’re done like DB is literally that so there’s not much to do except for just change everywhere with this database with DB and now you’re using a real database that’s permanent so let’s restart our app now and let’s go to our uh let’s refresh right plus plus plus so we’re at six right now now I’m going to kill the server and start it again and you can see that you’re getting some database here right because I’m printing the DB so I could also print the DB number as well and I could restart the server so now I’m going to restart the server and look at that it started at six when I hit Plus right you could see that it’s at eight and this number is coming directly from DB now if I go to the this app right here and we refresh this should be at a nine it’s keeping track of all of it right all of it plus plus plus I can restart the app again I can go back to this page and we can go to the home page now and you could see it’s still in 11 right 12. that information never went away so just like that you have a full on database my friend just like that and really all the lines of code that you wrote to make that happen were very very little right we wrote this one two three four five five lines of code and you have a nice looking home page which you know obviously looks nice once you uh you know open this app or just stretch it out so you could see it kind of like through the whole screen right so once you could see it through the whole screen it looks great and boom you hit plus and you get what you want right so in the iframe if you do it on Rebel it’ll be a little bit funny like this but if you do it here it should be fine and it should only go up by one every single time you hit it but now here’s the problem when I hit minus it goes to this decrement route slash decrement right that route and it just doesn’t know what to do the requested URL was not found in the server if you enter the URL please check your spelling and try again and it doesn’t know what to do it doesn’t know what to do so now I’m gonna give you one bigger exercise and this exercise is going to be for you to figure out how to make the decrement path work from start to finish so we’re not going to do baby step it but you’re going to be writing about four lines of code they’re going to look very similar to this with just a few changes where it might say increments there might be another word you might be using there so pay attention to details don’t just copy paste like one of my friends was trying to do you’re gonna change a few letters at least okay uh otherwise going to be similar where you increment the number and decrement think about what you need to be doing or you’re going to increment the number when somebody hits minus no you’re going to decrement the numbers your decrement function will do that okay and then this line of code I’ll already give you a hint this will look very similar or pretty much it’ll be you know it’s a high chance it’ll be the exact same okay so take about five seconds to pause and try the try making when you click this button things work and then make sure to restart the server with command enter or control enter if you’re on Windows or just by or just by stopping and starting again to have the new changes take effect and then go to this other browser and run your app and hit the minus button run your app like go on back to the home page and then hit the minus button right and then see what happens okay so I’ll give you about five seconds to pause and try adding the decrement functionality all by yourself so five four three two one all right welcome back let’s go ahead and let’s add the decrement functionality together so it’s gonna be very similar right so I’m gonna do app.route and I’m going to say decrement this will handle what happens at this path we’ve gone through this right and then I’m going to go ahead and call my function decrement okay again these function names don’t matter but I’m going to call it decrement here because that’s clean and it makes sense and I’m gonna get the number from our database and I’m going to decrement it so I’m going to say minus equals one you could also do multiply equals one and minus equal 1 is the same thing as basically saying it’s same thing as saying this okay but it’s just easier to write it so I’m going to say minus equals 1 here so I’m decrementing it by one and then I’m just going to go return show so we’re going to show the home page and then for number I’m gonna show the database number okay that’s that’s the number I’m going to send to my show function okay so I think that that should be it let’s give this a try now so I’m gonna restart the server with command enter the server is restarting let’s go back to our main app right over here if I decrement this should go down by one so let’s try it hey let’s go if I hit Plus beautiful right it goes up by one we hit minus it goes down by one right every single time so there it is just like that your very first app is done and if you want to show it off to everybody just take that URL make sure you take the home page URL right here and just send it to anybody you want right now and they can use it now this app is not mobile friendly so it might not look good on their mobile device but it is web friendly so as long as they’re on the website on their computer desktop friendly so if you they’re on a desktop it’ll look great and it’ll work great so look at that you did this with just a few lines of code Isn’t that cool you did your very first project with python I mean this is mind-blowing I’m so excited for you that you got this done I’m just I’m excited I’m pumped you got this done congratulations uh this was wonderful and uh with that said let’s keep on going and let’s build some more amazing stuff all right what is up now we build this amazing app that is great but how did we build it and how did we build it so easily so there’s a lot of mystery around this app for example what is this home page thing that’s happening and what’s up with the show function that just magically does everything in reality things aren’t going to be this easy for you right now the truth is that there’s a lot of stuff that I’m hiding from you and I’ve done a lot of the hard work for you and so when you’re writing the code you only have to write a few lines of code and then everything works so we’re going to unpack and demystify this app into its component parts and see the real truth about how this is actually made and you’re gonna build it with me from scratch and we’re just going to unpack one thing at a time until we’re fully unpacked where there’s really no abstraction that I’m offering you just have a fully built app with python now let’s start off with the simple things first the framework we’re going to be using to build this app is called flask flask is a web development framework very popular with python and allows you to build apps really easily when you think of flask you could think of it kind of like node.js and JavaScript now if you’re a beginner none of that might make any sense to you and that’s fine as far as you’re concerned when you want to do stuff on the web with python you use flask as simple as that there are other Frameworks like Django Etc we’re gonna keep it with flask because flak flask is the simplest and easiest one to understand and when you get more advanced I think you could go on to Django or something else but flask is very popular lots of apps are built on it so let’s go back to our app right over here okay now we have a bunch of uh things that that we don’t really fully understand like what is this counter app file what’s this home page what is this app what does this show what is this database thing okay so let’s deconstruct kind of one thing at a time with our app now the one thing I want to deconstruct First is let’s go ahead and start by deconstructing um deconstructing this show function okay so what the heck is show and how is it working now in reality you won’t have a flask a function called show okay what you will have is a function called render template string so let’s go ahead and play with that real quick so I’m going to say from flask import render template string okay so that’s what you’re going to have now on this home page I’m going to go ahead and comment out this line just so it’s there so you can see it in comparison but we won’t be using it now if I run my app let’s just say return hello and when I run my app you’ll see that it won’t look very great you should only see Hello right over here okay now let’s start from there so I see that hello how do I write something so it looks a little bit nicer so we can use obviously we can just write regular HTML here but what we want to do here is we can also use render template string so I can do this render template string and I can go H2 and I can say hello and end H2 okay and now what it’s going to do is going to go ahead and show you that hello but since we’re using HTML so it’ll look bigger and kind of bolder now since we’re using HTML and we’re going to be using a lot of HTML let me go ahead and just kind of introduce you to HTML okay with python um you know or with anything really in general it’s like hey how does how does web development work what is this HTML CSS stuff if you’ve never been introduced to it before it might be a little bit confusing but let’s just go ahead and do this together so I’m going to go ahead and from flask instead of importing render template string but I’m actually going to import is from flask import render template okay just render template just that right or actually you know what let’s leave it as render template string let’s leave it as that and I’m just going to go ahead and create a variable we’ll just call it HTML for now and I’ll put three um quotes like this so this will think of this as like our HTML file okay what we would make and HTML files usually have a certain structure but I will just say okay we have a H2 and that says hello end it and and whatever I wrote here in this HTML I’m gonna just go ahead and pass it to my render template string so I should see essentially the same thing so let’s remove this and let’s give it our HTML you could call this a variable whatever you want we could call this um my I could call this my home page okay so it’s different than this home page that we’re being passed I’m gonna say my home page right there and let’s just copy and let’s paste this okay let’s run this now so if we give this my home page okay we’re getting an error so let’s do that and let’s see if that still gives us an error it doesn’t and that works okay so you can see my home page is being rendered out now what else can I do with in HTML well I could have an input box if I wanted and the input could be you know type uh type text so now if I run this I can see that I’ll have an input type right over here but type text okay I could also have a button if I wanted so I could do input and I could say type is submit there’s multiple different ways of doing buttons and make sure you close this tag with the self-closing tag like that let’s run this okay and now you can see I have uh input thing that I could actually type in and then I have also the submit button showing up okay in this input I could actually have have a placeholder that says let’s say username let’s run this make sure you do command enter that’ll restart your server and there you go now it says placeholder is username right over here okay or clever clever cozy whatever right I want it to be so we can write H2 elements we could also write H1 elements in our H1 is even bigger so you could go H1 or if you want to go all the way you know hello say welcome back and then you could go all the way down to H6 it’s the smallest and I could do this and let’s run this okay and H6 again is going to be the smallest you can see I have that this that and then I can also make input Fields right and HTML is very nice and I could have things like P tags so I could have a P tag and the P tag could say hi my name is qazi okay PTAC boom and we run that okay let’s restart our server so let’s do command enter and there you go P tag is showing so P tag is a paragraph tag so if you want to write like a paragraph or like a text you normally will wrap it in P tags if you want to have inputs where the user can type in or do something then you’ll call them inputs okay kind of like this H6 is heading six so that’s the smallest heading and H1 is the biggest heading so you could do all kinds of stuff like that now what I could do is what if I wanted to wrap all of this and color it red how could I do that well I could uh let’s start off with how could I wrap one thing and give it a color of red so let’s just say welcome back if I want to give it a color of red well what I can do is I can create a div and divs are these like these boxes that you’re creating around things and I can wrap this H2 in this diff okay in this div right here so you can see I have uh let me zoom in a little bit more okay all right there we go so you could see I have this H2 and I’ve wrapped it in this div okay so in other words all I’ve done in my mind is I’ve created this empty box around this and now I can control that box and I can style that box and I can do whatever I want okay so that’s all I’ve done that’s all what a div is doing now what I can do is I can do something called so there’s something called CSS okay so here you’re learning a little bit of HTML and then there’s CSS so CSS is the styling of a page okay so HTML just gives its structure so it just shows the page but CSS is how the page is actually you know it gets styled all right to explain to you what styling is and how it works say I go to Netflix you could go to youtube.com or really anywhere and say that I I right click here can we turn this off okay so say I right click here and we hit inspect element okay now inside of inspect element say I go to the uh to the Head okay and usually in the head of a HTML page okay so this is usually how HTML page is structured and you can even highlight things on the HTML page so I could like go here and you know we could see like all right this is like a HTML div you could see it says div and then it has an image inside of it and has a number next to it Etc but let’s go ahead and I’ll unselect this okay and I’ll go all the way to the top and so you’ll see like HTML we have HTML although at the top and at the bottom so that’s usually how a HTML page is structured and then it’ll have head it’ll have body body is usually where you put all of your content okay so that’s where like binge-worthy TV shows and all of this will go okay head is where often times people put styling okay this actually always where you put your style file or you link your style file so let’s go ahead and remove the style file and Watch What Happens so I’ll go here and I’ll basically what I’m trying to do here is I’m trying to find anything that says style sheet so you see where it says Rel style sheet it’s usually a style sheet I’m going to click it and I’m just going to delete it and then click another one delete it click another style sheet and delete it so once I’ve deleted all the styling on here now there’s essentially no more styling left so look at this this is actually how Netflix would look if you didn’t have any styling this is what it would look like okay and you tell me if this looks good it doesn’t look very good right this is how this is how everything would be looking on Netflix this is Netflix okay hard to believe right very hard to believe now and then you can see that like list over here it looks so hideous right all of it just looks terrible so if I hit inspect right in our in our head is where we removed it from and if we refresh this page now you’ll see that the styling will come back the style file is linked and now boom all the styling is there okay so that’s just a little bit of how HTML and styling Works in real life so here’s how I can add styling here now in our app I’ll zoom out here so inside of our app I’m gonna go ahead and say this div that we created here right this block right here that we created I want to give it a color red so I can actually say background Dash color okay and let’s put some uh we’re gonna put quotes around it and we’re gonna say style equals I’m gonna say this and I’ll give it a color of red let’s say and then end quotes and put a semicolon here okay let’s go ahead and restart our server now let’s see if anything happens okay we don’t have any changes yet ah so this quote should not be here actually so let’s remove that quote from there let’s do it right here okay so we basically want to say style is an attribute and the style that we’re giving this is a background color of red so now let’s hit command enter and run this and now you can actually see that that div is red okay so here’s what I want you to do I want you to make this entire thing red so do that as an exercise okay real quick and basically what you’re doing is you’re going to be wrapping the entire thing in one box so a hint for you is you should only write one line of essentially two lines of code and you should only create one new div and your div should wrap everything all of the content inside of that div and then all I want you to do is simply just go ahead and say color red okay background color red and once you do that that should be good to go okay so I’ll give you five seconds to pause and try it on your own five four three two one all right let’s go ahead and do it together now so since I want to put everything inside of a div I’m just going to wrap the whole thing inside of a div okay and I don’t need to indent it but I just like to indent it so everything can look cleaner so I’ll just go ahead and indent that all right so there we go and the CSS part just so you’re confused the CSS part is this part any part that has styling that’s CSS this is literally that’s the CSS part and then the HTML part is like the entire thing is the HTML Port okay all right so you know think of HTML as like the skeleton or the body that you have and then think of CSS as the clothing or the Fashion on that body so the shirt that I’m wearing is CSS okay but like my body and my actual skin that’s more like HTML that’s a skeleton the structure that holds me up okay now that I’ve put everything inside of that div I can just go ahead and say style I’ll say background Dash color red okay let’s run this and you can see the entire thing is red now okay so that’s just to kind of give you an example of how things are working under the hood and how you would create your own HTML now I’m going to remove this annoying looking red and we’re going to move on to the next part of our application let’s remove the Reds here okay let’s remove all of this I’ll remove this entire div and remove this entire div and we’ll hit command enter so we restart the server okay and let’s remove that div there too we don’t need that anymore okay let’s hit command enter here and so just so we can have everything clean and nice okay just like this now if I go to our final version of the app which is this right here you will see that I have two buttons okay so these are two buttons just like this button that we created the only difference between this button and this button is that their styling is different but they’re still both buttons that’s it alright so this just has a different styling and I’ll even show you a little bit of how you can add styling but in this course we’re not going to go too much into styling okay that’s not my goal my goal is to keep you focused on the python aspects of the course all right so that’s what’s happening there okay so they’re they’re both of them are buttons so this page will have two buttons but watch what happens when I click a button right so if I click a button you’ll notice that the URL at the top will change and say slash decrement slash decrement so I’m going to click it and Watch What Happens boom look at that so how is that happening how is that saying slash decrement over there and how come when I click the plus button it takes me to slash increment okay well let’s go ahead and see that so what I can do is I can take let’s say an input so like I’m going to remove um let’s say I make a new button okay and I’m going to remove both of these for a second and to make a new button I’ll say form okay and we’ll close form and inside of this form I’m going to put an input and it’ll be a self-closing tag and we will say and self-closing tag just means when you open a tag in HTML you have to close it like this so if you open an H1 you have to close the H1 okay H2 you close H2 if you put a div you close the div same thing with form if you open the form you got to close the form now in this input I’m going to go ahead and just put a button and so I’m going to say type submit okay and um yeah we’ll just leave it as as uh as that okay we’ll leave it as that now the only problem with this type of button when you put submit is that you can’t really change the text so instead what I’d like to do is I’d like to just say uh button and say let’s say um strawberry okay and we’ll close this button and now when I run this so let’s restart the server I’ll hit command enter you can also stop and start again now I should just see the button strawberry okay and also open this in a new tab because you’ll need to open a new tab in order to see the URLs changing okay so keep this open in a new tab and what I like to do is I like to open this up kind of one third of the two-thirds of the way and then I like to take the app that I’m trying to see and set it one third of the way so that way on the left hand side I could see code and on the right hand side I could see my app all right so now I have this button but when I click that button if I go all the way to the end my URL doesn’t change yet so how will I get my URL to change so what I want to do here is I want to say action and for the action of this I want to give it a path so I’ll just say strawberry okay so when you click the strawberry button it’ll actually take you to the strawberry page all right so now let’s go ahead and try I’m going to full screen this so you can see this I’m going to hit strawberry and Watch What Happens okay actually let’s refresh our server so I’m going to hit command enter okay and then let’s go ahead and give it a try we’ll refresh here and I’m going to hit strawberry and look at that it took me to the page called strawberry right so now so this is us working on our front end right now okay we connected our back end earlier but now we’re actually building out our front end the client side of the app and we’re we’re making it work so if I click strawberry that’s very good if it’s taking them into the strawberry page now let’s go ahead and I want you to try making a button on the home page okay so go back to the home page of the app and the button that I want you to make should be called banana okay so when I click banana it should take me to a URL route called banana so I want you to give that a try right now I’ll give you five seconds five four three two one time okay so uh if you’ve paused the video and give it a try good job if you haven’t that’s okay we’ll do this together now so I’m going to create a form all right and we’ll end the form the action of this form will be banana and the button will be banana just like this okay so this will give you a button and this form action thing is what does the thing that does the magic of like when you click it it actually takes you to that page okay the action is doing that so let’s keep going here I’ll do command enter and we’ll restart this app here so just make sure you come here and you refresh okay once you start now you should see strawberry and banana and I’ll full screen this so you can see URLs changing so I’ll click strawberry first and you saw it change now I’ll hit back and I’ll click banana now and you can see they’re actually going to the banana route now that’s very good now let me ask you this since we’re doing that that’s awesome how do we actually get uh you know so if we go back to our completed app so let’s take this app and let’s put that over here if I go to my completed app right this part is very good like if I hit this minus sign it changes the URL if I hit this plus sign it changes the URL but these buttons are also connected to code remember so for example when I click this minus sign it actually runs this function over here so how do I connect my strawberry and banana in a way so this um like a like a strawberry function runs or a banana function runs when it’s clicked okay maybe maybe uh when you go to this page instead of it saying not found it shows a if I click strawberry instead of doing that it actually shows me a strawberry emoji and when I go to the banana page it shows me the banana Emoji how could we do that so I’ll tell you this you actually have all the information you need to be able to complete this this will be a difficulty of like six or seven you need to just recall and put things back together kind of like how we did for increment and decrement because you already have the pages routing you so it’s very similar to the exercise we’ve already done so I’ll give you about five seconds to pause give it a real try and then come back and let’s do it together five four three two one time all right if you give it a try and you got it good job if you didn’t that’s all right we’ll do it together so I have these two forms right and and I have these routes so when I think the route in my mind I’m already thinking I gotta make that route so I can just do app.route and I can go ahead and do strawberry and the function that I do is going to be called strawberry okay and what this function will do is I’ll just have it return strawberry that’s it it’s literally all I’ll have it do okay let’s hit command enter and then let’s click strawberry and look at that that easy so you have the front end part you’ve made this button that’s your front end part you did okay and this is your back end part okay where your front end makes a request to the slash strawberry page that shoots it over here then this piece of code runs on your back end or server side it gets this on the server’s side and then it returns that okay it returns that back out to your front end to your HTML okay so that’s like a it’s a relationship there’s like you’re going from client to server side to client side okay I know it sounds complicated right now but over a long period of time these terms will start to make sense okay but you’re going from front end to back at the front end okay so this HTML part right over here is the front end part in other words whenever I say front end it’s the stuff you can see and touch so this button over here right when it shows up that’s something you can see in touch so that’s front end this year you can’t see and touch this is happening on the back end but then when it shows it back up here and you can see and touch it again it’s you know that it’s showing it on the front end okay so usually things you can see are easy way to sit that’s front end things you can’t see AKA you storing data in a database that is more of the back end so we’ve already done this now I want you to go ahead try and adding the banana route so that when I click the banana button it actually shows me the banana Emoji so I’ll give you about five seconds to pause and give that a try five four three two one all right let’s do it together now so I’m going to go ahead and I’m going to go ahead and say app.route and we’re going to do banana that alone is already going to run this piece of code here that I’m about to write and I’m just going to go ahead and say return banana okay if you don’t have emojis go get emojis make sure you’re putting it in a string okay putting those quotes around that it’s important and uh if you don’t have a mode you just use the word strawberry or banana that’s fine okay now let’s restart the server hit command enter and then hit banana and there we go now we are seeing a banana output it to the screen when I click that button so just like that with that much ease here’s what you’ve done okay this form button here is what’s showing this guy okay specifically this form action here is what’s updating the URL once you click all right and then this guy over here is rendering what happens on the next page once you click it so once you click it right this is the guy that’s showing all of this so that is how all of this is Magic is actually working okay so those are the buttons that you didn’t have to create that we essentially helped create for you now how will you convert this from strawberry and banana into increment and decrement well think about it right you will still have a form and an action and you still have two buttons so let’s say that I get rid of all of this stuff over here right I don’t need that and let’s say I get rid of all of this stuff over here let’s restart this let’s refresh this if I go to the completed app there are two buttons that’s it those are the only two buttons we actually need right here right now let’s go back to our app we got two buttons so that is good now let’s look at what those buttons are called This is called minus sign this is plus sign those are the titles of that button right or the names or whatever so simply we’re going to change the strawberry with a minus sign and this banana what will we change that with you go ahead and do that part I’ll give you three seconds you could do this in three seconds three two one we’re gonna give it the plus sign okay so now we have two buns let’s hit command enter so it refreshes let’s refresh our page and let’s see what happens great so now we have two buttons here that’s very very very very very very very very good now we need a number at the top we need a big huge number just like this at the top so here’s what we’re gonna do I’m just gonna go ahead and let’s say uh for now I’m just going to do H1 and I’ll just say 23 okay and we’ll we’ll end this H1 okay and then let’s run this okay let’s refresh this and there we go we got 23 and we got a minus and a plus now when I click this minus I go to the strawberry page when I click the plus I go to the banana page right but that’s not what I want to happen what I want to happen is I want this increment function to run and I want this decrement function to run once I hit this minus sign and this plus sign so if I want those things to happen well I can actually just change the names to increment and decrement right because this button is decrement so I can actually change instead of strawberry or whatever I could just say decrement right there and instead of banana right here I could say increment right here now what’s happening is it’s actually going to run those functions at the bottom okay but I’m going to remove all of these show commands okay we don’t need those commands for now and let’s say we even remove this render template string command and home I’m just going to say hey render my home page that’s it so all of these I’m just going to have them return my home page for now that’s it so if we run our app hit command enter and let’s run our app and let’s refresh here we should still see the same exact app right now okay so nothing should have really changed that much now when I hit the minus sign I want to detect that I’ve hit the minus sign so we can add a print statement or we can have it say return so for now let’s just have it say return and we will say um you know we we land on the increment page increment page and here we will land on the decrement page okay so I’m going to hit command enter let’s hit minus sign and you can see I entered the decrement page and if I hit plus I enter the increment stage so this is a really good sign so far because it means our buttons are connected which means even this is happening this DB number command is happening so whatever number is stored in our database is actually going up so I don’t need this strawberry and banana route anymore I can actually get rid of them those are there just to help you kind of understand what we were doing okay now the next thing is I need this number to be dynamic because right now this number is not a dynamic number it’s a um hard-coded number but that’s not what I want I want this number to come from the database now this is the HTML part which means this is the static part okay so usually any HTML CSS code is static which means HTML does not have it’s not a programming language it doesn’t have the capability to like calculate stuff and do stuff it can just show what already is meaning that HTML can automatically go from 23 to 24. okay it’s static it can’t change so what we need to do is use Python to get the updated number pass that number back to HTML and then HTML can show it in the front okay that’s kind of how the relationship between HTML and CSS and and python works here so how do we give it a number well if I do just return my home page or return increment we can’t pass it any data okay so how do we do this in that case well how let’s just start with the my home page how do I give Dynamic data to my home page so it can actually render that out over here here’s what I can do I need to actually call my render template string function that I had imported from flask earlier okay so I’m going to go ahead copy this render template string and I’m going to paste that over here render template string okay and render template string is a function that takes in the path so we’ll give it home page or our my home page this this one that we just created over here we’ll give it that as a path and then it takes in like what are the arguments that you actually want to send it okay which means like uh you could send it a whole bunch of information and it will have access to it on in the HTML side okay so for example I could actually say here’s a cookie okay and I can actually give it a cookie now I can change this cookie variable right over here my HTML will have access to it okay but in flask there’s something called ginger templating j-i-n-j-a and that allows you to write like python flask code inside of HTML so it can basically allow you to make HTML Dynamic so here’s what I mean instead of just 23 I could actually change this with the word cookie and now whatever is the value of cookie that I’m storing here it’ll actually pass it right there okay so let’s go ahead and uh hit command enter and let’s refresh and let’s see if we get a cookie at the top there we go you could see that we’re actually getting back a cookie at the top so we we’re giving our page access to some data and it’s able to retrieve it so I could pass it multiple pieces of information for example I could give it a cookie variable I could also give it a um age variable and let’s say age it says 28. and now we have access to the edge so I can go ahead and say age is and I can see the age variable right there okay so now if we restart this server and hit Refresh on our app you could see age is 28 it’s whatever I passed in here it’s passing it in boom right there right so let’s go ahead and delete the H because we don’t need that and for cookie we don’t really need the cookie right well we actually need access to is the number and the number we can get from our database so I could actually say DB number okay now let’s go ahead and refresh so I’m giving it access to number and make sure that this is the number that we want to pop in here so replace this variable cookie with this number so go ahead and do that now give you three seconds to do that three two one time let’s do it together boom I’m just going to change this number hit command enter and simply refresh now it’s going to have the number here uh that’s in our database okay now if I hit uh since we’ve connected I believe let’s see whatever we connected so we have increment working except when you click increment watch what’s going to happen it’s just going to take you to this page that’s going to say increment but when I come back right nothing now if I refresh you’ll see it says one so if I actually go here it’ll take us to the page increment when I go back and refresh the home page it’ll say too so incrementing is happening we’re incrementing the database except we’re going to a different a completely different page okay and later we’re going to deconstruct what databases as well what is this database thing it will deconstruct that as well leave it as a note here for now so here if you notice in our finished app when I hit minus it renders me back to the home page it doesn’t take me to a newly designed page so we should still be using this exact page that we’ve created here and we should just reuse it okay so this page here we should just reuse that page so I’m going to go here and return I’m going to say my home page okay and we’re going to say render template string so you can see render template string allows us to pass arguments to our back end okay boom and render template string my home page and then the argument I’m going to give it is also number DB number so now increment page is going to look exactly like our home page and this number is going to be updated dynamically because it’s literally going to just run this pop in the dynamic number and then output that number okay so if I full screen this app let’s go ahead and actually hit command enter so we restart our server let’s go ahead here and let me hit plus sign whoops getting an error let’s refresh first let’s hit plus sign now okay what’s happening here let’s go ahead and check uh okay let’s go ahead and make sure we’re going to the right page so we are going to the increment page so let’s see what the error is I’ll open this up so I could see what the error is it says my home page is not defined okay got it so it’s uh it’s my home underscore page okay like this so I just had a misspelling and you can use you know just debug your code and you’ll be able to find your errors let’s go ahead to our home page now and now let’s hit plus and watch what happens you can see that I got routed to increment and boom it changed to six now hit let’s hit plus again boom it changed to seven plus again boom it changed to eight nine I clicked it twice it’s eleven so that is working completely okay so now I’ve changed my this function over here pretty much to not be using the baby guardrail code that I had for you right the show function you’re writing your own actual factual flash code to make this work so very very good now what I want you to do is make decrement work so we’ve made increment work from scratch try to make decrement work by yourself I’ll give you five seconds to pause and give it a try yourself five four three two one time all right so let’s make this work if you have good job if you haven’t let’s do it together so I’m still going to be decrementing the database number and here I’m going to pretty much just do the same thing because I’m returning the home page and giving it the number that was just updated so now decrement should be done okay so let’s hit command enter so we restart the server let’s refresh here just to be just to be clear and if I hit minus right you could see that it took me to the decrement page and you could see decremented this number let’s hit minus again minus minus minus let’s hit plus plus and it’s working perfectly okay now let’s go ahead and destructure we still need to so we have pretty much destructured and deconstructed all of this code into real flash code we’re very close uh we just need a couple of more things and then we should be good now uh so this show function you won’t need it anymore you could actually delete it in this home page function that I gave you you won’t really need it you could delete it okay now what we need here right is um we need to understand what this database thing is so here’s the cool thing replit comes in with something called a database it’s very nice and they make it very easy to use so if you want to use it here’s instructions just click it and you’ll if you click this insert over here it’ll even insert this code for you so I’ll hit insert that’s it so once you import this database you can just use it just like how we were using it and we would just say DB and then we just use the DB and that was it so now you don’t even need access to this DB that I was giving you for my counter app because you now know how to get it you can just replit import DB so we’re done with the understanding of the database okay and we’re just going to write one line of code which will account for what happens if you’re running this app for the first time if you’re running this app for the first time and you never had a database if you try to Index this number thing and this key was never created you’re going to get an error okay so for example if I’m in Python and I’ll go into shell and I’ll type in Python okay and uh let’s say that I create a variable called thing okay thing or or a database okay and it’s a empty dictionary let’s just say if I try to go ahead and grab a number from there watch what’s gonna happen I’m going to get in there it’s going to say what the heck there is no key there’s no key so what I want to basically say is I want to say hey if number key is not in my database well then I just want to go ahead and create that key so I’m going to go ahead and say uh database number and I want to just set that equal to zero because that’s where the default number should start from okay so I’ll do that and now if we look at the database you can see it actually just created this so now if I try to index it or if I try to increment it it won’t have a problem it’ll just allow me to do that without throwing an error right you can see it’s incrementing and I can keep incrementing it right and now I print it out it says number is three because I incremented it twice so on and so forth so this line of code here we’re just going to write that right here so I’m going to say if number not in DB then DB number just create a new key and set it to zero okay so this is like useful for initializing the database if it’s not been initialized yet okay so we’re done with so now you’re completely done with understanding the database and how it works and if you actually command enter and run this app from scratch let’s refresh it will everything will work so if you hit plus it’ll work if you hit minus it should be working okay very very very very very very good great so we’re done with that part when it comes to understanding the database you know we’re done okay now what’s what’s left what’s well what are some of the other things that you have to kind of understand or you have to know so the next thing I would say is this app thing where’s this app dot run coming from how are you doing app.rout whatever this is a flask app that you actually initialize at the very start of your app so whenever you have a flask app here’s what we need to do from flask we need to actually import flask this is a very important step of any flask app that you ever have most of the flask apps that you create will kind of come with this default code written in okay the next thing that we need to do is we need to Define what app is an app is just flask and name okay and this you honestly will never have to remember or memorize you will always just look it up how to initialize a flash Gap and this is how you’ll have to initialize it okay so this right there just initializes a flask app and uh creates it for you and then in the future anytime you do app.something app.run for example it runs this flask app that you created okay so this is like something you can memorize or just look at it and copy it next time you don’t really have to try to understand it too much that’s just how you run a flask app so now we don’t even need access to this app thing that we were getting from counter app and as a matter of fact we don’t need access to counter app at all we can get rid of this whole darn thing and be left with this and now this is completely all of your own code with no help code from me right now okay so let’s go ahead and hit command enter and let’s run this again so I’ll refresh okay and now if you hit minus it’ll work minus right if you hit plus plus plus there we go okay everything is working now the only thing that’s not looking very good in this app obviously is the styling The Styling looks kind of terrible so let me show you how you can get the styling as well okay and let me show you really the Santa is not real type of situation here so I’ve hidden a lot of things from you if you actually click here and you actually click show hidden files Okay click here and the three claps collect collapsible dots right there and hit show hidden files you’ll see there’s a bunch of hidden files so I don’t want you to worry about this file config and I don’t want you to worry about this file vent okay the only files I I have the solution for you right here and I have the raw solution for you right here okay so this is the solution with the guardrails the baby guard rails and this is the actual factual truth the raw raw raw code that you would have to write if you coded this app up from scratch okay so those are they’re available for you if you want to see them now what’s interesting is I want to show you this static app where there’s a CSS file and here we’ve written already a bunch of CSS for you which is the styling for the app so you don’t have to do really anything um you just have to import the style file and then your app will actually look really good so let’s go to our main.py file okay and at the top right here I’m actually just going to go ahead and import this okay and how I’m going to import this is I’m going to go ahead and say link and we’re going to say Rel stylesheet okay and we’re going to say href and I’m going to give it the path of this file right here okay so the path of this file right here is actually dot slash static because I need to go to the static folder and then inside of the static folder it’s right there so I can say static slash counter underscore styles.css close it out and then that’s it you can end the tag or you could just leave it like this either way it works it’s a self-closing tag so that should be good and that will link your styling okay so let’s go ahead and let’s run this and let’s see if it actually makes a difference so let’s hit start wow look at that right it’s already looking so much better so if I oh full screen it look at that the no the minus sign here is looking good right the plus sign here is looking great that is awesome this is already styled um now if you want to put these buttons so they’re in a row and not in a column like this all you have to do just wrap it in a div okay so let’s go ahead and take these forms and wrap both of these in a div I’ll give you five seconds to try it on your own five four three two one let’s do it together now I’m just gonna go ahead and say div here and I’ll set div here and just indent this so it looks nicer okay I hope I select it all and I just hit tab or shift and tab to bring it back and let’s go ahead and refresh and now your minus and plus signs should be above just like this they’re looking juicy now right they look great awesome and the last thing we need to do is make sure that our 12 right looks like that 18 right because it has a yellow circle around it and so the secret for that is if you go to our styling and I’ll kind of explain some of the styling to you here as well I’ll open this in a new tab okay so some of the styling that you’re seeing so if we look at this here right I can I can change things inside of my styling app as well that’ll obviously change uh how my styling looks but for example let’s take a look at our forms right or our buttons so this is the styling that’s showing that so it’s like for example it says Hey make the background color this so you can see the little gray outlines make the color of it this or transform 1.1 so when I hover over it you see how it becomes a little bit bigger right like a little bit bigger right there right there right that’s happening because of transform and the transition is making it happen over a period of 0.2 seconds it happens pretty fast so that’s what it’s doing and you can see it says button colon hover so on Hover do these okay and then the styling that we want for our number is going to be in our span tag so instead of putting our tag inside of an H1 if we want to put the big circle we want to wrap hours in a span tag instead so let’s go ahead and put this in a span tag actually like this and let’s go ahead and end it in a span tag like this and let’s go ahead and refresh our app okay or sorry let’s hit command enter first so we can restart our server so it serves our new static file and there we go okay our app is looking and doing exactly what our finished version of the app was doing all right so I hope that that deconstructed and demystified a lot of what we did just do let’s just do a quick recap of everything that we’ve done right we have a flask file uh flask app and we need access to our template string we got our database okay we initialized our app right here and we initialize our database right there we created what our home page should look like and that’s div my bad okay we created our home page and what that should look like and then to our render template string we gave our home page and then the database number and we did the same thing for all of our paths we created an increment path and a decrement path so if you click the minus button you come here and if you’re the plus button you come over here okay um and then here we’re obviously just running the app okay so that’s all we did and we got the entire thing uh pretty much up and running okay now the last thing I want to do is for organization purposes normally you will not write I mean after this app you will never write your HTML here okay that is a good way to do it when you’re a beginner and you’re just starting out but what we want to do is we want to have our own HTML file so let’s go ahead here and I’m going to also hide hidden files because you don’t really need to see all those files it’s not necessary we’ll create a new folder and we’ll call it templates inside of templates I’ll add a new file it’ll be called index.html so this will be our HTML file okay and we’re going to take all of our HTML code and we’re actually going to just put it in here okay now we don’t need my home page anymore so we actually don’t even need to use this home my home page that we created here okay and what we’re going to do is instead of render template string we’re actually going to import render template from flask just render template just like that okay and then let’s scroll down so we’re going to replace all of this with rendered template render template render template okay and then instead of my home page I’m actually just going to give it the index.html file just like that so now it’s just rendering this file it’s very important you put in the templates folder if you don’t it won’t be able to find it okay because it looks inside of a folder called templates this function right here render template and there you go it’s going to just go ahead and use that file right over there okay let’s copy this let’s paste it okay so we’re done and you can essentially comment this out now you don’t need this I’m I’m just going to go ahead and take this put it below I don’t really need that anymore okay I don’t need this statement in there anymore I don’t need the show function right there anymore right um there we go okay now let’s go ahead and run this app and see if it does what it’s supposed to do or if it crashes let’s refresh and looks like it’s the same right boom beautiful plus minus minus right it’s working perfectly so that means that we’re pulling all of our data from our index.html so now it’s much more organized meaning that our our client-side logic and our on our backend logic is separated so here right we have all of our client side or front end logic all right and that’s separated from all of our logic here which is essentially our like um let’s say server side or back end logic okay so they’re they’re separated now which is really nice and uh yeah you preferably you don’t want to have like you want to have each file right should have its own language specific code so for example a CSS file should have CSS code HTML file should have HTML code a Javascript file should have JavaScript code a python followership a python code before we had a python file and had a bunch of HTML coded and I think that’s that’s plenty confusing we don’t need to do that right and CSS we don’t want to go too much into CSS but uh how it works essentially is if I go here and I show you for example the span tag if you want to style the span tag you call the span tag right here in your CSS file and you just give it styling if you change anything here you’ll see your spam tag change and I encourage you to try to change things and see if you can break things and then bring them back by just undoing right and see what happens um you know you could change your div style it you could change your button styling you could change the font size on the button right you can make the font bigger or smaller you could make the button with a bigger bit with bigger smaller height bigger smaller you can see what changes that makes and if you want to get access to counter underscore Styles file then make sure you hit show hidden files so you can show you all of the hidden files that I’ll just keep hidden for now okay so hopefully you had fun with that project that hopefully deconstructed a lot of the mystery for you with flask and python you should now have your very own fully functioning python app from the ground up if you want to see any types of solutions you can click raw solution and this will show you the solution that we came up with right now versus the guardrail solution that you should have came up with earlier uh it’ll show you the the helper file that I wrote for you it will show you the Styles file that that we have for you so I’ll show you everything okay so hopefully that was helpful I hope that you enjoyed it and things made sense hopefully you weren’t too confused and with that said I love your face and I’ll see you in the next project now we’re gonna build the YouTube app with python and flask this is going to be awesome it’s gonna we’re gonna have apis we’re gonna pull the data from YouTube and we’re gonna render it out and we’re gonna build it in a really cool way where it’s almost going to look like react code because we’re going to use components and something called macros inside of flask which is a really nice feature we’re going to show you how to use templates template filters inside of flask and it’s going to be completely beginner friendly okay so don’t worry so if you’re watching it as a clip on YouTube the link is in the description below if you’re watching it as part of this big python course that I’m doing right now well then you’ll have the link kind of available to you right over here so I’m gonna go uh into this right here and so on the right hand side if you’re in the python course you should see this big markdown file Okay click on that and it’ll walk you through this project okay so this is a project we’re going to do right now it’s going to be the YouTube app and it this will walk you through what are the concepts that are covered here okay you don’t need to worry about list of dictionaries we’ll cover that inside of this but you should know these over here okay this is what the project is going to look like and here are the three links available for you projects a live demo solution let’s take a look at live demo of this app so this is what it looks like uh this is what this app looks like now you could have you could be pulling data from Mr beasts video or you can be pulling data from my videos what that’s crazy that was so quick or you could be pulling data from Peter McKinnon’s videos and just like that we’re pulling data from Peter McKinnon’s video so you could see this app works it’s fast you could be doing this all with python and guess what it’s actually responsive so if I go ahead and change the size of it you can see boom it’s becoming smaller you see that right there right so this is fully responsive and all of this is going to be happening with python so I hope you’re excited for this project it’s going to be a really really fun project with that said let’s just get right into it so I want you to go here and click project okay this is a solution if you ever need to look at it don’t look at it right now you know look at it at the end but let’s click project okay or again it’s going to be in the link in the description below if you’re watching it as a clip this is a page you should come to and your flask app right here should already be running now if you come to this page most likely it’s going to ask you to Fork this so you’re going to come to a page that might look like this just go ahead and Fork it not a problem right there okay then you can actually make changes and do whatever you want with it and just make an account if you don’t have one okay forget and then you call it whatever you want so I’m gonna go ahead I’m gonna Fork this as well and since I’m your instructor I’m just going to call it instructor okay you could you could leave it as exercise or put your name plus exercise and then just do it okay so there we go and now we should have like a fresh fresh fresh file and when I hit command enter I should see this hello world or um or home page uh pop-up okay it’s going to pop up soon let’s give it some time there we go so now we have our home page coming up over here and we know that our flask app is up and running okay it’s it’s up and running like this is live live live live live which is so cool so freaking cool I remember when I used to use flask or do stuff with programming and I was a beginner I would just try to install it on my computer and it wouldn’t work and I’d be like how do you install flask and then when I would try to install that it’d be like you need to install pip manager and then I could such a process and like now you’re just getting this app up and running um this is awesome and like you guys should thank replit because they make your life so freaking easy okay now inside of here here’s what I want you to notice there’s only a couple of lines of code a bunch of it is not really that important these are just channels right here that you’ll need access to okay so it’s like five six lines that don’t even need to really worry about but I put this here because for this YouTube build you’re going to need access to a couple of Channel IDs I didn’t want you to have to go to youtube.com go to my channel find out how to get my user ID or Channel ID so I just gave you IDs for Mr Beast MKBHD and Peter McKinnon uh right here okay so that’s one second this is just a average flask app which you know this is just a boilerplate code that comes with flask right when you start it and we’re saying on the home page like when you route to this slash right that’s the home page just right now print this out right this takes in HTML so if I change this to H2 right here and I change that to H2 this will turn into a heading tag and it’ll become like that and then this line here is just running our app so if you remove this line your app won’t run okay so that’s that’s it that’s it for all of these lines of code we’re done understanding that next up is our static folder this I don’t need you to touch at all do not touch this at all I’m just leaving this here for you but this is where your styling is going to be for this app so in order to make it look beautiful kind of like this a final version of the app right you’re going to need access to that styling and since I don’t want you to waste a lot of time doing The Styling we have essentially done The Styling for you so as long as you provide the HTML structure of the page correctly and name the classes and divs correctly which I’ll show you how you can do that you should be good to go alright so CSS don’t worry about this you can pretty much ignore this don’t delete it but you can ignore this okay so you are just starting from here and now you understand every line of code in here like this is not that important I understand this is a boilerplate code that comes with flask that’s really it okay so a few lines of code and we’re getting started hopefully from a point where you and I both are at a pretty similar understanding of what’s going on here okay now let’s build on top of this now what I want to do is I want to pull in data from YouTube that’s the first thing I want to do so the first thing that comes to my mind is let’s go to rapidapi.com okay and they’re going to make your life really easy in terms of pulling data and it’s free sign up here and we’re going to go to YouTube okay and uh go YouTube and instead of having to like sign up with YouTube and try to pull data from there it’s going to be harder rapid API makes your life a lot easier okay so you it’s if just take my word for it if you haven’t done too many apis and it’ll even generate code for you okay so this is going to be the really nice part so watch this I’m going to zoom in here a little bit so you could see but if I want to make a request it’s going to give me all the code that I need okay so for example let’s look at what I want so let’s look on this left hand side oh first of all if you are new you’re gonna need to go inside of pricing and you’re going to need to subscribe to the zero dollars per month it doesn’t even ask you for your credit card so just go ahead And subscribe for it and then click come back to end points okay and points is where you need to be now inside of your endpoints this is the API that tells you with the YouTube data API you can add various YouTube features to your app use the API for search videos playlists channels and more more what we’re going to be doing is we’re not going to be using their search API though you could okay but we’re going to be using their Channel and Channel videos okay so for example what this should do is if you give it a channel ID like this type of ID right here it should spit back out videos for you for that channel so for example if we go back to our app and I grab the idea of qazi right here we go back and we go here and we paste it watch what’s gonna happen this ID right here is going to change dynamically okay so watch I’m going to paste keep your eyes there paste right and that ID just changed because it took it from here and plugged it into there okay great now this is node.js code that’s not what we want we want python code so let’s hover down and let’s go on request I’m going to hide myself here so hover down here Python and choose request okay that’s what we want so now this is the code from the request library that should be showing you okay now we’re going to go ahead and click test endpoint so we’ll see what type of if we send this request here let’s see what type of data we get back so I’m going to hit test endpoint and boom there we go so here’s the data structure okay and this is something important to understand I know we didn’t work with this type of data structure too much before so it might be a little confusing but it’s at the end of the day it’s a dictionary okay this is a dictionary and it has only two items inside of it um you’re gonna get scared when you look at these items because they’re pretty crazy items but just understand the underlying data structure is something you already know this is a dictionary with two items okay it starts right here and it closes right here has two keys only that’s it it has the contents key and the cursor next key we don’t need access to the cursor next key the only key we need access is contents okay now inside of um let’s go click inside of content so I’m going to click inside of contents it tells me that there there’s 30 items which tells me as content is a key and is a value it’s storing a list and that list has 30 items and my gut feeling before I even look at it is that it’s probably videos that’s what it’s actually storing as items okay so let’s click here and that’s what we see 30 items and every single one of them is a video okay and each item is actually a dictionary so this is a complicated data structure right but it’s basically a dictionary so it looks like that like the squigglys let me go make it here okay so this is what the data structure looks like right now okay I’ll make it right there so you have a dictionary okay inside of the dictionary you have what you have two items you have two keys you got um Let me let me manually write this for now okay so like this okay we’ll just call this dictionary data that’s like the topmost one and it has content as a key and that has a list right and then it has another one which is cursor next so I’ll just say cursor next and I’ll just say blah okay so we’re not going to be worrying about cursor next now inside of content it has videos and so each video is a key value pair okay it’s like this so these are empty dictionaries now each video If we go inside uh each dictionary has two keys inside of that so it has a key type and as a key video so let’s write that out so we have type and it says video and then the other thing other key it has is called video [Music] Okay so let me show it to you like this okay all right boom okay and what does video have well video is a key value pair and as a key you have video and as a value of that key we have a dictionary with nine items in there okay so let’s go ahead and take a look at it video and this is a dictionary here and this has a bunch of items in here so it has a is live now is fall so meaning like this video is currently not live so this is false and if we take a look at other stuff let’s see publish text if we look at that so published time text right if we look at that it says null it has uh it has not been published yet so it’s a video I scheduled but it hasn’t been published yet by the time you’re watching it’ll probably be published so this here would be a none okay if you see a null in Python that’s a nun most likely okay the next thing it has are stats so stats once we understand this data structure the rest of like making of the app is not going to be that hard but this is like vital vital vital that we understand this data structure because if you understand this and we can manipulate it already then our life is going to be really easy we’ll just plug in the request into our app play out the data structure and we’re ready to go okay so it has stats okay so stats has one item and just one key of views so stats key uh and then as and then stats is like a dictionary okay and then inside of there is you have views views and uh this video has one view so far okay but we’ll just write let’s say 23 000 views okay great and then we have thumbnails okay that’s another data structure we’ll have in here so let’s go ahead and write that thumbnails thumbnails okay and thumbnails is a list and it has four keys inside of it okay but we’ll just make two keys that’s enough so when I was going through these what usually they are is they’re they’re just all um they’re all thumbnails so it’s a URL of the thumbnail and then it tells you the height and the width of the thumbnail so what I realized is the zeroth one is the lowest quality thumbnail and then the third one is the highest quality thumbnail okay this last one is the highest quality thumbnail but not every single um video has four thumbnails some have like one and some have four okay so I’m just gonna say this video has like let’s say two thumbnails and let’s just take a look at what the thumbnails look like so I’m I just care about the URL you’re not gonna need to worry about the height or the width so I’m just gonna put the URL here so I’m just going to say URL and blah blah.com okay put another URL here I will say link.com okay there you can put a comma here okay so if you think about this right this looks complicated but what’s Happening Here I have data data has how many keys let me ask you this and let’s just practice this right now okay data has how many keys data is a dictionary right and data has how many keys I’ll give you a hint it’s between one or two that’s the answer either one or two how many keys does it have if you guessed one close but may you might have forgotten about the second one so it has one key okay content and it has a second key cursor neck so it only has two keys okay great now um inside in in these two keys the only key we care about is content so we’re gonna go ahead into content now now inside of content you’re going to have about 10 20 30 items because it’s going to get you like the last 30 videos of the channel okay so if I get back my last 30 videos each video is going to be what data type each video is going to be represented either as a list or dictionary which one do you think okay take about five seconds to answer that five four three two one each data type is actually going to be a dictionary okay so it’s the dictionary actually starts from here right starts from here and ends right there all right now you can have other movies there for sure so like if we could have other videos there uh let’s just put one more video so we could have like a more accurate um way to view this so let’s look at another video so this is like build modern portfolio with next.js and we can see time text is 10 days ago so let’s put that there which time taxes 10 days ago is live now is false stats are this is how many views it has we’ll leave this like this okay so now we know that this has how many so the content has how many videos inside of it can you tell me right now okay I’ll give you five seconds five four three two one content is a list with only two items okay you could even call Len on this now you have a third item but it’s empty so I could delete that item but I’m just leaving this for now so technically yeah you have three items but like two actual videos right the other one is like completely empty so that’s what it is if I if I close this right you could see all of the items okay let me even let me remove that too so you can just see content right now has only two items so let’s just let’s play with this right now let’s just prove that this is true so let’s go ahead data and let’s go ahead and check the type of our data okay I know this is like kind of boring stuff it’s like why are we worrying about data structures or whatever but trust me like you’re gonna these Keys Should Be Strings okay so that’s what the error is right now let’s run this so yeah right now I know that it’s going to be like annoying or it’s going to not make sense and it’s like why are we worrying about this nerdy stuff right now where’s the app called that you’re showing us I promise you if you learned this the lists and dictionaries and these data structures underlying you’re going to become so damn good because everything every API requests everything you try to do on the web is going to come to you as a list of dictionaries okay lists of dictionaries is literally going to be everything knowing how to move through it Traverse through it get the data again in and out that’s going to be a game changer for us if you get good at this you’re gonna smoke everybody else okay trust me on that um it’s getting confused what some of these are so I call them as variables because I’m used to JavaScript so let’s just go ahead and put quotes around these um so we we tell it like hey this is the key all keys should have these quotes So URL should have a quote around two we’re gonna need to do that for other and let’s put quotes around these quotes around this quotes here quotes here and we should be good okay let’s go ahead and run this all right let’s see what we get in our console so we print data right so it’s proving to us that that is a dictionary right now what we want to do is we want to get uh content okay inside of our data we want to get content so let’s see if we can get content and you can see on the bottom here we’re actually getting that content right that content is actually popping up right over here uh it’s giving us all of the data that that we’re looking for so for example if I move this up make myself a little bit bigger here if I move this up you could see uh I can see the type I can see video I can see it has another key called video and then that video has all of the data that I need right so when I’m getting content let’s say all right now whenever I get these lists of dictionary stuff and all of this and I’m printing it out usually being able to see it in a nice way is actually a game changer when you see it like this it’s usually very confusing it’s like what the heck are you looking at how many items are there it’s kind of hard to like visually understand what is happening so what I recommend is you use something called P print okay and um and it’s something you’ll need to install so but don’t worry repl I believe will install it for you automatically and we’re just going to go ahead and uh give this pretty printer indent of two okay and all it does is gonna print it but it’s gonna make it pretty so it’s just going to make it so it’s like more this this here is more readable okay like for example if I take this and I go to like Json format or something online and I pop this in um and I process this it’s going to make it prettier for you okay so like you you’ll be able to go and you’ll be able to actually like look at your data and you’ll be able to like hit minus plus plus and you can actually see like what the heck is happening okay so that’s kind of why I like some Json formatters but let’s just use uh pretty print for now and see if that can actually do the job for us so I’m gonna go ahead and say so we can just do uh p p dot P print okay like that so instead of a regular print we’re gonna use like a more pretty print and we hope it does the job for us let’s reduce the indents to one and let’s see what happens okay all right there we go so that is still looking a little wonky right but it does look a bit better so I can I can kind of like see it more now right uh so this right here I can see uh is my first video right here and this right here I can see is my second video okay now um let’s see how we’re gonna get uh deeper into the nesting of this right so for example if I have content well if I want to get this value over here video how will I get access to that if I’m inside of content and I want to get access to the first video let’s think about that if I want to get access to the first video how do I get access to that so here’s how I want you to think about it right usually when you look at this this is going to overwhelm you and kind of complicate things now it’s looking complicated but if you think about it at the end of the day right if I go into contents here or even like our contents right here these are just two items so if I want to get the first item in a list remember you remember how to do that right remember we had fruits and we had a fruit right here how do you get this guy it’s going to be simple okay I hope you got this so let’s go ahead print and we’re going to say data content of zero so this is going to right here and I’m going to comment this out right now this right here right if I look it’s giving me the first it’s giving me the first item right in this case this is the first video and it’s giving me that right there okay well then how do I get further inside because there are two keys inside of um inside of this right so if I go in here you could see inside of content and even the zeroth items when I get the first video so this right here is vid one video right inside of here I have two keys key number one and key number two I don’t need access to key number one right I don’t need access to key type so how will we get access to the video and the value of the video I want access to like all the data of this key video how do I do that can you guess so see what you are going to put here so you can get access to all of this okay so take five seconds if you don’t get this that’s okay we’ll get this together take five seconds so try it yourself or pause the video five four three two one all right let’s do this together so now at this point I know I’m already getting this dictionary right here if I want to get access to this video well that’s as simple as just doing video okay if I now hit enter I’m not going to get this type key thing anymore and I’m just going to get that dictionary AKA this part right here okay that’s the part that I want that’s the meaty juicy part that has all my data now that I have access to that video well what do I need access to now well I’ll need access to if I look at our finished app I will need access to published time text for sure I’ll need access to the title of the video so that’s important so let’s go ahead and grab a video and copy the title here right title so let’s come down here and let’s actually add that in so I’ll go ahead and dictionaries type order doesn’t really matter and we’ll just say build [Music] whatever right I’m just copy pasting this you don’t you don’t necessarily need to have this exact title if you have any other title make sure to put the comma there so I’ll put a title here and then we’ll also put a title for this video and we’ll say title is uh uh let’s say I’ll grab just like some other random videos title because this data does not need to be like accurate right it doesn’t matter and we’ll just grab that title okay bring it in and I’ll post it paste it right over here okay there we go cool so I have two videos and they both have titles now okay so let’s go ahead and grab the first video’s title well now since I have access to video right inside of video these are all the keys I have access to is live now publish time text stats title so in order to get access to title what will I need to do you give that a try okay you’re gonna add something here and what’s the key you’re going to add here to get the title I’ll give you five seconds five four three two one let’s do it together I’m going to go ahead and say title okay title will get me access to that ah I probably made that mistake again of not putting quotes around um everything so let’s just double check what I did here title app I forgot to put a comma here comma okay there we go all right and let’s go ahead and run this so I should see this title right here build medium with next.js and there you go you can see that build medium with next.js right so this is the first video and once I guess once I get the first video I can get that first videos all kinds of information about it right so for example I could say first video and save it in a variable and remove this title part from it so this is actually going to get me access to the first video so let’s go ahead and print first video and let’s run this and then if I want to get the data um let’s remove that parenthesis so I’m getting access to the first video now if I want to get the data from the first video well I could actually just do print first video title right if I want to get the publish time text I can go published time text and if I want to get let’s say thumbnails I can go ahead and grab them just like this oh oops oops thumbnails okay and now I should get all of that data about the first video so you can see I’m getting the let’s let’s comment out that first line because I really just want this okay so there we go title is that right publish time text is basically like oh this video is not really published right so that’s why it says None um and then thumbnails it gives me uh a list of thumbnails so in this case we just have these dummy URLs showing up right those are the those are the quote-unquote thumbnails okay so that’s the data structure that’s we are looking for okay that’s the data structure that we’re gonna once we retrieve it we’re gonna be able to do stuff with it now I think that we’re able to understand what’s happening here right and then as it gets more complicated understanding this underlying data structure will be super valuable for you and now what we’re going to go ahead and now we’re going to actually start pulling the data dynamically into our app so we can go ahead and make that request that we were making on rapid API except let’s go ahead and do that in our app so let’s go ahead and copy all of this okay and let’s go inside of our app and I’m just gonna go here I’m gonna paste it okay I’ll take import request and I’ll put it all the way at the top so request is a library and um you know a replit will install it for you if you try to run but you know you can always go to the package manager and install it if you don’t have it for whatever reason cool and then yeah and then let’s just go through what code it gave us right so this URL here is going to be the API endpoint that we need to hit which don’t have to worry about too much but this is we’re gonna be making our get request okay this query string right here is where we’re going to be passing in our Channel information so for example remember I gave you channels earlier well let’s go ahead take those channels and let’s put them all the way at the top of the app so every part of our app has access to these channels and now we’re making the query to the specific channel here we’ll remove this I’ll go here and I’m going to go ahead and remove that and instead I will write channels and I’ll just write quasi okay so this is going to get you clever programmer or let’s just go ahead and change this to clever programmer right clever programmer let’s change this to clever programmer and there we go you’re gonna see that and this is yeah and then delete these things you can just leave and ignore because rapid API gives these to us you can ignore these two these are important and required so you can’t remove them but this is like API Keys some like tokens whatever like this is rapid API giving it to us so this is like important stuff right so just leave it there but you can kind of ignore it and then here it’s actually making a request for you and then it’s passing in the headers and all of the information that’s necessary so it’s making a get request it’s passing in this URL as an endpoint and then for headers it’s passing in this information which means like it’s passing in the sensitive access tokens the password AKA um and then it’s passing in any query parameters that we might have okay great so you know for query parameters is passing this it’s passing in you know this is the channel that we need to hit okay and these are the languages that we need to head and that’s a response that runs and then it will go ahead and get that response for you so let’s comment out all of our other stuff that we were printing out actually we don’t have any other print statements right there let’s comment out all of this here for now and let’s go ahead and run this once we run this we should see response.txt show up on our screen let’s see there we go so where now you can see all of this data so this is about like 30 videos that we’re getting back from clever programmer okay and if you go really looking searching inside of it you could see it says build a to-do list app with solidity right there right so that tells you that um for getting back real data okay it just looks ugly and don’t worry we’ll we’ll actually know what to do with it afterwards but it was important that you understood the overall structure before we went in okay and this will be called contents most of the time so I’ll call it contents here too so in your notes it’s easier because that’s what they’re calling it right contents if I go here and we see the response right you see contents and not content okay so for this response well now what I want to do is I want to actually put this inside of our home so whenever somebody refreshes the page we want to make this request every single time so I’m gonna go ahead and starting from this URL Point all the way to this I’m just gonna cut it okay and we’re gonna go down here inside of our index function and I’m just going to paste it in there okay now I’ll need to go back back and delete uh and enter okay so I can adjust my indentation because if your indentation is off your app will break which is annoying but just how python is all right and there we go so now I fix my indentation so all of this stuff should be underneath the index function so right when I hit the home page boom uh this request should run and get the data okay so let’s give that a try let’s just make sure everything is running so I hit command enter to restart the server and yes I’m getting back my data very good now instead of getting it back as a response dot text I want to actually get it back as a Json so I’m going to go ahead and I’m going to say data is equal to response.json okay that’s the data and let’s go ahead and print out the data okay and let’s run this and you should see all of that data right over here okay so now we’re getting it back as a Json and what I’m going to do is I’m going to import just sonify from flask and that allows us to just take a Json dump it out onto the screen and I’m gonna return and I’m going to say jsonify and I’m going to give it our data okay and we want to dump it out onto our home screen rather than our console so let’s go ahead and hit enter and let’s just see if we can do that right now so we want to dump all this data yes and we can this is huge success right now like huge right so if we go here we can see this data is being dumped and I have a I have an extension I forget what it’s called but it’s some kind of Json I think it’s Json viewer that’s the Chrome extension I have and it makes the jsons very nice for me so like for example I can play with the Json really nicely so here I can see I have this data and it has two keys contents and cursor next if I hit plus it opens up all of this stuff and I can hit minus minus minus and like every single video object uh video dictionary I can close it or I can like open it and then go through close its keys or open their keys Etc so it allows me to like basically go through and like understand the data structure really well so I would say it’s pretty helpful you might just see if you don’t have this extension you might just see like a straight up white ugly looking page that looks very similar to this so like if you don’t have that extension uh this is the page you should expect to see but that’s not very useful right okay great so that means we’re getting the data and we’re putting out the data now all we are really looking to do is like make it look pretty that’s it we’re like we’re pretty much done doing a lot of the dynamic heavy lifting of this app that we needed to do okay now let’s go ahead and do that so I’m gonna go say I’m gonna say contents okay and I’m going to get I’m gonna go inside of this data right here and I’m gonna pull out this key contents right because when I’m looking at this so that dictionary right there I’m calling that data okay so that’s called Data now inside the data I’m going to index it and just pull the contents in here so that’s why I’m going to index it and ask for it to give me the contents okay just like that so let’s go here contents and let’s go ahead and print contents now and see if it prints it out into the console yep it is um it’s in the console all the contents right you guys I can see a lot of information about video video with thumbnails I’m seeing all kinds of stuff so that’s a really good sign now what I want to do is I want to write a nice little um for Loop and what that for Loops job is this I want to create an array of all of these videos except I do not care about this key right here at all I don’t care about this key right here so I want to ignore that key type and the second thing I want to ignore is I don’t um the second thing I want to ignore is any video that’s not been published so on my app I only wanted to show the videos that have been published anything that has not been published I don’t want it to bother even showing me that because I don’t care I don’t care if it’s not been published do not show it to me so let’s go ahead and write a list comprehension that can actually do that for us so I’m going to go ahead and say videos and I’m going to say get me the video for video and contents okay so I’m gonna basically I’m looping through this contents and I’m going to say if the video and let’s index the let’s get the video key so we’re making sure essentially that we’re not getting this type thing we’re specifically saying or return this okay so if that video um has a published time text okay so only get the video if it has published time text if it doesn’t meaning if it has null it won’t get it and here we are saying video All I’m gonna say here is just get me the video key so don’t get me the type key at all okay so that’s videos now let’s go ahead and print out videos and let’s see what we see okay and now let’s uh let’s see if we can get videos to be in this jsonify let’s see if that works let’s give it a try it does work so let’s refresh so now we can see that yep so now we’re just getting a new list right and that list just has videos in it straight up there’s no dictionary called key there’s no extra key uh keys in there there’s just a dictionary with a video data inside of it and that’s all that’s all we’re getting and this should be about like 20 or 30 videos right so boom right there are all the videos they’re stored in a list called videos and if I open this up there’s no key called if we look at all the keys you know there’s no key called uh type and now we just have all the useful keys that we need like badges is live now length seconds blah blah and every single thing that publish time text was null is now been filtered out okay that’s the nice thing about this this is saying only get me the ones that have published time text that’s it so if it has a publish time text get it back to me okay and out of those just do this okay so we’re we’re mapping and filtering here which is very nice and python allows you to do that with list comprehensions which are very handy okay so we’re getting our videos now what we need to do is we need to render our template and we need to send this data back to our index.html and start playing with this data there okay so I’m going to go ahead and I’m gonna say render template and I’m going to say render index.html and I’m going to pass it these videos okay so I’m going to take these videos and I’m going to pass it and give it this value videos so in the in the front end in our HTML file we’ll have a variable called videos that we could then use okay now this render template make sure that we import it from flask so I’m going to go ahead and import it because we’ll need access to that okay and then what we’re going to do is create a new folder called templates and inside of templates we’re going to create index.html and in here let’s just go ahead and say H2 and let’s say videos let’s just make sure that this page is actually loading so I’ll put this down here and our videos page is actually loading let’s make sure that that page has access let’s make sure that this page access has access to all of our videos so I’m going to go ahead and save videos just like this so let’s see if it has access to our videos variable nice you could see it does okay now let’s just give it one video okay so let’s just get our first video here so I’m gonna say first video and I’m gonna go videos of zero and let’s just give it one video and let’s see how it deals with it okay so we’re gonna say first video equals first video and we’ll go ahead hit save command s and here I’m gonna go ahead I’m gonna say first video so we’ll just get and I’ll I’ll delete this for now okay let’s run this and uh that’s it we’re getting back just enough data for one video okay now what we’re gonna do is um we’re going to go ahead and just start really like pulling the data out that we need okay so for example uh let’s go ahead and in this H2 I’ll say first video and I’ll say title and let’s put these double squigglies here so it knows that this is like a python code and not just HTML so let’s write that and let’s see so boom I got my title of the video okay so that’s very good let’s go ahead and see if I can get the published uh time text let’s try that okay and then after this let’s go ahead and see if I can get the views of that video so if I go look I have stats and then stats has something called views so I can go stats and then I can go views just like this so let’s see if I get my stats and you can see I’m getting my stats and then the next thing I will need is I will need a um I will need let’s go look at our final app okay so I have views so here I want to say views after this okay views and then I want to say the channel name okay so we could we could hard code the channel name if it doesn’t give that to us yeah it doesn’t seem like it gives us the channel name in this case and the next big thing big obvious thing we need is this image we need to get these images from here okay so let’s go ahead and grab the image as well so I’m going to go ahead I’m going to say image tag okay and for Source I’m going to write Dynamic python code here and we’ll say first video okay and let’s go ahead and just call it video instead of first video you’ll see why the rest of our code will become a lot easier so let’s go ahead call it video and video here okay so it’s just one video and then let’s go ahead to our templates okay and let’s rename all of these and call them okay let’s see if it allows us to play with this ignore that sound that’s the sound to keep me productive I’m going to select all and I’m just going to change this to video and that’s it okay all right now video now let’s go look at this data structure so how do I get my thumbnail so I’m in my video then I will in uh I will get this thumbnails key so I’ll go ahead and do thumbnails okay and let’s put a double quote around here double quote okay so I get my thumbnails now then after I get inside of my thumbnails I need to get my zeroth element so I can get zero one two three four or zero this is zero this is one this is two and this is three okay so I could get any of those this would be the lowest quality and then this would be the highest quality okay but let’s just start with the zero one so I’m going to go ahead and I’m gonna say zero and then I need to get the URL right so after zero I need to get the URL so I’m going to go ahead and say um URL okay so grab the video get its key thumbnails inside of that get the first thumbnail and then get the first thumbnails URL that’s basically what I’m saying here and let’s end our image tag here okay let’s self close it let’s run this let’s see what happens and there we go we’re actually getting that image back now right we’re getting back the image and uh let’s comment this line out so it doesn’t run okay let’s do this so now we just see all of this stuff so it’s already looking pretty clean we’re dynamically getting the data you know if we change this we already know this app is dynamic and I could prove it to you by if we go here and you replace cozy here right because we’re giving it in if we go to query string we’re giving it in from channels clever programmer but you could also give it Mr Beast Mr Beast right here so just give it Mr Beast uh inside of channels and you’ll notice that it’ll pull a Mr Beast video and show that there we go all right holy crap that’s a lot of views right so you could see and it was one day ago so you can see this app is already Dynamic it’s pulling the data it’s doing the things that it’s supposed to do and uh those are really the four pieces of information that we need if we if we’re looking at this right we need the image we need the title of the video we need the streamed how many days ago how many views and then the clever programmer part and the logo is hard coded okay so that will remain the same so now at this point we have to make this card here look really nice so let’s create a card and make the card look uh look nice okay let’s go ahead and do that now so I’m gonna go ahead and cut all of this for now or just comment it out and leave it let’s let’s select all of this and let’s comment this okay and let’s go here and let’s just make a card so I’m going to go ahead div there’s going to be some HTML stuff here that we’re gonna do don’t worry about it too much but the only thing I want you to worry about here is just make sure you’re really following along and writing all of these lines lines and especially you’re naming your divs the same and your classes what I’m naming them here so let’s say we have our class card okay inside of that we’re going to have a let’s start it backwards so then it’s easier for you to see okay so let’s take our image and let’s work backwards so I’ll take our image I’ll uncomment this uh okay that’s annoying would not take this and paste it here okay so let’s take this image for example let’s run our app so basically right now it should be just showing you that image right there okay and something to keep in mind if I change that to a three the quality of that image is going to become higher so I want you to keep this in mind for future because we’re gonna we’re gonna be doing something with this we’re gonna write a piece of code that’s going to dynamically always choose the highest quality available all right so now that we have this image um what we want to do is we want to put it in a thumbnail container okay so let’s go ahead here and this is where this magical style file that we’ve created for you is going to start coming in real handy so do me a favor and at the top just link this file okay so at the top I want you to just say uh link and we’re going to say a style sheet not ref I meant Rel okay we’re going to say Rel style sheet and then for href I’m gonna go ahead and say static slash styles.css okay and we can just close this self-closing tag all right now let’s run this okay so you shouldn’t see too many changes yet except there is already some changes coming up so if I go to this app right now you can see that things are already kind of centered right and our image is already in in the center there and and the background is dark okay so that already happened from just bringing the style file so that’s a very good sign now what I want to do is make our card so we’re going to finish making our card so in here I’m gonna go ahead and say class and we’ll set thumbnail container and let’s take this div and wrap it here so let’s wrap that image inside of this thumbnail container and let’s run this okay now it’s starting to look already better now we’re going to create a card so I’m going to say class card and that’s going to wrap everything okay so everything about this card that we’re making here I’m I’m selecting everything and hitting tab everything that we’re making about this card here is going to go inside of these card divs okay so for example everything is going to go inside of this card diff from this point on okay so let’s run this okay so now here’s what you should see you should see a card and there’s like already some some stuff in the back right your image is already going in the middle so that’s good okay next up what we’re gonna do is let’s go ahead and give this image a class now so we’re going to give it a class here so image source is going to remain that but class is going to be thumbnail okay there we go so now this image should be looking a lot better okay and then next up what we’ll do is I’ll I’ll just give it three right now so it looks good and then in the future when we run into that error because some of the images don’t have a three uh they only have low quality available and I’ll show you what we can do all right so boom there we go thumbnail container is good now let’s create an info container where we’re going to store information about you know the title uh of the video the the channel name how many views how many days ago it was published Etc okay so let’s go ahead and do that div and then let’s go and we’re gonna say class and we’re gonna go info container on this and inside of this info container we’re going to put all the stuff that we want to put okay so div I’m going to create another div here and we’re going to call this Channel logo container really make sure you’re not making spelling uh errors here because what needs to happen right now is your your code that you’re writing needs to match up with the CSS file so for example let’s say you’re making this card component right and I go here your class here that says card needs to match up with what it says in the styles.css as the class for card because if they don’t match up all the styling for the card you’re going to lose it so for example let’s just say You misspelled card and you must you you wrote card with two D’s well that ain’t gonna work buddy because if I run restart the server and run this apparently that’s working okay I don’t know what to tell you there that’s not supposed to happen why is that happening um okay if you keep doing that at some point you’re gonna get into trouble okay if you keep misspelling all of this stuff here right let’s just give this a try let’s restart this it’s probably my style sheet is not refreshing or something that’s what’s happening right now okay but just trust me if you mess with this you’re going to get into a lot of trouble so don’t do that just make sure that you’re staying consistent with all your uh just don’t make typos okay you should be good let’s go back to our file so thumbnail container okay or info container sorry and then Channel logo container and then inside of this Channel logo I’m gonna just go ahead and drop this logo and um you can copy it or you can paste your own logo so here’s the logo that I’m using this is a this is the clever programmer logo and the classes Channel logo okay that’s all I did here I just popped this image in okay inside of this Channel logo container so let’s run this now and we should see a logo be logo is popping up okay very good so we’re we’re good with our info container now we still need more stuff in our info container we’re good with our Channel logo container but we need more stuff in our info container here what I want to do is I want to have another div and a class and we’re going to say info and let’s end this div and within this info I want to go ahead and create a P tag and I’m going to go ahead and say I’m going to say video title just like that and let’s see if that shows up so let’s hit command enter and restart the server I’m going to make myself a little bit smaller so you can see there you go you could see that it says should I start a Hindi coding Channel next up we’re going to go ahead and create a span tag and I’m going to say clever programmer and we’ll do a line break after this let’s go ahead and do this okay there we go now that’s being popped in correctly okay and then what we could do is pass in another span tag and this span tag will have the stats of the video so I’m going to go ahead and say views on this okay and we’ll we’ll write the word views after this so let’s run this and let’s just see what we get so this should be the actual data and then this should be the word views showing up right there okay very good make this a little bit bigger okay and then I’ll put this uh dot here it’s option eight on Mac on Windows I’m not sure what it is if you don’t have this dot copy it from online look for DOT something if you can’t get it or don’t want to get it then just don’t and put a period there or a dash that should be fine too okay and then the next thing we’re going to put here is the time text okay so I’m going to go ahead and save video uh published time text okay let’s hit command enter so it restarts our server or control enter if you’re on Windows and there we go it says streamed five days ago so if I go to this app and I refresh I can see all of this data popping up here which is a great sign the only concern I have right now obviously is we only have one app one card and we don’t have lots of other cards okay so that’s that’s a concern but it’s not that big of a concern now here’s what we’re gonna do I’m going to refactor some of this code and and make it make it pretty nice actually okay so this card component here we can make it its own component I’m gonna get rid of all of this here okay and we can use it like a component how we do in react if you have now if you don’t have experiences around it’s totally fine but all we’re gonna All I’m gonna say is I’m gonna make it just nice and organized and easy to use okay so let’s go ahead and do that so I’ll show you how to create something called macros it’s really really nice uh to create macros okay so I’m going to create a new file and we’ll call this components dot HTML and I’ll paste this code in here I’ll delete the style sheet link okay at the top I’m we’re going to go ahead and put this percent sign so only one squiggly and then a percent sign okay not two squigglies this time and I’m going to go ahead and say macro card okay and it’s going to take in a video as a function so literally this is like I’m writing a function here okay and then you’re going to put a minus sign and then a percent sign okay it’s weird I know but just trust me just do that your life is going to be great okay and then at the bottom we’re gonna go ahead and say end macro okay but make sure you put a minus sign here I’m going to say n macro and a percent sign and done so our macro is done here now in our index.html look at how clean this is going to become remove everything okay and simply what you could do is now you never have to look at this component again since you’ve made it already once you don’t have to worry about it and what we could do here is a we can actually do this go all the way to the top and we can say from from components dot HTML import card okay so now we can import card okay let’s run this app and just watch what’s gonna happen so if I run this app really like nothing should be showing up like no card or anything should be showing up okay because we’re not rendering this card component right now it’s our index HTML that’s the main file components is not running right now now if I want to show the card component all I actually have to do is I have to just go ahead and say card and pass it the video that I have that’s it now once I passed the video card component once you pass it the video it already automatically knows what to do once you pass it the video it goes here it sees videos as a function it then takes this video goes here grabs all of this it uses a variable video here so it literally works like a function inside of python which is freaking epic it’s so cool right so there you go you have a card and now what we want to do is we have lots of videos right we have 30 videos not just one video so we want to write a loop that can help us access all of those videos and just iterate through them so we are already sending back this videos list okay so and we only send this video video back uh for dummy data and like playing around and seeing if we could make a component now that we’re done we could pass our entire list of videos and then just Loop through and make a card for all of them okay so let’s go back and I can just go ahead and write a for Loop so I can say four video in videos and pass it to our card and then just end for Loop just like that and if you run this this is going to be kind of crazy but I think literally the entire app will work let’s see so let’s see hopefully we don’t get a crash okay we got a crash it says the there’s a video that does not have an element three if you could see that three right there okay so that was the error I had warned you about so let’s just put replace that with zero look go back to your components.html replace that with the zero and hit command enter and run this again okay and let’s refresh this page boom there we go we just got everything right like everything is showing up this is insane um there’s just one thing that we have to fix with styling like you see this is if we look at this in the final solution there’s a gap right here right between each card but if you look here there’s no Gap it’s just like one big thing so all we want to do to fix that is in our index.html we need to just create a div and we need to give it an ID of home and we need to put the whole thing inside of that okay and run this and that should fix our problem okay and let’s go back here and let’s refresh and look at this it’s done it’s showing up right if I make it smaller right it shows up one by one so it’s fully responsive as well all right um that is so nice and then one last thing that I want to do is here it says 40 998 views if you go to the final version it says 41k views 13k views 44.5 K views so that’s how YouTube is actually YouTube it says either K or M for million so what we’re going to do is we’re going to use this Library called numerize it’s really nice and here’s how it works so I’m going to go ahead and open up our command line here and let’s go and pop this bad boy open make it make it huge so you could see it gonna go right there I’ll move this here we’re going to say python okay and so just in the Shell just run Python and um from numerize dot numerize import numerize okay and if I call numerize on a number like 23 000 watch what it’s going to do it’s going to turn into 23k if I call it on a bigger number it’ll turn into 230 million and turn it as a string okay so numerizes really nice if you don’t have numerize you could probably just do if you for whatever reason you don’t have your pip install numerize or you could install it from this package manager right here click that and just type in numerize and install it okay but if you Fork this the right way you should already have access to all of this okay so numerize a super nice Library so we’re just going to use numerize for what we’re trying to do so I’m gonna zoom out here okay and we’re gonna go and say from numerize dot numerize this only annoying thing about this but that’s fine import numerize okay so we’re going to go ahead and do this and I’ll show you something really cool in flask you can create these filters and they’re awesome if you have experience developing with react ever man with flask you could create these uh template filters which is essentially like you’re creating something inside of your context API or Redux and your entire app has access to it it’s awesome but if I’m going to keep it at a simpler level and you’re a beginner watching this for newbie it just all it means is awesome and it’s very easy it’ll make your life simple okay that’s all you need to know about this all right so let’s go ahead and create a um not this but we’re going to set template filter okay and then this template oh don’t put a colon there so inside of this template filter here’s what we’re going to say we’re going to go ahead and say Define and I’ll just call it numberize okay this is going to be our function it’s going to take in a number of views so you’re going to pass it views it’s going to be a function we’re going to make and it’s going to return it’s going to just call numerize on it and it’s going to pass in views and we’re going to say comma 1. How many decimals do we want okay that’s it that’s what this filter number is that we created is going to do it’s just gonna depend on this numerize thing okay so uh once we once we do this you get here’s what we can do now okay now that we’ve created this filter here’s what we can do so we can go back to our templates and let’s go to index.html or let’s go to our card component rather so I’ll open a new tab here let’s go to our card component and here I’m pulling views and what we could actually do is I can actually just pass this guy numberize like that like a filter okay and once I actually pass this markup filter it’s going to take this number of views here and call this numberized function on it and do what needs to be done so let’s hit command enter and watch it in action there we go 41k views how freaking sick is that that is awesome right 41k views 13k views 44.5 K views 15.7 K view so it rounds it to one decimal 170.1 K views I mean it’s it’s gorgeous right it’s uh it’s it’s doing what it needs to be doing I mean it’s awesome so in terms of number eyes we’re good now we just need to create one more filter and all this filter needs to do is grab the third image if it’s available and if it’s not available just grab the xeroth image okay grab the highest quality image if it’s possible okay so we’re going to go ahead create another filter and this is super cool I’m in love with the flask templates they’re so cool so I can go ahead and say template filter and I can say to find highest quality image and it’ll take in images because we’re going to give it a list of thumbnails and what this will do is it’ll return the uh is going to return images of three okay uh and and the URL of that if the length of images is greater than or equal to four so meaning if I have a thumbnail let’s go here right if I have uh if I have thumbnails and that has a length of 4 or greater then just get me the highest quality one but if it doesn’t have a length of four meaning it has a length of 1 well that clearly means that it doesn’t have this and doesn’t have this and it doesn’t have this just grab me the zeroth one so if it doesn’t have a length of four or greater just grabbing the lowest quality one because if you try to get this one it’s going to error out because it’s going to say it doesn’t exist right so that’s what it’s going to do so we’re just going to check for length and if length of 4 or greater then just grab the fourth one and if it’s not length of four then just or if it’s less than length of four just grab the zeroth one that’s it okay so grab the highest quality what you want if the length is greater than or equal to four else grab the lowest quality one let’s go ahead to our components and here I’m going to remove this okay I’m just going to give it remember this function takes in a list of thumbnails okay so let’s go ahead and even maybe rename it to thumbnails thumb you know it takes a list of thumbnails and so this is a list of thumbnails so pass that to that function we’ll call it highest quality images okay and now if you see keep looking at this image and if I run this app you’re going to see that the image quality is going to become much better we have an error no template highest quality images ah yeah because we called it highest quality thumbnails now so thumbnails so let’s do this let’s see if this works and we still have an error it says highest quality no filter name house quality thumbnail so let’s see what’s happening let’s go to main.py um oh yeah highest quality image singular is what it’s called highest quality image because the function gets us only one image so that makes sense why would we call that there we go that image is looking way better now right it’s looking way way better now if you want to check this app out and see if it’s working everywhere let’s go ahead and um let’s go ahead and test it out right let’s go and replace where it says clever programmer with Mr Beast okay so let’s go to channels so let’s go ahead and just say active Channel okay and uh let’s pass in active channel here okay so we don’t have to keep going all the way down here to pass this active Channel and here I’m just going to say channels um and let’s give it uh Mr Beast okay so let’s go ahead and make the active Channel Mr Beast and let’s see if that works oh boom there we go and if I make it bigger boom that is nice right right there look at that if I go ahead and change the active channel to MKBHD let’s see if that works let’s hit command enter and let’s wait for it there we go right MKBHD is showing up and working with this python app that we made let’s go ahead and change it to Peter McKinnon let’s give that a try and we could see Peter McKinnon is showing up right if you want to make this even more realistic see if you can add in uh put in Peter McKinnon’s logo here manually pass that to the back end render that out on the HTML and do the same thing for the name and see if you could do that I bet you could actually at this point all right so this app is super Dynamic right when I go when I scroll it it’s a responsive mobile responsive this app is fully functional it’s deployed online already and this is a phenomenal python project to show somebody and especially if you’re a beginner and you you’re made this or at least you understood some of this right as you were doing this this is phenomenal now do I expect you to understand everything here and expect you to be able to come up with this yourself hell no okay or figure out all this HTML stuff or how it connects to styling no do I expect your brain to be hurting right now absolutely like I don’t expect that you should be able to get this okay because you’re new okay right now is not the time where I expect that you should be able to understand all of this your brain is probably hurting this is probably a lot to grasp you might even be thinking man maybe I’m not cut out for this or I should quit or something along those lines I know those feelings because I’ve been there all I want to tell you is programming and the start for a while it feels overwhelming then when those Concepts become clear and you keep building on them it becomes simpler and simpler and a lot less overwhelming because you learn how to see things for what they are and just iteratively do one thing at a time okay it’s kind of like learning how to ride a bike there’s so many steps if you really think about it or driving a car but once you get used to it it’s so easy and it’s all automatic okay so I hope you enjoyed it and had a lot of fun I had a lot of fun making this for you and building this project and uh with that said I love your beautiful face and I’m gonna see you soon in the next project great job look at this it’s so sick ah all right I’ll see you soon peace I just picked a number between 1 and 100. can you guess which one it was I’ll wait I’ll give you three seconds no no really take a guess three two one wrong the number I chose was 73. all right now let’s build this game with python we’re going to use Python to build this guess the number game okay and you’ll be able to have users guess what your number was and then you’ll be able to select if they were right or wrong in this case the one that we’re building is you get to play with an AI the AI picks the number aka the computer picks a number and then you get to crack that code like a detective and we’re gonna build this game with python using flask and there’s going to be JavaScript and HTML and styling and all that stuff but the good part is the way that I’ve built it for you you’re not gonna need to worry about a lot of the HTML and the CSS part you’re just going to focus on building the game now what’s covered in this is you’re gonna need to have a database to be able to do this so I’m going to show you how to set that up even if you’re a complete beginner you’re going to need to be able to input and the user can type in what your actual guess is and then once you hit check me it will actually actually tell you if that guess is Right meaning it’s correct right the green one will come up or if it’s too low or if it’s too high right so then yellow and red those things will pop up and let the users know and then you’ll be able to restart the app as well and then you know get back to it so there’s a bunch of functionality that this app will have and let’s just go ahead and demo this right now so here we go I’m going to pick a number computer has already guessed the number okay and it says good luck cracking it so I’m going to go ahead and pick 50 and I’ll hit check me and it says it’s too low I’m going to go ahead and pick 75 it says it’s too high let’s go ahead and pick 65 because we want to pick a number lower okay it’s a 65 is too high as well okay let’s go ahead and go 58 in between 60 and 50. okay let’s go 54 and between 50 and 50. oh wait actually we gotta go higher so it’s in between 65 and 58 which probably means it’s around 63. it says that’s too high so it’s in between these two now I imagine so I’ll go pick 60. too low so 60 it’s either 61 or 62. there we go and it says that 61 is correct right so we have solved the problem we’ve cracked the code and if we wanted to play again we just hit restart and we get going again all right so this is the demo of the app very simple game but what I would say is that there’s a lot of Concepts that we cover here in flask you’re going to learn routing you’re going to learn how to deal with databases all that stuff right so with that said let’s go ahead and get started now if you’re watching this as a clip on YouTube the link will be in the description below for the project go ahead and Fork that uh click that Ripple it and then you’re going to click Fork that’ll give you access to this entire project and everything in between if you are watching this inside of this epic python course great if you’re watching this here well you’ll have access to all of this inside of that mega mega mega Python course and obviously if you want to take a part of that mega Python course and you’re not a part of it already just scroll down below and one of the links will be there to enroll in it and it’s all all of these resources everything is free okay so you’re going to scroll down and you’re gonna find the guess the number project you’re going to keep scrolling in the python Mega course and just scroll down to guess the number project okay this might be higher or lower for you just depends on the time that you’re watching this and if we decide to make any changes and this is the project and here are all of the links for it that are there for you so you got project live demo and solution so click live demo if you want to see the live demo click solution if you want to see the actual solution so I don’t recommend you click this right now click this probably at the you know and not something you want to be working on looking at right now the project is the one where you’re going to do this as an exercise and try to solve this yourself okay so those are the things that are available to you so I’m going to go ahead and I’m going to click this project link so we can get started working on this okay and then I have my solution open up here just so I can always see what it looks like so you can have your live demo open up the whole time so you can kind of always have a point of reference to see which which you’re going to be referencing right and uh let’s go ahead go ahead here and if you hit command enter right now it should just start this app for you right here so if I open this up this is not the solution file this is your exercise file okay and if you’re clicking this it would ask you to do a fork so I’m gonna do that fork with you okay I’m just gonna call this instructor okay let’s go ahead and Fork so if it brought you to a page asking you to Fork just go ahead and Fork the project that way you can code in it right and you won’t have to be blocked by anything and then let’s go ahead and hit command enter to run this server and run this app and again what’s nice about replit is you already have these static files and templates available to you which essentially means that when this app runs you won’t have to spend you know really any time on on getting it set up right like all of the styling and everything is going to be there for you so there we go I just had to restart the server again and now my app is open so I’m going to open this one up okay and now this is the version that we’re currently working on okay so like you’ll see if I write in stuff there’s no functionality like if we type in numbers here there’s no functionality if I hit check me it says it’s broken um nothing happens if I hit restart it tells me it’s not found it’s broken so all that means is we need to just work on it and and make it functional okay so when I’m thinking about making this functional here are the first things that I’m thinking about right the first things that are actually really important to me is one I want every single button to be connected okay and that’s really easy to do so I just I in my mind already I’m thinking okay I need to create a uh route for this button and we’ll just create the route of it to be at home page so that should be fine and then I’m also thinking I need to create a route for this restart button or reset button so I I’m just going to go ahead and Route it to reset okay how do I know these things well if I actually click check me um or let’s put in some information and hit check me you’ll see the URL didn’t change so that’s on the home URL so I already know that piece of information and then when I click restart it takes me to slash reset all right so if I look really zoom in there you’ll see it says reset right so now if I go back that’s how I know where each button is being routed to so in my mind the routing problem is already solved what I’d urge you to do is see if you can create the routes and then just like print that the button check me was clicked for this one and uh for this one also print that the restart button was clicked okay so see if you can create those routes in the function so then when I hit check me it doesn’t actually um it it basically shows me you know hey print checkout I don’t care where you show it whether it’s on the screen or in the console it doesn’t matter as long as I see it somewhere when those buttons are clicked so you should have that much information and you should be able to do that part so I’ll give you five seconds to pause and give it a try five four three two one all right let’s go ahead and let’s do it together now so since I know I need to make these routes well in my mind but I’m going to go ahead and do let’s lower this here a little bit so we can see this I’m just going to go ahead get to working on those routes I’m going to say app.rout and first route is already made so I actually don’t even need to do anything okay first shot is made so I’m going to go ahead and make a second route here and the second route is just basically going to go ahead and say reset and I’ll Define reset and we’ll just say print reset or in this case we could even just do return reset page okay so it’s going to take us to a new page reset page and then here I could print you clicked me you clicked check me button okay so if I were to go ahead and try this let’s hit this button check me and let’s see what happens so let’s go ahead and give it a two let’s hit check me okay let’s restart the server so don’t forget that hit command enter to restart and you can actually see it says you clicked me check button and let’s click it again you clicked check me button let’s type in hit check me you clicked check me button right and if you don’t type in anything and you do this it’ll stop you because it’s just doing validation it’s making sure that you’re putting in the right information I’m just going to make the font size a little bit bigger for you guys right there all right so you can see the you you the check means showing up correctly and then let’s go ahead and do the reset page one so if I hit restart now it takes me to this page that says reset page okay so all this means is I made sure that all the buttons have something right like all the buttons actually do something they’re not just broken so all my routes are working okay now the next thing that I need to do wait this probably just needs to refresh refresh let’s try 23 let’s hit check me okay so now the problem that we’re having and the reason why we’re getting this error is because we’re making a post request and a post request is not allowed by default how I know this is if I if we go to our templates and we go to our index.html okay so this is where all of your HTML for this app is coming from so for example you know here’s your nav bar right um the AI has picked a number that’s all coming through this HTML file and then for example if you’re if you’re like hey how is the styling happening that’s all already done for you in styles.css as well okay that’s all happening right here so uh but you don’t need to worry about too much of how this HTML stuff is happening because I want to stay focused on the python parts so what’s happening here right let’s go look at this form this is the only like real interesting thing we need to see so I have this form here okay and this form has a method of Slash okay and then I have another form here for the reset so this form here as by default has a method of get so anytime you don’t specify anything you have a get request that you’re sending but this one over here is doing a post request okay why are we doing a post request because the user wants to send over some data so the user is typing the stuff in and then the user is like clicking one of these buttons and then use users trying to send some data okay so that’s that’s effectively what’s happening and so if we’re getting a post request well then on our back end we actually need to look for that post request okay and retrieve the data from there so what I want to do is um for first before I even do that I want to detect and I want to allow that method so I’m going to say methods and we’re going to say we want we’ll allow get and we will allow post by default get is allowed meaning if you don’t meaning if you don’t put any of this it’s the same as if you had put this okay these two are equivalent statements so by default it’s allowed so we’re going to go ahead and add in a post request so it’s allowed now now if I run this we’ll hit command enter and we’ll go here and refresh okay and then I’ll type in 23 and hit check me you see it brought us back to this exact page and we didn’t run into any errors right it just brought us back to this page and there were no errors it didn’t say method not allowed because now post post requests are indeed allowed if that makes sense okay [Music] great so it seems like we’re good on that side all right so now next up what we want to do is we want to actually grab the value that the user is inputting in here okay so how we’re going to do that is in this way so I can go ahead and say request okay and we can do request.form and I can actually say that let’s go to our index.html and I’ll open this in a new tab okay so in our index.html you’ll see that this input has a name number guess we’ll need access to that specific name so let’s go back to our main.py and I’m going to go ahead and say get me that number guess so that’s it’ll extract the value of whatever was inside and it’ll put that here once you click this button check me okay and let’s just try printing this and see if it does anything so I’ll restart the server and let’s see what happens so right now I actually got an error and the reason why I got this error that says bad request is because I did not run this like it’s it’s asking for a form that doesn’t really exist right now okay so for example if I ran this without it let’s see what happens our app runs okay and if you look at our form or form right now if I hide myself here right if we look at our form our form here is pretty much empty right so if I look at my form you can see empty okay now let’s say I put 23 okay and then we run you can see I have this form here and it says 23 inside of it now okay so if you think of a form it’s really just a dictionary in Python and it has the key number and then that key has a value guess okay so it’s it’s really just a dictionary it doesn’t look like a dictionary it kind of looks weird but it it that’s all it is it’s just a dictionary okay that’s all you need to remember about this so um what I need to do is I only need to get the data when there is a number guess because if I restart the app in the start there is no number guess right there’s no number that has been guessed at that point so we could do it in a few ways we could assign that number to be Zero from the start or the way that I really like to do it is we could do and check if the request method is post if it is a post request then we will extract the information from the form and we will extract this piece of information we’ll say number guess okay and then we will move this you click me button inside of there as well because right now the problem like if we run this the problem with the you click you check me button is it will run on every page refresh so even if I just refresh here it runs if I refresh again it runs if I refresh again you click check me button runs so I don’t want that to be being printed every single time right so I’m going to go ahead cut it and put that inside of this if statement so it only detects it and runs this once it is a post request so let’s go ahead and hit command enter and now you’ll see it doesn’t say you click check me button anywhere and I can keep refreshing this page and again I’ll refresh it and again I’ll refresh it so you can see I refreshed it like multiple times but it didn’t say you click check me button which is exactly what we want now say I put the number 23 here and I hit check me now it says you click check me button and it pulled a value out of the form and gave us 23. very good so now we can actually get access to guess this is the guess that the user is guessing Okay um and we can detect that the click happen so it’s very good I don’t need this print statement anymore and let’s just go ahead and see what the type of guess is so if I run this here’s one interesting thing you’ll see even though we’re putting in a number guess will actually be of type string because whenever we’re taking it user input by default it’s actually type string so what we want to do is we want to actually type use a special built-in function in Python and type cast this to an integer so we want to go ahead here and go ahead and do int um and we can actually put that here right there so now we know that if I print type yes and we run this it will be an integer because it will convert it into an integer so let’s say 23 let’s say check me okay and you can see it says class integer okay so now we have essentially forced it to become an integer okay so that’s that’s good so we’re able to get the user input now what we’re ready to do is we’re actually just ready to write python logic because we’re getting a number and at that point we know what to do right we know what to do so let’s go ahead um and we at that point we just know we can write like regular looking python okay so let’s go ahead and write a python function so I’ll call this function define check number show message and here’s what this function will do okay uh it will it will take in um the guess uh the computer number okay or let’s just say the guess number the one that the human guess and the computer number so meaning this is a compute this is the number that the computer will pick automatically so I’ll take in two different uh pieces of information okay it’ll depend on those two arguments they’ll have like two arguments in other words okay and it will either return instead of returning true or false here’s what it’ll do okay so imagine I called let’s say I call this function check show number and I gave it some the value of let’s say for the guest number I said the human guest five and the computer number guest uh 50. okay so if that was the case would this function check number should do is it should return the following message it should say you know so let me ask you this right is this number lower than the guess of the computer yes it is right so since it’s lower because 5 is less than 50 right so this is slower it should print a message that says 5 is too low okay here is and it should be a string okay I’ll put it in double quotes just to emphasize that it’s a string okay uh if you guess 75 here but the computer number was 50 well then it should say what it should say 75 is too high right and what if you guessed 50. well then in that case it should say 70 uh it should say 50 is correct so that’s what this function needs to be able to do either says the guest number is too low or the guess number is too high or the guess number is correct that’s the job of this entire function okay so see right now with this information okay one last piece of information you’ll need is how to actually generate a random number from a computer okay so that’s going to be something important but for now you could just hard code a computer number and you should be fine okay so before we even we’ll make a note Generate random number we’ll do that later but for now even if we don’t do that you should be able to write this function and if I give that function you know 550 it should give me five is too low if I give it 75.50 it should say 75 is too high if I give it 50 50 it says 50 is correct okay so take five seconds to pause the video and give that a try yourself five four three two one all right welcome back let’s try to do this together now okay so I know that it takes in two parameters so I’m gonna two arguments I’m gonna go ahead and say computer uh or a second guest guess um guest number and then we’ll say computer number so those are my arguments that I’ll take okay and then the definition of function is going to be pretty simple it’s going to say hey if guess number is greater or this case scenario let’s do this first case scenario lower right if it’s lower than computer number well then what I want to do is I want to return right and I want to go ahead and say return uh guest number and let’s put that F outside the string guest number is too low okay and then what I want to do is I’ll paste that and I’ll say if the guest number is higher than the computer number then I want to say guess is too high and then l so we can we can make this an else if so let’s do an L if okay and then else we could say it’s correct because that’s the only other choice left so we’re going to go ahead and say guest number is correct okay just like that so those are the functions that we you know this is essentially what check number show message should do right it should check the number see if it’s correct or not and then it should show and return this message you know one of these messages right here now let’s try this function in our command line so I’m going to pop that up right here and go like this like that and let’s go ahead and do it so from main import check number let’s see if it allows us to do it check number show message okay wow that was interesting it didn’t allow us to do it so I’ll restart the server and let’s try it again let’s try it one more time restart the server python from Main import interesting does that want me to kill this server I don’t think so right now that’s fine it’s just saying it’s in use so let’s do it the regular way let’s just go ahead and go and try print and we’ll do check number show message here and we will say check number show message and I will give it the guests of five and the computer guess will be 50. okay so this will be like the first example that we have here all right let’s go ahead and run this and let’s see what happens and you can see that the function ran and now it says 5 is too low okay let’s go ahead and give it 75 and the computers is 50. let’s go ahead and hit command enter so we run our app again so it runs that it says 75 is too high and then let’s go ahead and try 50 50 should be correct and you can see it says 50 is correct so now we know that our function is working perfectly that is exactly what we’re looking for right and now all we want to do is make sure that when we call this check number we already have the information of what the user typed in so let’s call this function right over here now okay so I’m going to go ahead and say check number show message okay check number or it’s really um check number message because it doesn’t show we have to show but it’s check number message so that’s fine so we’re going to call this function check number show message and we’ll give it our guess and we also need to give it our computer guess so let’s just comment this out for a second right now if in if we’re in Python and I’m going to go ahead and just type in Python right here how do you create a random number well you got to do you gotta get uh random choice and what I like to do is let’s say I have an array of five items if I call this function choice on that let’s call this function let’s put parentheses at the start let’s put parentheses on the end make sure you’re in Shell if you want to run python here and now every single time I call Choice right you could see that it’s picking a random number and it’s doing that by itself it’s picking a random number between one and five so that’s how it works but now I need to give it a big list of a hundred because I need to pick up between 1 and 100 all right so I need to give it a list that starts from one and goes all the way to 100 like I need to give it a list like that so I’m not going to manually create a massive list I don’t also want to write a for Loop so in Python there’s something really nice it’s called range and what range really does is if you give range let’s say we give range 2 and we call the function list on it it’ll create a list with two numbers if I give range 5 it’ll create a list with five numbers if I give lists uh if I give range 10 I’ll create a list with 10 numbers okay and here’s another nice thing about range you could give it a start and a stop so in other words I could say start at one because I don’t want to start at zero because we don’t want to pick a number zero we want to pick between 1 and 100 so we’re not not going to pick a zero for this game okay so we’re gonna go ahead here and it ends here but usually in coding it’s up to but not including so it’s inclusive of the first number but exclusive with the second I don’t know why they do that they shouldn’t do that it’s stupid um but whatever that is the case so this basically means you get a list from 1 to 100 okay so there we go you have this list from one to one hundred so how we could do this is if we want to create a random number okay or we want to make something like computer number we could just go ahead and called uh called choice on Range one comma 101 right and now if we print out computer number and hit enter right it will it will do that so if I just keep running this right here choice between that every time I call this it will get me a random number right because that’s effectively what’s happening under under the hood okay so this piece of code will get me a random number so I’m going to copy this go back to our code and we’re gonna go and create computer numbers here so I’ll say computer number and we’ll set choice and from random import Choice okay because we want uh the choice to be coming from uh we want to import it okay so we can have access to it all right so now that I have access to the computer number and I have access to the user number right so this is a computer number here’s the user number now I need to call my check number show number message function and pop both of these in there okay so we’re going to go ahead and say guess and then we’re going to put in the computer number right here okay so we’re going to call check show number with the guess and then the computer number and um since it returns information we want to do something with that right so this is going to be our message and let’s go ahead and try to print that message out so I’ll say print message so we should be able to see stuff happening inside uh here okay so let’s go ahead and put in 50. so like right now the computer has picked the number and to show that to you is like we can we can write a statement here a computer picked and uh we’ll just put computer number in here okay and the future we’ll remove this because we don’t want to be able to see the computer’s number okay so let’s go ahead and pick 50 now or let’s go ahead and run the server hit command enter otherwise your changes won’t show up and if I go to the console here we look uh okay so it says computer has picked and let’s stop our function from running from here so I’ll I’ll do this let’s hit command enter one more time okay so you could see it says computer pick 97. now obviously if I pick 97 message will say it’s correct so let’s pick 50 First and let’s hit check me right it says 50 is too low great let’s pick um 75 okay it says 75 is too low let’s pick uh 90. let’s say check me it says 90 is too low as well let’s go ahead 95 it says 95 is too low and let’s go ahead and pick 97 and now it says 97 is correct so there you go okay so that was one easy way for us to get the computer number and then um our routes and everybody kind of have has access to that computer number everywhere else okay so uh that’s that’s one way we could do it and then run this function Okay computer Choice and then boom we just keep guessing now what we want to do is we want to make sure that this guess that we did is showing up so we want to be able to see a history of guesses as well so for example if I type in 50 here right I see a history of guesses that’ll show oh actually this is the r version so let’s go ahead to the solution version and in here if I go 50 and we hit check me you’ll see it says 50 is too high let’s go ahead and do 75 it says 75 is too high let’s go ahead 25 25 is too low right so this history of guesses we want to go ahead and make that now okay so how do we do that well think about it like this every time the user types in something we essentially have access to it right so we need to just append whatever the user adds to that list so imagine we had an empty list like this of uh let’s call it guesses if we had an empty list of guesses well what hap what would happen in that case what would happen is every single time the user makes a guess well we just pop in that guess right here right so couldn’t we just do that so let’s go ahead and do just that so I’m going to go ahead and say guess it’s not append and we’ll just pop in that guess and every single time we pop that in we just print out the guesses list okay so let’s go ahead and take a look at this now so I’m gonna run this let’s hit command enter now we’re getting to the really interesting parts of the app so let’s say check me now you can see that there’s a list with 23 in it let’s go ahead and put 24. now you can see a list that has 23 and 24. let’s go ahead and do 100 check me and now you can see it says 100 right um so we could see that now what we want to do is we want to pass it not just the guess but we want to pass at the actual message so we want to pass it um you know we want to pass it not just 23 but we wanted to pass we wanted to have the information if it’s too high as well okay so how could we instead of appending the guess number we could just append the guest message in that case so let’s go ahead and move it here and we’ll say guess dot a pen message okay instead of just appending guesses so now if we run this okay let’s go ahead and do 23 let’s go ahead and do 25 let’s go ahead and do oh we need to show this too so we need to do print guesses because we’re not showing this let’s rerun our server by hitting command enter or stopping and starting our server right there let’s go ahead and do 23. let’s go ahead and do 50. let’s go ahead do 75 and now you can see this list that is becoming longer and longer and it says 23 is too high 50 is too high 75 is too high now let’s go ahead and do um 15. let’s go ahead and do 10 and it makes sense right because what did the computer guess um I don’t see what the computer guessed computer picked four so let’s go ahead and pick four and now you can see it says correct and you could see in our history we also have correct okay so it seems like we’re able to actually get the number of guesses now what we want to do is we want to pass the guesses that we’re getting and we want to pass this information back to our we want to pass this information back to our front end okay back to our front end meaning like we want to actually show this information show up here so there’s just going to be a little bit of kind of like front end part we’re going to do but it’s still not HTML related it’s still python related okay so what we want to do here is in render a template we need to give it the guesses okay so I’m going to say guesses is equal to guesses okay so now our front end our index.html will have access to this variable called guesses okay so let’s go ahead here um and look and if you keep scrolling down you’ll see this div here and it’ll have this comment inside of it that says insert guesses here so that’s already telling us where to do this exactly okay so I’m gonna now basically just say let’s say I do double squiggly and I just say guesses okay let’s restart our server let’s hit command enter and let’s go ahead and do 23 and you could see that that 23 is too low is actually showing up on our screen right let’s go ahead um let’s open it up here as well so we could see it more clearly see uh we’ll say 26 check me a hundred check me so it’s actually already showing up and and looking pretty good actually you know like not looking the best but it’s showing up because we’re passing a list back so it’s literally just spitting out that list right now so what we want to do is we want to Loop through that list so what I could say is I could say hey turn this into a for Loop okay so put only once quickly with percent signs opening and closing it and we’re going to say four guests in guesses I just want you to give me the guess okay and then we’ll close this out with N4 so it ends the for Loop let’s hit enter and run and check this so if I hit um now let’s go to our app 100 is too high 50 check 50 is too low 30 check so now you can see it’s actually showing up as you know as one big giant string so let’s see if we could do something further if we want to make this even better well we could can’t we just put it in P tags would be my question is that possible could we do this so let’s give that a try and let’s see if putting it in P tags will get it to do [Music] um we’ll get it to do what we want it to do all right so after adding the P tags if I refresh this app here you could see that now it just breaks it up into new lines okay so you could wrap this inside of P tags just like this and now every single time you type it in it should break it up into different lines now watch this this div if we actually give it a class because we’ve created The Styling for you so if you actually go to static um the Styles and I scroll all the way down to red green and yellow we’ve created The Styling for you where if you actually give it the class of guess and then you give it a class of red green or yellow it will automatically highlight it the right way so I’ll just show you what that means let’s go to our HTML and let’s just go and say guess red like this so if I give it a guess red like this okay watch what’s going to happen here uh let’s go ahead and say let’s restart the server so we hit command enter right now and you could see if I give a 35 you could see it’s in Red so let’s run this again okay 50 okay 100 all right now with our styling we don’t need these P tags even actually we can get rid of these uh P tags so let’s get rid of them okay and what we’re going to do is we’re going to wrap just our guess inside of the div okay so let’s go ahead and do that now let’s just add this inside of the div and then we’ll do the loop outside of this and then we’ll have a div here uh class guesses container so hopefully by the time you’re watching this this should already be kind of there for you and you shouldn’t have to do it yourself but right now we we do have to do it ourselves that’s fine let’s just go ahead here and paste this okay and then boom oops let’s go ahead like that okay just like that cool so I have a div here that guesses container and then inside of that I’m doing my Loop okay and then every single time I loop I create a new div with the guess in between so this is really what it’s looking like all right um and then yeah that’s that should be good now let’s go ahead and let’s go ahead and hit enter and then we’re gonna go and style this so let’s go to our app now let’s refresh and let’s take a look and see if it got any better and boom it got way better right let’s hit 50 50 is too high 80 and even when you hit enter it automatically Works 100 100 is too high so now let’s go back no matter what number you give it it’s going to sit too high for example even if you give it zero or you give it like negative 55 it’s always going to say too high so that is also not the experience that we want we need it to have different things um I’m sorry it’s not going to say too high that’s not what I meant to say it’s going to color it red is what I meant to say Red’s going to keep coloring everything red because we have hard-coded red here so instead of hard coding right here we want to add some logic here so that’s a really cool part and what I want to say is if the guess is too high then I want to go ahead and get a red if the guess is low then I want to go ahead and get something else so we can go ahead and write um a conditional here okay inside of this string and I can actually say if hi the word hi is in guess uh let’s put a double quotes around this double quotes like this we can say if high in gas if that is true then I want to say red okay so let’s see if this works so basically what it will do is if it’s ever true it’ll evaluate it to true and it’ll just say red it’ll like replace this whole thing and it’ll just say red like that um and then we need to obviously end our if so let’s go ahead and say end if and let’s close it okay let’s run this and uh let’s hit command enter to run this okay I’ll say 23. so since it’s too low you can see it’s not styled but let’s pick 100 and it’s too high and now you can see that it’s actually red so this is working correctly it is correctly detecting if the word hi is in your guess based on that I’ll conditionally render it just make sure that you have a lowercase high in your function uh this function over here so you have a lowercase High and a lowercase low or lowercase okay otherwise it won’t really work um so if is if that’s in guests then you’re good red now we’re just going to add these conditionals multiple times so there’s a first if then we’re gonna go ahead and write another else if so this is the second one is going to be an else if low in guess then it’s going to render out yellow okay and then last but not least we’re gonna go ahead and say green if none of those conditions are true and so I can actually just write green right here okay so if I said guess yellow or gas green or guess red it’ll style it accordingly so now let’s restart our server uh we’re getting an error because we’re not ending our ifs correctly so let’s take uh let’s take this here and we can put that outside just like this and then let’s wrap one more else if so we can actually say else if we can say correct and gas then give a screen otherwise and if so there we go now let’s go ahead and hit wait what’s the error happening now so if highs and guess um I don’t need to end if here because I’m ending prematurely so let’s stop that from happening okay so if high is in guess then give me a red if low is in gas then yellow else if correct is in guessed in green otherwise just end if so let’s hit command enter restart the server Let’s Go 23 let’s go 100 so now this is yellow this is red perfect let’s find the correct number 75 is too high let’s go 60 is too high let’s go 55 it’s too high let’s go 40. it’s too low let’s go 50. it’s too low let’s go 53. scroll down and it says 53 is correct okay but now there’s another annoying thing so that’s really good styling is working perfectly but there’s another annoying thing that’s kind of annoying me right now I have to keep scrolling down from my most recent guess and that’s a that’s a bad experience right because I think what should be actually happening is I should have did not do that like I should just automatically pop up so like if I go to the solution version and I type in a guess like 55 or let’s say 100 let’s go 100 and I run this you could see that that pops up at the top so that’s what I want to be happening I want it I want all of my guesses to pop up to the top well how we can do that is keep in mind that this is a list here this over here is a list so let’s say I take this list and let’s just open up python for a second and I’ll go um actually let’s just zoom in like a hell a lot so you could just see this like super clearly because I know it’s super annoying to like constantly have to look at this in like such a tiny screen I’ll just move this here for now okay so I’ll paste that list and we’ll just say messages or guesses and I’ll pay sellers right so I have guesses here now this is the order that it’s in but I want to see this one first all I got to do is I gotta just reverse the order of this list if I do reversed guesses that alone reverses the guesses now you can’t see it because it puts it in this reverse list iterator object so you can still Loop through it but you can’t see it okay if you want to see it you can just wrap it in a list so it’ll convert it back to a regular list object okay so make sure when you do this listing make sure to put right close all of the parentheses and you could see that it has reversed its order which is amazing that’s exactly what we’re looking for so if you want to do that well then all you got to do is if you wanna so you could you could do this right like if if I did it like this now this will give you this list object but if you’re looping through this it’s not a problem so for example let’s say four guess in reversed guesses and I go ahead and I say print guess you could see it Loops through it no problem and then it returns you this result so let’s go back to our app and now what I want to do here is I want to return our guesses but I don’t want to just return regular guesses what do I want to do to them what order do I want them in that’s right I want them as reversed guesses okay I want to reverse them so let’s go ahead and do reversed guesses that’s what we’re going to be sending back to our front end okay so once our front end gets these as reversed it automatically is just going to Loop through and it’ll be good okay so we’re just sending it reverse from here so let’s go ahead and restart and give this a try so I’m going to say 50. so oh geez 50 is correct wow guessed it right on the first try huh 75 is too high let’s go 90 that’s so rare there was a one out of a hundred chance for me to get that that is so rare I got that on camera that is insane um yes such a low chance man jeez so 0.0 one percent right one divided by 100 yeah 0.01 [Music] um I mean one percent chance why am I being such an idiot the one percent chance it’s a one out of 100. anyways 95 is too high all of my most recent numbers are showing up at the top okay 34 is too low so that’s exactly what I was looking for right I wanted them to show up at the top so this reverse trick does exactly that now the last thing I want to do is reset so when I hit restart well it shouldn’t actually bring me to a different page it should still bring me to the same exact page so let’s attempt that first okay so if I hit restart let’s not give it guesses right now let’s just do it like this and let’s say I go here restart my server and I type in a few guesses like check me and then I hit restart boom right it just restarts and then we get going again okay 50 check me right however the problem that’s happening is like for some reason all of my guesses are coming back so let’s say like I guessed about multiple stuff right so I guess 30 50 35 but now when I hit restart it goes away invisible for a second but then when I hit this again all of my guesses come back that’s not what I want that’s that’s pretty weird I want it to truly restart and I wanted to truly like guess a new number for the computer so one thing I know for a fact is I need to re-guess the computer number so we’re gonna go here and we’re going to say computer number um is now like re like changed basically okay so now the computer number is picked again once the reset happens the other thing that I want to do is I need to set guesses to empty again okay so guesses will be an empty list again and then I call this function okay and if you ever wonder why do we never pass computer number to our front end is because we never want to show it it’s only for our back end to know not our front end okay so our python app needs to know about it but our HTML JavaScript CSS like all the stuff you’re seeing visually here doesn’t because we don’t want to reveal what the computer Choice was so this will reset it effectively and then we’ll just send that list back and now we don’t even need to really reverse this because it’s an empty list right so why does it matter if you reverse this or not and now let’s hit restart our server and let’s give this a try so I will say 35 hit check me it shows up I hit 90. hopefully it’ll be red it’ll show up and now let’s hit restart and now everything is gone let’s hit 50. let’s see if it’s computer picked a new number okay let’s hit 50 again check me ah interesting it brings it all back it brings all of it back so let’s go let’s see if it’s a problem that we’re only having on replit like AKA iframe problem let’s take a look at our version of the app or deployed version of the app here our instructor flask guess the number right and what’s super cool is this app is deployed so you could actually just link it to anybody and even if they are Incognito or wherever they could just open and start playing with your python app so that is a really really cool thing that I do want to give a huge shout out to for ruplet because that is that is awesome like not you not even worrying about deployment and things already being deployed that is crazy so let’s go ahead and hit 50 here and also check me and I’ll hit 90 and we’ll say check me and then I’m gonna hit restart okay and now everything is empty let’s go ahead and hit 50 again okay and it brings it all back so now this is a problem that we gotta solve because that’s really annoying why is that happening so the way that we can actually overcome this weird problem we’re having the way to overcome this is you know really what’s Happening Here is I have these local variables like computer number and guesses and computer number of guesses and these variables are getting confused and lost in here because the same stuff that this has access to this route Index this stuff reset route doesn’t have access to that and so then that information is getting Lost in Translation okay between these two routes so what I want to do is I kind of want to create like a global variable that everybody has access to AKA like a database variable that everybody has access to so let’s do it that way okay so what we’re going to say is we’re going to say from replit import DB okay and then I’m just going to initialize certain keys so for example one key I’m going to need is guesses and then the second key I’m going to need is computer number so I’m going to store that inside of our database so our app knows about it the entire time okay if you’re thinking about a database it’s really just this like I’m gonna have guesses and it’s going to be stored as your like stored as a list okay there’s gonna be a list of moves so or list of guesses so seven is too high for example okay and then you know let’s say five is correct okay um so let’s go ahead here this is just to show you what this will look like and then we’ll have another key and that key will be computer number and that’ll just be whatever the computer I guess is right so this will just be like 57 whatever the computer has guessed here okay so that’s what the DB will look like it’ll just be a dictionary with like some data in it okay that’s all I want you to understand about it but this is a database we’re getting from replit and it works out of the box really easy to use okay so we’re gonna have two keys in that database one is going to be guesses one is going to be computer numbers we’re going to initialize it and I’m going to say because if I try to append to guess this but guess this doesn’t exist as a key inside of this dictionary like if I try to pen to guesses but let’s say guesses doesn’t exist and I do DB bracket guesses dot append it’s going to give an error it’s going to say hey that key does not exist so what we need to do is um I’ll just initialize all of this I’ll save guesses not in DB so it’ll check if this key exists in the database if it doesn’t then I want you to initialize that key so we’re going to say guesses equals this okay and then we’re gonna do do the same thing for computer number if that doesn’t exist then we’re just going to say computer uh DB computer number is equal to uh this random choice so yeah so it’s like whenever the game starts or it’s initialized it should just be a random choice that the computer picks and that’s what should be inside of the database and problem is solved okay so I’ll remove this I’ll remove this DB stuff here because that’s what it is and then I’ll remove our guesses and I’ll remove our computer number this statement over here because we don’t need access to any of these anymore okay what I do need access to is computer this database variable here okay so now when we call this function message I need to give it the computer number from the database okay and now when we need to append I I’m not I can’t just append to guesses because that doesn’t exist anymore I need to append to DB guesses okay so there we go and now when we need to clear stuff out I just go directly to the database okay and I say hey clear out the number of guesses and also I go directly to the database and say hey reset and pick another computer number okay and then in my backend for databases right here I give not guesses but DB guesses and same thing here I give DB guesses for my guesses okay so that is really it that’s really it so let’s go ahead and hit command enter and try to run this now so I will open it up here uh let’s go ahead to this route and hit enter okay and I’ll put in 50 check me uh okay let’s see what error we’re getting and now the error we’re getting is it says print guesses we don’t have guesses anymore so let’s remove those okay we do not have guesses anymore uh you could put uh instead of that you could do DB guesses for print and you could do DB computer number for message okay let’s go ahead and run this let’s go back to our app hit continue all right there we go let’s go ahead hit restart it’s gone let’s now type in 75 and you can see none of the old data came back right 50 run this 25. 30 is too low 45 it’s too low 60. is too high okay 55. 50 53. come on yeah let’s go baby all right so we got 52 is correct so it is working correctly it’s pulling from the database and what’s great that it’s pulling from the database means I could even restart the app so let’s say I like restarted the app everything is still Gucci all my data is still there and I could kill the server and start it again so I could stop and start and when this app would refresh it would refresh with all of that data so like if somebody has a game session that they’re playing uh that will stay there even after you hit refresh or whatever they won’t just lose it simply because they refresh their page or something so or you restarted the server so that’s a really cool part about having a database because it’s pulling all the data from a store database okay and with that said I mean unless I am mistaken your app is pretty much your app is honestly done this has every feature every functionality you could restart you could uh guess the number and check if it’s correct or not right and then this returns our function check number show message will return one of these statements we get that as a message we’re reversing it so it’s coming in the right order we have all the styling in place for it so yeah with that said there you go that is fully your app you can share with your friends your family could show off hey look what I made with python you know and especially if you’re a newbie or getting started I mean I’m really proud of you for being able to get to this part and get it done even if you just copy pasted everything that I did or even if you looked at the solution file or even if you’re using the solution file okay the main thing is you’re trying and you’re here and the start all of this stuff is really hard I know but I promise it gets easier over time okay so I don’t expect that you would have figured this out yourself or done it yourself but I do expect is like you at least gave it a try and that’s all that matters okay with that said I love your beautiful face great job on making guess the number and and I’ll see you in the next video Rock Paper Scissors Shoot maybe I got you who knows welcome in this video we’re gonna be building rock paper scissors with python and using a web development framework called flask now I’ve abstracted away essentially all the stuff that you need to know about web development so you really don’t need to know any web development at all the way that I’ve designed this app for you is you’re pretty much only going to need to worry about writing python code okay uh if you’re watching this on YouTube and you want to follow along and build this app out with us the link isn’t going to be in the description below if you’re watching this as part of this big course that we have done um I’ll show you exactly where you can go ahead and get the link but first things first I’m going to go ahead and demo this app so you can see exactly what it is that you’re going to be building so let’s go over to it and I will share my screen with you now all right so this is the app that you’re building now I’m gonna choose which one I want and I chose Rock and you saw that it said human picked Rock and the computer picked paper and paper beats Rock so I’m now at negative one and it says you lose okay let’s go ahead and pick scissors this time damn damn I lost again beating you what the cheese okay there we go now to draw I’ll pick scissors again it really likes rock huh let’s wow right when I pick paper it picks scissors this computer is too smart okay so I’m just getting destroyed here okay what’s crazy is that this is totally random so I should be probably at some point winning but you get the point now I can also hit end and it’ll end the game and watch this it’s actually really cool when I actually refresh right the data doesn’t go away the data is still there so how the heck am I doing that with python well guess what this app also has a database so you’re going to be mastering databases you’re going to be learning about routing how that works you’re going to be learning arguments you’re gonna be learning about so much and like so much python stuff as well so just hang in there it’s going to be a fun project all right and then if I hit this end it completely clears up and starts all over again boom boom boom okay all with pure python all right with that said if you’re excited I’m gonna walk you through it so if you’re going through the course the clever programmer python course okay and if you’re watching this and you’re like I have no idea what the heck you’re talking about I’m doing an amazing python course which is you know 8 10 12 hours long it’s a pretty long course and um if you want to become a part of this course the link is in the description below you could just join it completely free no charge for anything um and uh yeah it’ll give you access to all of this material the entire course all the exercise of projects we do and you’ll have access to this okay so here we go so we’re gonna find this project this rock paper scissors project right over here and then inside of this project uh you’re gonna see that that project has links so we have a project link I’m going to click that and open it I have a demo link and then I have a solution link but I will not open the solution link okay I’ll only open these two uh the solution link we’re gonna leave it not click it because that’s the solution I don’t want you to see the solution right now okay I want you to work on the app so click the live demo and let’s go ahead and check it out so here’s the live demo of the app right we’ve already checked that out so that’s all good we can close it now actually I like to leave the live demo running so let’s just leave that open for now and then the next thing I want to see is the actual project so let’s go here and now what I’m going to do is I’m going to walk you through an explanation of this entire project because this project is going to be different than the ones we’ve done before this or the ones we’ll even do after this you are going to be doing a lot of this because I think you are ready but it’s designed for you in a way where you can do everything step by step however for all of that to happen I need to explain to you how it works because if you tried this on your own right now it won’t really make sense and you’ll get lost or frustrated I don’t want that to happen I want to make sure that you have a great experience and you actually build it let’s talk about first things that you don’t need okay so these over here static template stuff you don’t need to worry about it pretty much at all we’ve hidden these complexities from you completely in this project so you really don’t even have to look at it I was planning to hide these files folders but I think at least you should know that they’re there okay in the static is the styling of your app which you don’t need to worry about okay it makes it look beautiful and color powerful and all of that and then in templates is your template structure so it’s like where does the div go where does a head go Etc okay so this here is all your front end meaning right now if you open this app and you hit run at the top right uh first thing we’re actually going to do is when you open this exercise it’s going to ask you to Fork so make sure to go ahead and hit fork and I’ll just call this uh instructor okay so make sure to go ahead and Fork because otherwise you can’t write code obviously unless you Forks you probably forked it um okay so there we go so static and if I run this app right now okay it should show me this over here and nothing should happen so like let’s say I open this okay and I’m going to zoom in here so you could see a little bit better if I click nothing will happen click click click click meaning none of these buttons are connected to anything so you’ll see routes changing at the top you see it says end you’ll see it says play Choice equals Rock you’ll see it says Choice equals paper you’ll see it says Choice equals scissors so all of this information there’s something happening but these buttons aren’t connected to anything yet meaning there are no functions that we have that are firing off yet because that’s the functionality that you’re going to build okay okay so here now let’s walk through the rest okay so I don’t need to worry about this I the only one file I need to worry about is this main.py that is it okay now let’s zoom into that main.py and turn our attention to that at the top you’ll see some imports that you’re gonna need to do not touch these These are perfectly imported for you um you will need to add any other Imports and you won’t need to remove anything so you can just leave this untouched uh like some of the code here pretty much all the code that’s written for you you can you should leave that untouched because you’re going to need access to that all of this here is I’m just creating database database information for you that’s why the app even when you refresh right it still has the data because we’re using a database here and we’re using the replit database so game start player score choices results those are initialized in the start as like false right because when the game starts it hasn’t started yet it won’t start until you hit play that’s when the game will actually start alright so yep basically here all of this stuff database information that we’re going to need and it’s going to change so choices is going to be you know what choice the human picked and what choice the computer picked and computer will be picking a choice at random player’s score will be well what’s the current score of the player meaning if this player has let’s say two points or the score is two the score is two then that basically means that the net result of the student uh of the player is like two plus two okay uh if you lose player score goes down by one if you win player score goes up by one right so you get either plus one or -1 based on winning or losing and if it’s a draw player gets zero so that’s what’s going to be going in there that’s all going to happen later okay all of this stuff you’re going to touch later now let me go to the functions that you should work on first and then you’re gonna work on other things next okay so I’m gonna close this index one and I’m going to close this play one okay and you’re going to see this function called get computer Choice okay and so you’re going to see functions like these so if I go ahead and close out all of these functions here this game is made up of um one index you don’t really have to do too much there okay but one function play which is going to be your main logic that’s going to go in there it’s made up of computer Choice function two three four okay and then there’s end game so I would say like about five functions that you’ll need to make now let’s start with an example of a function so let’s say we want you to make this computer Choice function okay you don’t need to change what you write in here the arguments are already given to you in this case there are none none that it takes there’s documentation here for you so it tells you hey get computer Choice randomly selects between rock paper and scissors and returns that string for example if you call this function git computer Choice it will return Rock if you call it again it will probably return scissor okay so every single time it returns something different and it’s a string and so now you have to actually based on this information you have to write this function get computer Choice okay and the hint that I’ll give you is you’ll be using choice that you got from random so remember when you did from random import choice so you’ll be using Choice okay and if you want to like play around with python and see if you could get this function working which you could always do is you can just pop open the shell you could type in Python and then here let’s zoom in a couple of more times you could see like super clearly so you could type in Python here and um then you could play with randoms you could say from random import Choice and then you could see how Choice works so choice you could give it an array and then you could see okay every time I give it this it picks a run number randomly okay great well what if I gave it strings like hello [Music] ah that works let’s see if that works yeah eventually at some point it’ll get the string hello okay great what if I gave it uh Rock here okay and I kept running this wow it gives me rock so then you could actually take this and see if you could convert this uh into into code for your app so you could go here and then you could actually say okay I know how to use Choice and then this time instead of giving choices of one two three I’ll give a choice of rock paper scissors okay so get computer choice you should be able to do this and then at the end make sure you return so you should return Choice and then whatever is the array that you’re going to give it okay so I’m gonna put in one two three but you’re probably gonna put in something else right like rock paper scissors or something and then that would be essentially this computer Choice function would be done now if you want to test that your function is done here’s what I want you to do if you want to actually test that you have written your function correctly you can go ahead and do print get computer choice and then pass in actually this won’t take in any uh anything so it’s like get computer underscore Choice like this okay and call the function just like that and it should return either rock or it should return scissor and it should do that as a string so if your function can do that then you know your function works so you could test it out a couple of times by hitting stop and start or command enter to restart your server and then you can actually also look here in the command line and it’ll tell you it’ll print out the result in the command in the console okay so that’s how you would test it now you have another function here called calculate result and this will compare Player move and computer move and return the score accordingly so meaning here’s a situation where a human would win if you call this function calculate result and you give it Rock and scissors it should return one why because player choice is the first positional argument and computer choice is second so if I give the player Rock and I give the computer scissors well who would win in that case the player right because rock beats scissor so then you get one point for that so human wins here’s another condition second condition which is human loses well you flip that scissors and rock and the computer wins aka the human loses okay and then the human draws so you calculate result if you do rock rock you get zero so you understand now right how this function would work it’ll take in this it’ll compare them against each other and it’ll give you a zero negative one or one and it’ll return a number okay it’ll return an integer so your job is to go into this function calculate result and your job is to basically fill out the body of the function now I’ve given you a lot of hints to make your life easy so for example here I tell you create a variable called score and set its value To None well you could go and do this then you’ll need to write another line and maybe even multiple lines here it’s like hey uh what are all the situations where a human draws well all this situations where a human draws are probably whenever the player choice and the computer choice is equal to themselves so basically it’s like if player choice is the same as the computer Choice well then in that case what am I returning here I might be returning something right a 10 a 20. so you’re gonna write this line of code I’m not giving you the right answer here right but you’re going to return either a negative one a zero or a positive one so you have to figure that out but that’s how you would write that first if statement they’ll catch essentially all drawn situations all drawn situations are where it’s the same then you’re going to chain that with elseifs of all of the situation where the humans human wins so it’ll be where you know if a human picked um Rock and the computer picked scissors if the human picked paper and the computer picked Rock if the computer picked so you literally write out all the situations where the human would win and then you would write that in Python code right and then you would write down your else statement at the end which would be you know what happens if the human loses in that case set the thing to negative one and if you do this part correctly you could just simply do an else here you don’t even have to really write any if else if uh type of conditions here okay so that is what is happening with this function so make sure that that’s working correctly in order to test it call it on these terms and see if you get the same results that I’m getting here one negative one zero okay so give that a try so that’ll be calculate result then the next function you’re going to need to build after calculate result is um the next function that you’re going to need to build okay let’s go ahead and do this is going to be the get result function and what this should do is it should take in a string with the value of U when or you lose okay or it’s a draw based on the score for example if I give it a 1 it should say you win if I give it a negative one it should say you lose and if I give it a zero it should say it’s a draw so the logic for get result will be very similar to the logic for uh you know doing one of these previous functions like the calculate result one so that’s going to be pretty similar so you should be you should be good to go there okay it’s going to be very similar type of way you’re going to do it you can have a score and then you know if that score is a one something should happen and if it’s a zero something should happen if it’s a negative one something should happen okay so that’s what’s happening with the get result it takes in a score and then it returns something okay to test it call get result on one you should get back you win call get result on negative one you should get back you lose call get result on zero you should get back it’s a draw and then uh one of the last ones we’re gonna do is end game and in here what you’re going to need to do is what endgame does is resets every value in the database to its initial value okay so you’re gonna go ahead and reset all of the database values that you have here so game start player score choices result you’re gonna set them back to their initial States because well we’re ending the game right end this slash end would run when you hit this button here so that’s what we want to do here okay and then we want to pass that information to our front end so make sure that this all of this information here right now just hard code it to be false and zero and empty strings but you’re going to need to pass in your own um your current state of the game from the database so for example it’ll look more like DB game start for example and you’re gonna basically do that for every single one player score will come from database choices will come from database result will come from database so it’ll just put the database variables there that you’re passing to the front end and then that should be good because then your front end needs that information and based on that information it goes and does things and keep the variable names the same here because if you change player score variable name or Choice as a result your front end will break because your front end is depending and it’s attempting to look for those specific terms okay so for example it’s looking for game start player underscores player underscore score uh choices result so it’s looking for that specific piece of information okay so that’s end um end is fairly straightforward and then we have the function play okay so this is going to be the entire logic of the game this will be the most difficult one to write for you but what this will do is it will change the game so in the start you’ll have these notes okay so read all of these notes and try to do them yourself I won’t explain every single line of these notes but basically you know it’ll ask you to do things and then you just gotta try to do those things okay so for example this request.orgs thing might throw you off but how what we do here is if I’m playing this game if I want to retrieve that the player picked scissors or let’s say the player picks Rock and I want to retrieve that from the URL I can in Python I could just do requests Dot orgs and I can say rock or sorry I could say choice so basically what I’m doing is I’m putting in that keyword argument right there of choice and it’s going to actually return and spit that value back out to me so tell me that the player pick Rock for example okay so that’s how you could do this and you could get back the player choice by doing request.org so I’m giving you that hint already on how to do it okay then you’re going to generate a choice based on the computer you’ll calculate the score of the players you’ll update the result choices and player score in the database and then you’ll send all of that data here and so all of these are going to be pulled from the actual database so all of this here will be coming in from the database okay so there you go it’s a really big project I want to just make sure that I thoroughly walked you through it and covered every single detail that I could about it I’m gonna try to leave you to it take hours days however long you need to do this and then we’ll get back together and go through it don’t look at the solution until you have worked pretty hard okay this is the exercise I would say don’t look at the solution until you’ve worked pretty hard and give it a really really strong try Okay um yeah and but don’t be discouraged if you don’t get this I don’t expect you to get this right it’s a massive project for you to do right now would be really difficult but it will push you and I want to just push you I’m hoping that you get the calculate result uh get computer Choice I’m hoping that you get those right okay you get you get some of these functions right two or three functions you get right even if you don’t get the flask specific functions right or whatever right I’m happy if you do that so that would be a good place to start give this a try and then once you give it a solid solid try remember there’s always a solution file that you could cross reference if you’re completely stuck you could cross reference the solution file or which is right here behind my face um or just wait till pretty soon you and I are going to go through the solution together and we’re going to build it out step by step okay so good luck enjoy go cry crush this click the link below right hit the replit fork it so you could work on it so do that right now and with that said I’ll see you in a second and good freaking luck all right welcome back now we’re gonna go through the rock paper scissors solution together I hope you gave it a try and you got close I know you might have spent hours you maybe might have even spent days um I just want to have you pat yourself on the back because that is powerful if you didn’t get it that is okay that is totally what I expect you are a newbie in coding you’re not supposed to get this but all I’m trying to do is challenge you and trying to get you to see the beauty of coding and trying to understand it bit by bit but also feel that pain and frustration that comes sometimes now let’s go and do it together step by step and um yeah let’s see if it makes sense okay I just want you to have fun that’s your goal right now so let’s go to our app and let’s go through the solution I’m just gonna try to go through a little bit at a faster pace because I’m sure you’ve already taken a lot of time with it right already so let’s go to um let’s start with the simplest function first get computer choice this is very easy I’m gonna go ahead and say return and I’ll call Choice which I’m importing from random right there okay and then I can simply just go ahead and say choice and give it a rock paper scissors okay um and I know that I need to uppercase it title case it like this because even in these documentations right here it tells me that it should say rock like that scissor like that so that’s already telling me how I need to get this right this will basically get the computer Choice okay that’s how we’re going to get the computer Choice okay now I’m going to work on another function so if I want to test this function I’ll just probably call and do this get computer choice and let’s just run this and see if it I’ll restart my server and let’s see if it works get computer choice you can see it says Rock so that’s a really great sign let’s run it again and you can see it says scissors so literally it set it in the same exact order that it says here right so I know and scissors let’s make it plural my bad okay so I know that it’s it’s you know um I know it’s working okay I know my function computer choices work because I’ll remove that print statement so we’re good with computer Choice that’s uh right that’s a correct correct one now the next one I got is calculate result so I’ll just follow the comments and everything that’s there without even really going crazy so I’ll set the score to none and then all situations where a human draws well this is if human uh if player choice right I have this argument this has two arguments so I know that if player choice is the same as the computer choice well then we’re just going to set the score to zero okay so this is like a drawn situation all situations where a human wins so now we’ll chain all of this with else ifs because it even tells you to chain him with else if so I’ll say else if player choice uh equals equals Rock okay so the player can win with three different ones right so here what are the three wins that a player could have well the player could win with rock the player could win with paper and the player could win with scissors those were the only three times where the player could win so these will take care of all the situations and rock can only beat one opponent and paper could only beat one opponent scissors could only beat one opponent opponent meaning scissors can only beat paper paper can only beat rock rock can only beat scissors so I know that I’m just gonna have one uh line of code is essentially gonna take care of this and another one that’s going to take care of this another so it should be like three else ifs essentially so if player choice is Rock and and um or actually I can say and like this and computer choice is what computer choice would have to be losing choice we’re gonna go ahead and say return or score is equal to one so this isn’t a situational human wins I’m going to copy paste this here okay now it’s the second choice if player choice is paper and computer choice is Rock player wins okay what’s the third situation where the cleric will win if the player chooses scissors and the computer picks paper right so now we officially have all the situations where the player would win so we’re done with this part now it’s a little ding right there okay and now we have else otherwise human loses so I could just say else score is negative one this is the only other case lost if we’ve accounted for all the drawing situations and we have accounted for all the winning situations the only other situation that’s left is um if the human loses right that’s the only situation left and at the end we’re just going to return score so this function now is complete now you should go ahead and test it obviously and write print statements to test it I won’t because I’m very very sure that it’s good okay but you should give it a try and I’m also doing this so we can keep the pace of it uh smoother all right now we gotta work on another function called get result well how does this work so get result returns a string with the value of you win you lose or it’s a draw based on the score okay so if I call get result on one I get you when if I call it on negative one I get you lose otherwise I’ll draw okay that should be simple enough right I could say um if score is equal to one return [Music] you win right elsif score is equal to zero or negative one return you lose and else okay else returned it’s uh and let’s put double quotes here it’s a draw the reason why I’m putting double quotes is if we use single quotes it would be confused because it would think that the string starts here and it ends here but that’s not what’s happening right we have a string starting here and ending here so putting double quotes So then it knows that this is an apostrophe and not a string ending okay so that’s it for my get results so I’m done with my get result okay that’s good now my end game well what I need to do is I need to reset all the variables right that’s what it told me so I’m going to go ahead and do just that so I’m going to say DB game start I’m going to set it to false okay um DB player score I’m going to set it to zero DB choices I’m going to set that to empty string and DB result I’m going to set that to an empty string as well okay and then I need to pass all of those to my front end okay so this is the part that I’ve told you is going to be a little bit tricky so boom right there boom right there we’re only passing only database stuff to the front end okay that goes there cool so now our end game function so whenever we hit end game our end game function will run right and then all of this will happen and then it’ll send that over and once I send it over index.html will render all of that out on the screen okay so now our end game is done and then all we have left at this point is really um really just a few ones so this one needs all the database information so this one is very very easy just like the last one let’s just finish this there isn’t even really any code you need to write here like any extra code we’re just sending the database variables here to index which is our home page so let’s just get index done okay the result and then we’ll set it to this okay and then the last one that I I really got to do is I want to be able to change the state of game um I need to change the state of game start in the database to true so for example once you hit play well now the game has begun so game start will change from false and it’ll now become true so we’re gonna go here and I’ll say I’m just gonna yep perfect that’s great I’m good here I’ll do game start and I’ll set that to True okay so I’m good with that now the next thing I need to do is get the human Choice from URL arguments and save humans in a choice in a variable okay so we’re gonna call it um we’re gonna just say player choice and we’re gonna set it to this because that is getting us the player choice uh from the URL okay so that’s what’s happening there player choice and next up what I want to do here is I want to generate a choice based on behalf of the computer and then save it in a variable so here I’m going to be leveraging the functions that I’ve created earlier so I’m going to say computer choice is equal to get computer choice so now I’m leveraging this function that I created earlier then we’re going to calculate score of the player versus computer choice so now I can do uh get the score from the database so I can say result and I can set it to the database so now I can do get result and then I can give it the score okay and if we want to get the score let’s actually get the score first so let’s let’s calculate the score first so we’re going to go ahead and say score and I’ll say calculate result and we’ll give it a player choice and computer choice because now we have the player choice and we have the computer choice right so this is coming from the URL you know with whatever the user is clicking um and then this computer choice is being uh we’re getting this randomly okay and then we give this for calculate results function so if I go to our calculator result function it knows what to do and then it Returns the score so it either returns a negative one a positive one or whatever right so if I go to our thing I know that this is either going to be a negative one or a zero or a one okay so that’s what the score is doing then what I’m going to do once I get that score is I’m going to increment the result in my database by plus equals um or sorry actually I want to set the result in my database to whatever the result was of the most recent game okay so score and I’ll get result and get result either says you win you lose or you draw or as a draw okay so that’s what writes out for example in the solution version that’s the function that’s gonna do it’s the draw it’s a draw part or it’s a win part or whatever that’s how we’re figuring that out and then score is the function how we’re uh score in the database is how we’re keeping track of this two and three and then it turns to four Etc so let’s go back here to our app and then I’m going to get the uh set the choices key to a string and this is going to be a string that’s going to say it’s going to be a string formatting here and we’re just going to go ahead and say player choice and we can make a human Emoji here man person okay so this will represent a human Choice and then I’ll put this bot here robot and we’ll this will represent the computer Choice okay and then we’ll go player score we’ll increment the player’s score by the score that we just got for the game so if the human lost or if the player lost it’ll decrement it by one if the player if it was a draw it’ll just add zero which won’t do anything and then if the player one then it’ll be a plus one it’ll increment it by plus one now I need to give the database values to our back end so we’ve done this before a few times so I’m just going to take this and put that uh for the result we’re gonna give our front end choices so we’re gonna put that right here we’re gonna give our front and player score and we’re going to give it game start so let’s go ahead copy this right there and we’re going to give it game start okay there we go and now at this point we should be pretty good actually the whole game should essentially be working so let’s go ahead and hit command enter on this and I’m just going to open it up in a new tab and let’s check it out boom look at that right paper versus rock you win let’s go hit this scissors versus rock you lose right that was a one of the situations that we had Rock versus paper you lose huh interesting paper versus Rock so we pick paper computer pick Rock so that means we win and you can see that it’s incrementing that uh result as well so if I keep winning it’ll keep going up to a plus score or a zero score if I keep losing it’ll keep decrementing down to a negative score and now watch what happens when I hit end boom everything is gone even if I refresh it’s gone right not there so we are using a database and we’re getting all of this to work right so there you go uh did not mean to put that there and get it out of here there we go there we go got it okay so there you go that is rock paper and scissors you went ahead and built it yourself now you even have a URL it’s deployed online already so you could share this with your friends family anybody that you want um but overall I just really hope that you had a ton of fun building this you had a great time and don’t be so hard on yourself if some of the stuff you didn’t get or feel like it’s kind of going over your head you know go through the rest of the course if you’re not part of this course make sure you sign up for it below so you can you know be able to go through the entire course um because if you go through the whole course it’ll make a lot more sense and if you just jump to this project here because that’ll be a lot trickier right with that said I hope you enjoyed it this was a great project I had a lot of fun building it with you I hope the explanation and then the solution type of method worked well I spent a lot of time with my team building each you know exercise function in a way that’s just awesome um but yeah I hope that worked for you I hope you enjoyed it with that said I love your beautiful face and uh I’m gonna see you soon let’s keep going time to build another app with python and flask this one is going to be called a random dog generator because Isn’t it nice when you click a button and you get a picture of a nice puppy that’s what this is about and this app will have a lot of functionality for example this will have a database this will have the ability to track the users and the sessions and um yeah it’ll have a leaderboard for who generated the most dogs and we’ll do this in very few simple lines of code and so it’s gonna be a lot of fun building it let’s go ahead and demo this app so I can actually enter my name here so let’s say I go qazi and I go login slash sign up it’ll bring me to this page over here right this is the home page now it’s telling me that my username is qazi it’s uh it’s telling me that this is my first login that’s ever seen and it’s also telling me that these are the total amount of dogs that were generated the entire you know website wide all users combined and then it tells me here who the users of this app are so we have these three users right here Kamikaze David test those are the three users of this app so far okay and then these are the dogs that were generated all right now if I go ahead and I hit refresh okay let’s go ahead to this page and let’s go ahead and hit get random dog there we go you can see that now I just got a brand new dog right here let’s hit another another time get random dog boom I just got another dog and you can see this total number goes up and it’s also tracking dogs that you have generated so it actually can keep track of every single user and all the dogs that they’ve generated so when you log in it’ll keep track of your dogs but when you log out and you log into somebody else then it’ll keep a track of that user’s dogs right so I can keep clicking and you can see that I’m in the leaderboards right here now if I go ahead and I do more than nine I’ll actually pass test okay so let’s keep going let’s keep going I just generated eight dogs let’s go ahead and generate nine dogs and ten and Watch What Happens boom right when I did 10 you can actually see that I pass test and now I’m here all right so that leaderboard works perfectly you can detect my number of logins now let’s say I log out right and then let’s go ahead and I go quasi and I log back in it remembers that I’ve generated 10 dogs let’s say I log out and I log in as a new user so let’s just say I’ll say um John let’s enter and say you have generated zero dogs and number of logins is one now if I hit get random dog right the dog is generated and I can see it on the screen so a lot of concepts are covered here like for example being able to keep track of the session of the user meaning when I refresh it still knows that I’m John so the app is still running a database all of this information is actually stored in a database forever right so it says 45 when I refresh it will still say or if I generate a new dog obviously it’ll go up to 46 so you could see how this app is working right like every single time I click this I get a brand new dog we have this little nice little spinning effect if you hover over it and all of this data over here from the leaderboards this of course is coming from a database as well where we’re keeping track of all of the users and then it’s showing that so you’ll learn a database as well in here okay great now if you’re watching this clip on YouTube you can just click the link in description below it’ll take you to the exercise page of the app for the repository and you’ll be able to use it if you’re part of this python course that we’re going through the mega Python course we’ll link that below as well uh then just simply go down to where so in the python course right the best Python tutorial in the world if you keep scrolling down you’ll see all the projects you’ll see this counter project if you scroll down you’ll see this YouTube app project that we’ve built and you’ll see this random dog generator so you could go down here to random dog generator and there will be Three Links available for you one will be a project link so this is the link where you’ll actually click and build your application uh the second link will be the live demo of the app and the third link will be the solution file so this only look at it if you really really really really really need to and you’re stuck but this is like the solution so don’t look at it right now okay but you could look at the live demo that should just get you to this page and you should be able to hit get random dog or if it doesn’t see that you’re a user in session you could just create a new user and keep clicking get random dog and it’ll keep generating random docs for you right so it has apis as well because we’re using an API to get these dogs all right so I want you to go ahead and click project okay and now that you’ve clicked project it should bring you to this page and if you’re hitting it from your computer it might actually bring you to this page that asks you to fork okay then once it brings you to this page just go ahead and click four okay go ahead and click Fork like this and it’ll ask you to create an account create an account it’s all free all right so uh nothing really to lose there and then just Forks can build off of it and since we’re not building it from scratch we’re giving you a couple of components and things that you’ll need like HTML and whatever you know it’ll be wiser for you to Fork this will make your life a lot easier all right so I’m gonna go ahead and Fork this with you as well so I’m going to go ahead and Fork this and you can call it whatever you want I’ll call my Fork instructor because well I’m your instructor okay so that’s what I’m going to be calling it that way feel free to call it whatever you want right as long as you can find it easily and and do whatever you need to do okay so here we go all right now that you’ve forked everything and renamed everything once I run it you should see this page over here okay this guy right here so this basically means your app is you know running it’s not fully built yet right so for example you have this front end but let’s say I log in hit login and sign up it doesn’t work if I hit get random dog it does not get me a new dog if I hit you know let’s say I hit log out it won’t log me out right and this data over here right now is dummy data meaning it’s not dynamically changing or anything like that it’s just hard coded in here okay so right now in the current state this app is broken okay but all the front end is there why is the front end there is so you don’t have to spend because it’s a python course and we want to keep the concepts focused to Python and flask and we don’t want to focus the concepts on web development necessarily so let’s go ahead to um yeah we want to just keep the concepts Focus to like python web development and flask web development not necessarily HTML and CSS and The Styling part okay so great so this is where we’re at now if we want to connect these buttons right like a get random dog button so it actually gets a random dog well what we need to do if you remember is what and some of these things if you want to give them a try you should actually pause the video and give those things a try because that’s how you will learn if you just keep watching me doing all of this it’s gonna be not like the most helpful way for you to learn okay so I urge you to keep pausing the video at times and giving things a try now some things are new Concepts but like connecting this get random dog Button uh to a path where when you click it right once you click it it takes you to just get dog route is as simple as creating a route inside a flask and so you’ve seen us do that with python multiple times at this point so I want you to give that a try on your own okay I’ll give you five seconds to pause the video five four three two one all right now the URL now I’ll we’ll do it together if you if you didn’t pause it or whatever or if you’ve tried it great so let’s go ahead and do this together so the URL said get dog so I’m just going to go ahead and I’m gonna create get dog and dog function and I’m just gonna say uh let’s go ahead and say uh return dog page okay just just let’s just do that for now let’s go ahead and hit command enter I’ll go hit refresh here okay and then we’ll hit get random dog okay and there you go you can go ahead and see that the dog page is now showing up okay so like that part is connected now what I want to happen is I don’t necessarily want a entirely new page to come up um what I want to happen when I hit this get random dog is this same page to render okay the same page to render just with new data though this time so let’s just walk through some of the other pieces of code that we have in here just so you can fully understand what we have so this is a basic flask app so far okay and we’re rendering index.html okay so index.html is right here and this is already all created for you okay so this is the HTML and the CSS part that you don’t really have to do okay this is all created for you now if I go back here and this components is also created for you as well it’s a card component that we created so you don’t have to create this and then the styling is all done so that’s that’s the stuff that’s all already there okay now let’s go back and inside of here we’re we’re getting the dog but now what we need to do is still return the same page okay so what I’m going to say instead is I’m just going to say render template and I’m just going to pass in this guy right here I’m going to say index.html okay and all we need is information about a dog so let’s just say we had a dog uh image okay let’s let’s just say we had a dog image and this image was let’s just go to Google and let’s get a dog image okay so there we that looks like a really cute dog all right we’re going to go ahead and copy image address and I’m just going to paste that image address in there okay so this is a dog image now to my front end I can send this information okay so I can go ahead and I can say dog image it’s equal to dog image okay now what this means is I am sending this information so it’s available inside of my index.html okay that’s what I’m doing here and this variable here that I’m creating we will have access to it in the index.html okay so in my index.html what I could do is this is where the dog image it’s already telling me that this is where the dog image is supposed to go okay so this is where I’m going to go ahead and put my dog image I’m going to create an image tag and these notes are telling me exactly how to create it and I’m going to say my source in this case is the dog image okay so I’ll put double quotes this and I’ll say dog image just like that and we’ll give it a width of 200 and we’ll give it a height of 200 as well okay and then let’s give that a try let’s go back okay so let’s give all of this a try so I’ll hit command enter and we’ll go here I’ll refresh and right now I don’t see anything dog related right but let’s hit get random dog and let’s see what happens and right when I click get random dog our dog shows up okay and this stock shows up and that dog we already have like ready-made you know ready to go and that dog shows up right away so uh so far so good right like so far so good when I run this get random dog when I when I click it initially right it shows me the dog right away so that means our app is working I can restart the server obviously we can go back to our app and we refresh and we’re seeing the dog because I’m already on that route get dog but let’s say I go back to the home page and you see I see no dog but right when I click get random dog it shows up okay now this styling and where the spinning and everything is coming from we’ve already created a CSS styling for you where when you add an image we make um we make stuff happen to that image okay so we’ll make it so that image will either spin or look a certain way or fit a certain way Etc so all of that is done right dog hover uh that’s what’s doing that hover effect okay but I don’t want you to worry about the what’s inside of that Styles file okay so we’re putting this in this div with the dog okay so now my dog image is showing up let’s go back here but what I need to do is I need to now actually get the dog data okay so let’s go ahead and dog image API so let’s find dog image API there is dog.ceo and what this does is this will get you a random dog every single time you click it okay so if you hit fetch over here zoom in more okay and the URL is there if you need to go to a dog.ceo dog API okay and if you hit fetch it will just get a random dog for you okay and it will return it to you in in Python this would be a dictionary that has two keys message and status and then those keys have these values so the key we care about the most is the key called message and that’s really the URL I don’t know why it calls message it should be called the URL okay and every time I run this it creates a new URL and then it shows that image right and that’s the image that we need okay that’s the image we need access to so I’m going to go ahead and I will copy this that’s what we need okay and we’re gonna make a request so if you remember we’re gonna go ahead and do import requests which is a library and we’re going to go ahead and do request.get we’ll put this dog image in there we’ll say response is equal to that and then I’ll say data is equal to response.json and then let’s go ahead and print um print that data over here so I’m going to go ahead and rerun this and let’s see if we get any errors or what happens and did I get any data okay I didn’t get any data yet but maybe once I click the get random dog I might actually get what I’m looking for so here’s what I’m going to do I’m going to make this like this and I’m going to take our app and I’m gonna put it like one thirds here and I’m gonna make our coding editor two-thirds so I can just see this here the whole time now let’s go ahead and let’s see what happens when I hit get random dogs I’ll make myself a little bit smaller here and let’s watch what happens I’ll hit get random dog and right when I hit that you’ll notice that I actually got a bunch of data back and I can actually see I got this URL I got the message dictionary and then it tells me here’s the URL for the dog and if I command click it it’ll open in a new tab and I can actually even see the image of the dog okay so that means that that’s clearly working so now all we need to do to get that out is we need to just say data and we need to get the data message and that will just get us the URL without necessarily getting us the entire dictionary okay so let’s hit command enter always make sure to restart your server otherwise your changes won’t show up okay and in this app on the right hand side I’m going to go ahead and hit get random dog just like that okay and you could see I got back just the URL right here just the URL okay all right now that I’m getting the URL I know that I can just send that image back to my backend and I’ll just print it out so let’s go ahead and remove this I will say uh this is the dog image and boom it’s that’s already plugged in so we’re giving it the dog image okay and we don’t need this dog image variable here anymore the hard-coded one and uh that’s essentially it so we’re giving the dog image to our front end our front end already knows how to render it right over here okay so let’s go ahead and command enter command enter will restart our server you could always also stop and start again so either way should work and now I’ll hit get random dog and there we go we just got a random dog let’s click it again I got another dog let’s click it again I got another dog beautiful now that is working perfectly but what isn’t working perfectly is I need to find out a total dogs generated so every single time a user clicks any user clicks I don’t care who any single time a request is made by clicking this get random dog a get request is made to our get dog route or get dog function I need to store that in a database and say hey somebody requested a dog which means a new dog image was generated so if I want to do that I need to actually have a number in my database and I need to increment that number in my database so you should actually give that a try see if you could pause the video here and you could get the number from the database okay or you could create the database with replit and try to right and just so you have a hint from replit import DB okay so that should get you the entire database and then the database all it is is it’s just a empty dictionary so to use it is very very easy okay that’s what DB is it’s empty dictionary when when it starts and to use it is very easy if you want to create a new key you could just say total dogs generated or whatever right then um that will just be the key okay total dogs generated and you could set it equal to one and now set it to one or you could increment it by one so see if you could do total dogs generated and make it so in the get doc function it increments it by one make it so you pass that information to your front end and make it so that your front end actually shows that information out here if I go to our index.html file you can already see you have this card Emoji with total dogs all you have to do is pass it data okay where it says zero you need to just pass it a number so what you’re going to do once you have your number is you could just do something like you know dogs generated something like that and once you do that everything will already automatically be handled for you okay because this card component knows what to do it just needs three pieces of information and once you give it to it it’ll make a card for you okay great so let’s go back to here I want you to take five seconds to pause the video and then we’ll do this together five four three two one all right let’s go ahead and do it together now okay I hope you gave it a try and hopefully you made some progress and even if you didn’t get it don’t worry because this is you know this is going to be a tricky task and we’ll do this together all right so from replit I’m already getting access to my database and so now what I want to do is I want to go here and um just increment my database by one so increment okay so I want to say total dogs generated by one okay but here’s the here’s the tricky thing right it’s possible that total dogs generated might not be um might not be uh created yet as a key so if I’m incrementing a key that has not been created I’m going to get an error so a simple hack I like to do which we did in our last app as well is is uh I will just say if dot total dogs generated not in DB then simply go ahead and set that key total dogs generated to be zero okay so now that we initialize it so we will initialize it but anytime our app runs if this key has not already been created okay once we do that we can increment it by one so that’s not a problem and we’ll just send that information back okay so we’ll go ahead and say uh dogs generated equals DB total dogs generated okay so now we’re using the information from the database and storing it in a variable called dogs generated okay or we we could say total dogs generated as well okay um yeah this is fine dogs generate is fine that way it’s not too long and doesn’t run across multiple lines so it’s easier this way okay so I think we’re good on this now let’s go to our let’s go to our front end now so I’ll go to our index.html and inside of our index.html let’s go ahead and add it so here where it says total dogs I’m just going to go ahead and change that with total dogs generated and let’s see if that works let’s go ahead and hit refresh okay we got an error let’s see if we can put this in quotes and if we will still get an error if we put it in quotes let’s try it oh I have to close this out let’s give this a try let’s refresh our app all right so this is not what we want right it says it it literally says the the ginger templating so that’s not what we want so we’re gonna remove that ah actually since this is already python because it’s in these two squigglies it’s Ginger is what they call it but it’s flask calls it I could actually just write the variable name here and I can just say total dogs generated because I have access to that already so let’s go here and let’s refresh okay great um let’s see why it’s not showing it is it giving us an error so let’s just dissect this one at a time so I’m going to go back to our main.py and let’s go ahead and hit print and we’ll see total dogs generated and let’s see what it tells us for total dogs generated um [Music] seems like it’s not actually giving us oh yes okay okay okay okay okay okay so the problem that’s happening is we need to go to this path get a random dog that’s why this will only run when we go ahead and say get random uh dog okay when we click that button click this button right here so let’s go ahead and click that button get random dog okay and interesting it still says total dog to zero let’s hit it again set it again all right and let’s see what our ah dogs generated so let’s go back to our index.html and we’re gonna call this dogs generated here okay so we were also giving it the wrong variable name so that’s what also caused the problem so let’s run this now and let’s hit get random dog okay and now we can see that total of seven dogs are showing up and then eight dogs and nine dogs and ten dogs but watch what happens when I go to our home page it says nothing is showing up right so what we want to do is in our home page we want to also pass this information okay so what I’m gonna do is let’s go here and think about how you can pass dogs generated to also your home page okay so I’ll give you five seconds to try and figure that out you should be able to figure that out and it’s it should be pretty easy okay so I’ll give you five seconds to pause and try it yourself five four three two one all right let’s do it together now so it’s very simple I’m just going to copy this line because I already have access to the database and I’m just going to do the same thing docs generated is these total dogs generated and let’s run this now when I hit refresh it’ll show this even on our home page as you can see the URL is the home pages URL okay great so that problem is now solved when I say get random dog every single time and I’ll increment this by one and that’s already working that is so cool okay now what we need to do is we okay we’re Gucci with this right but now our problem is different our problem is this how come when I go to the home page it doesn’t show me a dog why is the link broken well because if you look at the home page we’re not passing any information about the dog image so when we go here and it’s saying uh where did we put the dog image let’s see yeah we put the dog image right over here right well this variable dog image is actually not defined so when it’s not defined it has no idea what to show so it just doesn’t show anything so what we’re going to do is how about this whenever we get how about this right so whenever on the client side so our client side somebody makes a request to get the dog so we give them a image of a dog okay this is a dog my drawing is terrible then what we could do is we could actually store that inside of our database okay store that inside of our database and we could store that last dog right the most recent dock that they clicked and then since this is in our database now our back end our server side will have access to this okay it will have access to this if you don’t have it stored in the database then there’s no way that our server side will actually have access to it because it’s not stored anywhere it’s just gone that image is just gone so that’s the solution that I suggest we use okay it’s going to be a simple one and so what we can say here is we’ll go ahead and create a new key inside of our dictionary and we’ll say last image okay or we can call it last dog and that will be this dog image right here okay that’s going to be the last or the most recent dog that we got okay and uh yeah and so that’s then since it’s in our database Now everywhere inside of our app will have access to it okay and we’re just going to write that line for that one too so we’re just going to say hey if oh uh last dog last dog is not in the database then I want you to just create it but uh give it an empty string okay so just set it to set it to nothing like no dog so that’s what we’re gonna do and then let’s go ahead and pass this so let’s go here and I’m gonna say last dog okay or we can just say dog image is equal to database last dog okay so that’s the last dog that the user got right so let’s run this now and let’s take a look at our app I’ll refresh this so now our home page will get the last doc from our database since we didn’t have a dog in our database yet you can see that it’s empty but let’s say I now do get random dog that’s the dog that it is stored in our database now watch what happens when I go to the Home Route and we run this we’re now still going to see a picture of that dog because that was the last dog that the user got got stored in our database and then we’re just outputting it okay so now we’re done with the functionality of uh you know being able to ask for a dog and then being able to show the dog on on either to get dog route or the home page route let’s also just add a little bit of Border radius to the dog so he looks nicer so again we’re not going to be doing too much CSS but this is pretty much um all we’re going to tune this is really simple okay so let’s go ahead here I’m going to say style and we’ll add a little border radius here so I’ll just say border radius and we’ll give it a 10 percent that should be good and let’s refresh here and let’s see what it looks like there we go so we got the Border radius so the image looks just a lot cleaner right just to we didn’t even have to add too much border radius just that alone should be good all right so at this point of the app all of the global data parts of this app are now completed meaning uh the total dogs is like this global data part of the app that’s completed and being able to get this dog on when you click get random dog that part is completed so like the surface level functionality you know or the global app functionality is completed but the entire main functionality of this app actually lies in user-specific information okay so this part is not completed yet that part is not completed yet this part is not completed yet being able to sign up with the new user has not completed yet being able to log out is not completed yet and then showing those specific users and everything is not completed yet right so there’s a bunch of functionality that we need to add and all of that functionality is user specific okay it’s user authenticated in other words so it’s like if that user is not authenticated uh this stuff will not work okay so let’s go ahead and add uh stuff in about the user so I’ll show you something you can actually uh first of all actually let’s do this let’s make it so when somebody types in their username and hits login we actually get access to that data okay so let me go ahead here and let’s type in a name so I’ll just say Kamikaze or I’ll say Kazi right and we’ll hit login here you can see it says method not allowed and this method is not allowed for the requested URL now let me show you something here because this is going to be an important thing to understand so if I go ahead and I hit inspect and I go into Network okay and make sure that you’ve chosen all in this case and let’s go ahead and hit click a certain page okay so let’s close this for a second and I’ll hit get random dog all right now let’s go ahead and hit inspect and go to network and um just go ahead and do get random dog again or just refresh the page while you’re on this URL that says get dog and you can see we’re actually making a uh we’re actually making a request get dog okay so you could see right over here it says get dog and let me see if I can actually make this a little bit bigger so you could see it okay there we go um and you have you could see that we may get dog and you can see the request method here is a get request method status 200 is means everything is good it’s clear it’s working and made that request this was the request URL and then it knows as a result of that request there was also a response okay you click this response right here I’ll show you like what happened as a result and it popped in that image right and that’s what happened this is obviously what the page would look like if it didn’t have any styling okay so you could see that that’s the that’s the response we’re getting okay now if I go ahead into this username um and the next one Watch What Happens okay so this is this is how a get request looks like okay we’re doing a get request on this now when we’re sending data through a form like here if I’m writing cozy and I’m trying to send the data through a form this will not happen from a get request get request is only so you can retrieve information but if you want to send information then you use something called a post request okay so this will get info okay this will send info okay so what what do I mean when I hit get random dog I want to get a dog so I make a get request but when I want to set when I want to sign up a new user I’m getting the data from the user the user is sending that data so that’s going to be a post request to our back end okay and watch this when I go ahead and click um login sign up it’s going to take me to this page it says method not allowed okay now let’s go ahead and hit inspect and let’s go to network again okay and let’s refresh this exact same page and you will see I get this 405 error right here and this will be all red so let’s click this and it’s saying hey the request method was a post the status code with a 405 meaning like not found or error basically okay so 405 this is so it’s a method not a loud error that’s usually what that means I believe and it happened at this URL okay so now we need to think about well you know what the heck happened and you know how how do we uh stop this from happening now you might be asking the question well how come one button get random dog sends a get request but clicking the login sign up button sends a post request how come they are sending different requests like how come this is get and then this uh this button right here is post how’s that happening well the answer lies in the HTML part of the code so if I look here and we find this login button so I’m going to hit uh get random dog so this button here right we’re passing the action slash get dog by default the method that’s being used whenever this is a situation uh is that it’s a it’s a get request so here this is a get request that’s happening okay that’s the default Behavior over here where we’re saying login slash sign up right over here if you actually look at the form the action is route to the home page but the method is actually post okay so the method is a post request that’s what’s triggering it so in other words I could actually take this here and say method is equal to get and it wouldn’t really change anything because by default if you don’t write this this line is already there okay so having this or this is the same exact thing but I’ll just leave it there so it’s you know you could cross compare it so here we have in this login form we have the username placeholder thingy and then we have the button so you could actually click that button and then you know since it’s a post request it has the ability to send the data over to our server now here’s how we retrieve this data so when we go here to our main.py let’s go here to our app route now that method it said is not allowed right so we want to do is we can send in methods and we want to say you we should allow the get method and this route will actually allow the post method now let’s run this app and let’s click login and sign up and let’s just see what happens and what’s the error it gives us so when I did that watch what happened it didn’t give us an error this time right let’s click it again log in and sign up no error so what did it do well it detect it said that this method is loud and then it just went ahead and rendered the home page okay but since we have a post request happening we want specific things to happen we want to be able to retrieve the data that the user just gave us so what I can actually do is I can say if request and request this is global uh variable that all of the flask app has access to and so if you run it inside of a like a route here and you do requests you’ll get access to things like what the user passed in as you know query parameters or what the user passed in through a form you can access all of that and so we can also detect methods so if request method is equal to post then I want to say print you clicked the login slash sign up button okay so let’s just go ahead and try that so I’ll command enter so our server restarts and takes changes I will open this up so I can see everything being printed out and then we’ll run this ah request is not defined so we want to do is we want to grab requests from flask so let’s go ahead and grab request okay let’s hit command enter restart our server here okay let’s go back and now we shouldn’t have any errors coming up now if I hit log in and sign up you could see if I click it you could see over here it said you click the login sign up button right over there okay you click the login sign up button okay so that means I’m detecting The Click happening now I need to actually get the user form so I can actually say print request dot form and we can ask it for the username okay and we’re going to say user underscore name now where is this username thing coming from this has to do with our front end so if we go here we said that this input is going to have the name of user underscore name so if we want to pull the value out of this we need to make sure that in our back end we also say user underscore name just like this okay so now let’s go ahead and open this up and we’ll run this hit command enter to run it and then we’ll type in a user here okay so let’s go ahead and say quasi and I’ll say log in and sign up and you could see that it’s pulling the data from The Forum now so we can actually now get input from the users right that is awesome so now I can see it can say okay you click the sign up button and it says cozy over here okay so this is a really good sign it means we have made a ton of progress all right now that we’re getting the user what we could do is we could actually store that session so I’m gonna go here I’m just going to comma session I’m going to import uh also get access to each session inside of flask and sessions are kind of like cookies to store data about the user okay and now in the session I can just go ahead and say session user okay or username whatever you want to say and I’m gonna go ahead and do this say username is equal to whatever the user puts in the form okay so if the user makes a post request and submits this form whatever user the name they put in that becomes your username and then restore that inside of our session okay so that becomes a username we store that inside of the session Okay now what’s cool about session is our app has Global access to this session thing which means that I can actually go to our index.html here and just call session without even needing to pass session through um through my template okay so let’s go to index.html and here where I was passing the word cozy instead I’m just going to go ahead and say session user just like that okay so let’s go back and let’s re-run this app so I’m going to hit command enter okay so we restart the server and let’s go ahead and try saying my name is David and let’s see if this changes over here so I’ll say David okay we got an error ah so whenever we’re setting sessions we need a secret key okay our app will need a secret key so we’ll just go ahead and set it up if you’re getting this app you should already have the secret key set up for you and some other variables in there but I’m going to be writing this from scratch so you could just understand better what’s going on so basically here I’m gonna go ahead and set my secret key okay so I’m gonna say app dot secret key and I’m going to say very bad secret key okay if you want to use actual secret Keys what I recommend is Rebel it has something called environment variables so you could store a secret key there and then you could retrieve it from here okay and they have docs too if you want to go see how to use sensitive information and they have OS dot Environ right and then you can actually type in whatever password you wrote so that would be a pretty good way to keep it secret and as a matter of fact we could actually even do that right here so I could say OS dot Environ okay and if we look at the docs the docs tell us to do this right the docs tell us call this function and then uh or not sorry to call this function it’s a dictionary so index it and then I’m just going to say flask key okay I’ll say flask Secret key I guess we’ll say that and then here I’m just going to put very bad secret key okay that’s it and I’ll add this as a new Secret flask secret key now if you’re gonna store a secret key do it like that that’ll store it safely that way anybody viewing your code will not actually know what your secret key is so you could actually keep it hidden rapplet makes this very very easy for you but yeah right now we’re just doing dummy data stuff so that’s why we don’t need to have put like a actual you know 64-bit or you know 256 bit Shaw hash cryptographic secret key we could just put a pretty bad key and we’ll be fine all right and then what I want to do is I’m going to do app.config and I want to say session so since replit has an iframe that they’re using here sometimes cookies don’t work properly so if we go ahead and do session cookie same site that’ll solve that problem okay none and then we’re going to go ahead here and I’m going to say I have a cookie config so this session cookie secure and we’ll say true okay and that’s done this these lines are not even really necessary this is just so in the replit iframe if you’re running your app it works the main thing was just setting the secret key here okay without this you can’t have sessions so let’s hit command enter let’s refresh our app and let’s see what happens okay I think we got an error let’s see what’s the error we got so it says OS is not defined so let’s go ahead and Define that and at the top here we can just go ahead and import OS just like that okay so let’s run this and let’s see what the error is now okay now it says app is not defined okay no problem so we’re going to go ahead here and put that as one of the first lines of the code okay and now we shouldn’t be having errors great let’s go ahead and refresh this there we go you could see it says David okay now let’s go ahead and change it to Cozy and if I hit login sign up you can see it says cozy and let’s go ahead and say ninja and I’ll hit enter and now you see Ninja here okay and there’s something deeper that’s happening okay so for example if I refresh let’s say I go ahead to this link and I just refresh right if I do that the name is still there if I hit get random dog the name is actually still there what the hell is happening how are we making this happen the secret lies in what’s what happens if you hit inspect and you go to application here okay and inside of application you’ll see we have session storage and we have cookies okay so let’s go ahead into our session into our cookies here okay and inside of our cookies we can see that we have this session okay this session is storing all of the value okay this inside of this session is where we’re storing our user and everything so as long as you have access to that session right there you’ll have access to everything that that session sees okay so if you store a key in there you’ll have access to that session and let’s see the expiry date over here okay it’s kind of like cookies okay uh the expire it pretty much never expires okay it runs for a really long time so that means even if you refresh you’ll you should still see this as long as your session is up okay and I believe until you close out your Chrome browser or something like that or your Safari or whatever you’re using and you come back so that’s what it’s where it’s stored it’s stored in like a local storage cookies uh inside of your browser and that’s how it actually stays there even after some time okay so that’s why we used um that’s why we use sessions there all right now that the user is signing in and I’m able to retrieve that user’s information what I’d actually like to do is store that user inside of my database and that way we could have a big database uh with you know so just imagine a giant dictionary with um every single user that’s ever signed in okay and information about them so essentially how I would want my users to look like is Imagine This I imagine I had users and they would be it would be a list of dictionaries so like this okay and then each of those users would have some information about them so for example I’ll have their username uh they would have you know so let’s say qazi they would have logins okay so that let’s say that would be you know One login or whatever and then they would have they would have dogs generated okay so like the dogs that they generated and that might be you know five or whatever and we would have each dictionary here would represent the user so let’s say David has like four logins right then we have another user and their name is Kevin and they have like 12 logins and then their dogs generated is like 27 or something right so this is how I want my users to look like and then I want to be able to Loop through the users and get you know get a user update a user change their record delete them whatever I want to do right so that’s how I would want to do it I would want to have a list of users just like this and each dictionary representing a user now what I would want to do is I’m going to store this inside of a database okay I don’t want to just store this in a dictionary because while that is nice um after I refresh all that information is just going to vanish and so I don’t really want all my information to just vanish right that’s not fun so let’s go ahead and use a database so what I will say is in my database I’ll create a collection called users okay and users will have um have some data okay and we want to add if we want to add a new user well how we would add a new user is we would actually go to our users okay and we will append a new user and when we append we’ll need to essentially append a dictionary just like this okay so remember since this is a list I can call Dot append on it and so when I call an append I can add a user to it okay so if you’re having trouble visualizing some of this don’t worry let’s open up our shell and let’s just write some python code here so you can see what we’re talking about okay so I will go like this and let’s zoom in a little bit so you could see clearer okay so let’s go ahead all right let’s go ahead and say um let me zoom in more so it’s even easier to read just like that okay now let’s say I have a database okay and I’ll make it an empty dictionary just like this now inside I did inside of that database I want to have a list of users so initially I’m just going to create it and we’ll set it to this so now when I do DB you should see DB has users and it’s empty so it’s an empty collection right now now if I want to have access um you know users I want to add a user I could just go DB dot append right and then I can DB users if I do this it should give me back that empty list now if I want to append to that list I could just go a DOT append just like this and I can give it a user to a pen so I’ll just give username qazi and then logins two and then we will say uh dogs generated I will say seven all right there we go now when I do DB users I will see that I have that user quasi in there okay now if I want to get information about the user I could go DB users and I could say you get me the zeroth user okay if we have multiple users get me the zeroth user so let’s try adding more users so I will say I will also add David you can just hit up up on your keyboard and it should bring this up and I’ll also add Kevin okay now when we do DB users Watch What Happens I get all of this data right here okay uh let me zoom out here and zoom here now as you can see better there we go okay so now that’s what DB users returns to me right it’s a list of users so if I call type on DB DB users you can see that it’s a list so since it’s a list I could even I could Loop through it I could also index it so I could go DB users and if I ask for the xeroth guy well that’ll get most likely cozy now if I did DB users and I said zero you can see it’s giving me Kevin okay if I go ahead and do one your your zero might be different than mine because my computer crashed and I had to redo this again so that was fun okay but uh yeah so but idea is that it should be giving you some users okay I’ll do user three I ended up creating more users okay I I have like four total users right and so it’ll it’ll show you those users now if I want to grab a specific user and update one of their properties well what I could do is I could go ahead and do username and set that equal to uh let’s just say a fire emoji okay so now if I print out DB users you could see that there’s a user with the username of fire emoji uh just put quotes around if you if you do use an emoji make sure to put quotes around it but you could see that now we got a guy with the fire Emoji so that name has been changed okay so this is how it interface with it uh this is how I would you know add to it this is how I would uh update data inside of it Etc so keep these notes available for yourself because this information is going to be really valuable helpful and important um when we’re creating our database okay all right now when we’re creating our users we need to create that inside of our database okay so I’m going to go ahead and say DB users and when it’s not initialized I need to just go ahead and initialize it so I’m just going to go ahead said if users not in DB then just go ahead and say DB users and set it to an empty list just like this okay so this is going to be the default state that we’re going to have for this and then here I’m going to go ahead and say let’s go down here so once the user signs in we’re going to go ahead and say DB users and I’m simply going to append so we’re going to append and we’re going to append that user okay so when I think about that user well that user at this point is going to be you know their username is going to be uh the username they gave us because that’s already stored here um and then we’re gonna go ahead and check there logins and we’re gonna so since it’s a new user right so this is for a new new user okay we’re going to set their logins to one and we will also set their dogs generated to zero because right now they haven’t really generated any dogs they just signed up they haven’t like hit get random dogs yet right that’s all they have done so we’re going to set that to zero and so in our users database now that user should be there okay but you’re gonna see that we’re gonna run into a problem pretty quick with this so let’s go ahead and do command enter and I’m also going to open up our shell and inside of our Shell let’s just clear all of this out I’ll write python I’ll hit delete at the top and I’ll type in Python so you can see this okay and now let’s go ahead and do from replit import DB so we get access to our database in this environment okay and if I show you the DB right now there shouldn’t be much inside of it okay like let’s go ahead and do DB dot keys so we can see all the keys that are there so right now there’s only uh there’s a last dot key there’s a total dogs generated key and users key so let’s go ahead and take a look at users okay and so far we don’t really have um any users yet okay inside of our database and that’s what we wanted now let’s go ahead and create a new user so I’ll say Kevin and I’ll hit log in and sign up okay now this should be updated to Kevin here let’s go ahead and run that line again and you can actually see that Kevin was added to the database that is very good so look at that we’re already being able to add users to our database that is exciting let’s go ahead and add cozy and let’s see what happens I’ll hit log in sign up let’s go hit up and enter and now you can see we have two users so this is an observed list kind of ignore some of this Parts observe dick but all it is the list and this is a dictionary okay and then here in this dictionary here in this list here there’s two items so let’s see if we can um call length on this or maybe it doesn’t allow us to call length let’s see yep so we can see that there’s two users in here okay and I can also just go ahead and do users of zero and it’ll give me the first user which is Kevin and if I do a one it’ll give me the second user which is qazi and yeah so I could see both of the users are there with all of their information logins is set to one doc generated is set to zero so that’s good so far but here’s the problem what happens if I sign up with Kazi again okay and this is case sensitive so let’s say I sign up with Kazi again let’s hit sign up and let’s go and print out our database and you can see we got three users now what we got three users so what there’s two causes you know our goal was to make sure that everybody has a unique username so if you sign in with the same username right in our in our final working app if you sign in with the same exact username so let’s go ahead and do qazi here it will be a login okay and if you sign up with and it won’t reset all your data it’ll keep it at 11 52 all of that but if you sign in with a new username and you hit log in um you could see that it resets your data right login One log dogs you generated is one as well okay so let’s go ahead leave this signed in okay and let’s go back to our app that we were building Okay so huh why when I do quasi this is happening so what we want to do is we want to make sure that that doesn’t happen so how do we do that well we need to actually I’m going to remove this because you don’t need that anymore what I need to do is actually need to make sure of the following okay when a new user when a user signs up or logs in so basically when the user hits when user clicks this button right so when user clicks login sign up here’s what needs to happen check if that user already exists okay that’s step one and then two if that user already uh if and okay so let’s say that that user exists okay so I’ll put it under here if that user already exists then what we want to do is we’re going to select that existing user okay we’re not going to append anything we’re just going to pull that user out of our list of users okay select this is from our list of users otherwise um if it’s a new user if it is a new if the user does not exist already AKA new user so if it’s a new user well then I want you to go ahead and append create and append that user to the database okay so this is really the logic that we’re actually trying to do so we can’t just like mindlessly put a new user without checking if that user already exists so what I want to go ahead and do is I want to check if user already exists okay so how do I check well look at this let’s go ahead to our python rubble and here if I do DB users I get all these users here right so if I want to check for a specific user in here I can actually use filtering which is very powerful so let’s say I’m looking for a user um you know let’s say I’m looking for qazi okay what I could say is I could say get me the user for user in DB users okay so Loop through so this is the for Loops it’s like Loop through every users and users okay and what I want you to do is only get them to me if users username [Music] is equal to cozy okay it’s exactly equal to cos if that’s the case then get that user for me so what it’s going to do is going to filter this entire list and get me where the name is qazi so let’s run this and now when I show this it’s giving me two people but both of them have the name Causey in there so it’s filtering it out okay in a normal list we would not have this other qazi in there the second quasi that we have we would not have then in a different database okay let’s try this same thing for Kevin now so you should give this a try okay see if you could get Kevin here okay give that a try I’ll give you five seconds to pause and try it five four three two one okay so we’re just gonna go ahead and say so this is saying Loop through for the user and users if the user has Kevin inside of it then just give me that user object okay or give me that User Dictionary so let’s go ahead and do this let’s try to type in Kevin and you can see that list is being filtered out and we’re only getting Kevin back so that’s how we can actually check for a user okay so I’m gonna go ahead and pretty much copy paste that code okay and what we’re going to do here is instead of a hard-coded Kevin here we’re gonna put with the user we’re gonna check for what the user just typed in as their username okay so that variable is already called username so we’re going to take that variable and put that there instead of Kevin instead of hard coded Kevin so I’m going to say username just like that okay so um what will happen if it doesn’t exist let’s also go ahead and see that so let’s look for a name that doesn’t exist so I’m going to go and remove this and then just say Lance because Lance I know does not exist okay oh there’s an indentation problem if somebody doesn’t exist it’s going to give you an empty list and if you call Bool on an empty list it’s going to give you false okay so if a list is empty by default it’s false and if a list has something in it like haha or something it’s going to be true this is going to be helpful for our if then statement that we write here okay so we’re going to say if uh the user exists that’s what this is saying if the user exists then I want you to say for now I’ll just say user already exists okay if not then I want you to go ahead and append the user okay and I’ll say print uh new user okay and we’ll put ta-da here and we’ll put an exclamation mark here so it’s easier to see it in our print statements let’s hit command enter so these changes do take place okay and in our app here I’m going to say quasi and let’s hit login slash sign up and you could see it says user already exists it’s telling me right there okay let’s go ahead and now type in Lance okay and it says you click the blah blah button new user and now you can see Lance is showing up here and now watch what happens if I hit Lance again and I do log in and sign up so now it’s detecting it as a login there we go rather than a sign up okay so now it says user already exists it already knows that that’s Lance Okay so um so yeah we’re able to actually detect if the user exists now what happens if the user already exists well we don’t really need to do much we just need to update their logins that’s really it right so if a user who’s already signed up logs in again and if we look at our main app we just need to increase increment their number of logins so how could we do that well we can go grab that user right so that’s that user right here okay so let’s go ahead and say so I can actually say user is equal to that and remove that colon from there and I can say if user so this will grab the user it’ll either return an empty list or it will return a list with that user okay so we’re going to say if user print user already exists and will actually print out information about the user by doing user of zero okay and then what we want to do is we want to grab that user’s logins and we want to increment them by one okay so let’s go ahead and paste the print statement after the login so we can see the new updated login so let’s go ahead and hit command enter here and we’ll open up this and we will now go ahead and type in Lance and I’ll hit log in and sign up okay we got an error it says list indices must be integers or slices not string so let’s see ah yes so we want to go ahead and say user uh user of zero right so the user that we found that specific user’s logins we want to increment that by one okay so let’s go ahead and give that a try so let’s go ahead and open this up I will say Lance here and we will hit login okay and it says user already exists okay let’s go ahead and type in Lance again all right so let’s see what’s happening here is it printing that out print user of zero yes it is and so you could see it’s saying user already exists and then it prints out the users you could see the user initially was at two logins and then we print them out again and there are three logins and then there are four logins right so that means that the number of logins is incrementing correctly now to make our lives easier so we don’t keep having to say user of zero what we could say is we could say user is the following user is um so we’re going to redefine user here and I’m going to say it’s user of zero if user exists if we found somebody and if not then we’re just going to say user is none okay so it’s either the first guy in the list that we get back here after we filter the list or if it’s empty then we just return none so this user is either going to be uh a user like this with a username and blah blah blah blah blah blah blah okay so it’s either going to be a dictionary or none okay like that so we can remove the zero here now and I can remove this zero here now too because I’m deconstructing the user from that list okay and let’s run this and I will go ahead and say Lance here and we will hit login and sign up you can see it says user already exists logins five and it’s working correctly okay great so there we go a lot of that functionality that we wanted to add is now you know pretty much completed it check if a user it checks if a user already exists if they do exist then it goes ahead and um and increments them right if they don’t exist then it goes ahead and appends them okay so now what we could do is we could turn this into a nice little function so we don’t always have to come to home and see this big giant definition which can look intimidating and honestly sometimes confusing so we could actually say Define create or update user okay and I’m going to Define this function all the way at the bottom so not here okay so let’s go ahead all the way at the bottom here and I’m going to create a new function and it’s going to just be called and this is going to be a regular function so it’s not going to be a routing or anything like that we’ll say create or update user I’ll take in user and then the definition of this function it’ll do this okay I’ll copy that and I’ll paste that in here okay and um indentation problems will happen so just go ahead backspace delete everything I’m doing yeah just backspace everything correctly okay backspace delete and then this else is going to be on this same level right there okay so I’m actually holding command and then I’m hitting delete on my Mac you could probably hold Ctrl and hit backspace or something on your windows and then hitting delete one more time to come here and then hit enter command delete delete enter command delete delete enter command delete delete enter command delete delete enter command delete delete enter there we go okay um so there you go okay so now we’ve created this function Creator update user and it will take in a username so we’re going to go ahead and say user underscore name okay so this function if you just give it a username it will automatically know how to find that user and then once it finds that user it will either update their logins or it will create the new user inside of the database so that’s what this function does create or update user so we’re going to replace all of this code right here and delete it so now our functions everything should look very similar and we’re just going to say create or update and give it the username and never have to worry about the logic anymore because the logic is already done okay so once the new user signs up then we create or update that user in our database that’s it okay so let’s go ahead and check if this is working correctly so I’m going to open up my python here let’s go to shout let’s run this and let’s see if we’re getting any errors or anything like that okay I’m not getting any errors let’s go to our shell okay I will delete this right here and then we’ll just say from ruplet import DB so I can take a look and then let’s go ahead and create a new guy called cookie and we’ll hit this and now I’ll do DB users and um let’s see if we find Cookie yep I see cookie with logins of one okay let’s have cookie log in again and I’ll do DB users and we could see cookie logins too okay so no problems there that worked perfectly so being able to create a new user works and updating an existing user works because you notice it didn’t create another cookie it kept the same cookie guy and just incremented right so for example if I update Lance if I log in Lance Lance should update and not get created so if I do Lance login now when I run this you’ll see Lance logins of six okay but if I do pants a new person and we do login and I do DB users you will see that pants here has a login of one so it’s a new user that was created so the ability to create a new user is now working perfectly okay Creator update user and now that that functionality is working and we have that user inside of our database now we can actually start passing that user around that information we could actually pass it around and do stuff with it right so for example name I don’t need to worry about because name I’m pulling straight up from the session but what I do need to pull from the session okay is not just the user but I need to pull more information about the user like their number of dogs okay or um the number of logins okay or the dogs that they’ve generated Etc so what I’m going to need access to is I’m going to need access to that specific User Dictionary okay that user object that stores all the information about them like their username docs generated Etc so this function here Creator update user I’m going to have it return something so I’m going to say return and I’m going to have it return the user okay so the user is either going to be the first guy or it’s going to be none okay so I’m going to always get some type of user okay now what I can do is over here I can actually go user is equal to that because I know that this function is going to do its thing but then at the end of the day it’s going to return a dictionary okay uh or it’s going to return none for the user and that user I can pass it to my database or sorry my front end so I could pass that user to my front end and now my front end will have access to that user’s information in terms of the dogs that that user has generated or that logins that that user has generated okay now the last thing that I want to do well let’s actually make logins work first so let’s go ahead and go here I’m going to go inside of my templates and we’ll go to index.html and since I have access to that user what I can do where I have the number of logins I can actually just go ahead and give this one a try because you can actually figure out what to write here okay how will I get access to users logins so see if you could make this work all right I’ll give you five seconds to pause to try it yourself five four three two one all right hopefully you’ll give it a try and figure it out um I’m gonna go ahead and say user and logins okay since I already have access to that user so let’s go back to our main.py I’ll open it in a new tab here see if I could reverse this cool okay and then let’s go ahead and hit command enter okay and now let’s see if it shows us number of logins so I’m going to go ahead and do Lance and let’s hit login look at that it’s saying seven let’s log in again let’s run it run eight Wow Let’s Do It Again login nine right now new person one you could see new person one doesn’t have any logins because this is a brand new uh user okay it’s a brand new user it doesn’t have any logins right now okay so number of logins right now is zero and we don’t even have to write zero okay so for logins we can actually do that same trick as well if we do want it to say zero you could probably just go ahead and say logins is user uh logins okay uh if user logins exists else is zero so this basically what we’ll do what it will do is if the user has logins then it’ll show the login and if the user doesn’t have logins if this statement is false meaning that it they have zero logins it’ll be false okay that’s the cool part about python there’s something called truthiness of a statement so 0 will evaluate to a false then it will just give zero okay so if I do this and we can also always go back here and just say um logins okay so I could run this and then let’s refresh and there we go number of logins right now let’s say I whoops that’s not ours this is our app okay so let’s go ahead and now refresh okay so this person has so let’s create a new user I’ll just say two uh okay so not not user login or not user logins check let’s just check if the user exists okay so give me the logins if the user exists otherwise l0 that’s better that’s a better statement okay let’s run this again okay and let’s look at it okay let’s create a new user I’ll say three let’s run okay and now you can see my username is three and my logins are zero so that’s actually working correctly now or let’s say uh Michael okay let’s hit enter and you can see I’m Michael but my logins are zero okay so that takes care of that you know number or zero problem okay we could even create a template filter and then that could be you know a thing that gives us whether it’s a number or zero okay we could do it that way uh we could certainly do it that way as well okay actually it should say zero without us doing any of this hacky stuff um the problem is I believe happening that we’re not returning our user here after we create the new user so we shouldn’t be returning none that’s not right uh we should be returning a user so for example if a user exists well then we know that we can just send that user right that’s easy because that’ll already exists but if the user doesn’t exist we want to create the new user right and then we need to set that as the new user so we can actually go ahead and say okay I don’t I don’t even need this uh here right now the statement here okay or yeah I do so we’ll leave this user statement here that’s fine and now we’ll just redefine the user and we’ll say user is equal to okay and then we’ll find that user again in the database using the same exact trick right here okay so we’ll find the user so we’ll say user is equal to [Music] username but we’ll find that specific user inside of the database okay after we create that user now we can find them right because we couldn’t find them earlier so find the user if they exist obviously just increment their login but find the user if they don’t exist append them then find them again from the database so we can actually have that specific user dictionary that we need access to okay and that’s going to be the that’s going to be the user okay and then we can just get the actual user right here um and then we can actually return that user okay or we can just run that same line again but in this case we know for a fact that since we just created that user looking up somebody with that specific username will return to somebody and we’ll get the first item in that list by doing the zero right there and then we can just go ahead and say return user okay so now that problem will actually be solved so let’s go back to index.html we can go ahead back to saying user logins okay let’s restart the server by doing command enter control enter okay and let’s create a new person and we will say Angela and let’s run okay and there we go we shouldn’t ever see Zero logins either by the way we should actually see One login because when she created a new sign up that is considered a login right so it should start her with one and then if I do Angela again it should put her at two Etc Okay so that part is now good now the next thing we want to do is you want to do some conditional rendering meaning if a user is not signed in AKA when the user is on this page you notice that I’m not seeing any of the other stuff right I’m not seeing the table I’m not seeing get random dog I’m not saying log out I’m not seeing a leaderboard I just see this enter your name okay that’s all I’m seeing so but then once I log in successfully then I am seeing this entire page okay but then I’m not seeing this input here anymore okay so we want to make sure that we have that same functionality as well that technique is called conditional rendering meaning only render if this statement is true or else show this other thing so what we want to do here is let’s think about what are the things that we need to hide if the user is logged in well if the user is logged in we need to hide a we need to hide all of this right there okay so what we’re going to do is find that part so this is the form and this is where it says enter names all of this stuff here we need to hide once the user is logged in okay so I’m going to go ahead and say if session um if the session does not have a user okay meaning if there’s no user logged in then show this otherwise don’t show this so let’s give this a try so if I’m logged in with Angela it should not show anymore once I restart the server fingers crossed let’s see let’s refresh obviously because we didn’t do that okay and we can just go ahead and do this okay so now we have a different problem it says the variable user referenced before an assignment okay and to debug errors don’t freak out let’s just go ahead and see how to debug okay because that’s another thing you’ll need to learn so we can actually click right here user and it’s telling me that it’s something is happening on this line here so I’ll go ahead and click this so it’ll expand and it’s saying the error is happening right here on line 43 and home you’re referring to user but the user has not been actually defined okay so let’s see what the problem is okay it says local variable user reference before assignment so to me when I’m looking at this that doesn’t look to be true because I’m clearly assigning user here and then referencing it here but let’s see if it says a different story when we go look at the code so let’s go to our let’s see if it’s referring to anything here ‘s a reference before assignment let’s go back up to our home ah so this user is only getting registered when it’s a post request meaning when it is uh when we’re submitting a form if you’re just going to that page normally right the there’s going to be no user when you’re going to that page let’s say uh you know by just refreshing and uh not doing a post request so what happens in that case so what we’re going to say here is otherwise the user is just none okay so for example else uh we will say user is none okay so we don’t have a user so that’s what we’re going to do for the user okay let’s go ahead and hit enter now we shouldn’t have an error happening anymore let’s hit command enter as well here so we restart our server let’s go here and refresh and let’s see what happens what will happen hopefully it will do what we want it to do here we go we’re seeing Angela we are logged in and notice that we’re not seeing that form up there that used to say login username blah blah blah right now let’s go ahead and set up our log out functionality so we’re going to set up a route for logout so this should be pretty easy at this point because you know the pattern already right slash log out so we know that we’re going to create a slash logout route and then we’re going to write python code that corresponds to it so I’m going to say app.rout put at sign here and we’re going to say log out and then we’ll create our function log out and what that will do is set the session of the user to none okay that basically means that the user is logged out okay that’s all we’re going to do with this it’s going to set the username to none basically okay that’s all we’re doing here and then we will just render our template okay render and now I can just basically go ahead and return um all of this stuff here so I can just put this return statement and I can say return render template index.html set the dog image well we won’t have a dog image here in this case because we’re sending you back essentially to a page that doesn’t even really need it what we’re going to have is dogs generated for log out and that’s essentially it so let’s go ahead and hit enter command enter to restart the server and now when I hit log out it will log me out and you won’t see Angela here anymore so let’s hit log out cool okay now we’re getting a different error which is user is undefined okay and that error is happening where let’s go ahead and take a look let’s click it and it’s saying that the error is happening right here where we’re calling user logins it’s like where the heck is user even coming from right so now we just need to figure that part out because if we’re passing in none here right for the user then we should essentially have no user to pull login information from so here’s the way we want to solve it we could actually solve this with conditional rendering all of this stuff here like all of these cards right and these buttons container if we get random dog or this button it says log out or this leaderboard all of this I don’t even need to I don’t I only need it to show up if the user is logged in I don’t need it to show up anytime else so it’s going to solve that problem here so we’re going to say if session user meaning if the user is logged in then show all this stuff otherwise don’t even bother showing this so I’m just going to finish wrapping this entire thing okay and uh so where did we start wrapping it right here wrapper let’s see where that wrapper finishes finishes right here so we’re going to go ahead and say end if there we go okay let’s hit command enter so we restart our server and let’s go to our app and let’s go home page okay and you can see we’re here and if I log out you can see that it brings me to this page it’s just rendering only these components here but when I log in so let’s say Angela and I hit enter boom Angela right there number of logins right there get random dog okay now we’re getting a different error for user okay so now we’re getting when I hit get random dog uh we’re getting an error with the user so let’s see what the problem is so let’s go back because we know that we’re logged in for sure so what’s there here we’re not actually grabbing the user and uh we’re not uh basically when somebody clicks get dog we need to get our current logged in user right and then we need to pass that current logged in user uh here we should be able to actually pull that information from our session as well so we should be able to go user and then say session user if there’s a user logged in that session could have access to it but let’s go ahead ah but the problem with session is that we only have um yeah we we only have the username we don’t necessarily have the login so let’s actually just grab the user from the database so let’s do this there’s this piece of code that keeps repeating itself okay this guy right over here it repeats itself here and then it kind of repeats itself here it’s kind of annoying so let’s go ahead and refactor this because we’re going to need to find here we’re going to need to find that user that’s logged in so we’re gonna have to essentially repeat this code for the third time so let’s not repeat that many times okay there’s a principle called the dry principle and it stands for do not repeat do not repeat yourself okay in programming so we don’t want to keep repeating ourselves okay so let’s create a new function and this function will call it get user get user from database and this will take in a username okay and it will return that user now all it will do is this okay and what I’m going to say is I’m going to say this is the user and here we can just return return this okay so either return the user otherwise return none so now in order to get the user what I could actually do here is for the user I can actually just say hey get the user from the database based on the username so I can refactor that part of the code and then I can get rid of this don’t need that and then here we need to repeat that same thing so just copy this paste it here because we need to repeat that process again so find the user in the start find the user if you create a new user and return that user so this function we’ve already needed to use it twice so it was already very helpful creating that and it simplifies our logic a lot okay and uh great now I’m gonna go up here and reuse this function here and I’m gonna say user is get user from database okay and then we’re going to give it the username and the username here we could actually get that from the session if we want okay so we can definitely get that from the session so we need to actually check if the session actually exists okay so let’s go up here and I’m going to check um you know we could put it at the top or the bottom I could even put it here so I could say if session okay so if there is an existing session with the user then what I want you to do is get that user from the database so get user from database based on the uh username so I’m going to pass in the username right here and then I also want you to get the dogs generated for that user so I’m going to go ahead and say user uh dogs generated and I’m going to say and there’s a nice little trick that we could do so for example imagine if we did this right dogs generated and I did increment the dogs generated by plus equals one right that’s what I would like to do but the problem is that there might be a case well actually in this case every user will have this docs generated I don’t think we even need to worry about that I think we could just leave this with with dogs generator plus equals one and I think that should be fine okay and then once we do that we can actually so we’re going to update that user’s docs generated by plus one because obviously they just hit get dog and then we can actually pass that user back so I could say user user okay let’s run this now and let’s go here let’s refresh our app and hopefully we won’t have errors now okay so right now we have Angela and it says dogs generated or zero let’s hit get random dog okay get random dog get random dog okay so we know that this button is working we know that it’s getting us random dogs we know that the number of dogs is increasing we know that the user Angela is logged in but the dogs you generated is not changing why is that happening if I go here right why is that happening well the reason why dog generator is not showing up is because we haven’t connected it yet to our front end so let’s go to our index.html and here we’ve hard-coded zero so all we need to do here is I need to say user dogs generated just like that let’s hit command enter let’s come back here let’s refresh okay and now when we actually do get random dog you could see that that is updating right get random dog boom there we go right all of it is updating now so if you’re Angela you can refresh and come back and it’ll still say nine dogs generated right if you log out and you log in as heavy it’ll say that this is your first login ever and then you have zero deduction right it’s all remember you if I log out and log back in as Angela you’ll see that it remembers that too it’s a 10 dogs generated number of logins four so it’s showing each user their very own authenticated information which is very powerful right in in all of the apps that you see with Instagram or whatever you’re only seeing your own data you’re not seeing somebody else’s posts you’re not able to control somebody else’s post you’re not able to log somebody else out you can only log yourself out so even though we didn’t have email and passwords this is still a very good mechanism to be able to see where you’re at and what’s actually happening now the only real functionality we have left is creating a leaderboard so we can actually see all these users and then we can actually like increment all these users right and um yeah so all the users we created like for example right now just showing dummy users so we want to be able to show all of our users and then we want to be able to show them in the right way so first of all let’s just try to show all of our users okay so how will I do that well my database has access to all the users already okay let’s go here let’s go take a look at our table okay and right now what it has is it’s uh all the data here is hard coded okay so first let’s go ahead and just study the structure of this table here so we have a table class leaderboard it has these are the headers all right so for example if I look at this it says uh name right that is right there so I can change that dogs generated right there okay and that star just represents that this is the column we will be sorting this by okay so our headers are fine we can leave our headers as they are what we do need to change is hard-coded data so you could see I have uh this is two here okay so this is the index okay so for example I can put a trophy here like this right and then hit um we can hit command enter so it serves this page correctly and refresh right and then you will see that it says one trophy next to me okay so uh this is where our trophy or index will go okay so this is going to be the position this over here will be the this over here will be the username right it says that right there username then this guy underneath it will be the number of dogs generated okay so dogs generated will go here and then this will be the number of logins okay at the bottom or the last one and then I’m just hard coding all of these other rows so I can actually just remove all of these other rows okay because all we need to do at this point is we need to write a for Loop that just goes ahead and makes all of these rows for us okay so for example if I go here we’re going to repeat this process right we’re going to repeat this process the process of creating a new row adding the data in there creating a new row adding the data in there creating a new row adding the data in there so whenever you have things that are repeating like this I like to think to myself all right maybe we can use a for Loop here okay so I’m going to remove all of these rows here let’s go ahead and refresh this uh page okay and I’m just going to go like that just go to the home page directly and we should only see one person here right now okay number of logins name blah blah blah okay we should only see cause you right now but this is still because the Quasi is hard-coded now we have access to the user so I can actually write a for Loop here I can say four user in DB users okay I can go ahead and list out each of the users right so for user and users here where it says username I can go ahead and say user username and since this is going to be python code I want to make sure I put that oh sorry username does not go there username goes underneath it okay username goes there so we’ll just we’ll just say zero here and then where it says 12 number of docs generated we’re going to go ahead and say user dogs generated just like that close it and then number of logins will be user and then we can say logins just like that okay and so let’s go ahead and make sure that we wrap this entire row inside of a loop so let’s go ahead and end the loop now so I’ll say end four and we’ll put a percent sign and don’t forget to put a percent sign at the top here as well so this should have a percent at the start percent at the end percent at the store percent at the end let’s hit command enter and let’s see if this is going to show all of our users let’s refresh okay so we’re it’s saying DB is not defined that’s fine because it’s we just call it users so let’s just say users here okay because that’s how we pass the database if you look in our main.py uh wherever we’re passing users which is where we’re passing users uh we’re actually not passing users anywhere interesting so let’s go ahead and pass all of the users so we’re going to say users and I’m going to say DB users put a comma here and then we can pass in users here as well so I can say users and I can say DB users just like this okay let’s run this app right we just need it on two of our views let’s go ahead and refresh now we won’t have that what what’s there this time okay let’s see invalid syntax okay I probably forgot to just put a comma that’s probably it comma yep let’s put a comma here okay let’s refresh and there we go right we’re getting all of the users with their number of dogs generated right and um yeah so there we go number of dogs generated is 10. and name Michael we have Angela Angela’s 10 dog generated now we need to make it so uh now we need to make it so that when we have 10 dogs generated Angela should be number one so we actually need to sort this okay so we’re going to go ahead and create this function there’s going to be a bunch of stuff that you’ve probably not seen before in this one so just kind of hang tight and you know once you get used to seeing stuff like this you’ll understand okay you’ll understand the pattern of this okay I don’t want you to freak out over this so we’re going to create this function called get leaderboard okay it’s going to take in users and what this will do okay is um and I’ll show you an example okay so this is going to be example of how sorted works I’ll just put this example here okay and how it works is you call this function sorted and let’s say we give it a list of companies and lists of companies is a list of really dictionaries each dictionary representing a company and if I tell it uh hey go ahead and sort it by its Revenue so let’s say the dictionary has you know uh it’s a list of companies there’s something that has revenues let’s just say Revenue here okay so if I make a data structure here we have companies let’s just say and um we have company one company two right so company one is basically you know has a revenue of hundred dollars and the name of this company is let’s say Facebook FB and then we have another company who has a revenue of say two hundred dollars and then the name of this company is Google okay so if I ran this line of code what that would do is it would take this list here right and it would sort it based on Revenue so the best highest revenue will come first because we’re saying reverse is true so it’ll give it to you in a descending order which means it’ll put highest revenue first okay and we’re telling it to filter by this key called Revenue right there okay so filter by that key instead of filtering by the name because you could also sort it alphabetically if you did it by the name so we’ll do it by Revenue so it sorts it numerically okay based on the revenue number and yeah and they’ll return to us the companies okay and we could we could even play around with this in the uh let’s open up this Shell let’s go ahead and clear everything okay I’ll go here and we’ll say python I’ll zoom in a bit so you can see better so let’s go ahead and write companies right and then I’m going to go ahead and say sorted and we’ll do sorted right let’s run this and you can see it says hey this is what the revenue is and it sorts so if I had a hundred or a thousand or ten thousand or one million companies it would do it in a matter of a second and it’ll sort them all okay so it’s a very powerful thing we could do all right so there we go that’s the power of sorted that’s how we use it so since we already have access to all of our users what we can say is we can say leaderboard okay and we can call sorted we can give it a new list so let’s say we’re going to create a list of users okay and these users are basically we’re going to create a new list okay let me show you this in the rebel first because this part is slightly confusing so let me go here and um let me show it here first okay so let me delete all of this okay now I’m going to type in um from replit import DB now let’s go ahead and do DB users okay now what I want to do is sort the users by docs generated so imagine if I did that same thing so let’s say I said um where was that line of code that we had right so it is sorted command so I’m going to say sorted and for the list I’ll give it DB users and for the key I’m going to give it Lambda and I’m going to say user is user sorted by dogs underscore generated and then reverse is equal to true and I’ll run this and boom there we go you can see that it just sorted that based on Doc generated so I have the highest dogs generated showing up and then I have the lower docs generator showing up after so that’s really it looks like that essentially works so let’s go ahead and basically write that line of code here okay so I can actually go I can even go here and just copy what we wrote Because that’s effectively what we’re going to need okay and uh that’s what our function leaderboard is going to do I’m just going to do we can set a return to sorted leaderboard okay that’s it and we can remove everything else so now this function is a nice little one-liner which it goes and grabs all the users sorts them by docs generated and the highest ones are at the top okay so now that we’re done with that let’s see if this leaderboard thing actually works and if it does you know we can actually just send these users to our back end and our backend will be able to deal with it okay so let’s go ahead and try that so now instead of giving my let’s say my home page instead of giving it just regular users will give it sorted users so I’ll go ahead and say I’ll call the function get leaderboard and I’ll give it users like this okay and I’ll take this copy this and I’ll do the same thing for when you hit the get dog route okay and let’s run this okay and let’s refresh our page and let’s see if it actually sorts it the way that we want and there we go we’re seeing Angela we’re seeing dog generated now let’s see if we can try to compete with Angela so let’s become Kevin I’ll log out we’ll go ahead and log in as Kevin let’s hit get random dog let’s again random dog and you could see that we’re starting to actually compete with Angela right I’ll hit it four times here and I’ll hit it three more times okay let’s hit it again and let’s hit it and there we go Kevin is already in the lead let’s run it again there we go right Kevin is now in the lead okay when somebody has two of the same numbers then they’ll just sort it randomly okay it’ll just pick flip a coin between the two so there we go Kevin is now number one but there’s only one thing that is missing right now okay there’s only one thing that is missing right now and that is this place okay so how come for the place we have uh zeros for everything why is that we need to be able to get a number so I want to have first one here and I want to have two here and three here and four here Etc so here’s a trick that we can do I can Loop through something so for example let’s say let me show you here first okay so let me show you here first let’s go to Shell and DB users right and I can let me write a regular Loop so for user in DB users and I can say print user okay so that’s great that it’s getting me the user but what if I also wanted the index well remember I showed you this trick a lot a while earlier and it’s called enumerate so I could actually do for user in enumerate DB users and now when I call this I can go ahead and say let’s actually do this uh let’s run this again so I just did a control C to kill it and start it again so let’s go ahead and say index comma user and enumerate user and now I’ll go ahead and say print the index and the user and run it and now you can see I’m getting the index here with the 0 1 2 3 4 5 6 7 8 9 10 11 right and then I’m getting the users on this right hand side so I’m getting the index and the use for index and the user index of the user so enumerate allows me to Loop through and get the position of each of the indices so then in that case that’s going to be a very easy thing because I can actually just call enumerate here enumerate and wrap this leaderboard and enumerate and do the same thing here enumerate right wrap this in that and on our front end I can say for user for for index comma user and users I can go ahead and say index plus one because we don’t want to start it from zero we want to start from one so let’s go ahead and hit command enter and let’s refresh and let’s see if it gives us what we’re looking for there we go there we go so now you could see Kevin is position number one now for Kevin we want to give him a trophy but everybody else we don’t want to give them a trophy okay so we could do this in a few ways we could write an if else statement right inside of here or we can create a template filter I kind of like creating filter sometimes okay so we could say we could create a new filter so let’s go ahead and do app.template filter and I’m going to say this function will be um trophy so trophy for first place okay and what this function is going to do is it’s going to take in a number okay and uh basically we’re gonna say return and I’m going to put a trophy and the number one as well if number or it’s actually going to be taking in an index okay index so put one if index equals equals zero okay and let me think if this is going to mess anything up this actually might mess mess it up hmm this might mess it up but let’s give it a try first so let’s go ahead one if index is zero else index plus one okay so we could we could do that all right now we’ll have access to Trophy first place pretty much at any point um no actually I think this should work I think this should be fine so let’s go ahead back here and let’s go ahead and say index and go ahead trophy first place so pass an index as the argument for this function trophy first place and that will either print out the position or give you the trophy okay so let’s go ahead and run this and let’s see if that works there we go that filter worked real nice okay so we could actually even call it um trophy or position right it either gives you the trophy or the position so trophy or position okay we could call it that and fix the naming here let’s go ahead here and refresh and now you can see that Kevin is in the lead okay and if I I can obviously log out as Kevin I can go ahead back in as Angela and I can keep hitting get random dog multiple times okay let’s go ahead if you click too fast you’ll break it okay so don’t click so fast all right let’s see what the error is right now let’s restart the server okay so I’ll hit command enter here and let’s go right there and let’s go to the home page okay it looks like it’s coming back up perfect there we go okay Angela is at 11. we can get random dog get random dog get random dog and Angela can soon take over and as soon as she takes over you’ll see that she gets the trophy okay so let’s go take over boom she took over she got the trophy she moved up in the spot right dog generated became 17 for her and so you could see that this is working correctly okay the next thing I want to do is obviously I want to remove any duplicates that I have because once I remove them then in the future anytime anybody’s using our database it will not allow you to create duplicates so let’s just go ahead and remove if we want to get our app in like perfect condition let’s just go ahead and um we could remove [Music] okay let’s go ahead and get rid of that we can essentially remove our database okay so our our users at least so I can just say DB users and let’s set it let’s actually do it from here I think it’s safe it’s a better choice so let’s go ahead and make sure you do from replit import DB and then we’re just going to go ahead and say users is equal to a empty list okay so as soon as you do that if I go here and I refresh this app would either break right because there’s there’s uh now we don’t have any users at all so let’s go ahead and see DB users we have no users at all okay so that’s gonna be a problem so this is basically the very first time somebody’s using your app and let’s see where we got the error from and it’s saying total uh total dogs generated plus equals one and it’s trying to increment the dogs on somebody okay so let’s go ahead to enroute to the home page and now I’m on the home page okay and because we have sessions um on right that’s why it’s saying Angela but in terms of users it has no existing users data that’s why you’re not seeing any user specific data this is coming from sessions okay so this here is completely empty well now we can actually start creating users and we won’t be able to create any duplicates so now if I create Angela hit enter hit get random dog you can see Angela is now in the database at the bottom right you can see her numbers incrementing every single time I generate a dog for Angela let’s log out let’s log in as Angela again let’s just make sure her number of login increases and you can see it does okay let’s log out let’s go ahead and come in as Kevin and you should see that number of logins should be one let’s hit get random dog get random dog let’s see if Kevin can overtake Angela let’s run it a few more times all right and there we go Okay Kevin has officially overtaken Angela we have a total of 66 dogs created so if you want you could you could have reset this as well if you wanted to okay but there you go uh you have 66 dogs generated in this app you have two users that everybody could see here in the leaderboards you can log in and log out you can generate a random dog use an API request you’re sorting this table by the leaderboard and so you learn how to sort so there’s so much here right there’s sessions there’s databases there’s server side routing there is front end stuff that you’re dealing with here I mean you just accomplished so darn much and if you got to this part honestly I’m really really proud of you even if you didn’t and you just made progress and you got closer to it hell I’m still super proud of you I mean this is absolutely amazing and incredible that you made it this far and again if you ever get stuck you know just go ahead and hit that solution file and the solution file will have everything for you because if you’re watching it as a clip the solution file might might be in the comments below but if you’re watching it as part of that python course then just you know you you still have the solution file inside of your you know python course right over here you can just always go to this uh you know read me just hit markdown and here you have all of the apps and everything that you’re going to be building and you can see okay for random dog generator here’s the project here’s the live demo and here’s a solution and if you want access to this course again it’s completely free the link is in the description below you can get access to this mega mega mega Python course that has everything there for you plus some awesome awesome projects so you can always pull the solution file from there anyways super proud of you great work with that said let’s keep on rocking and rolling that was crazy we built so many projects together we did so much coding together you learn so freaking much you’re less than one percent of the people if you’ve made it this far because not a lot of people will ever make it this far so thank you so much for getting all the way to here I really appreciate your beautiful face this is not the end my friend this is just the beginning okay and I hope that I could play a tiny little part in your journey of becoming a developer I know you’re gonna go on to become a phenomenal developer and that just makes me proud and that makes me so happy just knowing where you’re gonna be at the end of the day with that said I love your beautiful face I hope you enjoyed this journey together it was kind of like a Harry Potter mixed with Lord of the Rings mixed with some Wizardry because man that was freaking awesome with that said I love your beautiful face this is Kazi and as always I’ll see you in the next video [Music]
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
👨💻 FORK THIS REPL 👉 http://join.replit.com/cp-python
what code editor did you use
Too not so bad your the real dev😂😂other Devs learn python and build chat gtp 90.8
Man , I also have the same wallpaper . when student and the master have the same taste in art <3 Love You, Learned alllllllllllot from you .
Assalamu alaikum
Qazi, thanks for this tutorial. I found it to be extremely beneficial
2:57:23
Qazi love you so much
specially as a muslim as a brother
top G
nice explanation
which software do you use to stay focused
This is great man, thank you Qazi! Ramadan Mubarak!
I cannot find the markdown at the beginning of the Counter App creation
I cannot find the Markdown section referred to at the beginning of the Counter App
Definitely the best python tut online.
I have zero knowledge of programming. For a beginner like me who is interested in learning programming, this course just makes Python so much easier. A million thumbs up!
I couldn't find the markdown page in the counter app flask part.
can somebody help
Unfortunately, the replit program warns me to enter:
check your browser and network configuration
And whatever I do, it cannot be corrected. what should I do?
where is the youtube project link?
Can I write the codes you teach in pycharm also??
which screen marking tool you used?
bro took 3 business days to say "My Favourite"
Very nice course(bahut acha)
Which emoji app do you use?
sir you are really hidden gem.
Honest opinion is that this course is a WASTE OF YOUR TIME. If you want to learn programming, buy 100 days od code course for like $10.
4:20:00
Can someone explain why this wouldn't be easier?:
def find_max(number: list):
print(f"The biggest number is {max(number)}")
find_max([1, 2, 3, 65, 7])
It's a fantastic course. Thanks a lot for your effort but Markdown is not coming. Any suggestions? @CleverProgrammer
you guys going to have to figure out more practical ways to teach this stuff, building little tip calculators and calculators is soooooo fucking boring
continue: 1:06:28
which is better using an f string or converting it into a str? 27:01
nvm both is good XD
6:05:35
HOly cow! I am not ready for that yet but I appreciate the work!
i fell in love with your teaching 😭😭
5:11:39 beb
❤❤💖💖
This video is 11:34. Why do you say 12 hours?
hi, I'm a beginner in coding. I have a problem with the counter app. the increment and decrement goes by 2. if I open the app in a new window , it becomes normal and the increment/decrement goes by 1. Can you please help??
7:25:13 how to get this Css file?(
where we find notes
where we find nots
Yoo
please i am having issues with the markdown its not bringing out anything please what do i do
You should give options to students: Replit or IDLE. I don't use Replit What if the Internet is down?!
This was so helpfull ❤the other courses are paid but this free and helped me alot now iam really into programing❤❤
Do anyone have notes for this course then please share
hi i am Munaf Kazi i went to your website but your website is not working
There is no fork button, it's not showing any fork button like it did for other's. Why?
changing lives out here man, hope the best for u, i feel this video wouldve gotten way more views but brocodes video on this has more views and likes so people click on that, i find both great but personally urs is better