I Learned Python By Building These Projects - Tutorial for Beginners

I Learned Python By Building These Projects - Tutorial for Beginners

Show Video

if you're a beginner in Python and you're looking for some simple projects to help you practice your skills then you've come to the right place in this video I'll be walking you through three unique python projects that will show you how to take some of the theory that you've learned and apply it into a real world application not only will I walk you through everything line by line but I'll explain my thought process and how I've designed the software so you understand how you could do this on your own and I'll even provide some mini challenges to you so you can try to code something by yourself before looking at my solution this is great for those of you that just want to spend maybe an hour or two writing out some python code you don't want it to be too intense but you want to learn how you can actually do something by yourself and how to take that theory and apply that into a real world project now these three projects are ones that I actually built when I was learning python so I know they're great for beginners and those of you that have learned things like if statements for Loops variables but are struggling to get to that next step so with that said we'll get into the three projects but let me tell you what they are there'll be time stamps Down Below in case you want to skip forward to a different project so the first project we're going to work on is a trivia game this will randomly select a certain number of questions ask the user that and then keep track of their score the next project we'll build is a random password generator where we'll generate a password based on the user's preferences like if it should have uppercase characters special characters numbers and the length of the password lastly we will then go to build a to-do list manager so something that will save a to-do list for the user on their computer permanently we can then Mark items completed or uncompleted you'll see how it works but this is interesting to see how to work with files in Python so those are the three projects and you're going to learn a lot in each but if you want some more challenges that are a little bit shorter and you want to really practice your python skills further then consider checking out my free newsletter this newsletter sends out tons of emails that contain all kinds of practice questions particularly in python as well as sample project ideas tips and advice from myself and to make it even sweeter for you I've prepared entire guide on all of the ways that you can make money from coding and I'll give that to you for free if you sign up for the newsletter simply sign up from the link below I'll email it to you and then you'll be subscribed and you'll get all of those free challenges again everything's completely free you don't need to pay for anything just sign up from the link below with that said let's get into project number one so let's begin here with the first project which is a trivia game now for this game I want to have a set of questions and obviously for all of those questions we need some type of answer the IDE aidea is that we want to randomly pick a bunch of these questions so it's not always the same every time you run the program we'll give those to the user and then we'll see how many of them they get correct so before I even start coding any of this out the first thing I like to do is come up with a little bit of a plan so I'd ask you before you even listen to what I'm about to say think about the different components that we'd have to this application now it's relatively simple there'll just be maybe three or four what are the steps we need to take you can pause the video and try to figure out figure it out sorry on your own okay so for me when I first start thinking about this I think that we need a list of questions right first thing we need to do is come up with some kind of questions and then we need to store the answers to those questions and have them Associated now after we have the questions we need to randomly pick some because we don't want it to be always the same so randomly pick questions we then need to ask those to the user so ask the questions we need to see if they are correct we need to keep track of the score and then tell the user their score okay so I'm just going quickly here because this is relatively simple but the idea is that I've just at least thought about what I'm going to do here I've written a little bit of a plan right so I kind of have some steps on the things that I need to do and now when I start coding I'm not completely lost and I at least know where to begin I might not know how to solve all of these yet but I've kind of broken it into some smaller sub problems here that are a bit easier for me to tackle so first things first let's get a list of questions now I'm just going to copy these in because I don't want it to take too long for me to type them out and I'll explain why I've gone with this format so what I'm actually using here is something called a dictionary now a dictionary in Python is a key Value Store it allows you to have some kind of key in this case the key is my question and the value is the answer so the reason I'm doing this is so that I can very quickly look up what the answer is to a particular question if you've never seen a dictionary before the way that it works is you can do something like questions and and then in square brackets key is equal to some kind of value and this would then set this key value pair right so in our case all of the keys are the questions and all of the values are the answer so if I want to see what a particular answer is then I simply just reference questions at Key like this and this would give me whatever the answer is so for example if I had this question and I pasted this here in the key and I just looked at it and just printed it out then it would actually give me three because that's the value as associated with this key so hopefully that makes sense but I'm just using the dictionary kind of as a teaching opportunity and because again it allows me to look up with the answer to any of the questions are very quickly without having to use all kinds of messy lists okay so now we have all of our questions and the next thing I need to do is store the answers okay so I've done that so I have the list of questions and I have the answers now I need to randomly pick the questions so in order to randomly pick the questions we're going to need to do some kind of random generation so I'm going to bring in here the random module now you may not have seen this before but this is something that can Generate random numbers for you and just generally do a lot of random operations so I'm going to import that at the top of my program now what I'm going to do is I'm just going to define a function and this function is where most of the code for my game is going to live so that I could run the function again if I wanted to continually Run the game multiple times so I'm going to make a function and I'm going to call this python trivia game and if you've not seen a function before essentially this is a reusable block of code so what I can do is just print something like hello and now that I've defined this function so you define a function using this defa keyword you write the name of the function and then you put a set of parentheses you put a Col in and then anything indented is kind of belongs to this function and it can be reused multiple times the way you use the function is you simply call it so I can just write the name of the function so that's what I'm doing python trivia game and then I call it using a set of parenthesis and then it will just exit ex cute anything that's inside of it so if I go here and I run my code so just go python project 1. piy you can see that I have hello and hello and obviously I should mention in order for this tutorial to work you do need python installed and I imagine that you would have created a file to put this code inside of okay so we have this function we can kind of start running it by calling it down here and now obviously inside of the function we're going to move on to the next step which is going to be to randomly pick the questions so in order for me to randomly pick the questions what I can do is the following well first I'm just going to get a list of what these questions actually are so I'm going to say questions is equal to and we'll actually say questions list is equal to and I'm just going to say a list of and then this is going to be questions. Keys now when you're dealing with a dictionary what you can do if you just want to get all of the keys is you can use dot keys if you wanted to get all of the values you can use values okay so I'm just getting all of my keys now what I'm going to do is I'm going to randomly select a c certain number of these U keys so I'm going to make a variable and I'm going to say total questions is equal to let's say five it's the number of questions that I want to select and while we're at it we'll make another variable called score and we'll just set that to zero because later on we're going to um keep track of the score so now that I know the total number of questions and I have a list of all my questions I can randomly select them so I'm going to make a variable called selected questions and this is going to be equal to random. sample I'll disc this in one second and we're going to pass our question list and the total questions and you can read this if I highlight over it is it chooses K unique random elements from a population sequence all that means is that it can take in something like a list so that's what we've passed and it will just select K values that are unique from that list so that's all that we want we have our list of questions and we want to select five of those so random. sample we just take five of them and then give it to us in a list so before we go any further let's just print out what these selected questions are and just make sure this part of the code is working before we go any further so let's go up here and let's run our code so we're going to say python project 1.

