Transcript for:
पायथन पूर्ण पाठ्यक्रम व्याख्यान सहग्रहण

hello and welcome to this python course in this full course you'll learn everything you need to get started with python python is the most popular programming language out there and is used for so many different industries like web development data science machine learning or generally for writing automation programs to automate repetitive tasks so learning python is definitely a good idea let me give you a short overview of all the topics i'll cover in this course after giving a short introduction to python we will start with the basic building blocks of programming the most important data types like strings numbers lists sets dictionaries boolean data types etc and how to work with them learn about variables and functions and why we need them write a program that accepts user input and learn how to validate the user input using conditionals error handling with try accept and also learn loops with while and for loops and again why we actually need them you will learn all these concepts with hands-on examples as a next step you will learn how to modularize your program by writing your own modules and then see how to use some built-in python modules with all this knowledge we can then build our next demo project to write a small program that accepts a goal and a deadline as user input and then outputs the number of days remaining till the goal deadline within this exercise you will learn how to use the date time module to work with dates after that you will learn about packages and comparison of package versus module and we will use an external python package in our next demo project in which we will automate some tasks for working with a spreadsheet file finally we dive into object-oriented programming you will learn what classes and objects are in programming and python specifically and why this concept is so useful in the final demo project you will learn another common use case with python which is communicating with other applications over the internet by making a request to fetch some data from gitlab api in this specific case we will list the gitlab projects of a specific user so that's what we're gonna learn if you like this course don't forget to give this video a thumbs up i'm really excited to teach you all of these so let's get started first of all python is a programming language just like java or javascript python compared to other languages has two very big advantages first of all it's easy to learn it has a simple syntax and it's very easy to set up and get started with for example compared to java where you need some initial configuration before you can even start your application but it's equally or even more powerful than java now what makes a language powerful or how can i say that python is more powerful well that comes from the ecosystem which means libraries and modules that python developers themselves develop but also external developers create and maintain so the more people adopt the language the more powerful it gets because new libraries and so new functionalities get added to it and the second advantage is that it's flexible now what does a flexible mean in this case what makes a language flexible it is easy to mold to your wishes so you are not limited or restricted by the language specifics like syntax or data types or some other constraints or even library functionalities you can extend python widely and as one of the results of this flexibility python also became a multi-purpose language meaning it is used for many different categories so let's see what these categories are first you can use it to write web applications the popular libraries for that are django or more lightweight flask python became extremely popular because of the rise of data science machine learning and artificial intelligence industries and more and more libraries were created and are still being added for python for all these categories they are very popular and highly used libraries for data analysis and data visualization libraries for artificial intelligence projects for things like face recognition voice recognition and a bunch of very powerful and widely used machine learning libraries python is also often used for data collection like scraping the web creating web crawlers that are basically programs that collect data from internet which you can save and then process later again many different powerful libraries to do all of that and finally automation with python python has many great libraries to automate devops tasks for example starting from ci cd pipelines to cloud platforms and monitoring your infrastructure etc you can also write python scripts to do automated backups cleanups on the servers etc in addition to devops tasks you can also automate just general tasks with python like when working with excel sheets which is a common use case in many big companies or automating some tasks on your own laptop and there are also libraries for mobile development gaming desktop applications but these are less likely use cases for python because there are better alternatives for that so you see that the use cases for python are pretty vast and it actually spans several industries and it's mostly concentrated around data analytics machine learning and automation areas and note that this isn't the case for many other programming languages usually one programming language is good for just few things and you should use something else for other use cases so now you see how useful python knowledge could be and how it can help you in your job but also make you more valuable at your work and for your entire team i hope you are already excited to learn python so let's get started as a first step we're going to configure our local python development environment so the first thing we need is install python locally on our laptop so first we install or download python package for your specific operating system and then basically just click through the installer wizard to install python locally on your laptop and note that if you're doing this on windows very important step in the installation process would be to check the ed python to path option basically that you see here because by default it is unchecked and you have to check mark it so that after the installation you'll be able to execute python commands in your terminal now an interesting note here if you're using mac os like i do is that by default on mac os there is python already installed so if i do python version and execute i see that python version 2.7 10 is installed and that is actually python that mac os the operating system itself is using however for our tutorial and generally when working with python we want to be using the newest version which is python 3. so we're going to leave that default python installation alone we're not going to use that or touched it and instead we're going to be using the python version 3 that we just installed locally so now if i clean this up and we want to execute python version 3 commands we're going to do python 3 instead and version and that will give us the version so that's how we can differentiate between the already installed python and the newest version that we just installed so just be aware of that difference now that we have python available locally it's time to download a code editor for python because we're going to be writing files with python code so we need a proper editor for writing python code and actually the best python code editor available out there currently is pycharm which is from jetbrains so that's what we're going to be using throughout our tutorial so let's go to pycharm it is an intelligent code editor which makes it much easier to write code so it makes you actually very productive when writing code and we will see why throughout the course as you can see there are two versions we have the community version and professional one community version is free and already has a lot of powerful features but the professional edition gives you additional very useful features especially if you're creating web applications or scientific projects in python so with professional version additionally you will get scientific tools that you can use in scientific projects with python but also you get support for python web frameworks and database integration which can be of course very helpful if you're developing web applications if you want to get the professional edition jetbrains actually provided me with a code for my channel to try it out for three months for free just use my code when you install it but for this demo community version is absolutely fine so i will go with this one and install it and just click download and once the pycharm installer is fully downloaded we can just click on it move it to the application and now i can actually use it locally so i'm just gonna open the application and we're gonna take a quick tour around pychar it's actually pretty simple to set it up and there you go we have our pycharm and we can now create a new project and let's call it my python project and that's it basically we don't need to change anything else and here you see the location basically of where this application folder will be created you see pycharm projects folder got created in my users directory so this is basically very convenient because all my pycharm projects or my python projects that i create using pycharm will be created in one location and here you also see that python version 3 that we installed is automatically being used for this project which is exactly what we want and you see it says base interpreter and python interpreter is basically a program that knows how to execute python code it will know how to interpret or translate our python code into instructions that computer can understand so with this configuration we don't have to change anything we're gonna create our project so first of all i'm going to make all this a little bit bigger so that you can see the menu here on the side and the code a little bit better so in preferences editor font we're going to set the size to 20. if i apply this right here you see that this code editor font basically got bigger and i also want to increase the size of this menu font here so in appearance i'm going to set it to maybe 18 and there you go so basically everything is bigger now so you can follow along and see exactly what i'm typing and also another note here if you want to configure your theme basically so if you don't want it to be dark or maybe you want some other color scheme then you can select it here in appearance you have four themes available let's actually try this one out and apply and there you go you have a different theme i actually prefer this one so let's leave it at that so as you see we have a very simple project with one main main.pi file the extension is for python files which basically contains very simple code and we're gonna basically just remove all of these and start from a clean python file state and in the next section we're gonna dive right in and write our first simple python application we're gonna start with the simplest example in python to basically learn python syntax and get started as simply as possible so first of all we're gonna write a very simple application that just prints some output and in python when we want to print basically display results of what we wrote we use this syntax where we say print and here we can pass in basically whatever we want to display and now if i want to see whether this super simple application works i can run it or execute it with this triangle here and i have the output so the basically the output is displayed or printed right here and i see the output is 1. i can display some other values like 200 and run it and there you go let's say we want to display some text basically whatever some sentence and again triangle and we have that output here right so again very simple example of writing python code that basically displays some information when we execute it one thing that i want to note here is you see that we write some code basically in this window right here so in our main.pi file and when we run it or execute it our code we see some display in this window so you may be wondering what is this window and why do we see some output right here the answer to that is that whenever you're working with tools like pycharm which basically have all the functionality that you need to write your code and then execute your code you basically have everything in one place so whatever we write we can execute or run right away now without such tool how would we write code and how would we execute that code and this will help you understand what's going on here and to show that i'm going to go back to my terminal and i'm going to create a new file and i'm going to call it test dot py we're going to write the same code basically in this file you can do it in a file editor i'm just doing it directly in the command line and save it so now we have the same identical file as here but not inside our code editor right not inside this pycharm tool and we have python 3 available locally so now how do i actually execute a python file outside this pie chart i can do that using python 3 test dot p y and if i do that i get the same output in the command line so basically what you see right here this whole thing is integrated so basically all in one place where you can write your code the editor right here where you can navigate your files in your project so basically this is just a folder on your laptop and you can navigate the files here and you have the execution environment at the same time so you have to go to terminal and execute python 3 whatever you have everything in one place and tools that provide you with this type of environment for different programming languages for python it happens to be pycharm these are called ides which stand for integrated development environment integrated because you have everything in one place so you don't need to use terminal but in addition to this integration you get more cool features which helps you in writing python code for example syntax highlighting highlighting errors or code suggestions autocomplete suggestions and we will see those cool features actually also throughout this course so basically just makes your life as a developer easier however it's good to understand that connection between executing your files on the terminal when you don't have such a tool versus executing and working on your application inside that tool awesome so let's get back to our code editor a file where we write our code and till now we have done something really simple like print a text basically and print a number right and this leads to the first concept in programming languages which are data types so in python just like in any other programming language you have data types for text and numbers the text data types are called strings so everything within the double quotes is basically a string but note that in python you can also use single quotes for strings so double quotes or single quotes they both work the same and there's actually no difference between them and for numbers we actually have different data types so for whole numbers like 2 20 but also 0 and negative numbers as well we have a data type called integer and that's also how it's called in most programming languages however you also have numbers for currency for example how much a product costs right so if you have an online shop you would have prices like this for example so these are basically numbers with precision this could also be for example when you're shipping something you could have a weight of a package that is also not a whole number but has a precision so this type of numbers in python and many other programming languages are represented as float data type so again you have integers and float data types and if we execute that you basically see all of that printed as we wrote them now obviously printing values like numbers and text like this doesn't make much sense unless we're getting some useful information from our simple program right here right so let's do something more useful with python now let's clean all this up and let's say we want to do some calculations very simple logic that basically calculates how many minutes there are in 20 days so we have 20 days which have 24 hours per day which have 60 minutes per hour so this line will give us basically calculation of minutes for 20 days and there you go that's our number now this makes a little bit more sense because now we have a program that actually does something for us so basically as you see you can do any math operations on numbers in python and again in many other programming languages you can do plus minus division whatever just like you know it from basic math however i want to note right here something that many people think about programming and maybe are misinformed about which is if you're doing web development or maybe devops automation with python and not something like data science or data analytics this is probably an example of the highest math knowledge that you need for programming in python because even though we're going to use some calculation examples because they're just good as examples to show you the basic concepts of python you do not really need any advanced or even intermediate knowledge of mathematics when programming because it's really not about meth however what you need is logical thinking and we're going to be doing some of that and see that as examples so just bear that in mind in case you are misinformed about that so back to our example we have this number that is displayed here as a result however maybe if we use this program we don't remember or we don't know what this number stands for if somebody else is using so we want to add some descriptive information about this number that says this is how many minutes there are in 20 days so basically we need a line that says 20 days are this many minutes and this should basically be exactly this calculation right here so how do we put this calculation basically right here in the middle so that we end up with this whole phrase displayed right here so how do we combine text and numbers basically or calculation of numbers and in programming languages again this is not specific to python we would do that using something called string concatenation and string concatenation is basically a fancy word for gluing together or combining multiple strings now how does the syntax for that look like we combine the strings using plus sign so apart from adding numbers obviously using plus plus is also used in programming for combining multiple strings so in our example we have three string values and we want to put them all together in one string or one sentence we have the first string 20 days r and then we have the second string minutes and in the middle the third string which will be the value of the calculation however if we just put a number here python interpreter tells us it's not a string it's a number so we need to turn it into a string other programming languages do that automatically in python we need to do that explicitly so we need to tell python take this number but not as a number but as a string and we do that using a syntax that looks exactly like that so basically we have a non-string value but we're telling python we want it as a string because it needs to be printed out as a text basically so if i execute this line let's see what happened and there you go we have our output just like we wrote it here but you see that there are no spaces around so basically 50 is just really glued together without any space around and how do we put spaces around these 50 basically add a space here add a space here now why does that work because this whole thing for python is a string again because we are putting it within these quotes so basically any character that you see on your keyboard right now if you put it between those quotes is interpreted by python as a string including this space character right so if we execute it again we have spaces around 50 and our sentence our phrase looks fine and if you're thinking right now this is actually kind of annoying because first of all it looks ugly and also there's a high chance you're gonna forget it that is absolutely right so in python again specifically there is a way to do it in a more elegant way using a different syntax and the more elegant syntax for that let's actually close this is print and let's write that again and instead of having these plus we have curly braces and we write our number inside or whatever non-textual value and at the beginning right here basically before we start writing a string which starts with quotes we just write the letter f and you see the syntax highlighting as well basically sees that this is not part of the text this is non-text value and if i execute it i should see the same output from the second line and this is really a way cooler way to write this instead of using plus but this is something very common and also something that you would encounter in most programming languages so just important to know that this is a syntax for string concatenation as well so we can basically just remove it and use this syntax for our examples and also note that this syntax is actually a new addition to python so it only works if you have a latest python version in our project if you remember we actually configured and chose version 3.9 and that's why it works for us if you use python 2 for example python version 2 this syntax is not going to work for you we can actually demonstrate it so i'm going to copy that line and i'm going to change our test.pi and now if i execute it using my python 3 you see it works fine if i do it with python which is version two point something let's see what happens there you go syntax error invalid syntax because python version 2.7.10 so older version basically doesn't recognize this syntax so just be aware of that so this syntax actually only works for python versions starting from 3.6 so everything below that every python version basically will not recognize this syntax as we see right here so again back to our code and now we just have a number here but what we actually want is the calculation for getting the correct result right so we want again 20 days in hours and then in minutes and if i execute it you see that the calculation was done and here we have the full complete sentence with the result in it and if you're curious this f actually stands for format so this is basically formatting our text or our string in a correct way awesome so we have this line of code here that basically calculates for us how many minutes there are in 20 days let's say we want to do the same for 35 days right what we can do is basically copy that line and replace the values for 20 with 35 and if i execute it i get the same calculation but for 35 days and we can do that basically multiple times for different values so let's say we want it for 50 days and we want the same calculation for i don't know let's say 110 days doesn't really matter and we have obviously different number of minutes for each of these values and now let's say we wrote this program it's ready and we're using it and at some point we decide you know what i want this program to actually calculate how many seconds there are for these provided days instead of minutes so what we would need to do in that case is basically change the calculation in all those four lines so instead of minutes it should actually calculate number of seconds in a day right so we have the hours minutes and seconds and we would have to change the text as well here and we would need to do that for each one of those lines right so we would have it here like this and again if we need to modify this for hours or milliseconds or whatever we basically have to do these changes multiple times right even though if you actually look at that this is the same exact calculation for each line that doesn't change right if you want seconds then this is the calculation that you need to do so how can we actually avoid repeating the same calculation and the same text in our code and the answer to that is variables so in programming languages we have variables that basically hold values that will repeat throughout your code that you can set once and use it in multiple different places so instead of basically repeating this calculation four times we basically do this calculation once like this and we basically save this value in a variable so that we can use it whenever we need right and the way that variables are defined in python is variable name let's call it two seconds equals and whatever that value is that repeats itself and that we want to save into a variable now i want to make two notes here regarding the variables in python first of all in python defining or creating a variable and giving it a value like this syntax for that is actually very simple compared to other languages because you just have the variable name and the value in many programming languages you actually have to define here some kind of data type for that variable for example if it's a number like float or integer or string etc in python you don't have to define that you just have name of the variable simple as that the second one is the naming convention or standard for the variables so we have two words for example if we name our variable calculation to seconds right we have actually three words here so variables that are descriptive so basically they tell you what this value actually is about so they have multiple words in them you can separate them using these underlines again in different languages the naming for variables can be different in python this is actually one of the standard ways of defining variable with underscores which i find pretty nice and easy to read so we're gonna use this syntax basically throughout the course it will be totally and absolutely okay if we wrote it like this or if we used capital letters instead but again this is probably the most readable form and this is what we're going to use now here note that we can decide whatever name we want to give our variable but in python there are some specific words that have special meaning to python these are called reserved words so you can't use these words as variable names and we will use some of these reserved words throughout the course so you will see some examples great so we have our variable defined here and this is the value that this variable gives us so how do we replace now these repeating values with this variable so we're going to delete that and again inside we're simply going to copy the name of the variable so let's execute and as you see we have our value printed just fine and we're going to do the same for all those values and again execute everything works just like before now you may be thinking if we change this to minutes we would have to change the variable in all these places so that's our case we can basically just rename our variable something more generic so let's say calculation to units and this could be now any unit that we want second meaning it doesn't matter and this is exactly the advantage of variable because you can name it whatever you want which basically just describes what this variable is about and now you won't have to change that again and we can also replace this one here accordingly since it's a text we can call it a name of unit we can also call it unit doesn't really matter we're the ones deciding what that variable name is and once we have that variable we can now replace it here how do we do this because this is actually part of a text a string the same way we did right here we're gonna delete that and create an expression create a syntax that basically tells python hey this is not a string this is a non-string value and you already know this could be either number or variable itself and so we're going to use the curly braces and the name of the variable and just like that we have substituted these values here and if i execute it works perfectly fine and we can do that in all four lines and our code is still working and this syntax right here with this format at the beginning basically prevents us or avoids basically saves us from having a bunch of plus signs here and then space characters etc this all looks way elegant now and now if at some point we actually decide you know what i want this program to calculate how many hours there are in the given days instead of seconds and we want to change the program to do that we could basically just change the calculation here and instead of seconds we have hours and if i execute there you go we see the changes right away so this is why when you're programming doesn't matter in which language including python using variables will be probably some of the most frequent thing that you do because you always have values that are repeating themselves and you want to write clean code so you create variables and one of the best practices when creating and using variables is as we're using actually here is to name your variables so that you later and also other programmers who are working with you understand what this variable actually does or what type of value it actually has because if you just look at 24 you would probably not know what this 24 is about right but with variable you actually know that this actually represents a number that is used for calculating units right it could also be more descriptive like units for days but generally another advantage of variable is to basically describe that value as well that you're using in your code now another thing you probably also noticed is that even though we're using variables here so that we don't have to change those values in four different places a lot of these four lines are actually pretty similar the only difference in those four lines are actually these numbers right here everything else is exactly the same so what if instead we wanted to make this code cleaner and basically avoid this type of duplication in our code right so for example if we did this for 10 different values we would have the same line this long line basically 10 times right so how do we avoid this type of duplication so right here we actually avoided duplication for specific values right piece of string here a whole calculation here but how do we actually avoid duplicating a whole line right the whole piece of code basically which has multiple different stuff in it not just a value and we do that using functions so functions are basically blocks of code like this right or basically logic in code that does something it's not just simply a value but actually does something more complex that is again used in order to avoid repeating the same logic or most of the same logic in your code so how do we create functions just like we created variables here we create functions and the way we do that is using following syntax right here let's create our first function we start with def so we define a function just like we define a variable but for function we need this keyword basically called def and now we can give our function a name just like we gave name to our variable we can give our function a name and here we can use the same standards like for variable we can name the function something descriptive something that actually says what this function does and let's call our function days to units and then we have the brackets and a column so this line basically defines a function it tells python hey whatever comes after this is actually going to be a block of code that is going to be referred to or is going to be named these two units and how do you write a function i'm just going to copy this one actually here and let's give us some space and again this is very specific to python we need to indent here with spaces and i'm going to paste in what i copied inside these two lines basically tell python this piece of code or this line of code because we just have one line here belongs to a function called these two units and again we just have one line but this could be function with hundreds of lines right so i can write whatever i want the next line all good and then i can do some calculations and i can basically write whatever logic i want here and all these will belong to this function so now this logic is inside a function that is called these two units and we can actually get rid of this and test our function and here if you noticed we have this yellow line under the function name that's actually a warning for our code style it says we need two blank lines before so pycharm has a built-in official style guide for python that tells you how to style your code or how to write your code correctly now why is code style important well just like you write code you need to read code from other developers or your own code to understand what the code is actually doing so it's important that code is easily readable and this is just another example of how an editor like pycharm helps you to write code so to fix the warning we're just going to add a new blank line before the function definition so now what happens if i execute this block of code i have the variables defined here that i'm using in the function and then we have these two print statements so what happens if i execute now and as you see in the output here there is no output right nothing actually got printed so what actually happened because we have those two print statements that we defined in the function but we don't see anything in the output and the way it works is whenever you create a function or define a function like this you actually have to use that function right again this is a similar concept to variables we have created variables here and we're using that variable in our code the same way we create a function and we need to use that function how do we use a function or in programming languages it's called calling a function or executing a function and we do that using the name of the function like this and as you see in the suggestion as well using brackets like this so this syntax here basically is what we call calling the function that we defined right here or using that function so now if we execute it there you go you see that those two lines got executed and we have the results here so we're creating variables and we're using that here we're creating a function and we're using that function here and note the difference between using a variable and using a function python knows this is a function because we are providing these brackets at the end right that tells us we're making a function call basically but as you see now we have these 20 days so this only works for 20 days right what about other values that we had what about 35 days and 100 days we haven't de-duplicated that right we have just one case here so how do we use functions so that everything else except for these two values right here actually stay the same and we do that in programming by giving our functions some kind of input value which are also called parameters so instead of having this 20 basically here hard coded as we said here and here we provide that value of 20 or 35 or whatever that value is basically whenever we use the function so we are telling python use these days to units function with a value of 20 or use that function with a value of 35 and way we define this input parameter in functions is very simply by defining this parameter between those brackets right so this is where the input we as programmers define the input parameters so right here how do we define that using our already familiar variables so basically right here i'm going to define a variable and i'm going to call it whatever i want i'm going to call it days or number of days but i'm not assigning a value to that variable right like i did here because i want it to be assigned whenever i use the function so this basically tells python that a function these two units can be used using one input parameter which is called num of dates right number of days so now the last part which is remaining is replacing these values using this variable right and just like we do it in these two places where we're also using variables defined here exactly the same way we're gonna remove this and define a block for non-textual value and we're gonna put our variable num updates here and also here so you see now that our function actually doesn't have any fixed numbers in it instead we're using variables we're using two variables that are defined outside that function and we're using one variable that is defined within that function right this variable actually belongs to that function and now as you see whenever we want to use that function we're telling python hey call this function called days to units with a parameter an input basically of 35. so now if we execute it by the way we can also do it here you see 35 days are and the calculation was done here and hours and now if we go back to our previous example where we wanted to calculate these for four different values right for different number of days we can actually do the same so we had 20 then we had 50 and we had 110 right so this basically gives us exactly the same logic that we had previously but with different syntax right so you see the difference that instead of having this basically four times we have much cleaner syntax where we actually see what this logic is doing which is days to units and then we have the number of days that we pass or give our function as parameter and if i execute this you see that it was printed four times for all different values we can actually remove this line and now if we make some change in our function for example we change the name of this variable we want it to be hours instead of units we just do it once in one place and that's basically it so whatever changes we make inside that function and that is actually the big advantage of using functions in code whatever change we make inside here it doesn't affect how we use the function because for using the function we only need the name of that function and the input parameter now probably you're wondering what happens if i do not give my function a value let's delete those lines and let's say we do not provide any value here and we try to execute or call our function without the parameter and let's execute and you see that python interpreter is complaining because we have told python here is a function called these two units and whenever i want to use that or my colleague another programmer wants to use that function they have to provide an input value here right so now whenever python sees the usage of that function and there is no input parameter that we are giving that function it cannot execute that function right because basically this value is missing here right and that's why we get an error which also says missing one required argument so as you see whenever we define a function with a parameter with input parameter we have to also provide that input parameter another thing that you may be also wondering is what if i want to provide multiple input parameters to functions how do i do that it's actually very simple you can do that by simply defining here another input parameter separated by comma and let's say we want to pass here something that we want to print out additionally like a custom message doesn't really matter and the same way exactly the same way as the first one we can actually use it in here we can even create a new line and basically just print out that message right and note that if you are using a variable without stream so basically it's just the variable itself we're not using any text in that case you don't need this format right so you can basically just print it out directly and now again we have told python i have this function and two input parameters are required so i also need to pass to input values whenever i use that function so now let me just pass in some custom message let's say i'm happy about the calculation or i know i'm going to be happy so let's say awesome and let's do another calculation and this time i say looks good and if i execute it you see that i get awesome the first time and then for 35 i get looks good so you can basically define and provide as many input parameters as you want common practice and also what you will see in just normal code basically is just a handful of input parameters maybe one or two so it's not actually overloaded with 10 parameters cool now that we know functions and variables why they're used or why are they useful and how we use them there is one point that i want to mention which is variable scopes in functions what does it mean as i mentioned previously in this function we're using multiple variables right we're using two variables that are part of the input right that we provide when using a function and we're using two variables that are defined outside the function right somewhere else in the code so variable scope basically means where is the variable that function uses defined and variable has a global scope for a function if it is defined outside the function like these two variables right here could be completely in a different file not even in the same file where the function is so these are global variables so all the functions that you have in your python code can use those global variables and then you have local variables local variables are variables that are created within that function right so these two variables are basically only available inside that function because it was created within that function so internal scope and global scope so that means that when we create another function let's call it scope check because that's what we're going to do we want to test the scope in this function i can access the global variables like name of units but i cannot access the internal variables like number of days and let's actually test that so i have num of units which is a global variable defined here outside the function and the second one i have a variable that is defined inside another function right so as i said internal and as you see right here we already get a red line underneath that says unresolved reference num of days so for this function basically that variable is not visible it's not accessible it doesn't exist basically at all and we can even test it out actually so again if we want to see the results we have to actually use that function like this run the function and if i execute it you see that we get an error that says name num of days is not defined right it doesn't know that there is a function like that anywhere in code however it does recognize the global variable and that means that variables defined outside function itself global variables are accessible to all the functions and if you have variables defined inside the function then they're only accessible for that specific function and not for other ones and that means actually that if num of days variable does not exist for scope check variable we can actually create a variable inside scope check with the same name and we would have basically no problem at all so this is not going to be a duplicate because this function doesn't know about this variable the number of days here and scope check doesn't know anything about variable num of days right here and if we want to use that function now we have to put provide the value let's do 20 and if i execute there you go you have name of units and the 20 that we provided so global variable local variable defined here another interesting thing is that as i said inside the function body so to say so this is basically whatever is part of the function right in this indentation is called function body so we can create whatever logic we want inside that function body so whatever i can do in python generally i can do inside the function body so one of those things is actually creating variables right just like we created variables here we can create variables here like this let's call it myvariable and variable inside function and we can print that out as well so now we have three types of variables that we are using in this function we have a global variable we have internal variable that is passed in as a parameter and we have internal variable again that is basically defined so the variable isn't provided when we use the function but it's basically just defined inside the variable and again if i execute this see hours num updates and variable inside function text printed out like this and obviously this is just for demonstration but we're going to see more realistic examples of that in our projects so i'm going to clean this up and we can move on to the next concept so now let's go back to the example where we calculate number of hours and we do that for four different values now if you want this application to be really useful for us or somebody else it doesn't help that we have to fixate here just four different values right the application should be able to take any value that we basically fit it right so we should be able to provide any number of days and the program should calculate the number of hours in those days so what we need here is user input so when we give this application program to somebody else or use it ourselves it should allow some user input so how do we write a program that asks for and accepts a user input and then does something some kind of calculation some kind of other logic based on that user input accepting a user input from a python application is actually pretty easy let's get rid of all of these lines first and we can do that by writing input and this will basically give a user a prompt to enter some input value now you probably already see from the syntax that this looks very much like using our days to units function right in this case with the parameter so input is actually a function that python provides us with so we didn't have to write this input function right we wrote the days to units function but input function is available in python so that we can use it whenever we want and this parenthesis here tells python we want to call an input function and we're calling it or using a function without a parameter so now we know that we can write functions ourselves for logic that we need but python has tons of functions that python developers already wrote and is part of python and we can use it in our code so that not everybody has to write their own input function logic and the code or the functions that python provides are again part of this python that we use in a project and obviously if you're using the latest version of python you will get the latest code and latest functions that python basically provides so back to our function execution and let's see what happens when we execute this program i'm going to run it and in the outputs you actually do not see anything other than my cursor is blinking here so it's actually waiting for user input so i can actually type in something here so i can do 20 and enter and process finished with exit code so i was able to input something doesn't really matter it could also be a string actually doesn't really matter and program basically finishes so that's the simplest usage of input however as you noticed here running this program and basically having this blank output here is a little bit confusing so if another user is using our program they will probably be wondering what's happening here so what we can do is in input function we can provide a parameter that actually tells a user something right like enter a value for number of days or something similar so that it's not just a blank prompt right so we can do that by passing in a parameter a string which is going to be our message so we can do a user enter a number of days and i will convert it to hours and here you see right now that the program wasn't finished because we didn't enter anything so this program finished line wasn't output yet and that means the program is still running it is still waiting for our input and in pycharm in this ide if you want to terminate the program manually yourself you can click this stop button and as you see process finished so now we can run the program again and as you see here we already have a message that we're displaying to the user so that they can enter the hours right and here we can provide our input 30 and there you go however this doesn't look nice here we actually want the user to get a prompt on the next line how do we do that as part of a string we can actually add a new line or newline character basically and that in programming is actually represented by backslash n and you see a special highlighting for that one now let's execute and you see that my cursor is on the next line now and again let's provide a value enter and program ended so we have now a more user-friendly way of asking program user for their input instead of just showing an empty prompt and we're getting user input now we're not doing anything with that user input right that input basically just finishes the program and that's it we're not doing any calculation for that input so how do we use the value that user enters here when input function gets executed we do that and this is a very important concept by assigning whatever result this function usage gives us which is in this case user input assigning that result to a variable so right now it's basically executed and it's just wasted right it just disappears so instead we want to save that value that entered input value in a variable so that we can access it later and we do that by let's call it user input variable equals so just like we created variables here with variable name and a value right or even an expression right if we did a calculation here that will be basically a whole expression whole calculation the same way we can use values that function execution gives us to assign it to a variable so now what i'm going to do is i'm just going to print the user input so that we can see what value that user input variable has so let's execute again it asks for our input let's put 20 and print user input gives us 20. so user input variable value basically becomes whatever we provide as an input now that's a new concept and it could be a little bit confusing so let's see another example with our own function what would happen if we assigned value of this to a variable we get a warning as you see here in the editor which is one of the great things about ides that it basically gives you warnings when something is not correct and the warning says function doesn't return anything so basically if we want to have some value as a result of function execution we have to return some value in the function and how we do that instead of printing the value right away we can actually return that value using return keyword again you see special highlighting here just like here because return is a special word that python understands that you want to basically give back a value this value right here as a result of using the function right so you could have some logic here doing the calculation whatever and then as the last line you can return that calculated value and now the warning is gone because we're actually returning something from the function and when we return value from a function again we can assign it to a variable so that we can use that value whatever function returned by referencing the variable and one of the usages will be basically just to print it just like we did before like this let's delete it temporarily and test our program and there you go you see that this function got executed with input 20 this text this display text basically was put together in the function and was returned or was given back as a result and we saved that this whole string basically this whole sentence in a variable called myvar and when we print that myvar we see that value printed out here and again if we enter some other value we would get different text and again that is actually a very important concept of giving back some results from a function execution and you saw in our examples that you can have a function that doesn't return a value just print something on maybe does something else or function that returns some value you can have both types so back to our example of user input so now hopefully it makes sense that input function returns whatever value the user entered as a result and we can save it in a variable and then access it later again let's get rid of this line and now let's actually do the calculation on the user input number of days how do we calculate number of days very simple by calling our function that actually does that so we're going to call our function to calculate the number of hours for an input and the parameter that we passed in is going to be whatever the user provided so instead of basically hard coding a value here ourselves we give it a value that user provided instead so the num of days will be the user input and now again because this function returns the whole string it doesn't print it remember it just returns it so when i execute this and i provide in some value that's to 200 you see that i don't have any output here because we didn't print anything we basically just returned the value here we didn't do anything with it so the last piece missing here is to display the text the string that function returns so we save that return value first let's do calculated value so we save this thing in calculated value variable and finally we can print it and now let's execute it and let's see what happens i'm going to type in 10 10 days and let's see what we get now and you see we have this weird value here as a result of the calculation so what exactly happened so this part of calculation basically which represents this number didn't do a proper calculation and the reason for that is because the input value that we get with inputs function is always treated as a string and not a number so basically at this place here number of days inputs 10 here is actually treated as a text and not a number and where this weird result comes from is basically number 10 printed out 24 times so instead of doing the actual calculation 10 times 24 10 is printed 24 times and that's what happens when this is interpreted as a string so how do we make python see that number see that input value as a number as an integer and not a text or not as a string and it's actually pretty easy to do on the next line the user inputs again we saw right here it is a string so we have to make it into a number and we're going to do that using int and user input and that process of turning a value from one data type into another is called casting if you remember we already saw an example of it when we turned a number into a string in the string concatenation example again from the syntax you should already know that this is a function call so we're calling a function called int again that python itself provides us with because we didn't write that function and we're passing in one parameter which is a string and we should get an integer from that string as a result and we can save that number into a variable and let's call it user input number and now we have 10 as a number instead of as a string and instead of passing in that string user input we can now give our function number that it expects let's save it and execute let's put in 10 and there you go now the calculation is correct what i want to show you now is how this user input actually works without a code editor like pycharm so i'm gonna copy all this code and let's go back to our terminal and in our test dot pi i'm going to paste in our code and save and let's now actually see how the user input works when we execute it in a simple terminal window and as you see here we have the message first and the prompt is right here so we can enter a value let's do 25 enter and we have a result 25 days are 600 hours great so our program is working it is converting the number of days to number of hours and accepts a user input now what happens if a user enters an invalid value here so for example instead of a positive value for number of days maybe they enter a negative value like -10 for example and obviously that input value doesn't make any sense but we are still calculating and giving a value right and this is another important concept in programming generally that when we allow users to give our program some input value we also want to restrict them and basically validate that what they provided as input is a valid value for our program specifically one that it makes sense like in our case -10 program still does a calculation but it doesn't really make sense and second it doesn't crash our program right so now let's see what happens if we enter some text here you see that the calculation didn't happen because we basically crashed the program application using that invalid user input does make sense or an input that crashes our application because the calculation isn't even possible and we want to avoid users basically either providing a nonsense value or a value that will crash our application so we need to validate user input and this is again important concept in program because whenever you allow user input you always have to validate it so in this part we're going to learn concept called conditionals and we're going to learn this concept with an example of validating a user input so where are we going to do the user input validation we can do it in our function right before we actually do the calculation we can first validate is it a positive number and not a negative one so user gives us their input we convert it to a number and then we pass that number to our function so negative 10 positive 10 that is a number basically will be passed on to our function and right here we can check whether this variable value here is a negative number or not how do we do that we do that using if else conditional statements so very simple and intuitive to understand we say if number of days is greater than 0 means it is a positive number then we want to do the calculation and return all of this but you see that we have a red line here so basically whenever we use if condition we have to have a proper indentation so all of this line basically goes indented for this line kind of the same way as we indented the whole function body inside the function whatever logic and whatever code is right here indented below the if condition will be executed if number of days is really greater than zero so let's try that again i'm going to execute it let's put a positive 10 you see 10 days are 240 hours got calculated now let's put -10 as an input and you see none basically no output for us because this line didn't actually get executed for -10 input value now what if we want to tell a user hey this was an invalid value and that's why we didn't do the calculation instead of just showing none we can do that by returning this feedback message or error message whenever this is not true so whenever number of days is not greater than zero we want to return something else right another message and we can do that using else and the same way as for if we have the indentation and whatever we write here will be executed if number of days is not greater than zero so here we are gonna return you entered a negative value so no conversion for you and here it's time for another special term in programming that greater than sign is called a comparison operator and we have three of them we have greater than less than and equals and these are called comparison operators because they are used in an operation to compare two values and before in this course we learned arithmetic operations like plus minus divide multiply so again fancy words for simple concepts but these are the official terms for these so now if someone mentions them you will know what they're talking about so now let's execute the application and let's provide -10 and you see that this line was printed as a feedback to the user so let's go through the flow again user gives us their input in this case -10 we convert it to integer so now it's -10 integer and we pass it on to our days to units function and these two units function basically has this if else statement that's how it's called in programming and our if else statement validates or checks whether this input is greater than zero if it is then the function will return this as a result so basically the calculation with a proper message else so basically if this is not the case then it will return basically just a feedback message for the user and note that else doesn't have such a check here we don't check num of days is less than zero and the reason for that is because it doesn't check for less than zero because it basically just decides if this is not true else or in that case just do this so we don't need additional check here now this segment right here where we're doing the check is called a conditional so basically we're providing our program a condition if this condition is correct then do something otherwise do something else and that conditional can be true or false if we enter 10 then this conditional will be true because it's greater than 0 if we enter -10 then this conditional will be negative it's not greater than 0 and those true and false values in programming actually are represented by its own data type which is called boolean and in order to show it to you that this conditional either gives true or false what i'm going to do is i'm going to print this conditional right before i do the check so for a positive number input this should print true and if it's negative so it's not greater than 0 this should print false so let's test it out first i'm going to provide positive 10 and right here you see true because this condition is indeed true and note that this is not a string that's why we don't need the quotes here so we can basically just put in the whole expression in a print function and now let's do the same with negative 10 and you see false and as i said true and false values belong to its own data type in programming which is called a boolean and again we can demonstrate that instead of printing it out we can save it into a variable let's call it conditional check and in python there is a function called type which checks or prints out basically a type data type of a variable or a single value so if we pass it so this will give us data type of conditional check and obviously if you want to see that result we need to print it so print the whole thing so it doesn't matter what we pass in this point and right here you see class boolean so this conditional check here which has a value of true or false is of type boolean now there are two things that i want to note here the first one is that you see that i am calling a function here which is type and then whatever that function type returns which is basically this output here we are printing it so we have two function calls that are basically nested and that is absolutely fine you can nest function calls another example of doing this nested function called would be for example right here instead of assigning these to a variable and then passing it here we can actually save us that step and in the days to units function call pass it as a value so this is a perfectly valid syntax and you can do that you can nest in the function calls and you would not need this additional step and you can do that as many times as you want it just looks cleaner when you have that syntax a little bit separated and not have two three four function calls nested so that's one thing and another thing is we just saw a class boolean type here and let's actually see the same output for string and number so just for the demonstration let's actually see the type of string and integer let's get rid of this for a second because we just want to test so we're going to print the type of a string which says this should be a string type and execute and you see class string and now let's provide an integer you see class int and let's provide a float and there you go we have float so this is a very handy way of checking the type and we're going to need that actually in a later example so back to our application and one note about booleans and conditionals is that you're going to be using a lot of those in programming because they're really the major or the core part of writing any kind of logic in programming so you're going to need if else conditions and statements and booleans in your application so it is a very core concept and we're going to be using a lot of them in our coming examples so let's get rid of this code right here and one note before we move on here is that the negative numbers are not being calculated because of this condition now what about zero let's actually try that out so i'm gonna enter zero which is actually also valued that doesn't make sense because zero days input obviously will give zero of any units so enter and right here you see a feedback to user that says you entered a negative value so no conversion for you and the reason for that is because this condition here number of days checks greater than zero which means that negative numbers and zero will not match this condition so they will basically give you this feedback but our message says you entered a negative value right when we entered zero what if we wanted to have a specific message for a user if they enter zero and a specific one if they enter negative value right we want to differentiate between those two how can we do that in the if else statement in programming we can have multiple conditionals so basically we can have multiple ifs so how do we do that first we check number of days is greater than zero okay great now we want to check whether number of days is equal to zero and we can do that using another if here and in python the syntax for that is el if which basically is a combination of else and if so if this is not true then we want to check another condition number of days equals 0. now you know the equation sign generally is this one right here however when we're checking whether something equals to something else in programming that equation check is represented with two equal signs and the reason is because one equal sign is already used to represent assigning a value to a variable so not to confuse those two actions in programming we have two equation signs when we want to check whether something equals a certain value and just like here we execute some logic whenever this condition is true so in our case we want to tell the user you entered a zero please enter a valid positive number so now again to go through this logic flow if the input value is a positive number then function will return the calculation and the message if it's not a positive number then we will check additionally whether it's zero if it's not zero then this is a last condition basically then we know that it has to be a negative number right so let's test this out let's enter a positive number there you go let's enter zero you entered zero please enter a valley positive number and let's enter negative one and everything works perfectly note that you can have multiple alif's between if and else statements and again note that el if has a condition just like if statement else does not have any condition so this is like the fallback so if everything previously stated all the previous conditions did not match so they were all false basically then this is the logic that will get executed in that case great so we have validated input value for users and we may feel pretty good about our program because it doesn't calculate anything for values that don't make any sense however we still have a problem what happens if a user enters a text instead of a number so basically anything that is not a number value doesn't really matter basically some text if i enter you see that our program actually crashed and this is actually a user input that we want to protect our programs from because we don't want to allow users to blow up our application right so let's see what happened it actually says that we provided an invalid literal literal basically means the text itself or a number so basically a value that we entered for int function that's the problem so this function call basically just blew up because in function expects a value which is a string representation of a number so basically ind expects something like this or something like this it doesn't expect my text or some text so our application crashed on some text input now let's see what happens if i provide a number but a float number instead of integer let's enter 19.99 and enter and you see the same error this function basically returns an error because it cannot convert float into integer as well so we have a problem here that whenever user enters anything which is not an integer our program will blow up so how do we avoid that so basically before the int function gets executed with an invalid value we need to validate this whole thing and stop our program before that happens so we need the validation before int gets executed and we can do that right here right so before that line gets executed we're gonna do a validation and one way we can do that is using our familiar if else statement so we can say if user input is digit so again from the syntax you know this is calling a function but note that instead of calling the function like this so basically just stand alone we have the syntax where we have the variable dot and then the function name so instead of passing this as a parameter right here in the function brackets we are passing it as a parameter again but using this syntax right so each digit function will execute for user input as a parameter and again in if we have conditions so this will be either true or false it's a boolean and if user input is digit it's a number basically so basically this will filter out input values that are text so basically they're not numbers and if the input is digit then we want to execute this line and also the rest of our application like this and if it's not true so if the user input is not a digit we don't want to execute anything in our application we want to stop the execution and want to tell the user this was an invalid input value i'm not doing anything with my program so that it doesn't crash so else if not digit we are telling the user your input is not a number don't ruin my program so now let's actually test it so i'm going to execute and write some text and there you go so user input is digit was evaluated to false so none of these got executed instead the else block was executed and it printed a message for our user so we basically avoided our application to crash with this check and if we try a proper number it works and if we enter 0 for example the application will get executed and we will get our message from here so our application is more or less protected and we're not allowing for a invalid input number you say not a valid number now we know that in programming there are different types of numbers we have float numbers as well so let's actually see what ease digit function really checks so let's actually try to provide a float number instead of integer which is not a proper valid input for our program so i'm going to type in 19.99 and you see that each digit is false for a float number so we got the same your input is not a valid number output for float number as well which is perfect it's exactly what we need and now let's actually try to enter a negative number and let's see if each digit function returns true or false i'm going to enter and you see that else block got executed because is digit was false for a negative number so basically this function filters out a lot of the bad input for us to protect our application from bad user input and that also means that we don't need a check for negative values anymore and by the way in python you can actually have if statement without an else at the end this will work perfectly fine and this could be our application however note that in programming we don't put this type of logic directly like this basically outside a function as a common and best practice we encapsulate most of the logic basically in functions so as a cleanup for our code we can take all this and put it into a function so right here i'm going to create a function and let's call it validate and execute or something like this and all the logic that i copied will be just simply pasted in that function like this so now we have the logic nicely encapsulated in its own function but as you know when we create a function we have to call that function otherwise nothing happens so we're gonna call it right here we don't have parameters and we don't have to provide a user input because user input is a global variable so our function has access to it let's actually test it out and it works and let's provide some bad input and it works too so that's actually a proper way of writing code having every piece of logic in its own function with a proper description for the function now as a next step i want to show you also a concept or something that you will encounter very often in code which is nested if else statements and i'm going to show that example by cleaning up our program a little bit so as you see here we're doing validation in two different places so we're validating user input here but also inside these two units function itself so what we can do is we can put all the validation in one place in this validate and execute function and basically have all the validate logic in here and let the days to units function just do the calculation and not the validation and this way our functions and code will be a little bit cleaner and more logically built so after we validate that user input is digit we convert it into an integer so right here we have an integer value of the user input so we can actually check whether that integer value is greater than zero or equal to zero and we can do that right here without passing it on to the days to units function and just like this if conditional we're gonna do user input number greater than zero if that's the case we want the calculation to happen so both of these lines will get executed only if user input number is greater than zero and else in our case if it's exactly zero we want to print a message to user you entered zero and remember we've got the negative numbers already covered using this condition here so that's the one remaining i'm just going to copy that and paste it right here variable is called user input number so we'll fix that and we're going to print out that message so that means we don't need any validation right here we can simply do the calculation so to go through this logical flow of if else or nested e-files statements we have the first if right here also note that indentations for these if else so basically they are on the same line here same position and the nested if else or in our case alif they're also in the same position so again first that's the big validation the first one is is the user input digit in the first place if not we basically shut down the program we print out a message to the user and that's it if it is a digit then we need additional validation is that digit a positive number or is it a zero so we do that additional validation using a nested ifall statement and basically this line gets executed only if it is a valid number and if it's a positive number and because of that we don't need any additional validation once the these two units function gets cold and you probably already think and notice that nested if else statements are not the most beautiful thing in the world they are pretty ugly actually and especially we have multiple of them or multiple else ifs in between the function may actually end up looking pretty bad so it's not recommended to have multiple levels of these nested if else statements in your code to basically just keep it clean but as i said you will encounter it a lot in programs in application code because sometimes you just have to write it like this thing i want to show you here is something called a try accept in python so right here as you see we're validating user input right and then we're executing this part of code only if that condition is true what if we had logic here where there were multiple places where something could go wrong let's say a function execution could basically just blow up because of a wrong input or even as an example maybe this is digit doesn't actually cover all the use cases so maybe there is a value that user can input that will still blow up our program so instead of checking each such possible scenario using if statements and then doing the actual calculation once all those things have been validated what we can do instead is basically tell python something like try executing this part of code here and if something goes wrong so if any line any function call or any logic execution fails we want to catch that error and we want to control what happens with that error ourselves programmatically and then catch logic will be accept and we're going to specify what type of value we want to catch basically using this block and the name of that error type is actually what we saw in the output when we provided some text and a float which is value error so that's the error that we want to catch basically so again we tell python you know what try to execute this block of code here and if one of those lines in that code results into a value error then instead of just crashing the program basically catch that error and print out a friendly message to a user so let's actually try this out and note that try doesn't have any validation like if statement so when i entered some text here int function will actually be called with some text and it will result in a value error and this block will then handle that error so i'm going to enter and and you see that your input is not a valid number message got printed and the same will work for a float number that also doesn't convert to integer and for a proper integer value it will work fine and again difference between if else statement and using try except is that you can cover multiple such errors with this whole try except block right so you don't have to do the validation specifically and that is especially useful for cases where you can't really validate something using if condition right if there is a chance that you might miss something in that condition check so basically you just say you know what just try to execute with whatever value and if error happens then i still got it covered and an obvious difference between this if else is that here nothing actually crashes the program the program will still work is just a nonsense value with zero days right and as you see here we have specified an error type but with try except what can also do is you can basically say you know what i want to cover any type of air i don't care if it's value air or some other type of air let me handle any type of air in this case you can basically just leave it without a specific type you get a warning though that it's too general however this would work and you will be covering all the air cases with it but we can go back to our value error and know that in many programming languages this is actually called a try catch so accept is actually pretty specific for python i personally think try catch makes more sense because you are catching any error that may happen right here and then basically handling it in the catch block so if you happen to hear or see try catch in other programming languages note that this is the same thing as try except in python one thing that we're missing because we converted that if statement into try except is now we need to validate the negative numbers again because negative numbers will not cause an error in our application so they're not covered by this except block here so let's add back our previous else block and again if neither of these are true it means automatically would mean that it is a negative number so let's print that message for a user enter negative number no conversion for you so now we got all the cases covered again so let's try that out -10 and there you go now there is one minor problem with our application and that is we can only use it once so basically whenever we type in a value it either calculates it or gives us a message that we provided an invalid value then the program basically exits so if we want to do a calculation for another value then we have to restart the application provided value and then do the same so that's not very convenient if we want to use that program to basically calculate a number of hours for many different values so basically we want to keep fitting it some different values for calculation because we don't want to restart the application over and over again so how can we actually make the program continue after it's calculated the first value so we want the program to basically just keep running and accepting our values so how do we do that and we can implement the logic using something called while loop concept of loops in programming is basically you do the same thing so the same logic gets executed multiple times in a loop and how many times that logic gets executed is basically defined in the condition of that loop and the condition could be it should run 10 times or condition could also be it should run until some specific event happens in the application and conditions you already know from if else statement conditions are basically logic that gives you either true or false so condition is checking some logic and then giving you a result which is either true or false so now the question is how many times do we want this application to run in our case let's say we want to run indefinitely right until we actually stop it from here until we kill the program so it means the condition for that should always be true and for our application we're going to use a while loop which is one type of a loop and very simple syntax while and while just like if actually takes a condition this is going to be the condition for the loop so basically we want to tell the while loop how many times it should run the logic coming after it and again you know our familiar indentation these two lines should execute in a loop over and over again with some condition right here so how many times do we want the program to execute in our specific case well we want the program to run indefinitely until we actually stop it from here right from our editor so that means that condition of the loop that decides whether the next lines should get executed or not in our case should always be true because if it's false then the loop will stop right no execution anymore so how can we make sure it's always true we just write true right here and you remember i told you that true and false in programming languages have their own data types well they are also reserved words even though this is just text representation basically you see the highlighting here just like for these other words because python knows this is a boolean value of true and note that again specifically for python we actually write true with capital t unlike in other programming languages so this will basically be just a string so python doesn't know what to do with it but it recognizes true width capital t so we're telling our loop our while loop the condition is always true so basically run these two lines indefinitely because the condition will never change it's always going to be true and the syntax is probably already familiar to you we have the keyword just like if or try we can actually compare the syntax to the if statement so we have the while keyword here then we have the condition so this is the same type of condition as this one right here it's just that with us is basically fixed true value here it could be true or it could be false based on the condition and then we have the colon and then we have the indentation for the next lines so all of these belong to the while loop so that's basically it we can now execute the application and see that our program runs indefinitely so this is the first one let's say we enter 10 awesome we have a result and then we have again the next execution where it's asking for our input again and we can do -10 now it's telling us we provide a negative value but we can enter the value again let's enter some invalid value that should be handled by this value error block execute it's not a valid number and application basically continues so again as you see if you have an application that should continue running and basically get user input over and over again you don't want that application to be crashed at some point by some bad input you want to catch and handle every type of input so that your application continues to run and if we want to stop it we can basically just shut it off here and process finished now we can do one optimization in our program and this would be to allow users themselves to stop the application using some specific input value so for example when they type in exit for example the application should stop only for that specific word so it shouldn't run anymore so how can we do that so basically instead of having a condition that is always true we want a condition that says if the user did not input word exit then continue running the program if user entered exit then basically just stop it right so we need to change our condition right here so here we want user input is not exit right so user input is not exit now we didn't learn how to check for not equals something right we have check for equality so we check whether input number equals zero or if it's greater than or smaller than however we didn't learn how to check for not equals not equals in programming and this is not specific for python is expressed like this so instead of two equation signs we have an exclamation mark and equals and if we want to compare it to a string obviously we type that string in our case it's exit so basically this condition tells our while loop while user input is not exit continue running the program so while this is true basically just keep moving on but if user enters exit this will not be true anymore it will be false so loop will basically break and application will end now you see that we have a warning on this user input that we're using here and if i hover over it you see a message that says name user input can be undefined so it's not always undefined but it can be and here i want to mention again that pycharm because it's an intelligent editor can give you actually this warning when otherwise in a simple or normal code editor you probably wouldn't get such a warning so again it helps you in avoiding some of the mistakes that you might accidentally make when writing code so basically it detects the errors or possible errors for you and tells you to fix it so again this kind of intelligent warnings and error messages can be a lot of help when programming an application and now let's fix that warning so what's that all about the problem is on the very very first run of this while loop user input variable itself will not exist why because we're creating that user input variable on this line first so our application will basically complain because when our while loop runs for the first time it will not be able to find user input variable let's actually try that and as you see user input is not defined so we need to create that user input variable before the first execution of the loop so right here we're going to create user input so we're going to create this user input variable before the while loops first run and it doesn't really matter what the value of that user input variable will be because what matters is the variable exists so the value is basically just empty and as long as it's not exit the loop will start running and our application will get executed so again to go through the flow of this we're creating a user input variable then we're making sure it's not exit if it's not this line will get executed so we get the input from the user that's going to be our first input and then we call validate and execute function that basically does all the checks and execution so that's going to be the first run of that loop on the second run of the loop the condition will get re-evaluated so now the user input is what the user provided so again python will check is the user input exit no cool let's execute the program again user will have to enter another value and then that value will get re-evaluated so note that after each time that this block of code has run and executed every single value that user will give as an input will be evaluated over and over again in that condition and while user input is something else then exit it will run the program if it is exit then the loop will break so let's actually test that so this is our first message and our first input let's do 10 it got calculated let's do minus again calculate it and we can do that over and over and over again and now let's actually enter exit and let's see what happens and as you see a process finished with exit code so our application actually stopped because this condition was not true anymore it was false because we entered exit so that's how we can allow users to finish the application and two takeaways here one is a conditional in a loop and the second one is a negative equals check for values great so now we have our application that basically allows inputs multiple times and can be terminated with exit input now what if i wanted to calculate number of hours for several days but i didn't want to basically just pass it on one after another because that's just too much effort for me so let's say i wanted to give it 10 different values but i don't want to type it in 10 times right i want that application to basically do the whole calculation for all those 10 values but i just want to enter that list once instead of entering each value separately so in this part we're going to learn another data type in python called list so till now we have learned several data types like strings for example as well as integers and float numbers and boolean true or false and list is just another data type and the syntax for list is using square brackets and inside the square brackets we have a list of elements and those elements can again be either strings numbers or boolean values in our case we want to provide our application with a list of number of days right so we're going to have a list of integers like this and you can have as many values inside the list as you want and these are going to be comma separated and the syntax will look like this but again as i said you can have a list of strings list of booleans etc so as i mentioned we want to let user basically provide inputs as a list instead of those individual values one at a time so i'm going to copy this let's clean this up and let's actually see what happens if i just input that list as a value right here and enter you see program doesn't recognize that as a valid number obviously because we have the check and causes a value error that's why we get your input is not a valid number don't ruin my program message right here so we want to change that and make our program actually accept that as a valid input so how do we actually implement it and let's go through this logic theoretically first before we start implementing it what we need here is that we want to read this list and we want to do validation and execution for each value one at a time so that means that validate and execute function needs to be called for each value in that list right so basically this function for all this logic needs to be executed for each element now how do we get the individual elements of the list so you remember the definition of a loop loop is basically executing same function with the same logic multiple times and the number of times basically depends on the condition right here we have a condition for while loop and the condition for executing the same logic for each of those elements is going to be the number of elements in that list right so we want to execute the logic as many times as we have elements in the list and for that we have another type of loop called for loop and basically we want to execute validate and execute function using a for loop the syntax for for loop is a little bit different first of all we have four keyword just like for while then we have the element is a variable so we can call that number of days so this variable basically represents each of those elements in the list and then we have in which is again a keyword of python so for in basically puts together the for loop logic in python so python knows what we are trying to execute and finally we need the list that we are executing this loop on and our list is our user input and that's our for loop however again you remember the indentation the logic that for loop executes needs to be indented so it should belong to for loop so now we can read that for loop statement as follows for each element which we call we decide to call number of days in a list called user input we want to execute this logic exactly what i said before so for each element in this user input list we want to execute validate and execute function we don't have a condition written here like we did in while loop or if statement so the condition is basically implicit right the condition says however many elements there are in this list that many times that function or that block of code here will get executed so if i provide five elements you will be executed five times if i provide 10 elements it will be executed 10 times and that's exactly what we want however there are still a couple of things we need to do for this application to work properly first of all validate and execute function still uses user input right here so instead we want it to use the individual values of the user input because now it's going to be a list and not a number so this element we can actually call it num of days element this value should be used here instead right so we're going to use that element instead of the whole user input list and paste it here so that fixes the first problem and the second one is as you know user input is always a string so even if we provide a list of these it is still going to be considered as a string so we need something similar to this right here so the list of numbers that are provided as input needs to be converted in at least in python and we can actually do that in a convenient way using user input dot split and split is a function that will take user input as a parameter and will give us a list data type so split function basically will return a list of all those input values and again note the syntax of executing the split function we're not just calling a split function like this with user input as parameter instead we are basically calling it on user input and i'm going to explain more about this type of functions in python later but for now let's actually execute this application and see what happens so first of all how am i going to provide a list to this application so that userinput.split can actually convert it into a proper list in python and the way i should provide the values are list of values with spaces between them so basically like that so why spaces how come it's not a comma or something else the reason is because split function by default splits that provided list on spaces and then creates a list value out of them so that's the default behavior of split however we can override that behavior and we can decide you know what we want the input to be a list of numbers that are comma separated instead of space separated so what we can do is right here we can do split on a comma so that's what our split function here will do and now if i re-execute it and i can do commas now 40 55. so that's going to be user input and split with comma should convert it into a list value in python so let's execute it and as you see it worked fine we have a calculation for each of those values now let's actually play around with this and let's say we provide a list with some values and somewhere in between we decide to add a text and then maybe a float so let's see what happens now and you see that for those two valid numbers the calculation was done properly and for the other two basically the validation kicked in and it says that these are invalid numbers however again our application ran without any problems and we can even provide another input so now let's actually do 20 maybe a boolean even and a negative number execute and you see that for 20 the number of hours got calculated true is obviously not a valid number and minus 100 is a negative number so everything works perfect validations in place and we can provide a list here now and as a final optimization we can actually change this message here that says hey user enter number of days as a comma separated list and i will convert it to hours so now we can provide either just one value and it works fine or we can provide multiple values as a list and again if we want to see how that list actually looks like and check the type that it has in python let's actually print it so first i'm gonna print type of user input called split and then i'm just gonna print that list itself so let's execute and let's provide some values and here you see this is the class list so basically the data type list and that's how our list looks like so we have these square brackets here and each element inside as a string because we have the quotes here and later our application then transforms it or tries to transform it into an integer and you also see there are some additional spaces here because that's how we entered the values so basically the split splits the values on comma so this space also becomes part of that element so to fix that leading space in each value we can basically just add a space here so it will split on comma and space and let's test it out and there you go we have our values without the space before them so you saw an example of when we actually need to use list data types in this example we wanted to allow users to basically just input multiple values at once as a list but obviously this is just one of the use cases for a list and one of the specific examples because throughout the application you can create lists and you can use the elements of that list in another function so you can use it for multiple use cases and just to show you some basic syntax of how to create lists within your code inside your application so how to initiate it and then how to use that list i will show you the examples here so basically to create a list in your code you would use that syntax that i showed you earlier with square brackets and inside that you would have list of elements and this could be strings so it could be for example names of month let's say like this and then we would have to assign it to a variable right again just like we did for strings or integers or other data types so we can call it my list and this will give us a list with three string values inside and once you have created a list you can then use it to read the values from the list right so basically to get the individual values elements out of that list so for example if we wanted to print out the first element of my list then we would access that using my list and then following syntax with again square brackets and then index number of the element which is starting from zero so this is the first element with index zero this is the element with index one index two and so on right so it could be confusing because it starts with zero not with one but that's how the specific element of a list can be accessed right so if i wanted the march value here the third element then i would just type in index two and this is a syntax of getting elements from a list if i just want specific values and not each element one by one as we had in this example right so one way to use a list is to basically loop through it and get one element at a time and that's why we don't need to use index here because it gives us elements one after another or maybe we don't want all the elements or to do something for each element we just want specific values from the list for different use cases in this case we can access them individually like this and in addition to accessing the values from a list or basically reading the values from release we can also add values to a list and we can do that by typing my list and then on the my list we're going to do dot and use the append function right so append will basically take the next element or another value basically and add it to the list of elements already in the list right so let's say we want to add the next month and now if i print my list we should actually see this here with april as a fourth element right so let's execute and there you go so first we have march here printed out which is the third element right index two and then we have my list append that adds fourth month name into the list so after append this is how my list will look like and now again we can access the specific element for example if we wanted to access the newly edit element on index three so that's going to be the fourth element and there you go we have the new element printed out and also an interesting note is what happens if we use an index here which is too high so basically pointing an element that doesn't exist right so instead of three let's say we have four here so this will point to the fifth element and since we only have four elements let's actually see what's gonna happen so i'm gonna execute this and there you go you see an error index error that says a list index out of range so every time you access or you try to access an element at an index that doesn't exist you will get this list index out of range error and this is actually something that may happen pretty often when working with lists if you're accessing the values like this so this is how you work with lists how you create them and access values in them as well as add some values however note that most of the time if you are using lists you're going to be using them in a for loop because most of the time you would want to do something specific for each element in the list so this basically should give you an idea of syntax of lists and how to use them generally and also how to use them in combination with for loops here i want to take a few seconds to thank jetbrains the creators of the awesome pycharm and intellij editors for sponsoring this complete course besides all the great existing products they have they recently introduced space which is an all-in-one team collaboration platform covering software development project and team management and the great thing about space is that every single tool you would need in a company to collaborate and work together in a team is in one space so you have tools for chats blogs planning and creating tasks meetings but also software development tools like version control ci cd package repositories and much more so all these in one platform and in terms of productivity the great thing is you get all the notifications in one place like code review updates newly created issues new blog posts etc you get all of these in chats where you can react directly in place or add them to your to-do list automatically so i think the main value of space isn't that it has all these tools but rather the fact that it integrates them in such an intelligent way that it improves the team communication and productivity overall now let's go back to coding now at this point here i want to mention a pretty minor and simple detail but something that is also very important and useful in programming and that is comments so how do we write comments in python and why do we need or in which cases are comments actually useful one usage of comments is basically to give yourself some notes on your own code so basically if your code gets a little bit complicated and it's not really clear what the logic or the function body is doing it could be because of bad programming but also because the function logic is just a little too complicated so you want to add some textual notes to that code so that you understand what's going on there even when you look at that code days or maybe weeks after you wrote it so for example right here we may want to add some notes about what this part here is doing and comments in python are written with this character so for example i want to add a note here we want to do conversion only for positive integers and that's basically my note that summarizes all that logic right here and it is not only useful for myself in case i forget what this complex logic actually does but also for your team members so if you're working in a team and your code gets really complex and it's difficult to keep an overview of different functions and variables and what all these things do you can basically add these comments as notes to your team members so they can also understand what you thought or what kind of logic actually you were thinking about when writing this code so comments is a way of communicating your thoughts and your logic on your code so that's one use case for using comments and the second use case if you have a piece of code that you do not want to delete because it has some logic or it has some example that you want to keep as a reference but you don't want that code to be executed so basically you can comment out code so that it still stays there but doesn't get executed and the way to do that is just basically having this character before the line and also note the color of the comment in the editor is gray so it's immediately visible that these are the comments and now these two lines will not get executed by the program they will be ignored but they are for your own reference in case you want to keep it there for example this could happen if you are unsure about the change and you want to delete that whole code from your application because you might actually need that eventually so you're not sure about it so temporary comment the code before you permanently delete that so these are some of the use cases of comments now if you have multiple lines of code that you want to comment out or maybe hold text that you wrote as a note so for example you want to comment out all these four lines here obviously you don't want to do this in front of every line so for multi-line comments you can actually use the following syntax which is three quotes at the beginning and at the end and again you see the highlighting is different here which basically is highlighting of a string however it is ignored by the program and it will not be executed as a best practice you shouldn't have too many comments in your code because it will just add additional clutter and your code will look basically unclean so you should use them only when needed in this part i'm going to show you another data type in python called set so what is a set let's start our application again and let's say i provide here input values as a list let's say 20 40 and 20 again so basically i have duplicate values in my list and when i execute it i see that the program got executed for each element and i have duplicate results because i passed in the same value twice now again users they can input any value they want either intentionally or accidentally this could be bad values or things that don't make any sense so let's say we want our program to behave in a way that if user provides the same values twice or three times we only want to calculate the number of hours for that value just once so basically for this input we want just those two lines and basically ignore the execution again for the same value and you probably already guessed that's where set data type will help us so set is basically a list of elements but with unique values inside so list data type allows duplicate values the same value multiple times set does not allow duplicate values so how do we use set instead of a list and it's actually very easy to convert an existing list into a set and we can do that using a function called set and basically the parameter of our set function will be the list so if this list here contains any duplicated values that will be basically just filtered out when we convert it into a set so that's how it works and again for demonstration let's actually print out how set value looks like so i'm going to print the list first so that we can compare then we're gonna print out the set and let's also print out the types and i don't want to repeat this expression over and over again so what we can do even though this is just for demonstration we can actually extract it into a variable let's say list of days like this and we can use it everywhere we need and this is a little bit cleaner because we don't have to repeat the same expression over and over again and again let's print out the type of list of days and then print out the type of a set of lists of days and again you see that nested function calls three times and this should give us some interesting value that we can compare lists and sets and just a small note here that whenever we have this nested function calls think of the execution from inside out so the first function that will get executed in this chain is actually the set right so it starts from the last function in that chain so set function will get executed and convert that list into a set then type function will get executed on that resulting set value and then print will get executed and print basically the result of the type so the order goes from innermost function to the outer functions so let's save it and execute and now i'm going to again provide a list let's say 10 and then 45 and 30 and 10 again and enter and first of all we see that 10 only got executed once and not twice and we can also see the first one is basically value of a list and you see at least here the second one is a set and you already notice the difference that list basically uses square brackets and set is represented using curly braces right and again it has value of 10 just once and again we have the data type of a list and a set so just to demonstrate that they are actually own separate data types and like we saw previously with lists we can also create sets as well as add an excess values from it so let's see an example here for example if you want to create a set the syntax for that will look like this as we already saw again set can have strings or numbers or booleans as its elements let's use the same example we use with lists so i'm gonna just basically type in names of the month here so let's say we have again january february march and we're going to assign that again to a variable so this will give us a set that we can create ourselves not just from the user input and now we can access the values the individual elements from the set and we can also add elements to the set however we cannot access the individual elements of the set like we did on lists right you remember on lists we use the syntax with square brackets and the index of the element in set we cannot actually do that instead we can only access the elements of a set in a loop so for example in a for loop we would write element in my set and then we can basically print that element so let's execute and right here you see all the elements printed out and again as i mentioned with lists also previously most of the time when you're working with lists or sets you're gonna want to look through the elements because you probably would want to do some operation on individual elements or some of the elements right instead of just grabbing individual one or two values from that now how do we add elements to a set let's see it as well we can add elements to my set using my set so the set itself the variable name and then if you do dot you see a bunch of built-in functions again set has its own built-in functions which are completely different from what list has and it has a function called ed and here we can basically add a new element again let's do april and now we can print my set and execute and there you go so we have here individual elements that are printed out and we have the new list after we edit april now one thing you probably already noticed when working with the set like printing its elements or adding a new element it doesn't work in the same sequence as with a list right so for list we basically had this january february march in this exact order and when we edit a new element basically it got added at the end right as a fourth element in set it's not ordered it's basically in a random order right so if i execute this again rerun you see the order changed here and here as well right and if i do it again there you go so this order and this order basically changes right so as you see working with sets is actually pretty different from working with lists so this means that set not having or not allowing duplicate values is basically just one of the differences between sets and lists and of course you're probably wondering i can add elements i can access elements what about removing elements right from lists or sets and you can do that actually the same way for both sets and lists so for example if we want to remove an element from a set we can execute a built-in function called remove and remove basically takes a parameter which is the value itself so let's say if we want to remove january from here then we're going to pass the value to remove function and now if i print my set again after the remove gets executed and let's run it and here you see the last print basically doesn't have january inside and as i said it works the same for lists so let me demonstrate this as well so on my list which has its own set of built-in functions one of them is remove which in this case is called the same for both set and list and this remove built-in function also accepts the value of the element you want to remove and let's print it again my list and execute there you go this is a set with the syntax with curly braces and this is a list which now doesn't have january inside because we removed it and as you know lists can have duplicate values so if we have january again here so two times and we do remove january it will actually remove the first occurrence of that value so if i execute this you see the first one got removed and the last one or the second january value is still there so that's how you work with sets basically and also how to remove elements from sets and lists so at this point i want to take time and review the functions that we have used from python itself so the functions that we ourselves didn't actually write and the functions that python basically makes available for us to use are called built-in functions so till now we have used function print which basically just takes some input this could be a string this could be a variable this could be a number doesn't really matter and basically just prints it in the output console right in the terminal we also saw input function that basically takes user input again we can provide a parameter which will be a message or leave it empty we also in the last example so an example of set function which basically takes a list and converts it into a set and another example is int again takes a string as a parameter and converts it into integer and all of these are built-in functions because python provides them to us so that we can use it in our code and in python there are a lot of very useful built-in functions for different purposes and as i mentioned most of these functions basically accept a parameter and then do something with that parameter that input that we provide that function and give us an output again print basically provides something here and gives us the output in the output window here in input we can provide some message that will again print an input here and the rest of them are giving value inside the code without displaying anything so in set we can provide a list and the same way a parameter here which is logical because the purpose of a function usually is to take some input like this as a parameter do something with that input and give us an output from that parameter and we have created our own functions that also take some input and give back some output like this and in addition to built-in functions that are called like this and our own functions we have also used third type of functions which is called directly on a value right and this is an example of that so basically directly on a value this could be a variable but also the string representation of that itself so if i had a string like that so basically directly on that value call a function like this and these are also built-in functions because python makes them available to us other than the syntax there is one major difference between these type of functions that are called directly like that and the functions that are called on the value itself and the main difference is that each data type has its own set of functions that can be executed like this so again let's take example of a string and if i do dot here you see the ide pycharm basically gives me a list of all the functions again built-in functions in python but functions that i can execute on a string value and again you see split is digit this is the one that we used in one of our previous examples that basically checks if that string is a representation of a digit or just a regular string and as you see lots of different functions so all of these are provided by python to do different stuff like turn the string literal to uppercase or replace a letter for example in that string and so on but the main point here is that those functions are only for string but as i said each data type has its own list of functions that can be called on that data type so for a demonstration let's take a list and if i do dot here again you see a list of functions that i can execute specifically on list values and there are lots of things that you can do with lists like add elements to it remove elements from it sort the list copy and so on so that's basically a major difference between these type of functions and functions that can be executed on specific data type values but the concept is the same here you also have a function that gets a parameter an input and gives you an output and the parameter for this type of functions is actually the value that we are calling that function on so this string here 2 3 is going to be parameter of the split and in addition to that we can also provide additional parameters for example for split you saw here that we were able to add a comma space to basically tell the split function on which character to split that text so that was function comparisons in a nutshell in this part we're going to learn a new data type in python called a dictionary and we're going to do that by modifying our application and modifying how users can actually input the values now our program currently takes the number of days and turns it into a number of hours what if we wanted to make our program a little bit more advanced a little fancier and basically allow users to also decide what units it should be converted to so user gives us the number of days and tells us whether to convert it into hours or minutes so instead of the whole list now we're going to get just number of days and the conversion units and this change may be a little bit complex in terms of syntax so i will try to explain everything step by step so the first thing we need to do is adjust how we get input from the user so now we want the user to be able to give us the number of days and units in one input so first of all i'm going to adjust the text here enter a number of days and conversion unit now there are a lot of different ways we can allow a user to do that but what we want to do is something like this as an input so basically number of days colon and minutes or number of days and hours again this is our own decision we decide how the user input should look like but this should be a nice way of providing both of those values basically colon separated so instead of a list we're going to have a single input with two values that are separated by a colon so on the next line i'm going to change this split here into a column and this will give us user input this whole value split into two values as a list this is going to be day and days and unit so least with these two values and let's actually get rid of this for loop because we're not iterating through a list of numbers anymore and to begin with let's just print out the output or the result of days and unit variable so i'm gonna refresh and let's do two hours and enter and this is what we get so basically splitting this string on column will give us a list with elements 20 and hours and note that 20 is still a string right because that's how we get the input and now from those two values we're going to create a value of a data type called dictionary so in python we have another data type called the dictionary which basically looks like this so you have curly braces just like we had for a set and inside we have key value pairs so we don't have just single elements like this but rather we have key value pairs so key in our case is days and the value of that key in our case would be 20. and then comma separated another value would be unit and a string hours so basically this is the syntax of a dictionary and obviously the difference between a list and a dictionary is that we have descriptions for each of our values represented as key value pairs so we want to create this construct right here from those two values and since this is a data type we can create a value and assign it to a variable of that data type so let's call it days and unit dictionary equals and obviously we don't want those values to be hard coded here we want to get it from the list now how do we access an element of a list how do we grab the first value which is 20 out of that list and to do that there is a syntax to access the elements in the list the list name and then you have square brackets and index of the element which if you remember you already learned in the section of lists so again days of unit list is this one right here and with index 0 we are accessing the first element index 1 is the second element and so on and index is basically just location this is location 0 location 1 and so on so this will give us the first value and obviously the second value is going to be with the same syntax with index one and let's actually print that one out as well so that we see the results restart the application let's do 20 hours so again this is our list 20 hours as two separate values and this is a dictionary that we created using those two values so we have key value pairs basically and the same way for minutes we would end up with a dictionary that looks like this so now we have these two pieces of information in one variable and now we can give that variable to our function so that it does the validation first of all of the number and calculation based on the units so on the next line we're going to call our validate and execute function and as you see we have this red line here because something is wrong first of all we don't have this variable called num of days element anymore because we deleted the for loop instead we have days and unit dictionary and here we are validating the integer value of the input number and the same way we need to validate this input number here as well so now the question is how do we access a specific value in this case the value of days here in a dictionary because we have to validate this value as well because i might as well have entered text instead of this number so the way to access values inside a dictionary is again using the square brackets just like we did for list but instead of an index index 0 or index 1 we don't need to use an index because we actually have a key so instead of index we're going to use the key like days or unit and this will give us value of 40 which will then be converted into an integer and then validated right here so again i'm gonna copy this for comparison so to access an element of a list let's say my list here and let's do 20 30 doesn't matter in order to access the elements of that list we can do that using an index so this will basically print number 30. again if we had other elements here we can do index 2 so that will print out 100 and let's actually check that and remember to comment out a multi-line code we can use three quotes at the beginning and at the end let's refresh and there you go 100 was printed let's do the same with the dictionary so we have a dictionary days like this and to access an element inside that dictionary we use the square brackets and instead of the index like here on the list we use the key itself and again i'm going to print this and there you go you see value 20 and i can also access unit value and if i execute it i see hours and so on and i can add any number of key value pairs in my dictionary as i want so for example i can add a message here all good and i can access that message like this so this is how you create a dictionary and that's how you access the values in a list or in a dictionary so let's clean this up comment in our code again like this and get back to our code so basically again this is how we access the number of days in our dictionary and in our days to units function we need both of those values and we can pass in those values separately so we already have a user input number from the dictionary and the second value would be the unit from the dictionary however we have to add that second parameter to our function so let's do that unit let's call it conversion unit so now we are passing both of these values to our function so we have the user input number here which is already converted into an integer and the second one which is the conversion unit and now we can finish the logic in this function here right now we are using this hard-coded calculation to units which is 24 and number of units which is hard-coded hours so first of all i'm going to remove those two we don't need them anymore because we get those values from the user and now we have to substitute those two so here we're going to check if the conversion unit is hours equals hours then we're going to return calculation with hours it's going to be 24 and conversion unit here we can also hard code hours here because we know it's hours else if so if the conversion unit is minutes then we can return calculation for minutes and a text like this and it could also be that the conversion unit that was provided is none of those or maybe some invalid text and in this case we're gonna add final validation so if it's neither hours nor minutes then we're just gonna say you know what unsupported unit so basically we don't do conversions other than those two so we just tell the user that's an unsupported unit and now if we execute our program and enter value there you go we have our output and same way if we do 90 days to hours we have our calculation in hours so that's how dictionaries work we can do one final thing here and as usual print out the type of our dictionary and there you go you see class of type dictionary here which is another data type and at this point we can actually summarize all the data types that we have learned so far so let's give us some space here so first one was string so basically a message or some output like this that's our string then we have integers example of that we saw already days then we have float numbers which for example can be used for price or weight of a product etc we also have learned boolean data types when we check some kind of condition whether if it's true or not so for example is it a valid number or is the user input exit we also learned a data type of lists so for example usage list of days like this including duplicate values and list data type can be used for strings as well not only for numbers so for example we can have list of month like january doesn't really matter we also learned a data type called set and set basically is very similar to list but it doesn't allow duplicate values and its syntax is with curly braces so the same way we can have numbers or strings or booleans or any other data types inside a set and the last one that we learned was a dictionary and let's take our own example day in unit and dictionary is basically a collection of key value pairs and the syntax is written like this so why do we need all these different data types well depending on the use case or what exactly you are trying to achieve in the program you're gonna need a different data type to achieve exactly that so each data type has its own specific purpose and throughout these days to units calculator i try to demonstrate the purpose of each data type and when we need to use them so these are all the data types that we learned these are also most of the data types available in python and the main ones that you're going to be working with so this should give you a good foundational knowledge about data types in python and also note that most of these data types you will also encounter in any other programming languages because as i said these are the core data types and most of the programming languages actually support these data types as well till now we have been writing all our code in one single file so basically we have a project with just one python file and we're writing all the logic inside that file however you can imagine that if you're writing a little bit more complex applications that have much more complex logic so they end up having lots of functions obviously you're gonna end up with a python file that has hundreds of lines of code maybe thousands of lines of code and it's not very practical to manage all this logic and all this code in just one file so instead you need to structure your code and divide your logic between multiple different python files and you do that with modules to give an example imagine a web application that has basically many features like facebook for example obviously you can put all the facebook logic in one single python file right you will have a structure of facebook project with subfolders in your project which all contain multiple python files so you would have maybe packages for each feature and each such feature has a lot of functionalities so they will be all grouped then again in multiple python files right so you would end up with a project with a hierarchy of folders and lots of lots of python files inside now the question is if i have multiple python files in my project how do i connect them together so for example if i have another file here and let's call it helper dot py and if i have some logic here basically functions defined here how do i use them or how do i reference them in another file and the way it works in python is using the concept called modules so a module is basically a python file that contains functions or variables that you can use in another python file so basically any python file that you have in your project both of these in this case are modules and you can reference one module from another so basically the idea is that you can structure your application your program using modules so you can make your project modular so in this part we're going to create our own module in helper file and we're going to reference that in main dot python and let's say in main.python i only want the code that basically starts the program right all the functions all the logic i kind of want it hidden away and grouped together in a separate file so what we're going to do is copy this entire thing so both of these functions basically and paste it in here so now you already see a couple of red lines here because now the reference is obviously broken right so main.python obviously doesn't know anything about validate and execute function right it says unresolved reference and the same way face to unit dictionary is unresolved for helper because it doesn't know anything about main.python and the variables defined right here so the first thing we need to do is we need to tell main note python file where to find this validate and execute function right we have defined it in helper python so that's what we need to define how we do that is using something called an import statement so we basically import this whole helper.pi module inside main.python and we do that very simply using import helper right helper is the name of the file and therefore name of the module and right now it's grey because we're not using it and the way to use it is helper dot and the function name and you see the red line disappeared so again what we did is we told main.python there is a helper module in this project and please import this whole module and all the functions that this module has and make it available in main.pi and then once we have that helper module we can reference any functions if we had any variables using module name dot function so this has to be the name and if i hover over it you see that pycharm actually displays the whole path to that helper.pi file and now note that here we still have redline because this is a variable which is defined in main dot pi but a helper module doesn't know anything about it and that's an important distinction here because we imported helper module inside main so helper modules functions are available here but not the other way so in order to make this dictionary available invalidate and execute function we would need to pass it in as a variable like this and this will work now now note that in main.python we actually only need this validate and execute function we don't need the days to units function because this function is only used by validate and execute function so that means that we don't need to import the whole entire module inside main.pi we actually only need the validated execute function and this is a small example but if you had a module with 20 30 functions and you just needed one of them you wouldn't need to import the whole module just for that one function so what you could do is you can pick and choose which specific functions you want to make available inside another file or another module and you can do that very easily using the following syntax we say from helper that's the module again import and the name of the function like this and now because we are accessing and importing that specific function we don't need to use module name anymore we have that function available directly so we can delete the module reference here and we have that function available like this so again instead of having entire helper module with all of its functions and other stuff we only have one specific function available here so that means we wouldn't be able to use these two units for example now we saw an example of a module that has just functions right however module can have many other stuff for example variables and you can make those variables also available for another program and that's an example here i'm going to create a variable called user input message and this is our user input message and we can actually put it into a variable instead of having it defined like that so i'm going to copy it and paste it here so now we have the user input in a variable in another file again how do we access it in main dot pi in this statement at the top we can actually import several things from a module right now we are importing a function we can also import our variable and we can do that also very easily like this and we can use that user input message here so basically if you have again 20 functions and variables from a module and you just need maybe five of them you can import them individually like this just basically just list the names of functions and variables what you can also do is import all this is what this asterisk sign represents so we're basically importing everything from the helper module again this is kind of a waste if you just need a couple of functions and variables from that module and not the entire thing however with this syntax you can import everything and now you may be wondering what is a difference between importing everything from the module and importing the whole module because in both cases you have everything in the module available the difference is in the syntax because note that if we use this from import statement you don't need to use name of the module every time you're accessing something defined in that module right so we don't need to do helper dot function name or helper dot variable name whereas if we imported the module like this you see we have red lines because we would need to do helper dot or module name dot whenever we use something from the module now this could be a matter of taste so basically which one you want to use i personally think that this is the most efficient and also cleaner way of using specific functions variables etc defined in a module and as a side note the things that are defined in a module that you can use basically in another file are called definitions so all these those three elements here are definitions of a module and you can use any of those definitions in a file where you import that module what you can also do when importing a module is you can rename that module basically to whatever you want so if the name of the module is too long or you just want to use a different name you can do as and then basically write a name whatever you want like h for example and then obviously you need to use that new name everywhere you access functions or variables of that module so basically how you import a module and whether you rename it is a matter of taste basically how you want to do that in your code however i think that importing specific elements from a module like this is most efficient and also cleanest way to do that so we saw how to create our own module and use it in another file so basically if you had hundred python files here with each one of them having their own functions and variables you can cross reference them from each other using the import statement very easily now in addition to creating your own modules we can actually use modules that python itself already includes so just like we used some functions that python gives us out of the box like inputs or ins so basically all of those the same way python also gives us modules for different scenarios so that we don't have to write this logic ourselves and there are a lot of useful modules that python actually includes for example math module for mathematical operations its own module for working with dates or date times module for working with specific files and so on so basically for many different use cases python already gives you ready modules and these modules will then contain multiple functions and variables for that specific use case again if we take an example of a date so if you have an application that works with dates and times you have this module from a python that gives you a bunch of useful functions for working with dates and again this means you don't have to write this logic yourself but rather use the existing one so to give you some examples of modules that python provides us with for example if you want to work with operating system there is an os module so we can print out we can get the name of the os for example and i'm going to comment all this out and run and you see the name of the os another useful module python offers is logging so if you want to use logging in your application the logging module gives you different functions to log an error message or warning etc as well as basically just configure how the login will look like again to see an example again it may look a little bit complex here but essentially just using the name of the module and then functions that it gives you obviously you need to know the functions and you need to know how to use them and you can get that information from documentation or if you're using pycharm it actually gives you all the information about these functions and the module if you just hover on it so you don't even need to google or check out the documentation and basically we're creating a logger and we're logging an error message so if i execute this now and here you see the output of the logger error happened in the app and logging in application is actually a very important feature so that's why logging module is available by default in python any use case in our application would be right here if an error happened for example instead of using a print basically just log and error so these were just some basic examples but as i said python comes with lots of very useful modules that you can use in your applications for different use cases and that of course makes your work easier because you can just use the logic instead of having to write it yourself in the modules that are part of python so that you can import them like this directly are called built-in modules we learned about built-in functions so these are the functions that python makes available to us directly and there are built-in modules that we get from python and by the way if you're wondering where this module actually comes from so where is the physical location on our machine or where this module files actually leave if you're using pycharm you can actually hover over it and hold down a control key or if you're a mac command key and click inside you see the file opens up which is the module the logging module since i said module is basically just a python file that has multiple functions variables etc available and you see right here on this top line the whole path for that python file so that's the location of that module file locally on my computer and inside this python file basically you again have python code and if we look for get logger function here like this right here you will find the definition of that function right so somebody implemented this function and called it get logger and this is basically the logic all the things that get executed behind the scenes and you as a user of python basically can use the name of the function and write your own programs with it and let's also look for error and there you go we have error function as well right here and obviously this is a more complex code here than what we write but just to give you an idea that this is actually a file on your machine that comes with the python version that you install locally and all the functions are defined already there by python developers and you can just use them like this without worrying about how the logic is actually implemented and another interesting thing is if i click here in this python 3.9 like this logging is basically one module that we used and here you will see the list of the complete list of all the built-in modules that you get from python so some of them are folders like logging for example email and bunch of other stuff and the operating system module for example that we used is a python file then you can find right here so these are the built-in modules so now that we know what built-in modules are and how to use them let's actually see a realistic example and also write a little more interesting application using built-in module so what i'm going to do is in my python project i'm going to create a new file and i'm going to call this time to deadline dot pi y and we're going to write an application that basically accepts a user input of a goal and a date like a deadline date and then we're going to print out back to the user how much time it is remaining till that deadline and that deadline will be a date right some date in the future so that means that we're going to be working with dates and in order to work with dates we are going to need python's built-in module that allows us to call functions and do some processing and stuff on date values and that module is called date time so import date time so let's have that import statement right there and we're going to use it in our program so the first thing we want to do is let the user give us an input about their goal and a date or deadline for that goal so let's do that first we already know how to ask for user input with a message so we're gonna say enter your goal with a deadline separated by colon and we also know this already basically what we want to do is user to be able to enter their goal like learn python column and a date and let's say again this is totally up to us to decide we're the ones basically setting the rules here so let's say we want the date to be entered in following format day month and a full year like this so that's the format that we're looking for and this is how user should input the value otherwise with a different format it's not going to work so we're going to save that input value into a variable let's call it user input so we have the user input let's do the split again on a colon remember we have this whole thing as a string as one string so we want to split it into two parts so we have the goal here and the deadline and this will give us a list with those two values and let's call it input list and now from that list we can actually extract or get those individual values using a list syntax so accessing the elements from a list which is what we learned already using an index right so we have input list index 0 which is going to give us learn python and let's save it as a goal variable and let's create another variable which is going to be date let's call it deadline and this will be the second value in our list now let's actually print this out print this whole list out and now note that we are actually in a different file we're not in the main.pi anymore and we don't want to execute main python file instead we want to execute this one right here we basically just want to ignore everything else in this project so how do i go from main to this file execution very easy in this editor just to right click and you see here run time till deadline and i'm going to execute this everything else is ignored in the project we are basically just working on this one right here so enter your goal with the deadline separated by colon let's do that i'm going to do pi learn python colon and then let's enter some date and enter and this is the output so we basically split that into two values and put it in a list right here and very important to note here that both of these elements in this list are strings because again user input is always interpreted as strings so this was a string and it was split into two strings which are learn python and this date here however we don't want date or deadline as a string we want deadline as a date and exactly for that functionality we need this date time module because we don't want to implement it ourselves we want python to give us a ready function from a module that will basically just let us convert this string into a date value and converting string to date is just one example usage of date time module but you can use this module to work with dates generally like creating new dates formatting the dates updating the value etc and we're gonna see some of those examples in this section now if you're learning this type of thing alone you don't have me basically teaching you and giving you an example the way you would do that is you would basically research and maybe google how this is done in python and you would basically see some examples of using this date time and what the name of this function is and how to use that as well as each module has its own documentation page where you can look up the syntax and usage examples because obviously you're not going to know how to do that by yourself so the way we convert a string into a date using this date time module is on this datetime module if i type in dot i will see a list of all the definitions that daytime module gives me right so i have date daytime time etc the one that we need is date time because the function that we need for conversion is inside that definition and don't be confused here because we have date time twice here the first one is a module and the second one is a definition in that module and we can actually check that as well so if i jump into this date time module and inside that if i look for date time again so right here you see this date time definition and on that daytime definition we have now functions that we can use and as you see this is not a function or variable it says class here which is another concept in programming we're gonna see later so just for now think about this as a container of functions and variables inside that module and that's why we have to access it like this and then on that daytime definition this class we now have functions that we can use to do the conversion so the function we need is called str so string p time and this function will take a string representation of a date and we'll convert it into a date format so our string representation is deadline and we need to pass in the second parameter because remember i said we basically decided that the date should be entered like this with points and a full year here and as you know there are lots of different formats and types for dates right depending on a country or a language or even within that language we basically have multiple different formats right we have formats like this or we could also have a shorter version of the ear etc so we need to define here the format that we want python or this method here to use when converting this string into a date again because there are so many different formats available so what we have here is a day then we have a dot then we have a month again a dot and then we have an ear so this is kind of a main structure of a format however formats for dates have their own specific syntax and this is actually for all programming languages so what we need to do is put this percentage sign in front of every letter so again we have day month year and there is one more thing about this format this format right here matches this date however we want the full date not just the last two and the format for that would be a capital y again this is something that you would look up in the documentation of the module instead of knowing it by heart but just know that this type of formats are used when working with dates and this is not specific to python you will actually see something very similar in all the other different programming languages and that will do exactly what we want it will take this date here and give us a proper date format from it let's actually test this so i'm going to print this first and i'm also going to print a type a data type of that value so let's execute i'm actually going to copy that so i don't have to retype it over and over again and enter and here you see we printed first of all the date not a string and now this looks like a proper date and not a string anymore note the change in the format plus we have these zeros here which represent hours minutes and seconds we didn't set any of those so these are all zero and we have the date time type again note date time for a module name and date time of the class name so this is the type of our converted date and the reason why we needed to convert that string into a date is because we want to calculate how many days or how many hours are remaining until the deadline from now starting from today and to do that calculation we need to have date type because we can't do that on a string great so now we have our deadline represented as a date so let's actually assign this to a variable it's a deadline date like this and remove this and now we need to do a calculation how many days from now till the deadline we already have the deadline we need now again now is or today is also a date and since we have this module date time we can also assume that this module will give us some function to tell us the date of today and if i do date time again daytime class and today you see that we have this function available and this will basically give us the date of today so depending obviously when you execute this program it will give you always the current date so let's print it today for me it's 7th of february so if i execute it now i'm going to enter this and today is 7th of february for me so if i execute this let's actually comment this out here i have the date for today which is 7th of february plus the exact time it is now but we don't care about the hours and minutes and seconds we just care about the day so that means we already have deadline and we have today let's also add it to a variable like this and now we can actually do the calculation calculation is super easy we just say deadline date minus today date and this will actually give us the difference between or time difference between today and the deadline date in the future again let's actually try this out and you see that so this is 12th of august for me today is 7th of february so the program calculated that there are 185 days until the time plus how many hours minutes seconds and milliseconds so that was a pretty easy calculation now what we want to do is tell the user some kind of message and we want the message to be dear user the time remaining until the deadline for your goal whatever the goal was is this so let's actually put that text and message together and bring it back to the user so first i'm going to save it into a variable let's call it time till and the message dear user time remaining for your goal and here we want to print what the goal actually is and you remember how we format a string when we want to use a variable we put an f here and in curly braces now we can use variables or numbers or some other expressions which are not string so first of all we're printing back the goal so time remaining for your goal whatever goal they entered is time till and let's execute our program and let's give it some other value like this and enter and here we have our message dear user time remaining for your goal learn python is 132 days and this many hours minutes etc now let's say we don't care about this whole thing here we just want to know how many days are remaining so how do we get rid of this part here again very easy and the time till if i do dot here i get a list of suggestions for functions or variables that i have available and one of them is days as you see here so if i click here and save and i'm going to reuse these inputs because i don't want to type and as you see we got rid of this whole thing here and we just have days and let's actually write days and there you go we have a cleaner output now for our user and finally what if the deadline was just a couple of days in the future and the user wanted to know how many hours are remaining till that deadline we can do that by getting rid of these days and again type in dot and we have something called total seconds here which gives us the time different in total seconds and now we can reverse engineer and calculate the number of hours from this total seconds which is pretty easy like this calculate minutes and then calculate hours and i'm gonna execute this again and here you see the number of hours with a decimal precision so we have 4450 hours point some fraction number here again we want to get rid of this extra stuff and as you see from the syntax it is actually a float number with a dot here so what we could do is basically convert this whole thing this float number into an integer so convert this whole thing into an integer using int function and we need this whole calculation here inside the int function and let's execute it again learn python and let's choose a date which is near and there you go we have a whole number 57 again cleaner output and as a code optimization we could take this whole thing out into a variable and call it hours till to make our code a little bit cleaner so now we have the whole program that takes user input and basically prints back to them how many hours are remaining till the deadline for their goal and for this program we used a date time module a built-in date time module from python and we also learned a new data type which is date time and as we learned from the module section we can actually optimize the import of the module because obviously we're just using the date time definition and nothing else from that module so what we could do is from date time module import date time definition which is a class in this case so now you don't need to use the module name so let's get rid of those and this could make the code cleaner as well as make the import more efficient because we're not importing the whole module even though we just need one definition from there when you install python it comes with a set of modules that are built in into python however there are many more modules for other different use cases like web development or data science and machine learning etc that are not part of that installation and these are basically external modules not built-in modules that you have to install as you need and this makes sense because the built-in modules are the ones that most of python programs will probably need and depending on what specifically you are doing with your program whether you are developing a web application or creating some machine learning program you can then add and install them as you need and there are hundreds of such external modules for python out there and obviously it wouldn't be practical to have them all on your laptop when you're just gonna use maybe a handful of them right now if i need to install an additional module for example django for web development where do i find these modules and how do i install them python modules actually live in a module repository where you can find them and that module repository is called pipey so if i look for pipey right here you see the python package index if i click inside you can search for any module that you want to add or install additionally for your application and here you see this word package everywhere instead of a module so what is a package or what is the difference between package and a module to give you a simple comparison module is basically a python file like this one right here that has a name of that file right so we have an ssl module or the one that we used earlier we have this os module right so module is one python file that contains all these functions and variables that you can use package is a collection of modules so if we scroll up and logging is actually one our example so these are actually packages and again let's go to logging so package basically contains multiple python files and use it in the same way you basically import the functions and definitions from the module and as you see clearly the difference is that package is more structured so for example the code is not available in just one single python file but it's divided like this in our case we have two python files and package always has this init python file and this file basically differentiates a normal folder like we have right here with bunch of python files from an actual package with python files so that's the difference between modules and packages and many external modules basically that we want to use in our projects are actually packages so basically package is a good way to define some hierarchy and structure for your modules and kind of group them together so that's why you see package here as a name instead of module so now that we know the difference let's actually search for a package for our project so for example if i type here django you see django packages that we can use from here we also have numpy and so on however in programming when working with and using these external packages a typical example would be where you need to implement some logic for example you want to write a program that basically talks to aws maybe and you don't know exactly the name of that package but you know what you're looking for so in this case instead of the exact name you would actually type in here in the search projects box a description of what you're looking for so let's say we're looking for an aws api package right something that will allow us to connect to the aws api and as you see here we get a list of different packages for that to choose from so basically pipey is a repository or storage for all of those modules and packages and people can also publish their own libraries like individual developers for example they can publish their own packages and modules there and make it available for other programmers to use and that's one reason why large community is so great for a programming language because then you can make use of all these modules and packages that others in the community have developed so for any kind of functionality you need in your application you could imagine that there is a module or package that helps you do exactly that now let's say i need a django package for my web application so first i find the package and now i can actually check some of the documentation for that specific package so if i click inside i see some description for the project as well as documentation and some other links now the question is how do i actually install this package locally so that i can use it in my project in python we install packages using a package manager tool called peep and right here on top below the package name you actually see a command peep install django so this is actually a command that will install that package locally on my laptop so we install packages in python using this pip command so what is this command or where does it come from pip is a package manager for python and if you know any other programming language basically the concept is the same every programming language has its own package manager tool for javascript it's npm for java it's maven or gradle and for python it is pip and one of the main tasks or usages of a package manager tool is to install external packages or libraries or also called dependencies for your project so if python needs this django package basically it's the job of the package manager to install that package and make available for python now where did this command come from do we have pip command available well in python version 3 actually pip is packaged inside the python so when we install the python we actually installed pip as well so basically we're going to copy this command peep install django and we're going to execute it actually from the terminal of pycharm so in pycharm editor we have this integrated terminal so this is actually pretty much the same terminal as you have outside like this so we are going to execute this pip install command directly in this pycharm terminal so i'm going to copy it here and do peep install django and you see it is downloading and successfully installed django 3.1.6 and we can actually see that package from our editor directly if i expand these external libraries and site packages and scroll a bit down here i have django 316 version installed so i see that the package is now available in my project and to also test it in the code i can do import and django and you see that pycharm actually recognize that i have django packaged locally so it gives me that package as a suggestion so that will basically import the whole django package so we can now use it in our application here you see pycharm actually noticed you are using or want to use django web framework as i mentioned in the beginning the pycharm professional edition has special support for different python web frameworks supporting you with syntax highlighting suggestions database integration and so on so again if you want to try it out for free for three months just use my code in the video description in the same way as we install the package we can also uninstall packages using pip and let's also actually demonstrate that pip uninstall django and let's confirm that successfully uninstalled django now if in the site packages i click inside you see that django packages are gone so this site packages folder got updated django was removed and you also see that in the code editor now i have this red line under django because pycharm cannot find a module called django and also if i do this obviously it won't give me any auto suggestion for that so that's how you can use peep to install packages and also to uninstall packages for your application now here i want to show you another cool alternative to installing and uninstalling packages directly in the pycharm ide and this is actually a feature in the latest pycharm release and for this i need to switch to an early access pycharm version so you see here it says pycharm eap early access to show you the feature however when you watch the tutorial it probably will already be included in the latest official pycharm version so you should have that already so instead of us basically finding this package in pipey and then executing peep install commands right here what you can do is down here you have another tab called python packages so if we click inside this is a place where you have everything that you need to know about the package the package search and all these features basically in one place so for example in order to install django we can just search for django right here and you see that when i type in the first couple of letters i already get suggestions so this is a list of all the packages that start with this name that are actually available in pipi so let's type in django and we see that right here and on the right you see the documentation of the package that you would actually see in pipey ui so you have that in place as well and in addition to that what they added is the documentation link so this is actually the package how-to guide and the full documentation with examples etc so very convenient way to basically get all the information about the package search the package and then to install the package we can choose the version here so either you can leave the latest or basically select the specific version and then once you have selected that you can just click install and here you see installing package and there you go so now in installed view here so we just had pip and setup tools these two things installed and right here we have now django in the installed packages list and again while you're now using the installed package or library in your code and basically you want to look up some examples or you want to check some documentation right you go back to the package click inside and you have link to documentation and the package information as well and also just by looking at this list you know which versions of which packages you have installed so i think it's really cool way to manage all your packages to install packages and for example if you want to uninstall packages from your project then again just clicking to that specific package right here you can just delete it and it will be removed from your application like this and no django anymore here so a very cool addition to pycharm because this will be very convenient to work with the packages so now i will switch back to my other editor again and continue from there so basically i have just cleaned up my python project we have this main.pi which is empty and we're gonna write an application that will read a spreadsheet file from our local file system it will read some information from that spreadsheet file and do something with that data and this could be a very useful use case if you're working with lots of files and you want to do some data processing in those files and you don't want to do that manually by automating or writing a program in python that basically can do anything in a file or across multiple files so let's see exactly what we are going to be doing with this program so first of all we're going to have an input file a spreadsheet file that we're going to be working with and that spreadsheet will look like this it's called inventory that's how it's going to look like so basically this file simulates something that many companies would have and employees of that company would be working with so we have this list of product numbers inventory for each product price for the product and a supplier so this could be an example file from a company that is basically dealing with selling or buying products so what we're going to do is we're going to read the information from this file and we're going to do something with that information first we're going to write logic that calculates how many products we have per supplier so we have three supplier companies here and per supplier we're gonna calculate number of products from that specific company and as a result we're gonna list the company so all three companies with their respective number of products then we're going to write another logic that lists inventory products that have inventory which is less than 10 so basically those ones the third exercise is going to be to list each company with their respective total inventory value and finally in the last exercise we will calculate the inventory value for each product so product count times price and we're going to write that value to an additional column in the spreadsheet and after that we're going to save that updated spreadsheet file programmatically using python so we're gonna see how to update a file and then save it programmatically in python if any of these sounds a little bit too complicated for you then just bear with me i'm gonna explain everything step by step and it's gonna become much clearer when we actually start writing the logic so the first thing what i'm gonna do is i'm gonna take this file that i have in the downloads folder and i'm actually gonna move that into my project so i'm gonna copy that and i'm gonna drop it in here so that we have the file right in our project and so we can basically just read it easily and okay and here is our inventory file so that's taken care of so obviously what we want to do now is read that file so basically let our python program read the contents of that file right here because we want to write some logic based on the values that are in this inventory file right we want to calculate stuff we want to list some stuff and so on so basically we want to work with these values and in order to do that we need to read all of these values into our program so that we have them available so how do we do that now there is actually a built-in module in python that allows you to work with files generally right it's not specific for a spreadsheet or for any other file type it's for different types of files generally however there is an external package that allows you to work with spreadsheets specifically and the obvious advantage is that that external module that allows you to work with spreadsheets or was created to work with spreadsheets specifically has much more functions and is much easier to use if you are working with spreadsheets compared to this built-in file module so that's one of the cases where you may have multiple ways of actually implementing some functionality for application and then you have to make this decision of maybe finding a better package or better module which is available externally that will make it easier for you to implement that functionality so that's what we're gonna do for our project we're going to choose the package that is actually made for working with spreadsheets we're going to use that one and it's called open pi xl and if i search for it this is the package that we're looking for and if i click inside basically we have some information about this package so here we see a description let's actually make this bigger description library to read write excel files with all these formats so this is the library that we're going to be using in the same way as we did before we're going to copy that command and execute it from pycharms integrated terminal so from here i'm gonna execute this pip command and install our module and as you see successfully installed open pi xl and this is the version of the module that we installed and in external libraries site packages you should be able to see our open pi excel package and now because the package is available locally i can do import and open pi excel again i get a suggestion from pycharm because the module is there and now we are able to use that package for our application and one small note here if you're wondering why there are some packages with basically a dot inside and some without these are the packages that we install python packages and we know that because there's this init python file inside and the ones without the dot they're just folders with bunch of files inside but they're not python packages so to say and inside some of those packages we see other packages as well with their own init file so basically hierarchy of multiple packages and at this point i'm going to say that the word library basically describes package that includes multiple other packages so basically which has a hierarchy of packages so we have module which is basically just one python file that we can use in our project then we have package which is a hierarchy of multiple modules with an init python file inside and then we have library which is basically multiple packages together in a hierarchy just like we see right here so note the difference so you don't get confused when you see library package and module especially if used interchangeably great so now we have our module in place so let's use it now to read our spreadsheet file and again you basically have to know the function names to do that if you don't you can do dot and get suggestions and load workbook is actually the function that we need in order to read our spreadsheet file so i'm going to choose this function and obviously we need to tell this function which file it should read so as a parameter we're going to say please load inventory file and we even get a suggestion because we have this inventory file here and there you go now this function will load the workbook and all its contents and later we obviously want to do something with that content so in order to be able to do that we need to save it first as a variable and let's actually call it inventory file like this and this will give us the file contents now let's actually open that file again and let's see exactly what we need so we can have multiple such sheets basically per file so we need to tell the program which one it should use exactly so for example if this was called a product list you would use that name but we're gonna go with sheet one so we're gonna use that name basically and we're gonna grab that using following syntax and you remember this syntax probably from using a dictionary so this will give us this one specific sheet and all the information inside and we can also save that into a variable so let's actually call that product list so now we have read the whole file then we have read this specific sheet of that file and now we have all this information in that variable so now we can read any of those values and do calculations etc using that variable so let's get to our first task and the first task is basically to calculate how many products we have per supplier and then list the names of the suppliers with that respective number of products so let's go back and do that so the results what we want to get is basically products or number of products per supplier right that's basically what we want to get as a result so we're going to create that variable and we're going to set it to an empty dictionary so by the end our program should give us something like this a dictionary where the name of the company is the key and the value is basically product count for that company and since we have three suppliers or three companies we're gonna end up with a dictionary with three key value pairs per company so we're starting with an empty dictionary for products per supplier and now let's see how we can programmatically calculate this first of all what we need is we need to go through each of these lines basically so we have these rows so we need to go through each and every row in order in sequence and we need to get or check a supplier name for that row and you remember whenever we execute a logic on multiple values over and over again so basically same logic or same function gets executed for multiple different values for a number of times that's basically a use case for a loop right so we're looping through those rows and we're doing the same thing per row which is getting the name of the supplier now as you also remember loops have conditions right we need to tell the loop how many times it should execute that specific logic and if we check our file here how many times we want to execute the logic the answer is as many times as the number of products so if we scroll down we have 74 products so we need to do that 74 times for each product so basically as many times as the number of product and obviously we want this program to work for any spreadsheet file right with any number of products so we need to calculate or we need to read that value also from the file so basically we need to read how many products are on this list so let's see how we can do that first of all we're using for loop to execute the logic for specific number of times and if you remember from for loop syntax four then we have the specific item so what is an item in our case our item is a row right product row is going to be an item of iteration so looping basically so we can call our item products row and then comes in and now we need that condition how many times and again this should be generic so we need to calculate or we need to read the number of lines here number of rows here from the file and the way we can read that is remember we have all the information we need in that one specific variable which is product list and that variable has the value for number of rows which is called max row now how do i know that max row attribute is available for product list i basically just looked it up on the documentation of the module and as i previously also mentioned whenever you're using a module this module will have a documentation so if you need some functionality if you need some values you can basically just search inside the documentation which functions and variables are available to get the functionality or values that you need and we can actually test it we can print it out like this and as you see here 75 so basically max row will always give us the number of lines or number of rows in that specific sheet so we know how many times to iterate now whenever we're providing a specific number in a for loop like 75 for example in this case we need to put that number in a range like this so we can't just say 70 or in this case max row now why do we need a range here and why is number just not enough because the for loop is for iterating over a list and the range will create a list of numbers to iterate through in our case range of 75 will create a list of numbers or sequence of numbers from 0 to 74. and with that we're going to have a valid for loop where for each item in this list of numbers some logic will be executed so that's why we need a range here and now there's one more thing that we need to fix we go back to our spreadsheet you see that the first row so this is going to be the first row basically does not actually include the product information it just includes the titles for each column right so we actually do not want to read this line we want to skip it and we want to start from the second row so instead of starting for loop from the first row we want to start it from the second row and in order to tell python to start at row 2 in the range function we're going to pass first parameter 2. so basically this will give us a new range of numbers starting from 2 instead of starting from 0. and since we're using those numbers in the range as an index for the rows the first number is going to be 2 so we're going to start iterating from the second row so this will be the start point and this will be the end point so basically from here to the end now there are two things specific to a range that you should be aware of first of all when we do not provide the start index basically the start number in the range by default it's actually zero so it's not one but it's zero and obviously we don't have a line zero here so if we executed it with the default one we would get an error so that's one thing to consider that range actually by default starts at 0 and not 1. but for us it doesn't matter because we're skipping the first one anyways and we're starting from line 2 because our spreadsheet doesn't have line 0 obviously another specific of the range is that the second value here basically the last number of execution in the loop in our case this is going to be 75 because we have 75 lines and we printed that out here that number is actually exclusive it's not inclusive that means that the range will execute from line 2 to 75 exclusive so basically till here this line will not be included in the loop again that's a specification of the range which we need to fix and basically want to tell you know what we want that iteration including the last number as well including that max row and a simple fix for that is going to be to do plus one here so basically whatever the max row is we want to include that one as well so we want to add one to it so now we have a range starting from two and ending in 76 exclusive so obviously the 76th line will not be red and this will now give us a range that we need and now that we have condition for the loop and we have set how many times it should execute let's actually execute the logic itself so what is the first information we need from that product row for each row we basically need the supplier name so that we can increment or start counting how many products this supplier has so we need this one two three fourth column and the value in that fourth column for each and every row right let's go back to editor how do we get a value in the cell you have to imagine when we iterate we have one row per iteration so right here we actually have this product row which is just one row and we have four columns here and we want value in this specific cell and we can get it using first of all product list because again we have all the information here so in this list we need a cell right we need a value of a cell so that's the name of the function and the cell basically takes two parameters again product list contains the whole sheet here so the whole list with all the rows and columns and we can get a specific cell value so whatever we are doesn't really matter from that variable by providing two values the row number and the column number that's it so row nine column one basically will be this specific value row five column four will be this specific value so for each iteration we're gonna do exactly that so for our case the supplier is always column four and the row is dynamic because we are iterating and therefore we're going to use whatever row we're at at the current iteration so that's the first parameter and column 4 as i said and this will give us the supplier name for each and every row so we can grab that value like this and this is going to be supplier name so we can save it in a variable as a next step we're going to start building how many products each supplier has we're going to start building a dictionary with the name of the supplier and then how many products they have so we're going to be building this dictionary here so this is a reminder now for what i said previously that you don't need knowledge of math in programming rather you need logical thinking right so you're going to get tasks like this where you have to basically logically decide how you are going to do a certain task and think all this through right so programming is more about logic rather than pure calculation or math which is actually more interesting and cooler and that's exactly what we're going to need in this example we're going to need some logical thinking to basically build this application so bear with me on this one the first thing we need to check here so think about this iteration on the first time and second time the first time the list or the dictionary is actually empty so we want to add the first supplier to the list right away right so we're gonna grab that and the syntax for adding a new entry in a dictionary is like this where we have a key and a value here so basically what we want to end up with is supplier name like a company and number of products for that supplier that's the dictionary that we want to end up with then it's going to be bbb company and so on so the very first supplier goes directly into the list and we're going to need the name of that supplier we don't know what it's going to be so name as a key and value is going to be 1 at the beginning right this is the very first iteration so after the first it iteration we're gonna have the first company name supplier name and count one for that on the second loop second iteration basically if the next line is a different supplier like here for example then a new entry will be added because we have a new key now with count one right so now we're gonna have company a with one product company b with one product now what happens when on the next iteration we get company a again right what we want to happen is company a the product count to increase to two right because now we found another product that is supplied by the same company however with this land we are actually overriding and setting it back to one so how do we make sure that number of products gets incremented instead of always setting it to one what we're going to do is before we execute this line we're going to check is it a new supplier or an existing one because that makes a difference so how do we know if it's a new one or an existing one we can look that name up in the dictionary if we have already edited that name in the dictionary then it's going to be there and we can do that very easily again with the syntax if supplier name in the dictionary it's actually very readable syntax that says is the supplier already added to the list or is it a new one so if this is true again remember if conditional so if this is true statement if the supplier name is already there then it is an existing one so we need to increment the product number instead of setting it to one so instead of assigning a new value we want to grab that value the existing one and basically add one to it right just increment by one so getting a value from a dictionary you probably remember the syntax already using the key and we can save it into a variable let's call it current number of products like this remember we're using a key in order to get the value and the key is the supplier name value is the product count so this will give us the current number of products on the first iteration this will be one or after the first iteration we're going to have one product for that specific supplier and if we already have that supplier in the dictionary we want to increment the number of products for that specific supplier by one because we just found another product from that supplier so we want this value to now be plus one right so current number of products plus one so that's the new value that we want for the existing for this current supplier and how do we assign it back to the dictionary again we grab that and assign it so as you see taking the value from the dictionary has the same syntax as setting that value in the dictionary in fact we could actually use a shortcut here so instead of current numproducts we could just take that value and add plus to it and then set it back as a new value and this will basically just give us a one-liner for the logic depends on the taste whether this looks nice or not or whether it's better to have a new variable and increment the value like this let's actually leave it like this so this logic will take care of adding a product count every time we find another product for the same supplier however now we have to decide what happens when it's a new supplier so if this condition is false if the supplier name is not on the list and this is going to be the case on the very first iteration as well because the dictionary is basically empty so there are no supplier names and for that case else we're going to do what we had before which is just setting that number to one right this is going to be the first value so this will do two things basically it will create a new entry in this dictionary for this new supplier and it will set the product count number to one for that new supplier and that's it that's actually our logic and let's actually try this out and print it out so right here i'm gonna print adding a new supplier and this is going to happen three times because we have three suppliers so we can safely print it because just gonna happen three times there is one more thing we need to fix here and that is getting the value from the cell so if we actually execute this now let's comment this out and actually print out what the supplier name here is let's actually get rid of this line we don't need this and run the program you see here we have cell information so it's not actually the the value the name of the supplier but it's just the object cell and the reason for that is because we're accessing a cell but we're not actually extracting or getting the value out of it and we can do that using this value attribute on that complete object again something that you can look up in the documentation of this open pi xl module so now let's run it again and now we have the actual values of that cell and this will take care of that small problem and now we are ready to execute our program and after the for loop actually let's print out the dictionary because we want to see the results of the dictionary that we are building here and i'm going to execute it now and there you go first of all we have three times adding a new supplier because we have three suppliers and this is the resulting dictionary and it looks actually very good first we have company a because this was the first on the list and we have number of products that were counted for that company then we have the second one with number of products and the third one so it looks like our program did exactly what we wanted so that takes care of the first exercise now i want to note here one small thing which is an alternative syntax for getting values from a dictionary which is actually a more recommended way of doing it which is instead of having these brackets here to have a get method right to use a get function with the key name so now you see the difference between those two so this is setting the value of that key again remember key is this one here and this is the value so we are accessing the value of the dictionary using that key and get function is actually another way of grabbing this value out of a dictionary using the key name again if i execute this i should see the same result after the first exercise basically we see from which company from which supplier we have the most product or the highest number of products now in the second exercise we're going to calculate the total inventory value per supplier meaning in our list for each product we have number of units for the product and the price so basically for each supplier we want to calculate how much is the total value of inventory of their products of all their products and again as a result we want to create a dictionary that basically tells us that value per company let's go ahead and do that so this was calculation calculation for number of products per supplier and here we're going to give us some space and do calculation total value per supplier or total value of inventory per supplier that's what we want to calculate so just like we did before we're going to create a new dictionary now and we're going to call it appropriately total value per supplier and start with an empty dictionary again for starting in an easy way let's consider the first very first iteration for the first supplier how are we going to calculate for the first supplier for the first product so i'm going to take this new dictionary and with our familiar syntax i'm gonna do supplier name so that's gonna be our key and the value of the very first iteration for the first product is going to be inventory times price so for the first product we're going to calculate how much or how many of the product items we have and number of price right that will give us value of the total inventory for that one specific product and that means we need those two values right now we're just getting the value of the supplier so we need inventory i'm actually going to copy this so we need cell for product row and column inventory is on column two and value so whatever the product row is going to be so for each product basically for each row we need always the second column so all these values here and then we need price which going to be product list cell and we are looking for the third column for every row number three and value so now we have inventory and price per product and the way to calculate value is basically inventory that's the number of product items times price very easy that will give us the first value for the first supplier now again the same way if we are getting a new supplier so basically in the next iteration if it's a new supplier then this line will get executed and everything is fine however if it's an existing supplier that we already have in the dictionary then we have to add to that previous value right instead of setting a new one so we're going to do very similar logic as we did here so we have the supplier name we're checking is it a new supplier or an old one an existing one this time in our new dictionary and if it is we're gonna grab that old or existing value again using this get syntax here existing value now is hundred we want to add to that hundred right so we're gonna grab that value this is going to be current total value and then we're going to add to that current total value the inventory value for the current product and assign it back to our dictionary now our variable names a little bit long so the code kind of looks a bit cluttered but it's easy to read and descriptive so you can also go for shorter variable names to have a cleaner code again matter of taste i usually name the variables something descriptive something that basically really differentiates or says what the value is about and that basically takes care of existing suppliers or the suppliers that we already have in the dictionary else we do this and this is complaining about too many blank lines so let's fix that and that's basically our logic again to go through it once for new suppliers again this is going to happen three times because we have three suppliers we are basically setting the total inventory price or inventory value for that specific product because remember we are iterating through products and then every time a new product iteration happens for the same supplier we basically just add an inventory price or total value for that specific product for what we already have in the dictionary for that specific supplier so now let's again print out our dictionary let's actually print both values so the first dictionary is this one right here supplier and number of products second one is going to be supplier and total value of all the products they have in the inventory with us execute and there you go we have both dictionaries printed here and here you see for each company we have the value in price basically right here the value for the total inventory of all the products they have on this list and we see that value per supplier and the values are decimal numbers with cent precision so 95 cents 47 cents because the prices are obviously like this as well and if you're wondering this comma here is actually a german format for number precision so this is going to be actually equivalent to english dot and we don't have to worry about this actually because python reads it and interprets it correctly so no problem with different language formats so that takes care of our second calculation now the third logic we're going to write is basically printing out all the products that have inventory less than 10 like this ones here for example so the logic for that is actually going to be pretty easy what we need as a result is again a dictionary let's call it products which have inventory under 10 like this and the value in the dictionary should be the product number which we have in the first column and the inventory count so let's get rid of these print statements and let's do calculation or we can do logic products with inventory less than 10. so basically as i said for each product which has inventory under 10 we want to print out or we want to save in a dictionary the product number and the inventory count so we have the inventory count already the value from the cell but we don't have the product number we're not accessing it so let's actually do that product number and we already know how this works this is going to be the first column right this one here and this gives us product number and now in our dictionary products under 10 we want to add value like this product number and it's going to be set to value of the inventory like this however we don't want it for every product we wanted only for products with inventory less than 10 and that's going to be an easy if conditional here if inventory again remember this logic all this logic happens for each product row right so all these get executed for each of these rows so basically we have inventory number on each iteration for each specific product and that's why we can just say if inventory is less than 10 so if the value on that specific row is less than 10 we want to add that product the current product that we are iterating on on the list and set the value of inventory as well again we have some complaining here because of the lines and this will be the logic basically and a use case for this type of calculation would be if we find products that have inventory which are less than 10 means we need to reorder them right because they're going to get out of stock soon and now at the end let's print the list of all the products that have inventory less than 10. so i'm going to execute it and here we have the product number and inventory count so we have three products basically which have inventory less than 10 and let's actually check that product number 25 right here which has inventory of seven and product number 30 inventory of six and the third one as well now you're probably wondering why we have this dot zero here because by default those values are interpreted as float numbers and not integers if we wanted to we can actually fix that using the integer function in both cases like this let's execute it again and we have the integers and not floats and finally as a last exercise what we're going to do is we're going to actually add some value inside that spreadsheet till now we have been reading the values and doing some calculations and just printing the result on the terminal as the last exercise we're actually going to create a new column here for every row and we're going to calculate basically the total price of inventory right the number of units times the price and we're going to basically set the value for each product inventory multiplied by price so how do we add a column or how do we make changes to a file it's actually pretty easy the same way as we were grabbing the value from this file and basically reading the values we can add values to it and add columns and rows etc so to add a fifth column we're gonna do product list cell product row this is going to be the number of the row and a column number so the same way we accessed the cells for all other values we can basically access the cell on column five which has an empty value right so we're not actually creating a new column we're just grabbing a value of a column five which happens to be empty and we are overriding that value and we could do the same actually for any other columns here right we can overwrite all these values if we wanted to so that's basically what we're doing so we're grabbing that cell and we can now save it into a variable let's actually call it inventory price so now that cell is saved into that variable so we can reference it and note here that i didn't add dot value at the end because if we want to update the cell we need the whole cell object right so that we can actually set a value on that so how do we set a value for that specific cell so again note that this is iteration so for each product row the value will be set so how do we set the cell value i'm just going to grab this variable here inventory price which is representing the cell in column five for each iteration and right here at the end because this is our last exercise let's write this is a logic or basically add value for total inventory price and the way we set the value in a cell is dot value equals and the calculation for that is actually very easy we already did it here inventory times the price for that specific product and that's it so we're using that dot value here in order to set the value instead of grabbing the value and that will actually update and add this calculation a result of the calculation on each line on this column but note that this will only change the value in a temporary file but it's not going to save anything right so what we want to do at the end if we want this change to persist to actually be saved we want to explicitly save the file so for example if i manually did something here added some value i would need to save it here as well right so that's exactly what we need to do here but programmatically using python so how do we save a file using python again this package or this module that we use here also gives us a function that we can use to save the file and since we're doing an operation on a file and not a sheet here we're going to grab that variable in file and here at the end we're going to call a save function on it and this will save the file changes however we're not overriding the same file the existing inventory file we're creating a new one and because of that we need to provide a name of the new file that is going to be created with the save function and we're going to call that file let's say inventory with total value and extension the same extension as we have here so this line will now save the changes and create a new file from the original one which has these values generated basically so now that we are done with the fourth exercise let's actually print out everything that we've done so far i'm gonna take all those values so this is going to be execution of all our logic printing these three dictionaries here and then updating the spreadsheet file and saving it into a new file so with this let's actually execute our program and see what happened first of all we have all those three dictionaries listed here or printed here with the values and if we pull this aside here you see a new file got generated with the name inventory with total value and if we open this file we should see that for each row a new column was generated and filled with values which is calculated for each product's inventory and price so that's basically our project that's how you work with files in python specifically with spreadsheet files and as i said at the beginning this could be actually very useful automation logic when you're working at a company where there are lots of excel files or spreadsheet files that employees have to work with and manually update stuff inside in this part we're going to learn the concept of objects and classes in python and generally object oriented let's consider an example of an application with lots of users for example linkedin right we have users and each user will have some information for example email address name of the user password maybe a current job title work experience a set of skills and so on right so each user will have all this information but obviously the actual values the actual information will be different for each user in addition to that users are able to do something with their own information right so for example a user can change their password change their current job title add a new skill and again basically do stuff with their own personal information and our program will be able to handle this user information plus user changing their information right so this will be user data and in order to do something so to perform some action we would have functions for that right so all of these will actually be functions however as i mentioned every user will have their own data and whenever a new user basically registers for an application all these data should be gathered for that new user and saved in the application so basically that means that in our program we need some way to define kind of a blueprint for a user for all the data user information and user behavior right things that the user can do in the application because if we have thousand users in our application obviously we can't write the same logic and same variables thousand times right we want to have a blueprint once and then we can use the blueprint for all those thousand users and that blueprint for a user is called a class and the specific implementation of that blueprint is called an object so think of this like a blueprint of a building right you can have a blueprint once which is like a general description of building with doors and windows and everything that every single building has and then the specific implementation of that blueprint will be the actual built ready buildings right so that's the same concept we're going to create a class blueprint for a user and the user class blueprint will define what information a user has and what actions user can perform in the application and object then will contain the actual information like this information right here for each specific user so let's clean all these up and let's now actually create class user so let's create a new file in our project and let's actually call it user dot pi so this is going to be our user class and inside that i'm going to paste in this piece of code that basically will represent data for the user and some behavior these pieces of data are also called attributes for that class so now let's turn all of these into a class so first of all we create a class using class special work and you also see the syntax highlighting and we're going to call that user and you know the syntax already for functions um etc with indentation so this is going to be where our class definition goes now note here the capital letter for u for user and lowercase user in the python file name so a standard convention is that we call classes with a capital letter and the file names are written in lowercase letters and now all of these should actually go inside that indentation right like this now we said that class is a blueprint for specific objects and blueprint cannot actually have specific values right so all these needs to be removed so in the blueprint we don't have any specific values we just have the attributes so basically we say this user will have an email name password and current job title the actual values of those attributes will be then set when we create an object from the blueprint however we need a function that will actually take those specific values and assign them to an object which is created from the blueprint and we're gonna create that function right here in the class and that function actually is called init with underscore or two underscores at the beginning and at the end a couple of notes here first of all you see the syntax highlighting just changed to this different color second note is that these functions that start with underscore in python are special functions so python basically gives them some special meaning so it knows exactly what this init function is and we also have to call it init we can't just call it whatever we want and final note is that this init function in python is something called a constructor again we have a blueprint and we're constructing objects from the blueprint and this init function the constructive function will help us construct objects from that user class and now for a function syntax you know that we use these brackets right and when i start writing the bracket you see that self got automatically created as the parameter here and the reason is as i mentioned python knows what the init function is and that it's a constructor so it knows that it needs this self as a parameter again self is also a special word in python it has a special meaning and it actually refers to this class here right this entire class if i hover over it you see self points to the user class and it will basically just help us access and reference all the attributes and functions within that class so it's for special usage within that user class and we're going to see examples of how to use that in a second now you can think of those attributes as variables within the class because they belong to the class right they describe basically what attributes or what characteristics this class has and in order to define that these variables belong to the class we need to use self here so and you see again syntax highlighting changed and the red line disappeared so basically we are defining that email name password and current job title belong to this class using this self keyword and now the last thing remaining in this constructor again remember constructor is to construct new objects so whenever a new object is created the specific values will be assigned to all these four variables right and those values those specific values will be passed into the constructor as parameters so email name password and current job title those four values must be provided to the constructor whenever we're creating a new object note that these names here could be different from this these are not the same these are just parameters and using these parameter values we are going to basically set the variables or attributes of the class like this and the warning is gone as well again as i mentioned this could be something else we could call it user email so it doesn't actually have to be the same it's just for convenience so this function will construct an object with parameters that we provide when we actually create it and we're going to see how to create an object later this is just the definition as remember when you define a function nothing actually happens until you call or you use that function so right here we're just defining that function so that we can use it later so constructor logic is done now we have these two functions that any user in our application can do right any user can change their password or change their job title and logically when change password happens by user basically user provides a new password in order to override the old one so the flow will be following the object will be created for the specific user so the initial data for that object will be provided so we'll have the user email name password and current job title and later at some point user may decide to change their password and their current job title and that means the initial data will be changed and that means that we're actually changing the attributes self.password with a new password right so logic will be like this now we have red lines here why because we need this self as a parameter first of all we also have that as um note right here so just like here we need to actually pass that self as a parameter that's just how it is because we need to access attributes of that class in that function and new password will be new password variable will be the new password basically that user wants to set so this will change the initial password with the new one and the same way in change.title we have self as a first parameter we always need it in all the functions because otherwise we cannot access the attributes in the class and new job title so now let's review this class that we created first of all we have this class keyword here with a capital letter for user you already know the syntax with colon and then indentation so this all of these inside this indentation is class body so part of the class logic first we have this init constructor that sets the initial values of that class attributes whenever we create a specific object for that class right so this happens only when we construct a new object and then on that constructed object we can change password we can change job title and note that right now we have nothing basically hard coded we have no specific values here right everything is just parameterized and also this is just a definition for class and its functions nothing will actually happen when we execute this because we're not creating any user objects so if i right click here and execute this run user you see that nothing happened because the class definition was created but we're not doing anything with that class definition we're not creating new users so now let's actually go ahead and do that in the same file in user.py i would actually use that class blueprint to construct a new user object and creating an object from a class is actually very simple we just write name of the class and parentheses this is actually the same syntax as calling the function with parameters and the parameters that we need to give that class are these four values here and note that calling this user class function basically in the background we'll call init function right so the constructor will be called whenever we write this syntax with class name and parentheses and this init constructor as you see expects four parameters so we have to provide all those parameters right here in the parenthesis so let's actually provide them let's do user email like this my username and password and a current job title again very similar to calling a function and this will actually construct a new object from the user class which has these four attributes with values that we just provided here and has these two functions that we can use to either change a password or change job title one note here is that functions that belong to a class are actually called methods so there's a special name for functions which are part of a class so we can refer to them as methods so user object basically gives us two methods that we can use now again if we execute this program now we will not see anything even though user will be created because we're not printing anything to the console we're not doing anything so let's do that run user nothing happens because we need to print a message or some kind of information so what i'm going to do now is inside that class i'm going to create a function or a method as we learned now that basically prints back some user information right prints me the name email and current job title because we don't want to display password so let's give us some space here and let's create a function called get user info so that we can see something and again as soon as i typed in these first parentheses self got generated because we need self in every function within the class and in the function body we're gonna print user information so let's say user and name of the user which we can access using self remember all the attributes that the object has can be accessed with this special word here so self dot name so user whatever the user's name is currently works as a and now we need the job description or job title and you can contact them at and now we can use the email so this message will be printed for the user again because this will be used for any user of our application the message these parts will be the same but each user will have their own different name different job title and different email address so all of these are and written as variables because we don't know these values up front so now we can actually use this function or method to print out information of a specific user so how do we use or how do we call that function of the user we first save it into a variable app user one or nana doesn't really matter let's call it app user one so it's generic so how do we use that function to print out the information about the user if i do app user one and dot you see that i have the attributes here the four attributes that our user object has or user class and we have these three methods of the class get user infor change job title and change password so using get info we can call this function and if i execute this right here you see user nanogenasia currently works as a devops engineer you can contact them at this email and now let's say we want to change job title of that user we do that in the same way app user one dot change job title and we're gonna provide the job title parameter note that even though you see two parameters here on that function this first parameter is passed in automatically so we don't have to basically pass that self right it's already done automatically we just have to worry about these parameters so new job title is let's say devops trainer and now we can call that user info method again and execute and you see first message printed here before we change the job title now it's devops trainer so that's how we can create a new object from a class and we can use methods of that object by first saving that object into a variable and then calling that function on that variable and obviously now this is just one user but if we had another one user two that's actually clean this up we can create a new user with different values like this they have their own email own name own password and on job title and again we can create that user and basically just use the methods defined in the class and in two different cases or for two different users obviously the user info will be different so let's execute this and right here you see the user information for both users and obviously if you have an application like linkedin you don't have two users or a thousand users you probably have millions of users and having a user definition once and reusing it every time a new user gets created or basically existing users do something in application is only possible when using classes and objects instead of just having all these logic non-structured throughout the code and again in this type of applications you would not have four attributes and three methods you will have probably hundreds of attributes and hundreds of such methods now as you see here we have created this user.pi file for user class but we're also creating these objects in the same file which actually doesn't make sense because the file should be only for defining the class right so all of this logic actually needs to move out from here and this should be just only for class definition so in the application again using an example of linkedin we would have class user and then we would have a class post right whenever someone posts something each post will have their own attributes like the actual message or text in that post the author who wrote the post how many likes it has etc as well as specific functions right for example changing the post commenting on the post etc so you will have separate file for each such class right in the application and then you would have one file somewhere else this could be a main dot pi where you would actually use all those different classes and create objects from those classes and that means that logic that we wrote here constructing a user object and then calling some functions on that will actually happen in another file in our case let's use main.pi and now let's see how we're going to do that i'm going to paste in the code that i copied from there and as soon as i paste in you see those red lines so basically main.pi says that it cannot find a reference to user so this file doesn't know anything about the user class so how do we fix that or how do we make this user class available in another file and if you remember when we needed to use modules or basically functions variables whatever from other files in python we use import to basically import those functions and make them available here or import the whole module to make its functions and variables available here and the same way we can import classes from another file import user and note here that i'm using the name of the file user lowercase and not the class name with capital user right so we are importing that file so now we can use anything that is defined in that file and one of them is class called user and the way we can access that class now is using the name of module and not the class user itself and this is basically exactly the same concept as we saw before because we are importing a module called user remember every python file that has functions or variables or classes inside is a module so we are importing a module and once we have that module imported now we can use and access functions variables or classes of that module and we can do that by copying the name of the module dot and there you go in the same way here so exactly the same concept whether this is a function or a class doesn't matter we use it the same way remember when we use the date time module with the syntax datetime.daytime and that was an example of using a class from a module now user.user may look a little bit weird so we can use our familiar import statement where instead of importing the whole module we can pick and choose and import specific definitions or specific elements of that module in our case we just have one element which is the class so we're going to say from user module import user class and now we don't need the module name anymore because we imported the class itself and now if we execute main dot p y let's do it again here we have the same result for two of our users and again as i said in applications you will have multiple classes that are connected to each other so for example if we have a post here post blueprint basically whenever a new post gets created with some specific values like the actual message and creation date time etc it will also have an author and that author will actually be one of those user objects right so you will have some functions here that actually reference the post and we can also see that in action let's actually create a post class let's give it just two attributes to keep it simple so we need a constructor here in it and we're gonna pass in message and author right this is gonna be the user who wrote it and you know the drill already and let's create another function which displays a post with its respective author so let's do get post info again we have self here and let's print post written by and the author name so this is going to be our simple post class and user is able to create new posts so we have the blueprint for a post and the same way we can create that post inside the main dot pi so first let's import the post class and then here we're going to create a new post in pycharm you get this nice display of parameters that function or class basically expects so we have message and author let's do some message or post and as a second parameter we have the author and we can use the name of a user so let's do app user to dot name and this will create an object also called instance in programming special instance of that post class and then we can print the information of that post first assign it to a variable let's say it's a new post and on a new post we're gonna call get post info let's actually run this application now and see the result and here we have our post message that gets printed by this get post info method in the post class and at the beginning i mentioned object oriented programming which basically means that when you're writing your code with objects and classes like this to create blueprints and then use those blueprints for specific instances that's called object-oriented programming and also an interesting note here is that in python almost everything is actually an object for example the data types like string integer list set etc when we printed them out we saw class of string or class of list so these data types are also classes in python and this int or sdr for string functions we called where actually the constructor we called to create a string or integer object and the constructor of int for example took a string representation of a number and in its init function and constructed an integer out of it now for us in terms of using these data types and variables etc it doesn't actually matter but it's just an interesting thing to know about how this whole thing works in python in this part we're going to learn how to use python to talk to external applications in our case we're going to use gitlab and then application and just note here that communication between two applications in our case our python application and a gitlab application usually happens using a common protocol like http so basically our python application will send an http request to gitlab application and from the gitlab application it will get an http response for that request so just know that http is just a protocol that these two applications can communicate with over the internet i have a couple of projects on gitlab so we're gonna write a very simple python application that basically goes to gitlab and asks for the list of projects for my user and then just prints it here in our run window and the concept of one application talking to another is basically done using something called api requests so the remote application in this case gitlab has an api so these are functions basically that gitlab makes available for other applications to call and we're going to call those functions from our python application in order to get the list of projects for my user and that communication or python basically asking for this information is going to be an api request or api call and what we get in response from this gitlab api is going to be api response so let's go ahead and do that in python in order to make those external requests to remote applications we're going to need a module called requests this is a generic module that you can use for any external application and as i mentioned at the beginning the communication happens using http protocol between two applications and as you see here also in the module description it says an http library which allows you to send http requests and then receive http responses from another application and that's exactly what we're going to be doing and this request module is not part of python so we're going to install it using pip again remember that we're using pip that actually comes packaged with the pycharm so in the terminal window of my ide of pycharm i'm going to execute peep install requests successfully installed request this is the version of that module and if i expand this external library's site packages i'm going to see the requests package in the list great so once we have the module available locally we can import that module in our file and this requests module now has or package actually has functions variables and objects that we can use to talk to these remote applications like gitlab as i said gitlab is just one example you can talk to any remote application that has an api and we can not only do requests for getting the existing information but we can also make requests to change something in that remote application so for example i can write a python application that will actually create a new project on my gitlab account but to keep the demo simple we're just going to be fetching information from gitlab so how do we make requests to an api of an application with this requests module we get a function called get and get takes one main parameter which is url so basically we need to tell python where to find that remote application or again in our case where to find gitlab to talk to it so we need the url of gitlab and where do you find this url information of a remote application you can basically google that every application has their own documentation where you can see the urls so for example for gitlab i actually googled it so if i do gitlab api documentation like this let's make it bigger so you have basically description for the api and what type of information you can get from gitlab and also what kind of things you can create in gitlab and you see an example api here that basically just lists all the projects however we want to list projects of one specific user in this case i want to list my own projects and in this documentation i found this list user project section where i have an example of how to use projects for a specific user so we have slash users and the user id so that's going to be my gitlab user id and projects so basically the url will now look like this first we're gonna have this base url so that's basically https gitlab.com api v4 and all the things that we want to do is going to be at this base api and after that we can basically depending on what exactly we want to do we can choose one specific action and again going back this is the action we want to execute on this base url so from gitlab we want projects that belong to user and right here i'm going to add my own gitlab user id so if you have a github account you should add your own user id here and that's going to be an api url for user projects and this will actually give us some kind of response so requests.get will make that request to gitlab and it will return some kind of response and we can save that response into a variable so let's call this variable response because that's what it is and on the next line let's actually print and see what's in that response let's run our application and right here you see we have an object response with a code here 200 is a code for a successful request but we're not seeing the actual contents right we want to see the projects with their details and in order to get that we're gonna do dot and text and let's actually execute this now and now you see these square brackets which means it is a list because we have a list of projects and this whole bunch of information about projects there and you also see curly braces so this is a list of dictionaries and each dictionary holds information of one specific project now if i actually print the type of that response text like this see that it is a string so even though we see the format is actually a list of dictionaries we're getting a string because we're accessing text attribute now there is actually another attribute which will give us the same thing but as an actual list of dictionaries instead of string and this will be actually a better way to get that information because then we can work on it so we can look through it and get the individual elements and access the values so what i'm gonna do now instead of dot text i'm gonna use json json is a standard format that all programming languages understand and usually when two applications communicate with each other which are written in different languages and with different technologies with json they have a common format for communication and this requests module basically gives us this json function to read the json format that gitlab sent us so json function will read the json response from gitlab and turn it into one of the python data types so i'm going to execute this now and now you see that response.json actually gave us list data type instead of string so again for demonstration since this is a list i'm going to access the first element of that list and print that out and right here you see that is actually the first element just the first one from the list and that element is a dictionary as you see so all these just to give you an idea of what we're getting back from that gitlab api and what we can do with that response or how we can actually work with that response so i'm gonna clean all this up and we saw that response.json gives us exactly what we want and that is a list of my projects so let's call this variable my projects and now what i want to do with this is i want to print out in our window for each project project name and project url so basically the repository url for that project so i don't want to have any other values like id or description or whatever other stuff i have in this dictionary i just want name and project url for every single project and i want that information printed out in this window so how do we do that we have a list so we're gonna loop through that list to access each element inside that list so you remember we have for loop to loop through or iterate through a list and give us access to each element one at a time so here i'm going to write for loop and if you remember the syntax of for loop we have four and then a variable which will represent the single element in that list and in this case it's going to be a project a single project and then we have in list so for project in my projects do something so for every project element in this my projects list we want to print out the name and the url so right here i will do print and let's do our formatted string here and let's start writing our message let's do project name and the project name will be this one right here now how do we actually access the name of the project in each project element as you see here these project elements are actually dictionaries and how do we access a value in a dictionary using the name of the dictionary in this case our variable project so this whole thing is going to be saved into a variable called project and then on that variable on the dictionary we're going to pass in the name of the key which is name now i've been using double quotes throughout our project and for a syntax for dictionaries as well like this right however here we have a small problem which is we have double quotes inside the double quotes but as you remember for string representation we can actually use single quotes as well as double quotes and this is one of the really important use cases for why we can actually use both single and double quotes for a string and that is if we have a string inside another string like right here we can actually use that interchangeably so that we don't have this problem here so now as you see back to normal everything works because now python knows okay this is another string which we're using inside this string so we can use these interchangeably as we want so this could be single quotes and this could be double quotes wouldn't really matter but that's a really good use case why we need both types of quotes so we have the project name now we need a project url and let's see where the project url is in our dictionary this is one element one dictionary right so i'm gonna scroll it to the left and we have web url this is what we could use or we could also use http url to repo let's go with the web url so again this is a key inside the dictionary so we can access the value so this is actually the value we need and we can access that using the dictionary name projects and the key name which is web url and this is actually project not projects and this code will now print name and url for each project so let's actually execute our program and as you see we have project name and project url for each project we can make the output a little bit cleaner let's do a new line here and a new line here so we can separate the projects from each other and run again and there you go this looks a little bit cleaner for each project we have a name and a url so if i click inside i'll go directly to this project so that's basically a very simple example of how to use requests module to talk to external applications or external urls and then do something with the response that we get from that external application by first converting that into an actual data type that we're getting using json function so in our case it was a list and that's why json function actually converted it into a list if the response was a dictionary instead of at least then json would convert it into a dictionary and then we can do something with that response value congratulations you made it till the end i hope you learned a lot and got some valuable knowledge from this course if you like the course please leave a thumbs up on this video on my channel i actually cover lots of different devops topics like docker ci cd terraform and more so be sure to check out my tutorials if you're learning python to become a devops engineer i actually have a complete devops bootcamp with all the technologies you need to learn as a devops engineer which also includes a module for automation with python with several cool demo projects to automate some common devops tasks like application and server monitoring cloud automation tasks with aws jenkins etc so if you're interested check out more info in the description also happy to connect with you on social media so i would love to see you there with that said thank you for watching and see you in the next video