📝 Zusammenfassung
openai-gpt-4o-mini
## HAUPTTHEMA
Im Video werden die **zehn besten integrierten Python-Module** vorgestellt, die Entwickler kennen sollten, um ihre Programmierkenntnisse und die Qualität ihres Codes zu verbessern.
## KERNPUNKTE
• **Logging**: Nützlich zur Überwachung von Programmausgaben in Produktionsumgebungen durch Erstellen von Log-Dateien.
• **Collections**: Bietet spezialisierte Datenstrukturen wie Counter, Queue und Default Dictionary, um bestimmte Aufgaben effizienter zu erledigen.
• **JSON**: Ermöglicht das Speichern und Laden von Daten in einem format, das leicht in APIs und Webdiensten verwendet werden kann und dem Python-Dictionary ähnelt.
• **Subprocess**: Ermöglicht das Ausführen von Systembefehlen aus Python heraus, was Automatisierungsaufgaben erleichtert.
• **Math**: Bietet Funktionen zur Durchführung gängiger mathematischer Berechnungen, was die Programmierung vereinfacht.
• **Random**: Wird zur Generierung zufälliger Zahlen verwendet und ist wichtig für Spiele oder Anwendungen, die Zufallselemente erfordern.
• **Datetime**: Nützlich für die Arbeit mit Datums- und Zeitangaben, einschließlich Berechnungen für zukünftige Daten.
• **Re (Regular Expressions)**: Ermöglicht das effiziente Suchen und Manipulieren von Strings anhand vordefinierter Muster.
• **Sys**: Wird zur Verwaltung von Systempfaden und zur Handhabung von Programmfehlern verwendet, einschließlich der Möglichkeit, Programme ordentlich zu beenden.
• **OS**: Ermöglicht das Verwalten von Dateien und Verzeichnissen, einschließlich des Erstellens, Umbenennens und Auffindens von Dateien im System.
## FAZIT/POSITION
Das Video hebt hervor, dass das Verständnis und die Anwendung dieser Module entscheidend sind, um den eigenen Code effizienter und robuster zu gestalten. Der Autor ermutigt Zuschauer, diese Module in ihren Projekten zu verwenden und mit Beispielen zu experimentieren.
today we are going to be looking at the best 10 built-in python modules that you need to understand today to better your code I've written out code problems for you guys to Showcase these modules and I'm going to take you guys through each one welcome back guys to the channel welcome back guys for another episode of Code with Josh for obvious reasons I'm Josh and I'm stoked to have you guys here before I dive into today's video do me a favor hit that like button and subscribe that really does help my channel reach more people around the world hit the like subscribe we're about to get into it now today we're focusing on the 10 best built-in python modules there are endless libraries and modules out there but before we explore those python comes built with modules already and I want to take you guys through the top 10 that you need to know to better improve your code now the first link in the description is my weekly python newsletter where I write and I break down things for you guys I write long form articles and projects to help you guys build on your existing skills so first link is my newsletter and all the other links down below they're dedicated to you and helping you in your python Journey now let's get into why you're all here the best 10 built-in python modules let's check them out guys before I take you into check those out okay I just made this diagram for you so we could really see now everything in the middle here this is just the basic python this is everything you've been learning the logic the functions the classes the conditional statements this is python then on the outside layer you can think of this is like the cloud this is all the green stuff this is all the extra code python has it's built into it but it's not in this core python right we you still need to import it but you don't need to download anything it's like making a quick call to the cloud to pull information a few examples of some built-in modules could be like random math Cy time right and these are just libraries that come pre-installed with python because it's tedious to have to install every single module especially ones that we use frequently then on the outside the brown this orange this is all the external libraries and these are really powerful they could be for data analytics uh app development web development machine learning right they're really heavy but they serve and cater to very specific needs okay so uh I will talk about these ones and I've use these in a bunch of my videos today is focusing on the buil-in core modules that you should know for python now what are the best 10 you ask let's get into it all right guys so first up we have the logging module now remember these are in no particular order all right I just decided to cover this one first and this is particularly useful in production environments where it's impractical to debug in real time so it's going to allow us to keep track of everything that's happening in a different file so we can go through and really see what's happening and this allows us to have realtime monitoring so to begin this we import logging right this is a built-in module this is what we're covering and we need to configure our loging system okay now I give it a level which I want to debug I give it a format this is going to allow for us to structure the way the log appears in the newly created log file as well as the time format okay and then I give the name of the log as well so this allows for different levels of logging like info just for basic information warning errors are debugging and so we can categorize the severity of the events that are happening um and this allows us to read and interpret it so I have a basic function here okay and you can see that I have uh I'm I'm getting basic information about it uh I'm getting any warnings so if it's less than this amount okay I'm just trying to trigger these uh errors debug information information and then uh specific exceptions okay so down here I've called my fun function four times so we can see what's going to happen and I've even marked here what we are going to see happen as well okay so when I've run this code you can see here all the things that are happening right like I'm just I'm calling those but I also created a log file so I can keep track and as we go through a project it's easier to come back to if you go open it up you should see a log file created and this is that transaction log so everything I had here in the terminal we can actually see here and it's nicely detailed just like it was on the terminal but we can come back to these time stamps later and you can see that this is the basic information ooh I triggered a warning here what happened right so overall the use of logging helps in making applications more robust maintainable and production ready by giving us a detail record moving into the second top module which is collections now this collections module provides specialized data structure structures that just really complement the built-in containers we have in Python like lists dictionaries tles and sets Okay um and these are more optimized for specific tasks and give us additional functionality so I've used three things to demonstrate this uh I'll actually turn off the bottom two and we'll go through them together so in the first one I'm using the counter class counter is great CU it really just allows us to uh count the occurrences of words in text so here I have text like this video if you like python all right when I run this it's going to create a uh dictionary well I'm specifying a dictionary um of how many words we have so we can see here word frequency like occurred two times and it allows us to count the number of times something occurred um and I structured it in a dictionary if you don't it's not really a data structure okay it just comes out here as the counter class it returns that okay but I can store that in the dictionary later okay so counter allows us to quickly count okay um great moving on now now another type of data structure we have is called a uh Q right and a q is kind of like a list all right but it's um first come first serve it's like your waiting in a line at a store to check out if you're the first person in line you're going to get helped first right in a list in Python well a list we could structure it that way but by creating a cue this runs more smoothly and in a real world environment I would probably create a class for this okay but here to Showcase this I create my queue here using the deck function from collections we can append just like a normal list okay and then when we're ready to remove from the queue I can use the method pop left so this is going to remove the first person in the queue which is subscribe right when I run this the first person to get helped in my queue is going to be subscribed and it's going to go through the line like subscribe to code with Josh and you can see customer served subscribe our last one is using a default dictionary okay so this is you can view this as a new type of data structure now a default dictionary is going to basically insert values automatically if they are not given right so I'm creating a default dictionary and the value in that dictionary is going to be an integer okay so you can see here I have a list of countries and I want to basically count how many times that country was there right so France Vietnam Costa Rica Vietnam now when I run this the default dictionary is automatically creating this for us but our output it allows us to count the number of occurrences as well kind of similar to counter right but default dictionary we are giving it a default value guys real quick if you're getting value in today's content do me a favor hit that like button and subscribe that really does help out the channel back to it next up in our module battle we have Json or JavaScript object notation and this is one of the most commonly used data formats especially for apis web services and configuration files it basically allows you to save data into a different file so as your program is updating your app is running it's not overwriting anything it's keeping your data safe and Json can be interpreted by other languages too the great thing about Json is it's just like a python dictionary it's nearly identical so all the code you know to write a python dictionary it follows the same use case in circumstances here so I have a function that would load a Json file okay uh I have data I'm just loading the file that's it okay if I want to update user information with a python dictionary we would say dictionary key we can use the update method that python gives us with any new info right so this is the same right if we want to get a new user or add a new user how do we do that with a python dictionary dictionary key is equal to a value and finally to save the user data we have a dump function now the use of dump this ensures the file is human readable which is important when debugging or manual editing that file okay um so going down here right I'm going to create a Json file called users Json and then I'm going to be giving it information so I'm giving it this python information right this is a key the name and then we have a dictionary okay when I run this it's going to automatically create a Json file storing it in a different file structure okay so everything is added because I didn't have this to begin with if we go over and check you can now see that I have a Json file called users Json and look at that it looks just like a python dictionary now there's no key to the dictionary okay because that's Json but internally we do we have a key which is the name of the person and then we have a value which is a dictionary to work with this Json data it's it's just like a python dictionary okay dictionary unlocks a value which is a AR and we have keys and values okay so working with Json is instrumental especially when working with other types of data and being able to save your data python has a built-in module called subprocess and this allows uh our programs to spawn new processes and connect their input output and error pipes and we can obtain the return code so uh think of it like this if I want to run system system level commands directly from python code I could be automating tasks which are typically done manually through our command line or the shell subprocess speeds this up so in my code here I I built out two functions one is just called list directory uh and this is going to list the contents of the current working directory and you can see here that I'm tapping into subprocess and run which is the command to list all the directory contents on a Mac or a Linux so LS and then our get up time this is going to run our shell command uptime to get the systems uptime information uh I'm also using run with uptime here right and this is going to show the systems up time and it's kind of like list directory because it's going to print it but it's going to remove any extra white space here and if we get an err I'm going to I'm going to trigger that air let me bring my functions outside actually okay now when I run this ideally I should see all of these files here and you can see my transaction log users Json file everything that we've been working with so you can see that list directory okay it's opening up a new process for me it's listing out everything um and then you can see here my uptime information so uh the current time I did this at uh the system has been up for how long uh I've been on this for 15 minutes now how many users are there and it's loading the averages over the last one five and 15 minutes all right so this is keeping track of our uptime for me so long story short guys subros is a key module in these built-in modules because it allows us to spawn new processes right and this allows us to connect to our pipes our input our output our errors so it speeds up our automation crossplatform compatibility any error handling and communication with external programs so I don't know about you guys but math was never my strong suit in school and it's funny cuz now I'm a developer but in developing right we can write code to do a lot of math stuff for us we are applying logic in programming so it's a great thing that python has the built-in math module this handles all of our mathy stuff for us and in my example I've built out for you guys this is just going to highlight several common functions from the math module so up top here uh I can quickly calculate the square root with the built-in square root function if I want to calculate the power right I'm going to raise it to the power of two the power of three whatever you want to raise it to okay I'll change it to three for us and then if I want to get the sign or it's abbreviated here as SI n value of a triangle so I can do that with their built-in method uh right and this is just the ratio of the length of the opposite side to the length of the uh hypotenuse in a triangle so it has all these built-in functions now when I run this it's going to do all the heavy lifting for me right so anytime you're doing mathematical things okay use math it speeds up the process makes things easier let's say like 27.5 enter boom here is the square root of 27.5 the value squared I didn't change that this is not the value squared anymore it's I cubed it I Rose it to the third power and then the sign value how cool is that speeds up the process check out the math module anytime you're doing math this is a must know moving on to my next tip or my next module is random and I'm assuming you've used random before okay if you haven't you've been living under a rock that's okay I'm here to explain it now literally anytime you want to work with a random number or you need the computer to do something randomly you should be using the random module now here I have used two functions from random there are plenty more these are some really common ones that I use often and I just made a really basic number guessing game now I'm using choice choice allows you to choose a random item from an iterable list and then I'm using randant randant means random integer so I'm giving it a starting random integer and an ending one for example if this is z and 100 Randon is going to choose a random number between zero and 100 okay uh you can see here in my list I have tupples okay so it's going to choose random tupple it's going to get one of those three random tupples and then I'm I'm going to use the first element and the second element as my random number so for example if I got this one it's going to choose a random number between one and 20 okay um and it's just a guessing game so I'm using random here because each time it's going to be completely different uh let's try this I run my game uh you can see I've selected it's a number between one and 50 so Choice chose the first one randomly I'll run it again afterwards let's guess a number let's say uh 45 too high 30 too low 35 too high ooh 32 too low 33 34 boom congratulations you guessed it I counted the number of attempts for you guys if I run this again oh it chose one50 again I'm just going to run it till it changes oh uh please enter a valid integer let me just run it till it changes there you go one and 200 right so choice choice is doing the job for us 100 too low 150 two high 125 to low 135 two high 130 128 126 127 boom 127 eight attempts okay so anytime you're trying to do something random Fire up and use the random module maybe you've been building an application that requires the use of the date or the current time like a calendar app or a to-do list application I've been there I've done it the great thing is Python's got the built-in date time module we use this anytime we're working with date time now I know there's other modules out there there's extensions or external modules I've used a a pendulum I've used Arrow right but this is built in it does a good job for what we need it to so anytime you need the current time super easy it has the Now function okay that's all you need to do it gets the current time now we do need to string format this time still okay so I do year month day hours minutes seconds to format the time um and then maybe we want to calculate the difference in time I could collect the current time and then we're using something called time Delta from date time and I could be like okay what's the time going to be be in 10 days or a month right so 30 days right and then I can say future date minus today and we can get the difference in the time when I run this it outputs it really neatly for us and this would allow us to use it in other applications or databases that we're making right so current date time the difference between the future date is 30 days right because I just added 30 days to it but you get the point anytime you're working with the time and the date check this module out CU it's going to help things out one maybe you haven't heard of but you definitely should is the re module re stands for regular Expressions these are a powerful this is a powerful module for matching searching and manipulating strings based on a pattern we give it now regular Expressions allow you to describe and search for different string patterns in a more concise way okay so um I have two functions here okay and I have a list of emails you can see which ones aren't valid right this is not valid and I have a function now this is a pattern to define a pattern you can see that I'm using R and then a pattern structure you can get this on the documentation for regular Expressions but I'm basically saying what is valid in this string then I'm using the match function I'm taking the pattern and I'm matching it to the email do they match mat is it permitted okay and the same thing goes for me trying to get different domains so example.com um. org okay um I am trying to get the value domain so one two these aren't going to be valid okay one two three these should be valid and I'm just going down and I'm just printing them off so really anytime we want to manipulate strings replace strings modify them um yeah you can do it with the built-in python methods but really re regular expression is much more common for this you can see here that I'm using match and I'm using search and this is going to search for a pattern anywhere in the string I'm specifically looking for this pattern okay when I run this this is going to be really great for us boom you can see all the valid emails it automatically did for me and then all the different domains right so this would be in our case Google Yahoo AOL come on who uses AOL right uh but these can keep track of that right maybe we have text with prices or numbers inside those strings regular expression we can do the same thing I can go and find those numbers right so regular Expressions guys remember you can match search manipulate and they're really great for working with complex strings so check it out you should be using this our next one that is a must know is the system module or CIS uh if you've ever built out any types of I don't know databases web apps with Jango or flask uh desktop apps whatever we use this all the time and you've probably seen me use it in projects as well the CIS module is used to manipulate the python path and handle exit statuses okay so I use this specifically for exit statuses uh and then other times I've worked with Frameworks that require the use of like right here system aggravation uh if I run this which I will in a second in our first code this is going to print the command line arguments passed to the script in my second code I'm just demonstrating how uh this would shut down the application this would quit the program from running if this was triggered this is a great way to end programs near neatly like games or apps uh and the last one I just tapped in here right I could get the version of python by just saying version kind of cool uh if I run this let's watch what happens boom so I didn't pass any arguments okay that's that's fine okay uh processing value right we got that right here okay because I said 10 and then here's the version of python I am running okay now if I turn this on now notice that I got the version of python but if I turn this on this is a negative number I'm going to trigger the exit so it's actually going to shut everything down so I shouldn't actually see running python version because when this is triggered it shuts everything down for us boom it's done negative value detected exiting okay so typically when you want to smoothly exit programs we use CIS for this as well as other key attributes for dynamic module path management uh controlled exit Behavior debugging information you name it we can use the CIS module I've saved this one for last and you're going to see why the final one to bring it all together guys is the OS module what does OS mean operating system we use the OS module anytime you want to access your desktop you want to work with your directory and this is used to manage files and directories on your your local system right so anytime you want to get a folder from the desktop or you want to get your current directory this would be a directory uh we use OS for this okay now I put together this code for you guys quickly here um and it does it's going to structure everything here very neatly for me okay automatically so I don't have to do it right so in the first one I am just changing the current working directory to the specified directory okay I'm I'm tapping into OS here I am listing all the files in my directory so all these will be listed okay and then I am basically trying to rename and create a folder so when I want to create a folder okay I am making sure that the current folder does not exist or the current file and make dur is creating a new folder okay I can then move the file to the folder so this is really cool when I run this watch what happens to all my files boom did you see how that cleaned that up Json my log all those python files are now neatly organized for me now I can do a lot of other stuff right because I could change directories list the directories change files create them rename you name it but the point of this is anytime you want to work with your files your desktop your directory you need to be using the OS module right that's why it's here in the best built-in modules in Python well there you have it guys in no particular order I included the best 10 built-in python modules that I use and that you can use to speed up your workflow and write better code if you guys think that I missed a module today that you use that's built into python drop a comment and let me know okay which ones do you already use which ones don't you use that you learned here today let me know and if you got value in today's episode do me a favor hit that like button and subscribe that helps out the channel and I can't wait to see you guys in next week's episode of Code with Josh until then python crew