py and you can see that we have 1 2 three four five questions perfect that's exactly what I wanted okay so now that we have these questions we need to ask them so what I'm going to do naturally is I'm going to Loop through these questions using a for Loop a for Loop is something that will repeat a certain number of times in this case we want to Loop over our list so to Loop over our list there's a few different ways that we can do this but what I want to do is I want to have access to the index inside of our list index is like the position of the elements index zero index one index 2 index 3 and I want to have access to what the question actually is so I'm going to use something you may not have seen before which is called a numerate bear with me I'll explain it in one second but I'm going to say for idx standing for index Comm a question in enumerate and I'm going to enumerate the selected questions okay now what this is going to do is it's simply going to Loop through all of the questions and it's going to give me the position or the index of each question and what the question value is so if we have a list like this let's just say you know like one actually let's go a b c then zero is the index for Value a one is the index for Value B and two is the index for Value C so what enumerate will do is it will give us the index as well as the value so if we were enumerating over this list a b c then we would get zero for the index and a for the question then we would get one for the index and then B for the question so since we're doing that over selected questions then we'll get the index and we'll get whatever the questions are that are contained inside of that list so hopefully that makes sense okay so now we want to ask the question so in order to ask the question I want to print out what the number of the question is and then asking so to do that I'm just going to do a print statement and I'm going to use something called an F string now an F string is simply a string that allows you to embed variables inside of it so I can put F and then I can put a string this can be an uppercase F by the way or lowercase f doesn't matter and then inside of here anytime I want to print out the value of a variable I can just put it inside of a set of curly braces so if I want to print out the question number I can do something like idx + one and then dot now what this is going to do is take my index simply add one to it so it means if it's zero it become one if it's one it will become two and then I'm going to put a dot and then after this I'm just going to print out whatever the question is now the reason why I'm adding the plus one is because we're going to start iterating at index zero and I don't want to say question zero I just want to start by saying question one so that's why I'm adding the plus one and then we can say whatever the question is and then we can ask the user for their answer but for now we're just going to start with that and I just want to see if that works so let's scroll up here and let's run our code again and you can see that it now prints out all of the different questions so 1 2 3 4 5 great so we're making good progress we've now printed out all of the questions we're effectively asking them to the user and now what we need to do is ask the user to give us their answer so I'm going to make a variable called user answer and notice for all of my variables here that I'm trying to give them a meaningful name something that makes sense and that even if someone didn't know how to code they'd be able to read so whenever you're writing your program you want to just try to make sure everything reads like English in this case we're defining that we have a python trivia game we're getting the list of questions here's the total number of questions we have here are the ones that are selected even if you didn't know how to code you can still kind of understand what's going on here because you can read the variable names so just a tip and just really get in the habit of doing that early it's going to help you a lot later on in your coding Journey all right so for the user answer we're going to use input input allows the user to start typing something in the terminal and all we're going to do is just say your answer like this with a colon and then I'm going to add a space now the reason I add a space is because I want to add some separation from the colon so when the user starts typing they're not squished with the colon they have a little bit of space right so they can start typing after that because that's how input works after you put this is when the user can start typing on the same line so we just add a space there's a little bit of Separation then what we're going to do is we're just going to convert whatever the user types in to do lowercase so by default whatever the user types will be given to us as a string data type in Python and we'll just convert it to lowercase to make sure that if they accidentally type something in uppercase we don't tell them it's wrong when we compare that to our answers and notice all of our answers are all in lowercase right so we just convert what they type to lowercase we're also going to add this dot strip now what strip is going to do is it's going to remove any of the spaces from the beginning or the end of the string so if the user accidentally typed something like you know space 8 we don't want to tell them that they're incorrect if the answer is actually eight so so we just remove that space so that when we're comparing it to the answer we get the same thing hopefully that makes sense but strip is a good thing to kind of use on the user input all right so that's going to ask the user for their answer and we can quickly test that and just make sure that works so let's go up here and let's run our code and ask us for the answer and we can just type some stuff in here and make sure that's working okay perfect very good so let's close that and let's continue so now that we're able to get the user answer we want to compare that to whatever the answer actually is so I need to get that answer first so I'm going to say the correct underscore answer is equal to and then this is going to be the questions so we have it called questions up here and then we're going to grab the question so we have our question right that's one of the keys in our questions dictionary so we look in questions we use the question as the key and then that will give us whatever the answer is associated with that so now we have questions associated with question okay perfect that'll give us what the correct answer answer is and now we can compare the user's answer to the correct answer so to do that we can use a simple if statement and we can say if the user answer is double equal to this is how you check if something is the same the correct answer and we'll just convert the correct answer to do lowercase here just to again ensure that everything's all in lowercase so we're not comparing cases and then if they got this correct which this statement would tell us they did we can print out correct and we're just going to print a back sln character now this back sln character will simply move us down to the next line in the terminal so we get a bit of spacing between the next question that we ask now as well as telling them that they got it correct we can increment the score so we can say score plus equals 1 so we have score variable and if they got something correct well they got a correct answer so we'll just add one to this that's how you can add one to a variable right just score plus equals one okay perfect now otherwise if they didn't get this correct we need to tell them that they got it wrong so we're going to do a simple print statement we're going to make this an F string and we're going to say wrong and then we will say the correct answer is and then inside a set of braces here we're going to say correct answer so we're just going to tell them what the answer is so they actually know that then I will put a period And I will add a back sln character here again just so that we go down to the next line so we have some spacing between the next question okay I know we wrote a good amount of code there just to quickly recap we get the user's answer we then get whatever the correct answer is we then compare these so we check are these things the same if they are the same then that means they got it correct so we say correct and we add one to the score otherwise we tell them that they got it wrong and then we tell them what the correct answer was so that they know so let's try this code before we go any further we're actually almost finished by the way and let's start answering this so how do you start a for Loop in Python four correct what is the keyword to define a function in Python let's just get it wrong let's go F and it says wrong the correct answer is deaf what data type is used to store true or false values bull wrong the answer is Boolean okay so maybe we want to change that one but that's fine and what is 10 over three three okay perfect and let's do this one input there we go all right so that kind of completes the trivia game however we want to if we go here tell the user what their score was and keep track of the score so that's kind of The Next Step here we need to tell them what the score actually was so let's go down here here and let's start by just printing something out and telling them what their score was so we're going to make an F string and we're going to say game over and we'll say your final score is and then we can say score over the total questions so we're just telling them you know like four out of five five out of five whatever it is we just tell them how many questions they actually had okay perfect so that's fine and then if I come here let's give this a quick test and let's just answer these quickly so what is the keyword to find a function that's defa to import a module is import what is the result three this is eight I believe how do you start a four Li let's get one wrong and it says your final score is 4 over five fantastic so there you go that's pretty much it for this simple trivia game now again kind of the purpose of this was just to show you the thought process how I created a little bit of a plan I went through the plan step by step obviously we skipped a few few steps but generally we kind of followed this so we knew exactly where to start and it was a lot easier for us to create this program because I knew kind of the steps that I wanted to follow and I didn't have to stop constantly and keep thinking about what to do next so generally what to take away from here is when you are building out some kind of project always start by just asking yourself you know what do I actually need to do how can I break this down into some smaller steps that are easier for me to solve and then solve them one by one and kind of combine them together to ultimately come up with the end code I'm sure some of this code might be a little bit confusing especially if you are a complete beginner but I wanted to not really focus on the theory and just get directly into the project so you can see exactly how to write this if you want to analyze the code for yourself I will leave it linked in the description but now we're going to move over to the next project where we're going to build a random password generator all right so we're moving to project number two which is going to be our random password generator now same thing as before I want to begin by coming up with a basic plan now my idea here is that I want to just generate a random password but I want this to be based on the user's preferences now we can come up with whatever preferences we want but usually speaking when you have a password there's a few kind of sets of criteria that you need to fulfill so for example it may need to be a certain length so we want to ask the user what length the password should be it may have to contain special characters or uppercase characters or things like numbers so for me right away the first thing I want to start writing down is what I need from the user before I can continue like the user input that I want to collect CCT so the first step in this project is collect user preferences and those preferences are going to be the following the length of the password should contain uppercase okay should contain special or special characters and should contain numbers or digits okay so this is just what I came up with you can change this to anything that you want and I would actually encourage you to do that to mess around with it a little bit you can see length contain uppercase special and digits that's the first thing we need to do is we need to collect that from the user now after this it becomes a little bit less clear what we need to do but I'm going to walk you through my thought process so if we want to randomly generate a password we're going to need to randomly select characters out of the available characters that we could pick what I mean by available characters is that if the user doesn't want any uppercase special or digits then we just have the lowercase characters to pick from right and we need to randomly select them and add them into some type of string but if they selected all of these or whatever kind of combination they have that creates a pool of characters that we can then pick from so what I'm going to do next I'm going to say create all available characters or get all available characters so we kind of know which ones we're able to use once we know all of the characters that we can use then we can simply pick them one by one until we reach the desired length so we're now we're going to say randomly pick characters up to the length however the only thing that we're not really accounting for is that we want to make sure that when we do this random selection that if the user specified to include digit special or uppercase that we have at least one uppercase one special and one digit if we just randomly pick out of all of the characters that we have available to us it's possible that we don't get one of those characters so we need to add kind of a caveat here and say ensure we have at least one of each character type right okay so whatever they select and now that also tells me well what if the user ask for a password of length one or length two but they say that we want uppercase special in digits well that's not going to be possible because if they only want two characters then I can't you know satisfy that constraint of having an uppercase character a special character and a digit so I'm going to add another thing here and I'm going to say ensure length is valid so we're just going to make sure the length of the password matches whatever the criteria is that they've specified so we can actually generate what they're asking for okay now there's some other things that we probably need to do here but generally speaking this is a decent enough plan where I've now thought through what I need to do and I can start coding this out okay so hopefully that's clear what I'm going to do here is I'm going to import two modules to begin I'm going to import random and I'm going to import string now you may not have seen the string module before but this is something that will give us access to a list of all of the characters that are lowercase uppercase digit or special characters so it just saves us a little bit of time from having to say manually write all of them out ourself okay next we're going to create a function like we did before and notice by naming generate password right I'm just making it super clear what this function actually does now the first thing we had on our list is we need to collect the user preferences like the length and then if it should contain uppercase special or digits so let's start writing that out we're going to say the length is equal to and we're going to have an input and for the input we'll say enter the desired password length like that and then what I'm going to do is I'm just going to do do strip just to remove any of the um spaces now after that I'm going to have a variable and I'm going to call this include and this is going to be uppercase and we're going to have an input and this is going to say include uppercase letters question mark and I'm just going to put inside a set of parentheses yes or no so we're telling the user what we expect them to type in then just like before I'm going to strip this and I'm going to convert it to lower so that it's a lowercase so when we compare what they typed in we don't check for the case now we're just going to copy this and I'm going to paste it two times because we're going to have the same thing again except now rather than uppercase we're going to have special and we're going to have digits okay so we're going to say include special and I'm going to change this to characters and then for digits we're going to say include digits and then yes or no okay so now we've collected the input from the user and what I'm going to do is I'm just going to first convert this length to a number so by default when you type something in as input the type of that variable is going to be a string and I want my length to be a number because I need to have a numeric value so I can use it in Python and for example Loop through it if it's a string it's just not in the correct type so I'm just going to Simply put an INT function around this input. strip now what this does is it collects the user input strips any of the spaces and then converts it to an INT now this is going to assume that we type in a valid integer if you don't type in a valid integer this will actually give you an error but for our program now we're going to keep it simple and just assume that they're going to give us a valid number if they don't the program is going to crash so for now let's call the function and test this I don't like to write too much code without testing so let's do that let's go here and then let's go Python and then project 2. piy and let's just go through these fields so four yes no no and very good okay now we're able to enter all of these values sweet so now that we've done that the next thing that I want to do is I just want to ensure the length is valid here now I'm just going to make this really simple and I'm going to ensure that they give us a length that's at minimum four because if they give us a length of four then we know no matter what they've asked us to do here we can include a character of every single type so I'm going to say if the length that they've given us is less than four then I'm going to print the password length must be at least four characters and I'm simply going to return from the function now when you return you're just going to exit the function and go back to the line that called it which means anything that I put down here will not run so if this is true we will simply return which means anything beneath this line won't happen inside of the function so just exiting out early okay great so now that we've done that the next step is to get all of the available characters so how do we do that well I'm going to show you you probably haven't seen this before I'm going to say lower is equal to string Dot and then this is going to be the asky lowercase now what asky lowercase will do is give me all of the lowercase letters that's all it does it's just going to give me a string that contains all of these lowercase letters and if we want to print it out just to kind of test our sanity here we can do that so let's just go here and run the code and oops it says inval literal for INT with basee 10 that's what I was talking about so let's actually I need to fix this because now it's going to tell us that the passwor must be minimum four okay five and then go here and you can see that it gives us all of the lowercase letters sorry about that mess there guys just because of the way that I was running the code but the point is lowercase or string. asky lowercase

