5 Quick Python Projects for Beginners (finish in one day)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're a beginner in Python then these five projects that we're about to build will absolutely take you on Python Programming to the next level and what better it will also allow you to show off a bit because as much as these projects are very simple and only require the very basics of python they're also very impressive and once even better any given one of these projects should take you no more than like half an hour to an hour to build but at the same time I've designed these projects in such a way that as we go through them in this video all of them will teach you new things python so that at the end of this video you will have new python skills that you can also go and show off and get laid okay probably won't help you with that last part but building projects is the best way to improve as a programmer and these very projects are perfect for you if you're a beginner and you want to improve your skills we're going to make games we're going to make a finance app we're even going to make a language learning app everything's going to be time stamped down below so you can skip over go away come back to this video for for different projects at any time that you want before we get into the projects if you like my teaching style and you are serious about not just learning python but actually becoming a real python developer and getting hired in the industry then I have designed a full program where we go through Python Programming object-oriented programming web development project building skills as well as all the technical and non-technical skills that you need to actually get hired in the industry even if you don't have a degree or any previous experience in as little as 6 months it's called python developer boot cab you can check that course Down Below in the description if you are interested and use the code IMC for a discount on the program but with that let's get into the video now the first project we're going to start off with is going to be very simple it's going to be a quiz game the idea here is this I'm going to show you some footage of the completed version of the screen now but essentially you have a series of questions and the program is going to Simply ask you these questions and give you some options for the answer and then you are supposed to guess what the answer is and then while this program is running it's going to keep track of your score at the end it's going to print your score to you or something similar so what we are going to start off with is a list in Python that contains objects so AKA python dictionaries with different properties where first we have the prompt which is the question we have options which are the options for the different possible answers as well as the correct answer for that question and then in a second what we'll do is now Loop over these objects and then print first The Prompt then print the possible answers and then ask the user to give either A B C or D as the answer and if it's correct we're going to increment the score of the user and if it's not correct we are not going to do that and basically at the end we're going to print that so that is the idea here what we're going to do is start by defining a function called run quiz and then what we're going to do is take these questions as a parameter which means that we are of course going to have the move that up top just like this so this is going to be a function that we are then going to run at the end so the first thing we need to do is initialize a score variable at zero which we're then going to keep track of throughout this program then what do we need to do well we need to somehow Loop through all of these questions and ask them all in order so we're going to run a for Loop for question in questions so if you're not familiar this is running a loop through all of these items where in every iteration of the loop all the code inside of this Loop which is going to be indented inside of this for Loop is going to run with each iteration this question variable is going to refer to first this one then this one then this one then this one Etc and of course you'll be able to customize this array with any questions and answers you want so first what do we need to do well we need to print the actual question on the terminal which we're going to do with this line we're going to print question prompt so we accessing The Prompt attribute of this question object from here so we're accessing this one and we're printing it on Terminal good well then we need to print all of these so of course what we could do is simply print question options as a list but it's going to probably look a bit better if we actually print them line by line so we're actually going to also Loop through all these options so for option in question options like this we're then going to print the option like that and we're just going to run this right now to see what it looks like for now and actually well of course nothing happens because we're not running the actual function we're going to run quiz with the questions as the input so this is what's going to happen what is the capital front it's going to print the options etc etc etc do that for every single one now what do we need to do well we need to ask the user for the answer and so the way we can stop the run of this program to like wait for input from the user is using the input function in Python if this is new to you then don't worry because I'm going to show you how this works so we're going to Define a variable called answer and set it equal to input and then what is inside of this input is going to be the prompt so sort of the question that we want to print to the user I'm going to show you in a second how this works so we're going to print something like this enter your answer and so just to show you what's going to happen basically this is going to run the first itation of this loop it's going to print that print this but then the run of the program is going to stop it's going to wait for me to give this program something in here once I give a then it's going to do the next one etc etc etc of course we we're going to stop that right there and we're going to need to do something else but that is essentially what happens here so now this answer variable is going to contain whatever I give to the program as a string now that's important it's going to be given as a string always so what do I need to do is I need to check if this answer is correct essentially so I'm going to compare this against the question answer property which is going to contain the correct answer so if these are equal then I know that our user successfully gave us the correct answer in which case we can do something like print uh correct hooray or whatever and I'm going to also print something like this forward sln which is going to tell a program to print an empty line of that and it looks like we have a typo correct just like this now if we are correct we need to remember to increment the score variable so the way that happens is doing something like this plus equal to one what plus equal to does it takes whatever is the value of score right now and he increments one to it and then sets the new variable with plus one compared to the previous one as a new value of score just like this and in the else case if we are not correct we are not going to do anything to the score we're just going to print to the console to tell the US user what a complete loser they are for not knowing the the correct answer the correct answer was and we're going to be a bit mean we're going to say wrong loser the correct answer was this and then at the end of the program so when this entire Loop so now these lines will run for every question essentially after this entire Loop has run through we still probably want to just print what the score was and things like this so we're going to print something like this you got score out of blah blah blah questions correct so what is happening here is we are first of all we using something called an F string in Python so when you put an F in front of the string is going to allow us to plug variables instead of the string that we're printing with this print function like this where we use these squiggly Square no squiggly brackets whatever these are called and inside of them we can put the variable name and that is now going to be printed inside of this string and we're accessing the length of the question to tell us how many questions we had and just like this so now if we run this we can see what's going to happen where's the capital of France well it is Paris uh correct to which language primary is sping in Brazil it's going to be Portuguese now the next one is the smallest prime number now let's say I have no idea and I'm a bit of an idiot and I say d uh this going to tell us that we are a loser who to kill the Mocking Bird actually I have no freaking idea actually I do Hing way right no God damn it well as we can see we got two out of four questions correct now this one more thing one more optimization that we probably want to do because let's say we run this again and I'm a bit careless and I don't realize that I actually have to input this answer in uppercase the thing with programming is that programming languages are always going to take us completely literally so let's say I put a in lowercase it's still going to say wrong which might sound strange but it actually makes sense because in this program we're specifically comparing here the answer to the question answer which is always going to be uppercase so if we give a lowercase a then then that's still going to give us a wrong answer so we might not want that what we might actually do here is when we're getting this answer we're going to append it with this function called upper which going to take whatever we give it and if it is not in uppercase already it's going to put it into uppercase so even if we give this answer in lowercase it's still going to compare it properly essentially so we're going to run this again we're going to give a in lower case and it's still going to say correct that is probably what we want so now this is sort of the basic version of this what I want you to do if this interest you you can of course add your own questions array and you can absolutely just go and play with your dad or your friends in your own python quiz program which is pretty cool but that is the quiz game by the way all this code is going to be down below in the description if you want to take this code and then go and expand it in your own ways which I always recommend our next one is going to be hangman so the idea here is that we're going to be playing the Hangman game you might have played it at school or something the idea is that someone chooses a word and then we tell the other players how many letters are going to be in that word and then one by one the people playing the game are going to be guessing letters if the letter is included in that word those letters are going to be placed in the correct spots in the word on the board or wherever it is or in our case it's going to be in our terminal and then there's going to be a certain number of attempts by which we have to guess what the word is and if we do then we're hanged and we die essentially so high stakes here so we're going to make a python version of this we're going to create a file called hangman dopy and we're going to get started first of all we're going to define a list of words that's just going to be a list of random words that for the sake of this example are going to be the words that we're trying to guess then we're going to have to import something called random because what we're going to do is we're going to choose a random choice of these words which going to be the word that we're then having to guess when we're playing the game and the way that works what random is is a built in library in Python which we're importing that gives us access to a lot of random operations essentially now this might be new new to you and if so then well now you know how to use it the way this works that we go random. Choice and then we give this Choice function a list of the words or whatever we want to get a random Choice from and so now this chosen word is going to be a random Choice from one of these words in here then we're going to create a word display which is essentially going to first just look something like this where you just have underscores that is of equal length to The Chosen word so this might be new syntax to you actually um we're essentially running a for Loop where the result of that for Loop is going to be a list where every element of that list is going to be this underscore and the way this works is that it's going to be a list that is going to run for every element in this chosen word where now every element of this chosen word is going to be actually every character of this chosen word for example in the case of python this followup is going to run the first iteration we have P then y then T then H then O then n and then for every one of these items a new instance of this underscore is going to be appended this list so I'm just going to show you what comes out of this so I can show you so I'm going to print word display and I'm going to run this so just like this we're going to have a list with underscore so whichever one of these words was chosen I would assume it's probably JavaScript maybe actually no I don't know anyway this is going to be a this word display list so we're not going to have to do that then we're going to give some number of attempts to our user how many wrong ATT temps that they essentially going to have to guess these letters I'm just going to give eight you going to give whatever number you want and then we're going to print welcome to hangman and now the game is going to start so what do we have to do well we're going to run some sort of a semi infinite while loop where essentially this rounds of this game are going to keep running until one of two things happen either until the entire word display is filled with the correct word or until we run out of attempts so essentially we can go while attempt is more than zero right while we still have attempts left this game is going to keep running and while there still is an underscore in this word display now the way this works is that it's going to check if this word display array includes at least one item of this underscore that means we still have letters left to guess in the word display therefore the game should keep running so while both of these conditions are true our game is going to run and this is going to be the game so going to start by printing first a new line plus then sort of cryptically empty character whatever do join word display okay what the hell are we doing here well let me just show you so basically this is what's going to print and okay it keeps running infinitely what we're simply doing is we're taking an empty screen and we're concatenating it with every element of this word display which for now are these underscores and we're printing that just for the user to show them how many characters we have in our word and then we are again going to ask something from our user what we're going to ask is something called a guess we're using the same input function to ask for them and the prompt is going to be guess a letter and then we're going to put that letter into lowercase so in case we give the letter an uppercase we want it to be lowercase so now we have a guess which is going to be a lowercase letter that we got from the user so what do we have to do now we have to check if this guest letter is inside of this chosen word which by the way the user is not going to know obviously so we're going to check if guess in chosen word then what do we have to do well we have to then modify this word display such that the places where this letter gets appears this underscore is now replaced by that letter so we sort of revealing that letter so there's may be many ways to do that the way I'm going to do this is I'm going to run a for Loop where I'm going to access the index of the word as well as the letter itself so what is the letter in the world and the way I can do that is by calling enumerate with the chosen word just like this to what enumerate does if you overover over it okay well this description is not useful at all but what it essentially does is it takes this chosen word and it gives us access to both the actual letter in any iteration of the for Loop through this word as well as the index of that letter so indexing starts from zero so let's say we have python in the first iteration letter is going to be equal to p and index is going to be equal to zero because we start indexing from zero in the second iteration letter is going to be equal to Y index is going to be one etc etc etc that's how it works I'm going to show you now why we do that so first of all what we do is if this letter is equal to the guess then we're going to go to the word display and we're going to replace the corresponding index of that with the guess itself now if that doesn't make sense in a minute you will see why we do that so what this is doing is this is essentially revealing the letter and then in the else case if the guess is not in the chosen word what we have to do is first of all we're going to print that that letter doesn't appear in the word and we're going to be mean again we're going to go idiot like this you don't have to be as mean as me and then what we have to do importantly is go to the attempt and now deduct it by one so similarly previous we had plus equal 1 which it was appending the variable y1 minus equal 1 is deducting one from that variable just like this so now this Loop is going to keep running for as long as we have attempts left and as long as they are still underscores AKA as long as we still have unguessed letters inside of this word so at the end based on the result we're just going to print a couple of things either we guess the word or whether you run out of attempt etc etc etc I'm just going to show you how this works now okay so this is going to be the word I'm going to see okay it could be Java so I'm going to say G there little just no appear in the word okay what else could it be these are programming languages Swift obviously it's not that uh I'm going to have to cheat okay it's probably Ruby right so I'm going to go R okay there we go u b y just like this and we guessed the word obviously I had to cheat by looking at this actual array but that is the game I encourage you to perhaps take this a bit further you can again this a game that you can literally go and play with your friends and you can showcase your amazing programming skills they probably won't care by the way but it's good practice nevertheless okay let's move on to the next one now this one is cool we're going to make a budget tracker so the idea here is that you can set yourself a budget and then whenever you have expenses let's say you buy coffee for $5 and you buy whatever for $50 you can then just add those expenses into your program in your command line and then you can print whatever budget you have left for the month or for the week or however you want to do this so just to save some time I'm going to first print to you how the main program is going to run so I'm going to define a function called main which is going to be the function that's going to be run when this file is run so usually in Python when you have several functions in a file you add this line the end where it say if uncore name uncore uncore equals toore maincore uncore weirdly then we run the main function that's just basically checking that we are running this file sort of properly from the command line so what's going to happen is going to print welcome to the budgeting in Tracker app and then it's going to ask for the initial budget so what is the budget that we wanted to set for ourselves it's going to get the input and remember the result of the input is always going to be a string and so we want to turn that string into a float which is a type of number with decimal points and we do that with this float casting function so this initial budget is not going to be of type float it's going to be a number that we give it there some lines that are commented out here we're going to get back to those later because there's actually a bit of a problem with this initial implement mentation that we're going to do then we're going to set the budget to be this initial budget we're going to set an expenses going to be a list of expenses that we're then going to be asking from my users so then we're going to run what's called an infinite Loop so while true so essentially this Loop is going to keep running Forever Until We manually end this program so we're going to print what would you like to do either add expense or show the current budget or exit the program and then we're going to get the choice either one two or three from the user and then if the choice is one so we want to add an expense we're going to ask the user for the description of the expense as well as the amount of the expense which again we need to remember to cost into a float and then we're going to run an add expense function which we're going to Define in a moment similarly if the choice is two which is to show the budget we're simply going to run the show budget details function which again we're going to implement in a moment and lastly if we have three then we're simply going to break which is going to break out of this Loop and end the program now first the most important thing is going to be this add expense function so we're going to go and Define add expense like this and it's going to need to take is the expenses which is all the expenses as well as this description as well as the amount of the expense that we're trying to add now what do we need to do well this expenses variable is going to be a list of objects where object is going to have a description and it's going to have an amount so we're simply going to go do expenses and we're going to append something like this which is an object with description of the description that we gave it and an amount attribute of the amount and then we're going to print to inform the user that this expense was added and an important detail about how python works is when we give these expenses list to this function and we pen to it that change is actually going to apply to this expenses variable even outside of this function this is related to a concept called object reference passing I made a previous video about five crucial python con Concepts that you should learn as a beginner python programmer to take yourself to the intermediate level I'm going to leave that video down below in the description if you want to go into it we go into this and four other Concepts in more detail in that video but just good to understand so that is the ad expense function then we're going to define the what was the other one show budget details function we going to take in a budget variable which we're going to give it as well as expenses and inside of here first going to print the total budget again using FST strings we're going toint total budget this budget and then we're going to print expenses colon and then we're going to Loop over these expenses and print all of them just like this where we print the expense description as well as the expense amount and then and obviously you can do this however you want but I'm just doing it like this but at the end we're simply giving total spend which is going to call a function called get total expenses which we're going to implement in a second as well as get balance so in fact let's Implement these functions right now we're going to go def this and we're going to need to take expenses essentially we want to sum together all the amounts from these expenses lists and then return that from this function so we're going to initialize a sum at zero and then for expense in expenses we're going to then add the sum the expense amount just like this and then at the end we're going to return sum just like this then we're going to need to Define this get balance with a budget as well as expenses so here we simply need to take the budget that we had initially and then deduct from that the total expenses that we have spent so far so that we can here print what the remaining budget is so very simply using this function that we just defined before we're going to Simply return budget minus get total expenses with this expenses just like that now this one should work well that seems to do it because we now have the show budget details function and we also well in the last one we didn't need to Define any more functions so that should do it for this basic application of this program so we're going to test it out please enter the initial budget I'm going to put 10,000 what would you like to do I like to add an expense let's say I spent 100 bucks expense amount actually well that was the description I messed up anyway doesn't matter 100 now we want to add another okay let's say groceries using this properties this time I don't know 56 okay now we want to show budget details looks like we have a total expense that we've spent it's going to list them out very nicely just like that and then it's going to give us total spend this and a remaining budget of this much now there is a bit of an issue here because let's say I exit from this program which might just happen when you like turn off your computer or something like that now when I go and run this program again what I'm going to have to do is reset everything like all my expenses everything is gone from what we might want to implement is something called persistence we want some way to store the expenses and the budget somewhere outside of just variables of this program because all these variables when you stop running the program are going to be reset and they're going to disappear and that is where these lines in here are going to come in so what we're going to do is have a Json file so Json is essentially like a type of data format that we often use to like store simple data in actual files on our computer and then what we're going to do is for this budget and expenses data we're going to be modifying that file rather than just variables inside of our program to make sure that these things stay persistent so we're going to have a file path which is just going to be a Json file that's going to be saved in the same folder where our program is we can call it whatever we want we just going to calling it that then we're going to have this load budget data function when this program runs it's going to load data from this Json file so we're going to go ahead and make that we're going to Def load budget data with the file path that we give it now this might again be a new thing for you in Python but it's very very useful and this is how can we write and read files inside of our Python program the way we can do that is first of all we go and we go try I'm going to explain a second why we do that and then this is the crucial line with open file path and as a second argument R which refers to read so we're telling this function open which is the function that we're using to open up a file that we're just like trying to read it we're not trying to write it if we were trying to write it we might apply it with w and if you hover over it's going to give us all the options over the default to R which means open for reading in text mode other the compon values are W for writing X for creating and writing a new file and blah blah blah and you can read that for further details and then we go ask file so now this file variable is going to give us access to this file inside of our Python program with this line we have now opened up this file for our Python program to read it and like access the details and things like this so we're going to need to run something called data which is going to be equal to Json no not Json json. loads file and Json is going to be another package so a library that we're going to have to import just like that that gives us access to different tools to read Json files and now that we're going to return data and from the data we're going to grab initial _ budget and we're going to grab data expend says just like this and this is just the form in which we are simply deciding that we're going to store this stuff inside of these files and of course they should be R just like this but whenever we're doing anything with files like we're trying to read files we need to account for the case where we might have an error here so if an error happens when we run these files we're going to run into something called an exception so we're going to catch that exception to make sure our program doesn't crash using this except line at the end instead of it we put putting the potential errors we might get and if that happens we're returning zero and an empty list back into a program to basically indicate that there was nothing inside and one more thing there is a typo right there it should be Json load not Json loads just like that so that means we no longer need this line because we're actually getting the initial file from inside of this file and if the file doesn't exist I'll show you how we fix that in a moment okay so we're halfway there we're correctly getting the data from the file the file exists but what we also need to do is when we are exiting the program we need to make sure that the current stuff that is just stored in variables inside of our program is now then saved back into that file and so instead of this choice three which is going to be the exit the application we're going to need to call something called save budget details or however you want to call it with the file path initial _ budget as well as expenses and we'll implement ment that in a moment the other thing we need to do is make sure that if nothing comes out of here we need to give ourselves the initial budget so if the initial budget is zero we need to get it which we do like this so we go if initial budget is zero then we ask for it from the user as we did before and these expenses obviously does not need to be initialized because it's either going to be empty when it comes with the file or doesn't come back whether it exists or not and if it doesn't exist it's going to come back as empty anyway so now L things lost save budget details is going to be a function that still need to be implemented we're going to Define it just like this taking in file path initial budget and expenses so what we're going to do is Define a data object which is then going to go inside of this file which we're going to save we're going to again use this syntax to open up a file this time we're going to be using a different flag to indicate that we actually creating or writing into a file using this double like this and as file again this file is now going to be this file which we either create or modifying depending on whether it exists or not and by the way this thing does it automatically so then we go Json dumps or no dump dumps not dumps dump so Json dump is a function to essentially just dump this data object into this file that we are creating so we're giving data as the first argument which is the thing to be dumped into that file and then we're getting the file which is this file that we are opening up over here and we're giving something called an indent to like basically make it nicer to look at we'll see in a second why we do that so that should cover it okay so I'm not entirely sure if I've done everything correctly here but usually in these cases it's best to just test out your program and your program is just going to tell you if something is wrong it's much faster than trying to like read through everything and see if I've made something wrong somewhere so I'm going to start this welcome to the budget app please enter your initial budget so in this case we didn't have this file yet so this came out empty in IAL budget game out at zero so it's asking for us this if condition was triggered so we're giving us a $1,000 this time now what would you like to do at an expense just like this groceries of 100 bucks another expense dinner 50 bucks like this going to show budget details okay expenses total spend budget left now let's see if this works I'm going to I'm going to put three to exit the program okay Trace back we made a mistake somewhere blah blah blah it's not supported between instances of float and list okay so we can see that on line 64 is where our error happened last save budget details okay it looks like it's just a typo oh yeah indeed whoopsie it should be a comma of course let's try this again that didn't work so the file now doesn't exist because we had an error last time I'm going to just add an expensive groceries type 100 okay I'm just going to test it out again again three indeed it looks like it worked this time if you look closely instead of our folder is now created a Json file with the extension. Jon if we click it open we can see that this data is now inside of this file how awesome we have an initial budget which was the initial budget and so now if I run this program again budget tracker and I go and show budget details without adding anything then we can see that we still have these expenses as well as our total spend and our remaining budget because it's being stored persistently inside of this Json file rather than just inside of our program this is the key inside here this is super important thing obviously it's going to be a much more complicated and sophisticated database solutions that you can make with like sqi and like different database languages now that is what you want to do perhaps at the next level of your programming Journey but this right here is the most simple way for you to essentially create a database where you can store data on your computer persistently very simply using this Json files and you'll be surprised like in the real world just how complicated things are just stored in this simple Json file in a lot of our startup coding for example we literally stored like pretty complex ass data just in Json files using these Json libraries in well we were using in JavaScript but anyway same principle very easy to use so a very good skill to have as a programmer and now we have a budgeting tracker and now obviously you can take this and make it more complex allow different things like different commands and maybe you can even make a visual user interface for this like this is a really cool program that you can definitely work on with that though let's move on to the next one which is going to be a language learning app now this is pretty pretty cool which I know I'm saying about every project but this is particularly cool because I think it's quite unique I haven't anyone on YouTube seen do a program like this in Python so we're going to create language I just call learning app.py so when I was younger I was really into learning languages I still would be but nowadays I'm more into learning programming languages rather than human languages but what I learned about language learning is is that in the beginning the most high Roi thing you can do is just take a list of like the most common 100 or thousand words in that language and then just practice those words over and over because you might be surprised to hear that like I think the statistic is like 80% of Any Given language is just made up of the most common thousand words so if you can study the most common th000 words you're literally going to understand like 80% of that language so what you can do is you can go to chat DBT right here and you might have to try a couple of times to get it to give it to you in the proper format but let's say you want to learn Spanish you're going to tell it can you give me the most common 100 words in Spanish with English translations please give as a python object let's see if this works it's going to give me common Spanish words it's going to give it as an object object now that's one way of getting it the way I got it before was in a format that I slightly prefer which is going to be like this which is going to have a words list where every item of the list is going to be an object uh with the attribute Spanish and the attribute English I just prefer it simply because this allows us to practice them in both kinds of ways we'll see in a moment how this works but now let's get into the actual app so first again we're going to start off with the main program that's going to run which is going to print something like welcome to the language app and input which is going to pre enter to start the quest just to prompt the user to start the game and then we're going to have this quiz user function which is going to be the main thing that we're mainly running here so def quiz user with a words list that it takes in that allows us to use different lists of words this is just I think like 30 or something now there's many ways to do this and this implementation here is not going to be the most optimal way to learn but you can take this and modify it but we're just going to first go to this random Library again which we are of course going to have the import and we're going to go and do a random shuffle of these words which is going to just take this words list and just shuffle them randomly so we don't know in which order we're going to get them then we're going to give ourselves a score and then we're going to Loop over this list of words so word in words so just the same time I'm just going to print the so the same time I'm going to copy paste so we first we're going to print what is the English translation of word Spanish so it's going to take the word and the Spanish version of it then we're going to get the answer from the user and we're going to call two string Methods first is the lower so if we for whatever reason have caps lock on or something that's not going to affect the answer just going to put everything in the lower case because everything here is going to be lowercase as well and we're also going to strip so for example if we're being careless and we're adding like uh White spaces those are not going to affect our answer either so as you can see if we hover over it return a copy of the ring with leading and training white space remove so that is what we're doing then we're going to get the correct answer which is going to be the word English attribute again we're going to put it in the lower just in case something here happens to have uppercase for whatever reason then we're going to compare if user answer is equal to correct answer then we're going to print correct amazing as well as well as and empty line and we're going to append our score by one like that and in the else case I'm going to print wrong idiot the correct answer answer is that and at the end we're just going to print the score now this is not going to be optimal just yet in reality you might want to do what's called like space repetition So based on when was the last time you saw these words you might want to like space them up a bit further or a bit closer based on whether you got them correct or not you might want to like give some like additional attributes here like last scene or to indicate like whether last time we saw this word it was correct or not but just to illustrate this to you this is how we're going to do this so we're going to run this program and we're going to enter to Star what is the English translation of uh well it's going to be slightly suboptimal because I'm going to say two well okay that was wrong anyway L is going to be the correct is para that is four right no well again this right here is again not optimal because I gave four and the correct answer is four slash2 so you might want to implement some sort of logic with the English translation is perhaps a list of of and from and then what it does is that it will check through all of these and it will check if your answer is one of the items on this list now that's going to be a much better way to do this but I'm going to leave that to you as an exercise e is going to be no that's going to be Andro just like this I'm pretty good at Spanish as H whatever him the off C Dam who anyway you get the point this is pretty cool if I say so myself but yeah let's move on to the last one which is going to be probably the most complex perhaps but nothing we can't handle let's take a look it's going to be a desktop cleaner so the idea of this program is that let's say we have our desktop folder that looks like this that's messy with all kinds of different files what we might want to do is just run one program which I'm going to do right now and boom have everything sorted into different folders based on the file type nicely sorted just like that now how might we do this well for this purpose we're going to need two libraries that we're going to have to import we're going to have to import OS which essentially gives us access to a lot of like operating system level like things that we can do to our computer as well as shuttle which is like a library that allows us to move files inside of our computer's file system by the way I have a full video where I make a slightly more complex version of this on my channel you can click on it down below in the description by the way I also have a full playlist list of python programs if you're interested in more stuff again link down below alongside my full program if you want to Learn Python from scratch and become a developer also link down below but what are we doing well this is going to be the main run we're going to run that the script is running we're going to get a folder path now this folder path is going to be something that you're going to have to Define for yourself like whatever is the full path to the actual download folder on your computer the way you can get that is if you open up your terminal on your computer and this on Mac on Windows is probably slightly similar but you can just Google how to do this so we're going to go and we're going to see to the downloads folder so CD means change directory so we're changing directory to the actual download folder and then we're typing in PWD which stands for present working directory and it's going to print us the actual path downloads folder so you can just get that copy it into this string folder path right here so then we're going to check os. path. isur and the purpose of this is just to validate this is an actual directory whether this act actually is a directory and if so it's going to run this clean folder function which again we're going to Define in a second and then it's going to print clean in complete else is going to give it's going to tell us that it was an invalid folder path then that we were naughty and stupid basically so we're going to Define clean folder with the folder path now what we're going to do is go for file name in os. list D folder path by the way if you're wondering how the hell do I know all of these functions from all of these libraries well I don't when I was doing these I just took this OS library and I went on Google and I typed in OS Library documentation where you see like a documentation of all the different operations and functions that you have access to and this particular one if you hover over it it will tell you well actually it doesn't because this documentation sucks but if you read it from the documentation it will tell you that this simply gives us access to a list list of all of the files inside of this folder that we're giving this function and actually well Pro tip much easier way to do this is just to tell chat gbt and it's going to probably suggest something there's probably other ways of doing this as well there you go it's giving us the OS library and there you go that is it and it's also well it's slightly different sytax but same thing anyway now we have access to every file inside of that directory what are we going to do with every file well first we have to validate that that actually is a file in a way so we're going to do that by going os. path. is file so similarly before we checked is there to validate that it's a proper directory now we're validating that it's a proper file and now we're actually going to need the full file path which is going to be this file name with the path of the folder in which it is inside and so we get that full path with this os. path. join folder path which is the path of the folder with a file name so this is just literally joining these two things the folder path with the file path to get the full path to the actual folder and so just to see where we are right now I'm just going to print well I'm actually just going to print this to show you what we are getting colon what is wrong what is wrong what is wrong of course there is one more there is one too many of those okay let's see I'm going to run that okay so we get a list of all of the files that are inside of our download folder which if we open it up up we see that that is indeed what it is just like that next thing so what do we need to do we need to start making subfolders per the file extension of course there's many different ways to do this in my other video I do this in a more complicated way to do it by like different images and or videos or documents and things like this but we're just going to need to access the file extension from every file so we're going to use a bit of trickery to do that we're going to get a variable called file extension by going file name do split so we're splitting it by dot because we know that the file extension is going to come after the final dot inside of that string and then by indexing with minus one we get the last thing that is coming out so file name do split is going to give us a list of substrings that are split by the dot and we know that the last one of those is going to be the file extension by indexing minus one we get the last one because minus one indexing gives us the final one and then we're going to go do lower to make sure that everything is in lower case we're going to make sure that as long as it exists we're going to make a name for this new subfolder that we're going to start creating which is going to be an F string with the file extension name in uppercase then space files that's just going to be the name so for example if we have a BNG file then the name of the folder where that file is going to go is going to be PNG in uppercase space files you can of course name this in whatever way you please then we're going to need to get a subfolder path obviously in the beginning that subfolder now obviously we have sort of two cases here here the subfolder might already exist if in a previous situation that subfolder has already been created or it might not exist so we're going to need to create a new function called create subfolder if needed we're basically just going to take a folder _ path as well as a subfolder name and it's either going to go ahead and create that subfolder and then return as the path or just return as the path if it already exists so first we're going to check if this subfolder path already exists so it's going to go OS path join it's going to take the folder path as well as the subfolder name so it's going to give us that path and then we're going to use this check to say if not os. path. exist so this again which you would just get from jbt or either the documentation will give us either true or false depending on if this subfolder path is already a real folder and if not we're going to go and call os. make dur with this subfolder path and this function is going to Simply create a new subfolder with this path and then after that we're going to return the subfolder path and of course if it already exists then this line is not going to run and the path is simply going to be returned which means the back in our function we can just access the subfolder path which is going to be returned by this create subfolder if needed function which now we know that it will exist then we're going to need to get a file path which is going to be again OS path joint with the folder path as well as the file name and then finally we're simply going to have to move that file with this file path to this subfolder path for which we are going to make use of our shuttle Library which we imported here and from jbt or the documentation of this Library you would find there is a method called move the shuttle do move where the first argument is the file that we're moving second argument is the folder to which we're moving it and that is going to move our file correctly and at the end I'm just going to print moved file name to sub folder name just like this uh I feel like this should work let's try it so we're going to have our current downloads which going to be a bit messy we're going to run this and we get errors oh no okay destination path already exists okay so what we're going to do is be a bit lazy and we just going to ask chat gbt for help and I'm showing this to you because this is legitimately what I would do if like within a few seconds I don't know what to do it's usually just easier to ask chbt because chbt is just a genius and wide waste brain power if you can just get it to fix it obviously understand what the problem is after it gives it to you because that is how you learn but use these AI tools I use them all the time everyone uses them don't be afraid of using them okay okay so this is the line that we're going to have to add going to come off the file path new location Okay so we're first going to get the new location which is where that file would go I suppose and then we first check if it already exists over there then we're going to move it otherwise we're going to skip this and we're going to see if this works now of course it doesn't and of course this can now be removed and now things seem to have worked let's open up our downloads and beautiful everything is beautifully organized in two folders so that is the final project I hope you enjoyed this video I hope at least one of these projects was interesting to you certainly took a lot of time for me to make this video so if you enjoy this please leave a like down below if you want more python Tut tutorial those are going to be linked Down Below in the description if you are serious you enjoy python you want to do this as a profession you actually want to get hired with python I have a full boot camp an online boot camp that you can do completely self-paced from the comfort of your home and learn all the skills that you need to get hired in the industry as a python developer in as little as 6 months can't guarantee is going to happen in that time frame but that is what is realistic if you take this seriously go check that out down below in the description if you're interested in that and use the code IMC for a discount on the program with that if you want more python tutorials you can watch this video right here we we go into more automation projects been one of the most popular videos on this channel so I guarantee you you're going to enjoy it with that I'll see you in the next one
Info
Channel: Internet Made Coder
Views: 32,215
Rating: undefined out of 5
Keywords: python projects, learn python, python automation, automate your life, learn python fast, python fundamentals, learn to code, coding projets, coding project ideas, python tutorial, desktop automation, coding tutorial, python 101, python project ideas for beginners, python for beginners
Id: 4TZ1K8EHT2M
Channel Id: undefined
Length: 48min 20sec (2900 seconds)
Published: Sun Feb 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.