Python Full Course 🐍

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys assalamualaikum welcome to live what a cartoon welcome to amigos code and in this video i'm going to teach you about python in this full course i'm going to make sure that at the end you are so comfortable with this popular programming language if you're new to my channel go ahead and subscribe and also give me a thumbs up so i can keep on recording these courses for you so in this course i'm going to take you through the beginning level of python and make sure that you understand key concepts so python is very popular and you can do a lot of good things with it whether you are interested in machine learning data science ai building websites building back-end services and a bunch more so i hope you are excited and without further ado let's go ahead and learn about this awesome programming language right before we kick off this course i want to make sure that you are part of the amigos code community i'm going to leave the link in the description of this video so you can join the private facebook group as well as discord and that way if you are faced with any issues or or literally any question that you might have you can ask them on the group and the group will help you so the group is almost 10 000 members right now and it's growing and you should be part of it also make sure that you practice as i teach right so when i teach make sure you practice if you want to take this course on your own pace go ahead and enroll for free on my website where you can skip the videos one by one without further ado let's go ahead and kick off this course the first thing that we need to do in order to use python is to install it in our machine so navigate to python.org and in this page right here you can see that we've got the downloads link so go ahead and click on downloads and right here because i'm on a mac i can see the download link for mac os but if you are on windows simply click on windows or linux you can simply use this the version specific for your operating system which is cool also right here you can see that these are the active releases so the active release right now so the latest one is 3.8 right here and if i show you so 2.7 has reached end of life so you shouldn't really be using this version right here and if i scroll down you can see that we can have um you know specific release versions right but usually just go ahead and download the latest version which has support until 2024 so right here see that the latest version as i speak is 3.8.5 you might watch this video later and if you haven't if there is a new version everything that i'm going to teach you in this course will work with the version that you will install so let me go ahead and simply download python there we go let's finish now let me open up the downloads folder and in here you can see that i've got python so let me simply click on it and let me just collapse this so the installation process for both windows and mac os is really straightforward you just simply have to press continue and then agree and then install so before i actually press or actually put my password if you are on windows make sure to tick the box that says add python to the path right so that's really important so that you can use python from your terminal so on the mac it does it for me so right here let me put my password install just give you a second and there we go so we've successfully installed python next let's go ahead and install a program that will allow us to write python code right for this course we're going to download and install pycharm which is an ide ie integrated development environment and what it does it allows you to write python code so it gives you all the tools necessary for you to write code in python i've got a full course teaching you this amazing ide so if there is things that you are not sure or you think that there are so many things going on don't you worry because i've got you covered i've got a full course just on pycharm and i want to make sure that at the end of this course oh actually at the end of both these courses right so learning python and then pycharm you have the tools ready there for you so you can start exploring all the areas without further ado let's go ahead and download and install pycharm when it comes to python there are two text editors which are the most popular one so you've got vs code which is amazing but there is a product from jetbrains called pycharm and this is by far the best ide for python developers so go ahead and install pycharm because it's amazing and i always recommend jetbrains product because the integration that they give you it's amazing right so what pycharm really does for you it's like a text editor but with a bunch of features that allows you to be more productive so with vs code you actually need to install a bunch of plugins to get the best out of it but with pycharm everything is out of the box so let's go ahead and download pycharm and in here you can see the version for let me just zoom this so you can see a version for both or actually mac linux and windows so go ahead and download the one for your operating system and also you can see that there are two versions so you've got a professional which comes with extra features for web development including html javascript and sql support but for most of the time the community edition is more than enough so go ahead and download the community edition which is free and you don't have to pay anything so i'm just going to allow just give it a second all right so that's done let me open up the downloads folder and right here you can see that we have the pie charm community so let's go ahead and double click on that and then in here the installation process it's very simple so simply drag and drop this into applications and there we go now let me simply search for pycharm and then open it up open right here simply say do no import settings if this is the first time that you are installing pycharm and then right here so if you've never used pycharm go ahead and stick with these key mappings you can also choose the actual ui theme so if you want light or dark so i'm going to go with dark and what i'm going to do is simply skip remaining and set defaults and there we go we successfully installed pycharm next let's go ahead and write our very first python program to create our very first program let's go ahead and create a new project and right here you can give it a name as well as the actual location where the project is stored so for my example i'm going to simply name this as my and then python and then app and then you see right here where it says base interpreter you can see that we have python 3.8 so this is the version that we've just downloaded so if i click on this drop down you can see all the different versions of python that i have but let's go ahead and stick with the latest one and basically what an interpreter is it's a program that executes your python code that's literally what it is and then right here you see that we have this checkbox create main dot py welcome script basically if it sticked and tick it because i want to show you everything from scratch go ahead and take the box if it is ticked and then simply create and there you have it so inside of this folder right here so my python app go ahead and open it up and inside what we're going to do is create a new file so right click new and then file and right here the name the name of the file simply call it as main and then dot and then right here make sure that you type py so this is the file extension so the same with for example javascript or java or golang you have for example go for golang java for java and then dot js for javascript with python is the same so dot py so right here let me simply press enter and there we go so what i'm going to do is press command and then one and you can see the keyboard shortcut that i've used down below so right here again command and n1 and you can see the keyboard shortcut down here so let's go ahead now and write our very first python program go ahead and type print and you can see that pycharm is giving me auto completion there so inside of this print block go ahead and simply add double quotes and right here simply say hello and then me equals code and you can even add your name so now let me simply press enter so i've got a new line and to execute this program simply right click and then you can see so right here let me do it again so right click in here and you can see that we have run and then main so this main right here is the name of our file so main dot py you don't see the actual extension but it runs the main file so let's go ahead and simply run and right here you can see that we have the low amigos code right here if i was for example to change this to hello and then mariam and then run you can see that we have mariam so there you have it this is your very first python program right let me first teach you a very important concept which you need to understand and that is variables so to understand variables just think about a box right so variable is a box right and with this box right here your variable you have to give it a name right so let's say that we have this box right here and we name it x for example now inside of this box right here the cool thing is that we can store values inside right so just to illustrate so right here i've got my pink phone and by the way don't laugh at me but inside i can store this phone right here inside so now my variable x that we initially called x has the value of my phone right so my phone is inside of the variable right i can close the box and this is my variable now right now if you want to access my phone i can access through the actual name x similarly we can store um so we're going to cover about objects what what this really means but we can store for example so right here um i've got you can see uh it says keep calm and bismillah but for example i could take all of those letters right and put them inside of this box right and i'll close it and let's say that i've taken all of that and i put inside of this box right um you know for example this could be for example your name right which is inside of this box which is your variable right so this is a sequence of characters you can also store numbers right if you wish so primitives ie one two three or decimal numbers for example the number of pi 3.14 um literally anything that you want you can store within variables so throughout this course you're going to learn about you know different data types so with variables right i've showed you that you can store a phone you can store a sequence of characters so those are the different data types by the different things that you can store inside of the variables but in this course we're going to create lots of variables and by the end of it you should be really comfortable working with variables but next let's go ahead and create our very first variables right let's learn how to create variables with python so a variable it's a placeholder it's a placeholder where you can store values so here let's have this variable right here called name and in here we're going to have this name called jamila so this variable so the name of its um it's actually name right and then it holds the value jamila and this is a sequence of characters right so j a m i l and then a now let's define another variable so age equals 2 and then let's say 20. so now we have two variables so this variable age contains a number so with variables when you define variables you can store literally anything that you want right when i say anything you can store numbers you can store lists that you will learn later but for example you can have numbers and then here this is a list so here i can store more than um two numbers right so you can see that this is a list of numbers you can have sets you can have custom objects anything that you can think of in the real world you can model in code and then store that in variables right so variable so variables are placeholders that allows you to store values when working with variables this is something that you have to be aware of so here you see that we have age and i can say h equals two and then pretty much just a string right so h equals to sad so here now what i've done is i've reused the same name but then i did put something else inside and in this case is a sequence of characters so you're going to learn about data types and how to avoid this but for example if i say this will be an integer which is a whole number you can see that now pycharm is telling me that expected type ain't got str instead which is a string data types and you'll learn more about data types in a second so let me just delete this because i don't want to confuse you so just remember that if you have a variable like this you can do it if you want a decimal number so here i'm going to say pi equals to 3.14 and this is a decimal value here right so you see that we have a whole number decimals lists this is a sequence of characters or strings this is how they are known as strings and yeah so this is variables now one little trick that you can do is the following you see here where we have name and age we can actually combine all of these into one single line so i can say name and then age oops comma age and then here i'm going to say comma and then 20. so this is the same as this but in one line if you want it right so this first name is assigned to this value so jamila and then the second variable name is assigned to 20. finally let's delete this variable right here h because we already defined it here just like that and let's print all of these values so print and then name print age print pie and finally print numbers to run this i can right click run main or click the play button and if you don't see the play button is because so right here let me just go to edit configurations and i'm going to delete this python and then main and then apply ok and you can see that it's no longer there right so what we need to do is simply right click and then run and now the play button comes right here so here you can see now the results from our variables so jamila 20 3.14 so that's pi and then our list and this is how to create variables in python so throughout this course we're going to create lots of variables and you'll become really comfortable working with variables when it comes to naming variables with python you've got couple of choices if you have a single world like name as we had before so here let's say james so this is the variable name if we have full and then name so this is two words we can do the following so if i type full name equals two and then j and then james so you can see that this is the actual full name right you can use camel case right so this is the first word is all case and then the beginning of the second word you start with a upper case so this is common case or you can have it like this so full underscore and then name so all lower case so these are the conventions when it comes to python now obviously try not to use for example names with for example ampersand so this will not work or names with dollar sign um or names with comma for example right or dot in it so they will not work right you can also have for example um let's say age or upper case you can do it right so this is usually when you want to define a a value that does never change like a constant age is not constant because age is a value that keeps on going up and up and up right so a constant would be something like the value of pi right so this this value does not change so 3.14 but usually speaking for single words you'd start with lowercase so never with for example name like that don't do this right don't do it it's not the pattern so always start with lowercase if you have a second word either use underscore or camel case right so common case here would be for example like that so there you have it if you have any questions on naming variables please do let me know otherwise let's carry on when you define a variable you need to specify the value that goes inside of that variable now that value has a data type so let me go ahead and simply say brand equals two and then amigos and then code and right here h equals two and then two and right here numbers equals two and for now simply add these square brackets and i'm going to show you exactly what this means in a second but for now i just want to show you the actual data types so go ahead now and print and then type and then within parentheses and then pass brand let's also duplicate this so i'm going to press command d a couple of times and right here i'm going to pass h and then right here i'm going to pass numbers so let me actually delete that space there and in here you see that we have three variables and we want to find out their data type so let's simply run it and check this out so in here you can see that we have class and then str so str simply stands for string and then we have class and then int for integer so this is the actual whole number that i've mentioned and then in here for numbers you can see that the data type is list so let me actually give you another example so in here i simply have the value of pi equals two three oops three point one four and let's also print the actual type of pie so let me duplicate that and then in here pi just like that run it and in here you can see that the data type for pi is float so float simply means that it's a decimal number right so anything that has a decimal number so in here three point it's a decimal number whereas age is a whole number and a whole number simply means that it's an integer so there you have it obviously you do have some other data types such as dictionaries sets and so on but i'm going to show you later in this course how to work with those so one final data type i want to show you is so right here let's say is and then adult and then equals to true so capital t and then r u e and let's now also print the actual data type so in here is adult and then run it and you can see that the data type for this is actually bull so a bull or boolean represents true or false so the the only available boolean values that we have with python is simply true or false right so oops there we go so this is false when working with python you will hear people saying that python is a dynamically typed language and what that means the data type of any variable or function is checked at runtime so here you saw that we had the brand so here amigos code right so this so the data type for this is actually a string now unlike languages such as java right you would have for example the same is brand equals to an enemy course code so here the differences is that with java and don't worry about this this is java and if you want to and if you want to learn more about java i've got full course on java just on java just like this one but here the key thing to know is that you have to specify the data type here right whereas in python it is optional you literally don't have to right the only thing that you need to do is say brand just like that and then you say right so now this is my content now if you want to be explicit when defining your variables i.e saying that this is a string you can do it right you can do it so you can say column so after the name of the variable and then the data type so str and you've learned about data types the same with booleans so if i say is and then adult equals two and then let's say false so here if you want to be clear about it you can say dot and then boo so this is a boolean right and this helps all the developers looking at your code knowing that this is actually a string the data type of this is a string however you won't see many people doing this with python right because python the reason why python is is simple and easy to learn is because it removes all of these keywords that other languages have right so for example if you have a method you just say def and you learn more about methods and then hello and this is your method right so here i can say return and don't worry about this for now you'll learn all of this so return and then hello right so here you can see that the method is very straightforward this method is really easy to learn in java for example you would need to do something like this you need to say public and then string and then hello the bracket the and then the curly brackets and then return and then hello right just like that and here so you can see that this is much easier literally it's much easier for beginners and to express whatever you want to do right so this is why python is very attractive and has gained a lot of popularity for its simplicity so in this function as well and let me just comment this you can have a reference so in this function if you want to say that this function returns an a string you can say here you can say that this will return an str just like that and it's a bit different with functions but this is how you say that this function will return a string because here there's no way for me to know whether this returns a string or or an integer because here i could say for example one right and you can see that you know there's nothing stopping me of doing that however if i now say here for example that this returns an int right you can see that this is actually complaining so it's saying expected int go string instead now i just have to change this to uh one right so this is the correct return type for this method right here but usually you will see a lot of code without these types because you know the less you write the better and and when python came out it didn't have these concepts of actually specified of actually specifying the types so this is something to bear in mind when you work with python and know that python is a dynamically typed language which means that the data type is only checked at runtime and not compile time so you can see the way that we can enforce the data type is by saying right so this variable has to be a string or a boolean for example but in this course we're not going to worry too much about this so specifying the data type for each variable that we create because it's not something that you'll see quite often we can have comments that describes what the piece of code or functionality is all about so to have a comment is very straightforward add the pound sign right here and then here i'm going to say i am a comment there we go so this line right here won't be executed so right here this is just a comment if i run the program we should see hello and nothing else so if i was to comment out this line right here guess what's going to happen so because this is a comment if i run it you can see that we have no output so comments are used for documenting your code so right here let me simply go back so we have two types of comments we have single line comments or we have multi-line comments so there are two ways that you can do multi-line so for example here if i want to have another comment so a second and then comment or if you want to have a third comment you can also have a third comment and by the way i'm simply pressing so if i delete that i'm simply pressing command and then forward slash and you can see that i do get this found sign right here so in here third and then comment right so i can do this or i can remove all of this and press the following so i'm going to press quotes three times so one two three and then inside i can have my comments so right here i don't need the pound sign anymore oops just like that and now this is actually a multi-line comment right here so if i was to run this program you can see that we only have hello and if i move so in here if i move print inside you can see that this becomes part of the comment and if i run it you can see that we get nothing on the console so this is all about comments let's learn about this awesome data type which allows us to store sequence of characters so you've seen that we can have for example brand equals two and then within quotes so right here i can see amigos code and now this variable right here brand has this text so this text right here is a string so it's a sequence of characters with strings we can enclose them with double quotes or single quotes just like this now the cool thing about the strings is that they are very useful if you want to store literally any text right so imagine you you have a program and you want to store some text that you can then perform some actions upon so right here let me go ahead and simply say print and then brand and if i run the program you see that we have amigos code right here now what is interesting is that with any given string right here i can say dot and then right here you can see that we have a bunch of methods right here right so in here let's say that we want to uppercase every single um character in this string right here if i run this program you can see that now everything is uppercase so right here have a look this now is all uppercase so these methods right here that help us to work with strings so right here i can also say for example replace and then a well actually this has to be within quotes so capital a with and then lowercase a just like that so if i run this you can see that now i've only replaced the first a right there right so now it's lowercase but let's say for example you want to replace with let's say 33 run it and you can see that this now has been replaced so this is strings in a nutshell and let me also show you another method so let's say that if for example you want to find out so right here let me actually comment this for you so if you want to find out the actual length right so how many characters that this string right here brand has simply type uh let me actually show you a nice trick so simply type brand and then dot and then len so if i press enter you can see that pycharm is doing its trick so basically we have to start with len and then surround with parentheses on both sides and there you have it so if i run this you can see that there are 10 characters or 10 letters inside of this brand right here another thing that you can also do with strings is you can compare them so if i simply say print and then right here say brand and then equals and then in here i'm going to teach you more about this what it means later but basically this is saying that i want to check whether the brand is equal to and then amigos and then code and if i run this you can see that we get false right it's not the case but if i change this to uppercase and then run it you can see that now it is true so you also have not equal so not and then equal run it you can see that it is false because they are equal and if i change this so this is now so here you can see that they're not equal because this brand right here starts with uppercase a and this one starts with lowercase a so if i run this this is true right so they are not equal finally let me show you one really nice thing and that is if you want to find out whether this string right here so the brand so let's say that we want to find out whether the word code is in and then brand just like that right so we want to find out whether the word code is in the brand so if i run this you see that it is true right so if i say uh not and then in so that's the reverse run it and you can see that it returns false because it is inside right and the and the result of this is a boolean right so boolean can take only true or false as you saw previously with data types so there you have it this is pretty much strings in a nutshell next let me go ahead and show you some more examples on strings still on strings let's create a variable called comment equals two and right here let's say that we have you know some comments right so here and now let's say that we want to for example have another line so you can see that i've just pressed enter and we have this backslash there and if i press enter you can see that i'm getting backslash and basically this is now my string right so if i pretty much just say print and then comment and then run this you can see that we get this comma right here but this is actually everything in one line and basically um it's not it's not really what i want right so what i really want is to have things lined by a line right so if i collapse this so what we can do is in cases where you need to have you know for example long strings you can simply get rid of all of this and then type double quotes and then double quotes and then double quotes so now you can see that we have three on each side and then press enter and now in between you can write whatever you want so here so that's my comment one more and basically you get the chest so if i run this you can see that now check this out so the output so right here let me show you so this is exactly what i wanted right so this is really nice with python and many other languages are actually lacking this feature let me collapse this and what i want to show you is let's actually say uh something here so let's say hello and then uh this is like an email right so let's let's let's simply say um let's change this to an email in fact so email and then email here right and then here type these curly brackets and then comma and then how are you and then maybe it was oops it was nice and then talking to you something like that right now let's create a variable here so let's simply say name equals to and then jamila and now what we want to do is actually pass the name so this name and fill the gaps in here so this is what this um question mark allows us to do so here i can simply now say email and then dot and then format and then pass name there we go and if i run this you can see that we have hello and then jamila right so basically we've just fill in the gaps and you can have as many of these as you want and this is really nice when you want to when you have a long string and then you want to format the actual output so another way that you can do this is with so in here so what i'm going to do is simply remove the format just like that and simply type f at the beginning of your string just like that and you can see that now the ide is telling us that we need to pass something in here so now we can actually pass the name directly and you can see that this is much neater so if i run the program you can see that we get the exact same result right and even here you can also have so let me actually say for example age something like that right so and then we can have the actual h in here so we can actually pass a value inside and for example if you want to compute something let's say four plus four you can even do it in there right so if i run this you can see that we get 8 in there so there you have it so i just wanted to cover these two things with strings because i feel like it's very useful and you'll end up using them a lot if you have any questions on the strings please do let me know otherwise let's move on all right so we've had a great introduction about python and you've seen variables and strings and before we move further what i want to talk to you is some of the rules that you need to be aware when working with python and the first one is indentation just like that add a comment in there so basically with python python is very very different from languages such as java golang javascript so on and so forth so right here let me actually go ahead and create a variable so i'm going to say name equals two and i can put this a bit bigger so name equals two and then let's simply say maria and right here if i want to have for example surname right so sir name equals two and then double quotes and then jamila and basically if i want to do this right here this is not allowed this is all this is not allowed because with python things have to be indented so here there is no need for us to actually put this um how many spaces is one two three four right so four spaces and then start the actual variable so we can't really do this with python with javascript we could definitely do it but right here you can see that we have an expected indent so basically this has to be like this in line so when would you indent things so let me actually give you a very good example so here let me go ahead and cut this and for now type def my and then function and you haven't learned about this so don't worry i'm just teaching you about indentation so right here have column and now press enter and now i can paste my two variables right here and you can see that this now works so this is the perfect time to indent things because this is a function right here and everything that falls under this function should be indented so right here if i want to put it in the same level as the depth right here you can see that this does not work so literally you have to indent things in python right here you can see that it's indented and also you can see that my id is complaining about this function name right here so you can see that it says function name should be lowercase so right here what i'm going to do is type my and then function add an underscore there so this is the actual convention right here so another thing that i want to talk to you about is so right here i want to talk to you about is reserved and then keywords so what reserved keywords are are keywords that are specifically for the programming language for example def right here so i'm not allowed to use def anywhere so for example if i want to say you know name equals to and then death i can do it because this is a reserved keyword or for example and or is these oops these are reserved keywords so i can only use them to construct my code so right here what i'm going to do is actually delete this and at the very top type import and this is a reserved keyword and they simply import keyword so what this gives us so what this gives us so this import and then keyword we can simply now say print and then type keyword and then dot and then kw and then list so in here before i run this you can see that we have these quickly brackets so basically it says that we need to reformat the file so right here let's format the file and it simply adds the correct spacing in between so if i put this smaller so you can see that now things are formatted properly now let me go ahead and run this program and you can see that this gives us a list and you can see that all of these are the reserved keywords so async wait break class continue you've seen death else except for from import so in here import lambda not so on and so forth so these are the reserved keywords which are specifically for using when writing your python code so you can only use them for constructing your code and nothing else and finally what i want to show you is with python right here you see that in languages for example such as java or javascript you need to end every single statement with a semi column for example here right so javascript is is actually optional but with java for example you need to have a semicolon after every single statement so right here and also right here but with python there is no such thing so right here we use no semicolons also you can see that for example for this function right here instead of curly brackets in between so many languages use it so including javascript java go c-sharp this is how they define functions right and basically this is not a valid syntax with python because with python we use indentation right here so we use indentation and this is how we tell that this block of code right so name and surname belongs to this function right here so it's enclosed within this function so there we go this is pretty much everything about the python syntax indentation and reserve keywords let's learn about operators so operators in python are a construct that allows us to manipulate a value of an operand so right here let's consider the following example so right here type result equals two and then right here i'm gonna say one and then plus and then two so in this example right here and if i put it bigger just like that so 1 and 2 are the operands and the plus sign is the operator so we've got arithmetic operators we've got logical operators we've got comparison operators and others so in here what we're going to do is actually say print and then result and then run the program you can see that we have 1 plus 2 is 3. so what i'm going to do is actually inline this so you can see the keyboard shortcut down below so control plus a and then n so here let me actually duplicate this and let me close that so let's learn about the arithmetic operators so here we have if you want to multiply numbers together you can add a star so right here so two times two and you can also divide so two divided by two and we have the famous modular so oops it's not that one is this one right here right so let's say nine mod two for example right so how many times two goes into nine so two goes into nine four times and then we have one number left which is the result so right here let's simply run this and you can see now we have three four right so one plus two three two times 2 4 and then 10 divided by 5 or actually by 2 my bad is 5.0 and then right here the modulus right here so if i was to for example change 9 mod 3 so 3 goes into 9 3 times and the remainder right that's what the mod or the modulus is is the remainder right so there is no remainder so if i run you can see that it is zero one more example so ten so three goes into ten three times right and then we have one left so run it if i also change for example seven so how many times seven goes into ten just one time how many remainder three if i run it you can see that we have three and i almost forgot so the last one that i want to teach you is the power so right here let me duplicate this so you saw that multiplication it's one star but we also have two stars so let me in fact put this right next to it just there and then press star and then star right so let me put simply 2 so right here what this is saying is 2 power 2 right so if i run this you can see that 2 power 2 is 4 and if i change this to 2 power and then 5 right so 2 times 2 times 2 times 2 times 2 so basically um let me just add a comment here and then times two so basically five times right so if i run this you can see that we have 32 so there you have it this is about python arithmetic operators next lesson about the comparison operators with python we have comparison operators and what they allows us to do is to compare values and return true or false so in here let's go ahead and simply say print and then what we're going to do is say 10 and i'm going to compare 10 with something right so let's simply say 10 and 5 for example right now in between i can add these comparison operators so this one right here is simply greater so this is saying is 10 greater than 5 is 10 bigger than 5 right so if i run the program you can see that yes of course 10 is bigger than 5. i can duplicate this and i'm going to show you exactly all the comparison operators so if i collapse that so now we can say is 10 greater or equal there we go so is 10 greater or equal to 5 of course it is right but if i change this to 10 for example and then run it you can see that this is true but if i say it's 10 greater than 10 of course it's not right we get false there so let me actually now put the equal back and let me show you the rest so in here we have the um less sign which is the reverse right so less and we also have the not and then equal and finally we have the equal right so is 10 equal to 10 of course it is so if i run this you can see that we get a bunch of booleans right true right so let's start from the top so 10 is greater than 5 true 10 is greater or equal to 10 of course it is it's 10 less oh actually i should have removed the equal there right so it's 10 less than 10 that should give us false so if i run it again you can see that we have false in here so this is the output and then 10 less or equal to 10 yes and then 10 not equal to 10 you can see that it is false and then in here 10 equals to 10 of course it is true so there you have it these are the comparison operators that you have within python next let me go ahead and teach you about logical operators now that you know how to work with comparison operators let me teach you about logical operators in python so let me actually remove all of this here and if i put this a little bit bigger so you can see probably so what do logical operators allows us to do is to combine multiple expressions so in here you saw that 10 right so if i run this 10 is greater than 5 right but i can also use the logical operators so in here i'm going to add a second parenthesis so a set of parentheses and now i can also say and right and let me just make this a tiny smaller and then within parentheses again i can say that let's simply say one right is greater than three right so this is the logical operator so it pretty much allows us to combine one or more expressions so if i run this you can see that it is false and this is because when you use the and operator every single expression has to evaluate to true right so this has to be true this also has to be true right so now if i was to reverse this right and say that one is less than three now we should get true so everything will be true right here so obviously i can combine so i can even say and so in here i can say and and then let's simply use strings so you can even combine strings here so a and then equals 2 and then a right so let me put this on a new line just like that right so you find in this there you have it so just like this right so now if i run this you can see that we get true but now if i change a to for example c so the whole thing is evaluated to false so this is and and i can even drop these parentheses if i wanted so this simply allows me to group them together right so this is one expression another expression and then this is another expression so the other logical expression that you have that is available to us is the or so basically oops so basically or means that at least one expression has to be true for the for everything to be true so in here i can say or instead of and and then or in here as well right so here now check this out if i run this this entire thing will be true but now if for example i say that 10 is less than 5 and then 1 is greater so it's greater than 3 which is not the case or a is equal to lowercase c so in this case this is false right so this is false this is false and this is also false which means that everything will be false but now if i for example say that a is equal to a now basically we only need one one expression to be true for the entire thing so right here you can see that now we have true the last logical expression that i'm going to show you is the not so not simply allows you and actually just before i move on so here you can see that we have or but basically you can combine all of these expressions right or with and and you can use them throughout your code so you don't have to specifically just use and you can also use both so the last one i want to show you is the following so let me simply say that in here i have a so let's have a or let's simply have um james and then equals two and then maria right so in here if i print this you can see that it's false right because these two strings they are not the same right and right here what we're going to do is actually say not so here i can surround everything with a not and basically what not does it reverses this expression right here so now this will be true because i've said maria is not oh actually james is not equal to maria so if i run this you can see that it is true so this is everything about the logical operators and obviously you'll see exactly how everything will fit together in a second if you have any questions drop me a message otherwise let's move on right the last operator i want to teach you is the assignment operator so you've seen this but i didn't explain exactly what it meant but the assignment operator allows us to assign values to variables so you've seen that we had for example brand equals two and then amigos oops amigos code right so in here this right here so the equal is the assignment operator because we are assigning a new value oh actually we are assigning a value to this variable right here now i could also go ahead and simply say brand equals to right here and then something else right let's simply say nike for example so now if i print brand you'll see that we get the latest value which is assigned right so initially it was amigos code but then we change it to nike so let me teach you about another assignment operator which is going to be useful to you so number equals to zero so i can say that number is plus and then equal to one if i print the value or actually if i print number run this you can see that we have one so what this assignment operator is doing it's simply taking the initial value so number and then adding one right so plus equals is the same as so if i simply add a common here so actually let me just grab this pound sign so if i add a comment here so this is the same as saying number equals to number and then plus and then one is the exact same thing right so in fact if i remove this just like that and then run it you can see that we get one right if i change to 10 we get 10. if i change the original value to let's say 14 we should get 14 now right so instead you use this assignment operator which is much shorter in here so just like that in here you can also say that instead of a plus you can say minus you can also say times right so 4 and then times so you take the value and then you times it by 3 is 12 you can even divide as well so division just like that and then by 2 right so if i run this we get 2.0 if you if you want to power the number you simply say star star and then equal and then two if i run this you can see that four power two is sixteen i can change this to three there we go sixty four so these are some of the assignment operators that you will end up using most of the time i'm going to leave some more links around these operators so that you can go and learn more about them but this is pretty much everything you need to know about these operators when you start writing applications with python you most likely will be performing lots of decision based on certain conditions and this is when if statements come into play so if statements they are very easy to use and basically it allows you to perform or actually execute a piece of code when a certain condition is met or not so in here let's say that we have a variable and then we're going to have this variable to be 15 right here right so this will be the variable 15. now what we want to do is actually print to the console whether the number is positive or negative so to do that we can use the if statement so we're going to say if and then number is greater than zero and you've learned about comparison operators so this is why i was saying to you that everything will come into play now remember when we've learned about indentation right so now i can add a column and then enter so this now so this block right here everything that we type from this indentation right here belongs to this if statement right here print and we're going to say number and then pass number is and then positive just like that so let me just put this smaller just like this so you see everything right so now let me add a space there and let me save this right so now if i run this program you can see that we have number is positive right so number is positive so if i change this to let's say zero right you can see now we get nothing because we said if number is greater than zero right but the number is not greater than zero right it's actually zero so in that case we're going to skip everything within this uh block right here right so what we can do is say right so if it's not equal well actually if it's not positive right so we want to say else so we're going to print and then let's actually print the exact same thing here and or actually in fact let's just have this so f and let me just remove this number here so it's shorter right so here i'm going to say number is and then negative just like that so if i now run the program oh i didn't want that for sure i wanted this so if i run it you can see that zero is negative well actually zero is not negative right so and i'm going to show you exactly in a second but for example if we have um minus one you can see that minus one is negative if we have 2 2 is positive so you can see how if statements work now if we go and say that 0 right so let's simply say 0 right here we're getting zero is negative so let's actually use another keyword that we can use with if statements so in here i'm going to say that if the number is greater than zero we're going to print this and then in here i'm going to say l and then if right so this is another condition that we're going to check so if number equals to 0 right i want to say print and then f and then the actual number is and then zero or actually zero is zero but you get the idea right so now if i run the program you can see that zero is zero right and let's actually change the message here so we're going to say that it's simply zero right so let's just print the number right so there we go zero so this is pretty much everything about if statements so in here you've learned about comparison operators and in here you remember right so remember that this is a block right this is a block we can actually combine with logical operators so we can even say for example here i could say not right i could say not and now everything flips everything flips around right so if i run this you'll see that right so basically if i say that now this is actually 10 and before remember that 10 was positive if i run it you can see that it says now 10 is negative and that's because we've just flipped the actual condition right and you can also have ands so on and so forth this is how you use if statements for making decisions let me just emphasize something with if statements so you saw that if you want to use if statements it's not necessarily that you must have for example alif and then else is not necessary it's not because you saw that if i remove this and then run it you can see that we have 10 is positive right and i can even say now here so i can say print so right after it and then hello i can even say that and you can see that we have 10 it's positive and then 10 is positive and then hello right because so in here we've reached this branch and then right this is true we're going to print this and then we're going to carry on our code right so this gets printed so in here you saw that the l if right allows you to perform extra conditions extra checking right if the number is equal to zero print number right and then you have the else so the else is the last option if none of the above is met and one thing to bear in mind is that you can have as many lifts you want so in here oops let me just grab this again just like that and then put it here you can have as many of these right so oops just like that right and just bear in mind that you can only have one if and then you can have as many alifs and then you can only have one else right which is the last thing if none of the above is met in here so this is so i just wanted to point this out because you know sometimes you might end up doing things like this where the first condition isn't met then you have this the second one the third one and then you have the fifth one and you know the last resource is else when none of the above is met then you want to perform something else this is all for if statements next let me teach you ternary if statements which are really awesome right let me teach you about the ternary if statement so you saw that we can have if number is greater than zero we can say that the number so we can say print and then positive and then if it's not the case we can have else and then print um zero or negative right so in here you see that we have four lines of code right four lines of code for whether the number is positive and if it's positive we perform uh line four otherwise we say zero or negative right so what the ternary if statement allows us to do it's simply to have everything in one line so let me actually show you so in here say message just like that and now i'm going to say equals 2 and then check this out i'm going to say positive and then if and then i'm going to have my condition here so number there we go and then else and then this so zero or negative just like that and basically if i make this smaller you can see that we actually collapsed everything into one single line which is really cool right so this is the power of ternary if statements and to be fair you should really be using them only like only if you have one if and then else right as soon as you have l if as you saw before so l so here l if you should you should you should you can do it with these ones right so basically you can have another uh if in here right so you can have another condition but it becomes just you know too much you should really be using them for cases where you only have one if and then else and that's it right so now i can say print and then message if i run this you can see that we have positive and if i change this to let's say -1 run it zero or negative this is how you use tenerife statements with python so you've seen that i showed you this example where i simply say type and then in here we had this curly brackets right here actually a square brackets right so if i run this code you can see that we have list right so this is actually a list so what a list is is the following right here what i want to do first is collapse this so we have this variable right here called number equals to one right let's have number two and then equal to two we can also have number and then three equals to three right and you get the gist right but so what we can do instead is to collapse all of these three variables into the following i'm going to say numbers so this is the name of the variable and now i'm going to have these square brackets and now i can go ahead and simply say one two three and let's simply put four in there right and basically now i can get rid of everything so now this is my list that contains all of those numbers and if you want you can even have um you know anything inside here right so for example if you want a letter a inside or if you want another list right you can definitely have it right but it's a bit awkward to have a list with different data types inside right so for now just let's just keep it simple and have for example minus one and then in here let's have zero right so what i want to teach you with lists first is how they work so in here you see that we have one two three four five six elements so the way that you access an element within the list is as follows so if i say print and then numbers right so let's just run this you can see that we have our list right but let's say that i want to grab only this number here so number one so the way we do it is by placing these square brackets and then we pass an index so this is the actual index right so index zero so in many languages including python when you work with lists this first element is the index 0 this number 2 is index 1 this is index 2 index 3 index 4 and index 5. so now if i run it you can see that we only have 1 right so if i do index five you can see that it's zero so you know i can even change this to uh minus or actually minus 20 for example run it you can see that it is minus 20. so this is something to bear in mind right and if we try to access so let's try to access index 6 and we know that this is index 5 and it's the last element right so this will actually throw an error so if i run it you can see that we have list index out of range and you can see the index error right here so something to bear in mind when you work with lists so as i said you can pretty much just have a list of anything right so here let's simply say that we want another list here and let's have for example um a and then b inside or actually just b like that right so now this is index and then six or actually six if i run this you can see that now we have another list a and b and right here we can even say now right give me the first index and this will give us a right so if we want index 1 that's b and index 2 that that's not existing and we get an error again but you really wouldn't have um you know a list with different data types right so for example you can see why you would have a list inside of lists for numbers maybe you want to you know build like um a matrix for example and you can do it with lists within lists so there you have it this is the introduction on lists next let me show you some of some of the available methods that are available within lists right so let's learn about some of the useful methods within lists so in here let's take this numbers list right here and at the end simply say dot right so in here this gives you again a bunch of methods or functions that you can use on the actual underlying list so for example if you want to sort your list you can simply say dot and then sort so if i run this we get none and this is because this actually doesn't return a list right so what we need to do is simply say num oh actually um print and then numbers here so if i run this now you can see that the lace is actually sorted so here we started with minus 20 or actually we ended up with minus 20 at the end and now we start with minus 20 1 1 2 and then 3 4. so now it's sorted if you want for example the reverse and let me comment this out for you so you can say numbers dot and then reverse there we go if i run it you can see that now it's the actual reverse right so 21 and then four three two one so basically we've just reversed the order of our list if you want to append to the list you can simply say numbers dot and then append so here we're simply adding let's say 1 000 right so we're just adding a number to our list there we go there we go if you want to find out the number of elements inside of the list you can use len right so i can say print and then len and then numbers and if i run it there we go and you can see that we have seven items inside of the list we can also remove every single item inside of this list so if i comment this and if i say numbers dot and then clear and then run you can see that now we don't have anything right so we've just cleared our list and finally what i want to teach you is if i run this again so you see that we have the list let's say that we want to find out if right so we want to find out if um any given number is within the the actual list so for that we can say numbers or actually we can say the number that we want so for example 5 and then in numbers if i run this we get false i can say five and then not in numbers and of course it is true right so if i oops if i say that we know that we have minus 20 there so minus 20 oops no minus 10 minus 20 inside of numbers run it and you can see that we get true so there you have it these are some of the common methods that you have within lists if you want to for example delete a number right so for you to delete a number so let me actually comment this oops just comment this and then print and then numbers right so if i run this so let's say that we want to delete this number right here right so number one so we can say so in here we can say numbers dot and then remove in here so we want to remove one and run it you can see that one is now gone so if we want to remove -20 oops minus 20 you can see that minus 20 is gone right so if we have so in here if we have two ones right so two ones and then if i say remove one in here run it you can see that it only removes the first instance right so it only removes the first instance that it finds we can also remove with pop so in here i can say pop and there we go so pop removes the last element so in here you saw that we had 20 right so this is the actual top of the stack um and pop simply removes this one and then goes from the top you can think of of it being like this is this is the actual bottom of of the list right and this is the top so pop starts from the top of the list and then it goes and then removes right so if i say pop two times should remove minus one as well so if i run it there we go you see that we removed minus one so i just wanted to show you um how to delete because it's something that you will do quite often another way that you can delete is if you say for example so here let me add a space you can say so del and then numbers and then in here we can simply pass the actual index so run you can see oh actually we are popping uh the other two numbers right but if i comment this for now you can see that we had two ones so here two ones and let me just remove this one here so right here if i run it we should see two right so we've removed um the first element if you want to remove two instead so two is at position or actually at index one run it you can see that two is now gone you can also delete by a range so let's say that you want to delete everything from everything starting from zero so index zero all the way to indexed three you can do that and right here let me just show you so we're going to remove 0 1 2 and 3 and we're going to be only left with minus 1 and minus 20. so if we run this there we go so actually we have four so yeah basically it's actually going um so three is actually uh exclusive right so it doesn't remove this one right so it just removes these three actually it's not index based right it's from zero all the way to three and three is actually exclusive so if you want to remove let's say all the way to six run it you see that everything is gone let's try seven and basically it's not it's not giving us an error um as you saw with accessing the index 7 because it's not is not there but here we get an empty list so there you have it these are some of the ways that you can remove items from any given list now let me teach you about sets so with lists you saw that we had this variable right here numbers and inside we can literally just put anything that we want right you can put numbers you can push strings you can put lists within lists so on and so forth right you can put the entire world inside if you wanted to so sets is somewhat similar to a list but the difference here is that with sets duplicates are not allowed so let me actually show this to you so in here i'm going to start just have a numbers and then list i'm going to call it list and here let some let's have a numbers and then set so the difference here is that which sets you have these curly brackets instead of square brackets so here if i have one and then one so let's print and then number list and then let's also do the actual uh setups just like that number set and then run this you can see that we have one one but inside of our set duplicates are not allowed duplicates are not allowed so something to bear in mind is if you don't want if is if you don't want duplicates use a set otherwise use a list and what sets gives us so sets they pretty much you know the methods are lists give us is you know set also give us so if you want to clear you can say for example uh clear you have pop you can see the length there as well you can add you can remove so here remove and basically it's the exact same thing as we've done with lists and also in here so you can see that we have numbers but this works the same way for example if you have um well actually let me not change that one right so for example here let's have a letter or actually letters there we go set and then here if i have a and then a and then b and then c and let's have let's just have one more c we should just get a b c right and we should actually print and then letter set so if we run this you can see that we have uh b a c actually so and this is actually this brings me to to the point that so in here right so you saw that we have a a b c c right and this was the actual order that we added them right the difference between sets and lists is that with sets the order is not guaranteed so here if you were to expect for example when you retrieve the elements right so the actual set the same order it's not going to happen right so orders so sets are unordered so if i run again perhaps we should see a different order there we go so now it's actually a b c if i run again you can see that we have bca right so keep in mind those two so no duplicates in order is not allowed and also the actual syntax is different how you create the actual set and lists so which sets is curly brackets and with lists is square brackets lists you can have duplicates and the order is guaranteed let me teach you about set union set intersection and set difference so to illustrate this purpose let's have letter and then a or actually letters and then a equals two and in here let's have a and then b and then c and let's also have d right so d so let's now have a second set so let's simply say letters and then b oops b now let's print union so what union means and in fact this second set let's simply have a b c d and then e and then f right so what set union means it means that we can take letters a and we can say union and then letters oops and then b so if i run this the output is basically the union right so it's just it's just everything combined right so a f e d b and c right so it's literally everything and remember the order is not guaranteed right so that's the actual union so union just takes everything from both sets and then puts it inside of another set so here let's actually have union so equals 2 and then letters a and then letters and then b right and now i can print the actual union next let's learn about the actual intersection so intersection is is very interesting so here i want to say enter and then section equals 2 and then letters a and then to use intersection you simply have this ampersand this ampere sign and then letters and then b right so this now is the actual intersection so what i'm going to do actually um f and then format this so i'm going to say union and then here that will be union just like that right and let's have f there as well and then enter and then section and let's have intersection and let's have a equal there and equal here so and actually this should be within quotes and there we go so if i run this so for the intersection and this should also be quotes here there you have it so if i run this you can see that the union is everything but the intersection we have an empty set right and this is because in here so there is nothing that intersects between these two sets so if i was to have for example a in here and then run it you can see that now we get a inside so basically it's whatever happens in both sets or actually whatever occurs in both set in both sets so a is in letters a and also in letters b so if i was to have d in here so d just like that run it you can see that it happens in both sets the final one is the actual difference so let's go ahead and say print and then actually let's create a variable first so difference equals two and then letters a and then the difference you simply use a minus so minus and then letters and then b right and the difference is so right here let me expand this so the difference is oh actually the difference so this sign right means right so everything which is in in set um so this set right here so everything which is in letters a but not in letters b right so here we have a b in both sets but c d right there actually um b and c my bad so b and c they are not in letters b so right here if i say print and then difference and let me actually have the exact same thing here just like this and if i run you can see there you go we have c and b right so this is the actual difference and here it's very important right so if you flip this around so b so let us be minus and then let us a this will be a different result so if i run it you can see that now it's f and e so there you have it this is how oh actually this should be difference here so this is just a string right the friends so there you have it this is pretty much everything about the set union intersection and difference if you have any questions drop me a message otherwise let's move on let's learn about this data structure called dictionaries with python so dictionaries allows us to store key value pairs so in here let me go ahead and say that this is a variable called person equals two and then the dictionary works as follows so here this is the actual opening curly brackets and then close curly brackets and inside you have what it's known as keys right so here let me simply say name and then here you can specify the actual value right so let me actually move that space there so here this is jamal and i'm going to have a comma there age and then jamal's age is 20 and i forgot all them there so this is a dictionary and i mean then this here and let's also say that this has an address just like that and then here us right at usa there you go so this is our dictionary and in here so this is the actual key and this is the value so key value it's a key value pair data structure now if i want to get values or actually print and let me put this smaller so you can see a little bit better so in here i'm going to say person and then if i want to get for example name right so this is how i do it if i run it you can see that i do get jamal right but if the key so this is the actual key and i'm getting the actual value so if the if the key doesn't exist you can see that we get an error saying that key error and then name so that key is not present so if i want h for example simply type age run it 20 and finally address and then run it you can see that we have usa so maps are really helpful when you have for example structured the structured data like this where you need to have a key and then a value now with keys right with keys they have to be unique right you can't do this for example um name and a name right you you can't right so you can see that even pycharm is telling me dictionary contains duplicate keys name so the keys they must be unique so let me just put that back and let me actually now show you some methods right so with dictionaries i can say actually let me just put everything there so we want the actual name we want age as well right so with dictionaries we can say print and then let's say person dot and in here the same methods that you saw with lists and sets they are available but here for example you can see that we can get the keys so if i run this so here look at the keys right so these are the keys name age and address we can also get all the values so i've just pressed command d and then values and now we get the actual values so in here you can see that we have jamal 20 usa and the keys above if you want to clear so right here so if you wanted to clear for example person dot and then clear and basically now if i print and then person right here you can see that it's empty right we've just cleared everything so let's actually not do that and one thing that you might want to do is actually update the value right so let's say that we want to update the actual age so before i print person and then dot and you can also say get right and we want to get age so there and basically this actually is a no another way of getting so let me see print so you can see so you can do it this way right here oops this way or this way so i kind of prefer it this way here because it's the syntax is better so if i run it you can see that we have 20 in here right so what i wanted to show you actually was so if i want to update age right so i can simply say that right so i can say that oops oh my god account type today so in here i'm going to remove that print statement there and i'm going to say equals to 100 hooray now if i run this check this out so in here age was 20 and then we updated age to 100 so there you have it this is how to work with dictionaries so dictionaries is very powerful and you will end up using them quite a lot this is all for now if you have any questions please drop me a message but this is the most common data structures that you're going to be using with python and i'm going to drop some links on some other data structures which you will find them useful as well catch me on the next one now is the perfect time to learn the concept of loops so loops in python allows us to iterate through sets lists or pretty much dictionaries or any data structure available so let's create this list right here so list well actually let's simply say names equals to ahmed and then anna and james and let's also have jamila for example right so this is our list let me put this on a new line right so this is our list of four names so you saw that if for example i want to grab print and then names oops names and then names zero and if i do one two and then three so right here you can see that i do have the name so ahmed and james and jamila right but this is me going out and actually printing line by line so what loops allows us to do is to actually loop through the list and then have access to each individual item so the first way of using loops is using the four and then here i'm gonna say name in and then names and then add the column right there very important and also the indentation is very important right so right here add just press tab and then you get that indentation now here print and then the actual name if i run this you can see that we do get the exact same output but now we are actually looping through our list right here so this is loops in action so there you have it this is how you loop through your lists using for loop so this construct right here so this is actually called for loop so here let me add a comment so four and then loop and right here you see that we have um a list but this could also be a set right this will also work and then run it and there you go you can see that we get the exact same output so next let me show you how to loop through dictionaries right so you saw that we had this dictionary called person and it has the key name value jamal age 20 address usa so in order for us to loop through this dictionary we can use the following construct so it's exact same way that we've done with lists but we get an additional information so here four and then key and then in and then person and then print and then the actual key so if we run it you see that we are getting the actual key so name agent address now what we can do is the following so here i'm going to say f and then double quotes and here i'm going to pass the actual key and then i'm going to say key and then column and then value column oops column and then inside i'm going to say person and then get the actual key so here i'm just passing the key inside right now if i run this you can see that we have key name value jamal key age value 20 and then key address value usa so this is one way of looping or we can do something better so here what we're going to do is actually comment this oops i didn't want to do that so i'm going to comment this and what we're going to do is the following let's simply say 4 and then key and let's also get the value in and then person dot and then values and then don't forget the actual column and make sure you indent with tab now let me actually take this print statement and put it there and comment and i need to indent this and right here remove that and this actually is the value right value and there we go so if i run it and we get an error so too many values and actually this is meant to be items right so my bad so items in there so for key value in person the items and then run it and you can see that we get the exact same result so let me actually show you what items contains so values and you've seen this right so print and then person though and then items so if i run it you see that this is the actual items right so this is an array of key value pairs right and then we are simply looping through it and then getting the key and the value instead of doing it this way where we actually had access to the key only and then we said person in here so person and then we pass the key which is how you access the value for any given key so this is pretty much how to loop through dictionaries if you have any questions drop me a message otherwise let's move on all right i thought it would be nice for you to have a go at this exercise so given a list of numbers right so let's say that we have some numbers here just like this and you can put any numbers that you want right so given a list of numbers try and sum up all the numbers inside of this list right here right so you need a variable to store the result and then you need to loop and then perform the actual addition right so give that a go and i'm going to give you the solution in the next video all right let me give you the solution for this exercise so what we need first is to have a variable and we're going to call this variable as result in here equals two and initially will be zero now we need to loop through this list right here so numbers four and then number in and then numbers column and then on the new line say result and then plus equal right and then number so number right so remember that this assignment operator is the same as saying number or actually a result plus and then number is the exact same thing right so what i'm doing is simply collapsing it into plus and then equal and then number and finally print the actual result so i'm going to say f and then result equals 2 and then result so let's run this and you can see that result is 31. so this was an exercise on looping just to make sure that you understand how it works and do a bit of logic so that you can practice your python skills if you have any questions for this exercise please do let me know otherwise let's carry on right let me teach you about the while loop so while loop works a little bit different than the for loop and basically it loops while a condition is true so let's start with this example right here let's say that we have the actual number and then equals to zero right here so we're going to say while and then here so the same with if statement we have to pass a condition so here we're going to say while the number is less than 10 right and then add the column we're going to do something so for now let's print so this is what we want to do so we want to print the actual number so if i run this you should see that we have zero zero zero you know it's just basically it's just printing zero right because the number zero is less than ten in here therefore we're going to keep on printing the number so what we're gonna do here is the following so we're going to say number and then plus equal and then one so each time that we go through the loop we're going to increment the actual number so if i run this you can see that now we get zero through nine right as soon as the um the number is bigger than 10 we skip out of the loop and this is the power of the loop so we can also here have else so this is another construct with with while loop and then print and oops i didn't want to do that sorry and here else in while and then loop and then if i run it you can see that so basically this is the actual end right so when it reaches the end so when this condition is no longer true we have the else block which then we can say anything and in fact let's just say that um while loop ended just like that and if i run it you can see that we have while loop ended and basically the while loop is very simple um and yet very powerful and basically just allows you to keep on looping while a condition is true as soon as it's false we skip out of the loop we if you have the else we execute the else and then it just breaks out of the loop so there you have it this is all about while loops let me teach you about two keywords that you're going to be using with both for loops and while loops and they are the break and continue keyword so what i'm going to do here is remove this elf statement there and right here what we're going to do is the following we're going to say that if and then number so still inside if the number is less or actually if the number is equal to five we want to do something right so for now let's simply say print and then number is and then five and then hooray right i just want you to see first the example so let's run the program and you can see that we have hooray number is five right so if it is equal to 5 we have number equals to 5. so now let me collapse this and what i'm going to do is remove this print statement there and i'm going to print number so in here so number and i'm going to say if the number is equal to 5 what we want to do is to break so we want to break out of this loop right here so check this out if i run it you can see that we have one two three four five and then as soon as the number is equal to 5 we break out of the loop so this is one way that if you don't want to finish the execution of any loop both while and four loops you can break out of the loop the other keyword so let me actually comment this so let me comment this and the other keyword is the continue and in here what we're going to do is the following so i only want to print the number here right if it's bigger than 5. so i'm going to say if the number is less than 5 if the number is less than five and e and here i wanna increment the number so right so if the number is less than five we're going to continue so what this does is it goes through the loop and it checks right is the number less than five yes therefore i'm not going to carry on from this point onwards so i'm not going to print the number and then go back to the beginning then 2 is 2 less than 5 yes continue so it just goes back and then checks it right as soon as number is bigger than 5 then it prints the actual number so if i run it you can see that we actually get 5 6 7 8 9 10 right which is exactly what we wanted so these two words break and continue you can use them with four loops right so it's not just wall but it is with four loops right so let me just give you an example so here if you have a list so let's say 4 and then n in and this will be 1 2 3 and then 4 5 and i want to say if and then n is less than 5. right we're going to continue and then here we're going to print so basically the exact same thing we're going to print n so if i run you can see that we get the exact same output here right so in fact let me just remove actually comment this while loop here and then run the program and there we go so if the number is less than five we continue basically i need more numbers here right so six and then seven so just like that run it and you can see it prints from five 6 7 and i can also say if the number is equal to 5 we want to break out of the loop which means that it will print 1 2 3 and 4. so if i run it you can see that we have one two three and four so this is everything you need to know about the break and continue keywords in this section let's learn about python functions so a function is a group of statements to perform a specific task previously you saw that we had this keyword right here so death and this is the actual way to create functions so let's say that we want to have a function that greets someone and to create a function is as follows type def and you've seen this before and here let's give it a name so we need to name this function so here we're going to say greet and then add parenthesis and i'm going to explain what this means in a second and then here column and then indentation so remember this is actually tab right and now inside is any logic that i want this function to perform right so in our case let's simply say that we want to print and then hello and then how are you just like that right so now what we can do is use this function right here and the way that we call the function or the way that we invoke the function is simply by saying greet and oops and you see that if i say just greet python doesn't know anything about this right and when you invoke so when you want to invoke the function right you add the parenthesis now here i can run the program and you can see that we have hello r u so most of your code will rely on having functions and then inside of the function you will have lots of uh logic right and i'm going to show you exactly more examples in a second so this is an introduction on functions right so you know how to create a function so this is the actual name and then inside you can have a series of statements right and the way you invoke the function is by simply saying greet so the name of the function and then parenthesis so now let me teach you about arguments so with functions you can have arguments so in here let's say that we want this function to take name so we're going to take a name right so here we're going to take a name and then we're going to say hello and let's use f and then here let's pass the actual name so now you can see that this function is actually complaining because we need to pass an argument into it so here what we can do now for example jamila and we can have the same function invoked twice but with a different name so here let's simply say alex and now if i run the function you can see that we have two statements and here it says hello jamila how are you hello alex how are you so these are the actual arguments that we are passing into the function and this is the actual parameter now you can have more than one so here for example if you want to have name and then um for example age and right here let's have for example we're going to say i know your and then age equals h here and then here let's remove this and if we run this you can see that we have an error and it says that missing one requirement and then argument h so we need to pass h so here at 12 and then 22 run and you can see that basically now we have this function that we can reuse again and again and again right so let's say that for example you have you want to have a default value for this parameter right here so h so what you can do is simply say h equals and then 2 minus 1. so now check this out if i run this you can see that this still works but now i can remove for example the actual age from jamila run it and you can see that it works and it takes the actual default value now we know that no one has an age of minus -1 so what we can do is we're going to say if and then age is less than zero right we want to do something right but what i really want here is to revert this so i'm going to say not and then age less than zero so if it's not less than zero i'm going to print i know your age or you can even reverse right you can say if age is bigger or equal to zero you know it's really up to you so now if i run it you can see that for jamila it says hello to miller our u and then for alex it says hello alex how are you and then i know your age 22. so this is how you use parameters and then arguments so this is arguments so this is the actual value that you pass into your functions let's write a function that prints first whether you are an adult or not so here let's say death and then is and then adult and then here we're going to take age and we're going to say if age and then greater or equal to 16 we're going to print and then adult and then else we're going to print not yet an adult and then just a sad face there right and let's add a happy face so now let's invoke the function i'm going to say is adult and then pass let's say 10 if i run this you can see that we have not yet an adult and then we have a sad face if i pass let's say 45 hooray it's an alloy now what i really want is just a function that tells that tells me whether i'm an adult or not right and i don't want this print line so with functions what we can do is to return a value from it right so here we can say for example we can say return and then true oops capital t and then true otherwise we're going to return and then false so now check this out if i run we get nothing and this is because right this is because this now has a value which can be true or false so that means that we can store this into a result variable and we can print now the result so if i run it you can see that it is true and if i put for example 10 you can see that now it's false so with functions you can return literally anything right so this so this can be you know a string or you can return a list or a number whatever you want right in our case we're just returning um this boolean right here true or false now because this returns a boolean value we can remove all of this and then simply say return just like that and then remove the semi column and this is the exact same thing so now if i run it you can see it's false and if i say 80 whoops you can see that we get true so this is functions and return statements so in here let me just tell you or actually prove it to you that we can return something right so let's say that we want to have a function that takes m or f and then returns male or female so death convert and then gender and here we're going to take gender and the default will be unknown and add the column there and now i'm going to say if gender dot and then lower so we want to lower case the actual input that comes in equals two and then m or actually m or instead let's just uppercase right so upper so if this is the case we're going to return and then mail and then alif gender dot and then upper equals to and then f return and then female and i forgot the actual column there and now i want to say else we're going to return gender just like that so now if i invoke the function so i'm going to say print because this returns the actual gender and now i can say convert gender i'm going to pass f i'm going to pass m i'm also going to pass the lowercase version so there lowercase version for m and then let's pass hello and actually let's say that here so we are missing the actual column so here i want to say f and then gender and then pass gender is unknown and now if i run the program you can see that we have so here female female male male and we pass hello and then we say gender hello is unknown so this is how you return values from functions right now that you know about how to work with functions to be honest we've been using lots of functions throughout this course so remember when we had lists and sets we said dot on any list and check this out you saw pop sort count extend uh all of these methods right so these are actually built in the language right so this is what python provides you out of the box which means that you don't have for example to implement the reverse method right if you want to reverse a list you don't have to do it because it's already provided the same with strings so if i have a string and then say dot on it you can see that we can uppercase we can lower start with we can capitalize we can center and code find formats and all of these methods so these methods are actually built into the language to help you and the same with other programming languages right so they give you the necessary tools to build programs so within your methods you'll end up using built-in methods so these are actually built in methods and the ones that we define ourselves so when we say death so this is actually our the ones that we define and with our methods we can define any logic that we want so i just wanted to show you that we've been using methods throughout this course but those methods were provided by python and with python we have this important keyword so import and you've seen import so what this allows us to do is to import modules within python so here for example i can import the math module and inside of this math module so if i say print for example i'm going to say math and then check this out dot and now i do have available methods and values so here you can see that this is a value so pi if i run the program there we go so this is the actual value of pi if i dot and then let's have a look at a different method so here is square root so let's try and see if 16 is a square root and this actually gives us the square root of 16. so if i say 25 and the square root of 25 is 5 so 25 oops can't type today so 25 and you can see that it is 5. so this is the import statement as well and you can basically just use modules that are built in within python or your own modules or external modules that other people have written and if you want to use their code you simply import the module and then use it now with imports so there are two ways that you can import so here you see that is square root so i can say from and then math import and then is square root so this is a different way instead of bringing the entire module you can just bring is square root just like that if i run it you can see that we get the exact same output so this is everything about functions and using the import statement to import existing modules that gives you the flexibility of using existing code written by other people or from yourself or code that comes with python so in our case we have is square root now is the perfect time to teach you about modules so you saw that we learned about the import statement here right and the import statement allows you to import existing code right so we can import built-in code provided by the python library or we can import external modules that contain code that other people have written and i'm going to show you this later in this course so in this video what i want to show you is how to create your own file which then you can import and use it so what we're going to do here is the following open up the project tab so i've just pressed command and then one and inside let's create a new file and here type cal and then creator dot and then py right so you can create a file like that or you can create a python file here there we go and then put the name and you don't need the actual py there so now we have this calculated py so in here what i'm going to do is the following i'm going to have two functions so let's have a function so say def and then add and let me put this a little bit bigger right so add and this will take n one and then n two and it will return and 1 plus n2 let's have another function so here so we're going to have oops just like that let's have four right so here this will be subtract and then minus and then divide and then this will be division and then multiply right so what we can do with this calculator.py and let me just put bigger so you can see what we've done right so we've got a function that adds subtract and then divide and then multiply and i've got typo there so multiply just like that right so we've got these four functions now what we can do is actually go to main.py and then say import and then calculator so we actually importing our own module right so the way you saw that we had import and then math right so this is provided by python so we didn't have to write any of this but here we are importing our own code so you you can share functionality throughout your application by creating reusable code so now we can say here so let's say print and we're going to say calculator dot and then we've got our functions here so divide multiply subtract and add so let's pass two or actually two and then two and let's say add divide oh actually we already have that so multiply and then subtract and if i put this larger so you can see there you go so now so now i can run this and you can see that we have the results right here right so 2 divided by two is one and then let's actually say two multiplied by four is is eight it should be eight but we have an issue here so we have six here and then subtract so two take away two is zero so let's fix the actual calculator so here let's go to calculator.py and then fair enough so here where we said multiply this should be a star just like that if i go back and then run the program again and now we get the correct answer so 2 times 4 is 8 right here now the last thing that you saw is we can change this import statement a little bit so we can say from and then calculator import and then divide we can also duplicate this we can say import add and then multiply and then the final one will subtract right so the reason why you would do this is for example if i only need the divide method i can remove all of that and then just have it like this right so now i'm not bringing the entire functionality so all of these methods into this main.py file so this is how you create modules and import them if you have any questions do let me know otherwise let's move on right let's learn about classes so classes is a concept which is really important that you understand so what a class is and literally this is across every single programming language so what a class is is a blueprint for creating anything that you can think of literally anything in the real world that you can think of you can represent in code and what class is it's just a blueprint that allows you to create um objects right so class is the blueprint and then from the blueprint you can create objects so one or many objects right so just let's just take this example right here so again my pink phone don't laugh uh so right here so in order for us to create this phone right here so this iphone right we have a blueprint and a blueprint is the specification of how this phone is created right so um you know this could be in the factory where they have already the template where they can say right so now give me you know a thousand of these similar phones right here right or you can say right so instead of being pink give me a black phone or a red phone so on and so forth right so with classes what we do is we specify the attributes so attributes are anything that um resembles the phone right for example here um the color for example the color right is an attribute of this phone is pink the brand is an attribute apple uh whether it has touch id right so that's um another attribute the price right how much does it cost that's an attribute right and then you have behaviors so attributes and behavior so behaviors are what this phone right or what the actual um object can do right so for example here we can unlock so this is one behavior right so we've just unlocked the phone or we can make a phone call and this is another behavior right so with classes you can literally model anything that you want and this is uh the the and this is the world of object oriented programming right involves you creating a bunch of classes and then connect them together so i hope you understood what classes are so classes are a blueprint for you to create anything that you want for example i've got this lens right here so again i can't create a blueprint so a class that tells me how to create many of these lenses right um so you've got classes the blueprint and then you've got the object so the object is the real thing right it's a real thing if i had a second iphone these these would be two different objects right or for example right here so this is one object and this is another object right so these are two different things and how we create them is by using classes so i hope you understood this example and without further ado let me go ahead and show you how to create classes with python alright so let's define our blueprint to create as many phones that we want so let's go ahead and say class so this is how we create class in python and we have to give it a name so we're going to say phone and column here now let's define some attributes simply say def and then underscore underscore init and in here so this in it is how you create these phones so this is the actual constructor so inside we want to pass the brand and price and you'll see this in a second how it works so now say self dot and then brand equals to brand and let's say self dot price equals two and then price so now we have two properties so brand and price let's define one behavior for this phone so here let's have a behavior so here we're going to say call and we always take self in here so this refers to the current instance of the current class let's say that we want to print for now and then i'm going to say f and then pause and then say phone calling and let's pass well actually let's go back and then brand or actually self we need to say self and then brand to refer to the current instance is calling and then dot dot um in fact let's let's just give one more argument here so let's say phone and then number and then here phone and then number so let me just make this a bit smaller so you can see everything so we have and this should be actually just like that so this is the convention you saw that pycharm was yelling at me so now we have this blueprint which allows us to create many phones so let's create the actual phone so this is the instance right for our phone so in fact let me just kill this space here and now to create a phone we can say iphone equals to and then phone and inside we're gonna pass the brand and the price so this is iphone and then 7 plus so this is my phone and the price we can pass a price so here let's say that this phone is like 300 pounds and there you have it now we have a phone let's create another phone so here this will be for example sam sung and then s i think is s20 and then this is like you know 1400 right which is really expensive so now we need to actually um rename this so this actually is samsung so the name of the variable just like this and this is the actual iphone let's now print the values so i want to print iphone so i just want to see something here first and i'm also going to print samsung so in here right so this iphone this is now the actual object so remember a class is just a blueprint for creating objects right so it's just a class it's just a template and now in here we are creating the iphone so this is the actual thing the actual physical not physical but the real thing the actual object and then here we also have the samsung right so we are creating multiple phones from the same template and this is what a class is if i run this you see that we get main and then phone and this is object and then this is the actual address in memory and this is not pleasant right so you know when you print an object you sometimes want to actually get the actual string representation of the object and i'm going to show you this in a second so here we can go and say dot and then brand and we can say dot and then price so let's just let's just do it for iphone now if i run it you can see that we have iphone 7 plus and 300 that's the price we have one behavior so price actually print and then iphone or we don't have to print because um in here it does a print so here let's say iphone dot and then call and we want to call this phone number so 999 and then if i run it you can see iphone 7 plus is calling 999 let me just take all of this and then do a print and that will be blank so here let's change this from iphone to samsung run and you can see that oops let's change the name here the number actually just any number run it and you can see that we have samsung s20 1400 the price and then samsung and i've got type 1 samsung so sam and then sung just like that and run it again and there you have it so this is an introduction on classes and objects so just to recap a class is just a blueprint and in here oops let me just kill so in here so this method so def init is this so this is the actual init and here you see that we pass the brand and the price and this is why we pass brand and price in here right and then we're saying self dot brand equals to the brand so self refers to the current instance of the current class and if i remove for example the init method you see that i i no longer can do this so i can't do it so all i have to do now is just remove um this right and now i've got a phone which tells me nothing so this is what constructors are so let me just go back and then we have one behavior which simply call right so the call behavior simulates the phone calling a number and then here we have the actual objects right so iphone and samsung so these are the individual objects so these are two separate things so this is everything about classes and objects if you have any questions please do let me know otherwise let me show you how to print objects properly right so you saw that we tried to print so in here let's print and then iphone and in fact let's just move everything right so we try to print iphone and also samsung if i run it we get this nonsense right here so object add and some random numbers so what we're going to do instead is inside of our class we can override the tostring method so this works the same with other languages so here what i'm going to do is use pycharm to help me with this so i'm going to press ctrl and then enter and then override methods or actually you can press command o so let's press command o instead and you can see that we can override a bunch of methods so we have equals we have str we have format reduce data and what not um so if i type in it so you see that we have actually we don't have in it here but i thought we had in it so in here what i want is to override this method right here so sdr so press ok and now what i'm going to do is remove this and i'm going to return oh actually it's actually returning already so f and then i'm going to say brand and then self dot brand and then price equals 2 and then self dot price just like that and now if i run the program when we print these two objects check this out so now you can see that we have the string representation for our object so usually you'd have for example brand equals two and then a comma something like that or even a new line if you want it so this is how you add a new line so if i print um basically i remove that space there so there you have it so you can see that we have brand and then iphone 7 price 300 brand samsung and then price 1400 so this is how you override the string method so obviously um you can override any uh many other methods for different purposes which i'm gonna leave some links that you can read about but in here so let me just remove the def uh init function and if i press command o you can see that we have the init so before we didn't have because we already had the method but here i can simply just do it like that right it's a bit different but we can just just remove this and then change it the way that we had it so let me actually put it back just like that and there you have it so this is how to override the str method within python and one thing that maybe you haven't you haven't noticed or if you have noticed is this arrow right so this arrow right here so what this error means it simply means that this method returns the string data type in here so i can even remove this and this is absolutely fine but if i want to be more precise i can say right so this method returns an sdr or for example if this method was to return an integer you could say an integer but here now you can see that this is complaining because it says expected type int and got str so let's remove this or you can leave it if you want and it's really up to you and this is pretty much everything catch me on the next one with python we can work with dates and it's very straightforward and something that you will end up having to use every now and then if you want to store for example dates or date of births or anything which is date related so to work with dates we have to import the datetime module so you saw how to import before so simply type import and then let's say date and then time so now i can say print and let's say date time dot and in here we have uh the ability of for example getting the actual date time so daytime gives us an object so you can see the value is an object and in here we can say dot and then now if i run this you can see that this is the current date and time if you only need to grab the actual date i can duplicate this and then say date instead of date time and then now run it and we get an error and this is because date doesn't have um now method right so here and instead it has today so my bad there if i run it you can see that this is today's date so 2020 september the 27th if i want if i only need time i can duplicate this date time and it's pretty much the same as the other one so now and then dot and then time if i run it you can see that we also get the actual time in here so this is how you work with dates let me now show you that instead of having for example date time and then daytime day day time and end date let's improve this a little bit so we're going to say from right so we want to um say from and then date time we want to import date time and we also want to import date just like that and now we can drop this date time dot and then um date time and then basically we can drop oops we can drop this right so here we can drop that and we can drop that as well and we can drop this as well so this looks much neater if i run it you can see that we get this output so date time here we get the actual date only and then we can also get the time in here so let me see whether we have the date so here yes we also have the date in here so we could actually get the date like this um but if for example you want to get the year right you can only get the year if you want if you want to get the month and finally if you want to get the actual day you can do it so if i run it check this out so we have the time the year month and then this is the actual day right and this is how you work with dates next let me show you how to format dates with python right so you saw how to work with dates in the previous video so if i run this i want to show you something so here you can see that we have 20 20 and then the ninth and then 27th so sometimes this is not the actual format that we want so maybe we want the format of this date to be the 27th of september and then 2020. so let's learn how to format our date i'm going to leave this first print statement here for now so so we can compare things but the way we format dates is as follows i'm going to extract this to a variable and i'm going to say now and i'm going to print now so basically this is the same thing so if i run it you can see that we have the actual date as well as the time but what we can do is say now dot and then str f and then time and here we're gonna pass a string so this is how we want to format things so here go ahead and say percent and then d and then space percent and then m space percent and then capital y and let's also format the time so here percent and then hour so capital hour and then percent m capital m actually and then percent oops percent capital s so this returns the formatted date and i can say print on it just like that and if i run it check this out so now we have things a bit different so we have now the 27th of september so we reverse things and then the date and check the time as well so 0 0 and then 18 and then this is this is the actual seconds so i can do this actually so i can do a forward slash here if i want forward slash and then column and then column oops and then column here run it and this is our format today if you want for example dash so many systems work with dashes and there you have it another way to format is so let me duplicate this if you want the month right so instead of being a number but you want the actual month you can say b and then run this so here we have september right so here this is the number but by just saying percent and then uppercase b we get the name of the month and if you just want an abbreviation so let me just duplicate this just simply say lowercase b and then run and check this out you have 27th of sep instead of september and then you have the the actual um time and this is how we go about formatting dates so obviously we've done with date time but you could do it with the actual date so for the date let's say date dot today dot and then strf and let's just grab this here so it's the exact same thing so here and then we can say print and and that so let me actually put it right at the end so i'm gonna cut that and then put it here and run it and check this out it's the exact same thing if you want uppercase b there for the month so duplicate and then instead of m uppercase b run it is the exact same thing this is how you go about formatting dates let's learn how to work with files in python so to create a file it's very straightforward and easy simply type f and this is the actual name of the file so in fact let's just say file and then open and then here you can see that we can pass the file and then mode buffering encoding so on so here what we're going to do is the following so we're going to say dot forward slash so in the present directory we want to create a file called data dot c s oops csv and then next what we need to specify is the actual mode so the mode is whether we want to write to the file we want to open a file for reading or we want to append to a file so if you want to write to a file you simply say w so this will create a file if it doesn't exist if i open up the project tab so in here i'm going to press command in one and let's run this you can see that we have date and it's not date that i wanted was data so let's just delete this file there we go if i run it again you can see that we have a data.csv so now inside this is empty so for now let me ignore the extension but you can see that we've created a file so how do we open the file for reading and writing in here let's change this to r and then plus so this is how we open the file for read and write if you just want to read you say r but now i want to open for reading and writing so i can now use the file so here file dot and then right and you can see that we can add um actually we can write a line or we can write lines for now let's go ahead and write a line and this will be id comma name and then email if i now run the program you can see that if i open up the data.csv check this out we have id name and email and one thing that we need to do here is to close the file so anytime that you work with files and you perform any operations on them you need to close the file so file and then dot and then close this is how you work with files so in here let me go ahead and have for example id one name jamila and then jamila at gmail.com let's have another one so here alex and then alex and then id will be 2. if i run go back to data.csv you can see that we have everything in one line but really what i want is a new line after each one so here i'm going to add a forward slash and then n for new line grab that and put it here and the same here if i run it go back to data.csv and you can see that we have our csv file in here which is really cool and you saw that every time that we run this we pretty much just override the file with the new contents so if you want to append to the file you say a just like that and now this will append so if i add for example if i duplicate that i just want to comment this and then say three and then here samir and then samir gmail.com run it and check this out we have appended to the file but if i remove the a and then say right and then plus and if i uncomment everything and then run we've just override the file with new contents so let me actually just put a note here so you know exactly so in here we have w for writing and then a for appending and then r for reading and then r plus for reading and then writing and depending on the flag that you pass in here is the mode that you will operate on the file next let me show you how to read from our file now that we've written some contents to the file let's read it so to read let's use the our flag so we just want to read the file and let me just delete all of that and in fact let me show you that if we try to write you can see that we have an error right so the flag is actually there to do its job so we can only read now so if i remove all of this and then i can say print and file dot and then read so this will actually read the entire file so there you go so this is the entire file if you want so if i comment this so if you want to so file dot and then read so you can read a line so here you can see that we've read the entire file but you can also read line by line and to do it is really straightforward so if i comment that out you can say four and then line in and then simply say file and here print line if i run it you can see that we read line by line and what reading is line by line gives you the ability to is to take the line and then you know if you want to turn the line into an object or do anything with it you can do it and if i show you so let me just comment that out we can say file dot and then lines so here read lines right and then run this this actually gives us nothing but if we print and run it this gives us a list with each line so you can see that this is line one line two and line three and finally line four so there you have it this is how you read from a file next let me show you a syntax which is the preferred way when it comes to writing and reading from files so another way that we can work with files is to use the with syntax so what the within syntax gives us it gives us the flexibility of removing this file.close so we don't have to remember to close the file when we open it and the syntax is as follows so let's remove this from here and the way we work with the with keyword and file is by saying with and then open the actual file so in here we have data.csv in the mode for now let's just say r because we just want to read and then as file so this is the actual name and now we can perform everything that we want so here we can just read the file as we did before and for now let's just say print and then file dot and then read we just want to read the entire file run it and you can see that we get the exact same output and the benefit here is that we don't have to say file dot and then close this is automatically done for us now one thing that i want to show you is if for example we say data so we know that this file doesn't exist so if i run this you can see that we have an error right so data dot csv no such file or directory so what we're going to do instead is the following so we're going to check first if the file exists and to do that we need so if i remove this we need to import os path so i'm going to say import and then os dot and then path so with this we can check whether the file exists or not so let's say that here we have file and then name equals two and in fact let's just have file name equals to dot and then data csv and what we're gonna do is say os path dot and then file actually is file and then pass file name and this returns a boolean so if so if the file exists what we're going to do is open here so let's indent this and let's pass the file name in here file name and there we go else so else we're going to print that file and then file name does not exist and now if we say for example gibberish here we know that this file doesn't exist let's run it you can see that we have file and then the file name does not exist however if we change this to data.csv and we know that this exists let's run it and actually i've said data.csv.csv so my back there run it and you can see that we have the contents of the file so this is a better way of you working with files because you want to check whether the file exists or not so i think we can even remove the dot forward slash there so let's just try this and yes everything works the same so there you have it this is how you read write and you've you've seen the actual flags if you want to append to a file and using the always path to check whether the file exists before opening the file if you have any questions on this section drop me a message otherwise let's move on in this section let's learn how to use python to fetch data from the internet so right here i mean google.com and i want to show you how to use python to retrieve this page right here so let me go back to pycharm and in here we're going to say from and then url lib import and then if i press ctrl and then space you can see that we have couple options and the one that i want is request now what we can do is say request dot url open and we're going to pass the actual url so http and column for slash www.google.com and there we go so now we can extract this to wave variables i'm going to press option command v and now we have the url open here so what i'm going to do is actually name this to r for request now i can perform a print so let me print the actual request and if i run this you can see that we have an object so here this is a response right so we got a response back so with this response we can say dot and then get and then code and invoke the method there run it and you can see that we get 200 status code which means that the request was fine and we can read the actual data so to read the data i'm going to print and then in here i'm going to say dot and then read and then invoke the function and if i execute the program check this out you can see that we actually have the website so we've opened up google.com with python you can see that this is a long string which contains some html and javascript so there you have it this is how you can read from the internet using python and you can see that this was you know in about four lines of code we can read from the internet which is really awesome next let me show you how to to make an api call and work with json objects all right open up google and type jokes so jokes and then api so in here go to this very first link so github.com official joke api and what we want in here is to get a random joke so here you can see that we can get 10 random jokes or just one random joke so i'm going to grab this link right here and open up a new tab and then paste that in and you can see that we have some data back so this is a json object if i go back to that url and grab this one or actually it's it's the same but random 10 so here at the end simply say random 10 and you can see that now we get an array of objects containing jokes so let's grab this url and use python to fetch these jokes right here so let me just go back to this one joke first so i want you to see so here you can see that we have the id type setup and then the punch line here so let's open up pycharm and in here let's just modify this a little bit so let's have the url in here equals two and then last url for 10 jokes and then here let's pause the url just like that now if we run the program you can see that we get an error and the reason why we are getting an error is because of ssl certificate so what we need to do is just remove the s so here instead of https simply say http and run and there you have it so you can see that we have the actual jokes so here we have 10 jokes so oops so here you see that this is an array and then we have 10 objects inside so let's take this data and then use python to load the actual json so i'm going to collapse this and what we need is to import and then json and then json there we go and now we can say json dot loads oops loads just like that and we're going to grab the read so here i'm going to put this into a variable so json or actually content or let me simply say data so data and then paste that in and now we want to load the actual data just like that so if i now extract this to a variable i've got the json data and let's print json data so json data so i want you to see things step by step if i run this you can see that this is our json have a look so what we're going to do is the following so we're going to loop through right so we're going to loop through this json data and then pull out the setup and the punch line so let's do the following so what we're going to do is so remember how to loop through lists so let's say 4 and then and then j for json in json data and now we have access to the actual json so this is the individual json so here we can get the following so we can get the general so let's have a variable called j nero equals to j and we're going to grab the setup oh actually so here setup so it's not general so it's set up my bad so setup there and we're going to grab the setup and here let's grab the punch line so here you can see punch line so let's just grab the punch line equals to j and then punch line just like that and let's for now let's just print to the console but if you wanted to turn this into an object you could definitely do it right so you could have a class and in fact let's just do it so let's just do this so what we're going to do here is the following so we're going to create class so let's create a class in here so class this will be joke just like this and remember we want to have the init so overwrite methods init and in here what we're going to do is say that we want to pass the setup and then punchline just like that self dot and then punch oh actually setup equals two and then setup and we want self dot and then punch line oops just like that equals two and then punchline right and this is our class so now what we can do is say joke equals two and then joke and what we're going to do is pause the setup and punchline and in here let's have a list so let's say jokes equals to an empty list for now and we're going to append so let's append i should actually just say jokes dot and then append and then joke and you can see that we are building a little program here so now we finished and in here they should be within quotes just like that and now let me just say print so we're going to print so let's for now print jokes and then dot len so jokes dot and then len just like that if i run the program you can see that we have 10 in here but now check this out if we loop so let's actually loop through our jokes that we go back so in fact what i'm going to do is override the str so string so we're going to override the string so here press command o and then str there we there we have it and we're going to return so here this will be the setup and then self dot setup and then punchline and then self dot punch line there we have it now i can go back and we can loop so let's loop so four and then joke in and then jokes we're going to print and then joke so let's just have f got and this will be within curly brackets and then close it here and then say jokes now let's run it and check this out so you can see that we have a bunch of jokes so uh this was the status code 200 this was the json we got 10 jokes and you can see all the jokes so set up do you want to hear my pizza joke punch line never mind it's too cheesy i don't know if this was if this is funny to you but yeah so these are like very dry jokes which they actually become funny uh so yeah this is pretty much how you go about using the request so from url lib as well as the json to read json objects so now you know how to perform http requests all right let me teach you about the package manager for python so pip is the package manager for installing python packages remember i said that with python we can import existing code that other people have written and pip allows us to download those packages and make it available to us so in here you can see so i'm going to leave the link in the section of this video so you can follow along and you can see that installation guide should be very straightforward and basically when we installed python remember that i said you should add python to the path right so when you do that everything comes bundled up and if you have any issues you can refer to this page how to install pip so once we have that out of the way we can go back to pycharm and in here so this is the example where we downloaded jokes and you'll see where we heading to with this um little application but in here go ahead and open up the terminal so in here you can see the terminal and type pip and then three so this is to make sure that we have pip installed so pip3 is because is what comes with python 3.8 so the latest version that we have installed right so in here you can see that we have a bunch of commands so the command that we want is install so install to install packages and then you can uninstall packages and you can also list packages so you can see that you know the commands you can even show information about package um you can verify so on and so forth and in here you saw that we imported request from url lib now the most popular package for performing http requests is this one right here called requests so let's install requests and then use it instead of the request that comes with python so open up the terminal and in here i'm going to clear the screen so i just press ctrl l or you can simply type clear right so if you want to learn more about how to use the time you know go ahead and check my course where i teach the terminal and vim fundamentals so this is really important for you to be able to use your terminals it's an essential skill like you should have right as a beginner you should know how to work with the terminal so here let's type pip and then three install and then requests so requests so in here you can see that this is what we're doing right so we are installing requests so this is the name of the module if i press enter you can see that we have a bunch of things and everything was successfully installed now that we have requests installed what we're going to do is use requests so let me collapse this and we can say here import and then requests so you can see that we have requests here and with requests the way that we perform the request is as follows so let me actually show in the docs so you can see here you say requests and then dot get so we want to get from a url so let's do that so in here what we're going to do is right here we're going to say requests dot and then get and then pass the url and what we get back is a response so now that we have the response let me show you so in here we have the response content and you can see it's simply.text so what we're going to do is we're going to replace the data with response dot and then text right here and i can remove the request with this package right here so let's delete this import and then you see that we no longer have to do this so let's just get rid of that and let me put it a bit smaller and you can see that we have an error here so the way that we do get the actual um code is by saying response dot and then status code there we go and to be honest this is everything right everything should work as before and let me just add some spaces here and in fact we can even collapse this so let's take response here we're going to cut this and this will go here we don't need this data anymore and let me add some spaces here now if i run the program everything should work as before so here if i run the program you can see that we still have our jokes right so remember status code 200 got 10 jokes this is the setup and then we have the punch line here right so there you have it we've learned how to use pip we installed a module and we are using it right so with python there are tons of these modules that you can reuse next let me go ahead and show you something really cool that we can do with python and that is we're going to take our jokes so we're going to take our jokes and transform this from text to speech with python there is this library called pi text to speech that allows us to convert text to speech and it's really really cool so the installation process is very simple so you say pip install and then pi and then tts so python text to speech and then this is like a version and they give us some usages here so we can change the voice we can increase the rate the volume so on and so forth so let me go ahead and show you how to use this so let's open up pycharm and let's open terminal and we're going to say pip and then three install and then pi text to and then speech so tts so pi tts and then x and then three install and then enter just give it a second so it's downloading and that's done so let's now hide this and to import the module you say import and then pi text to speech three so now we have the module here so what i'm going to do first is the following so in here you see that we have the jokes and then we loop through the jokes here right so what i'm going to do is after we loop so after we loop through the jokes i'm going to say pi or actually pi text to speech dot and then we can say speak now i'm going to pass a text and i'm gonna say amigos and then code so if i run this and you can see that it said amigos code and you can even change the voice if you wanted to now i'm going to take this line and then indent it because what i want to print is the actual joke so here instead of amigos code we're going to say joke dot and then set up and then by and then tts x3 dot and then speak and then joke dot and then punchline so here let's uh say here we want to speak the actual setup so setup and let's take this line and then say the joke oh actually the punch line right the punch line and then if i now run this center what did the great do when he got stepped on the punk line he let out a little wine set up did you hear about the kidnapping at school the punk line it's okay he woke up so you can see that it's reading which side of the chicken has more feathers is reading all ten jokes the outside and then the next one there we go what's forrest gump's facebook password the punk line one forest one these jokes are really dry by the way let me just stop this and there you have it so obviously just through an error there because i've just stopped it you can see keyboard interrupted but this is pretty much how you turn text into speech with python this is really really awesome and as i said so in here you can change the voice if you want so here you can go and experiment with some other usages but here for example you can change the volume here's how to change the voice so you're going to change the voice to female you can do it right here and you can even save the voice to a file so you can even so you can imagine that for example you might even take a pdf file you can convert the entire pdf to voice and then you pretty much don't have to buy audiobooks anymore so i hope you enjoyed this video if you have any questions on what we've done in this section please do let me know otherwise catch me on the next one i'm super happy that you made this far so in this course we've covered the foundations of python which means that now you can go and explore different areas whether you want to learn machine learning data science ai building websites building restful apis full stack applications you can go and explore those areas because now you've got the foundations of python so if you're new to my channel go ahead and subscribe and also give me a thumbs up so i can keep on recording these videos and comment down below literally comment down below and let me know what you thought about this video if you're not part of the amigos code community go ahead and join i would love you to have you there and this is all for now i'll catch you on the next one you
Info
Channel: Amigoscode
Views: 279,948
Rating: 4.9608297 out of 5
Keywords: learn python programming, python full course, python, programming, python programming tutorial, python tutorial, python for beginners, learn python, python crash course, python course, python 2020, python tutorial for beginners, python basics, python programming, amigoscode, python from scratch, python 3, python 3.9, how to learn python fast, how to learn python for beginners, how to learn python for free, python for absolute beginners 2020, python programming language
Id: LzYNWme1W6Q
Channel Id: undefined
Length: 215min 7sec (12907 seconds)
Published: Mon Oct 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.