gives us all the lowercase letters which is what we want so next we're going to get uppercase and this is going to be string. asky uppercase however we're only going to get this if the include uppercase otherwise we're going to have an empty string so pretty much what I'll say and actually I need to adjust this I'm going to say if include uppercase is equal to yes so if this value is equal to yes then we will get all of these otherwise we will just have an empty string now you may not have seen this python code before that's intentional I'm trying to make it a little bit more complex to hopefully teach you something but this is known as an inline if statement or a Turner statement anyways the name's not important the point is the thing on the left hand side will be the value in this variable if this condition is true otherwise it will be the thing after the else so it'll either be an empty string or it will be all of the uppercase characters okay hopefully that's clear now let's copy that and we're going to do the same thing now but for special so I'm going to say special is equal to string Dot and then I believe that this is going to be punctuation if include special is yes okay and then we can copy it one more time and we're going to do digits okay so we're going to say digits is equal to string Dot and did you guess it yes you did it's digits if include digits is equal to yes so now that we've got all of the characters that we should be using what we're going to do is combine them all into one large string so I'm going to say allore characters is equal to and then I'm just going to add all of these together so I'm going to say lowercase plus uppercase plus special plus digit this is known as a string concatenation and it just squishes the strings together so for example if you add hello and you added world to this right then what you would get is hello world right it would just combine them together okay so hopefully that makes sense but that's what we're doing here and the reason why this will work is because if we're not including any of these then it will just be an empty string and when you add an empty string to another string it just doesn't do anything it just gives you the original string so if we are including these then we will combine them together if we're not then we'll just have an empty string so when we do this addition effectively nothing will happen for you know the special digits uppercase whichever one we don't actually have so let's do another sanity check here and let's just print the all characters and let's make sure this works based on the parameters that we give it so I'm going to run this I'm going to go length 10 let's include uppercase let's not include special and let's include digits and you can see that we get everything except the special characters let's run it again let's go 10 let's not include uppercase but let's include special and digits and now you can see we get all of the special characters as well as the digits and of course the lowercase letters which we always have okay so now we've selected essentially the pool of characters that we're able to use and the next thing we need to do is randomly pick characters up to the length and we also need to ensure that we have at least one of each character type now we can do this in different orders but what I'm going to do is I'm first just going to select one character from each category that we need to have at at least one of so in order to ensure that we have at least one special character one digit or one uppercase letter we're just going to start by selecting one of those characters if that's included and then we'll have those be a part of our finished string and whatever remaining characters we have less to pick left to pick sorry we'll randomly pick them so the idea is if you want to have an uppercase character we'll start by randomly picking one uppercase character then we'll randomly pick for example a special character if you want to have that we'll randomly pick a digit if you want to have that and that will cover three characters of whatever the password length will be so then we will just pick whatever the remaining number of characters are so let's say the length is 10 and we've already picked three we'll then just pick seven random characters from all characters and put those in the string this will just ensure that we always have at least one of whatever the user asked for okay so in order to do that we're going to have this list called required characters okay and what I'm going to do is I'm just going to add into this list all of the characters that we need to have so they'll be at Max three so I'm going to say if include and let's go with uppercase here so if include uppercase and we're going to say this is equal to yes then what I'm going to do is say required characters and we're going to say append and we're going to randomly select a character from The uppercase list or from the uppercase characters so to do that we're going to say uppercase and we need to now randomly pick one of the characters inside of here now there's different ways to do this for example we can use this random. choice so I can say random. Choice and then uppercase and

