Python Full Course - Learn Python In 3 Hours [2021]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to this video this tutorial is a python full course we are going to be learning python programming from scratch so this is a list of everything we're going to be talking about in this video so let me give a brief introduction to python so python is one of the fastest growing programming languages in the world with python you can go into various fields like machine learning and ai web development and plenty others so taking to learning scores and choosing to learn python is a very good decision for your programming journey so without wasting any time let's get straight into the tutorial so right here i'm just on my browser so we're gonna download python on our laptop so to do this i'm just gonna search for download python for windows since i'm on the windows and i'm going to click on the first link and then right here i see download python 3.9.1 which is the latest version so i'm just going to click on it and then you can see here that it's downloaded it automatically so already have python downloaded now what i just need to do is to install it so i'm just going to come into where i saved it and then i'm gonna run it so make sure that when you're installing python you must tick this add python to part if no you're just gonna make it more complex than it's supposed to be for you so let's click install now yes and so this is just going to install python on our machine it should take a few minutes a couple of minutes to install and i'm gonna be back when it's installed finished so right now the setup was successful so as you can see the installation has been done so i can just click on close now for me to confirm if i have python installed i'm going to open up my command prompt so here in my command prompt i'm just going to type python once i type it you should open up this shell you might not understand what all this is now but this is just to verify if python was truly installed so now that we know we have python installed let's just go ahead and install our code editor so python is the programming language but where we're going to code where we're going to program everything or the python we're going to do it's going to be in a code editor and there are various code editors out there we have visual studio code we have pycharm we have sublime text we have atom there are countless out there but for this tutorial we're going to be using visual studio code so let's go ahead and download it so i can close up my command prompt right now so let me just say that if you don't know how to get your command prompt you can just search for it on your windows search bar so right here we can just go back to google and then we can search for let's say download visual studio code and i'll just click on the first one so since i'm on windows i'm going to download the one for windows if you're on ubuntu or linux or mac download the one for your os so right now you can see that it is downloading so i already have it downloaded i have it on my pc i'm just going to cancel that since i already have it downloaded let me just show you the installation process so right here i have it downloaded already we just click on enter that's to run it so what it tells me here is that setup has detected that visual studio code is running so this simply means that i already have visual studio code installed on my computer so i'm just gonna press cancel but for you as a beginner you want to install this as a new software so when we already have this installed we're just going to open up a visual ceo code and this is what it should look like once you first open it up so now we have everything set up we downloaded python and installed it and then we downloaded visual studio code and installed it now we can go straight into coding so now that we have everything installed what we just want to do is to create a new file and this file is where we're going to code all our python so in visual studio code we're just going to come into the toolbar and click on new file so once we click on new file you can see it automatically as has this untitled name but we want to save it by pressing ctrl s and i'm just going to save it first of all let me say all files and then now to save it as app dot py so the name of the file is app but at the bottom of the file extension i used dot py so anytime we are working with python anytime i want to create a new python file it must always have the extension dot py that will make whatever we're running on know that we are using a python file so now once i save it it's updated here and as you can see visual studio code automatically detects the logo of python now that's how to save creates and save a new python file so this is basically how everything looks like in visual studio code on the left hand bar we have a folder but that's where if we are opening a folder this is where we're gonna see all the files and folders in there we have this search this github we don't need to bother about that for now but this is how visual studio code looks like so since we have everything we know how to create a new file what we want to talk about now is the print function so in python we have some built-in functions that means some things that are already done automatically which we don't need to write too much of code now let's say we just want to print uh text to the screen like we just want to show a user like hello world or welcome we're going to use the print function you're just going to print a basic text to the screen now first to do this we're going to say print and then we're going to open parentheses and then we can use single codes or we can also use double quotes there is no problem here in python but we can just go with single quotes so you can just pick one and go with that so in this codes i'm going to write my text and i can say hello world [Music] now as you can see i have printed hello world and i must make sure i save this file and then for me to run this python file for me to see the output of my code i'm going to have to come to the right hand corner right here and click on run now once i click on run you see that it opens a new terminal automatically and our code is going to be run in this terminal so if we just wait a second we're going to see the output of this code you can see right here that we have hello world automatically it prints hello world if i say hello world welcome and i run it you see it changes and it says hello world welcome so that's how to run a python file now let's talk about some more features about this print function now let's say we want to print on a new line like hello world and we want this welcome to be on another line we can easily do that we can just remove these and then on a new line we're just gonna print welcome exactly the same thing now once i run it you can see it says hello world down here and it says welcome so that's how to print on a new line we can also do a lot of things let's say we want to add another value to this hello world like let's print a sentence let's say my name it's tommy and then i want to say my age is then i can add a comma right here and say hundred so this will automatically tell python that this is a number and it's no more a string just a normal character so now once i run it you're gonna see it says my name is tommy my name is android with no errors and automatically you see that it leaves a space right here right here we didn't leave a space but automatically once we put this number here it leaves a space to clarify that if i put don't leave no space and i run it you see that the space is there so that's just a smart picture of python so i think that's going to be all for the print function now let's talk about variables in python as you can see here i have three print function the first one says team is a boy the second one says team is 18 and the third one says team is from turkey so as you can see in the three print functions i'm repeating team three times now we can use variables instead of repeating a particular text now variables are just basically saving a data in the memory of python and then we can get that data back by referencing its name so let's see this in action let's say above the code i have a variable named name so this name is the name of the variable and i'm saying equals to i put single codes and the name is tim so name is the name of the variable and team is the value assigned to this name so like this we've saved this variable up here so we can print that variable once you say print whenever we are printing a variable we don't put code like we do when we are printing the number string so we can remove the codes and just type the name of the variable which is name so now when i run this you're gonna see it prints all these and on the fourth line it prints team it didn't print name now this is what variable is about it's just basically saving a particular data and then referencing it through the variable name so now instead of saying team three times we can just replace this theme with name and for us to do this right here you see we printed only the variable and we didn't have code but right here we are printing string with code and we also want to print the variable with it so this is called concatenation we are going to join the variable and the normal string together to do this let's remove theme and we're going to say name which is the name of the variable plus the remaining text now this was a name plus is a boy it's gonna print us team is a boy so we can also do the same right here we just simply remove team i'm gonna say name plus is 18 we do the same for the third line we say name plus it's from turkey now once we print this you can see it says team is a boy tim is 18 tim is from toki but right here we said name so that's what variable is about so let's just type cls to close this up but variable is more broad than just this so there's something we call data types now as you can see this is just team team is letter is a character and that is a string so string are basically just characters now string is a data type we have if this is a number like 23 is no more a string is now an integer we have boolean which is for true or false values and others but for this tutorial we're just gonna be particular about maybe two or three so now if i want to let me just undo that let me specify a new variable and name it age now this variable i want it to be the age and let's give it 18. so whenever we are using an integer or a number we don't add code when we are assigning the variable you see right here we added code and inside our code we put team but for it to be an integer you will see that we do not add quotes if i over on these you see that it says 18 is int which stands for integer and then team right here is a string so now we can just join these by saying plus h now once we print that it's still going to give us okay so you can see now that this is can only concatenate string not integer so now there's a problem with python anytime we want to concatenate a string with a variable it doesn't i mean an integer with a string it doesn't work so we can just solve this instead of using this we can say comma h once we print this you can see it says team is from toki so let me close this up right here we can see anytime we're concatenating with a string it's possible but when we are dealing with an integer we can't use this plus we have to use comma and we can also use comma when concatenating with a string also so this is the two things we can use addition sign with only string and then we can use comma with string and integer so if i run it it's still going to work team is 18. now that's basically how to concatenate and join strings with variables now we're going to be talking more about strings so strings are just plain text so if i print something like print i this is a string string i just plain text now we're going to talk more about the features of strings different things you can do with strings now let's say i say i i put a full stop our u now once i run this you see that it runs it on the same line which is i our u but if i want this to be on another line without inputting another print function i can just simply use a box slash n this is a backslash n now this is gonna automatically take this to a new line it's just like a break it's gonna add a break in between these and this so now once i run it you're gonna see it says i how are you but we have the space here because of this if i cancel that and i run it again you're gonna see we have i our u so we can also print string as a variable so let's say we have something named [Music] name and the name is team so this is a string is a variable but a string type of variable and then in prints function we can also print that string variable by just saying print name without quotes once we print it you're gonna see right here we have team printed so let's just close this up so if we come up here we can also print let's say we want to add a double quote or single quote now you know we can't add a single quote in between our text that's because python is going to see this like we want to close up this print text so for us to add the double quotes we can just easily add the backslash then the code now once you have a black slash and that's code this code is going to be printed once i run it you see now we have i with this codes our u so if you want to put special characters we use the backslash or you can just print the backslash alone it's gonna work so you can see the backslash is there so we can print use the backslash to break to make it on a new line we can also use the backslash to add a quote and then we can print the backslash just like that now let me talk to you about special functions on strings so functions in python is just a block of code which does a particular task so in this string string has a lot of function which does different things let's say we want to convert all the characters in this to uppercase we can use a string function or we want to check if it's lowercase or once we get only the first letter or the second those are functions in python so now we can just say we want to get only the first letter now i want to get the first letter we can just easily do something like print name square bracket and zero now zero represent the first letter if i say one it represents the second letter if i say two represents the third later so in python on programming in general numbers start counting from zero so once i put zero and then run it down here you see it prints only t now let me run two which is the third one you see it prints only m and if i run what is not there like 0 1 2 if i try to run 3 it's going to give us an error index error so you can see string index out of range which means it's not present there now can also do multiple things let's say we want to convert this all to uppercase we can just do name dot upper case [Music] now once we run this it says has no attributes of package so sometimes we can run into error we just change it so i think it's because there's not supposed to be case so name dot upper is the function that changes it to uppercase so once i run it you see now that we have team in uppercase we can also do the same thing with lower we just say name.lower and then we run it now you can see that everything is being changed to a lowercase now once we come back here we can also check if everything which is present here is uppercase or if all the characters are lowercase so for us to do that we're just going to say name dot is lower so once i run it you see it says false so it's going to give me a true or false response if everything is uppercase it's going to give me false but if it's lower it's going to give me true so you can see since there is an uppercase t here i asked if it's lower and it says false but if i ask if it is upper [Music] and run it is this is false that's simply because there's uppercase and there's lowercase not everything is upper or lower but if i change it to a lowercase let me change everything to upper cim and then i run it you can see now it says true because everything is uppercase and then we can also join different functions together now you can see if i say something like is lower and i run it it says false but i can first of all convert it to lowercase then ask if it's lower so i can do dots lower [Music] and then run you see now it says true but it's uppercase so first of all it says name.lower which means convert it to lowercase then it checks if it's lowercase it gives us true so that's basically how to just join different or add different functions together and then we can also do something else by let's say we want to get the amount of characters we have in this variable to do that we can say len then we open a bracket and close it when we run this you can see it gives us three because we have one two three if i have a lot and then i run it it's gonna give me 18 the number which we have in there so that's how to get the length of a string but let me just undo that but let's say we want to find where a particular text is so this is i let's say we want to get the index number of i as we know this is zero and one so this i is index number is one if we want to get it we're just going to say len we're going to remove len so i'm going to say name dot index [Music] and then we're going to say i so now it's going to print me 1 because that's the index number of i once i run it you see it prints me one which is the index number now let's say we want to replace a text let's say where we have m we want to replace it with c so we can easily do that we're just going to say is name the replace we're going to open a bracket parenthesis then the text to replace we want to replace m we're going to put m and then what we just want to do is comma and then what we want to replace it with we want to replace it with t so now once i run this you can see it's print t i t so that is replacing this m with t so that's basically how to replace so there are also a lot of functions a lot of string functions but these are just some i showed you and how to use them so that's going to be all for strings in this part we're going to be talking about numbers in python so to print a number in python is pretty easy we just type print and then we just go ahead and type the number 78 now once we just run it you'll see that it prints 78 so we don't need to add any syntax or anything we just print 78 and it gives us 78. now we can also specify a number as a variable you can say no the number is equals to 79 we just write it like that we don't need to add these and then we can print number so once we run it it gives us 78 and 79 now we can also add numbers so let me just remove this so instead of just printing 78 we can print 78 plus 22. now we can add a number we can perform arithmetic operations once i run it you see that it gives me hundred it automatically adds these two numbers or integers together and then it runs it i can also try this with a decimal number let's say 0.7 is like this then once i run it you see it gives me 100.934 so that is how numbers work and we can also test it out with the subtraction you see it gives us 55.066 and then we can test it out with the division gives us the answer and then we can also test it out with the multiplication when we run it it gives us the answer so that's basically a basic arithmetic operation with python well we can go for that just what we did with strings we can go for that and use the built-in number functions so let's say we want to show the remainder of a division so let's say something like 20 divided by six now you know that this will give us three remainder two so it's gonna give us 3 remainder all of these but what we just only want to get the remainder we don't want to get the main answer we can just do 20 percentage sign 6 once i run it you see it gives me two which is the remainder now that's how to get the remainder of a division we can also convert a number to a string now let me show you let's say we have this variable we have this variable named number equals to 55. you can see that it's a number an integer right here then i can specify a new variable i can name it number 2 and i want this number 2 to be a string so i'm going to say string of number variable so now if i over on this number two you're gonna see that is a string when i print [Music] number two it's going to print 55 but now we just string it's 55 also but it's a string so how do we know if is really a string remember that when concatenating you can concatenate numbers but you can concatenate with string so let's try to concatenate so right here let's say [Music] number is let's try to concatenate with the main number so let's say add number and let's run it you see it gives us an error can only calculate any string not integer so but now that we've converted it let's try to concatenate with num2 once we run it you see that gives us number is 55 so that's to show that it has converted an integer or that number to a string now python sees that as a string this is also very useful in a lot of cases so that's that and then we can also get the absolute value of a number so let's say let's just cancel this let's print minus five now obviously when we print this it should give us -5 as you can see down here let me just close this up but if i wanted to print only 5 regardless of the sign if it's positive or negative i'm going to add a b s now this is a function it's a number function as i explained earlier function is just a block of block of code which does a particular task now we can write a function manually by ourself but python has some built-in function which you can automatically use that's why when we use abs it automatically sees it as a python function now this abs means absolute that's the full meaning of abs so it's going to get the absolute value of this number now when i run it you see it sprints only five regardless of this negative sign beside it so we have four and two so now you know that four is obviously greater than two so once i print these it's gonna give me four this is showing the highest number of this so that means uh 4 raised to power 2 i'm not foreign about that this means we have these two numbers max is gonna get the highest number so let's say we have another number named three once i run it it still prints four because four is greater than all of these but now what if i have 16 once i run it it gives me 16 because 16 is the highest number in this range of numbers i can also do the same to get the minimum amount of numbers i can say m i n so that is going to automatically print two because that's the minimum so once i run it you see it prints two so that's the basic way of getting the maximum and the minimum and then we can also round in number two so we can estimate a number so you know um in normal mathematics if we have something like 3.2 this is estimated to three but if we have something like 3.5 this is estimated to 4 we can also do this here in python first to do this we just say round [Music] if we round 3.2 it's gonna give us three but once we round three point five it's going to give us four just normal estimation rounding off to a number and then we also have one which is bin let's see bean so what this bin does is that it converts a particular number to a binary string now if you know binary numbers is this weird type of text so each number has its binary string so if you want to get the binary string of a number you just run it and it prints this now let's run something like 334 it prints the binary string for 334 now that is some basic functions but they are countless functions for countless number functions so all these we're not able to access them because we're able to access them because they are already built in python but there are still more that we can't access except we import now there's something in python called import now we have more number functions the one that i can able to find the square root the one that can get the power variables are countless so now the reason why we can't access this one because we haven't imported them the ones i just show you we can use them without importing but the ones i want to show you now we need to import them from the python matte function before we can use it so for us to import we're going to say from math something like this import all but instead of writing all right asterisk so what this is doing is saying from the math class or the math function import everything which is there now in this math class or function it might they might be the one that can find square roots they might be the one that can find the power everything so we don't know the one we want yet so we're just importing everything with this aesthetic so now we can since we have this imported we can find the square root now we can just say square roots of let's do something like 100 and once we run this it should give us 10 as you can see it give us 10 that is this point zero it's okay but now it gives us 10 so that's gonna be all about working with numbers so in this part we're gonna be getting users input so what we're going to be doing is to tell a user to input a text and then we're going to save that text in a variable and then we're going to tell the user what's the input so let's go ahead and practicalize that so to use an input in python it's pretty easy we're just going to say input then open and close parenthesis now it is open and close parenthesis we're going to ask the user what we want aim to input so we're going to put the codes and in that code we're going to say this is something like input your name so now this is going to tell the user to input your name and then once the user type in something like team or tommy or john anything users type we want to save it in this variable called name so now this variable called name is gonna be the input so now let's try that we run it you see now the first thing it says is input your name let me say tomy and i click enter so nothing happened but that's how to use an input so let's just close we can come back here and just simply print to the user name so let's run it what is our input name and say team you see that i just print steam so we can make this more interactive by asking for the user's name and maybe asking for the user's age and then telling the user your name is this and your age is this so we have we need to have two variables the second will be age so name equals input your name and then age will be equals input your age so what we always want to print is your name is and they just want to add the name and they will close that and they will say and you are the age [Music] years old now when we run this it says input your name let me say john and he's 105 years old so he says your name is john and you are 105 years old so this is basically how to just collect simple data from the user and then use it the way you want to tell the user um your name is this your age is this so this is the basic concept you can also link it with how they do websites like when you create or sign up to a website and then they save that your name your email and everything in the database and they later tell you welcome john or welcome whatever your name is so just we can link that concept it's basically just getting a user input and then storing it as a variable in this case as a data and then we can print it to the user back so that's how to all you need to know about getting users input but we can also make it more fun now when we get a user input we can see that the age is seen as a string right here you see age is string but we want the h to be integer so we can convert this string into an integer so first to do this i'm going to say int [Music] we open the brackets and then we close the brackets so everything is inside this integer function if i come back here you see now that it has been changed to integer normal string but now if we try to run this i input my name and i input a random age you can now see it gives us this arrow can only concatenate string not integer so for us to be able to add age you know what we need to do we need to either convert it to a string which we don't want to do or we can just use comma so let's just remove this and then let's run it so let's input a random age right here random name and h so now you see it works it says your name is john and you are 13. now that's how to basically just get user input use it how you want and then you can pass the information back to the user in this part we're going to be doing a simple python exercise so what we're going to be doing is a simple word replacement program so what is going to be happening is that a user is going to input a sentence and then let's say use i want to change a word in that sentence or change something in that sentence then we're going to allow the user to be able to do that so if a user inputs a sentence like i am a boy and he later wants to change that boy to let's say a guy or something else so that's what we simply want to do so we're going to ask the user for three inputs the first input will be the sentence the second input is going to be what the user wants to change and then the third input is what the user wants to replace it with so let's get that and practicalize it now the first thing we're going to do is just to have those variables so let's first have a variable name sentence [Music] and i want this variable to be an input [Music] and then i'll just say enter your sentence [Music] now let me just print the sentence to the user let's run it now let's say the sentence i am a boy and he just simply prints that sentence we can come back here let's just say your [Music] your sentence is i am a boy which is going to be whatever the user writes so then what we now want to do is to have another variable let's say word one it should be an input now this variable is the word the user wants to remove or replace and then we can say enter word to replace [Music] let's say the word to replace and then once we have that it's going to be stored in this variable named word1 and then we're also going to have another variable let's name it word2 this is going to be what the user want to replace it with so let's say enter the what to replace it with so now once we have all these we're just gonna print sentence dot replace that means we want to replace this with this open a bracket to take two inputs which is word one the word we want to replace and then word two what we want to replace it with so what this is doing is that let's say we have a sentence named i am a boy then this word one is the boy we want to change and then word two is what we want to replace it with so let's just go ahead and test it so right here is running okay it gives us invalid syntax that's because we forgot to add a plus right here now let's do that again right here you can see it says entire sentence now let me say i am a boy now it brings us your sentences i am a boy and it says enter the word to replace so i want to replace this boy and enter the world to replace it with i want to replace it with dude now you see it prints i am a dude so that's basically how to do a simple word replacement exercise it takes in an input which is a sentence and then it takes us another input which is what we want to replace and then the third one is what i want to replace it with and then it basically just prints that to the screen so i hope you understand what we did in this part if you didn't you can just go back and watch this part again and then make sure you understand before moving to the next one so in this part we're going to be talking about working with lists in python so when you're working in python you're going to be dealing with a lot of data and you want to be able to know how to fix your data into a list so a list on python is basically just a list of different attributes of different values fixed into a variable so let's just dive straight into the practical now to define a list is similar to defining a variable so we can just give it a name of a list let's say a list of countries and i can name the list countries and i'll just use the equal sign and the square brackets when we are defining list we need to use a square bracket and i can just give you some inputs like united kingdom [Music] and i can also give it another list like let's see ghana and let me say nigeria let me give it one more value and i can say australia so now this list a list variable it has four values so now once i print the list like this i'll just say print and i give it countries which is a list you see that it just prints me the old list the way it is right here so that's basically the main thing you need to know when defining a list but this is more broad than this there are lots we can do with lists like let's say i just want to get the first attribute now each list has their index number like this is 0 1 2 3 and so on just the way i said earlier in this tutorial that in python or in programming general numbers start counting from zero so the index number of this value is zero one two three it's just similar to when we have a variable let's say we have a variable named name and we have tell me so this has an index of 0 1 2 3. now that's very similar to a list each value has an index of 0 1 2 3. now we can just delete that now let's say we only want to print these we don't want to print the whole list i can just do it like this countries with square brackets and i'm fixing the index number in between those square brackets now when i run this you see now that it prints me only united kingdom i can also do the same thing let's say i want to print nigeria i give it to i run it now you see that we have nigeria there are a lot of things we can do now let's say um once we get this we just want to print only n from it we can also do that so after saying countries index number two we can specify another index number saying zero so once i run it you see it prints me n now what does this mean it says countries get the value with the index number two one two so now it has nigeria and then i say from that nigeria gets me the one with the index number zero and then we see in nigeria n has the index number zero so that's basically how to just add some simple things in python python lists now let's say we just want to get only um all the lists from ghana to the end we don't want to add united kingdom or we just want to get the list from nigeria to the end we can also do that so we just say countries if we want to get from ghana to the end we can just say our square bracket one and a column now this is gonna get everything from index number one till the end now let's make sure we close this when i run it you see now it gives me the least without united kingdom that's how to just get it from a particular index number to the end we can also do the same thing we can say from two you'll see that it gets me from nigeria to australia right here nigeria and australia now i can also specify a range let's say i only want to get the list from one to two okay let me add one more value here let me say new zealand [Music] yeah so right here i said new zealand and this is zero one two three four let's say i only want to get from one to three i don't want the last one and i don't want this i can easily do that i'll say from one which is from ghana index number one then after the column i'll say three so one two three once i run that you see okay it gives me ghana and nigeria so this is zero one two three okay so let's say to four and when i run this it gives me ghana nigeria and australia so it gives me from here all the way to three so this is basically going to get from one which we started to the one before which we end so that's how to do that in python and then we can also get the type of the list now if i print something like type countries now it's going to print to me that this is a list down here so let me just quickly type cls to clear everything i've done now let me print it you see it says class lists so this is showing me that this variable type is a list now if it's just a normal variable let's say a string variable is also going to show me that it is a string now let's try that let's just divert from list for a second and let's say we have name [Music] tell me and i said the type of name when i run it you see down here it says a string if i change this to 12 you see down here it says integer so that's how to get the type of a particular variable so that's how we know now that this is a list variable now i can just simply delete that and then let's say i want to change the value of one value in this list now let's say i want to change this from united kingdom to united states so what i'm just going to do [Music] is to just say countries 0 because the index number of united kingdom is zero should now be equals to united states and then let me just add a print let me print countries now when i print countries okay you see name is not defined that's because we've removed this variable that was code name so let's just remove this line and run it again you see now the first value is now united states meanwhile is united kingdom right here but that's simply because we changed it to united states here we can also do it again let's say we want to change australia which is the third we'll say three and let's say we want to change australia to a country like canada and when i run it you see we have united states ghana nigeria we don't have australia again it's not canada and then we have new zealand so that's how to change a particular value in a list now let's say we want to get only the end of this list you know we can also get it by just let me just quickly delete all this we can get new zealand by just typing countries with its index number which is zero one two three four new zealand index number is four if i type four and run it it gives me new zealand but i can also do it in another way now if i add a negative sign right here let me delete this and i say one now this is gonna get the list from the last value in the list now if i run this you see it gives me new zealand if i say minus 2 -2 is obviously australia if i run this now you see we have australia so note that when you are using a negative sign it gets you that list from the last from the bottom basically but you notice that from the beginning we use 0 for the first and then when we're starting for the last we use minus one yeah that's because as we know in normal math zero is neither negative nor positive so when we start from the back we use one not minus zero so when we start from the front it's zero when you start from the back it's minus one so that's how to basically use that now there are also a lot of other things we can do with list like we can get the length of this list now let's say we want to get how many values we have in this list because most of the times you have large amount of data can start counting them one by one like these but you can just say len countries which is the name of the list now once i run it you see it gives me five which is because i have five values in here but note that when it gives me the answer it doesn't give me based on index value now index value is just like the id assigned to each of these values right here but when i calculate the length it gives me the actual amount we have here which is five but if i have to calculate it from the index number it will be four i hope you understand that you can also do some other things like in a list as you can see all of these are strings now instead of printing strings let me just delete this new zealand instead of printing only strings i can change let's say this gunner i can change it to a number like two so now we have a string we have an integer if i print these okay this gives me four so let me remove length once i print this you see it gives me what i want to see if i print the index number of this which is one and i press run still gives me two so we can mix different data types in a list i can also change this nigeria to true which is a boolean value once i run it let's remove this i run this it gives me that so there is not going to be error nothing is going to conflict we can just easily do that and you remember that we checked the type of a list so right here once i run this you see that it gives me class list which means this variable is a list but let's say i want to check the type of the values in a list like i want to check the type of this value or the type of this value now as you know this value is a string this is an integer this is a boolean value this is also a string let me overrun it you see it gives me int with this integer and once i over on this let me see okay it doesn't give me anything there but this is a boolean value and this is a string so let's say i want to get this i'll just say type countries square brackets and i give it its index number now when i run this you see it says class is a string let's say i want to get for the second one which has an index value of one once i run it it gives me integer let's try for the third one it gives me bool which means boolean so that's how to basically get the type of a string and we can also do some other things like let's say the last thing i want to show you for this part is another way to define a string or let me say to assign a string i mean a list sorry so instead of using the square brackets we can also use something we call lists but now when we are using this list constructor it's called the list constructor it is used to construct a list we are going to use square brackets again we are going to use normal round brackets like this but now so let me just delete this line now let's use the list constructor to specify how to construct a new list so you say list and i'm going to use normal brackets parenthesis and it has to be double so when i'm using the list constructor it has to be double like this now in here i can have all my values like nigeria i can have 34 i can have false and once i print these okay we don't want to print type we just want to print everything countries now you see it gives me everything which i want to see so that's how to basically use the list constructor to specify a list so we can print a list in two ways the normal um the second list constructor and then we can also print the with the normal way without list like the square brackets like these let me say countries to so now if i say type print the type of countries you see it's gonna give me list and then let me also print the type of countries too just to show that both of them are still a list so list and list so that it will be all about the basics of list in the next video we're going to talk more about list attributes there's still a lot of things we can do with lists in this part we're going to be talking more on lists specifically we're going to be talking about lists functions or list methods in python so in the last part i showed you about lists and plenty things you can do at least plenty list attributes in this video we're gonna dive more deeper now let's say we have two lists the first one is list one and this list one is just a bunch of numbers one two three four five and then we have list two and then we want this list two to just be let's say a bunch of fruits so we have banana and then we have um apples mangoes [Music] and then let's see arranges [Music] so now we have these two lists now let's say we want to join these two lists together like we want to print it together we are going to do something like list one [Music] dot append dot extend i want to extend it with list two now once i print list one what this is going to give me is the this joint with this so let's print it right here see one two three four five then banana apple bangle oranges so you just give me these joined with this the first list joined with the second list so that's what extend does it combines the two lists together but let's say we want to add a value to the ending of this list like oranges um we want to add what other one do we have i think we have cherries but we just want to add something else to these we can do something at least two dots append and then what do we want to append to it let me say cherry now once i print list two it's gonna print me all these with cherry now cherry is part of this list you see now that i picked me bananas apples mangoes oranges and it prints me cherry now if i say i want to get the length of list two now we have four here but since we appended one it should give us five now you see we have five so that just seems it's a simple way to append a value to a list and it might be very useful also but let's say we want to put in a value in between one of these like we have we want to put in a value in between banana and apple this cherry we want it to be right here something like this now if we want to do that i'm just gonna say list two dot insert and then this is going to take two inputs now it's going to take the index number the index place we want to put it want to put it in one and then what you want to put there is cherry now let me explain this further so it says list two dot insert we want to say in the index number one that means right yeah then cherry should be right here so zero one we want it to be here we want to input cherry now when i print cherry [Music] let me just run this you see now we have banana then we have cherry in between so in between bananas and apples we have cherry so that is how to insert a value in between a list but now let's say we want to remove a particular value from that list we can say this 2 dot remove and let's just remove bananas [Music] now once i print list two it's gonna print mean list two without bananas and that's very easy it's just by saying remove then whatever you want to remove but now let's say we want to do something like we want to delete everything in this list want to clear it up we can just say list2.clear and nothing is going to be in here once i see list2.clear this is going to be empty it's just going to be an empty list it clears everything deletes all the values in there once i run it you see now it prints me an empty list so that's how you just delete a list very easy but now let's say that we have these values and the mango we want to get the index number of mango as we know this is zero one two so the index number of mango is two but we want to get it let's say we have thousands of values in this list i want to get with the mango is located in that list i'm just going to say print list 2 dot index of mango so you're just going to print the index number of mango let's just run it right here you see it says 2 0 1 2. so it tells me where mango is located in this list which is very very helpful now let's say we have a value appearing more than once so we just want to know how many times the value appears in this list now it's very easy what we're just gonna say is list dot count mango so now if you tell us one because mango only comes in this list one time once i run it you see here we have one let me come here and input mango again [Music] now once i run it this time you see that we have two because mango is in our list two times so that's how to simply do that but now let's say we have a list of numbers and let's say they are scattered like let's say we have one yeah [Music] and let me just delete all these we have four five let's say we have three here so four three five one and let's have two at the end so let's say we want to print this list in ascending order you know as i said in python you might be working with a lot of data and we have thousands of numbers that are scattered and we want to print them accordingly let's say from 100 we want to print to 250 to 350 accordingly not scattered we can easily do that what i'm just gonna say is list one dot sort like this then right here i'm just gonna print list one now once i print it it's not gonna print it four three five one two it's gonna print one two three four five in ascending order now you see now we have one two three four five this also is very useful when working in python but now let me just remove this mango we don't want to have it twice that was just for testing but now let's say that we want to print a list from the back we want to reverse the list we want to print it from the bottom what we're going to do we'll say list 2 [Music] and dot reverse it's very easy we just release two dot reverse and once we print these two you'll see that it's printed from oranges then to mango to apples and to bananas let's run it now you see oranges mango apple banana now that was how to easily print a list from the bottom from the last value in that list well now let's say we want to duplicate a list like we want to just take this list and copy it again or something like that let's test it for list two let me delete this so let's have another list named list three and let's say we want this list three to just be exactly the same thing with this a duplicate of list two so what we're just going to say is list theory can be equals to list one or list two i mean just copy so you're just going to copy all the values in these two and then paste it in this three now once we print this three and run it we have exactly the same thing we get when we print list two so it's exactly the same thing and then we can also do something more fun like let's say we just want to delete the last value from this and just print everything else we can do that by saying we can use that by saying the pop method so we can just say list2 [Music] list2 dot pop so when we say list2.pop it's just going to remove the last value we have in that list let's just delete this we're going to remove the last value we have in that list and then relieve the rest so when i print these two now you should have only three values so now you see we have bananas apples and mangoes no more orange but let's say i want to be specific we won't want to delete you remember that we can just remove a value by saying this to dot remove and then we can just specify it here let's say banana and then when we print this it will remove banana but we can just also delete or remove a value by using the index number so in pop we can just say list dot pop just like pop out to remove something let's remove the one with the index value of one let me say one and print it gives us banana mango oranges it gives us that same list but without the value of that index number but we can also use do this instead of using pop we can do this exact same thing but now we can use delete so when we say d e l i will say list two let's say zero so now this is going to delete the value with the index number of zero in this list two lists so when we print list two it's gonna give us everything without banana let's run it now you see we have apples mangoes and orange i remember that we i also showed you how to just clear a list how to just delete everything in a list we can also do this using this deal function so instead of specifying something here if you just say delete the list 2 now this thing is going to be cleared it is list once i run it you see it says list 2 is not defined ok but we do have list 2 right here so let's do something like um delete [Music] this one yeah and then let's just print oh okay so um the reason why it says list two not defined you can see we have list one now here we have list two which is banana apple mango and oranges then what we're just doing we deleted list two so once we delete this list two is quite different from the one which we did earlier which was list dot clear remember that if we do list two dot clear you're just going to remove everything here but that list is going to be available in our code but when we use this delete it's just going to delete away that list it's going to delete that variable so it's not going to be in our code again that's why we're not trying to print list two python doesn't see it again it says list2 is not defined so it completely removes it so that's the difference between the clear function and the delete function now that's gonna be all for lists for now in this tutorial we're going to be talking about tuples in python so tuples are used to store multiple items in a single variable so if you know list which we talked about in the last part tuples are very similar to list but they have some basic difference like tuples are immutable now immutable means you can't change any value in a tuple now let me show you how to write a tuple in python now let's say we have a tuple of three numbers let's say three underscore numbers and i give those numbers um and when you are writing the top we should be in a normal bracket not square brackets so one two and three so let's say we have these now when we print three numbers and run that it just gives us this top but we can also do some things like get the first value in that tuple using the index number i can say get me zero which will print us one so we can also do all those type of things but let's say we now try to change the value of two to something like 25 it's not possible because as i said earlier on it's a mutable which means we can't change it once we have the tuple that's the way it's going to be for our code we can't refine it or edit anything from the tuple later in the code so let's say we want to do something like three numbers announce it from the index number one change it to 23 and then i want to print three numbers now this is going to give us an error so here it says type error topo object does not support item assignment so it's saying what we want to do turpo doesn't support it so that's the basic difference between tuple and list and then we can also do some other things like tuples allow repetition of numbers or repetition of values like i have one two three i can also input one again when i just let me remove this line i have one twice once i print that it's gonna work fine it allows repetition of the same value and i can also do some things like i can get the length of a tuple i can just say print lan and then it's just going to give me the amount of values we have in this tuple as you can see it prints four because we have one two three four values and then i can also check the type just to make sure this is a tuple like i can do type to know if this is a tuple or not once i run it you see it says class topo so that shows me that that is a top and top also allows various data types like i can have another tuple and say strings [Music] and then i can say home land let me give you one more earth now once i try to print strings you see that everything works fine so it allows various type of data types we can also do it again for like the boolean type i just say b-o-o and let's say true false and then true again now when we try to print boo [Music] you see that it works fine so it allows various data types and then it doesn't just allow them on separate variables we can mix them together like now we have one i can have another one which would be a string which would be oh and then i can have another one which is gonna be a boolean so once i print the first one which is three numbers now you'll see that everything still works fine so tuples allow all these kind of things and then in tuples we can also check the data type of the value of that tuple now if you want to check the data type of this first value we can say type of three numbers with the index number zero which is the first one so you should print us int which is integer so as you see here it says class int now that's toppo can also do that now instead of just writing um this parenthesis right here or normal bracket or circular brackets anything you want to call it is totally fine we can also use the top constructor just the way i told you about list constructor there is also a tuple constructor so the only difference is just that we're going to add topo right here and it have two brackets like this once i try to print everything still going to be fine let me just print the top of variable normally now as i print it you see that it's still a toppo nothing goes wrong now that's gonna be basically all about tuples the basics of tuples in python so we might be using these tuples less often in python we mostly use lists but in some cases we really want to use tuples in python something like let's say you are working with coordinates or just some numbers that you don't want to change or some values you don't want to change tuples might be the best option in this tutorial i'm going to be talking to you about functions in python so functions is just a bunch of code which perform a particular task now functions allows you to package your code well or let me say restructure your code well it's just like a block of code which performs a particular tax then when you want to use that function you just call it so let me show you how to tell a function in python so first to define a function we're going to use a keyword called def now once we type this def python knows that now we want to define a function so we can say d e f let's just do a simple function which is gonna greet the user and maybe to say welcome or something like that so we can just say greetings so now the keyword which is def and then after it we have greetings which is the name of our function so i can say greetings underscore function [Music] so this is the name of our function and then it's going to be followed by open and close parenthesis and then after it are going to have a column so after this block of code python knows that whatever is going to come below it is going to be the task in which we want that function to perform so once i click enter you're going to notice that it doesn't start from beginning it starts right from this place automatically it indents now in python indentation is very important so now this indentation shows us that whatever code we're going to write let's say like print something it's going to be under this function but if i just remove the indentation and i start from here then the function is cancelled this is an invalid syntax because normally there's supposed to be an indentation and an indentation one indentation equals to four spaces so if i go here you can see it's one two three four so once it's indents automatically that means it's created four spaces now let's say you are using um a code editor which doesn't does the indentation um automatically like if you are using notepad and then once you click on enter it doesn't add the indentation you can just ask for space like this one two three four then that's automatically indented so now we want this function to just tell the user welcome uh greetings from here something like that so we can just say welcome user so like this this function named greetings function is printing to the user that welcome but if i run this nothing is done let me just close this up and why is that so when we have a function for that function to be performed for that task to be executed we need to call that function and now we will call this function we can just say now once i press enter you see it's still indented but i want to go out of this function i'm done with what i'm doing in this function so i can just press the backspace now this is out of this function now if i just say greetings function with open and close parenthesis now i'm calling this function so that means this is go is just the same thing as everything we have in this function so now it should print welcome user once i just run it you see now it says welcome user so that is how to use functions in python but functions is more broad there are a lot of things we can do with function now as you can see this thing says welcome user the result but which user let's make it more fun something like that we can pass in something called argument or parameters so right here in this open and close parenthesis instead of just leaving it blank we can pass in something like um let's say we have name oh not this so name which is the name of the user now instead of saying welcome user we want to say welcome name so right here we're just gonna say name and this is just gonna print welcome name but instead of name it expects us to pass in something when calling this function if i just run this you see it gives me an error it says greetings function makes it one required positional argument name so this is showing us that when we are calling this function now we need to pass in whatever user that is so let's say the name of that user is john so now when i run this right here you see it says welcome john so let me just go back and make sure to explain it very well right here we have this function it's having an argument or parameter which is name so this name is going to be able to tell the function which user it wants to grit and then it's just going to greet the user with the print function so for us to give it this name this name is just like a variable so when we're calling the function instead of leaving this blank like this we're just going to pass in there a string like john but you know we can also pass in other data types so you can see that this john is a string but let's say we just want to pass in an integer or a number and you know anytime we're dealing with numbers in python we don't need the quotes we can just say 34. once i run this you see it says can only concatenate string not integer so if you'll be following this tutorial from the beginning i showed you how to convert an integer to a string so right here it says it can't concatenate string and integer that means you can add them both together so for us to be able to use this we must make sure that we convert this integer to a string so i'm just going to say string like this now this name is a string once i run it you see it says welcome 34 so that is how to basically do that now let's just go back so let's still make sure we're passing in john so that is how to pass in parameters or arguments now you can call it parameters you can call it argument whatever you want is fine and then we can also pass in more than one parameter or argument so we can pass a name can also pass in h and then we can say welcome then the name and then we can say something like um let's do this [Music] you uh then let's type the name let's say age yes boat and a full stop so right here when we are passing in this john we can also pass in the age as an integer let's say 27 but remember now this is an integer if we run this obviously we're going to have an error which is kind of concatenate string and integer so you know that we now have to convert this to a string right here and then run it so you see it says welcome john you are 27 years old so that's what that's how to simply do that you can pass in two arguments or parameters now let's say you don't know the amount of argument that's going to be passed in here let me practicalize that before explaining now let's say we have names let me just remove all these welcome name so let's say we have these but we don't know how many is gonna be passed in here so we're gonna have to put something like esteric now this asterisk is showing that we are passing various amounts like a tupu a tuple of which is like a list of values were passing in here and then we don't know the amount that is being passed so let's say right here we have this tuple john let's say we also have another one named tim and let's give it one more okay right here one more named tom [Music] so right here we're passing this three list these three values in here as names so these names is like a topper now that's why we put this as steric because we don't know so for us to now show the name which we want let's say we only want to grease this second user then we can greet the user using its index number so we can say names the index number is one now when i run this you see it says welcome team so that's how to pass in various values more than one value if we don't know what we are passing now let's just go back let me press ctrl z back to name and age quickly okay like this yeah so let's print this and make sure that code is working welcome john you are 27 years old right now we can also pass this instead of just um passing the values we can pass it through the variable names now we can say something like name is equals to john and then age is equal to 27. it's still exactly the same thing so when we run it still going to print us welcome john you are 27 years old it's basically the same thing so let's make this more fun instead of just passing out these values statically or or just add coding the values we can first ask the user to give us an input of his name and his age then we're just gonna print that so let's quickly do that right here no right yeah so right here we can say name input enter your name [Music] and then let's give it another one named age input and let's say enter your age so now asking the user for the values we're not just typing these and then once the user give us these we're just gonna replace that here so by placing it with name and i'm replacing this with age so let's run this so if i come down here it asks for my name i say team it asks for the age i say 101. now it says welcome team you are android i won so i hope you get the concept of functions in python these are the basic thing you are going to be dealing with in python in this part we're going to be talking about return statement in python function now return statements are just being used to get a response from the task being executed in a function now as you know in the last video when we defined our function then we just simply printed out whatever we want to return but normally in python we use what we call a return statement so this return statement is gonna like give us an output or give us a feedback of what has been executed so let's personificate this right let's say you are you have been sent an errand to go let's say get some information and then when you come back you want to give a response or you want to return like a feedback you want to say what you went to get what the task which you executed so that's the same thing right here in python function the code block goes for the errand and then the return statement give us a response so let's show you how to do that we're just gonna define a normal function let's just say my function [Music] and then it's not going to take in any argument for now so in here we just want to return let's say five plus four so this basic function now is just simply going to return 9 because that's 5.4 but now if we we can just print out my function so once we print out my function you see well it says function my function it's just showing us that there's a function but we also need to add our open and close parenthesis when we run it again we see nine now we can make this like more interactive we can just have a function that adds up to numbers or subtracting numbers or does something like that so we can say add numbers and then i want to add i have two argument num1 and num2 so what we just want to return is num1 plus num2 so right here we can just pass in norman as 2 and num 2 as 3 so this will give us you see it says my function is not defined that's because we've changed the name to add numbers we're just gonna run it again you see now it gives us five but let's let's make this more interactive here let's say num1 is equals to input [Music] enter first number [Music] and the let's have a num2 input enter second number and right here instead of saying 2 we'll give it num1 and right here we give it num2 so now when we run this it's going to ask us for the first number let's say 80 and let's say 20 as a second number it's gonna add this up and give us android now right here is you see it says 80 80 20. and the reason why it says this is because it's saying this as an as a string and so when we use the addition is seen it as concatenation so that's why it just says 80 20. but i want you to add if i come here over it you see it says the string but we know we are collecting an integer but python doesn't know that so we have to tell python that hey what you want to collect is a number so i want you to add it to do that we'll convert it to an integer by saying int [Music] then we close it right here then we also do the same thing int and then we close it right here now when we run this it's gonna add it for us 80 plus 20. now it gives us hundred so that's the basic concept of return statement and also want to point out something that whenever is in return statement in a python function we do not write anything after that code block like that return statement shows the end of that function if i come here now and say print hello once i run it it asks me for the numbers you see it just gives me eight it just returned this it doesn't print hello because it is outsider function and it's not a normal indentation so that's code is not seen in our code block but if i come here if i delete it and inside the function i paste it it's gonna print that for us so let's say four four now you see we have hello and then we have the answer of the addition so that is how to use return statements in a python function in this part of the tutorial we are going to be talking about if statement in python so if statement is basically just giving python a condition now this concept is very easy to grasp not only python but in programming in general there is always if statement but maybe with different syntax so if statement is just allowing python to execute code automatically by itself so we are saying that if a particular condition is true then python should do this but if it's not true then python should do that so we're just giving python a condition before it runs the code so let me first type a simple little note so you can understand the concept so i can say something like if i write a number [Music] so now i write a number and then if that number is divisible by 2 then we know that the number is an even number but if it's not divisible by 2 [Music] then it's an odd number so something like this this is not how to write code this is just plain text but i'm just showing like yeah a user wrote a number then if the number is divisible by 2 then we can tell python to tell the user that okay this number is even but if it's not divisible by 2 then this number is odd so now let's practicalize this and let's write it in real python code so now we have a variable named a and a is equals to two and then we have another variable named b and b is equals to three now we can have a simple if statement checking if 2 is greater than 3 or 3 is greater than 2 or if variable a is bigger than variable b so say if a is greater than b then we can just print to the user a let's concatenate is greater than b okay but here we know that b is greater than a so let's just make this greater so our code will run now let's run this now you see cannot concatenate string and all those type of stuff so let's just convert it to a string [Music] so let's run this again you see now it says 4 is greater than 3 so we checked if a is greater than b now we can also check obviously if a is less than b now you see that nothing happens because we said it should run this code only if a is less than b but since it's not less than b that could be executed now we can also check if a is the same thing as b so if a is equals to b let's just make a equals to b and we run it you see it says 4 is greater than 4 because that was it said it should say but we can just simply say a equals b so once you run it you see it says a equals b but if we change this now to three now four is no more equals to three it doesn't run so that's the basic concept of if statement and we can also do this for different data types now let's say we have a string which is team and b is also team [Music] so now we know that a is equals to t my b is equals to t so obviously a is the same thing as t then she say a equals to b when we run it you see it says a equals to b then we can also use the boolean so let's say true now we can say if a is equals to true that means if a is true then we can just simply say a is true and then we can just run this so a is true so that's the basic concept of an if statement but you know we can also say if a is not true that means if a is something like false then we'll say a is not true so you know now a is false when we run it it says a is not true we can also add two different types like if let's quickly change this back to integers [Music] so let's say if a is greater than or equals to b so you know a is greater than b or if a is greater than or equals to b then we'll say let's just say true or something like that so let's run it you see it says true because a is greater than b and if a is also equals to b it will just stay true because a is equals to b but now let's say a is less than b when we run it nothing is being executed so now we want to add more functions to this if statement so it says if a is greater than or equals to b so let's just check if a is equals to b then we'll say a equals b but what if a is not equals to b what should the python do what should the code do should we just not run anything so we can now add else this else means if a let's just correct this if a is not equal to this so i say if a is equals to b it should print a equals b else means anything except that then she just print a note equals b so if a is equals to b print this then else means if it's not the opposite of that then if you just print a is not equals to b so when i run this now it prints a is not equals to b so that is how to use a simple if and else statement we can also do that with like a boolean just make this true so say if a is true let's say a is true if not let's say a not true so when we run it you see it says a is true but if i change this to false [Music] obviously a is not true so it says a not true so that's how to basically use a simple if else statement but we can also go further now let's say we want to add more than one condition like if a is equals to true alif [Music] a is equals to false [Music] print a is false [Music] else a is none of the two so if a is equal to true issue print a is true a leaf means we are adding another statement is the same thing as if but else if that's meaning of l if so if a is not true then you cannot check if a is false so if a is false print this but if it's none of these two then just print a is none of the two so now it should print a is false when we run it i come here we see a is false now that's how to add a simple else if statement now we can add any amount of earlier statement we want to add we can say can add another one and say a leaf a let's say not equals to let's say a something like that then we can print we can just bring anything we want to do so you can add any amount of if statements in there that's the basic syntax and way to use an if else statement but now we can make this more we can step up a little now instead of saying if a is equals to true so we can say if a is okay let's let me specify a variable let me say boy is true and let me say short is also true so i can say if boy is true then prince is a boy now since boy is true right here there's nothing like that so let's just change this to boy since boy is true then we can say he is a boy but we can also do something like if boy is true or short it's also true so that means if any of these two conditions are true if we print is a boy or he is shot so when i run this you see he's a boy or he is short so since both of these are true it prints this if this is false it's still like it was saying if boy is true or short is false it's still going to print this now the reason why still printing this is because or when we're using it all is going to check for only one so if one of this condition is correct it just runs this so when i run this you see is a boy or a shot but if i change this now to and you'll see that it doesn't print this it will print a is none of the two so let's just remove this else if for a while so now you see that it prints the else statement a is none of the two and that is because when we are using and it has to check that this and these conditions are both correct so if one of it is not correct it's just going to go to the next statement and skip that statement so that's the difference between or and and so now that we know the concepts the basic concept of if else an ellipse statement let's just go ahead and do some simple exercises now let's say we want to use if else statement to check for the data type and then tell the user okay this data type is let's say a string or it's a list something like that now first of all let's tell the user to input something so let's say value should be input and they will tell the user [Music] input of value now once the user input of value we're gonna check for that value and tell the user that oh this is your value so we can use an if statement for that so we want to check if the statement is a string so we can say if type if the type of value is equals to a string str then we'll print let's just print that value value is a string [Music] but let's add an alif let's say we want to check if the type is an integer the type of value is an int that's an integer we will print value [Music] is an integer and then we also want to check let's say if it's a list so say alif if the type of value is a list then print and say value is a list but if it's none of these we will just say we don't know [Music] so right here i wanted to add this but obviously it says python says that i've closed it so for me to add this column the code there i have to use the backslash so now once i use the black slash this is going to be printed with it so now we can say we don't know the data type of then let's just add whatever the user root of value now let me just close this quickly and then let's test this out now input a value let me put six it says six is a string that's nice to here now the reason why i say six is a string because anything we input automatically it gets it as a string now if we want to get this as let's say an integer we have to type in we have to convert it to integer so this type of exercise only works for string so now we're changing the name of this exercise to checking if it's string only so what we just want to do is we cancel all this we're not checking for init or list or any other data type we're just checking if it's a string and that's because python automatically sees it as a string and we have to convert it to an integer if python wants to know if it's an integer so we can also go further to check if it's an integer by using some complexity statement or some regex which you don't need to bother about for now but for now let's stick to this basic exercise so a checking if is a string so if your string will say it's the string if it's not we say uh is not a string so we just bring all these to the top so right here's where this should be leave a space and then let's run it again now when i print a value now and i say i now you can see that it gets that i is a string so that's basically how to just you know do it but if we want to like change it ourselves and say integer so now anything we input is automatically seen as an integer so it will automatically say value is not a string so even let me put six now you say cannot concatenate string so um that's simply because we have to convert this to string before concatenating or let me show you a better way to do it since we're just dealing with one instead of using plus here i can just use a comma so when i run this again and input an integer it says 67 is not a string so now the second exercise we're gonna do is to check if a number is divisible by five so we know that we have numbers multiples of five or yeah something like that that means the numbers which five can be divided by so we have five itself we have ten fifteen twenty but if we give it like 17 we want to tell us that 17 cannot be 5 cannot be divided by 17 something like that so we're just going to automatically get an input first of all and make sure it's an integer so we say input a number and then we'll say if let's just okay what i want to say now is that if the value so what this is doing is that value is basically the number the user input and then this percentage sign means the remainder so we'll say if the user inputs 20 so 20 divided by 5 is 4 then the remainder is 0. so when we do something like um 2085 in python it means give us the remainder so first of all let me just cut this out and let me show you what that does so if i say print twenty percentage five now you're gonna see that it's gonna print zero it's gonna print the remainder of that division if i say print 22 it should print me two because 22 divided by 5 is 4 then the remainder should be 2. now you see right here we have 2. so let me just quickly bring that back right now we're saying if the value percentage 5 that means if the remainder is equals to zero then obviously that just means that value can be divided by five else value can not be divided by five so since we're using an integer let's add comma yeah like this so this should work now it gives us a number let's say hundred now it says android can be divided by five let's run this again and give it a number that can't be divided by five like one zero six it says 106 can not be divided by five so that's how to build a basic program with checks if a number can be divided by five now the last exercise which we're going to do on if else statement is going to be the exercise to check if the length of a sentence is less than 10. so again we're going to collect a value but this time it should be a string because it's going to be a sentence now we want to check if that the length of that sentence is less than 10 so if the length of the sentence is less than 10 they'll say value is less than 10 else the value is greater is more than 10. [Music] so we can just easily do this and let's type in random things like my name is my name now this is obviously more than 10. i hit enter it said not supported between instance so because this is not seen as uh as an integer we can do this so right now anytime we are using this less than or equals to it has to be as that's to basically be an integer we can do less than or equals to for a string but there are see other ways we can do this we're going to talk about it we could have solved this now but i'm leaving this to further in the tutorial because i do want to jump in some steps so further in the tutorial we're going to come back to this exercise and i'm going to show you how to do this with strings but for now i hope you get the concept of if an else statement so before we continue with more concepts in python we just want to do a simple python program with checks if a number is an even number or an odd number so as we know an even number is the numbers we can be divided by two an odd number is the one which counts so this shouldn't take us up to like two minutes to do so we're just going to collect an input from the user and name it number we'll say input and we'll say and a number and then we want to make sure that this is an integer and then we're just going to say if number percentage 2 is equals to 0 then obviously this is an even number what this is doing is that if the remainder of number divided by two is equal to zero then is an even number else [Music] we just say odd number now when we run this we put a value of like sister c's now substitutes is an even number we run it again and it pulls 67 67 is an odd number so that's how to do a simple exercise like that using if statement in python in this tutorial we're going to be talking about dictionaries in python so dictionaries are used to store data values in the key value page so dictionaries are also data type just like list just like strings dictionary is also a data type but this dictionary stores value in a pair of keys and value so just like a main dictionary like an english dictionary where we have the word as the key and the value as the meaning of that word so that's basically how dictionary works now dictionary uh they are changeable which means you can modify them even after like configuring or assigning them but they don't allow duplicates not like in list or tuples that you can type in a value more than once in dictionary you can't do that so let me show you how to write a dictionary in python so let me just name this dictionary in my date how to write a dictionary we're going to use this curly braces and then we're gonna give it the key so the key might be something like name and then i'm gonna use this column and then the value can be team and then i'm gonna put a comma i can have another key and value there let's say age oh no let me say something like um nationality [Music] and i can say something like african something like that and then let's just have these two let me give it one more and say qualification and let's say college degree or something like that so this is a basic dictionary in python now let's just start with the basic thing by just printing this dictionary to our screen uh what's this let's just print my dict now once we print this you see that it just gives us this dictionary name team nationality african qualification college but now let's say we just want to print only this name value we want to print the value of this name so this is the value and this is the key assigned to this value that means if we want to get this value we need to search for this key just like when you go online and search for meaning of order or meaning of anything amount of words meaning of anything so let's for example meaning of order so order is the key then the value you are looking for is the definition so that's how dictionary work is very similar to the normal word dictionary so now we want to get the value of this name we can just use the square bracket after this and in there we say name so what this is going to print for us is team right here you can see team which is the value of this name now there are other things we can do as i said duplicates are not allowed in in a dictionary so now let's say i have two names and the other one is john [Music] now when i try to print these you see it prints only the first one it doesn't say i mean prints only the second one that's the most recent one it's automatically cuts this one out of it now does what i mean by duplicating not allowed in dictionaries you can't have two key with the same name but it can have two values with the same name now what i mean by that is this is the key this is the value so two keys we can have them with the same name but i have two values let's say this is name two and name two can still be team now if i print this you see that name is team name two is the team so we can have the values the same we can repeat that but the key being assigned can not be repeated it cannot be a duplicate now let's say we want to get the length of this dictionary it's very easy you're just saying length like this then once we print you see it tells us four that's because we have four keys and values in here and then we can also mix data types now you know that let me just remove this you know that this name the value being assigned to it is a string the value being assigned to nationality is also a string and the value being assigned to qualification is also a string but we can change it let's say i want to add like h and we know that h will obviously be an integer we can just add it here let's say it's a seven now the normal way will write an integer without the codes that's how we're going to write it in there and then once we print we can still go ahead and print [Music] age it's going to print it's the same for us with no problem you can see it right here 87 we can also add a boolean we can say is tall so easter can be true and then once we print i may want to print my dictionary but it's tall you see it prints true so it tells us that okay this person is top so we can add different data types and then what is very cool is that we can also add a list so we can the list of his friends [Music] so we can the data type as a list like this so we can see one of his friend is peter we can say paul we can say precious and other so here once we print friends it's just going to print us that list okay right here it says invalid syntax error and that's because we forgot to add a comma right here so we should make sure to add a comma before adding another value so once we click enter you see it gives us pita poll and pressures so now let's just print the whole dictionary without anything attached now we go down here we see that it prints everything name team and everything we need to know about that dictionary and then to check if this is a dictionary we can also use the type so once you just put type yeah it's gonna print dictionary for us so you can see right here it says class is dict which means that this is a dictionary so we can also make it more fun by specifying a variable let's say we have a variable named x and we want this variable to be equals to one of the values of this so we can just simply say x can simply be equals to my digged name so x now is name now once we print x obviously it prints the name now those are basic dictionary functions and dictionary methods so i hope you get the concept of dictionary in python in this tutorial we're going to be talking about while loop in python so while loop is a python feature which allows you to loop through a block of code while a certain condition is true so let's say you have a condition like if a number is greater than 10 then it should just loop through a bunch of code below so it's just like a function with an if statement something like that so we're giving it a condition then under it we have a bunch of code which we want to loop through so let me just show you how to use while loop in python so first of all we can specify a variable named i and we can say i is equals to 1 and now to use a while loop we can say while one is still legs than six so this is a condition well see as long as one is less than six then we just want to print i i mean as long as i is less than 6 not 1 i is this variable that is still 1 then we want to print i and then we just want to increment i by one so we can say i equals to i plus one so this is just gonna run this while loop as long as one is i is less than six it's just going to print i to the screen and then it's gonna increase i by one then run it again run it again until it makes sure that it doesn't pass six it doesn't reach six so you can also increment this by typing i plus equals one the same thing anyone will work now when we run this you see now is one two three four five so what happened was that this loops through this block of code so we said while i is less than six so that means it should loop through this block of code printed i then after that we incremented it then now i is equals to two still less than six that's why we have two then it did the same thing incremented it we have three four five but after that five it did the same thing incremented by one but now i is equals to six so because that's this condition is really false i is normally less than six then the while loop has to stop just like cutting away from that while loop so that's basically what while loop is about it's very important in python there are a lot of python programs you want to build that will need you to use a while loop to loop through different things with a condition and then you can also do more things like e while i is less than 6 or i is equals to 6 something like this so now you know that when we run this it should print us 1 to 6 because it's normal just only when i is less than 6 is also when i is equals to 6 so now it gives us an error because when rising equals 2 in python it has to be double so let's run it again so now you see 1 2 3 4 5 6 so while loop in python loops through a block of code at a certain condition being true so because this is true one of this is true it just prints i increase increases by 6 comes back does looping does that code again increase it by 6 come back and then it makes sure that i is equals to 6 now we can also say i equals to 10. once i run this it gives us one two three four five so if we say and and now and we run it it doesn't give us anything so sometimes in white loop uh i'll i put this in while loop conditions is not similar to if statement you know we need statement we can do if is less than 6 and i is this then it should do a certain thing so now you can see that if i is less than 6 and i is equals to 10 so the reason why this didn't run is because at first i is less than 6 so that should run but we're saying and it must be equal to 10 but we know that i right here is not equals to 10 so if i change this to 10 it still won't run and the reason why it won't run is because one of these is not correct so i is not less than 6 because 10 is no less than 6 but we say is less than 16 now then we can run it and you can see it prints something now so when you are using and or or in the condition when using o as long as one of it is one of the condition is true then that while loop will run but when is the end the two conditions must be true before that while loop runs so now let's just don't bother about the and and now let's change this to one and let's change this to ten so now when we run this we should get one to nine let me just expand this we see that we have one two three four five six seven eight nine now this is basically the basic concept of why loop in python in this tutorial we're going to be talking about for loops in python so for loop is used for iterating over a sequence now this means it's used for looping over a sequence and this sequence can either be a list a tuple a dictionary even a string or a range of numbers it can be anything that is a list now we use for loop to loop through them now for loop is very used in python is used a lot in python actually because most of the times we have list of data we have data large amount of data now for loop is used we can follow up to loop through each amount of those data each value in that data so let me just show you how we do a for loop or we call the follow in python it's very easy now for you to type a for loop the first thing you need to do is to type this for keyword for now this keyword shows that we want to loop through something so we can say for [Music] letter in for letter yeah for letter in let's say something like hello now this is same for each letter in hello which is h-e-l-l-o we just want to print letter now when i run this you'll see that it prints h-e-l-l-o differently it prints everything separately so it's looping through each letter in that hello string like iterating through the sequence now we can also do this by having a list now we can save my list and we can have this list to be anything let's just say j i j u j o now we can look through this and say for let's say like for x now note that this can be anything let me just this in which i put letter here it can be denoted by anything it can be represented by anything so when i first got started with python um i was kind of confused in this for loops just simply because of this letter so i thought that you know where since we are using a low and each is called character h is called a letter is called a letter so i thought we have to use letter for each but now i just want to clarify that so that nobody will have to go to that uh confusion so this can be denoted by anything if i say for x in yellow and i just say print x it's still going to do exactly the same thing if i say for j i or for anything i do it can be denoted by any single thing now let me just so now i just want to say for items or for values would be better for values in this my list so i'm going to remove this string i'll say my list now i just want to print out the values now once i press run you see now it prints everything j i j u j o which is j i j u and j o so that is basically how to just loop through a list now you can look through anything actually you can look through a dictionary so let's just have a simple dictionary and then let's say we have curly braces and then we have name [Music] john we have age [Music] 13 [Music] let's just leave those two now we can see for values in my date print values now once i print this you see it says name and h so you just basically print these two which we have name and age so that's iterating over what's over a list of values and then in for loop there's also something we call the break so now let's go back to our list just yeah so now we have we still have this our list and we set for values in my list print values but i can now say if values is equals to j u then i want it to break so what this is going to do is that it's going to look through it one by one it goes from j i then it goes to ju so i'm saying once it gets to this ju it should break then you should stop that loop so once i run this you're gonna see okay right here it's supposed to be double once i run this you're going to see just mean j i and ju now why is this i said that it should continue looping print out the values but once it gets into that value is equal to ju that means once it loop to this ju then break break means stop the loop so that's why i want to guess to ju it stops that's why we don't have j o right here now we can also instead of having the break here i can do something like this so even before printing values i can have my if statement and i can say for value in values if value is equals to ju i can say break and then after that break i can print values so if you have gotten by now the difference is that if value is value so it's looping through these values but if the value is value it should break you know in the last time it first printed value it first printed is ju before it's broke but now if value is ju it should break before even printing so it's gonna guess that okay we have ju we want to break then we're not going to print so python runs code line by line first runs this code then runs this then runs this then like that so the first thing it sees is that we are checking if what we want to loop through next is ju and we see that yes it's true it's ju then we just break then after breaking we print values so the only value we're going to print is j i because we have broken when we got here so when i run this you see that i only have gi we don't print ju again so that's basically how to use the break in for loop but we can also loop through a range of numbers so let's say we have four x in range four now what i can just do is to print x now as i print x it's going to give me a list of numbers from 0 to 3. so it's going to give me a range of numbers starting from 0 to the num last number before four so i can also change it to something like 10 you're gonna give me from zero to nine so as you can see here it gives me from zero to nine and then we can also specify for it to loop through a particular range of number so now we're giving it only 10 but we can say it should look fro it should loop from 3 to 7 or from 10 to 70 or any amount so if we wanted to loop from three to seven this is how we're going to do it we just say for x in range three to seven loop through this number so when i run this you see it gives me three four five six it doesn't start from zero so it looks from three all the way to the seven now that's how to loop through a particular range of number and the final thing i'm going to show you in for loop is that we can also use the else statement so now let's just loop to for a range of seven so after looping we can say else what this else does is that once this loop is done once it's finished we can just print finished looping so now once i run this it's prints from 0 to 6 and it says finish looping so that's what the the else statement does is that it just simply first of all finish a loop first of all finish the iterating over the sequence then it just tells us whatever is in the else statement so that is an introduction to for loops in python in this part we're going to be talking about 2d list in python so 2d list also means two-dimensional list so it's like well we have multiple lists inside a list variable so right now we can have a normal list like my list which can just have a numbers like one two three four and once we print my list [Music] know that we're gonna get one two three four which is a normal list but now we want to have a 2d list first of all is recommended to press enter just to have a nice indentation then in here we're going to have a list so this can be one two three and then we can have another line which can be four five six then we add a comma and on another line can be seven eight nine so now this is a 2d list because it has rows and columns this is obviously a list in a list so this like the eye level list and it's like the sub sections something like that so now once we just print my list you see that he just gave us this normal list but let's say we want to get this value of one or two so we're gonna get it using the index number so we know that the index number of this is one of zero which is the first one then we also want to get the first value which would be zero so once we print this out you'll see that we have one but let's say we want to get the value of five so we know that the index number of this is one there was index number of five it's also one now once i run this you see now that i have five so that has to basically navigate and like print or get some certain values from your 2d list and then we can also loop through this so i want to introduce you guys to nested loops so nested loops is where you have a loop in a loop a for loop to be specific it's where you have a for loop in a for loop so now instead of just printing let's just say four um something like lists in my list then you should just print my list now once i run this you see it just prints my list for listing my list it just loops through that but instead of just looping through this or just printing this what i can do is i can add another for loop indeed so for list in my list then i cannot say for maybe the row or the column for row in list then now we want to print row so when i run this it gives us an error that's because in python we must have indentation to show that this is under this so let's run it again now you see it says type object is not iterable so for row in lists we must make sure that everything is intact so i put a list but the name of this is list so i have to add s now when i run this again you see it gives me let me just open this up you see it gives me from one to nine which is every single thing i have here one two three four all the way to nine so that is how a nested loops work so is like looping through a loop already so a loop in a loop but i've not tried it before but i think we can also have another loop again on the pinch or something like that yeah so there are various things you can try out with python it's very great so i hope you understand the basic concept of 2d list and nested loops so in this part of the tutorial we're going to be talking about comments in python so comments is being used to prevent a particular code or block of code from running with your actual program now let's say we have print hello and then we also have print one now when i run these you're going to see print hello and one now if i come here and i put an hashtag right beside these like this you see that this is greened out it's normal part of our code it's still there but when i run it it's not going to be wrong with our code see only one prints so that's what comment is for mainly in python comment is or mainly programming in general is used for explaining our code or is used for taking down notes in our code very well like making our code more readable in python you can also just add comment let's say you just want to quickly test your code without a particular code block you can also use comment so you can also add comment say at the end like you can say hashtag this line prints hello so you can just use it to explain your code or you can just use it to block the code from running and as you've seen if you want to comment more than one line you can put hashtag on this line on the second line then everything will be commented out but there's also a better way to do this in python you can just easily at the let's say you have a code block or you have a function like my function and then this function just prints i now let's say you just want to comment out this particular function now what you're going to do is above of that function you're going to add three quotation marks like this and as you can see it automatically commits all the whole code out so if we run this now nothing is going to run at all it it says we have a syntax error and stuff like that so we have to close this comment by putting those three codes back under where i want to close it so we want to close from the beginning of this function to the end now when we run this we will see that print um hello and print one run so now we try to execute this by saying my once confined you'll see that it's going to give us an error it says name error is not defined that means it doesn't even see this part of the code we've defined the function but when we try to run it it doesn't work so that's what comments are used for in python mainly two things used for removing your code or just for taking notes and making your code more readable in this tutorial we are going to be building a basic calculator using python so we are going to be using all the skills and everything we've learned in the previous sessions i'm gonna be adding them together and then building a simple calculator so now what we want to do is to have three inputs collect three inputs from a user which is the first number the second number and the operator so the calculator i want to build is very basic it's just the one that is going to add subtract divide or multiply two numbers together so we want to collect a number which would be num1 and a second number which would be num2 and the operator the operator is whether you want to add subtract or whatever you want to do so now let's define that same num1 should be equals to input and we can say enter first number and then we can also do the same thing and say num2 input enter second number and then let's say op which is the operator input enter operator so now that we have the three inputs we're now going to use an if statement so what is going to happen is if the op which is the operator is equal to addition then we just want to basically add the first number and the second number then we're going to use an alif to get if his subtraction or whatever then we're going to do according to the operator the user's input so let's say if op is equals to plus then if is equals to plus we're just going to print out num1 plus num2 so now let's just test that so right here let me say 2 3 and i want to add now you see it says 23 which is very wrong 2 plus 3 is supposed to be 5. now the reason why it says 23 is because it still sees this as a string if i over run it you see it says string but it's meant to see it as a an integer so because it's a string it's just concatenating it together but we want it to be an integer so it can perform the normal operation so we have to say int then in that int we're gonna paste that back there and we're gonna do the same thing for the second one hint and paste now when we run this it should work enter first number let me say 75 and second one should be 25 and i want to add now you see it gives me hundred it adds it up together that is what we want to happen so let's just do some adjustments and let's say the addition is num1 plus num2 but now we want to check what if a user inputs subtraction so if a user say subtract then we just want to print the subtraction is num1 minus num2 but we must make sure to add this plus sign right here but as we all know we can do this we can do the addition is um num1 plus num2 because this is a string this is a number and then this is a string so what we can do right now is to just put comma right here also we can just put a cover so let's test it out if i say 17 and 20 and also subtract it gives me the subtraction is 50 which is nice and then let's do the same thing for multiplication so we just say the multiplication is num1 times num2 so that's a basic arithmetic for multiplication and then we're also going to do exactly the same thing for division so if it's equal to divide then we just print out the division is num1 divided by num2 so now this should work now when i run it and i say 60 20 and i want to divide you see that gives me the division is 3.0 but if i do something like the absolute of these oh no so absolute of these let me run it again now if i say 60 and 20 and i put divide okay still gave me 3.0 i was expecting it to give me three but we're going to bypass that so this is how to build a basic calculator using python using getting user inputs and then using if statements if statement a leave statement so let's add one more thing so if the user doesn't input plus minus times or divide what we want to just tell the user that would be on else we'll say print and can just say invalid operator so let's test this out by running it let's say user says 60 or 90 this time and 7 and user now do something like dollar sign it says invalid operator but now let's run it again and test from beginning so let's test for the addition 90 20 addition gives me 1 110 now let's test for the subtraction we have 50 we have 70 let's subtract it it gives me minus 20 which is the correct answer and now let's test for the multiplication we have 40 we have four and then we have multiplication now it says 160 which is correct and then for the division also we have 80 we have five we have division 16.0 now everything is working fine so i hope you understood what we did by building this basic calculator in this tutorial we're going to be talking about try accept in python so what this does is that it prevents an error so most of the times when you're working in python you're going to get a lot of errors you might do some things and then the program is going to draw an error or an exception so most of the times this error just stops our program or scatter our program if our program is running or doing a specific task once an error comes up the whole program is down so to prevent this we use the try accept method now this is going to get automatically get an error and then just print whatever we want to tell the user now for example you know when we want to do an addition we want the user to input an integer but let's say the user inputs a boolean value or string or something else so instead of the but you know python telling throwing an exception or trainers an error we can automatically tell the user that invalid inputs that this is not a string or something like that so let me show you how to do that in python so now let's say i have um x is equals to input emphasis integer to be specific input then we'll say input and integer and then we print x now when i run this once i input an integer it prints x it prints 7 the integer now let me run it again and input a string now you see that it gives me an error named value error invalid literal for int so it's saying hjk is not an integer so we can just automatically get this error by ourself and then you know tell the user instead of python throwing this exception which can stop our program so for us to do this what we want to do we're going to say try like this then all this is going to be in try then after it we're going to say accept then we're just gonna print value not an integer so this try is saying try all this code then if there is any exception at all just print value know an integer or we can just print um something where trunk because we don't know what might cost the exception it might not be that a user inputted a string it might be something else so let's just say something went wrong please try again so now when i run this and i input team now you see it says something went wrong please try again it didn't trust this area like the last time so that's how to basically use try except in python but it's more deep than these we can get we can use the accept method for some specific type of exceptions if i scroll up here you're gonna see that this exception it gave us is called value error now we can say except there is a value error then say this this one i'm doing right here is just getting if there is any error at all but let's be specific let's say if there is a value error [Music] so now if there's a value error a value error means uh the user inputted what we don't want we're asking for a string and then input something else then we'll say then now we can now say value not a string but not an integer now when i run these i scroll down and put something else now it says value not an integer but it's getting for only this type of error now let me try to add another error intentionally so let me say let me try to concatenate with a name so right here i'm trying to concatenate with a variable named name but we didn't have any variable like that before so it's going to give me a name error so when i run this i put wrong now you see like if you scroll up it shows me name error name is not defined so but if i just put only accept and i run it shows me name is not defined so this is because we're trying to define something that is not like a variable that is not there so that error is from us which we can pass something like this but now we're trying to [Music] pass an error from the user we're trying to make sure that the user doesn't input anything wrong so now we say accept value is not an integer so i hope you get the basic concept of try except method in python now we can also do something like let's change this but something went wrong you can see something went wrong now when we say something went wrong after this we can say else print nothing went wrong now what this else is doing is that after typing after running this code if there's anything that went wrong you should do this but if everything went fine then you should say nothing went wrong now let's run this it's asking for an integer i input an integer now it prints out that integer and it says nothing went wrong but if i run it again and input a string now you see it says something went wrong but he didn't say this so this else is if everything is successful if there's no error then it's going to say something nothing went wrong then the last one i'm going to show you is called finale it's also a keyword now this is gonna run either there's an error or not so i can just say try accept finished so whether there's an error or not this is going to print i have seven there's no error it's print seven and says try except finished if i run it again and i print a string so it says something went wrong which is this and it says try except finish so that's just saying that after everything if there's a problem or not just print that try accept finished now i want to talk about reading files in python so sometimes when coding with python you might want to work with some external files let's say like a text document or like uh like a spreadsheet like an excel spreadsheet or like an html file or any external file so you want to know how to read and write that file so let's put that to practice first of all let's create a new file right here and let's just name it something like has been saved this and then i'm just gonna save it in the same place i saved these so let me say all files this i'm sure is located okay first of all let's cancel this and make sure we know where this is saved um yeah if i come here yes so i see where it is saved project jungle tutorial yeah so now i can create a new file and then save it so right here i can save it as something like countries dot txt so it's ready under txt i can just change it to all files so in these countries i want to have just a list of random countries so i can say ghana i can say mexico i can say morocco uh let me say spain i don't have any spelling errors there so um ghana mexico spain yeah i think that's okay let me add one more see france so i think that's okay so now for us to read this file we have to like import it into this our python file so it's very easy to do what we just need to do is to say open [Music] countries which is the file name dot txt now we need to make sure that these countries.txt is in the same folder as this app.py so we can easily navigate to it and then after saying this the second parameter this is going to take is r now what this r means is we want to read on this file so we want to only read this file we don't want to edit anything on it then there's also something like w now w means we want to write on this file we want to edit this file and we also have our plus or a this a means we want to append to the file so we can't edit in the middle of the file or modify the file or make any changes we just want to append to the ending of the file and then we have one more called r plus this means we want to do both reading and writing so it give us full capability so that's what the output is for but for this part we're going to be working only with r so we are opening this file and most of the times we always want to save it in a variable so let's just name a variable file country file and then it should be equal to this so if we print [Music] and any time that we open a very uh a file anytime we open a file we always want to make sure that we close the file so down here we can also say country file dot close so this is to make sure that we're closing the file and then in between those we can do anything we want with the file so now i can just simply print count file so first of all i want to check if this file is readable if i have access to read it so i'm just gonna do is to say conf file.readable now this is just gonna return a boolean value true or false so let me run this so now it says no such file in directory countries.txt so let's make sure so what we're going to do is to navigate into this folder and make sure we create that so let's go there i think i have it in um right here so this is in projects okay and then jungle so i think i have it right here and then there is countries.txt c-o-u-n-t-r-i-e-s c-o-u-n-t-r-i yes so it says that we can't open so it says no search file or directory so what we just need to do is to do something like let's um add this although this is not the reason why we're having this error so when we run this it gives us that arrow so the best thing to do is to just take the main directory to that and then paste it and it's the slash like this so now let's try to run it again now you see it says uh it gives us this error now the reason for this area is this slash right here so we're just going to change the slash to forward slash and right here also and then when we run it again now you see it says true so the reason we add that arrow is because right here is always in a backward slash but we are using a python it has to be in a forward slash so now we are successfully navigating to that countries.txt so when we say print confirmed or readable so we're asking it do we have access to read this file it says true so if we don't have access if we can't read it it's going to tell us false but now that it says true then i think we are good to go so now that we can read this file what we just want to do is to print something like we can say count file dot read line now when i say this red line is gonna print the first line of this file which is ghana so once i print this you see now that it's print gunner and this is like a sequence so if i print this file again if i say print and i say count file dot redline again now what it's going to print is the second line and notice that i didn't make any change the same code but when i run it it prints ghana and mexico so i can literally do this and just print everything i have in that file but the most recommended way is to just say confirm the print lines now prints lines is going to print everything for you in a list so it's going to print everything out the old lines of you have in that file and then store it in a list and now we can just get let's say the first one because they want to get the first one now we are going ghana you can get the last one now you see we have france so that's how to use the red lines function and then we can also make it more cool by looping through this so we can say for we can change this to a for loop for files in conf file dot red lines like this then we want to print we just basically want to print files [Music] so now when i run it you see just basically print all the old lines in which i have so the best way to call this is lines not files so lines so when i run it again the same thing it just prints all the lines i have once i come here and make some change like um let me say ghana is a country in africa and then i save it once i run it that change is automatically made if i move up you see right here the first line says ghana is a country in africa so i hope you get the concept of reading files in python these are you can read files read it by lines read it by text and stuff like that now let's move straight to the next part of this tutorial in this part we're going to be talking about writing files in python so in the last part we talked about opening and reading pyth reading files reading external files in python but in this part we're going to be talking about appending uh let's say some text to the file or just writing the word file again so for us to write a file right here we need to change this r to w so now when we change this out to w let's just remove these and then we can just say count file dot write and then what we want to do is to just write the offer now you can see that this is the file but if i input something here now like this is the new text now when i run this you see that nothing happens whenever i come in here you see that it has been changed everything that was there was gone so we wrote the whole file again now this might be useful let's say you want to write a new file it's very useful so the reason why it wrote everything again was because it was an existing file so it overwrote like a root on it but this is mainly used for creating a new file so let's say i want to have country now now let me say this is the new country file now once i save it and run if i come into this directory right here in my command i'm going to cd enter the directory okay it doesn't go to that directory but what this does is that it creates it it create this new file called country.txt in that directory so let's go there normally so right here you see that we have a new file called country let's change it and just say new file now when i run it let's remove this space when i run it you see nothing happens but right here new file is created now that's how to create a new file and automatically write something inside it so let's just control z back to the countries we were using so now i have this country's file let's say i want to append something to the bottom like a new text what i can just do is something like when i want to append i must make sure i change this w to a and then right here what i'm just gonna do is to just say file dot right then this is a new line now when i run this nothing happens in the terminal but when i come here it appends this is a new line to it but you can see it joined it together now let's say i want to form a new line i want this to be on line two i can just come here and i can say backslash n now this is automatically going to take you to a new line when i run it again and come here you see now now that this is on a new line so that is basically how to append in python now you can do this for various types of various file extension as you know we did this for the text document which is the txt you can even use a python file to open a new python file now let's put that in action now let's say we want to open a new file and name it new dot py and then in that new dot py i want to write and then what do i want to write in it so right here now let me just scroll this i can just easily write a python code i can say print this is a new file so right here i just wrote a simple python code and then obviously we have to use the box clash before we can write a code so right here i use this print this is a new file and it's gonna be in this new dot py so now when i run this you'll see now that we have a new dot py file and then i can just when i come here i can run new dot py you see now that it just simply prints out this is a new file which was the command of the line we put in here so it is also very important and very useful writing the new files in python [Music] now we're gonna be talking about classes and objects in python so i must say that python is an object-oriented programming language so this means that anything that deals with classes objects and things like that were classified as object oriented so classes is just like a picture in python saying something like a function a function is a picture that python has then classes is also a feature that python has but classes now is like a constructor of objects so you might not understand what i'm saying now but when i show you on the code i'm sure you'll get it more easily so a class is like a constructor of different objects under a class we have various objects so let's just go ahead and show how to do a class so now i want to code the class we're just going to say class using a class keyword then we can say my class and then under here we can say x equals to 5. now we have this class this is the class the name of the class is my class and then we have x which is an object on that is my class and x equals to five now we want to create an object this is actually a value or i can say an attribute under the class now let's say we want to create an object using this class what i just gonna do is to do something like p1 something like that equals to my class then we initialize it then we can say print p1 dot x now this is going to print 5 because we're saying that p1 is equals to my class which is this class then we're just saying print p1 dot x once i run this it prints 5 so that's how to basically print you now let's talk about the init function the all this thing i just said now those are the basic concepts of class but we can basically use them in real life let's say in reward things and reward projects but the init function allows us to initialize different values in our class now for example we have this class let's say this class is named person and then under that class we're gonna have a function i'm gonna take two underscores in it like that then it's gonna have some parameters self now this self is gonna be dead it's just there that's the way it is and then let's say it has name which we are gonna put and let's say yes age and then so we put our column just like a normal function so let's remove this and then to determine the name who says self dot name can be equals to name and then we oh my god and then we can do the same thing for the age so that h is equals to h just like that and then let's just initialize it the way we did here so p1 equals to my equals to person this time around but now we have to give it this name and this age so we'll say the name we can say the name is john and then the age is 87 now once we print p1 that's name you'll see that it gives us john okay so now it says indentation error so this is under this function it's supposed to be indented so let's run that again now you see that it prints john we can also print p1 dot h and then you are going to see that it prints 87. now we can also make this allow user to input so we can say name is equals to inputs i can say enter your name and then we can say age is equals to input in which you can say enter your age and then once we have the name and age of the user stored we can just replace it with this and say name and say h now when we run this first of all it accessed for our name let's say team 89 now he just prints team 89 so we can also use a class to do something like that so classes are very broad in python and it is very very used in python but there are still some other object properties that we can do like let's say we want to delete a property from this like we want to delete this edge so let's just remove this and then let's give it 13 john now let's say i want to delete this age from it what we can just do is to just say del p one dot h now once we delete p one dot h this is not going to have this h in it again and then we can just even delete this objects in total by just saying delete p1 once we run this this is going to be deleted if we try to print p1 after that it won't work now it says p1 name not defined that's because we've already deleted p1 there's nothing like that so we can print what is not there and then we also have one feature we can use in the class so right here let's say we have a class that is named person and for now we don't know what to put there we don't know the values and we just want to continue coding we can use something we call pass so this path allows us to bypass any error so if we have like this class that is empty we can just put pass for now then we can come back to like add attribute so once i put pass and i run this code nothing happens but if i just only put class person and i run you see it's gonna give me an error so this path is just to bypass that error so we can continue with our code for the main while so i hope you get the concept of class and objects in python now we're going to be talking about inheritance in python so narrative simply means taking from an existing class and then getting all the methods and everything in there and they're putting it in a new class so you're gonna understand what i mean in a bit so let's say we have an existing class in this file named new dot py i just created this file we actually created it earlier in this video let's have a class name student and then let's say that name is equals to team age is equals to 34 and then let's say gender is equals to male now we have this class here we can easily import it by saying we can say from new dot py import student which is this right here so now we are importing student so instead of saying from new dot py we can just say from new without adding py the extension it automatically gets that we are importing from this file but before we can import we need to make sure that this new and app are in the same directory so let me just cancel this we need to make sure they are in the same directory so now i have this new class named person what i just want to do is to just say this is student so i'm getting everything from this students class which we narrated and i'm just putting in this person class so i just put pass to avoid any error now i can say p1 is equals to person and i can now say p1.name i can just say print p1 does name now once i run this you see it prints steam all the way from new dot py file but you can see it's up dot py so that is the basic concept of inheriting in python it inherits every single thing which we have in this class and they just bring it right in this class so it's like a duplicate of darkest but not a duplicate because different names in different file so it's called inheritance in python in this tutorial i'm going to be talking to you about the python interpreter of the python shell so the python shell is an application that is automatically installed whenever you install python on your computer so let me just quickly open up my python shell so this is my python share right here as you can see idl is shell so let me show you how i open this up you can just come here and just search for idle if you have python installed it should automatically bring that out and just hit open but since i already have this open so the python shell is just like a small environment where i can run and quickly test some python code like everything we've learnt in this tutorial i can easily code them here like let me say print up you are enjoying this tutorial now once i press enter you see it prints that immediately it automatically runs that code as i press enter and we can also set like a variable here we can say name is equals to team and then we can now print name but right here in the python interpreter we can also print the name without even writing print if you just say name it automatically prints it out but that's only when you're just testing your code here in your python shell you can't do that in your main editor now we can do a lot of things like um i'm sure we can use a for loop let me see for letter in team yeah like that then i can just say print letter now you see it automatically prints team i can do anything in here i can even write a class and i can say the class should be pressing and then i can say name is equals to john and then right here i can say p1 is equals to person and then i can say p1 dot name now this is just going to print me john i can basically do everything i do in the code editor right here i can have an if statement let me say if p1.name is equals to john then print yes it is [Music] so now it's going to print yes it is because p1 does name is obviously equals to john and then we can also perform our basic arithmetic like two plus three gives us five nine minus zero gives us nine and everything which we can do on the normal place you can also define a function let's say we have a function to say i and then let it pass a name and let that name be a name and then let's say print i named now let's say i and let's give it john now you see it says i john so almost everything which we can do in here we can also do it in our main editor it's this like the same thing but it's not advisable to use these when building normal projects or stuff like that this is just basically for testing and anything like that so let's also try more things like try accept so try let's say um age should be equals to inch of the inputs of enter h and then let's close that so we wanted that to be age and then we just say accept yeah we must make sure we have the right indentation so except oh it gives us uh uh an error because of the intention but we can basically do all this right here in our python ideally is that what i want to show you so sometimes you might run into all this error of indentation syntax because is the indentation is not really obviously clear just like the way it is right here in visual studio code so that's why it's recommended to use your normal editor and not basically and ideally for editing but we get the concept of a python shell that's going to be all about the python interpreter so now we're going to be creating a simple sign up and login system in python so we're going to be putting in everything we've learnt in this tutorial and then building this simple program so as you can see here it says create your account now it says with your input username i'm just going to input admin and it says we should input password i'm going to input admin as a password now it says user admin created successfully login now so what this program does is that it creates this user obviously we're not dealing with real database we're just storing this in variable once we run this program again we have to create again so it's just for practices so once we create an account and it says user created successfully it says login now now when we try to log in if what we type is the same with this then automatically will be logged in but if it's different if our username is wrong or password is wrong then it will say invalid credentials now let me try to login with normal and i say admin now it says user logged in successfully let me run this again let me let me now create another one admin again admin now it says user admin created and wants me to log in if i now say admin by the password and i add to n and click enter you see now it says invalid credentials so this python program are going to build is going to get whether the password is right or the username is right or not so let's go straight into that so let's just quit this come back here you can also quit this so what we want to do we're going to take in four parameters so the first one is going to be the username and the password for creating an account and the second and the third and fourth is going to be username and password for logging in so let's do that let's say username will be equals to so this is the first reasoning for creating account because to an input i will say enter username [Music] and then for the password we'll say the same thing input enter password and then once the user has entered the username and password we can just print to the user and simply say that [Music] your account has been created successfully [Music] and then now we now to tell the user to log in so let's print again login now so now i want to have another variable which we use our name to should be equals to input [Music] enter username and then the password for the login password to input enter password so now we now to check if this is equal to this so just simply say if username is equals to username 2 [Music] and password is equals to password two so this is an if statement saying this and this must be equivalent that's why we use and if we use or it's gonna be wrong because if the username is correct and the password is wrong it's still going to lock the user in so want to make sure the two are correct it took corresponds before we log in so once it's correct we can just say print logged in successfully [Music] but if it's wrong else we just want to print invalid credentials like this so let's just come on to the top here and print something like create account now now this should work let's run it let's just so we create a username let's just say timmy the password see me now your account has been created successfully login now timmy and the password timmy now says logged in successfully let's run it again and try a wrong one now let's h or h y h y try hy now we now say the username is hyhy but the password is uiui now you see it says invalid credentials so that's how to build that basic python program using getting user input and if statement i hope you understood what we did here if you didn't you can just go back in like five minutes ago and just watch from there i'm sure you're gonna get it in this tutorial we're gonna be talking about modules in python so modules basically allows you to get the function class so everything present in another file it allows you to implement it and use those same function in your own file or project so modules is very very versatile is very widely used in python everybody use modules so let's say i have this file named new dot py now in here i have a function named say i and then that say i function i just wanted to print i now i have this function in here i can import this i'll just say import new now this new is standing as a module for this my file now right here i can just simply say new dot say i once i say this and run this file you see that it automatically says i so that's how to use modules in python now there are a lot of things we can do with modules and modules are very versatile so if you want to like i wanted to use a function that says i instead of coding it from scratch there's possibility that someone out there already written that same function or similar to what you want to do and already has it as a module or library in which you can get and implement so now modules aren't just on your local laptop on your local pc like this modules are hosted online so python has something we call pip which i'm going to talk more about in the next video the next part so all the modules are more hosted online let's say you're looking for a module that wants you to just do a specific task and you know that's yes of course someone else would have done this then you can just go and search for that module or search around research then you're gonna see that module online in the next part i'm going to show you how to implement pip and then install all those kind of modules on your computer so in this part i'm going to be introducing you to something we call pip now pip is used for installing external modules from the web to your local pc now as i explained in the last video what modules are they basically allow you to get a use function from another file or from another library so pip allows you to install a module from the internet to your laptop now if we go online and then we just search for something like python modules then we can just see let's say [Music] let's go to pypi so there's a site called pypi now this is where all the python modules almost all the python modules are being hosted as you can see 292 000 projects so it's just basically saying 292 000 libraries or modules that are hosted on this site now you can see that each one has different tasks they do now see this module is for spotify recommendation um let's search let's say we want a module like uh let's just go to the home page and then let's see what we want so you can also see trending project as trendy modules a lot of different things and then let's say we want to let's go to browse development or framework let's just click on this so now this is the name of our module let me look for someone that has a description so django deep serializer django is actually a web framework which allows you to build websites using python so this one is saying a jungle deep serializer so that's what this particular module does so but how do we install this on our computer we just type pip install jungle deep serializer the way it is being said here then how do we now where do we run this command if you're on a mac just open your terminal but if you're on your windows open your command prompt and then just paste it there pip install jungle iphone deep iphone serializer now if this is the library or module you want to use you can just run this command line and then it's going to go ahead and install this module or this library on your local computer now you can see it says down collecting django deep serializer then downloading django deep selector 0.1.3 and all those so it's setting up all these yeah so you see it says successfully install django deep serializer now i have this on my computer now i'm very sure that if i want to import this i'm just going to look for how they said we can import it just going to scroll down okay they didn't mention that here because we are not running the jungle project so you don't need to understand what i'm saying for now about jungle but this is how to basically install a module or library using pip on your computer now that's what peep is basically about and this peep you don't need to let's say download pip externally once you install python pip automatically installs with it that's why when once you're installing python it's good if they ask if you want to install all components it's good to list yes and then just add python to path and just stick everything so that all these components will be installed when you want to use them later so pip is automatically installed with python so if i come here and we just search for pip you see it says let me say pip python let me be specific so pip python is a package management system written in python used to install a money software packages so as i said pip is used for downloading or in this case installing packages so modules also you can also call them packages so pip is the manager is what downloads it and store it on your laptop on your computer so that's the basic concept you need to grab about pip so guys that's gonna be all for this tutorial i hope you learned something and enjoyed the video if you did please don't forget to smash the like button and subscribe and if you have any question maybe it's talk somewhere you can drop it down in the comments or you can contact me directly i'll make sure to answer all questions having that said thank you so much for watching and bye for now
Info
Channel: Code With Tomi
Views: 4,367
Rating: undefined out of 5
Keywords: python, python for beginners, python full course, python full tutorial, python 2021, python course 2021, codewithtomi, code with tomi, python web dev, python gui, python django, python flask
Id: nuC_wmAAG9A
Channel Id: undefined
Length: 216min 49sec (13009 seconds)
Published: Sun Feb 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.