Python Tutorial 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to my brand new Python 3 learn in one video tutorial this is gonna be a big one I'm going to cover the entire core language from the Python documentation and I'm gonna get deep into Python all the way through connecting to databases as well as GUI development where we will create a real working calculator a ton will be covered everything is in the description including the code as well as links to find exactly what you're looking for and I have a lot to do so let's get into it ok what I'm going to cover first is how to install Python as well as PyCharm on Mac OS as well after that I will show you how to install everything on windows very first thing you don't want to do is go to your Applications folder and open that guy up you're going to the end come down here to utilities and open that up you're then going to come in here and open up the terminal after you do that you're gonna want to type in Python - V and more than likely you're gonna have Python 2.7 in there that's not good we're using Python 3 so what you're gonna want to do then is type in quit with parentheses to get out of there and then what you're going to want to do is go to the App Store and you are specifically going to get Xcode after you search for Xcode Xcode is going to pop up right here you're gonna click on probably install or update depends on what you have set up and then Xcode is going to download and then you are going to just close out the App Store because we don't even need to open up Xcode then you're gonna open up your terminal which I showed you before you're gonna type in this command Xcode - select space double slash install then what we're gonna need to do is go to the homebrew site you can find that at brew dot sh and you're going to go dairy down here and you're going to select this and copy it jump back into your terminal to paste everything inside of there and hit enter then you're going to go to whatever your username a kind of folder is mine stearic bans obviously and you're going to want to look for hidden files to show your hidden files you're going to click command shift and period whenever you do that all of these files are going to open up but you just hit command shift and period to make them go away but what we specifically want is the bash profile that you see right here double-click that to open it and you're going to want to put this path right here that you see on your screen exactly the way that it is so put that inside of there and then save your bash profile you can pause your screen as I move here pretty quickly then what we're gonna do is type in brew install Python and air everything is going to install then you can type in Python 3 and you're gonna see that you have Python 3 installed and you can even type in print hello world and hello world will show up and not to shows you everything's working and then type in quit with parenthesis to get out of that now we're gonna go get PyCharm and this is free so you can just go to JetBrains calm and PyCharm and specifically you're going to want to click on download and then get to the community version which is the free version or he can pay them and get the professional version and then just gonna start downloading and you're gonna see it's down here dot dmg you're gonna wanna open that file up just by clicking open and then you just drag PyCharm over into your Applications folder then you can go into your Applications folder and double click on PyCharm it's gonna say yes you want to open this and then I would just check do not import settings and click on OK this is gonna come up you're just gonna click I confirm and hit continue and you can either send statistics or not and more than likely just put a check in here where it says I've never used PyCharm and click on next you can choose whether you want light or dark click on next and click that you want to create a script for opening files like that and click on next for featured plug-ins everything here now I just leave it the way it is and start using pycharm come in here and say you want to create a new project and then click on create and you can give it whatever name you want pycharm is gonna open up then you're gonna want to come up here where it says pycharm and preferences this guy's gonna open up you're gonna want to make sure that you have python 3.7 selected up here as your project interpreter click on OK then you can come over here and click on the folder I call it a Python Tut and click on new and then python file this guy's gonna open up inside of your window you're gonna want to type a name inside of there I just put a little world and hit enter that's gonna create your hello world file and all Python files and with the extension dot py you can just type in print and hello world and then you can click on run like this and then run after you do that this guy's gonna open up you can just double click on it and you're gonna see that everything runs down here now if you want to move this up to over to the right side of the screen you can just click on move to and right bottom and it's gonna move it over there alright so there you go you got everything working on Mac OS and now I'm gonna show you how to do the same thing with Windows alright it's pretty easy to set up Python and PyCharm on Windows what you're gonna want to do first is go to python.org and downloads and I'm using the latest version which is three point seven point four whenever you click on that this stuff's gonna pop up inside your window and there's multiple different ways that you can execute this I'm just going to click right here that I want to use this installer that's gonna say do you want to open this and then you of course say yes that's going to open up the Installer I just click on Install Now and you can also say add Python three point seven to the path but we're gonna I'm also gonna show you how to do that this is gonna open up you want to allow this app to make changes your device yes everything is going to install everything was set up now click on close now we're gonna go get PyCharm and go to JetBrains comm pycharm and click on download of course my tutorials gonna work with any IDE but I'm just using pycharm click on download for community which is free or you can pay for it this is going to download then you want to click on open down here there you go do you want to make changes yes click on yes and then you're just gonna click on next and then you're going to click on next and then 64-bit launcher I would go and put checkmarks and all of these things and click on next click on install and everything will install then you want to come in here and you want to reboot now then after a reboot whenever you double click on PyCharm to open it I would click here where it says do not import settings and click on OK check here that you want to confirm this and then click on continue you can either send statistics or not then you get to choose whether you want a dark theme or a lights theme click on feature plugins I will just ignore this and click start using pot charm our charm is going to open you're gonna want to click on create new project you can go and give your project a name and then click on create this guy's gonna open up now you come up here click on right click on your Python and taught or whatever you called your project new and python file you're gonna have to give it a name i called mine HelloWorld and then click on python file then you can just come in here and type in print HelloWorld just to test that everything works and then you come up here where it says run and then click on run whenever you do that this guy is gonna pop up you're gonna go and click on this to run your application or just hit enter and then it's gonna print hello world down here at the bottom of the screen if you want to move it over to the right side of the screen you can just right click on this guy click on move to right bottom and it's gonna go over there and now you have everything a set up and you raise start learning Python okay so I'll be using pycharm for this but you can use anything you'd like and I'm gonna come in here and create a Python file and the extension for our Python files are going to be py I'm gonna call this Python Tut dot py and the very first thing I'm gonna do is to import some modules which you will see later on as the tutorial continues and these are going to do things such as provide different math functions and the ability to handle threading as well as time functions and another function that's going to allow us to do really cool things with a list now the obligatory first thing we're going to do here is go and print out hello world and don't worry it's gonna get much more cool than that as we continue and there you go you can see hello world now as you can see here python statements terminate with a new line by hitting Enter that means that's the end of the statements unlike other languages that require a semicolon now let's say we would like to come in here and receive some input from our user which will be kind of useful when all you do is type in the variable name the way you want to store the input and then type input and whatever the message is you'd like show on the screen and then we can come in and use that data that was stored in the variable name name to say something like hi and then specifically the variable name and you can see right here what is your name shows up and I can type in Derek and it says hi Derek but what would happen if you would like to extend your statements over multiple lines which can be useful well there's two different ways to do that you can either use parentheses like this and what I basically did here is created a variable called v1 get more into variables here in a second and then you could do something like 1 plus 2 and then plus 3 and that's going to just be perfectly legal for you to be able to do across multiple lines and another thing you could do is say v1 is equal to something like V plus 2 and then a backslash and then plus 3 but that's not very common to do I just wanted to cover it because it's a question it comes up you can also go and put multiple statements on one line by using semicolons so you could say something like v1 is equal to 5 and then v1 is equal to v1 minus 1 perfectly legal but things that you probably wouldn't want to do with Python and there you can see the answer comments are going to start with a hash symbol like this and multi-line comments can be created by just going in here and putting 3 quotes and then saying multi-line comment exactly like that and in regards to variables they are just names that are assigned to values and whenever you're creating them just make sure that the first character is either a letter or underscore and then you're going to be able to use letters numbers or underscores and another thing that's important is that variable names are case sensitive so if you create a variable called v2 which is equal to 1 and then you create another one with an uppercase v equals the two which is equal to 1 those are going to be treated completely different you're also going to be able to assign multiple variables all on one line again this is kind of like sloppy code that you wouldn't normally want to do but it is something that you can do exact like that now we're in regards to data types data in Python is dynamically typed and that just means that depending upon what type of data you try to store inside of a variable it's automatically going to determine what the data type is and another thing is that the data can change so you can have V equal to one here and then change it to V equal to a here and that is all perfectly legal all data in Python is an object which I'll cover later and the basic types inside of Python is there's going to be integers floats which are just numbers with decimal places complex numbers strings and boolean and of course there's other types but these are the most basic datatypes inside of Python now if you'd like to get the type of the data you can just come in here and go type and either put a variable name inside of here or a number and it's going to tell you in this situation that you have an integer now there is no limit to the size of integers but if you would like to get a typical max size for integers you could just go in type this max size and you're gonna see you get this great big giant number over here flutes as I said before our values with decimal values at the end and if you'd like to get a maximum size for a float you could likewise go sis and float underscore info max to get that gigantic number one thing that's important to understand however is that floats are only going to be accurate up to 15 digits like most every other programming language of course there are going to be special datatypes that you could use however that can actually give you more precision and you can see I just added these two values together specifically just so I could show you how they are floats are inaccurate after 15 digits and you can see right there is the in accuracy complex numbers are going to be made up of a real part first then you're gonna have plus and then you're going to have the imaginary part and I'll just give you an example of what that would look like and with this we could say something like 5 + 6 J and that is a completely illegal thing you can do in Python boolean x' are either going to have a value of true or a value of false strings are going to either be surrounded with single quotes or double quotes and strings are going to have things called escape sequences so for example let's say that you would want to go and use a single quote inside of a string just go back slash single quotes backslash you could use double quotes of course there's going to be a tab character if you wanted to use a backslash you just double it up and then you have new lines and those are the main escape sequences that you're going to use all the time of course if you find that you're going to be using double quotes a lot because the this begins the string and this ends the string so that's the reason why we need to have that in there because otherwise Python might get confused and think that this is the end of the string and then you have all this garbage over here which will be confusing another alternative if you wanted to use quotes like double quotes and single quotes is you could just go and put three quotes inside of there and then these triple quoted strings are going to allow you to just come in here and put a single quote like that or a double quote like that or what have you without causing any errors now you're going to be able to cast between the different types which can be quite useful and I'm just going to use type here to show that the type indeed did change if you wanted to change something like a float into an integer you just go and put an int and surround this with parentheses and I'll give you a couple other examples to cast to a couple different types let's say you wanted to turn this 5.4 into a string you would do it like that STR there is no character type inside of Python but you could go and convert to a character type that way and here we'll put 97 inside of there and then if you wanted to turn a individual character into an integer you could say if Oh Rd like that then throw a single character inside of there like that and run it and you can see that all of the conversions worked exactly like we wanted them however right here you see that this is actually a string and not a regular character now for output you've already seen print let's say that we would want to go and separate different parts of this output with a forward slash for example I'm going to go in here and type the most important birthday in history and then I'm gonna say a separator is going to be equal to and a forward slash like that and you can see that it went and slapped those inside of there I'm going to get more into output as we go you may have also noticed that there is a newline every single time print execute so let's say that you didn't want that new line all you do is go end is equal to and then go and say that you don't want anything to happen and in that situation you're not gonna get a newline as we'll see here in a second another thing we can do is there is string formatting that is available so let's go now let's throw the newline inside of there and let's say that we wanted to have a decimal inside of here that is going to have leading zeroes of at least four follow that up with a string follow that up with a float that is going to have two decimal places of precision and a character we can do this just by coming in here and putting that percent sign in there and then all of the data in order how you would like it to show up in your output one point two three four and then finally at the letter A and if we run it you can see there's the leading zeroes there's the two decimal places of precision there's the character and there is the strength now we're going to talk about some math functions now Porcia probably expecting to be able to do some math with programming language let's say we have five plus two and we want to go and get the value for that we just go and throw in the calculation we want to perform you're going to be able to do addition you're going to be able to do subtraction multiplication division modulus which is going to get for you the manor of a division and that's just a % going to be able to get the power 5 ^ - just like that and we can also do integer division which is just going to disregard any remainder and you can see all of those values there your is also going to be different ways of performing calculations in a shortcut manner and let's say we have our variable I 1 here which is equal to 2 we could be able to come in and go I 1 plus equal to 1 I 1 just to get some output here and run it you can see the 3 work there and this is going to work for all of these other different operators as long as it makes sense so you could put multiplication and subtraction and so forth and so on and here are a ton of different built-in math functions absolute value max min power ceiling floor round exponents logs square roots and all of the different trig functions we have here as well as being able to convert a degree into radians and to convert a Radian into degrees and not to forget about finding the hypotenuse and if we run those you're going to see the results of all of these different functions and those are just the main ones you're probably more than likely going to use all of the time and feel free to pause your screen to check all of this out or the code is available in a description we're also going to be able to go and generate random values so let's say we wanted to create a random integer all you need to do is go random and I want to create an integer so R and int and 1 to 100 is the area that I want that guy to come from you can see I got a 47 there you're going to see that I got a 29 there and so forth and so on and that brings us to something that is sometimes confusing which is the man value what that means and INF or infinity I'm gonna give you some examples to show you what those mean so we'll go print and if you want to get ahold of infinity you could do something like math infinity and ask if it is greater than zero I think we know the answer to that you can see the true comes back so infinity is just the infinity that you're used to but you might ask yourself what exactly is a not a number or a man well I'll give you an example of a man let's say that you would go and take infinity and you would try to subtract infinity from it does that make sense huh I don't know well Python says that it doesn't and it is not a real number so there's an example of infinity as well as not a number and now I want to talk about conditionals now sometimes you'll wanted to execute a certain amount of code and in other different conditions you would like to execute other code well that's where conditionals come in and you're going to be using comparison and logical operators the comparison operators are going to be less than or greater than less than or equal greater than or equal equal or not equal and I'm going to show you examples here of how we can use them so let's say you have an age of 30 and you want to determine what type of vehicle a person would be able to drive well you could use an if statement to do that so you could say if age is greater than 21 and with this statement you're going to put a colon like that and you can see it automatically indented and that is because Python uses indentation to define all of the code that executes if this above condition right here is true and in this situation what we're gonna say is we're gonna say you can tractor/trailer but you may ask what do I do if I want to check another condition well you just go LF or else if and here we'll say age is greater than or equal to 16 don't forget the colon and in that situation you and drive a car would be an option then after that if you wanted to have a default you would just put else inside of here and then print you can't drive at all and you can see right there that I can drive a tractor trailer isn't that awesome now let's go and talk about a little bit more complicated conditionals using logical operators and the logical operators are going to be and or or not whenever we use and that means both sides need to be true or means that one or the other needs to be true or both and not is just going to convert throughs in two falses so we'll use age here again and I'm gonna say in this situation if the age is less than five what grade should you be going to I'm going to say you're not old enough to go to any so you should stay home else if and in this situation we'll say age is greater than or equal to 5 and here's logical operator and age is less than or equal to 6 well in that situation I'm just gonna copy this I'll say go to kindergarten and we can continue stacking this else if age is greater than 6 and age is less than or equal to 17 well in that situation we can go and perform multiple different pieces of code inside of here but we don't really need to we can have said just say grade and this is going to be an integer that goes inside of here and then if you want to perform this calculation you could use parentheses if you'd like you go age minus 5 and get that answer and then the default answer is just going to be College all right and if we run it you can see that College comes back as an answer and if we would come in here and do something like 15 you're going to see that grade 10 comes back as an answer and you can also say that we don't need this at all all right so let's save that and and there we go another conditional option you have available to you is pythons version of the ternary operator and the way that it works is you're going to assign a value and the value is the first value that shows up here is going to be assigned if the condition is true then you're going to have if your condition and then else and then the value that will be assigned if the condition is false and let me show you an example here I'll say can vote and that is going to be a variable that is going to be assigned the value of true if the age is greater than or equal to 18 and else the value assigned is going to be false and we can say can vote and you can see it comes back as false remember because we changed the age to 15 and there's a rundown of conditional operators we'll see more as the do toriel continues now I want to talk about strings now you saw before that a string is just a variable that is going to be a sign just a series of characters between single double or triple quotes let's go in and show you another one there's also raw strings that are going to ignore escape sequences and how you create that is just put an R inside of there and we could say I'll be ignored and then a new line let's go and do a couple more examples if you want to join strings you could do that just by putting a plus between them you're going to be able to let's create a string here and let's have it be hello you again you'd be able to get the length or the number of characters in that string just by going le n and the string name you want to test going to be able to get a specific character using an index so let's say you want to get the very first character you just go string 3 and that would start at 0 to get the first character you're also going to be able to get the last character just by putting a negative 1 inside of there and we can run that and there you can see there is our output now let's say you wanted to get to the first characters you could just go string three and start at the very first one and then you want to go up to but not include the third index you could also step through so let's say we wanted to get every other character you were going to start at the very first index you're going to go to the very last index and you want to get every other or you want to basically just get the first and then skip the next one get the next one and so forth and so on run that just so you can see that now it's very important to know that you can't change an index value by going in here and going string like this is equal to an A that's gonna cause an error and run it just so you can see say however you would be able to come in here and change it in other ways you could go string three is equal to string three and then call replace and then say that you want to replace hello with goodbye and then if you go in here and try to print that you can see goodbye you shows up you could also slice the front and back and then replace different values so let's say you go string three is equal to string three and you want to go start at the beginning two till the eighth index and then you wanted to throw a Y inside of there and then go and add on the rest of your string by saying that you want to go from the ninth index to the very end and there you can see we went and deleted the uppercase Y and changed it into a lowercase Y we're going to be able to come in and test if a string is in a string so let's say we wanted to find out if the word you is in string three you can see that it is you're also going to be able to check if it's not in there just by typing not in you're going to be able to find the first index for a match so let's say you index just by going string three and finds and if it doesn't find you inside of there it's going to instead return negative one you're going to be able to trim white space from the left as well as the right so what I mean by that is let's say we had a whole bunch of white space like this we could just follow that up with strip to get that and likewise if you just wanted to get the white space off of the left you'd go L strip like that or the right you would go R strip like that let's do some more let's say you wanted to convert a list into a string and then separate it with spaces just come in here and throw in your spaces that you want the list items to be separated by and then join we'll get more into lists very soon and this is how you create a list by the way so we'll say sum and then follow that up with words run it you can see they're separated with spaces you can also convert a string into a list and then define a defined separator or as they are often called delimiters so we can come in and say a string and then you want to split that into a list that is going to be all the values going to be separated with commas and spaces and there you can see it went and converted that those two into individual list items you're also going to be able to format your output with something called an F string so I'm going to go and create two integers I'm going to do it the sloppy way int one and two just to remind the you that you can do that and five and then I'm going to say print and then you just follow that up with an F and you can use single quotes double quotes doesn't matter then you could go and put the variable name directly inside of there and the other variable name is equal to and then you can perform calculations directly inside of here in exactly the same way and there's your answer going to be able to convert from uppercase to lowercase so let's go create a string and let's convert every character into lowercase and then convert every character into uppercase and these are convenient to be able to use even though if you're not used to programming all the time they might seem like kind of silly things they are very useful going to be able to find if a if a string is either a character or a number so let's just go in here and we could go 1 2 3 like that and then follow that up with is all numb let's also show you how to do this with other different things we can check if everything is a character or not so we'll just leave it like this and we can go is alpha and then also is its 8 digit and I'll just leave it that way and how you check if everything is a number is go is digit run it and you can see that they all come back as false however we get rid of the spaces that are inside of here and change this into 1 2 3 and run it you can see they all come back as true ok so cool stuff whole bunch of things you can do with strings now I want to talk about lists now lists are going to contain mutable pieces of data of varying data types or even functions what's mutable mean that means it can be changed immutable means that a value cannot be changed so let's come in here and create list 1 and you would be able to do stuff like go and throw an integer and a float and a string and true or functions or whatever I'm going to show you that later going to be able to come in and get the length or a number of items inside of it again using the same function we use previously le n with strings do a couple other things you'd be able to get a value at a specific index again we're gonna start at the very first index which is 0 just put l1 inside of there and 0 likewise you're going to be able to get the last index value and how do you do that one take a wild guess you type in negative 1 and there are your answers you're going to be able to change values unlike strings just like that you could also come in and change multiple values so we'll go l1 and let's say 2 up to 4 not including 4 and you want to instead put Bob and false inside of there we're also going to be able to insert at an index without deleting l1 and then go to and - that's where we're gonna put it and we're gonna say Paul and 9 are gonna go in that position you'd also be able to go l1 and insert at the second index and Paul like that if you'd like to use that instead be able to to add to the end here I'm gonna create a brand new list and just go l1 + and we'll throw a new list at the end of it by going egg and for you'd be able to remove an item by going remove and let's say you don't like Paul there he's gone could also remove at an index by using pop and here we'll say the zero index is what we want to remove and we can print that out to see all the crazy changes there they all are okay we would also be able to add to the beginning so list two is equal to and all you got to do is throw it in there and what you want plus l1 you can also create multi-dimensional lists so let's create a new list and all you're going to do is just put these brackets like that and then one and two and then like this and then three and four and then you'd be able to print those out so let's say one one is what you're looking for just go l3 and in the brackets you put in the different indexes that you want to look for and you can see four came back there we can check if a value exists or not by using in again so we'll say one exists and then inside of here we'll just say one in l1 or you could use not in you're going to be able to find minimum and maximum values so we'll go men these data types have to be the same though so we'll say min and then throw a list inside of here to check against and also maximum values and just change this to max of course you'd be able to slice out individual parts so let's say we to get the first to just go l1 and start at the first up to the second index but don't include it in their results and we can step through here or define the step as well so let's say we wanted to get every other item just like we did previously l1 once again and zero and then negative one to go to the end and two and we can do some cool stuff to reverse the list also just by going l1 and then putting two colons followed by a negative one and run it and there you can see how all of that output works out for you so pretty neat stuff pretty quick rundown of lists more is coming now I want to talk about loops now very often you're gonna want to can go and execute code over and over and over again and a while loop works great for that one rule for the while loop is you have to go and define a index outside of your while loop and basically it's going to execute as long as this condition is true you can see the colon there again you can also see the indentation everything that's indented is going to execute over and over again you just have to make sure that you are going to work your way towards this condition being false and that's what that code there does for us and you can see it prints one the whole way to four let's go and create a more complicated while loop though so I'll go w2 is equal to zero and here we're gonna say Wow w2 is less than or equal to 20 what we want to do here is go and print out even numbers so we can use an if statement here to go w2 and modulus two and if it's equal to zero well that means it's an even value and we want to print those out so we're gonna go print w2 then after that we can say else if w2 is equal to nine well in that situation we want to stop looping how you stop looping before that you know regardless of whatever this condition is is by using the break statement and then we could say else and w2 plus equal to one remember that's the same as if we would go put the both above w2 plus 1 or plus 1 like that okay that's just shorthand notation and then we could skip to the next iteration of our loop after that but remember right here we need to increment this otherwise this loop is gonna last forever so in that situation we're just gonna go w2 is equal to one like that and then we know that our codes gonna work and it's only going to print out even values which is exactly what it just did now I talked about lists before but I didn't tell you how to cycle through a list so let's go and create ourselves a list and we're gonna show you a neater things you can do let's just go just to prove that we can throw anything in here we want and I'll cycle through it with a while loop you can go wow length and L for : and then we can print out all of these different items what we're actually gonna do is pop them off of the list so they won't exist anymore but there we got what we wanted and that's a good run down a while loops maybe do a little bit later now I want to talk about for loops now for loops are gonna allow you to perform an action a set number of times and so let's go and create one here so I'm gonna go 4x in and I'm going to use a range function here and basically range is going to automatically kick out the values from 0 up to but not including 10 and let's say we wanted to print out all these different values let's throw a space inside of there so that they're all on one line we will also go end and like that and then afterwards we can say print and just whatever and there we go and you can see it printed all those values up to the value of 9 do okay there's all kinds of for loops let's go and create another one and create an another list just so we can cycle through it you could also cycle through the list with for-loop by going for X in l4 and then print out all those values and that's a non-destructive way of doing it versus the pop that I used before which actually deletes the list or at least it clears it out you can also define a list of numbers to cycle through so for X in 2 4 & 6 & print those out and there you can see they are and that's a good rundown of the for loop I'm gonna do much more with it as we continue now I want to talk about iterators now python is going to allow you to pass an object to a function called ITER iterator which or or iterable which is going to and then return an iterator which is going to allow you to cycle through values so let's go create a list and we'll just go 6 9 and 12 iterator is going to be you just go l5 and now you can just go print and each time you call next on that iterable it's going to kick out a value and you can run it again the next time and you're gonna get the next value whoops and you can see there it is alright so quick rundown of iterators now let's talk about ranges now you saw the range function before and basically the range function let's say we wanted to create a list of integers yeah you could do that just by this and then list followed by range and 0 through 5 and there you went and created it we're also going to be able to define steps inside of there so let's change this to 10 and then throw a step inside of there you see how that changed and of course we're going to be able to use our for loops X in range and we'll say something like 0 3 and then for Y and range and 0 and 3 again and then what I'm gonna actually I use this for is to cycle through a multi-dimensional list so I'm going to go numb and list is equal to 1 2 3 10 20 30 100 200 and 300 and then tab our way in there and then we can say print and number list and then the values of x and y as they change run it and you can see it prints out all those values ok so a whole bunch of examples with ranges now I want to talk about tuples now tuples are gonna be just like lists except they are immutable which means they can't be changed and let's go and create one whoops there we go alright so let's go throw our values inside of here again just to show you all of the different types if you'd like to get the length of it guess what you just go llen and whatever the tuple name is we can go and get them by indexes let's get to the very first one out of there which is at the zero index we can get the last one out of there in exactly the same way if I put negative 1 we can go and get a slice or the first two items out of it just by going 0 index up to but not including 2 we can step through it again every other and that would be the first index to the ending index and step through it two at a time and we can also reverse it so basically anything you can do with a list you can do with a tuple as long as you don't try to change a value and you can go and play around with that and mesons see all of the differences between lists and tuples because now I'm going to talk about dictionaries now dictionaries are going to be lists of key value pairs and the keys and the values can use any data type however duplicate keys aren't allowed so let's go and let's just create heroes this dictionary you're going to start those with curly brackets so let's go this is the key and this is the value put a period and then you can throw another one inside of there and Bruce Wayne okay so there you go you just created a dictionary now we could also come in and create another one or this is another way of creating dictionaries you could go dict like that and then you're basically going to do this same thing again here we can say lex luthor's secretly known as Lex Luthor and we'll throw another one inside of here you also can have another villain named Lukey whose secret identity is or low-key I mean is equal to low-key all right so two different ways to define dictionaries but I prefer the first so that's what we're gonna stick with now we're going to be able to get the number of items using length again and then le N and heroes what else can we do with them well we can get a value by passing in the key so we'll say heroes and if we want to get these secret identity of Superman that's how easy it is you're also going to be able to add additional heroes inside of here just by referencing a new key and then the value that you want to assign to it so let's throw Barry Allen inside of there but then let's say whoops I just realized I spare I spelled Barry Allen wrong I can change a value just as easily just by going like this and then change this a into an e you can get a list of tuples from a dictionary by going print and then list and then heroes and then get rid of this part right here and instead put items you'll run that there they are and you can see all of them they're also going to be able to get lists and keys so let's go get this guy here so both of them in there and here what we're going to do is just say that we to get the keys as well as our values and there you can see we just got those specific things that we asked for we're also going to be able to delete these items just by going delete and let's say we don't want the flash run anymore boom he's gone we would also be able to remove a key and return it so here we'll say heroes and pop and the specific we want to pop is Batman and that's gonna also return it we're going to be able to search to see if a specific hero exists so we're gonna say we're searching for the key of Superman in heroes and we'd also be able to cycle through heroes so heroes : and we'll just say print k and also we would be able to get values cycle through and get values in heroes and values : and print V and we'll do one more you'll be able to do formatted printing with something called dictionary mapping let's create a new dictionary here to demonstrate that so here I'm going to go and define name and bread is going to be the item we have here and price and the price of bread at least right now is 88 cents what we're gonna be able to do is say print % followed by whatever the name is in the dictionary so names cost and then I'm gonna throw a dollar sign inside of here another percent and price and let's say we only want that to be two decimal places Long's follow that up with f4 float this s is gonna stand for string of course and then what we'll be able to do is go percent and dictionary and it's automatically going to map those values into the right place so pretty cool stuff you can do with dictionaries now want to talk about sets now a set is going to be a list that is unordered only has unique values and while values can change those values themselves must be immutable so let's go and create some how you do that you can go set like this and then the individual values you'd like to throw inside of here well you're going to be able to create two sets in another way as well I think you can tell which way I prefer like that and let's go and get our size for our set again same thing as before length and I'm going to throw s two inside of there let's go and get a couple more you're going to be able to join sets just by going s1 and the or symbol there and s2 and store them into the new guy we can print that out just so you can see what it looks like going to be able to add values so add and what's throw Doug inside of there you're going to be able to remove items as well so we'll go s3 and discard and let's throw Derek out of there does he's quite annoying you can also route remove a random value which is kind of crazy so random s3 pop and the reason why it's random is because it's an unordered list you can add values will go s3 once again but remember everything is going to be unique so anything that is it unique is going to be automatically deleted you would be able to come in and return common values and how we do that we can go s1 and enter section and s2 let's go and run that see all of the different output that comes from that and just the way this works is only items that are in S will or in s2 that are in s1 or what comes back not vice-versa you can also get all unique values by going s1 symmetric difference and s2 we'd be able to get the values in s1 but not in s - by going s one difference s - we would be able to clear all the different values that are inside of a set with clear and another thing is there's something called a frozen set and a frozen set is a set that can not be changed in any way so all the things that I just showed you yeah I can't do those anymore but if you'd like to create one there it is and you made one and that's a rundown of sets and now I want to talk about functions why'd it take us so long to get here basically functions are gonna provide code reuse organization and much more so let's go in to find one how you define a function you go and define D EF and let's say that you wanted a function that just adds two values and returns them well let's call it get some and it's gonna get a number and now I could come in here and define the data type using a what it's called a function annotation but you don't need to do this as you're gonna see but if I wanted to I could do that otherwise I would just go num1 like this and num2 but I'm going to use a function annotation instead just to be different another thing we could do is provide a default value you don't need to do that either but what the heck let's do it just because we want to try all kinds of new things so there it is again : and then indentations going to define how big our function is and it's going to return the value of adding num1 - num2 okay and then the reason is squiggly lines here is because I don't have two spaces okay that's why that's there now what we can do is go get and sum and throw five and four inside of there and you can see that nine comes back as an answer all right so there's a simple one now let's say we would like to be able to accept multiple values in which we have no idea how many there's gonna be well let's go and create get some two if you don't know how many values you're gonna get just put a star and args and you can have multiple different values that you do now just as long as this here comes last so just remember that so let's go and some an indeterminable amount of different values we're gonna create a variable there and then we can just go argh in args and then say sum plus equal to whatever the value of Arg is and then after we do that we can then come here and say whoops return sum now once again we can come in and get that value just by going get some two and then throw in as many values as we would like and if we run it there's a syntax error and the reason why is I didn't have a closing parenthesis and now you can see we got our results all right cool stuff let's do more complicated ones what happens if we'd like to return multiple values well you could store them inside of a list and then return that but I'm going to show you another way so we can go next to let's say we want to get a number and then we want to return the next two values that come after it we can just say return and num1 or num plus 1 and then follow that up with num plus 2 and then we can go I 1 and I 2 is equal to and get both of those values from it and we could throw a 5 inside of there and then we can call print on this and then we could just say I 1 and followed by I 2 and you can see it gets you those values you would expect do even more complicated things let's say a function let's say we would like to make a function that makes a function that multiplies by a given value what I'm gonna do is call this mult by and it's gonna get a number passed inside of it and then what I'm gonna do is create a thing called an anonymous or an unnamed function and how you do that is you go lambda is how you create an anonymous function and this function is going to receive a value of x or an attribute with the name of X and multiply that times X times the number passed inside of this function alright so that's all that does and now what we're going to be able to do is go 3 times 5 is equal to and then we will call mult by let's surround this with parentheses malt by and this 3 is actually going to call the multi function right here and the num is going to have a value of 3 and then outside of that for the function that's going to be returned we're gonna throw the 5 inside of it and when we run it you're gonna see it works ok I need stuff let's so and crew let's say we want to pass a function to a function well we're gonna go define malt list and this guy is going to receive both a list as well as a function and then it's going to use the function to perform an operation on the list so I'm going to say for X in list and print whatever the function does to that value then what we can do is I can say malt by 4 is going to be equal to malt by and 4 like that and then I can go and call the malt list and create a list from a range from 0 but not including 5 and then after that we can go and pass in our function which is called malt by for cool stuff and you can see that worked as well alright so another thing I told you is you'd be able to actually create a list of functions and I'm gonna let you play with this on your own but let's say when to have power list is equal to you'd be able to go lambda X X to the power of 3 let's have that be 2 and then do it again so lambda duh and you could throw as many functions in here as you would like and you can go play without on your own to see how you can cycle through those list items and have them for formal kinds of calculations so cool stuff and now I want to talk about a related topic which is map now you can use a map to execute a function on a list in a similar way so let's go one two four and this is gonna be a range one to five and you could do something like times 2 is equal to lambda which receives a value of x and then x times 2 what we'd be able to do then is go print and list and map and then pass in the function which is x 2 followed by the list that we're working with which is 1 2 4 whoops this is x 2 got it and there you can see it went and generated that list all right and you can play around with map maps a ton of fun let's talk about a related topic which is filter basically what filters gonna do is allow you to select items based off of a function or a condition so let's say we wanted to print out the even values from a list we could go print and list and filter and then we'll say lambda gets a value of x and then it takes X modulus 2 and if it's equal to 0 then it's going to say it's good otherwise it's gonna say forget you and will go like this and like that and boom and you can see we only have our even values so neat and then another cool related guys reduce and basically what reduce is gonna do is receive a list and return a single result so let's say we wanted to add up values in a list we could go print and reduce and once again lambda gets a value of x and then it's also going to get a value of y say you can use multiple different attributes with these and it's going to go and take each of those individual values add them up and it's going to use a range and we'll say one through six there we are and there we go and you can see it went and reduce those two one some value so cool stuff now we're going to talk about exception handling now through exception handling you're going to be able to handle errors that would otherwise crash your program and what I want to do here is I want to create an infinite loop which means it's basically going to execute until break is reached and any time we would expect an error could occur what we want to do is surround that code with a try block so what I'm gonna do here is ask the user for a number and let's put input and let's convert this into an integer and put this here and go please enter a number and now what I'm gonna do is I'm going to throw break inside of there and then what I'll do is the is basically handle if they do not provide me a number so the code inside of an accept block is basically going to provide an error message to set things right and then we can either target a specific error using like a value error which is what this specifically is going to throw so we can go accept value error and then print a message out on the screen like an error message such as you didn't enter a number or what we could do and this is not really a good idea but if we want to catch all exceptions we just put except with nothing and then we would go an unknown error occurred and then after we do this we can come out and say thank you all right and if we execute this you're gonna see please enter a number and then we can throw five inside of there and it says okay that's fun or we could come in and do something like throw an A inside and you can see it says you didn't enter a number and then you see please enter a number and it's going to continue doing that until the user agrees to do the right thing all right so a simple way of handling possible errors using exception handling now I want to talk about file i/o now I'm going to talk about databases here in a minute but we can actually save and read data directly from files and what we're gonna do is we're going to start the code which is going to guarantee that the file is going to be closed automatically by using with so we're gonna say with open and we're gonna name our file my data dot txt and then follow that up with the mode that we will be using with this file which is going to be writing and if you put an A inside of there that would be appending so it's going to overwrite it then we're going to say what type of character set we're gonna use which is going to be utf-8 which is a standard and keep going with this line and I'm gonna call this my file alright so now that we have that file open or we have it created what we're going to do is to write to it we'll just say my file and right and then just throw some text all right so now that we have that in there what we want to do next in and the width is going to automatically handle closing this guy off now we want to open the file for reading so we're going to say with open and again the same exact file and again encoding is going to be utf-8 as my file we can actually just go and throw this up here it's gonna fit on the screen so I do that now what we can do to read that information and print it out to the screen you can just go my file and read and there you go and then if you want to guarantee that the file is going clothes you just say my file and closed and if we run it you can see that it wrote the text and then it printed the text back out and you can also see here it says true that means the file closed and there is a pretty simple example of how we can use file i/o now I want to talk about classes and objects now basically real world objects have things like attributes such as height and weight and they have capabilities such as having the ability to run and eat well in programming languages what we do is we model objects real world objects by defining those attributes and capabilities except we call the attributes fields and we call the capabilities methods or functions now I'm gonna create a class that's going to model a square and so I'm gonna first off create the initiation function and this is going to be called every single time you try to call or create a new square object and I'm going to go and put a height inside of here of zero and a width of zero also just as a default in case the user does not enter anything and then if we are going to want to refer to our object because when we create square objects we don't know what the user is going to name them so if you want to refer to the object itself you refer to to or you use the word self to refer to the objects you're working with so it's like one of those chicken or the egg type of situations so there you go whenever you create a square it's automatically going to initialize the height and the width now if we want to protect or provide some type of special output every time the user tries to get information what we are going to do is create a getter for it this is a getter function and how you create one is you just list out whatever the name is for the item you want to get and then you could do something special like saying retrieving the height ever or you could just verify that they're trying to give you really good data and if not block it if you're using setters and otherwise you just return the height and here is a setter to go and create that you go height and setter and then once again you go define and height and whatever the value is like I said you could go and block it if they're giving you some type of bad data I'll give you an example of how that would work if the value is a digit well then we will say it's okay to assign it and you just do it like this and there is our value and otherwise if they do not provide us with a digit we're going to tell them that we're not going to accept their value exactly like that and there is an example of getters and setters and what else can we do well we can do the same type of thing using the width as well so let's just go and copy all this and let's paste that inside of there and then here we'll just change this to width change this the width and with note that the width and I'm not going to bother changing those strings and everything else there is fine did we do this the right way duh and change this the width and now we have getters and setters for both that are going to work for us and what else can we do well let's we can define a capability or a function or a method or whatever you want to call it by call it creating get area and what it's going to do is just return convert this into an integer self width and put these two underscores here ahead of time multiplied times and again we're going to turn this into an integer self underscore height and now what we'll be able to do is create square objects so let's just call it square how you create an object from a class you just list the class name exactly like that if you want to set the value see I don't I didn't have to put those in there because I put defaults in there of zero so now what we're gonna do is set them so I'm gonna say height is equal to ten and then we'll do the same thing for our width and then what we can do is call our get area function and how you call a function for an object is just put the name ahead of time and get area run it and you can see the area's 100 exactly like we would have thought now I want to talk about inheritance and polymorphism well we're gonna create another class here and I'm going to call it animal creates another and knit function why do I keep typing self and we're just going to have this be simple we're gonna say a name which is going to be unknown by default and then a weight which is going to be 0 by default so that's going to initialize all of our animals and now property and do this for the name that's going to assign this name and to do so we'll just so self name is equal to whatever the name is and we'll throw a function inside of here called make noise which is just going to return ger as a default and if you would ever want to cast a object to a string type you can define that just by going define and string and then define what the return is going to be and here I'm just going to go and throw in all these different parts is a and I'm gonna tell you what type of object it is and says and then we will have the Gir show up there on the screen and I already showed you format earlier in this tutorial now what I'm gonna do is I'm gonna go inside of here get the name and throw it in the first curly brackets and then if you want to get the type object you can just go type self and then follow that up with name and that's going to give you the type of object you're working with and I'll put that on the screen and then I'm also going to call they make noise function which is going to put Gurr out on the screen and that looks good and now I'm going to show you something else cool I'm going to talk about magic methods basically what they they're used for operator overloading which basically just means that we are going to in this example I'm going to show you how to define how to evaluate if one animal object is greater than another animal object so to find and this stands for greater then I'll show you the other ones here in a second so what this is gonna do is get the object itself we're working with and then a second animal object and it's going to define if one is greater than the other one by checking weight and like that's and animal two and underscore weight and as you're gonna see you're gonna be able to work with inherited functions as well and we can return true in that situation and else we can return false of course and other magic methods you're gonna be able to work with there's equal not equal to less than greater than and they'll just make it so that you can go and have your objects work with each other I mean you can multiply them you can do all kinds of cool stuff alright so I just leave that to you to play around with it's a very fun what else are we gonna do well I think that's good enough so now what I'm going to do is show you how to create another class dog which is going to inherit all of the methods and fields that Arian the animal object just by putting animal in there just like that and now what we're gonna do is go and create another initialization function and this guy is going to be passed a name which we're going to put as unknown it's also going to get an owner and it's going to be passed a weight which will just have be zero after this if you want to go and call the original animal and knit function and have it initialize everything this is how you do that just animal in it and you can call all the animal functions or whatever directly just by putting animal ahead of it and put all those things that it can handle right in there and then you can handle the things that aren't in animal directly inside of here so animal can only work with things that it knows about it can't work with fields or whatever that it does not know about now let's say I would want to overwrite the string function up here how would I do that well I'm just gonna copy it because a lot of this stuff is gonna wow you know what I'm just just good like this and copy this first line cuz most of this is the same okay and throw that in there and what we're gonna do first is call our super version which is the animal version of string and like this STR and then that and then what we can do is go and is owned by and then follow that up with the owner which I'll just go to the next line and that is received by going self and owner and we got dog set up so now what are we gonna do we're gonna test this just to see that it works so I'm gonna create an animal objects called animal and how we do that is go animal and we could say spot is the animal's name and it weighs 100 pounds and then we are going to let's go prints animal that's going to call our our string function we debt we created and then we're gonna create a dog and it is going to be and doesn't have to be called dog can be called anyone but this part has to be dog and we'll call the dog Bowser and we will say that Bowser has an named Bob and Bowser weighs 150 pounds and we can Prince the dog function and then we can also go and test the magic method by saying Prince and is animal greater than dog and if I didn't make any errors you can see that it works and there it is spot is an animal says Goro Bowser is a dog and says girl and is owned by Bob and false all right so cool stuff and in regards to polymorphism with python polymorphism is gonna work a little bit differently from other languages in that functions are going to basically accept any object and then just expect that the objects gonna be able to provide the needed method so this isn't something really to dwell on Python just does polymorphism without having to think about it all right so cool stuff now I want to talk about threads okay so basically a thread it sounds complicated really isn't a thread is just a block of code and whenever we are going to be using them the way that I'm using them here which is basically creating them having code execute and then go to sleep for a random period of time and then wake up and start executing again this is just called threading and you need to import that module there to make this work you're also gonna need random whatever just keep them all alright so what I'm gonna do is create a function and it's going to be called execute thread and it's going to be passed a number and what we're going to do here is use a function called string F time or string formatted time and what this is going to do is allow you to define how time is going to be displayed so we could go in here st our f time and then go in after that and put in % + y + % + m + % whoops maybe put another dash inside of here for our day and you could go in and put our hours and let's what else is it minutes and seconds exactly like that and then to get the time you just go time and GM time and then throw in some curly brackets and I'm gonna show you here exactly how that's gonna work for us so what we're gonna do is we're gonna say print and then thread and get the number of the thread sleeps at and then get at what time it's gonna sleep and then format and we're going to put the threads number inside of there and then we're going to go and get this guy right here let's go and grab it and throw that right there and we will say time dot and string F time and I'm just gonna do hours minutes and seconds because it doesn't really make sense to do years and stuff I just did that up there just to show you that you could do yours and I think I got rid of all the extra let's just hope it got right or I did it right we'll find out in a second all right so now what I'm gonna do is generate a random sleep period between 1 and 5 seconds so I'll say random sleep time is gonna be equal to random and this is going to be an integer so random int 1 2 5 seconds and then to actually pause or sleep the or cause this the thread to go to sleep you just go time sleep and random you define how how long you want it to sleep and that's what it does it's just gonna sleep for you right there and then let another thread go and execute and now what I can do is go thread and this is just outputting information stops sleeping at and then the time that it's gonna stop and then we can say format this is just going to be exactly the same as the other guy that we just add some ice just copy it grab all this stuff copy and paste that inside of there and then get rid of these extra parentheses and now I'm gonna go and actually create all of them so I'm going to say for I in and I'm gonna create ten threads and then you'll get to see them start and stop and basically each time through the loop a thread objects gonna be created and then you're gonna pass it the function to execute and any arguments to pass to that method and then the arguments passed of course it must be a sequence which is why we need a comma here which you're gonna see in a second so I'm gonna go thread and threading dot thread and then target and the guy we want to execute which is going to be called execute thread say there's the function we're executing and then args is gonna be equal to whatever the thread number is like I said and then you put the comma in there because the arguments passed in must be a sequence and then we will go and just have that thread start executing by going thread start and then what we can do outside of this is basically just display the active threads so just so we have some stuff on the screen bouncing around so we'll go threads and then we can get threading and we'll just get the number of threads currently with active count and then we can come in and return a list of all the active thread objects thread objects by saying threading enumerated whoops where do I have an error at one thing I forgot to put enumerate Ronna and there you can see there are the threads starting up and there they are going to sleep alright and one thing to notice here is the threads are going to be open in order so zero one two three four and but how because we put them in here random sleep times they are going to sleep for a random period of time okay so I just wanted to give you a brief overview of threads I cover them in much more detail in my main python tutorial and now I'm gonna give you a rough overview of regular expressions and then follow that up with databases okay so basically regular expressions are going to allow you to locate and change strings in ridiculously powerful ways and basically they work in almost exactly the same way in every single programming language and I'm just gonna give you a overview of simple things you can do with them in Python I have other tutorials that cover regular expressions in mass detail one thing we're gonna have to do here is import another module and that's re and let's just go and check for ape in a string so I'm going to say to do that you go if or E and search and this is a very basic thing where you're looking for an exact match of the word ape and then we could say something like the ape at the a pax and after we do that I could say something like print there is an ape and if we run it there is an ape shows up okay so that's a very simple one and let's say that you wanted to get every single match you could use another function called find all so let's say go all apes is equal to our e and find all and ape and once again I can go and put this inside of here right there and there we got it and now what we can do is go for no get rid of that I in all apes and then print out all the Apes as you can see there's actually two Apes inside of there and save it and run it and you can see there's two Apes okay give you another example what we're going to be able to do here is get an iterator for all of the matching objects so I'm gonna go the string is equal to and I'll throw the ape at the apex and then I can say for I in or II find it er 8 + 8 is what I'm looking for here I'm gonna put a dot inside of here and that actually means that I'm looking for a followed by a single character so I'm gonna put the string inside of here that and then what we can do is store our results here in a tuple equal to I and span is gonna return that tuple and then we can print it out so focal ball and then what we can do is slice our match out so the string and 0 and 1 and you can see apex comes back and you can see also that it comes back there because that dot is going to match both for apex as well as with the space after ape okay so like I said just real quick a couple cool things you can do with regular expressions you can do so much more and I have tutorials that cover it and now I want to talk about how to interact with an SQL light 3 database using Python now a database is even better than storing a simple file because it's gonna make it very easy for you to organize your data for storage as well as fast searching we're gonna need a couple more modules here I'm going to also go and imports SQLite 3 and you can work with any database I already have assists and I'm going to import CSV ok Thomas separated values ok so what I'm going to do here first is create a function called print database and what that's going to do is retrieve data from a table using select if you know what select is if you're used to using data basses if you're not I have tutorials and we're going to potentially have problems here so I'm going to surround it with a try block and for this guy I'm going to let's go and get our result which is going to be equal to and we're going to go the cursor execute and then you can throw in the database command you want to work with and I'm gonna come down here and actually create the cursor actually you know what I'm going to go and actually create the database outside of this function then I'll come back to the function here in a second okay so DB connect is equal to sqlite3 Connect and inside of here I'm going to put a database name if it doesn't exist it will be created and the file by the way is going to be stored in the same directory as your current Python file and let's say we want to come in here and say database created just to show that it's there and the cursor is gonna be used to traverse the records of a result so that's why I stopped doing all that stuff in that function so I could explain what all these things do so we're gonna go DB connection and to get the cursor you just go like that and that we have it and what else do we want to do well I think we can jump back up here and work with this some more okay so here is a database command that is going to select the ID first name last name age address salary hide higher dates from a table called employees and whenever you do that result is going to store a list of all of those lists so we go for row in result and cycle through them and print them out and that's all you do to get those all out it's a list of course now I want to handle any potential errors that I could have so I'm gonna go accept SQL Lite three operation air and you would get this more than likely if a table you're trying to all doesn't exist so I'll say the table doesn't exist and otherwise I'll catch anything else and in this situation this normally means that we couldn't retrieve data from the database so let's just throw this inside of here and couldn't get data okay so there's that so we'll save that and continue down here so the next thing I want to do is I want to add a new employee to our database but what I need to do first is actually create a table now again there is the potential for an error so I'm going to throw this into a try block and then what you do to execute all of the different queries you want to make with your database is database connection and execute and I'm going to CRO and throw another block of SQL inside of here to create the table employees and here is everything so to create a table you're just gonna go create table the name of the table and then you're going to list out all of the different types as you can see here what we're gonna have first is an ID that is going to be an integer it's going to be the primary key we're going to use to help search through as well as organize the individual pieces of our database that ID is going to be automatically created for us and it can't have a value of null you're gonna have a first name which is just going to be text again not null just means it must have a value again same thing for last name age as an integer not and all again means we must have it address is going to be text we don't have not null so you could nullify that don't use it and salary a real number that just means a floating number and then hire date which is just going to be treated as simple text once again so that's how we go and create a table and then what we want to do is go and commit that change to create that table so we're going to go DB connection and commit and now we can say that the table was created all right and what else do we want to do well we're gonna have to handle any potential errors and more than likely the area you're gonna get is sqlite3 again operation error and more than likely if that comes back that just means the table couldn't be created so we're just gonna go like this and throw that table not created how about that okay and what else do we got to do here well while it's let's go and insert some data into our table and how you do that is with a SQL command called insert into so we'll go DB again and connection and execute and the command and what we're gonna do is insert into employees you're gonna have to list in order all of the things that are coming and then you're gonna list all of the different pieces of data you want to be stored inside of there all surrounded by parentheses and separated with commas alright so there it is and save that once again you're going to commit the changes to your database so copy that and commit and you could say the Union employee was entered or whatever you want to do but what I want to do is just go and print the database information we have so there's print DB and then after you are done working with your database you want to close it and how you close it is just go connection close and if we run it you can see that yes indeed it worked you are working with Python using a database all right so now I'm gonna teach you about recursive functions as well as modules at the same time I went and created another function and I just called it my func py and what we're gonna do is create a recursive function inside of here and a recursive function is just a function that refers to itself now one thing that is very commonly done with recursive functions is calculating factorials so I'm going to define a function inside of here called factorial it's going to receive a number and every recursive function must contain a condition which it ceases to call itself so I'm going to define that right now so I'm gonna say if num is less than or equal to one well in that situation we're not going to call the function anymore we're just going to return the value of one and then we're gonna say else if that is not true what we're gonna do is say result is equal to num times and here we are calling the factorial function and we're gonna say num minus one and then we will say return the result so pretty simple stuff so exactly how this is working is the very first time we come through here let's say we give it a value of 4 well automatically the 4 is going to go here and this is going to be factorial 3 so we're going to have the 4 there and then we're going to automatically then take the 3 automatically go that will go down here then this will become 2 and then we will take that 2 and bring it down here 2 times factorial 1 in this situation we reached our default here where we are no longer calling the factorial function we get a value of 1 this 2 goes from here up to there this 6 goes from here up to there and that's how we get the final value of 24 for the factorial so now I'm going to show you how to use this function over inside of our other program so to import this new module there's two ways we could do it we could go import my funk I'm going to show you both ways and then we could come in and go print my funk dot and then the name of our function which is factorial and then we'll throw the value of 4 inside of there and run it and you can see we're able to use the function in the module instead of the other way around the way you can do this is you could say from my funk and then list what you specifically what you want so we'll say import factorial and then you will be able to just hack this my funk part off of there like this run it and still get the same results okay so that's modules as well as factorials now we are going to develop a calculator and using TK and so I thought it'd be fun to actually develop a real application here to end this guy so what I'm gonna do is go get TK enter which is going to allow us to dude GUI development inside of Python so that's all that I'm doing here so get all of those things then I'm going to define a class for my calculator and then I'm going to describe every single thing that I'm doing but I'm not gonna go into tons of logic just because I'm restricted with time the very first thing I want to do is have some place to store the current value that is in our entry window for our calculator so I'm gonna call this calc value give it a value of zero point zero to start off then I'm going to have to track what buttons have been pressed or not so I'm going to have the division trigger and it'll be set for false and then I'm gonna have the same for all of the other different mathematical functions so we'll have division and we will have Maltz multiplying and we will have addition as well as subtraction what else should we have here well we're gonna have to handle the event in which a button was pressed so I'm going to define a function called button press and it is going to be passed a value representing that button I'm then going to get the current value that is inside of the entry window which is where the last number that the user entered is going to go and I can get that by going number and entry dot get which is going to be a window inside of my GUI interface that's the name for it I'm going to want to put any new values to the right of whatever is already there so for example if they go and click in the calculator for the value of one and then they type in two I'm going to put 12 up there instead of just you know put you know putting a two inside of there or whatever and how I'm going to do that is go entry and Val plus equal to whatever that value is I'm then going to want to clear my entry box and how you do that is go self and number entry delete and I'm going to throw a zero inside of there and then end then I want to insert my new value going from the left side to the right side how we do that it's go self and once again number entry insert 0 and then the entry value whatever the value is I want to put it inside then I want to check if a value is a float or not and how you do that I'm gonna call this is float you know what is float and it's going to be passed a string so there it is then there's a potential problem here so I'm going to create a try block and in a situation in which a string isn't a a float it's gonna throw a value error so I'm gonna say this and then the string value that was passed into our function if this is a float I want to return true otherwise if there was an exception I want to return false and the exception is going to be a value error just like I said before so I can just return false then I need to create a function that's gonna handle all the logic whenever the different math buttons are pressed so I'm gonna call this define math button press and it's going to be passed in whatever value or whatever button was pressed I'm only going to want to do anything with these math functions if there's already a number inside of the entry window so I'm going to say if self is float and then I'm gonna get convert whatever is here into a string and then I'm gonna go and get whatever is in the entry window just like that then I'm going to make all of my triggers automatically meaning all of the different mathematical function buttons I'm going to turn all of those to false so I'm going to have ad trigger and give that a false value and I'm gonna do the same thing for all of them so this subtraction trigger also is going to give a false value and then I'm gonna go in and find out which one was actually triggered they're false and then finally the division trigger also going to be set to false now that I have that I can go in and get the value out of the entry box so that we can do our calculation so I'm going to say self and calc value is equal to and convert this into a float so once again we're gonna go and get our entry value then I want to set the math button click so whenever equals is clicked the function is going to know what calculation to use so I'm gonna say if the value is equal to a division well then I'm going to go in here and just go print pressed and that's gonna show up over in my console window and then I can say self div trigger and mark that as true meaning that the division trigger was clicked on and then I'm going to do the same thing for all of these other different guys so here I'm going to go else if value is equal to and here I'll check for multiplication and then I'm gonna do the same thing I'm gonna just copy all this and paste that inside of there and then I can copy this and use it later so I've got multiplication I'm gonna change this to multiplication show that shows up is for debugging purposes change this into multiplication meaning that the user clicked on the multiplication button paste this inside of here and then I will check for an addition and then addition and then addition and I thought that would go into place but it didn't throw it like that and then we can go down to the next line throw this in there and then change this to subtraction and change this to subtraction and change whoops change this to subtraction and then we'll also change this to else to assume that that was division and then get rid of this one right here okay so I got that all set up then I'm going to come in and clear the entry box so I'm going to say self number entry and you're gonna see here in a second whenever I define the entry box and end and that's going to handle all of the math button presses so let's create another function make sure this all lines up here real nice and the next function is going to handle whenever the equal button is pressed and whenever the equal button is pressed that means that we need to perform our calculations so I'm going to call this equal button press and I don't need to do anything it's not going to get past any attributes first thing I want to do is make sure a math button was indeed clicked so I'm gonna say if and so and this is all inside of the calculator class just so that you're caught up here so I'm gonna say if that and then or one of the other ones let's do subtraction or himself and multiplication or and then if none of them are clicked when we're not going to do anything so this and division is the last one so if one of those was indeed true then what I want to do is go if self the add trigger was clicked on well then I'm going to go and store inside of solution whatever the calc value is currently + and I'm going to go and get whatever the entry value is and add it or add those two things together okay and I can just go and copy this because I'm gonna be doing much the same thing so let's come down here paste that there and then I'm just gonna change this to else if like that and then change this to subtraction and then change this to subtraction and go down to the next one and paste this in there change this to else if we know one of these is true don't know which one it doesn't matter multiplication and then change this to multiplication whoops chase the multiplication there we are and else since we know that one of them is in true well then we can just go and assume that the last one is division just change that to division and we got that set then what we could do is go and print out some information so we can do something like print self calc value and then go and get the self entry value what where the user entered in their information so self and entry value and go and get it and then throw in another space after that and put in our solution just so we can see what that information is on our screen oh and this guy gets out of there and that's all fine okay good after that we want to clear our entry box and put in our new values so we're gonna say number entry delete and throw zero inside of there and end once again and then go and put in our new value so we'll say number entry and insert and zero and then we'll put in our solution solution a rides so all of our logic is handled for our program and now what we do need to do is just design our user interface so whenever we create a new calculator in the initialization function we're going to say that route is what we want to define and then we need to go and actually define our entry value which is going to hold the changing values stored inside of entry so I'm going to say so entry value is equal to fun string for route initial value after we do that I can define the title for my application by going route and title and we call it calculator and I have to put this inside of quotes that we are I want to define the height and width for my window how you do that is go route G on the tree and this is going to be 430 by 220 I'm going to mark resizable as false so I do that resizable and width is equal to false and height is also going to be equal to false that will lock down the dimensions I'm gonna customize the styling for my buttons by defining style and to get that you go ttk and style and then I just put in all of the configurations so I say style configure and tea button I can define the font equal to serif 15 and I can define my padding to be equal to 10 so there got that what else can we do um spell button right that's something that's good there we go and I can also go style and configure for my entry window TN tree again define what font I want to use I'm gonna say serif 18 again and padding is going to be equal to 10 and then I just need to create my text entry box which we've been talking about so much and you do that just by going number entry is equal to TT okay entry route and then text variable is going to be entry value that's what we've been referring to over and over again in the code above so entry value and I can give it a width that is going to be equal to 50 and there we go we got that defined then the way that I have everything set up here or the way that TK enter has everything set up is we're going to use a grid system for putting everything in place so I'm gonna go number entry and grid and I'm gonna say that I want this on the zero row or the top row and then I can define and have everything broken down into four columns and I'm gonna say that I want this to take up every single available column which is going to be equal to four then I need to come in and define all of my buttons how you do that for all of the mathematical calculation buttons as well as the buttons for each of the numbers so this is going to be button 7t TK + button and again you just type in root I can define the text that I want associated with that button which is going to be the number seven I'm gonna go command is going to be equal to and I'm going to use a lambda here and tie into that self button press and pass in the value of seven and then I just need to define once again the grid where it's going to show up so it's going to be in the second row entry boxes in the top row so I'm going to go row is equal to one and column is going to be equal to zero it's gonna go zero through four so zero column yes or like that alright I'm basically gonna do the same things for all of these different buttons I'm gonna get this out of the way here so I can just do it for all of them and I went ahead and typed in all of them for every single one of them you can see here whenever the number button seven eight and nine are clicked on we call self button press we call the function button press and you can see how we have Row one column zero one two and then this is column three so we're just working across the column zero through three you can see that the rows are the same for all of these grid items you can see right here whenever we click on the math button for division we're gonna call the math button press function instead and the way that that works across the board and you can pause your screen here and go and type all of that stuff out just as you can see it right there and exactly how it is or you can get the code that's in the description whatever you'd like and I'm gonna finish this off so come down here and what I need to do is define route which is going to be the main window object so all you have to do is go TK like this and then I need to create my calculator I'm gonna call this calc and call calculator to create a calculator object and I pass root inside of it and then I just go main loop like this and this is gonna run the app until the user says they want to exit and we can save that and one little change I made up here in geometry I change this from 500 to 250 and save that and then we come in here and run it and you're gonna see like a little window open up down here you're just gonna click on it and you're gonna see here is our calculator we made quick and dirty and you can just come in and play around with it 2 times 5 is equal to 10 divided by 2 is equal to 5 and so forth and so on so hope you guys enjoyed this tutorial I covered as much as humanly possible and like always please leave your questions and comments below of the last til next time
Info
Channel: Derek Banas
Views: 185,888
Rating: undefined out of 5
Keywords: Python Tutorial, Python 3, Python, Learn Python
Id: H1elmMBnykA
Channel Id: undefined
Length: 111min 21sec (6681 seconds)
Published: Tue Aug 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.