this will just randomly pick one character from uppercase and in fact that's exactly what we're going to do okay now we're going to do the same thing we're going to say if include special is equal to yes then we're going to say required characters do append and we're going to append random. choice on special and then you guessed it we're going to do the same thing for digits so if include digits is equal to yes then we're going to say required characters do append random. Choice and then this will be the digits so again the idea is if we're including these so special digits uppercase then we're just going to randomly pick one character from just that list of uppercase special or digits we're going to add that into required characters and this will give those characters that we need to have no matter what and then we'll select the rest that we're going to have randomly okay so in order to do this we're now going to say that the remaining length or the remaining number of characters that we need to generate are going to be equal to the length minus the Len of our required characters so if we selected three required characters then the remaining number of characters we need to pick is whatever the length of the password will be minus however many we've already picked okay then we're going to say our current password is equal to the required characters and we're going to start adding more characters into this password all I'm doing is just kind of renaming this now so I'm saying okay we've picked these required characters these will be in our password that we're going to randomly generate and now we're just going to start picking new random characters to add inside of this password notice that I'm doing this as a list the reason why I'm using a list right now is because I can just keep a ending values inside of it very easily and then later I can take this list and I can convert it into a string I can Shuffle around the values inside of it it's just a little bit more flexible if I'm going to keep randomly picking a bunch of new items there's also a speed concern here it's faster to add items into a list than it is to keep creating a new string so it's kind of an efficiency concern as well but I don't want to get into that too much in this video okay so what I'm going to do now is I'm going to Loop through the remaining characters the remaining length that I have and just pick new characters so I'm going to say four I'm going to use underscore which I'm going to explain in one second in range and then remaining length now what this is going to do is it's going to have this for Loop running however many characters left there are to pick so if the remaining length is seven then this for Loop is going to run seven times now the underscore is simply a placeholder variable when you don't want it to define something so typically I would do something like four idx in range remaining length and then idx would be equal to 0 1 2 3 4 and it would count up to whatever this value is but not include it but I'm not going to use this variable I don't care what the index actually is I just want this for Loop to run seven times or remaining length times so I can just use an underscore so that I don't have this variable defined for no reason and it acts like a placeholder that's really all it is it's known as an anonymous variable so now that I have that here what I'm going to do is I'm going to randomly pick a value from my characters so I'm going to say my character is equal to random. choice and I'm going to pick something from my all characters so all of the characters that are valid to choose from I'm just going to pick one and then I'm simply going to add that to my password so I'm going to say password. append the character okay so

that now is going to give me a character so what I'm going to do is I'm going to print the password so that we can see what this looks like before we convert it into a string okay so let me run the code quickly I'm going to say let's go 10 let's include everything okay and you can see that this is the password that was generated now notice that we have an uppercase letter we have a special and we have a digit and it comes in this order the reason why is because we started by creating or selecting those three required characters then we just picked random characters so we got whatever was randomly selected so that's fine but I want to now Shuffle this up up and make it even more random because I don't want the three characters that I selected to always be at the beginning of the list or the beginning of the password so what I'm going to do is have another randomization where I'm going to say random. shuffle my password now what random. Shuffle is going to do is it's going to look at this list and just going to randomly mix up all of the items that are inside of it that's it just going to mix them all up okay so now that it's done that what I can do again is I can print out the password keep in mind it's still a list it's not yet a string so we'll convert it to that in 1 second and let's just have a look so let's make this for example length four let's go yes yes and yes and there you go we get our slashback tick 4D okay and if we ran this again and did the same thing we would get a different result so now I just want to convert this to a string so in order to convert it to a string I can say my string password is equal to and the easiest way to do this is to do a empty string do Jo and then our password okay now the way that this works is joyin will take a list in this case we have a list of different characters and it will combine all of the elements in that list together using whatever this string is as a separator now in this case we have an empty string which just means combine all of the values in a list to a string but if we did something like a pipe then it would combine all of them with a pipe in between every single character or comma in between every single character it's a very useful method to know in Python so what we'll do is we'll simply return the string password and then what we're going to do down here is we're going to say password is equal to generate password and we're just going to print out what the randomly generated password was okay so that's the code Let's test this and then we can kind of go through it and just review what we've written so we're going to enter the length let's make it like 25 let's just go yes yes or I guess that's not going to include now so we'll say Yes And Then There we go we get our password let's test it again let's go maybe 10 let's go yes no no and there we go we get our password okay so you can mess around with this and you can keep getting a bunch of random passwords but that is this project to quickly recap right we had to start by collecting all of the user values or all of the user preferences make sure the length is valid and then we get all of the characters that we can possibly use then we generate all of the required characters that we need to have in this password we then get the remaining length after we've generated those and then we start generating the password by just randomly selecting characters from all of the valid ones that we can pick then we Shuffle up whatever we've selected we convert it to a string and we return it back to the user when we return something from a function it simply gives it back to wherever the function was called so string password now gets returned to this and gets stored in this password variable and then we can print out the password okay so hopefully you enjoyed that was Project two let's move on to the next one so we are moving on to project number three which is going to be a to-do list management app now my idea behind this is I want to store to-do list items for a user um like on the system like in a permanent area where if the user runs the application again they can still see those same items so yes you probably wouldn't use this in the real world but I'm just showing you something so you can understand how to work with files and various different data types and kind of load information save information so you can use it when the program has finished running anyways the idea is we're going to have some kind of to-do list items like to-do list item one and then we can mark it complete or incomplete then we want to be able to save it to the dis and then load it from the disk so let's start writing some of these features so we have a little bit of a plan now first things first we're going to have to load existing data right like when we run the program load existing items okay then we're going to have a few different operations we should be able to perform now the first operation might be creating a new item then maybe we want the ability to list items to see which ones already exist and we probably want the ability to maybe Mark item as complete okay and then lastly we probably need to be able to save items right so I'm not coming up with a full detailed plan but I'm at least thinking of some of the different operations that I'm going to need to perform so I can start kind of stubbing out the program and just understanding what it is that I'm about to do okay so we have create list Mark and save now what I want to do is I just want to write some of the functions that we'll have in order to kind of perform these operations so the way that I start a larger program like this is I begin by just writing out the core operations I don't Implement them so I don't write all of the code and make it work but I just write kind of the name of the different functions and I start just mapping the program out a little bit so visually I know what's going on so like we said we need to load existing items so I'm going to make a function called load t tasks okay now I'm just going to say pass when I say pass this just means this function is empty right now and I'll come back to it later now next we're going to have save tasks okay we'll say pass and then we're going to have a few more right so we'll have view tasks okay we're going to have create task so create underscore task and then we're going to have Define Mark task complete okay perfect so those are kind of the five main things that I'm going to need to be able to do and then I'm going to have one function and I'm going to call this main the main function is typically the entry point to your code like the first thing that you start running and in this main function what I'm going to do is I'm just going to kind of connect some of these functions together so I'm going to have the user type in maybe 1 two 3 four five depending on the type of operation they want to perform and then I'll call these different functions you'll see what I mean but hopefully this is making a little bit of sense already in terms of the different things that we're going to need to do and we'll talk about saving into a file and all of that kind of stuff so for now inside of my main function I'm going to start by just loading my tasks so even though I don't have this written yet I'm going to say tasks is equal to load tasks so you can see that I'm kind of mapping out the program on what I want to happen and then later on I'll go and I'll Implement all of these functions so we're going to load the task that's the first thing that we need to do and then I'm going to set up a loop and this Loop I'm going to continually ask the user what I want them to do so I'm going to say while true we're going to use a while loop this means we're just going to keep running until we eventually decide to break out of the loop and then here I'm going to have a print statement and I'm just going to do a back sln and I'm going to say to-do list manager so I'm just kind of telling them what the application is then I'm going to print out the different operations that they'd be able to perform so I'm going to say one is view tasks going to say print two is maybe add task okay print and then this is going to be three this will be complete task okay and then print let's do this for exit Okay so these are the main things that they'll be able to do then I'm going to ask the user what they want to do so I'm going to say choice is equal to input and I'm going to say enter your choice and then I'm just going to strip this okay so we're going to go dot strip just to remove any of the spaces and then I'm going to see what they typed in so essentially what I'm going to do I'm say if they typed one then we're going to view the tasks if they typed two then I'm going to sorry where is this create the task or add the task if they typed three then I'm going to Mark the task as complete and I'm just kind of kind of mapping this up right and connecting the different functions together so the flow is there and then we can go and Implement them one by one so I'm going to say if choice is equal to one then what I want to do is say view tasks right okay now I'm going to say l if choice is equal to two and the way that the L if works is if this is not true then we will go to check this condition so we're going to say l if choice is equal to two then we're going to say add task okay or what do we call it we called it create task then we're going to say l if choice is equal to three then we're going to Mark task complete and then we're going to say l if choice is equal to 4 then we're going to print goodbye and we are simply going to break and when we break that's going to exit this Loop which will then end the program and then lastly we'll have an else and we'll say print invalid choice please try again okay so if L if L if L if else so if this is not the case we check this if it's not the case we check this if it's not the case we check this and if none of these were the case then we go into the else okay and that's pretty much it and then what we can do is we can call the main function so again I know I keep repeating this but what I've done is I've just mapped this out I've connected the different functions I now have an idea of what it is that I to do and really all that's left is to write these individual functions and it's significantly easier for me to think about these one at a time than it is for me to try to write them all at the very beginning so let's just run the code for right now and see if this works we're going to see nothing really is going to be happening but if I run project 3. piy you'll see the print out to-do list manager view task add task Etc if I type five I get invalid choice if I type one then it just goes to the next thing right three and then four and then I EX okay so just testing that so far this is working obviously we can make it look prettier if we want all right so that is great we now have the op uh the options and kind of like what we're able to do and the next step is to start writing these out so let's begin by worrying about how we're going to load the tasks in and how we're going to save them into a file now I'm just going to remove this for right now because we don't really need the plan anymore I'm going to import a module called Json now Json stands for JavaScript object notation and you can create Json files that can store data that looks very similar to a python dictionary so what we're going to be doing is we're going to have a python dictionary we're going to have tasks and then in this tasks list there'll be a list here we're going to have all of the individual tasks select task task is this and then we're going to have a variable that tells us if the task is completed or not so the idea is we can store this in a Json file because what we've written here is what's known as JavaScript object notation again something very similar to what a python dictionary looks like and then we can just store this file we can update if this is completed or not completed we can add additional tasks to the list we could remove tasks if we want and we'll just store that in a file we'll load it in we'll modify the tasks and then we'll override the file or save the new changes to the file after just bear with me as I write out the code and you'll understand how it works so first things first I'm going to say my file uncore name is equal to we're just going to call this to-door list. Json okay now we

can just manually make a Json file just so you can kind of see how this works to begin so I'm going to make a new file in my editor and I'm going to make sure this file is in the same directory where my python code is so where my project 3 file is is where I'm going to make this file and I'm going to call it to- doore list. Json now if you're working in vs code you'll see that it gives you kind of the uh braces here indicating that this is Json and what we need to do is we need to write what looks like a python dictionary so these open braces we need to specify a key in this case the key that I'm going to use is just going to be called tasks and then we can just have a simple empty list so for right now we have no tasks so we just say task is equal to an empty list okay this is our Json file now what I'm going to show you how to do is how to load in this Json file and then how to save content to the Json file so the way it works is we can go in load tasks and we can write the following we can say with open we're going to specify the name of the file which is our file name and we're going to specify the mode we want to open this in now R is the mode for reading so if we want to read the file and not modify it we open it in R mode which is what we're doing right here okay then we say as file and all we're going to do is return json. load and then file okay now what this does is it opens the file for US creates this file object and then we use this Json module to load the file as a python dictionary so what's going to be returned to us is simply a python dictionary that looks exactly like this that's it that's all we get now what we're going to do is we're just going to add a little bit of error handling here because it's possible the file may not be found so we're going to say try and then we're going to put all of this inside of here and we're going to say accept and for the accept we are simply going to return an empty list the reason we're doing this is if some kind of error occurs while we attempt to do this like loading the file then we'll just return an empty list indicating that hey we don't have any tasks and actually rather than doing that we're just going to return exactly what it would look like so we're going to say tasks like this because that's what's going to be returned from our Json so we want to make sure we return a object that's in the same format as this one okay hopefully that's clear again we're trying to open the file we load the Json using json. load it just gives us the python dictionary version of this file and then if something goes wrong with this we're just going to return an empty list of tasks perfect so for now let's just try to call this function so we have task equal to load task and let's just print what the tasks are and just for sanity I'm just going to add inside of here a random string that just says hello world so we can see if we get that okay so I'm going to go here and I'm going to run and notice it tells me we have tasks hello world if I change this and I add another task and let me just get out of this and run again you see that now we get the other task so we're loading from this file into our python script great so that's how we load now that we've loaded what we want to do is learn how to save so in order to save our tasks we're going to take as a parameter to this function the task that we want to save now this is just a variable that we can pass to the function and then the function can use this to well save the tasks so in order to save this we're going to do a very similar thing to to what we did when we were loading and we're going to just copy all of this and we're going to change the width so we're going to say width open file name this time we're going to open this in W mode now W mode stands for right mode and this will override an existing file so if this file already exists it's essentially going to delete it and then recreate it and write inside of it whatever we tell it to write so rather than returning json. load we're going to say json. dump and we're going to dump the Tas s into the file okay so what json. dump does is it takes our python dictionary and it just wri

2025-02-08 15:45

Show Video

Other news

HP 5087A Distribution Amplifier has a mystery board 2025-03-14 05:07
Logitech: The Secret to 45 Years of Innovation 2025-03-13 03:03
Trump's Potential Chips Act Reversal, Tech Faces Tariffs | Bloomberg Technology 2025-03-06 20:48