Python Full Course for Beginners [2024] | Python for Beginners - 12 Hours | Python Tutorial |Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone welcome to this video on python full course in this video I will teach you everything you need to know about python end to end but before we get started make sure to hit the like button and subscribe to our Channel you can also hit the Bell icon to receive regular updates from our Channel we also have a lot of training programs and certification courses on our website so if you are interested in them do check out the links given in the description now before we get started with this video I would like to first introduce the agenda for today we will start with python fundamentals what exactly is python how do you use it how do install it and start working with it and after that we can see some of the Core Concepts in Python like comment operators tle set dictionaries Etc after this we have loops in Python where we will discuss some of the commonly used looping structures like while loop for Loop Etc next we will be giving you an introduction to objectoriented programming using Python and this will be done through python functions after that we will be seeing arrays in Python along with some Advanced python Concepts like exception handling requests and rejects we will also teach you how to handle files using python this will include creating files deleting files modifying them saving them Etc you will also need to know how to interact with database Technologies right like nosql MySQL Etc this will be covered in Python databases 101 after which we will move on to python modules like numpy pandas matplot lib Etc these are very important libraries which you will need to know if you want to get ahead with AI ml data science Etc now I really do hope that this video lands you a drop so at the end of this video I have included a few interview questions and answers now this is pretty much it for the agenda of this video so let's go ahead and get started with the first topic which is python [Music] fundamentals python has been ranked the number one programming language in TB index and has been used by some of the largest companies in the tech industry like Pinterest Spotify YouTube Instagram Etc so you looking to Learn Python might just be the best decision you've ever made what is python python is an easy tolearn powerful programming language it has efficient high level data structures and a simple but effective approach to objectoriented programming Python's elegant syntax and dynamic typing together with its interpreted nature make it an ideal language for scripting and Rapid application development in many areas on most platforms and with that we can see why we use Python right the number one point will be the applications python is a very vertile programming language ranging from application development to web design game design uiux development data science machine learning automation Etc there's also the point of simplified syntax if you usually work with languages like Java C++ Etc you'll get me python is one of the simplest languages you can learn and this fact doesn't impact any of the features it is just as good as all the other options that you have and the only thing is it's a lot simpler the third point is wellb built packages now don't worry if you don't know what packages are I'll cover that later the only thing that you need to know right now is that packages have the ability to replace hundreds of lines of code with just a single line of code I'll show you how to do that later in this video and the fourth point that I want want to stress on is the highly supportive Community look I get it programming is not always easy and you get stuck sometimes but with Pythons Community you get a lot of support for the issues that you face and I'm telling you it's one of the most engaging and interactive communities that have ever come across now I think it's about time that we get started I do want you to know that this video will be a Hands-On session and I want you to keep your computer ready and follow along with this tutorial to started with python you will need two things the first thing is python itself which is an application that you install in your computer so that your computer knows how to interact and understand python the second thing you will need is a code editor or an IDE there are a lot of code editors that you can come across like Vim vs code sublim text Eclipse spyer pycharm Etc in this video we will be working with the Anaconda distribution now I have chosen this because it's the easiest one with which you can set up your python [Music] ecosystem to install Anaconda distribution all you need to do is open up your browser and then type Download Anaconda now once you are here just follow the first link if you not able to find this link then just follow the URL given above anaconda.com / products SL distribution the installation procedure doesn't really change for any operating system you can follow the same procedure for Windows Mac Linux Etc so once you're here just click download and save now once downloaded just click on the file and open it up now once you are here just follow the steps given by the application itself you don't really need to do any other settings just follow what's given in the application I recommend that you install Anaconda to the path provided by the software itself because this makes it easier for the files to interact with each other you can just follow along the steps and just make sure that this check boox is always checked register Anaconda 3 as my default python 3.9 what this does is that when you install any other code editors or IDs they will be able to recognize the python that is installed by anaconda in your system after all this you just just need to click install and then it will start installing Anaconda to your system now the installation might take some time but once this is done just finish this up and then open up the start menu and type Anaconda Navigator once you have it open it up and there you have it Anaconda navigator for Windows over here you can see the collection of idees and code editors that you get with and a cond navigator you also have your environments where you can manage your packages your educational videos and also a community to support your growth in this video we will be using jupyter notebook and to use that all you need to do is Click launch over here now you can think of this view as a file directory where you can create files navigate through files and even delete files so right now what I'm doing is going to the desktop folder and then I am clicking on new and Python 3 so what this does is that it creates a new file in my desktop folder to run my python code after that I just want you to do a simple program is just one line all right simply type print all in small letters and then Open brackets round brackets and then single or double Cotes and then type some message I'm typing hello world and then we can run it by clicking on this button over here or you can do shift plus enter and it will show you your message if you've gotten till here then congratulations because you just created your first python [Music] project in this section we will see what variables and data types are in Python variables are nothing but values that are temporarily stored in your computer's memory let me show you how to do this to begin with a variable you first type the variable's name like for example say h and then what you do is you assign it a value like 40 so in the statement what you're doing is you are assigning the value 40 to a variable name called H now if you run this again with the Run button or shift plus enter Then you can see in the left hand side that this St statement has been executed and now if you want to access the value of age anywhere in your code in the future you can just type age and it will give you the result now while you have declared a value for your variable you should also remember that this value can be updated in the future for example say now I say age equal to 50 and I execute the statement then if I come back to our second statement just age you can see that the execution gives me the result 50 this means that age has been updated so it's a good idea to always keep track of all the updated variables in a code let us continue by declaring a few more variables let's say temperature equal to 35.8 in Python you can declare floating Point variables which is nothing but numbers with a decimal point and also whole numbers you can also type something like name equal to Eda and you can see over here that I have surrounded it with codes the reason why I do this is because whenever you write string values or sequence of characters python requires you to surround it with codes either double or single this will help python recognize the entire sequence of characters as a single entity you also have some other kinds of data types like Boolean values now Boolean values are nothing but for example you can say is offline equal to false now one thing that you should know is that python is case sensitive notice what happens if I replace the capital F with a small see it is not being highlighted anymore this is because python doesn't recognize this as a keyword anymore it thinks of this as a name so always remember to be aware of the case sensitive keywords while coding in Python you might also notice that I have used an underscore while declaring cing the keyword this is just to make the code more readable notice what happens if I take off this underscore it's not that readable right so whenever you code in Python always make sure that you enhance readability for anyone else who might access your code now there are three types of values in Python you have numbers you have strings and you have Boolean now there are times where you will need to convert one type of data to another let me show you how so you can see from our previous declarations we have age equal to 50 which is a whole number we have temp equal to 35.8 which is a decimal point number you have a string which is a collection of characters and then you have a Boolean value over here if you want to check the data type of all these variables what you will need to do is say type Open brackets and then type the variable name if if you execute this statement then you will see that age is an integer data type you can do the same for temp and you can see that it returns float which is a floating Point variable this just means that there is a decimal point in that number you can also type name which returns string now string is represented as St or S Str in Python and then you have your final variable which is is offline which is a Boolean data type let's just get this out of the way now and try a few things what if I want to convert age into a floating Point variable doing this is very simple what you need to do is type name of the data type you want to convert it into and then you open brackets and then you type the variable name so now you have converted age into a floating point value you can see the decimal point over here this concept is is helpful in many situations for example if I want to say print age of XYZ is Plus Age and execute the statement you will see a type error can only concatenate stir not int to stir what this means is the data type string can be only concatenated or connected to another string python doesn't understand how to connect numbers with strings solving this is super easy all you need to do is go over here and then say Stir of AG this will convert age into string and will allow python to connect these two strings now if you execute the statement you can see that it is successfully executed now with this I have an exercise for you I want you to collect the birth year of a particular individual and using that information I want want you to find out his age you can pause the video over here take a few minutes and then come back and see what the solution [Music] is now you must have gotten stuck while receiving input right doing this is super easy in Python what you need to do is type input and then Open brackets and inside those brackets you can give any message to your user for example enter your birth year now obviously you will need to store this in a variable right so let's just save it under birth year now if we run the statement python will ask you to enter the input for your birth year variable now I was born in 1998 so let me just type that and then I'll press enter after this we will need to subtract the current year with the birth year right now the current year is 2023 minus the birth year now let's just execute this and oops we've got a type error unsupported operand types for INT and stir now let's try to break down this error let's check the data type of birth year type birth year you can see over here that birth year has been declared as a string right that's what's causing the problem you're trying to subtract integer with strings now we can currently understand that birth year is a number but python is isn't able to do that so what you need to do is tell python that this is an integer and then if you execute this and type in the same values again you will be able to get your answer now let's try to make this a bit more readable first thing we need to do is assign this to a variable so let's just say AG equal to this expression and we'll execute this again and after that we can say print age of XYZ plus h now if we execute the statement we again get an error this is again a type error and can concatenate strings with strings not in with strings right so again we will need to convert this into a string data type so python is able to correctly read the data so let's execute this and then you can see age of XYZ is [Music] 25 in this tutorial I will teach you how to do some cool operations using strings in Python to do this the first thing we will need to do is declare a string variable called title equal to python for beginners now after executing the statement I want you to remember a few things the the first thing is that when you declare a string in Python it is treated as an object you can think of objects like Real World objects think of a car a car is an object and this object has a few functionalities to it right it can go forward it can go backward it can go sideways it can honk it can turn on the lights Etc strings are also the same if you type title Dot and press your tab button you can see a list of functions these functions are similar to like print input Etc the only difference is that print input Etc are general purpose functions meaning that they don't belong to a particular object and these functions belong to the object called string now calling them functions isn't really the right terminology when a function belongs to a particular object you refer to them as methods so all these methods that are listed over here belong to the object string now let us just pick an option called is Lowa over here and if you execute this you can see that it returns false this means that our string is not completely in the lower case so you have another method to do that you can type title dot lower and if you execute this now you can see that all the characters all the individual characters have been converted to the smaller case letters now if you type do is lower continuing this then you can see that it returns true you also have other methods like title. uper which returns all of the characters in uppercase letters you also have other interesting methods for strings in Python like title do find which will help you find a particular character or a sequence of characters in your string like say if I say Y in codes then it will return the value one you see one over here is the index of the first occurrence of this character now the way indexing Works in Python is simple you start from zero and then you continue with the numbers 1 2 3 4 5 6 7 Etc so when I say find the first occurrence of Y what it means is that the index that is returned is number one now you can also find the index for sequence of characters like save for and you can see that it has returned seven over here if you go back over here you can see the F character starts at seven and that is where the sequence of characters start from there's also some other interesting methods that you can use over here for example you can try replace to replace one word for another like say for example four with four and then you can see that the string has changed right now python 4 beginners you should remember that these statements don't really make any changes to your original string so if you go ahead and type title again then the result of it would be your original string if you want to save your modifications then you will need to save your edits into a new variable now for the find expression that we talked about earlier there is another replacement for it for example you can type print for in title and this will show you the Boolean value of your result like this sequence of characters does not exist in your variable but if I change this to the small case and now if I change it into something that is is in the variable then obviously it will return true this kind of result is desirable in a lot of [Music] cases now in this tutorial we will be seeing the arithmetic operators in Python now these are the same arithmetic operators that you find in math for example you can add numbers subtract them multiply them and so on so let's just quickly do 10 + 3 and you can see that the result is 13 this operator over here is called the addition operator which adds two numbers you also have the subtraction which subtracts three from 10 you have the multiply which is an asterisk which multiplies 10 by 3 30 and then you have the division and technically there's two types of division one is a single slash and the other one is a double slash notice what happens when we did the single slash we got a floating point value right but if you try a double slash it will give you a whole number after that we have the modulus operator which gives you the remainder of 10 / 3 which is 1 so we also have the option to make powers of something for example if you want to say 10 ^ 3 then you can put in two asterisks and then if you execute it you will get the result of 10 ^ 3 now for all these operators that you saw we also have something called as augmented assignment let me show you what that means so up until now if you had to do something with arithmetic operators you would have done something like this right X = to 10 xal to x + 3 and then you would type print X now there's nothing wrong with you executing your code like this but you are taking a bit more memory when you do it like this because in this statement over here you're loading x two times right so a simpler way to do this would be to say x = to + = to 3 now this new statement is exactly the same as the old statement only difference is it's a lot simpler to write and it's it's [Music] faster all right now let me ask you a question suppose I set x = 10 + Y into 2 what is the answer unfortunately a lot of people get this wrong the answer to this is 20 the reason for this is because mathematics has something called as operator precedence where operators like multiplication and division has a higher priority than addition or subtraction so when you evaluate this statement you will first evaluate 5 into 2 which is 10 and then you will say 10 + 10 which is 20 let's execute to verify this now operator precedence in Python also works the same way as it does in math the only thing is that you can change this using brackets so for example if you want to execute 10 + 5 first you can just put this in Brackets and then 10 + 5 will be executed first after which it will be multiplied by two so let's just verify this again and you can see that it has been [Music] done now you've already learned what arithmetic operators are in Python up next we have another set of operators called as comparison operators let us see what it does suppose I write 3 greater than 2 if I execute this it will produce a Boolean result which means it will tell you whether three is greater than two or not this operator that we have over here is called the greater than operator and this belongs to a set of operators called as comparison operators you also have greater than or equal to you have less than or equal to you have equals now don't don't compare this with the assignment operator all right this thing is checking for equality it is checking if three is equal to two or not so if I execute this it will tell you it is false now one more operator that we have over here is the not equal to this will check if something is not equal to something now 3 is not equal to two and that is why this will return the Boolean value true so to summarize we have the greater than we have greater than or equal to we have less than we have less than or equal to we have equality and not equal these operators combined make up the comparison [Music] operators now that we are done with comparison operators we have another set of operators called as logical operators now to demonstrate this let me first declare a variable age equal to 25 now let's apply some logical operators to it we'll first say age greater than 10 and age less than 30 make note that the and word over here is The Logical operator so now we have age 25 must be greater than 10 and AG must be less than 30 so if you execute this you will get this as true the thing with and operator is that both the left side of the operator and the right side of the operator must be satisfied another thing we have is the r operator now for this operator either the left side of the operator or the right side of the operator or the entire equation must be true let us demonstrate this we can have H equal 5 and then if we say this and then if you come to the equation you have H greater than 10 which is false or you have h less than 30 now age is definitely less than 30 right so this equation must be true there we go the last thing is the not operator let's just put it like this if you use the not operator it will give you the inverse of the result that you were going to get so let's just say this AG equal to 5 and age is greater than 10 so this is false right but if you use the not operator over here the result will still be true now you can obviously inverse this and test the result again so to summarize this you first have the and operator where both sides must be true then you have the or operator where at least one side must be true and then you have the not operator which will give you the inverse of your [Music] result now let's look at if statements we use if statements whenever we want to make a decision in our program for example if we have something like say temperature based on the value of that temperature we can make certain actions let me show you let us first declare a variable called temperature equal to say 40 now based on this value I can take certain actions for this I will start with writing if which is a keyword and then you write temperature greater than 30 and then a colon and then you type enter you see Over Here If You observe what you have done you have first written an if statement and then used a conditional statement to check if the temperature is above 30 now a colon in Python is used to segment a block of code if you see a text cursor over here you can see that there are a few spaces behind it right this is what we call an indentation in Python if you indent a particular piece of code it will belong to the previous statement this is similar to using curly brackets in languages like C Java Etc so be very careful as to not put unnecessary indents in your code now for a condition let us say if temperature is greater than 30 we'll print say it's a hot day you can see that I have written this string in double quotes this is because I have a single code in the word its so if I put single codes over here also then python will think that you are just making a mistake while writing so the codes over here and let's continue as I told you before since you put a colon over here everything below that colon is treated as a block of code so as long as this indent remains this block of code will be executed only if this condition is true let's add another statement over here let's say print it's time for Coler after this we can come out and if we execute this statement you can see that it's hot day and it's time for Cola now know that you can add as many conditions as you want to now what if this value was less than 30 let's say 29 what would happen then nothing happened right this is because the condition was proved false and that is why these statements didn't get executed remember that you can add as many conditions as you want and all you have to do to do that is type LF and then you can come up with another statement say if temperature is less than 30 then print it's nice day you can also go ahead and write if temperature is less than 10 then print that it's cold day you can also add a statement saying print done over here you should remember that this statement doesn't belong to any condition so it will be executed no matter what happens so if you execute it now you can say it's a nice day and done great [Music] right now let me ask you a question what if I asked you to print the numbers 1 to 5 how would you go about by doing that obviously you say print one and then print two print three print four and then print five but I'm telling you that this approach is wrong the reason is because I asked you to just print from 1 to 5 but what if I asked you to print from 1 to 100 or 1 to 1,000 million billion perhaps would you go about the same way writing it down individually obviously not right that's a very timec consuming task so to handle that we have something called as loops and the first kind of loop that I want to talk about is the V Loop let me show you how you can start using it the first thing that you do is start with a variable and assign it the initial value of the sequence that you have for example I have given I equal to 1 after that I'll will type the V keyword and then I have my conditional statement while I is less than or equal to 5 and then colon after which I get a block over here the first thing is I have to print the number right so print I and then we will need to start increasing the value of I by one so let's say i+ equal to 1 this will start incrementing the value of I by one every iteration if you don't put the statement then it will print the initial number until your computer runs out of memory so now let's just run this and you can see that your output has been printed over here now you can also change this into say 1 to 100 or even 1,000 remember that if you want to increase readability of your numbers you can always separate it by an underscore so this is like one comma 0 0 so now we have the output 1 to 1,000 you can also print interesting patterns using this let me show you if you type the same thing and then say I multiplied by a string you can give it any value that you want and then if you execute this you can get patterns you can see that you've got a triangle over here what this is is a string operation basically you can multiply strings in Python so if you say I equal to string and then I has the value one then this will be printed once and if I equal to two it will be printed twice I equal to 3 then it will be printed Thrice and so [Music] on earlier in the video I introduced you to the data types in Python this included numbers like integers floating Point variables strings and Boolean now these data types are something that we call as basic data types in Python there are also some complex data types and one of them is called list a list is nothing but a group of objects let me show you how you can do this first we will start with the variable name let's just say we are doing a collection of names so our variable name is names and then equal to and then we have square brackets these square brackets represent a list and now inside this list you can write down a bunch of names so let's say Elon and then you have Sam Bob and Emma you can use either single codes or double codes it doesn't really matter now if you execute the statement and then you call out the variable you can get a list of all those objects the interesting thing happens from now while you call out the variable you can also call out individual variables let's say you want the name of the second index so you have 0o 1 and two right so second index and you can get Bob directly you can also give a negative value now what do you think happens if I give a netive 1 so this is zero and then a negative value would be the last element of your list so if you execute this you must get Emma and if you do a -2 then it will be the second last element now this is not it you can also go ahead and get a range of values say from 0 to 2 what this means is that you will start from zero and you will print all the elements until you reach two remember that if you execute this the last value like the ending parameter won't be printed so for example over here I have 0o 1 and this two won't be printed see there you go now this is what we mean by list operations you should also remember that a list doesn't really need to be just a single data type you can also include numbers like integers floating Point variables and even Boolean and if you execute that you will still get the same result earlier in this video I told you how to use string Methods right this was because strings was an object and objects have methods well the thing is list is also an object and list also has many methods associated with it the functionalities of this include adding an element removing an element modifying an element Etc let us see how to do this let me first declare a list of numbers see numbers equal to 1 2 3 4 5 Etc now we have a list called numbers let's see what methods this numbers list has so we'll type numbers Dot and tab and you can see over here append clear copy count extend index Etc so let us start with say append now upend is method that is used to add numbers to the end of the list so if I say upend six then my list would be 1 2 3 4 5 6 now if you type down numbers again and check its values you can see that you have six at the end see there you go we also have another method called as insert now the problem with upend is that you can only add numbers at the end right but with insert you can add it anywhere in the list to use this method you need to give two values first thing is the index second thing is the value of the element itself so say I want to add a value at index zero and the value would be zero then if I execute the statement and execute it you can see that zero has been added we have zero 1 2 3 4 5 and six now sometimes there will be situations where you would like to know whether a certain element exists in a given list or not so to do that you can use the in operator for say you can write one in numbers and then if you execute the statement you can see it gives you Boolean value of true you also have other methods like numbers do clear and this will clear the entire numbers list for you so if you want to know more about list methods or anything in general then you can head over to the description given below and I have pasted a Blog link over there this blog lists down all the keywords methods syntax errors Etc and it can give you an approach as to how you can learn python now I want to cover another looping method called as for loops you see sometimes in Python you will come across situations where you would like to iterate over a list of numbers individually this is where for Loops come into picture let me show you suppose we have a list of numbers 1 2 3 4 5 now what if you want to print them out you would obviously write print and then numbers right that will give you the entire result but now what if I asked you to print each of these out separately this is where you can use for Loops what you will do is start by writing the for keyword and then you will mention the iterator the iterator is just another variable name you can keep it anything I will keep it as items in and now the variable name numbers and then colum and after this you have a separate code block and over here you can see print items now if you execute this you will have 1 2 3 4 5 all in separate rowes you can also do this with v Loops but it will just be a bit longer let me show you you'll start with i equal to0 because that's your initial iterator after which you'll say while I is less than length of numbers and then cool now if you're wondering what L of numbers is l n is just a method that will give you the length of your list after this you can say print numbers and then give the index you will also need to increase the value of your iterator for every iteration you can get the same results but compare and see which one is more simpler this is why most pars use for Loops for iterations now in this section of the video we will see at a function called range so range is a function that will give you a range of values let me show you type range and then you pass a value to it now if you execute this you will see range 0 comma 5 which means that this range function contains the values 0 to 5 you won't get the actual values itself because this is the default representation of the range function but you can iterate through this function by using a loop for example say for number in range five print number and if you execute this you will get the number 0 to 4 note that the index starts from zero so you won't get the written value over here but the written value minus one now you can also give two values over here say 5 to 10 this will return a value from 5 to 10 10 excluded so it will be 5 to 9 there you go you have the values 5 6 7 8 and 9 you can also add a third value over here let's say two this is the number of steps it will take per iteration the default over here is one that is why you can get a difference of one over here right but if you say two then it will go from five to 7 to 9 if I execute this there you go by 7 and 9 in this section of the video we will be looking at tles now tles are kind of like less but tles are immutable what this means is that once you declare a tle it can't be changed let us see how to do that you'll first start with the variable name as always let's say numbers equal to 1 2 and three now till now you've been using list right and when you do list you use a square bracket but with tles you will use round brackets and once you execute this you can check for the methods and you won't find anything like append remove Etc you have count and you have index now what count does is suppose you have two elements of some sort and you say numbers. count three now you can see that numbers. count has given you an output of two what this means is that three occurs twice in your tle the other method is the index the index will just return you the first occurrence of a particular element that you ask for now why do you need Tes well I agree that we will be working with less most of the time but there are certain situations where you wouldn't want someone or yourself to accidentally change the data in your list right that is where you will use [Music] tles so now moving on towards the next topic of the session which is functions in Python before discussing what exactly are functions let's figure out why do we need python functions functions are used to manage inputs and outputs in a computer program programming languages are designed to work on data and functions are an effective way to manage and transform this data the modifications are generally done to drive outcomes like performing tasks and finding results the set of operations or instructions required to do so comes from logically functional blocks that can be reused independently from the main program in fact the main code is also a function just a very important one every other function is logically aligned and maintained to functionally execute your main code but if the function has not not been defined previously you'll just have to Define one yourself before using it this is because the function definition lists the steps of operation so basically functions are nothing but tasks that the user wants to perform repeatedly by defining it just once with the name the user will be able to use the function anywhere in the program this not just reduces the size of the code but also helps in debugging so now coming towards what exactly are python functions the functions that python are classic example of such reusability so to serve a wide range of applications from GUI and mathematical Computing to web development and testing Python's interpreter already comes equipped with numerous functions that are always available for the use also you can add on libraries or modules to your program that contain predefined functions readily available for use so now talking about the types of functions in Python you can either have built-in functions Lambda functions or or userdefined functions so taking a look at each of these in detail so python buil-in functions buil-in functions are nothing but the functions that are provided by the python interpreter some examples of these are the print function the sum function minmax functions Etc now let's get back to our jupitor notebook and see how you can make use of these built-in functions the first function that I'll be discussing over here is the print function I'm sure you all know the print function print in Python is a standard function that is used to print the output to the console let me just create a heading over here I'll say functions to show the syntax of this function to you guys I'll make use of the help function so help on the print function will return all the documentation about the print function in Python so as you can see on the screen the print function takes a number of parameters value is nothing but a value or a set of values that has to be printed out to the console a separator will decide how the values are separated from each other the end parameter is used to specify what is to be printed at the end of the output default value for the end parameter is sln meaning next line next is the file parameter this is an optional parameter which comes with a right method the default value of this is cy. STD out flush is also an optional parameter that is used to specify if the output has to be flushed or is it to be buffered if the output has to be flushed the Boolean value will be true and in case if it is buffered the Boolean value will be false the default value for this is false so now let's take a few examples of the print function so the first thing that I'll print is hello world next I'll take the same function and over here I'll print it as two different strings what I can also do is add a plus sign in between this so I'll just copy this and I'll add a plus sign over here so as you can see over here the first statement is printing the hello world string as it is in the second statement over here I'm printing hello world as two different strings in the third example I've made use of a plus operator which means I want to add the first string to the second string I can also add in some numerical values say 2020 and then print it so here it is Hello World 2020 now what do you mean by a separator in case you want to use a separator what you have to do is just I'll just take this example and instead of separating them by a space let me separate them by something else so I'll just say separator should be a hyphen okay so as you can see over here in place of a space character I've got a hyphen so if I have two print statements one after the other I can can change the end parameter from next line to something else so what I'm going to do is I'll say hello world edura and in the first print statement I'll make use of the end parameter and I'll specify it to be a dashed line so as you can see over here the outputs of both these print statements have been printed in the same line but they are separated with this Das line over here so I hope you guys are clear with this so now let's talk about the minmax functions the minmax functions are also built in functions that are present in Python so in case I have a list of values say a and if I want to find the minimum and the maximum of this all I can do is make use of these functions directly so I'll just say print the minimum of a and the maximum of a so as as you can see over here the minimum value that's present in a is 1 and the maximum value present in a is 7 another commonly used function of python is the sum function this function will return the sum of all the iterables that are given as a parameter to it so I already have my list over here as a I'll just say sum of a so as you can see over here it has summed up all the values present in my list and it has return the value as 28 one more thing I want you guys to note over here is the sum function actually has two parameters now to show that to you guys let me just type in help for the sum function so as you can see over here the sum function has two parameters which is the iterables and the start iterables over here is the sum of iterables whose value needs to be summed up the second parameter is the start parameter which is by default zero meaning the sum of all the iterables should be added to zero so it will be zero plus sum of all the iterables in case I specify some some value to the start parameter it is going to add that Value Plus the sum of all the iterables so in the previous example if I have sum of a comma 10 it's going to add 10 + 28 and return the amount so as you can see over here it has returned 38 which is 28 + 10 so I hope you guys have understood this so now moving on towards the next topic which is python Lambda functions python Lambda functions are functions that do not have any name they are also known as Anonymous or nameless functions the word Lambda is not a name but it is actually a keyword this keyword specifies that the function that follows is anonymous how do you actually write Lambda functions in Python a Lambda function is created using the Lambda operator okay so now let me just type in the syntax of a Lambda function so basically it will be the Lambda keyword followed by the arguments and then the expression here arguments are nothing but the variables that are used with this Lambda function expression is a mathematical expression that will decide the output Lambda functions can take any number of arguments but they can take just one expression arguments can be anything starting from zero and there's no upper limit just like any other function it's perfectly fine to have Lambda functions with no input so now let's just take an example of a Lambda function so I'll just take a variable a and now I'll make use of the Lambda function fun I'll just take one argument for the first example I'll say Lambda X return xar X and now I'll say print a of three so as you can see over here I've got the output for X star X when a is 3 and therefore the output is N9 let me take another example with two arguments I'll just say b equal to Lambda X comma Y and I'll say print x + y I'll pass the parameters for X and Y as 1 and 2 I'm sorry guys instead of taking B of 1 comma 2 I took a of 1A 2 so here it is I found the result for X + Y which is 1 + 2 and that is three so I've taken two examples over here but please make a note that both the functions have a single expression so now moving on towards user defined functions functions that we Define ourself in a program in order to achieve some tasks is referred to as a userdefined function so the Syntax for a userdefined function will be the DF keyword followed by the name of the function and then the arguments you can have any number of arguments guys then comes the body of this function the body of this function can have any number of statements so I'll just write statement 1 2 Etc so as you can see over here the first thing that you'll have to do is make use of the DF keyword now this keyword will actually help you to Define your own function next up will be the name of the function then you'll have to pass in the arguments of your function which can be any number of arguments starting from zero the body of the function can have any number of statements in them so now let's move on and create some userdefined functions so what I'm going to do over here is create a function to add two numbers so I'll just say Define add function I'll specify the parameter as X and Y and I'll just say the sum is equal to x + y and I'll return the value of sum after this I'll just have to call the add function and pass the values of X and Y I'll say 2 comma 4 and I'll run this so as you can see over here I have have the output for 2 + 4 which is 6 so now that you've seen what are user defined functions as well as Lambda functions let's take a small example wherein you can make use of the Lambda function within a user defined function so what I'm going to do is Define the function over here say new function and to this I'll just give one parameter as X now this function will just return one statement that is the Lambda function the arguments for this function will be y and I'll specify the expression as X St y now I'll take a new variable say t and now I'll have to call the user defined function which is new function what I'm going to do is just print T of 5 so as you can see over here my function takes one parameter as three so X over here is three and Y is 5 now if you're wondering why 3 is assigned to X and five is assigned to Y this is because I'm specifying three as the parameter of my new function then within this function I've made use of the Lambda function which takes one parameter therefore this parameter is supplied the value as five so I hope you guys are clear with this so now moving on towards the next very very important topic of this session which is data structures in Python so what exactly is a data structure data structures allow you to organize your data in such a way that it enables you to store collections of data relate them and perform operations on them accordingly python has implicit support for data structures which enables you to store and access data these special data structures of python are nothing but lists dictionaries tles and sets python allows its users to create their own data structures as well the most prominent data structures of this section are Stacks cues trees link list and so on so now talking about the built-in data structures of python lists lists are used to store data of different data types in a sequential manner there are addresses assigned to every element of a list which is called as the index the index value that starts from zero and goes on until the last element is called as the positive index negative indexing starts from minus one that is from the last element that is present in your iterable therefore positive indexing starts from the left hand side and moves towards the right hand side whereas negative indexing starts from the right hand side and moves towards the left hand side so now let's get back to our Jupiter notebook and see how to create lists in Python to create a list in Python you can make use of the square brackets and add elements in it accordingly in case you do not pass any values the list is going to be empty so I'll just take a new list over here and I'll assign some random values to it okay I'll take make another list I'll say my list and I'll not assign any value to this then I'll just print the values of both these so as you can see over here new list contains a list of elements starting from 1 to 7 and my list is empty not just this you can also make use of of the list function to create a list so I'll just take a new variable say a and I'll say this is equal to list of I'll specify an Mt list now let me just print out a so as you can see over here I've created an empty list making use of the list function now since lists are mutable you can add on values or delete values from lists in order to add elements to a list you can make use of the append extend or the insert functions the append function can be used to add a single element to your list the extend function will add a number of elements one after the other to the list the insert function on the other hand will insert an element at a specified index value in your list so let's take an example of all these three so I'll just say a do append and I'll specify a value say three next to new list I'll add on a number of values I'll say new list. extend and I'll give a list of values also I'll make use of the insert function I'll say new list. insert I want to insert the value at position two and here I'll insert a string I'll say edureka now let me just print all these I'll print a so as you can see previously a was empty but after making use of the append function I've added three to my list so now in case I want to see what's present in new list I'll just say print new list so as you can see nine and 10 have been added to the end of my list and edura has been inserted at index number two so one is at index number zero two at index number one and edura is present at index number two so that was about adding elements to a list elements can also be deleted from a list in case you want to delete elements from a list you can either make use of the pop function the remove function or the delete keyword now let's take an example of each of these for my new list what I'm going to do I'll say new list.pop of two and then I'll say print new list as you can see over here the value that's present at index number two has been removed from wer St here on the other hand if you make use of the remove function you'll have to specify the value to be removed instead of the index number so over here I'll just say new list. remove and I'll specify the value to be 10 and then I'll just print it so as you can see 10 is not present in my list anymore similarly if I make use of the de keyword I can delete some element that is present at some particular index number I'll say delete new list of five and then print new list so as you can see over here the element that is present at index number five has been removed elements of a list can also be accessed by using the for Loop so in case I want to access all the elements that are present in new list all I have to do is use the for Loop so for X in new list I'll say print X so as you can see all the elements have been returned one after the other you can also slice the new list from some particular range so I'll just print all the elements that are present after index number two sorry guys I said I want to print all the elements that are present after two but here I specified colon 2 which means I've printed all the elements starting from the index number 0 to two there are a number of other functions that you can also use which is the length function to find the length of the list the index function which finds the index value of the value passed as a parameter and the first occurrence of it the count function which finds the count of the value passed to it you can also sort the list by making use of the sorted or the sort functions so now moving on towards the next data type which is a dictionary dictionaries are used to store key value pairs to understand it better think of a phone directory where hundreds and thousands of names and their corresponding numbers have been saved here names will be the keys and the phone numbers will be the value pairs for that keys in Python dictionaries can be created using the flow brackets you can also create them by making use of the dict function so now let's get back to our Jupiter notebook and create some dictionaries over here I'll just take a variable say a and then I'll open the flaw brackets I'll take the keys as numbers so I'll just say one is hello and two is world and then I'll just print out a in case you do not specify any parameters an empty dictionary will be created so I'll just take an example of that over here I'll say B is equal to flop bra and then I'll print B so as you can see over here an empty dictionary has been created you can also create dictionaries by using the dict function so I'll just say C is equal to dict off and I'll copy the same thing so as you can see over here I've created a dictionary by making use of the dict function so now let's move on and see how you can actually change and add values to dictionaries values of a dictionary can be changed by using keys so the first thing that you'll have to do is access the key and then change the value accordingly so in case I want to change the value of two over here what I'm going to do is C of 2 is equal to edua and then I'll print C so as you can see over here the value for two has been changed from world to edura in case you want to delete items from a dictionary you can make make use of the pop function the pop item function or the clear function the pop function will return the value that has been deleted the pop item function will return a tupple of the key and the value to clear the entire dictionary you can make use of the clear function so let me just show you an example of this I'll say C do pop the value present at index number one so as you can see over here hello has been removed from my dictionary in case I make use of the the pop item function it's going to return both the key value pair so as you can see over here the pop item function has return both the key value pair that has been removed now my dictionary is empty in case I want to delete this dictionary I can make use of the clear function so I'll just say C do clear my dictionary is already empty and I don't actually have to make use of this function I just typed this function over here to show you guys how to use it the values of a dictionary can also be accessed now to access the values you'll have to make use of the keys only to do this you can either make use of the get function or directly specify the name of the key I'll just create a new dictionary over here I'll say d is equal to one is to python two is to Java and three is to C now I want to retrieve the values that are present for the keys 2 and three so what I'm going to do is use the print function and within this I'll specify the name of my dictionary and then in the first example I'll just specify the key value and in the next example I'll make use of the get function I'm sorry guys I've not executed this line over here so as you can see over here the value present for key2 is Java and that for key3 is C so now talking about tles topples are actually the same as lists but with one exception that the data once entered into a tle cannot be changed no matter what to create a tuple in Python you can make use of the regular brackets you can also create a tuple by making use of the tle function I'll just create my Tuple and in the first case I'll create an empty Tuple next I'll create another Tuple say Z and I'll assign some values to this I'll take another variable Y and then I'll make use of the Tuple function in order to create a tuple now let me just print out each of these I'm sorry guys I've missed regular braces over here so as you can see an empty Tuple has been created followed by a tuple using the regular braces and then a tuple making use of the tupal function like I've already told you all before you cannot alter the values of a tuple so in case I use any function such as an upend function with a tuple it's going to throw an error so now just to show this to you all I'll say y do append I'll say three so as you can see over here it has thrown an attribute error saying Tuple object has no attribute append in case you want to access the elements of a tuple all you have to do is make use of a for Loop so I'll say for I in y print I so as you can see all the elements of the Tuple have been retrieved one more thing I want you guys to note over here is that in case you want to add on some elements you can make use of the plus operator so now moving on towards the next next data structure which is a set sets are a collection of unordered elements that are unique meaning that even if the data is repeated more than one times while specifying the values the set data type is just going to take one entry of that element sets in Python actually resembles the sets that you've learned in arithmetic the operations are also the same such as intersection difference Union Etc to create sets in Python you can make use of the FL braces and instead of adding the key value pairs you just have to specify the values now let me just get back to my Jupiter notebook and show you all how to create sets in Python like I've already told y all sets are created by making use of the flar braces I'll just take a variable over here as X and to this I'll specify some values and then I'll just print the set so as you can see over here I supplied eight values but only five values are present in my set now this is because I have three repeated values and those values are not printed again in my set in case you want to add some values to a set you can make use of the add function so I'll just say x. add and I'll pass the value as8 now let's just print out X so as you can see over here8 has been added to my set you can perform many operations on set such as intersections unions difference Etc now what I'm going to do is Define another set over here I'll say y y is equal to some random values so in order to find the union of X and Y I can either use a pipeline character or make use of the Union function in case I just want to find the union of X and Y I'll say Zed is equal to X pipeline Y and then I'll print C sorry I've taken cap C over here which is small actually so as you can see I have the union of X and Y present in Z similarly I can also make use of the Union function to do that I'll just make use of the print function and then I'll say x Union y equals x dot Union of Y so as you can see over here I Found the Union of X and Y similarly you can also make use of the difference function the intersection function Etc so let me just change this from Union to intersection and see what happens so as you can see over here I changed the value from Union to intersection and therefore I found the intersection of X and Y I just have to make a change over here as well okay so I hope you guys are clear with this so that was about the built-in data structures of python now moving on towards the next topic which is userdefined data structures user defined data structures can either be arrays Stacks cues trees link lists graphs or hashmaps an array is basically a data structure which can hold more than one value at the same time it is a collection or ordered series of elements of the same type we can Loop through the array easily and fetch the required values by just specifying the index numbers arrays are mutable which means they are changeable and therefore you can perform various manipulations operations as required stacks Stacks are linear data structures which are based on the principle of last in first out here the data which is entered last will be first to get accessed it is built using the array structure and has operations namely pushing or adding elements popping or deleting elements and accessing elements only from one point in the stack which is called as the top this top is the pointer to the current position of the stack Stacks are prominently used in applications such as recursive programming recursive words undo mechanisms in in word editors undo mechanisms in word editors and so forth Q's a q is also a linear data structure which is based on the principle of first in first out here the data entered first will be accessed first it is built using the array structure and has operations which can be performed from both ends of the queue that is the head tail or the front back operations such as adding and deleting elements are called as NQ and DQ qes are used as Network buffers for traffic congestion management used in operating system for job scheduling and many more trees trees are nonlinear data structures which have roots and nodes the root is a node from where the data originates and the nodes are the other data points that are available to us the note that precedes the parent and the node after is called as a child levels of a tree shows the depth of information the last notes are called as the leaves trees create a hierarchy which can be used in a lot of real world applications such as HTML Pages which use trees to distinguish tags that come under the same block it is also efficient in searching purposes and much more link lists link lists are linear data structures which are not stored consequently but are linked with each other using pointers the node of a link list is composed of data and a pointer called Next these structures are most widely used in image viewing applications music player applications and so forth graphs graphs are used to store data collection of points called as vertices and edges graphs can be called as the most accurate representation of real world map they are used to find the various cost to distance between various data points called as the nodes and hence find the least path many applications such as Google Maps Uber and many more make use of graphs to find the least distance and increase profits in the best ways hashmaps hashmaps are the same as what dictionaries are in Python they can be used to implement applications such as phone books populate data according to lists and much more so now let's move on and discuss what are algorithms in Python algorithms in general are rules or instructions that are formulated in finite sequential order to solve problems and get the required results they give a pseudo code for problems and can be implemented in several languages as they are not language specific so how do you write algorithms algorithms are generally written as a combination of user understandable language and some common programming languages they are commonly written in steps however ever it is not always necessary to do so there are no distinct rules to formulate algorithms but you will need to keep the following points in mind figure out what exactly is the problem determine where you need to start and determine where you need to stop formulate the intermediate steps and finally review your steps say for example if you have to formulate an algorithm to check if a student has passed in an exam or not so after you start you're going to declare two variables say X and Y and then you're going to store the marks op obtained by the student in X and store the minimum passing score in y then check if x is greater than or equal to Y if yes then return pass else return fail and finally you'll have to stop so this was an example of an algorithm to check if a student has passed or failed however you can manipulate the steps according to your preference for example you can assign values to both variables X and Y in a single step rather than two steps so this way a single problem can have multiple Solutions and it depends on the problem and the programmer to choose the most feasible and the reliable Solutions so now let's understand what are the elements of a good algorithm good algorithms need to have finite clear and understandable steps there should be a clear and precise description of the inputs and the outputs each step needs to have a defined output that depends only on the inputs in that step or the preceding steps the algorithm should be flexible enough to mold it and allow a number of solutions the steps should make use of General programming fundamentals and should not be language specific so what are the classes of algorithms available for programming the three major algorithm classes are divide and conquer class dynamic programming and the greedy algorithm class the divide and conquer class divides the problem into subparts and solves each of them separately talking about dynamic programming it divides the problems into subparts remembers the results of subparts and then applies to the similar ones this way there'll be lesser number of repetitions when compared to divide and conquer method greedy algorithms involve taking the easiest step while solving a problem without worrying about the complexity of the future steps so now let's move on and take a look at the important algorithms such as tree traversal algorithms searching algorithms and sorting algorithms in Python the first that I'll be discussing in this session is the tree traversal algorithm trees in Python are nonlinear data structures having a root and node as mentioned earlier tree traversal refers to visiting each node present in a tree exactly once in order to update or check them so based on the order in which the nodes are visited there can be three types of traversals that is the pre-order traversal in AO traversal and the post AO traversal before moving on in detail about each of these algorithms what I'm going to do is jump onto my Jupiter notebook and create the tree that you see on your screens I'll create a heading over here and I'll just call it as tree traversal so like I've already told y all I'll be writing down the code to create a tree first so for that let me just create a heading this will be in edge2 so I'll have to have two hashes and then I'll just say creating the tree so the first thing that I'm going to do over here is define a class called node so class node in case you have any issues understanding classes and objects don't worry guys because I'll be discussing each of this in detail later on in this session so for now I'm just defining a class within which I'll Define a function that is the init function and then the mandatory parameter which is self followed by the value parameter now this self parameter will allow me to access the variables of this class all the details about classes and this self parameter I'll be discussing later on in this session when we go on to oops in Python so for now I'll just write self. left child or I'll just say LC equals none now I've specified none over here because right now there is no left child the first value should be assigned to the root note and therefore the left child and right child will contain nothing and therefore I'm assigning none to it similarly I'll write self. write child equals none finally self. node data will be equal to Value so now let's move on and create an object of this class so I'll just call the object as root and you guys have seen already that in my tree the first root or the root node is one so so therefore I'll assign a value as one so once this is done I'll have a root node that is created so once the root is created I'll have to create the left child and the right child for it so now what I'm going to do is root do left child will be equal to node of two and root. right child will be node of three so in my tree the node two has another left child and a right child so to create create that what I'm going to do is write down root. left child do left child is equal to node 4 and finally the right child of node two will be node five so I hope you guys are clear with this let me just draw this and explain this to you guys so at the first step I'll have my root node that is created and that will be node one following that I'll have a left child for this node so that will be two and then a right child will be created for one that is three now again node two will have two Childs that is left child will be node four and right child will be node five so I hope you guys are clear with this concept so now I'll just erase this drawing so now let's move on and see how you actually do the in order traversal in order traversal refers to traversing the tree in such a way that you first visit the left noes followed by the root and then the right noes you begin your traversal from all the nodes in the left sub tree then move towards the root and finally the right sub tree so if I have to implement the in order traversal over here what I'm going to do is I'll just create a heading I'll say in order since I have my base tree already created over here all I'm going to do is just Define the in order function so the first step of in order traversal will be to Traverse through the nodes present in the left sub tree then at step two I'll have to visit the root and finally the right sub tree so now I'm going to define a function I'll call it as in order and then I'll pass a parameter as root so if a value for the root is present what I'm going to do is call the in order function and over here I'll first visit the left child so root of left child and then I'll print the value for the root okay sorry guys if root I just missed that so once I visit the left child and the root I'll have to visit the right child so I'll say in order of root. right child after this I'll have to call the in order function so in order of root so as you can see over here the in order traversal starts from four moves on to node two then five 1 and three so in this tree over here for the inorder traversal it will start from the leftmost node and the leftmost node in my tree is node four so it will start from four then it moves towards the root and finally the right right node once I'm done with 4 2 and five my left sub tree is completed once the left sub tree is completed I'll have to apply the same formula again which is left root right so therefore I'll visit node one which is the root node and finally visit rot three which is the right node so I hope you guys are clear with this okay so now moving on towards the next traversal which is the pre-order traversal in a pre-order traversal the root node is visited first followed by the left sub tree and then the right sub tree so in this tree example over here I'm going to visit the root node first which is one then come towards the left sub tree following which I'll move towards the right sub tree therefore the order will be 1 2 4 5 and three now let's get back to a jupyter notebook and implement this so I'm going to define a function that is a pre-order function and I'll pass root as a parameter so if root is present I'll have to first print the value of the root node so root do node data and then I'll have to do the pre-order for the left child first so root. left child and then pre-order for the right child so once this is done I'll just have to call the pre-order function so as you can see over here the first node to be visited is the root node one followed by the left sub Child 2 and then the left sub child of two which is four and then I'm going to visit root five which is the right sub child of two and finally when the root and the left sub tree is done I move on towards the right sub tree which contains three okay so I hope you guys have clear with this okay so now let's move on towards the next topic which is the post order traversal the post order traversal begins from left then to the right and finally the root so in this example over here what I'm going to do is start from four move on towards 5 2 1 and 3 now this is because four is the leftmost child and at this stage I appli the Left Right root formula so my program is going to check if I have a right node towards the right of four is five I Traverse five or I visit five and then when I've completed four and five I go to the root node which is two once the left sub tree is completed I have to move towards the right sub tree and then Traverse it in my right subtree I just have one node which is three and after I complete the left subtree and the right subtree I apply the Left Right root logic again and then I visit the root which is one okay so I hope you guys are clear with that so I'll get back to my Jupiter notebook in order to implement this so what I'm going to do is just Define a function that is pre-order sorry post order and I'll pass root as the parameter the first thing that I'll have to do is visit the leftmost child so for that I'll just do a post order for the left child so root do left child and then post order of the right child finally I'll have to print the value of the roots node once this is done I'll call the Post order function and I'll pass root as its parameter seems like I've made a mistake sorry guys I just missed the if statement to check if root is present or not if the value for root is present only then I'm going to do this so let me just type in a tab for all this and then I'll execute it so here it is my left subtree is visited first Then followed by the right subtree and then one which is the root node so I hope you guys have understood all of these three traversals so now moving on towards the next algorithm that is the Sorting algorithms sorting algorithms are used to sort data in some given order sorting algorithms can be classified as mud sort bubble sort s insertion sort selection sort or shell sort please make a note guys that in this session I'll be sorting everything in the ascending order so the first algorithm I'll be discussing in the session is the m sort algorithm the M sort algorithm follows the divide and conquer rule here a given list of items is first divided into smaller lists until it reaches a point where each list consist exactly of one item by default a list consisting of one item will be sorted and the m sort algorithm then Compares adjacent lists and reorders them in the desired sequence this process is done recursively until it reaches a point where there exists only one sorted list so I'll just create a heading over here and I'll call it as sorting algorithms or I'll just say sorting the first algorithm I'm discussing is merge sort so I'll just call it as merge sort I already have the code ready for this I'll just copy that code and explain this to you all so as you can see on the screen the first thing that I've done over here is defined a merge sort function now this function will check if the length of the list is greater than one or not and if this is true it's going to divide the list by two following that I'm going to sort both the left half and the right half recursively finally I'll create a new list that contains the s Ed elements so over here I've called an mlist function but I've not defined it the next thing I've done over here is defined an mlist function the first thing that I've done over here is defined what is the left list and what is the right list left list contains all the elements from the left to the middle and the right list contains all the elements from middle to right following that I've taken three variables k i and J where K is the temporary variable that I'm using to fill on my final list I and J over here help me to iterate over the left list and the right list so while my left list and my right list contain elements that is when they're not empty what I'm going to do is if the value of the item present in the left list if the value of the item present in the left list is less than or equal to the value of element that is in the right list I'll place the element present in the left list into my sorted list else I'll place the item present in the right list in my sorted list so if the value of left list of I is less than the value of right list of J I'll place the value of left list of I into K else I'll place the value of right list of J into my list of K if I place the value of left list of I into my list of K I'll have to increment the value of I or else if I place the value of right list of J into my list of K I'll have to increment the value of J finally when I've placed some value for K I'll have to increment the value of K as well so once I have all sorted elements what I have to do is start merging my list the final part of my program will help me to merge the sorted sub lists so if there's any element present in the left list and it is less than the right list what I'm going to do is place that element into my new list then I'll increment the value of I and I'll increment the value of K when the left lists are completed I'll have to place the values present in the right list into k once all the elements of write are placed into K my list is completed so in the final part of this program all I'm doing is assigning some value to my list here in this program I've made use of the input function now this function will allow the user to specify some list since the user will provide a sequence of elements I'll have to split them and for that I've made use of the split function and for all the elements present in my list I've converted them to the integer find finally I call the merge sort function to which I pass my list as a parameter and the value of left is zero which is the first index value and the value of right is the length of my list finally I'm just printing the sorted list finally I'll have to enter the list of values so I'll just pass in some random values so as you can see over here my sequence is first split and then it's converted to integers and then find finally by calling the merge sort function I have created a list of sorted elements so now moving on towards the next sorting algorithm which is the bubble sort algorithm the bubble sort algorithm is a comparison algorithm that first Compares and then sorts adjacent elements and if they are not in order it swaps them this process is repeated n minus one number of times where n is the length of the list so I already have a program for this I'll just copy it so the first thing that I've done over here is defined a bubble sort function so like I've already told yall for every list I will have n minus one iterations where n is the length of the list in this program I've named my list as a and the number of iterations will be length of a minus one and the value of that is stored into variable B so for every element that comes in the range of B I need to take that element and check if that element is greater than the element that is present towards the right of it in case that is true all I have to do is swap the elements so a of Y will become a of y + 1 and a of y + 1 will become a of Y so as you can see I've given some random list over here in this case the length of this list is four therefore the value of B will be three so for every element present in the range of three I'm going to take that element and check if that element element is greater than the element that is present towards the right of it so first I'll take three and check if it is greater than six since 3 is less than 6 I'm not going to swap these values next I'm going to check if 6 is greater than 1 or not so as we all know 6 is greater than 1 I'm going to place the value of one in place of six and I'll Place 6 in place of one therefore six and one will be swapped and my list will contain 3 1 6 and then eight for the next iteration I'll check if 6 is greater than 8 so as we all know 6 is less than 8 therefore they will not be swapped so for the second iteration my list is going to look like 3 1 6 and 8 next I'll have to perform bubble sort on this list again so what my algorithm will do is check if three is greater than 1 or not since three is greater than 1 the position of 1 and three will be swapped and my list is going to look like 1 3 6 and 8 in the next iteration my program will check if three is greater than 6 or not as we all know that these are already sorted they will not be swapped this process is repeated until the complete list is sorted so so I hope you guys have understood the bubble sort program okay so now moving on towards the next that is the insertion sort insertion sort picks up one element of a given list at a time and places it at the exact place where it's supposed to be since I already have a program for this I'll just copy it over here and explain it to you guys so what I'm doing over here is first I'm going to place my pointer at the second element so whatever input list I have I'm going to place the pointer at the value that's present at index number one now to do that I've made use of this for Loop so for X in the range of one comma the length of a which is the element present at 2 and throughout the length of a I'm going to set the element present at a of X as the key and then I'll store the value of J as xus 1 so in the first case k will be 56 so 56 is going to be K and J Will will be 1 - 1 so that is 0o so in the first case k is 56 and J is 24 the next thing what I'm going to do is check if J is greater than or equal to zero here in the first case J is equal to zero so therefore the first half of the Y Loop is true the next thing to be checked if K is less than a of J so if the value present at K which is 56 is less than the value present at a of J which is 24 I'll have to swap these elements now since these elements are already sorted I'm not going to do that now once that is done I have to decrement the value of J now this step is done because I'll have to check the value of the key with all the elements that are present in the left subl list in the first case I just have one element present towards the left side but for the second iteration I'll have two elements present so therefore if I do not decrement the value of J I'll not be able to check this value with all the values that are present towards the left of it so finally I'll place the value of a of J + 1 into K and then I'll move on towards the next value of x which will be index number two so I'll just Erase this after the first iteration of my program the list is not going to change since the value of 56 is greater than that of 24 for the next iteration I'm going to make one as the key element since it's present at index number two so one 1 will become the key and the value of J will be 56 and 24 now this is because 56 is present at a of 1 and 24 is present at a of 0 and for all the values that are greater than or equal to zero I'll have to check if the value of the key is less than that or not so I know that 1 is less than 56 and 24 so what I'm going to do is just remove one from here repeat and then I'll Place 56 in place of one I'll Place 24 in place of 56 and finally I'll Place one in place of 24 so for this iteration I'll have 1 followed by 24 and then 56 which is my sorted list and for the unsorted list I have 50 and 17 so once this is done what I'm going to do is take the next element as the key which is 50 when 50 becomes the key I'll have to check this key with all the values that are present in the left side so I know that 50 is less than 56 but greater than 24 so therefore I'm going to shift 56 to the place of 50 and I'll Place 50 in place of 56 after this I'll have a list looking like 1 followed by 24 then 50 and 56 and in my unsorted list I'll have just one element which is 17 finally I'll take 17 as the key and then I'll check this key value with all the elements present towards the left of it since I know that 17 is less than 56 it's less than 50 and less than 24 what I'm going to do is Place 56 in place of 17 50 will come to the position of 56 24 will come to the place of 50 and finally 17 will be placed in the place of 24 so I hope you guys are clear with this even though there's a lot of mess on the screen I hope you guys have understood this okay so let me just erase that for now and execute this problem so as you can see on the screen I've sorted my list in the ascending order so now moving on towards the next which is the selection sort algorithm the selection sort algorithm divides a given list into two halves where the first half will be sorted list and the second half will be an unsorted list at first the sorted list is empty and all the elements are present in the unsorted list the selection sort algorithm will look at all the elements present in the unsorted list pick up the element that is supposed to come first and then it places it in the sorted list the step is then repeated by searching the least element present in the unsorted list and it places it next to the first element of the sorted list so in my examples since I'm arranging all the elements in the ascending order if I have a list of numbers I'm going to select the smallest element and then place it as the first element of the sorted list then I start to search for the next smallest element and place it towards the right of my first element so I'll get back to my Jupiter notebook in order to implement this I have the code for this already so I'll just copy it so initially what we have to do is assume that the first element is the minimum element then what I'm going to do is check if the first element is less than any element present towards the right of it so so in my list I'm going to first assign 34 as the minimum value so what I'm going to do next is I'm going to check if the value of this minimum is less than any value that is present towards the right so for every element that is present towards the right of minimum that is X if that value is less than the minimum value I'm going to place the value present at my array of Y as the minimum value so for the first iteration I'm going to check if 23 is less than 34 or not since 23 is less than 34 23 becomes minimum so after the first iteration 23 becomes the minimum value next I'm going to check if 1 is less than 23 since 1 is less than 23 one becomes the minimum value so after the third iteration I'll have one as the minimum value finally I'm going to check if 1 is smaller than 67 or not since 1 is is smaller than 67 the minimum value will remain one finally I'll check if 1 is smaller than four or not since 1 is still smaller than four the minimum value is 1 so the first element to be placed in the sorted list will be one my sorted list will have one and unsorted list will have all the elements which is 34 23 67 and 4 so this is my sorted list and this is my unsorted list so once this is done I'm going to repeat the same thing for the unsorted list again at this time 34 will be minimum and after the first iteration 23 will become minimum and for the third iteration 23 is going to remain the minimum following which I'm going to check if 4 is less than 23 or not since 4 is less than 23 4 is going to be removed from the unsorted list and it will be placed towards the right of one which will become the second element present in the sorted list so I hope you guys are clear with this I'll just erase all these drawings and I'll run my code so as you can see over here I have a sorted ascending order list now moving on towards the next algorithm which is the shell sort algorithm the shell sort algorithm allows you to sort elements that are apart from each each other the original sequence of the S elements follows the n by2 n by4 sequence where n is the number of elements present in the unsorted list for example if you have a list of eight elements the length of that list will be divided by two for the first iteration as we all know 8 / 2 is 4 now the first element will be compared to the element that is present at index number four and then the Gap will be produced by dividing 8 by 4 this time the Gap will be two and the elements present at these intervals will be compared finally I divide 8 by 8 which gives me the Gap as one so when I have the Gap as one I compare adjacent elements and the final list will be sorted so what I'm going to do over here is just copy a code that I already have written so my program over here will first divide the length of my array by two then if the value of Gap is greater than zero for every element that comes in the range of Gap if the elements present after the Gap elements are greater than y we'll have to follow the insertion sort algorithm and swap the elements finally the Gap is divided by two again since in my case n is seven which is my list contains Seven Elements I'm going to divide 7even by 2 to find the Gap so 7 by 2 which is equal to G or the Gap will be nothing but three now I'm taking three because it's the whole part of 3. 5 so the Gap over here will be three so what I'm going to do is first take this element 23 and then I'm going to check with the element that is present at index number three so at index number three is 17 since 17 is less than 23 these values will be swapped so after the first iteration 17 will be at the first place okay let me just clear these drawings and then write down the F list and then after the first iteration I'll have 17 followed by 12 1 23 which was swapped with 17 45 2 and 13 so for the next iteration I'm going to check 23 with 13 now this is because I'll have to check after the gap of three so since 13 is less than 23 I'm going to swap these elements so after that I'm going to have 13 in place of 23 and 23 will be placed at last in place of 13 so once this is done I'm going to divide the Gap by two again so my Gap in this case was three so therefore if I divide this by two I'll get the Gap to be 1.5 wherein I'm going to take the whole number which is one so Gap in this case will become one so next up I'm going to check all the elements that are present at Gap of one and then replace them I'm going to repeat this until my list is sorted so let me just clear all these drawings and I'll execute this problem so as you can see I have all the elements sorted in the ascending order so that was all about the Sorting algorithms now moving on towards the searching algorithms searching algorithms are used to search for or fetch some elements present in some given data set there are many types of search algorithms such as linear search binary search exponential search interpolation search Etc in this session I'm going to be discussing about the linear search and the binary search problems the linear search algorithm is used to successively search for a given element by comparing it with each element of the given array it is one of the simplest searching algorithms but very important to understand other searching algorithms so what I'm going to do over here is get back to my jupyter notebook and over here I'll just create a heading again I'll call this as searching algorithms the first one over here is linear search and I'll just paste the code that I have for it so the first thing that I've done over here is created a function that is going to accept the iterables the length of the list and the key element so if any element present in the list matches the key element I'll have to return that element in case the element is not found I just have to return minus one so for every element present in the range of 0 comma n that is starting from the zeroth index number and for all the elements present in the list I'm going to check if any element present in my list is equal to the key or not in case the element is present I will return X else I'll return minus one okay the key that I've supplied over here is 16 and my array over here contains some random numbers next thing that I've done over here is assign the value of n as the length of my array following that I've take another variable named match to which I'm going to assign the value of the linear search function if the value of match is equal to minus one then key is not present in my array else the key is present at so and so index so let me just erase these drawings and I'll execute this problem so as you can see over here my key is 16 which is not present in my array in case I change the value of this to 12 and then I run this program you see that the key is present in the given list at index number zero so I hope you guys have understood this so now moving on towards the next algorithm which is the binary search algorithm binary search is used to search for some given element in a sorted array by making use of the decrease and Conquer algorithm here the key is looked for by first comparing it with the middle element and then dividing the into half the left half is searched if the element to be searched is smaller than the middle element and vice versa the appropriate sub arrays are again divided into half and the process is repeated again say for example if you have a sorted list of Seven Elements the key will be compared with an element that is present at the middle or the element present at 7x2 which is 3.5 so in this case the middle element will be assigned as the element present at index number three so in case the key element is smaller than that of the element present at index number three I'm going to check the left sub list if the value of key is greater than that of the value present at index number three I'm going to search in the right sub list so I already have a program for this I'll just copy paste it so in my program I've created a function called as the binary search function to which I'm supplying the list as well as the key element left variable is going to point at the first element which is present at index number zero and the right element is going to be length of my list minus one which is the last element present in my list then I've taken a variable to which I've assigned a Boolean value as false initially the value of this is going to be false so until my list is not empty I'm going to divide my list by two so if the value of the middle element is going to be equal to the key I'll change the value of match from false to True else if the key value is less than that of the value present at the middle I'm going to make the value of r as middle minus one which means I'm I'm going to search only towards the left list and if the value of key is greater than that of middle I'm going to change the value of left as middle + 1 which means I'm going to check only towards the right of the middle element finally I'll return the value of the Matched variable so in this problem I've taken two examples in the first case I've taken an element that is present in my list and the second I've I've taken an element that is not present in my list okay so as you can see over here for the first case I have true and for the second case I have false okay so I hope you guys have understood this so now moving on towards something that all of you need to know which is object-oriented programming in Python object-oriented programming is a way of computer programming that makes use of the idea of objects to represent data and methods it is also an approach used for creating neat and reusable code instead of a redundant one here the program is divided into self-contained objects or several mini programs every individual object represents a different part of the application having its own logic and data to communicate within themselves so what exactly is the difference between object-oriented programming and procedure oriented programming object-oriented programming has a bottomup approach whereas procedural oriented programming has a top-down approach in oops the program is divided into objects whereas in procedural programming program is divided into functions oops makes use of access modifiers such as public private protected whereas procedural programming does not have any access modifiers oops languages are much more secured compared to those which are procedural languages the objects created in an object-oriented programming language can move freely within the member functions whereas in procedural programming language data can move freely from function to another function object-oriented programming supports inheritance whereas procedural oriented programming does not support inheritance so now moving on towards classes objects and the pillar of oops in detail so first let us discuss about objects an object is a real world entity such as a pen book person animal Etc objects have States and behaviors in Python everything such as variables functions Etc are considered to be an object a class on the other hand is a collection of objects or you can say that it is a blueprint of objects defining the common attributes and behaviors now the question arises how do you do that well the class logically groups the data in such a way that the code reusability becomes easy so for example consider an office-going employee as a class and all the attributes related to it like employee name employee age salary ID as the objects in Python so all the details of the employees will be placed in the class employee this way you'll not have to write the code repeatedly for all the employees instead a single class will will be enough to determine all the details of each employee so now I'll just jump onto my Jupiter notebook and show you guys how to write down a class in Python so I'll just create a heading so here I'm going to write down the Syntax for a class the first thing that I'll have to specify is the class keyword followed by the name of the class like I've already told yall in the naming conventions class names begin with a capital letter followed by small letters so in case the name of my class is my class my is going to be with capital M followed by a small Y and cap C followed by small l a s so this is my class name within this class I can have n number of statements so I'll just say statement one one to statement n so this is basically the syntax of a class so now talking about methods methods are nothing but functions that describe the attributes and behaviors of an object now let us take an example to demonstrate a class an object and a method please make a note over here guys that it's mandatory for python class functions to have a self parameter unlike other languages in Java self is nothing but a reference to the current instance of the class and it is used to access the class variables so just create a class over here I'll say class new class and within this I'll Define a function and I'll call it as new function so like I've already told y all the first parameter to this by default should be the self parameter so when this function is invoked I just want to return hello world now once this is done I'll come out of my class and I'll create an object of my class so I'll just say object is equal to class so this is how you create an object in Python you just have to give a name to your object and then make use of the class name sorry the name of my class is new class not just class next I'm going to make use of this object of new class to call the new function so I'll just say print obj do new function so as you can see over here with the help of this object I just created I've called the function which is present in my new class and therefore it has returned hello world similarly you can have multiple objects and functions in a single class so now let's take another example in which I'm going to have more than one functions and objects I'll just copy this I'll paste it over here and I'll take another function named my function I'll specify the self parameter as usual and for this function I'll just have one return statement that says welcome to edureka so now I have two functions and one object I'll create another object I'll say object two and this is a new class object and through this object I'll call my function so as you can see over here I have two functions two objects and with the help of both these objects I'm calling both the functions present in new class therefore I have an output saying hello world and welcome to edura so now let us take a more complex example of this what I'm going to do is create a class named calculating here I'm going to find the sum difference product and quotient for different values using more than one object so I'll just say class calculating and within this I'll Define a function say add the first parameter should be the self parameter followed by the variables that is a and b now this function is going to return the sum of A and B so I'll say return a plus b okay so I'll create a similar subtract function I'll just copy this and in place of add I'll write subtract and in place of a plus b I'll say a minus B now let's create a function to find the product so add will become product and from plus I'll change it to Star finally a function to find the quotient so this one add will change to divide and it'll be a divided by B so now I'm done with this I'll create an object called as o1 and this will be an object for the calculating class Now using this object I'm going to find the result of each of these so I'll just say print o1 do add and I'll specify some random parameters say four comma 5 I'll copy this paste it here paste it here paste it here and I'll change the names of these functions accordingly so subtract product and divide let me change the values as well I'll just say 12 comma 5 product of 32 and 55 and divide 44 by 5 or 15 so I have one object that is calling all the functions that are present in the calculating class in case I want to create another object I can do it in a similar manner so what I'm going to do is just copy Cy this I'll paste it over here and I'll change the name of this object from o1 to O2 wherever I have o1 in this I'm going to change it to O2 so I have two objects and four methods now I want to execute this seems like I've made some error I've made a mistake in the spelling over here it's not CL a it's c a l so as you can see over here I've got the output in case I want to differentiate between the results of both these objects I'll just write down a print statement over here and I'll just print a dash line so as you can see the first half contains the result obtained by object one and the second half contains the result obtained by object two so I hope you're clear with this so now moving on towards something very important which is the init method in Python the init method is a very important method that is always executed whenever a class is initiated so whenever you create an object of a class this method will be invoked the init method me is always used in the real world applications unlike the previous examples that we just saw so now to show you all an example of the init method I'll get back to my Jupiter notebook and over here I'll define a class and I'll just call it as Python Programming I'll just use a shortcut So within this I'm going to call the init function or the init method the init method has two preceding underscores prefixed and suffixed to it so I'll just say double underscore init double uncore and the first parameter should be the self parameter so here I'm going to pass in two other parameters which is name and the age of python once that is done I'll just bind the self parameter with the name and the H attribute I'll just say self. name is name and self. AG is age I'm going to Define a new function over here so I'll just say Define new function and again I should specify the self parameter and within this I'll just have one print statement that is going to print both the name and the age so I'll just say hi my name is python so that is self. name and my age is self. AG sorry guys I have to specify a plus sign over here so once this is done I'm going to create an object of this class so I'll just say P1 equals Python Programming and then I'll use this object to call the new function so P1 dot new function I'm sorry guys I forgot to pass the parameters itself so I'll have to pass the parameters I'll just say name is Python and age is 31 so as you can see I have an output saying hi my name is Python and my age is 31 so now moving on towards object-oriented programming methodologies objectoriented programming methodologies are nothing but the pillars of oops which are inheritance polymorphism encapsulation and abstraction so now let's move on and discuss each of these in detail the first thing that I'm going to be discussing over here is inheritance so what exactly is inheritance the method of inheriting the properties of a parent class into a child class is known as inheritance it has a number of benefits such as code reusability where we do not have to write the same code again and again and we can just inherit the properties we need in the child class it also represents a real world relationship between the parent class and the child class inheritance is transitive in nature if a child class inherits properties from a parent class then all other subclasses of the child class will also inherit the properties of the parent class so now let let me just show you all an example of this I'll get back to my jupter notebook so I'll just take two classes over here the first will be the parent class and second will be the child class so I'll just say class parent and then I'll Define a function and I'll call this function as first the self parameter as usual should be specified and within this function I'll just have one print statement that informs first function so next I'm going to declare another class I'll just call this class as child and this child is going to be a subass of the parent class so to inherit the parent class I'll have to specify the parent class within the braces so I'll just type in the name of the parent class within this I'm going to define a function named second and the self parameter to it now this function will also have one statement that is the print statement saying second function so once this this is done I'll create an object of the child class I'll just say obj equals child and then using this object I'm going to call the first and the second function so I'll just say obj do first and obj Doc so as you can see over here this object is able to access both the functions that is first and second the first function is not present within the child class but since the child class is inheriting the parent class this function is visible to the child class and therefore it is able to execute it so what we are actually doing over here is subclassing the child class calling a Constructor of the parent class by mentioning the parent class name in the Declaration of the child class is known as subclassing a child class identifies the parent class by subclassing so now moving on towards types of inheritance depending upon the number of child and parent classes involved there are four types of inheritance the first is single inheritance second is multiple third is multi-level and finally hierarchical single inheritance is when a child class inherits only a single parent class so in the previous example my child class is inheriting just one parent this is an example of single inheritance in case of multiple inheritance the child class will inherit more than one parent so what I'm going to do is just copy this I'll paste it over here and then I'll just call this as par one I'll declare another class as parent 2 even if you do not specify these bases it's going to work properly within this I'll just Define a function I'll call this as third and the self parameter and then I'll just print multiple inheritance so here my child class is going to inherit parent 1 as well as parent 2 then my object is going to invoke both these classes so as you can see over here in the first example I just had one parent as the base class but in this example I have two parent classes and a single child class the object of this child class is able to invoke the functions present in both the parent classes so I hope you've understood this the next is multi-level inheritance multi-level inheritance is when a child becomes a parent class for some other class I just paste the previous code and over here I'll create another child class I'll say class Child 2 and I'll inherit the child class I'll Define a new function I'll just call this as function and the self parameter and I'll print multi-level inheritance so here instead of creating an instance of the child class I'll create an instant of Child 2 and then I'm going to invoke all the functions using this object I'll say obj dot function and I'll run this so as you can see over here all the functions are accessed by this object so now talking about the hierarchical inheritance hierarchical inheritance involves multiple inheritance from the same base or parent class so in this case I'll have two different child classes that are inherited from the same parent class so what I'm going to do over here is just paste the code and then I'll have another child I'll call this as child to itself and this is also going to inherit the parent class this is the hierarchical inheritance and then I'll create two objects that is for the child class and the child to class so obj do1 will be an object for the child Class 2 let me just change the name of this function I'll just copy this paste it over here and I'll change the name of the function from second to function okay sorry guys I just forgot to change the name of the object over here as you can see over here both the child classes have executed and return the results accordingly similarly you can also have Hybrid inheritance h inheritance involves multiple inheritance taking place in a single program so just to show you all an example of this I'll take this code I'll paste it over here and I'll create another class I'll say child four or I'll say child three and this is going to inherit the child class as well as the parents class as you can see over here I have more than a single type of inheritances the first type you see is a single inheritance and now I have multiple inheritance as well similarly you can have any other type of inheritance also so I'll just Define a function save function to the self parameter and then I'll print hybrid okay so now let me create an instance of this class just copy this I'll paste it over here I'll remove these objects since we've seen their output so as you can see I can have multiple inheritance as well so now talking about the super function the super function in Python allows you to call a method from the parent class so to show that to you guys what I'm going to do is take the original two classes which was the parent and the child class I just paste it over here and within this function I'm going to type in something called as super dot the name of the function that's present in the parent class so that is first so now let me create an object of this I'll just say obj equals child and I'll execute the function present in the child class so I'll just say obj do second sorry guys I just made a mistake I specified capital s in place of small s so let me just execute that again so as you can see over here in the previous example I had to explicitly specify my object to call both the functions present in the child class and the parent class but over here my object is accessing the function present in the parent class by making use of the super function so now let's move on towards the next pillar of oops which is polymorphism polymorphism defines the ability to take different forms by making use of polymorphism you can redefine methods in the child class with the same name as that of the parent class polymorphism is taken from the Greek word poly which means many and morphism which means forms it means that the same function can be used for different types this makes programming more intuitive and easier in Python we have different ways to define polymorphism a child class inherits all the methods from the parent class however in some situations the method inherited from the parent class doesn't quite fit into the child class so in such cases you will have to reimplement the method in the child class this can be done in many ways such as using different functions class methods or objects to Define polymorphism so to understand this clearly I'll get back to my Jupiter notebook and show you all an example of this so I'm going to have different classes which will have functions with the same names so let me just define a class called this apple and this class will have a function called type within this I'll just have a print statement that is going to tell me if this is a vegetable or a fruit so as we all know apple is a of fruit I'll just type in fruit and then I'll create another class called as potato here I'll create a function with the same name but with a different implementation and instead of fruit I'm going to type in vegetable let me just change the name from type since type is a very general keyword in Python so I'll just say type off so now I'll create two objects I'll say obj1 and obj2 and by using these objects I'm going to invoke both these classes so I'll say obj1 do typeof and then obj2 type off now I'll just run this code so as you can see over here even though I have two functions with the same name my objects are able to access the right function and return the output you can also have polymorphism along with inheritance polymorphism like I've already told y all defines method in a child class that have the same names as the method in the parent class so as discussed in inheritance the child class inherits the methods from the parent class also it is possible to redefine a method present in the parent class in the child class this process of reimplementing a method in child class is called as method overriding so now let's take an example of that I'll just define a class called as bird and within this I'll Define a function called as information now within this I'll just keep a general print statement that is to say that birds birds are very pretty so now I'll create a sub class of this class I'll just say class and I'll specify the bird as parrot I'll inherit the bird class over here and I'll Define a function and I will redefine the function information here I'll just print parrots can fly so now I'll just create an object of the Paris class and I'll say obj do information so as you can see over here when I've called the function information which is present in both the base class and the inherited class the function present in the inherited class has been executed so that means I have redefined or overwritten my method that is present in the base class and that method has been executed so that was all about polymorphism now let's move on towards encapsulation encapsulation deals with binding up the data together with the code that is used to work on that data this not just makes our code clear but it also helps in preventing data from being modified as well in oops a classic example of encapsulation is a class A Class encapsulates all the data and the code such as variables methods Etc data that is encapsulated can have three types of access specifiers in oops these access specifiers are protected private and public members having Public Access specifiers are visible throughout the code protected members are visible just within the class and the subclasses whereas private members are visible only inside the class in which they are present even the inherited classes will not be able to access them so one thing I want you all to know of here is that python in real has nothing such as privacy model it does not have exclusive access to modifiers such as C++ C or Java therefore there are no truly private or protected attributes in Python now I'm sure you'd be thinking then what is the use of preceding single and a double underscore in Python members that have names with the preceding double underscore are mangled in order to protect them from clashes with the inherited classes sub classes are also allowed to Define their own private members which will not interfere with those of the parent class this is why they are considered to be private they are accessible outside the class but less is the chance of a clash mangling in Python is achieved by prefixing any such name with an extra unders and the class name this in turn provides an effective name space so in case I have a class named parent so I'll just say hash parent and if this parent contains a member with a double underscore so I'll just say underscore double uncore private member at the compile time this private identifier is going to convert to underscore parent double uncore private member so as you can see over here the name has been mangled so therefore in case I try to access this private member anywhere outside the class by just making use of a double underscore followed by the private member name I'll get an attribute error similarly if I have a child class I'll just say child and if this child has a private member then at compile time it's going to be something like underscore child double _ private member so I hope you guys are getting this the names of members proceeding with a double underscore are mangled such that you have a underscore followed by the name of the class and then the name of the member that has a double underscore so what I'm going to do over here is I'll define a class say a and within this I'll call the init method through this init method I'm going to invoke the so-called private method so therefore I'll have self doore High the name of my method is high I'll declare another method called as hello world so this method is just going to print hello world so here I've declared a method named high but I've not defined it so I'll just Define underscore High sorry double underscore High to which I'll specify the self parameter again and I'll say print welcome to edura now I'll create an object of this class and first by making use of this object let me try to call the hello world function so as we all know this object will be able to access the hello world function so I'll just execute this so as you can see over here I have both the outputs which is welcome to edura and hello world as you've seen I've not called this function which contains a double underscore but I still have the output to it now if you guys are wondering why the output is seen now this is because I'm calling the double uncore High method through the init method as I've already told yall the init method is always executed whenever I create an object of this class so in case I do not write this method so I'll just delete this from here and then if I execute this problem you'll not be able to see the output for the high function in case I want to invoke this function I cannot directly use obj do high I'll have to type in obj doore aore high and then execute this so I hope you guys are clear with this the name of high has changed from double uncore High to underscore a double uncore high so what happens when you have a single underscore go let me take another example to explain this to you all so I'll just say class base and then I'll Define a single underscore class I'll just say single and I'll just print saying single underscore next I'll Define another class called as double underscore or just double and then I'll print a message saying double underscore now I'll create another class which is the child class of Base Class and through this class I'll try to access both the functions present in the base class so here I'll just type in self do the name of this function which is single sorry I forgot to define a function over here soorry I'll just say Define function one with a self parameter I'll Define another function with the self parameter and then I'll say self. double so now let's create an object of the child class so I'll say obj equals child and I'll call obj do function 1 and function two so now let's see what happens so as you can see over here the function having a single underscore has been inherited and executed by the child function whereas the function that contains a double underscore throws an error saying child object has no attribute named underscore child double uncore double so in case you want to execute this all you have to do is just type in the name of The Base Class preceded by an underscore so I'll say underscore base and then I'll hit run so as you can see now both the functions have been executed using the object of the child class so I hope you guys are clear with this now moving on towards the final topic of this session which is data abstraction in Python so abstraction basically means to show the necessary data and hide the implementation details this not just increases the efficiency of the code but it also reduces its complexity for example consider your washing machines you know what happens when you press certain buttons such as on off save water set water level change program Etc but the actual implementation of these programs is hidden from you you do not know how it's being turned off or turned on you do not know how it knows to take less water or more this is called as data abstraction abstraction can be achieved by creating abstract classes any class that contains an abstract method is called as an abstract class so what exactly are abstract methods abstract methods are those methods which just have names but do not actually contain any function definition so that means that you declare an abstract class but you do not Define it these functions are also called as unimplemented methods therefore an abstract method does not have any body in Python this can be achieved by making use of the past keyword however an abstract method needs to be redefined in a subass where it's being used to achieve abstraction in Python you can make use of the ABC module that is the abstract Base Class module so to show an example of this I'll get back to my Jupiter notebook I'll just type in a heading so the first thing that I'm going to do is import everything that's present in the ABC module so I'll say from ABC import Star so this is going to import all the necessary functions that are present in the ABC module I'll create a class over here called as Bank within which I'll have a function that describes the name of the bank I'll have another function that is going to find the location of the bank but this function is going to be abstract or I'm not going to have any function definition next I'm going to define a child class called as location through which I'm going to redefine the abstract method I'll just define a class sorry class bank so the bank class is going to inherit the ABC class in case you want to know what this ABC class does I just add in a kernel over here and I'll say help on ABC so as you can see this is a helper class that provides a standard way to create an ABC using inheritance so now moving on I'll Define two functions having Bank name and the self parameter I'll just print the name of the bank I'll say HDFC and then I'll Define another function called as Bank location I'm not going to provide the body of this so I'll just write pass now I'll create a child class I'll say Bank uncore location this is going to inherit the bank class and then I'll redefine the bank location function I'll print the location as bang finally I'm going to create an object of the bank location class I'll just say obj equals Bank location and I'll call both the functions so as you can see both the functions have been executed [Music] successfully what are commments no they are not something there just to increase the size of your code but guys they are indeed very meaningful comments are programmer coherence statements that describe what a block of code means and they get very useful especially when you're writing large blocks of code say for example you've developed some software previously and now you're working on something that's new and completely different at this point of time you find that your previous software is throwing some error you would have completely forgotten what you did and how so this is where comments come into picture a good code actually consists of relevant comments these commands actually increase the readability of the program not just to the programmer but to the others as well now that you know what are commands let's move on and see when to use them the best practice is to write comments as and how you proceeded with your code comments are very useful but only if they are implemented wisely just keep the following points in mind when you're commenting your code make sure your comments are very precise and clear and preferably short as well they need to be as specific as possible to the block of code they are included with please make sure you use decent language guys and do not repeat your comments as they become redundant now that you know what are comments and the importance let's see how to write them in Python comments in Python are preceded by a hash character as you can see on the screen over here I have a small example with some two lines of code my first line is a comment and my second line is a print statement now let's move on towards a Jupiter notebook and see how this works now let's move on towards a Jupiter notebook and write some comments All I'm going to do over here is I'll open a new python notebook and I will rename this notebook to rename your not notebook guys all you have to do is click on the title give whatever title you want and use rename like I've told you before comments start with a hash character in Python so here I'm going to write a commment preceded by a hash as you can see my output there is nothing related to the comment line and whatever I have printed in the print statement has been return learned as my output now this is how comments work they do not show up in your output now let's get back to our presentation and see how they are interpreted which is exactly what is our next topic whenever The Interpreter encounters a hash symbol it omits whatever is present after that the hash character actually tells The Interpreter to stop reading whatever is present after that until the end of that line so when you're writing commence whatever is present after your hash character is going to to be omitted by your interpreter now what if the hash character was present inside a string let's go to our Jupiter notebook and see what happens I'm including a hash character within a string and I will run this as you can see when I run this the string is returned which means a hash inside a string means a hash itself and nothing else so you cannot write comments within a string okay so I hope you've understood how to write comments now let us move on ahead and see what are the types of commments comments can either be single line or multi-line single line comments can either appear individually or in line with some other code multi-line commments have to be preceded by a hash character in every line they appear now let's see a small example of single line comments like I've told you before single line comments can appear either in an individual line or in line with some other code now let's move on towards a Jupiter notebook and write some single line commments I'll just create a heading over here to create a heading all you have to do is go to code select heading give whatever heading you want as your heading and prefix it by a hash a single hash tells that it's H1 level heading two hashes tell that it's H2 and so on you can use the heading level of your choice here I prefixed my Heading by a single hash which means it's of level H1 now let's write some single line commments as you can see over here my first line of code is a comment line I've prefixed this line by a hash character and in my last line I have an inline comment this inline comment is present with some part of the code as you can see in the output none of the comments have been returned learned now let's get back to our presentation and see how multi-line commands work like I've already told you multi-line commands can appear anywhere in your code but each line needs to be prefixed by a hash character for example as you can see on your screens I have a hash character present in the first two lines of my code after that I have some code followed by it you can see in the output that none of these lines have been returned now let's go to our Jupiter notebook and write some multi-line comments as you can see over here I have three initial comment lines and all three have been prefixed by a hash after that I have some code and you can see in the output that none of the comment lines have been returned and whatever code is present after that an output is returned based on this code now typing a hash everywhere might be a problem and many of us would not like to do so let's look at some Shortcut methods of how to comment multiple lines at once all you have to do is type your multi-line comments first hold the control key and left click wherever you want to insert a hash character just like how I'm showing you all and then type it once and it appears wherever I have inserted the cursor as you can see on the screens now in case you want to remove multiple hash characters all at once do the same thing hold the control key click on wherever you want to remove the hash character from and press the backspace key as you can see from two lines I removed hash character in a single go so these are some Shortcut methods to comment multiple lines many of us think that comments and Doc strings are same now let's move ahead and see what are doc strings and whether they are actually comments or not doc strings are not actually commments but they are documentation strings these strings are written within triple quotes they are not assigned to any variable and many of times they are used as comments as well now you be thinking what is the difference between dock strings and commands dock strings are not omitted by The Interpreter unlike commands comments are omitted by your interpreter and nothing present after the hash character is going to be read by it on the other hand doc strings are strings that describe something about the code doc strings actually tell what some function is going to do whereas the comments will tell how it is going to do so this is the difference between dock strings and comments now let's go to our Jupiter notebook and see their functionality like I've told you before doc strings are written within triple quotes as you can see in the output when I execute this it Returns the string itself whereas in case of a comment when I execute a comment it does not return anything which means The Interpreter does not omit doc strings whereas it omits the command now these doc strings are efficiently used when you want to describe something about a class or a function or something else now these dog strings can either appear in your output or they can be permitted based on where you're going to place them let's try to place a doc string initially before the code starts as you can see in my output the doc string has not been returned whereas when I use the doc string individually without any code and I executed it you could see that it had returned the string itself but when I include the dock string before the code and after after that I've included some code lines and I run this you can see that only the output for my code is present and nothing related to the doc string has been returned as my output now let's try to include a doc string after some code as you can see over here when I included the doc string after the code it has returned the output for the code as well as the string itself so when you're making use of Doc strings just be specific of whether you want it to be returned in your output or not many of times these strings are called rather than writing it again and again so I hope you've understood what are commands and Doc strings as [Music] well so let's try to understand what exactly is an operator an operator is used to perform operations between two values or variables now we can call the operators as constructs or special symbols that are used to manipulate the values of the operant now you must be wondering what is an operant so the value that we use in an operation are known as operant these operant can be variables or any data type that we have in Python so let's take an example here so we have two operant operant one and operant two now when I add an operator between them we can get the result according to the type of operator that we are using here so before moving on to the types of operator that we have in Python let's briefly discuss variables and data types in Python the purpose of discussing variables and data types is to get an idea of what exactly is variable and data types because while explaining the different types of operators we are going to use these variables and data types as well so variables are nothing but the memory locations where we store a value and these values we may or may not change in the future according to the properties they possess there are six data types in Python namely numbers string list dictionary tupple and ass set now let's take it up to Jupiter notebook to understand these data types briefly talking about numbers or numerical data types they mainly hold the numerical values for example a variable let's say x is going to hold the value that is 10 so this is going to be a number or a numerical data type now in numerical data types also we have four types of data types first of all we have integer which is going to hold the whole numbers then we have float data type which is used for declaring decimal point values and then we have complex numbers which are used to declare imaginary part values for example we use the J as imaginary part over here and then we have a Boolean data type which is used for categorical output that is true or false so when I print the Boolean number over here it is going to give me the value as true or false so this was all about numbers talking about strings now strings are used to represent the characters or Al alphabets we can use the single orable codes to declare a string in Python also strings are immutable in nature which means you cannot change a string once you have declared and to access a value in a string you can use the index values over here so when I use the index number two it is going to give me the value at the index number two here so this was all about strings guys talking about list list is a sequence which is ordered and changeable which means you can change values in a list to declare a list you have to give a name for your list and we use the square brackets to declare a list in Python list is also indexed just like a string so we can use the indexes to access the values from a list and we can store different values in a list as well for example I can use numbers over here give duplicate values as well and I can use strings over here as well so when I print my list it is going to give me the value like this now talking about dictionary dictionary is a sequence just like a list but we use the key Val value pair in a dictionary I'll show you guys to declare a dictionary we use the curly brackets now I'm going to give the key value pairs over here so let's say this is my key 1 I'll give a semicolon now I'll give the value over here I'll give one more key value let's say key number two so when I print my dictionary over here I'll get the values like this now since it has a key value pair it is not indexed which is why we can use the unique key values as an index to access the values in a dictionary so what I'm going to do is I'm going to write the index number as my key value and it is going to fetch me the value at that particular key so let's talk about set guys a set is also a sequence which is unordered and not indexed which means you cannot access the values using the index number so I'll just write a few values over here some duplicate values as well so when I print my set over here it is going to give me the values like I have mentioned over here and it is not going to hold any duplicate values in a set also we use the curly brackets to declare a set now talking about topple tupple is immutable in nature which means you cannot change a value in a tupple once you have declared and we use the round brackets to declare a tle in Python so I can give as many values as I want over here even the duplicate values as well so when I print my tle over here I'm going to get these values vales that I have declared in the Tuple so the difference between a tle and a set is not index and it does not contain any duplicate values but a tle can contain duplicate values and it is index as well so we can declare duplicate values in tuples but not in the set so this was a brief discussion about variables and data types guys if you want a detailed discussion about variables and data types you can check a video on adura platform where we have discussed variables and data types in detail where we have also discussed various operations that you can perform on a data type moving on to the different types of operators that we have in Python so here is a list of all the operators that we have in Python so first of all we have arithmetic operators then we have assignment operators comparison operators logical operators then we have membership operators identity operators and bitwise operators so we will be discussing all of these operators in detail so let's talk about arithmetic operators arithmetic operators are nothing but the operators that are used to perform arithmetic operations between variables or two values so these are the arithmetic operator symbols that we use in Python so we have addition subtraction multiplication division modulus exponentiation and floor division so let's take it up to jupyter notebook to understand the concept of arithmetic operators so let's say I have two variables X I'm going to give this value as 10 and then I have one more variable say y I'll give it a value let's say 20 now to understand the concept of arithmetic operators what I'm going to do is first of all I'll try to perform addition between these two variables so I'll just write plus over here and when I execute this I'm going to get the addition as my output now to perform subtraction I'm going to use the subtraction operator and it is going to give me the value as the subtraction over here similarly I can perform multiplication then I can also perform exponentiation so I'll use two as tricks instead of one for exponentiation it is that easy in Python guys now we will perform division over here so it is going to give me the value of the division now when I do the floor division I'm going to get the value over here as well one more operator is left that is the modulus so I'll try to perform modulus as well so it is going to give me the value that is the remainder so this was all about arithmetic operators guys so these are all the symbols that we can use for various purposes like addition subtraction multiplication division Etc I hope you are clear with the concept of arithmetic operators now let's try to understand the next operator that we have which is assignment operator so you can imagine what an assignment operator is by the name itself that is assignment so we use the assignment operators to assign values to a variable or any value that we have in Python right so we have assignment operators like is equal to then we have plus is equal to so all these symbols we have let's try to understand what all these are used for for example let's say we have a variable X which has a value 100 now when I write this x + is equal 10 it is going to mean the same as x = x + 10 similarly we can write xus is equal to multiplication is equal to division is equal to or exponentiation is equal to now let's try to understand these operators or the assignment operators in Jupiter notebook so first of all we have is equal two as our first assignment operator so I'll have one let's say I have X so I have not assigned any value to this let's say what happens okay let's try one more variable I'll write this so it is showing me a name error which is saying name a is not defined because I have not assigned a value to it so in Python until you assign a value to a variable it is not going to be declared in Python so now when I give the value to this variable let's say I'll write five over here it is not showing any error which means I have assigned a a value to this variable now let's try to understand other assignment operators let's see so what does plus is equal to mean in Python so when I write this what should be the output guys so let's try to understand this so when I print a I get the value as 10 so why I'm getting this value because I have assigned the value five over here then I write one more assignment operator that is plus is equal to 5 so what is going to happen over here is it is going to be same as a is equal to a plus plus 5 so we have five over here and we have added five more to this statement which means we are getting the result as 10 over here now let's try to understand other assignment operators as well so let's say exponentiation is equal to 5 so let's see what the output will be so when I print this I'm getting the exponentiation to the value of five since a is equal to 10 and we are doing the exponentiation until five digits so we are getting the output as 100,000 so this was all about assign assignment operator similarly we can do the division is equal to then we have minus is equal to so these are all the assignment operators that we have in Python now let's talk about the next operator that we have which is comparison operator so comparison operators are nothing but the operators that we are using to compare two values or objects right so we have comparison operators like equal not equal greater than less than greater than or equal to and then we have less than or equal to as well so let's try to take an example over here so when I use the equal operator I'm going to write the statement as just I have written over here x is equal to Y so when I'm using the comparison operator I'm actually using double equal to so this is the basic difference between assignment operator and comparison operator so let's take it up to jupyter notebook to understand these comparison operators guys so let's say I have two variables value is equal to 10 and number is equal to 20 so to understand the comparison operators let me take one more variable I'll say this will be our Boolean variable so what I'm going to do is I'll write Val is equal to number one what I'm checking is or comparing these two values so what exactly I'm doing is comparing these two variables or values that we have that is Val and number one so when I print this compare variable what I'm going to get is either true or false so it is going to be false because value is not equal to number so I can do the similar thing I can check whether it is greater than or I can check if it is less than so when I write 10 over here so I can similarly check if it is greater than or equal to I can check if it is less than or equal to so these are the comparison operators that we have in Python which we can use to compare two values or two objects now let's try to understand the next operator that we have which is The Logical operators so logical operators are used to combine conditional statement so what are the conditional statements that we have in Python so let's briefly talk about conditional statement guys so mainly we have three conditional statements in Python we have an is if statement then we have an else if statement and we have an lse statement so I'll give you an example for better understanding so for these two variables only I'll write if well is equal to number one print equal else if well if is greater than number one print greater else print smaller so what happened here is guys first of all the execution will go to the if statement it will check this condition if this statement is true it is going to print this value if this is false it is going to move to the next statement that is the Els if statement that we have over here it is going to check this condition if it is true it is going to print this statement or if this condition is false it is going to jump to the next statement that is the else statement and I'll give one more condition so if all of these conditions are false it is going to jump to this statement and it is going to print not there over here if all of these conditions are false it is going to jump to the next statement that we are going to have in the program but I have not written any other statements so it is going to end the execution over here only so this is a basic example using the conditional statement ments now that we are done discussing conditional statements let's talk about logical operators so I have told you logical operators are used to combined conditional statements so what exactly are The Logical operators so we have a logical and we have logical or or we have a logical not as well by looking at the example let's say we have two conditions over here we have X is greater than 5 and then we have X is greater than 10 so let's try to understand these logical operators in Jupiter notebook now let's say I have two statements I'll write X is equal to 10 and what I'm going to do is I'm going to check if x is greater than 10 I'll write and which is my logical and operator and I'll write X is greater than 5 so what is going to happen if I use the logical and operator if both of these statements are true it is going to give the result as true but if one of these statements is false it is going to give me the value or the output as false so let's see see what happens if I execute this statement I'm getting false because one of these statements is false now when I write instead of 10 if I write eight over here let's see what happens since both of these statements are true it is going to give me the output as true now instead of and operator if I use the logical or operator let's see what is going to happen it is showing me that it is true because when we are using the logical R operator only one of these statements has to be true the output will be true but what happens if both of these statements are false it is showing me the value that is false but let's see what happens if both of these statements are true I'm getting true as well so one of the statements has to be true when we using the r operator now talking about logical not operator what I'm going to do is I'll write not in the beginning now I'll specify the condition let's say x is greater than 10 and X is greater than 5 this is the same statement that we have used before and for this we have got the output as false so let's see when we use the logical not operator what is going to be the output I'm getting true as the output here because this statement is negated over here using the not operator and we are getting the opposite value that we should be getting for this statement so this is the purpose which fulfills by using the not operator or The Logical not operator that we have in Python so now that we are done with logical operators as well let's try to understand what are identity operators so identity operators are used to compare objects now you must be wondering what are objects here so everything in Python is actually an object so we can use the data types as objects as well so let's try to understand what are the identity operators in Python so we have is and we have is not so these are the identity operators so what is does is it returns true if both the variables are same object for example let's try to do this in notebook so what I'll do is I'll take let's say a list I'll give it some values let's say 10 20 and 30 I'll take one more list give it the same values let's say 10 20 and 30 and I'll write X is equal to list one so when I use the identity operators let's say if I write X is list one let's see what is the output that I'm going to get I'm getting true because X is actually the same object as list one now let's try to write list one is list two even though when we have used the same values in list one and list two let's see what the output will be it is false because these are not the same objects right similarly I can use list one is not list two so here I'm going to get the output as true because list one is not exactly list two even though we have the same values it is not going to be the same object so this is the concept about the identity operators now let's talk about the next one that we have is membership operators in Python so membership operators are used to check if a sequence is present in an object or not now to understand the difference between an identity and a membership operator okay let's first understand what are the operators in membership operator we have in and then we have not in so it is going to return true if a sequence with the specified value is present in the object and it will return true if the sequence is not present so let's do this in Jupiter notebook we have already specified these objects so let's try to do if x in list one let's see what happens it is going to show me that it is false because we have no values in X so when I do the same thing with list one in list two now you see we have the same values over here let's see what the output will be it is false again okay let me do one thing now to check a particular value or a particular sequence what I'm going to write is I'll check if 10 in list one so it is going to check if this is going to be present over here or not it is showing me that it is true so what if I write like this 10 20 30 in list two it is false so this is the basic difference between is that is then identity operator and membership operators so when I write let's say it is going to check if this is present in the sequence or not it is showing false because it is not there but what if I add this value over here let's say now when I execute the same statement I'm getting true because this sequence is actually present in the object now to understand the difference between e equal to and the identity operator let's see what is going to happen what if I write list 1 is equal to list 2 it is showing false I have made a change over here so I'll do this once again so when I print this it is showing that it is true but instead of is equal to if I write is over here it is going to show me false because these are two different objects so this is the basic concept of identity and membership operators guys now let's understand bitwise operators so bitwise operators are used to compare binary values or binary numbers so this is a rather difficult concept guys so I'll be telling you about what are binary numbers first and then we'll be moving on to the bitwise operators that we have in Python so these are the bitwise operators like bitwise and bitwise or so these are the symbols used for these operators guys so this is actually what happens when we are using these operators so let's try to understand bitwise and operator first in Jupiter notebook so what I'm going to do is I'll just write 10 and 12 so what is going to happen over here let's say it is showing me the output as 8 but it is not clear why we are getting this output right so let's understand what 10 is in binary numbers so when I write 10 in binary numbers it is going to be 1 0 1 0 and similarly for 12 it is going to be 1 1 0 0 now when I use the logical and operator what it's going to do is if both the bits are one it is going to be one so in this case only first bit is one over here so the output will be 1 0 0 0 and if we convert this to decimal it is going to be the number eight so so this is the concept between the binary numbers guys similarly I can use 10 or 12 so what is happening here is I have these values let's say 1 0 1 0 for 10 and 1 1 0 0 for 12 now looking at first bits here that is 0 0 it is going to be one so all of these bits in the output will be 1 1 1 1 which is exactly is equal to 14 so this is the concept of using the bitwise operators where we are using the values as binary numbers we are comparing the binary numbers now let's talk about left shift and right shift guys so this is what I write for doing the right shift so 10 in binary numbers is 1 0 1 0 so when I right shift these two bits I'm going to be left with 1 0 and when I convert this to Binary I'm going to get the value as two so this is the concept of right shift guys now let's try to understand left shift so when I write this I'm going to get the value as 40 now let's try to understand how I getting this value so 10 if I write it in binary is going to be 1 0 1 0 so when I do the left shift what is going to happen is it is going to shift two values and I'm going to get the value as 1 0 1 0 0 0 so what is happening here if I convert this binary number to decimal I'm going to get the value as 40 so this is why I'm getting the value 40 over here so this is the concept of bitwise operators guys I hope you clear with the concept of bitwise operators it is a bit complicated because we are using the binary numbers even though I'm telling you these binary numbers you need not mention them while using the operators for example for these bitwise operators all these are written over here let's say if you're using the bitwise and operator it will set each bit to one if both bits are one as I've shown you and then for bitwise or operator it is going to be one if one of the bits is one so similarly for XR it will set each bit to one if only one of the bits bits is one and then we have bitwise not operator which is going to invert all bits let's say if we have all bits that is one it is going to convert all those bits to zero and then I have told you about left shift and right shift as well so this is about operators in Python [Music] guys let's understand what exactly is a tle in Python a topple is an immutable data type in Python and it is almost similar to a list in Python in terms of indexing which means we can access the elements of a toule using indexes and they can also have duplicate members just like a list as well and it is a collection data type that stores python objects separated by commas and let me show you an example which will explain how we can create or declare a tle in Python so let's take it up to jupyter notebook guys as you can see now we are in the jupyter notebook and I have a file name tupple in Python over here so I'll just show you how you can declare a topple in Python so I'll just take a variable here and inside this let me get a few values inside round brackets so I'll just go with 1 2 3 4 and 5 because this is a very basic example guys and when I print a over here this is my tle guys which is separated by commas and inside the round brackets but there is one more approach to this where you can actually declare a tle like this so you simply have to mention some values separated by commas and when I print a again I'm getting the same value as I was getting before so I can follow both of these approaches to actually declare a tle in Python so this is how we can create a tupple in Python let's take a look at the next topic that we have which is accessing elements in a topple so accessing items in a tle Works similar to a list and we can access elements in a list using indexes and we can specify the index value and it will return the item that is stored at the particular index value now talking about indexing it is a data structure technique to effectively retrieve the information from a data structure and in Python several data type support indexing like list a string Etc so what happens is let's see for example we have a tle with five natural numbers that have 1 2 3 4 and five so the indexing will start at the value zero where one will be stored and it will go until the end of the topple that is the number five and the index value at number five will be four so in Python we can use the negative indexing as well to access in a apple or any other data type that supports indexing so let me take it up to jupyter notebook guys and I'll show you how you can use the index values to access elements in a topple so we have a over here so inside the square brackets I'll just specify the index number let's say four over here because we have five elements so it should ideally you know give me the value number five okay I have an index error which says index out of range because we have only three values over here so I'll be getting to this also like reassigning the value vales to a tle so I'll just write two now I'm getting the value 30 over here because in my topple that is the variable a we have values 10 20 and 30 so the index number at 10 will be zero so similarly at the element number 30 we'll have the index value as two so we're getting this output now talking about negative indexing so if I write minus one over here what will be the value I should be getting I'm getting the same value as 30 so when we talk about negative indexing it will start with 30 so the negative index minus one will be at 30 and for 20 it will be min-2 and similarly minus 3 so this is all about negative indexing now let me talk about slicing guys so slicing is basically a technique in which we use the slicing operator which is colon and it is used to get a range of elements from a tle or any other data type that supports indexing for accessing elements so let me just show you an example this time I want the variable to be distinct and I'll just specify the numbers as let's say okay these numbers from 1 to 9 so when I print this I have a tle which has values ranging from 1 to 9 now talking about slicing it works similar to indexing only it's just that we'll have to add that slicing operator so I'll start at the index number okay zero and then I want to go until the end of the topple so when I print this I'm getting all the values inside the topple so in this tle the ending index is basically eight so I'll just write eight over here and we getting the values until the index number eight so you can see that number nine is not included here so when you're are doing a slicing operation the index number that you write after the slicing operator will not be included in the out similarly we can use the negative indexes as well I'm getting the same values and I can use values like these also so I'll just side - 8 the output is the same so this is all about slicing guys now let's take a look at the next topic that we have which is changing a topple so even though tles are immutable in nature but a nested object in a tle can be changed or in general a tle in Python can be reassigned with a different value so I'll just show you the example how we can do that so talking about the python n objects let's say I have a tle which has values 10 20 30 and inside this I have a list as well which has values 40 50 and 60 so this is my topple when I print a my tle looks like this so the first three elements in the tle are 1020 and 30 and then the fourth element is basically an object which is a list now what I'll do is I'll try to reassign the values inside the tople so I'll go to the third index which is the list and inside the list I want to change the value that is 50 to let's say 55 so I'll just specify one over here and I'll write 55 and now when I print this as you can see I have actually changed the value inside the list guys now let me try to do this with other elements so let's try to change 30 to 35 let's say so I'll specify the index number two let's say I'll write 35 I'm getting a type error which says tle object does not support item assignment so it's basically a way around this error that we will get like the type error if you are trying to change item inside a tole but for these objects the nexted python objects you can actually reassign the values very easily now let's take a look at the next topic that we have which is concatenating two tles so it might sound a little overwhelming but joining two tles is a very easy task so you just have to assign the addition of the two Tes to another variable and it will return the concatenated tle with values of both tles so let's say you have a topple with the variable a and then you have another variable B with another topple so what you'll do is you will just assign one more variable let's just say C and inside this you can do the addition of both A and B and you'll get the resultant as a concatenated topple so I'll take it up to jupyter notebook and I'll show you how you can do this okay we already have a and b so I'll just take one C variable and inside this I'll just do a plus b no errors and I'll try to print C now as you can see I have both A and B inside one tupple so I have actually concatenated both the tles inside one topple so this is how concatenation works with topples guys so now let's take a look at the next topic that we have which is deleting a topple so being an immutable data type A tle in Python does not allow any changes and you cannot even remove an element from topple after the Declaration but there is a keyword which is De and which will delete the whole topple all together so I'll just show you guys so we have a tle c over here which is the concatenated tle so I'll try to delete this I'll just write Del C no errors now when I try to print C over here it's not there because we have deleted the whole tle over here so this is how deletion works with a tle guys you cannot delete just one element from a tle instead you can delete the whole tle all together so let's just go to the next topic which is topple methods so there are not many methods like list or you know other data that we have in Python there are basically a count method and an index method so count basically Returns the count of the items and index will return the index of the item specified so let me show you this with an example so let's just say I have a tupple named a and inside this I have values let's just say one one one one I have written five ones over here because we can have duplicate items inside the topple and let's just say 2 3 4 and five no errors now when I print my eight here I have this tle now to show you how you can use these methods you just write a do count and inside this I'll just write one and it will return the number of times one has been written inside the topple which is six similarly the next method that we have is index so to check the index value at a particular element I'll just write five over here and it will return the value of the index at that particular element but what if there is a duplicate member so I'll just write a or index I already know that one is written multiple times inside the dle I'll just write one over here so it's going to return zero because it will return the index value of the first item that is being encountered even though we have the count as six for the element one it will give the index value as zero only so this is all about the tle methods let's take a look at the next topic that we have which is list versus topple so I'll give you a few different points so first of all a list is used for homogeneous data types and a tupple is generally used for heterogeneous data types a list is mutable in nature and tupple is actually immutable in nature which helps in Faster iteration as well so tupple is basically faster when it comes to iteration like you're working on for Loops or any other Loops for iteration statements so if you're using any iteration statements tle is much faster than a list and a list does not have any immutable elements but but immutable elements can be used as a key for a dictionary while we are using a topple last but not the least we have no guarantee that the data is right protected inside a list but if you're using a topple implementing a topple with data that does not change guarantees that it is right protected so these are a few basic differences between list and topple so let's take a look at the next topic that we have which is the tle Constructor it is possible to create a tle using a tuple Constructor as well I have shown shown you the two different approaches you can follow to create a topple but there is a topple Constructor which we can use to actually create a topple so let's take it up to jupyter notebook again I'll show you how you can use the top Constructor okay let's just say we have a list guys and I have these values 1 2 3 so this is my list and when I print my list over here I have the values 1 2 and three so using this topple Constructor I can actually change the list into a topple guys so when I run this no error now when I print B over here it will have all the elements inside the list but this time it's a tle so we can actually use a tle Constructor to change a data type for type casting and it can be used to create topple as well so this is all about topple Constructor let take a look at the next topic which is a few other examples so I'll be showing you two examples which is iterating through a topple and a membership test on a Apple so let's take it up to jupyter notebook first of all I'll iterate through a topple so let's just say I have a topple guys with a value until 9 and let's say for I in a print I so this is how you can iterate through a apple using a for Loop or you can use any other loop as well and this is a very basic example let's take a look at the next example which is a membership test so for the membership test you can use the membership operator which is in and in Python we can check whether an element is present in a tle or not so the following example is going to show you how you can check if an element is present in a tle or not so we'll use this a only so let me just try to print this a okay we have the same Tuple I'll just print it basically so I'll check 9 in a which is true and I'll just try to check if 10 in a which is false and I'll check if a string in a and I forgot to tell you guys it's not necessary that you have to use the integers only you can use the string or any other data type inside topple like we have used the list data type as [Music] well what is a set a set is basically a data structure guys which is is a collection of unordered elements all elements in a set are unique which means every element is present just once now these values can be of any data type but they are not indexed and because of this you cannot perform indexing operations such as slicing on sets so now that you know what are sets let's move on and see when to use sets in Python sets can be used when the order of data actually does not matter but the only concern is your data elements need to be unique so now let's move on to see how to create sets in Python to create sets in Python you can use two methods the first method is to use the curly braces and the second is to use the set Constructor I have a small example where I've created a set a using curly braces and a set B using the set Constructor now let's move on towards a Jupiter notebook and create some sets in Python the first thing I'm going to do over here is change the name of my notebook you can give any name of your choice and rename it like I've already told you you can create sets in two ways the first to use curly braces and the second to use the set Constructor let's just create some sets using both these methods I've created a set a using curly braces and then I've specified some random values to it you can also notice that all these values are of different data types after that I've created a set B using the set Constructor this set Constructor has converted a list of values into a set hope you're clear with how to create sets now let me just show you all how to create create an empty set using the set Constructor all you have to do is give some name to your set say for example C and use the set Constructor without passing any values to it and hit run now when you print this set you can see that the set is empty it has returned an output which is an empty set so I hope you're clear with how to create sets in Python now let me just show youall what are all the methods you can use along with sets to do so you can use the dir method and Supply the the name of the set as a parameter to it when I used Di and passed a as a parameter which is a set that I've already created it has returned a number of methods as its output so all these are the methods you can use along with sets now let's get back to our presentation and see what are the operations we are going to perform in this session today first of all we'll be finding the length of a set and accessing its elements after that we'll be adding or updating elements of a set and then removing and deleting elements from it following that we'll perform the mathematical operations that are union intersection and difference of sets now let's move on towards the first operation of this session which is finding the length of a set to find the length of a set you can use the Len method the length of a set is the number of elements that are actually present in that set and this Len function will return an integer value which is equal to the number of elements present in the set I have a small example where I've created some random set with the name my set and I've passed some values to it after that I've used the Len function and pass the name of the set as a parameter now let's go to our Jupiter notebook and see what happens when I do this I've already created set a b and c I'll just reprint my set a over here so that it's easy for us to refer to it now let's find the length of a when I used Len of a it has returned 7even as the output which is equal to the number of elements present in my set a now let's get back to our presentation and see how to access elements of a set since set contain unordered elements you cannot use index values to access its elements but you can Loop through it to access the elements of a set I'll just jump on to my jupyter notebook and show you all how to access set elements I'll reprint my sets A and B over here I've used the for Loop to access the elements of set a all I have done is for every element present in a print that element and as you can see in the output all the elements present in a have been returned one after the other now let's get back to our presentation and see how to add elements to a set to add elements to a set you can use either the add method or the update method now this add method will allow you to add a single element to your set but the update method will allow you to add a list of values to your set I'm going to be showing you all how to use both these methods in this session so here is my Jupiter notebook when I use the update method I've passed a list of values to be added to the set C but here I've passed three again and three has been added just once and it's not been repeated again this is because sets contain unique elements and there is no no second copy of one element present in a set so I hope you've understood how to add elements to a set now let's move on to understand how you can remove elements from a set to remove elements from a set you can make use of the remove discard or the pop methods the remove method takes one parameter which is the element to be removed from your set now in case that element is not present in the set this method is going to throw an error the question is what if you're not sure if some element is present in your set or not but you want to make sure that this element is not present and your program does not throw an error in this case you can use the discard method the discard method takes one parameter which is the element to be removed and in case that element is not present in the set this method will not throw any error now moving on towards the pop function the pop method removes a random element from your set now let's get back to our Jupiter notebook and use these methods I'll reprint all the three sets over here first I will use the remove method I've used the remove method and I've specified 2.4 to be removed from the set a when I print the set a 2.4 is no more present in my set now let me try to pass some value that is not present in the set a when I use a do remove six which is not present in the set a it has thrown an key error and it's specified that six is the key which is not present in the set a so I hope you've understood how to use the remove method now let's use the discard method and see what happens when I used b. discard 3 it has removed three from the set B now let's try to give some parameter to the discard method which is not present in set B when I used b. discard 90 it has executed but it has not thrown any error so I hope you're clear with both these methods now let's try to use the pop method when I used a. pop it has popped some random value from the set a and it has returned it as the output over here one is no more present in the set a so I hope you're clear with how to add and remove elements from a set now let's move on to our presentation and see how you can find the union of sets Union of sets refers to the concatenation of two or more sets into a single set but in case you have any common values in these set the resulting set will contain just one copy of the common element you can perform the Union operation in two ways One is using the pipeline symbol and the second is using the union method I have a small example where I've created a set a and b and then I've used the pipeline symbol and the union method to find the union of these two sets let's go to our Jupiter notebook and see how this works I'll reprint my sets a b c over here I've used the pipeline symbol to find the union of A and B all the elements that are present in A and B has been returned as a new set which is the union of A and B but the common element are just printed once now let's see how to use the union method to perform this operation I've used the union method to find the union of A and B now let's try to find the union of three sets using both these methods as you can see in the output I've used the pipeline symbol to find the union of three sets and then I've used the union method to find the union of a b and c so I hope you're clear with this now let's get back and see what is intersection of sets the intersection of two or more sets is the formation of a new set consisting only of the common elements that are present in all these sets you can perform this operation in two two ways One is using the ersin symbol and the second is to use the intersection function I'll be showing you all how exactly to do this on our Jupiter notebook I've used the essent symbol to find the union of A and B and you can see over here the elements that are common between the set A and B are four and a b c hence the output now let's try to use the intersection function I've determined the intersection of a b and c using the intersection function and the resultant is a string which is present in all these sets so I hope you're clear with both these methods now let's get back to our presentation and see what is the difference of sets the difference of sets produces a new set which contains all the elements that are present present in one set except the common elements to make you all understand this concept clearly let me jump on to my Jupiter notebook to find the difference of set you can use either the minus symbol or the difference method first over here I'll be using the minus symbol when I found the difference of a minus B I've got all the elements that are present in a but not the common elements of a and b when I used B minus a a set has been returned which contains all the elements of B except the common elements of b and a so I hope you're clear with what is the difference of sets now let's use the difference method to do the same I've used the difference method to find a minus C and as you can see all the elements present in a but not the common elements of a and C have been returned as my output you can use the difference method to find the difference of three sets as well when I found the difference of A- B minus C using the difference method only one element has been returned which is present in a and not present in B and C hope you're clear with this now let's move on towards our final topic of this session which is the concept of a frozen set a frozen set is a set whose values cannot be modified which means when you freeze a set it becomes immutable to create Frozen sets in Python you can use the Frozen set method as you all know I've created a set a just make a note guys that you cannot add or remove values from a frozen set as you all know I've created a set a which is a normal set and I've been using it to add and remove values from it now let's freeze this set I've created a new set D which is a frozen set and I've supplied the values of a as a parameter to this Frozen set Constructor now let's see what happens if I try to add some values to this Frozen set when I used d. add3 it has returned an attribute error which says Frozen set object has no attribute at therefore Frozen sets are immutable and you cannot change the values present in the Frozen set Frozen set helps to serve as a key in a dictionary [Music] so what is a dictionary in Python it is a collection data type just like a set or a list but there are certain features that makes dictionary unique so let's take a look at the features of python dictionary guys first of all it is unordered and we can change values in a dictionary as well since it is mutable also it has key value pairs which is like a map that we have in other programming languages and it is indexed as well since the keys are distinct and can be used as indexes to access or change any values in the dictionary a dictionary does not have any duplicate members as well now that we know what dictionary in Python is let's talk about why we use a dictionary in Python first of all the reason would be it is unordered and stores data like a map so it is one unique feature that python dictionary has and it also contains the key value pairs unlike any other data type or object in Python it is almost similar to a real life dictionary where we have distinct values and corresponding to these values there are respective definitions in case of the dictionary even though there are no duplicate members we can mention duplicate members in the values but the keys has to be unique in order to access these members since there are no indexes also we use the keys as indexes when accessing these elements now that we have learned about why we use a dictionary let's understand how it is different from a list in pth so I've listed down these differences between a list and a dictionary so first of all list is ordered but the dictionary is not dictionary is actually unordered guys the next one is list has indexes but in order to access elements in a dictionary we use the keys as indexes the next one would be the list is collection of elements but in dictionary we have the key value pairs as Elements which is unlike any other data type or object in Python the next one is list is often preferred for ordered data but dictionary is preferred when the data has unique key values so this actually differentiates a list from our dictionary guys and the next one is List have duplicate elements as well but in dictionary we do not have any duplicate elements when we are declaring the keys although we can have duplicate members when we are specifying the values for each key so these are the differences between a dictionary and a list guys I hope you are clear with the difference between those two now that we have understood what a dictionary is in Python and why we use it and how it is different from a list let's try to understand how we Implement a dictionary in Python so first of all to declare a dictionary in Python we have to use the curly brackets just like you can see in the example here I have the dictionary with the name my dictionary and inside those I have the key value pairs inside the curly brackets so let's take it up to pycharm guys to understand how we Implement a dictionary in Python let's enter the presentation mode guys so we will try to implement a dictionary here let's take the name of the dictionary as a so inside the curly brackets I can give the key value pairs so I'll give one as my first key so when I am specifying the key value pairs I have to use the colon to separate the key from the value now I'll give one value over here now when we are declaring a dictionary we have to separate these key value pairs with a comma now I'll give another key give it a value let's say data science I'll give one more key and give it a value let's say python so this is my dictionary guys this is how you actually implement dictionary I have declared a dictionary over here now let's try to understand how I'm going to access these elements so I'll just write a inside the square brackets I'll specify the key value so I'll put it inside a print statement guys now as you can see when I run this I will get the value that is specified in the first key value which is Eder now I can simply just print all of my dictionary guys so this is how I'm going to get the output as when I print the dictionary that I've just specified now to access these elements I can also use the get function and inside the get function I'll just specify the key value and when I run this I will get the same output as I was getting using the square brackets guys so this is how you access elements inside the dictionary now I can also replace values inside the dictionary guys since it is mutable so instead of data science let's say I want machine learning so what I'll do is I'll just specify the key value and I'll specify the value that I want to replace it with now when I print this you will see the replace value inside the dictionary as you can see here I have replaced the value inside the dictionary guys now if I want to add one more value inside a dictionary what I'm going to do is I'll just specify the key value that is let's say four because we don't have four over here so instead of machine learning I'll add the value data science that I have just replaced now when I print my dictionary here let's see what all values I will get I will get one more value that I have just added which is data science so this is how you actually Implement a dictionary guys and to access these values you can either use the square brackets and inside those square brackets you can specify the key value or you can just use the get function and inside the get function and inside the get function you have to specify the key value as well so now that we have understood how we can Implement a dictionary in Python let's try to take a look at the operations that we have in Python dictionary guys so as I have told already told you how you can actually add an element to your dictionary just like this I have specified a key value and given it a value and then there is a replacing element which I have already shown you guys so these are all the operations that we have in Python dictionaries so we will look at them one by one guys so let's take it up to pycharm and understand these operations guys so so we'll once again enter the presentation mode so we have a dictionary over here guys so first of all let's talk about clear so when I use the clear what it's going to do is it is going to remove all the elements from the dictionary so when I run this so I have a dictionary guys that I already specified so let's try to print the dictionary first so you will understand what all these operations are going to do so I have this dictionary which has these values all right so I'll do one thing I'll use the first operation that is clear let's see what it does to our dictionary guys when I run this okay so now when I print my dictionary let's see what all values I'll get it is saying that it is empty because the clear function has actually deleted or removed all the elements from the dictionary so this is what clear does guys now let's take a look at the another one which we have also I would want to declare this dictionary once again and when I print this I should be getting all these values again yes I'm getting these values so it would be fine if I do one more thing so I'll just use the copy over here so what copy does is it returns a copy of the dictionary guys so let's say when I print B over here what is going to happen it is going to show me the copy of this dictionary that I have just specified over here using the copy function so let's take a look at the next one we have which is the values so when we are using the values function what it's going to do is it is going to return all the values inside the dictionary guys so when I run this it will print all the dictionary values inside a list which is Eda data science and python so we'll look at the next one we have which is the update function so what does update function do update function actually updates the values of the dictionary with the specified key value pairs so let's say if I use update just like this what will happen we have no updates which it is showing that we haven't updated anything so this is what update does guys if we have made any updates it will update the changes into the dictionary so we'll look at the next function that we have which is actually get function I've shown you how to use the get function we'll just use the get function to get the values and inside this I just specify the key value and it will return the value with the specified key which is actually Eda so this is how you actually access the elements using the get function next one is the items function guys so I'll just use items over here and inside this I will get the list for a tle of each key value pair guys so let's run this so when I run this I'm getting tle of each key value pair as you can see over here I have two then I have the value then I have one more key so this is a toule of each key value pair which we can get using the items function now coming back to the next one that we have which is the keys function so what it will do is it will actually return all the keys inside the dictionary that we have which is 1 2 3 guys so this is all about Keys function guys so let's talk about the pop function guys so when I use the pop function what it will do is it will actually pop the value with the specified key so I'll just pop this from the dictionary with the value three so as you can see I'm getting that value over here guys now let's take a look at the next function that we have or the operation which is pop items guys so when I'm using the pop item function I can actually remove the last inserted key value pair so I don't have to specify any any values over here as you can see I'm getting the topple of the last key value pair I have specified inside the dictionary so this is what pop item does now the next function that we have is set default guys so what set default does is it Returns the value of the specified key if the key does not exist we can insert the key with the specified value so I'll specify the value over here let's say one now when I print this I'm getting Eda as well so when I specify the next key it is showing me okay it's a syntax error I'm getting the next value so this is what set default is used for which Returns the value of the specified key guys so this is all about the operations that we have in a dictionary guys there is one more thing to discuss about the dictionary guys which is the dictionary Constructor so the dictionary Constructor is actually used to declare a dictionary guys so let's take it up to py Cham again so I'll show you what dictionary Constructor does so I'll use a I'll use okay I'll use my dictionary as my variable dictionary name and I'll use the dictionary Constructor over here I'll specify the values so what I'm going to do is I'll just specify the values I will use the keys as let's say key one only and then I'll give it a value let's say urea now I can give another key value let's say two and I'll give it a value let's say python now let's see if I can get a dictionary from this so using the dictionary Constructor let's see if we have actually implemented a dictionary over here so as you can see I have declared a dictionary using the dictionary Constructor guys so when I run this I should be getting a dictionary here so as you can see I have declared a dictionary using the dictionary Constructor also one more thing I have missed over here when I was explaining the operations guys so which is the from Keys operation so I'll declare one more dictionary over here give it a value let's say one two I'm giving it a random values guys don't be alarmed now when I'm using the from Keys keyword what I do is I'll just say a do from okay we don't have a over here so I'll write a from keys I'll specify my dictionary over here so let's see what happens guys okay I'll so I'll just use the print statement and inside this I will use the from keys and inside this I'll specify my dictionary let's see what happens when I run this so I'm getting a dictionary with the specified keys and Valu so as we don't have any key1 key2 inside this so we not we are getting the nonone value over here so this is what from keys does inside a dictionary guys so now that we have actually learned what is a dictionary and how we can implement it in Python and all sorts of operations that we can perform on a dictionary let's try to understand what our nested dictionary guys so for this I have a problem statement guys so now that we know that ICC Cricket World Cup is just around the corner guys so what we'll do is we will Implement a dictionary with the stats of the 15 members from the Indian cricket squad for the World Cup 2019 and we will use the concept of nested dictionary in the use case so what we'll do is we'll Implement a dictionary first and then we will make the unique keys to access those values when we are actually getting the elements from the dictionary and then we will store the data inside the dictionary and we will also import a pandas package and convert those data into a data frame for better display statistics so let's take it up to py charm guys so okay let's exit the presentation mode so over here I have the value or the dictionary that I have implemented so let's take it up to presentation mode again so as you can see I have this module that I have actually imported from pandas and I have given it an alas as PD so we'll talk about this later so this is my dictionary guys in which I have made four more dictionaries guys which is actually our estate dictionaries so first key is batsman in inside the batsman key I have all those batsmen like RIT Sharma sikar daan and Virat kohle and inside those values I have one more dictionary in which we have the statistics for all those matches runs average highest score so actually inside this batsman key I have two more dictionary guys so this is the concept of nested dictionary so similarly I have made it for allrounder and we have Wicket Keepers and Bowlers as well so this is my dictionary that I have actually stored all the data using the dictionary or as you can say nested dictionary and then I have all these statements that I have written in which I have converted the data inside the dictionary into a data frame guys so I have this print statement so first of all let me print the batsman squad in which we will get the values for all the batsmen that we have inside the World Cup squad so as you can see I'm getting a data frame over here so I have these values we have Rohit Sharma shikar daan and Virat kohle so as you can see we have average highest score matches and runs over here so for Virat kohi we have more than 10,000 runs shikar davan has 5,000 runs and roh Sharma has around 8,000 runs guys so let's try to understand more from the dictionary that I have just mentioned over here we have got all the batsmen now we will try to get all the bowlers so I'll just print this statement and it will show me all the bowler statistics which is Kip yadav Muhammad shami Kumar us chel so we have all these statistics in which we have average we have best figures then we have matches and wickets as well so as you can see we have buhar Kumar with the most wickets which is 118 wickets guys so this is what you can actually do using the nested dictionaries guys as you can see this is an unordered data but we have key value pairs which actually helps in retrieving the data very easily guys so let's say inside bowler I want jped brra or for allrounder I want hardik Panda all these statements I can use it for getting all those values inside a data [Music] frame why we need Loops now let us understand this with an example that is there in front of your screen suppose you are a software developer and you are required to create a software module for payroll processing of all the employees in your office now what all things you need you need the salary of those employees the bonus and the total amount that they'll get after the end of the month right so all these things for each employee you need to print so for each of the employees you need to print all these three details now there are two ways of executing this task so let us see what are those two ways so the one way is you can actually write a logic to calculate the total salary of each of the employees that includes all the fields like salary bonus and total and you are going to write that logic for all the employees that are there in your office they might be hundreds of employees that are there in your office so you need to write the logic or you need to write the code for all of those employees so this process is actually pretty hectic you need to repeat the same logic for total amount of employees are there in the office I'm just giving you an example of three employees but what if you have like hundreds of employees so at that time this particular process will not work this will not only increase the size of the code it'll make it more complex and less efficient now another way of approaching this task would be you write the logic to calculate the salary you keep on iterating that logic for the total number of employees that are there so when this Loop runs once it'll print the salary of first employee when the loop runs for the second time it'll print the salary of the second employee similarly it will keep on repeating so if you have 100 employees it will repeat for 100 times now what advantage we get here the major advantage that we get here is we don't need to write the logic for each employee in order to calculate the salary we can write one logic and keep on repeating it for all the employees so this way the size of the code get reduced the length of the code is reduced at the same time it reduces the complexity makes it more efficient and even increases the speed of execution so this is just one example in order to show you why we need Loops so there are hundreds and thousands of examples that you can think of why we need Loops right so this is just one simple example that I've shown you in order to make you understand why we need Loops so let us move forward and see what exact L are Loops let me explain you this with the help of the flowchart that is there in front of your screen now what are loops loops basically allows us to execute a statement or a group of statement multiple times let me explain you this with the help of an example that is present in front of your screen now over here what happens the control comes here and checks the condition so if this condition is true it'll go on and execute the conditional code that is here this is nothing but the statements that are present inside the loop so it'll execute that and again it'll go back and check the condition so if the condition is still true then again it'll execute the conditional code or the statements present inside the loop and it will keep on repeating until this condition becomes false and the moment this condition becomes false the control will move out of the loop and it'll execute the statements that are present after that Loop now one thing to notice here guys is that there can be two kinds of Loops one is finite this is actually the flowchart for the finite loops and another can be infinite now in in inite Loops what happen the condition will never be false so at that time the control will never come out of the loop so it will keep on repeating and it will never stop that is what infinite Loops are so basically the condition will never be false and due to that the loop will be executed infinite number of times now there's one more way in order to categorize Loops that is called post- test and pre-test in postest Loops what happen the control will first enter the loop and then in the end it will actually check the the condition but in pre-test Loops the control will enter the loop only when the condition is true so the condition is checked in the beginning of the loop in pre- test Loops but in post test Loop the condition is checked at the end of the loop now in Python there are no post- test Loops present there are only pre- test Loops so we'll focus on pre- test Loops in today's session so we'll move forward and we'll see various type of Loops that are present in Python so python basically supports three kinds of Loops one is vile for and nested we'll look at all these Loops one by one and I'll explain you with examples as well so first we'll have a look at while loop so while loop is basically used when you don't know how many iterations are required so let me explain you this with the help of the flowchart that is there in front of your screen now over here what happens the control will move inside the loop only when this while condition is true and when it is true it'll execute the body of the loop and then again it'll go back back and see whether the condition is still true or not if it is still true then again it will execute the body of the loop and it'll keep on doing that until the condition becomes false and the moment the condition becomes false it will come out of the loop and execute the statements that are present after the loop now this is a sytax for while loop first you need to write while then you write the expression or the condition then you give a colon and the statements or the body of the loop now why we use y Loop basically I've told you earlier as well y Loop is used when you don't know how many iterations are required so you don't know how many times you need to execute the statements that are present inside your Loop so that is the reason why we use y Loops now let me show you that with the help of an example I'll open my pie charm and I'll give you a very basic example of using Y Loop and after that I'm going to increase the complexity of the code so first we'll see a very small introductory example of Y Loop in which I'm going to print the integer values between 0 to 9 that won't include nine so it'll print 0 1 2 3 till 9 but it won't include N9 so let me open my py charm and show you how to do that now this is my py charm guys so over here as I've told you earlier as well I'm going to print the integer values between 0 to 9 but that won't include 9 so for that what I need to do is I need to First Define a variable let me name it as count and I'm going to initialize a value to it that is zero now after that I'm going to use a while loop and I'll write while and then I'm going to give a condition which says that when count becomes greater than n it should come out of the loop all right so now what I'm going to do is I'm going to just type in print Let It Be number colon comma print count right now I'm going to increment the value of count count equal to count + 1 yep now when I come out of the loop I want to print say goodbye yeah looks fine now let me tell you what I've done here I've defined a variable and I've initialized the value zero to it after that I have used a y Loop in which I've given a condition that says that count should always be less than nine because I want to print values between zero till nines which won't include nine let me tell you that so my condition says that the loop should be executed only when the count value is less than nine the moment it becomes greater than nine then the control should come out of the loop after that it's just a general syntax I have given a colon and then I have written a print statement that will print the count value and after that I have increased the value of my count by one and finally when the control comes out of the loop I want to print goodbye so let us see whether it works or not I'm going to run it and as you can see that it works so we have 0 1 2 3 4 5 6 7 and 8 and finally when the control went out of the loop it has printed goodbye this is just a very small small example guys now let me go back to my slides now the example that we just saw was a very simple example so I'll increase a complexity a bit and I'll give you one more example so let us move forward with that so over here what I'm trying to do is I'm trying to code a little guessing game all right so what happens in this guessing game so there will be a random number that will be generated between say 0 to 20 and you need to Guess that number so the number number that you have guessed is less than that random number then it will print number is too small if you have guessed the number that is greater than the random number then it will print that number is too large and the moment you guess the right answer you'll exit the Y Loop and you have won the game basically so it's just a pretty simple example but this is a perfect example in order to show you why we use y Loop because if you notice here I don't know how many iterations are required in order to guess the correct answer so at that time why Loop Loop is a perfect solution to this problem so without wasting any time I'll actually open My Pie charm and show you how to execute this so this is the code here guys let me explain you each and every line that is present here now what happens first I need to import a module that is called random in order to generate a random number now over here I've defined a variable n and I've given a value to it that is 20 because I want the random number that is to be generated should be between the range 0 to 20 now I've taken one more variable and I've named it as to be guested and over here it should be an integer type so I've written in and this logic will generate the random number that I'm talking about and this will be generated between the range 0 to 20 so how it will be generated it'll be generated with the help of random module that we have imported so it'll be n random. random plus one this will actually generate a random number between 0 to 20 so after that what happens I've defined one one more variable guess and I have assigned a value to it or initialized a value to it zero all right so now comes the Y Loop now in y loop I have given a condition which says that the number that I have guessed if it is not equal to the random number then enter the loop now once you enter the loop you need to input the new number the number that you want to guess again now if that guessed number or the number that you are guessing is greater than zero then again there'll be one more if condition which says that the number that you have guessed is greater than the number that has been generated or the random number that has been generated then print number is too large and if the number that you have guessed is actually smaller than the random number generated then print number is too small now there's one more condition that is else which means that when the number that you have guessed is less than or equal to zero then that means you're giving up so it'll break it'll come out of the loop and it'll print sorry that you are giving up that's when your game gets over and you have lost it finally if you have guess the correct number then it will print congratulations you have made it so I hope you have understood the code that I was explaining here so let us go ahead and execute this and see what happens all right so it asks me for a new number so it has to be between 0 to 20 so yep I'll type 10 and let's see what happens so it says number is too small that means it is between 10 to 20 how about 13 all right so I've guessed the correct number and it says congratulations you have made it now over here as you can see that it took only two iterations for me in order to guess the correct number but what if it would have taken more iterations I mean I'm pretty unclear how many iterations it will take for me so that is the reason why we use y Loop because we don't know how many iterations are required now we can run this program again and I can show it to you once more ask a new number so I'll just type in say 14 it says number is too large so I'll type in 11 again the number is too large that means it is between 0 to 11 so how about eight guys number is too small so now now as you can see that already three iterations have occurred and uh for the fourth iteration definitely the random number will be somewhere between 8 and 11 so how about 10 so congratulations you have made it and the control came out of the Y Loop and it has printed congratulations you made it so we'll move forward and see for Loops in Python so for Loop is basically used when you know the number of iterations that are required unlike y Loop where you are not sure about the number of iterations that are required in for Loop you know how many times the statements that are present inside the loops need to be executed so for Loop provid provides you with a syntax that will include three Fields basically or you can say three informations so first will be the Boolean condition then comes the initial value of the counting variable and then the incrementation of the counting variable so as you can see from the syntaxes well first you write for name of the variable in the range then you specify the range in which that variable should be or and then you specify the range that means from which point till which point it should be executed so you know the number of iterations required then only you use for Loop now over here as you can see from the flow diagram itself the control comes to this and it'll see the item from the sequence it'll execute the statement go back again and then from the range it'll pick up the next item again it'll execute the statement again go back and then from the range it'll pick up the third item and it'll keep on executing until the range becomes over or that is the last item from the sequence so when there are no items in the sequence it will go go on and execute the statements that are present after the for Loop so this is how for Loop Works guys now let me just give you a very small example of how for Loop works I'll open my py charm again and I'll start with a very small example and then I'm going to increase the complexity of our code like I've done that in while loop now over here what I'm going to do is I'm going to first Define a list and I'm going to name it as fruits so what all fruits you like guys I'll first type in mango then I like grapes and finally I'm going to write and say apple yep so this is my list which contains three fruits mango grapes and apple now I'm going to define a for Loop and over here what I'll write I'll Define a variable say fruit for fruit in fruits give a colon print current fruit and type in fruit now when you come out of the loop just print again goodbye so I have declared a list and the name of that list is fruits now that fruits list contains elements which are mango grapes and apple now after that I have defined a for Loop which says that for a Vari name fruit in fruits so this will give me the range fruits will give me the range as you know that in the fruits list there are only three fruits so I know it'll iterate only Thrice first for mango then for grapes then for Apple so this the difference between the V and for Loop in for loop I know that it'll take only three iterations after that print the current fruit that's all and when the control moves out of the loop print goodbye it's pretty easy guys so go on execute this and see what happens so yep the current fruit first is mango then comes grapes then comes apple and then finally print goodbye so what I'm going to do is I'm going to increase the complexity now as I've done that in while loop as well so I'll show you an example first let me go back to my slides so what I'm going to do in this example I'm going to calculate the factorial now one thing to notice here guys again in factorial also I know how many iterations are requ because if you see if I'm calculating the factorial of five and I know that I need multiplication five times first I need to multiply five with four then four with three then three with two and then two with one so this way I require only four iterations so I know the number of iterations required so it's a very good example in order to show you where we should use y Loop and where we should use for Loop so let me go back to my Pie charm and execute this practically so this is the code in order to find the factorial of a number so over here what happens first I need to input the number that I want to calculate the factorial of after that I've defined a variable factorial and I've initialized a value to it which is one now I have defined a condition if the number that I've entered is less than zero then print the number must be positive because I can't calculate the factorial for negative numbers right and again I have given one more condition which says that if number is equal to equal to zero then factorial is equal to 1 and then else condition says that when number is greater than zero then I've used a for Loop which says that for I've defined a variable I in range 1 to num + 1 when I write num + one the range will be between 1 to num + 1 but it won't include num plus one so it'll only be including one till num all right so after that what I've written factorial is equal to factorial into I so factorial was one earlier then it will multiply by I I will also be one in the beginning and then again it'll go back and it will become two similarly it will keep on increasing until it becomes num so till that time it'll keep on executing the loop and the moment it becomes equal to num it'll come out of the loop and it will print the factorial value so let me go ahead and execute this and show you if it works or not so I need to enter the number so let's say I want to calculate the factorial of three which is six so if I want to enter one more number so what I can do is I can start it once more so what I'll type I can type again four so it prints 24 which is absolutely correct so we'll move forward and we'll see what are nested Loops in Python so python basically allows us to use a loop inside another loop for example you can use a for Loop inside a for Loop or you can use a y Loop inside a y Loop and at the same time you can use a y Loop inside a for loop as well and a for Loop inside a while loop as well so that is basically what is called nested Loops now we'll have a look at the example of nested Loops as well don't worry so let us move forward and see one example of nested Loops now over here what I'm going to do is I'm going to write a code in order to simulate a bank ATM all right so for that I'm going to use nested while loop and what will happen first you need to enter the four-digit pin and then you can perform the following functions you can check the balance you can make a withdrawal you can can deposit some money and you can even if you don't want to do anything you can tell the machine to return your carard as well so let me show you how it is done I'll open my pie charm once more so this is a code that I've have written in order to simulate a bank ATM I'll explain you each and every line so don't worry about that so first line is nothing but a print statement that says welcome to iron bank of browers ATM all right and then I have defined a variable restart and I've assigned a value to it that is why similarly chances I've assigned a value that is three and this is the balance that I'm going to initialize that is 67.1 4 notice you over here you won't get more than three chances in order to write the correct password now comes the while loop which says that when chances are greater than or equal to zero at that time what it'll do you need to enter the pin so you need to input the four-digit pin that you have and if that pin is 1 2 3 4 then it'll print that you have entered the correct pin now comes one more while loop which says that while restart should not be in this particular tupple that is the value of restart should not be n capital n o small n o or capital N so it should not lie within this particular sequence that is n capital n o no and N but we know that the restart value is y so it'll enter the V Loop now over here what will happen it'll give you four option first option is in order to check your balance for that you need to press one to make a withdrawal you need to press two in order to pay in some cash or you need to deposit something you need to press three or you want your card back you need to press four so these are the four options that will be given to you so when you choose option one it will print the account balance so over here as you can see that your account balance is and the balance that is there in your account after that it'll ask you for restart so you need to input that value so that will say would you like to go back and if your restart value lies in this particular tle then it'll break and it'll print a thanq statement but if you want to continue or you want to go back then you can press anything else apart from small n n o or this no or capital N so if you press any other button apart from these it'll actually go back and in the while statement as you can see it says that it is not present here so it'll give you all these four options once more now after that what if you choose the another option in which you need to make a withdrawal so for that what will happen you need to enter the amount that you would write to withdraw which can be a float number it cannot be an integer number so that's why I've used float here so you need to enter that value and once you enter that value it'll check whether it is in this particular list or not that is 10 20 40 60 80 or 100 Now by default it'll give restart y because you need to enter the amount once more so again it'll go back and it'll give you all four options so you can choose that withdrawal option again and you can choose the number which is present in this particular list now when you choose withdrawal equal to equal to one at that time it'll again ask you please enter the desired amount so this is for option two now when we talk about option three it is pretty much similar to the couple of options that we have discussed before so it is pretty much similar to that over here again whatever the amount that you want to pay in how much amount that you would like to pay and balance plus the amount that you are depositing so it'll print the balance that will be equal to balance plus the amount that you're depositing again it will ask you for restart if you press anything apart from this particular tupple then it'll go back to the while statement and it'll give you all these four options similarly for option four as well now what happens when you enter the incorrect pin so there's one more condition in the end which says that if the pin that you have entered is not equal to 1 2 3 4 then it will print that incorrect password and it will decrease the chances by one so earlier you had three chances if you press the incorrect pin once it'll decrease your chance and it'll make it as two again if you write the incorrect password then again it will make it as chances equals to Chances minus one which will become one and if the third time also you do the same thing then the chances will become zero and the moment it becomes zero then it'll print no more tries break it'll come out of the loop that's all guys so let us go ahead and execute this and see what happens so please enter your four-digit pin so I'll just write 1 2 3 4 and give me four options which is one for balance two for withdraw three for paying four to return a card so if I want to check my balance I'll put in one so it has printed my balance which is 6 7.14 and then the restart value that I was talking about it is asking for that let me just make it big for you yeah would you like to go back so if I press n here it'll come out of the loop and it will break and if I press anything apart from small n capital n o small n o or capital N it'll actually go back and ask gave me all the four options that were there so if I press y it'll again give me all the four options that are there you can see that press one so again if I want to make a withdrawal I'll press two how much amount that I would like to withdraw say I want to withdraw 10 so my balance has reduced from 67.1 4 to 57.14 so would you like to go back so I'll again press Y and it'll give me all four options if if I want to pay in something that is I want to deposit something I'll press three how much amount that I would like to deposit I'll say 10 so my balance became again 67.4 again it is asking me would you like to go back no I don't want to go back so I can type in N so yep so it says thank you and again asking me for four-digit pin so let me show you what happens if I write the incorrect pin say if I write one one incorrect pin if I write that pin again again incorrect again incorrect so we have no more tries it'll come out since I've have exceeded the chances that I was given so I was given three chances I couldn't write the correct pen in those three chances so it'll come out of the loop and it'll stop so this is an example of nested V now let me go back to my presentation and I'll show you an example of for loop as well so this is an example of nested for Loop so over here what I'm going to do is I'm going to write a code in order to print the Pythagorean number between a particular range so so what happens is suppose if I want to enter the number 20 that means I want the Pythagorean numbers between 1 to 20 so Pythagorean numbers are nothing but the integers which satisfy a square + b square is equal to c² so at that time it'll print all the Pythagorean numbers that are possible between 1 to 20 3 4 5 you can see that 3 Square 4 Square isal 5 S 9 + 16 is = 25 similarly it goes for 5 12 13 6 8 10 8 15 17 9 12 15 similarly for 12 16 and 20 don't worry guys I'll go back to my py Cham and I'll explain you how you can do that so this is the example of nested for so first over here I need to import certain modules in order to use the square root function so from math module I'm going to import the square root function after that ask me for the number in the example if you can remember in the slides where I've written 20 so it has printed the Pythagorean numbers between 1 to 20 that same number it will ask me and it'll print the Pythagorean numbers between 1 to n so whatever the number n that I'm going to put in or given input after that it'll run a for Loop and it will say that for a which is a variable in range 1 to n + 1 that means 1 to n that means 1 to n + 1 but don't include n + 1 after that there's one more variable B which is in the range a comma n that will not include n it will only include till n minus1 so the square root c² I've defined one more variable c² is equal to a exponent 2 or a to the^ of two similarly B raed to the^ of two all right and then what I'm going to do is I'm going to calculate the square root and then what I'm going to do is I'm going to Define one more variable C and it'll be an integer that will be the square root of c² so if that c² is minus c² is equal to equal to zero then print a c it's pretty simple logic guys okay I'll explain it once more Neil so this is an example of nested for so over here what I'm going to do is I'm going to first import the square root function from the math module after that I'm going to input a number so if you can remember in the slides I've given it as 20 so it will print the Pythagorean numbers between 1 to 20 over here also it'll print the Pythagorean numbers between 1 to n so whatever the value of n you input that will be depend upon it so the Pythagorean numbers will be depending upon the values that you input so it'll be printing the Pythagorean numbers present between 1 to n after that I'll run a for Loop which says that for a should be in the range 1 to n + 1 that will not include n+ one guys it'll only include n so one till n + one but don't include n + one after that I've defined one more for Loop which says that for B in range a comma n it won't include n it will be tell only n minus1 I've defined one more variable here which is equal to ccore s that is equal to a² + P ² now after that I've defined one more variable C and this will be actually the integer of the square root of the ccore square all right so whatever value that you'll get here the square root of that value will be converted to an integer value and will be assigned to C now one if condition I'll be using here which says that if ccore s minus c² is equal to equal to 0 then print ABC that means that will be a Pythagorean number so this is a pretty simple logic guys if you have understood it just give me a thumbs up or you can ask me any questions that you have in your mind all right so I hope you have understood the code that I've explained you here now let us go ahead and execute this and see what happens yeah so the first number that I'm going to type in say five and it'll give me all the Pythagorean numbers present between 1 to 5 so we have only one Pythagorean number that is 3 45 in that range now I'll execute this once more and I'll type in 10 here so we have two Pythagorean numbers that are 3 4 5 and 68 10 similarly if I execute it once more and I type in a big number say 25 yeah so we have these many Pythagorean numbers present between 1 to 25 we'll again go back to our slides and we'll have a look at one more example now in this example I'll actually be using a for Loop inside a while loop so what is this example I want the bulk reservation of train tickets so I have like group of people who are traveling somewhere and I want to give their details such as name age and sex I want that to happen in bulk I don't want to give it one by one I just want it to happen in bulk so for that what I'm going to use is I'm going to use a for Loop inside a while loop as I've told you earlier it's well you can even use a while loop inside a for Loop but for now I'll use a for Loop inside a while loop and see what happens so let me go back to my Pie charm so this is the code in which I'll be using a for Loop inside a y Loop so first I have defined a variable traveling so in which you need to input whether yes or no so if you're traveling you'll say yes obviously and then we'll have a v Loop which says that while the condition is traveling equal to equal to yes that means if you're willing to travel enter the number of people who are traveling so you need to enter the number of passengers that are traveling and that will be assigned to a variable num now there'll be a for Loop which says that for Num in range one to num + one but it won't include num plus one enter the name enter age and finally whether the person is male or a female and then print all those values now one more thing here is again It'll ask you whether you have missed someone or not so you need to input that oops forgot someone so at that time you can write in yes if you have forgotten someone then again it'll execute the loop again It'll ask you the number of people traveling and all these details if you rpe in anything apart from yes that means you have not forgotten anyone and it'll come out of the loop so it is a very small example but a very good example to understand how for Loop can be used inside the while loop so let me go ahead and execute this whether you're traveling I would say yes I am traveling number of people traveling I would say three name is sorab age is 78 male all right so again it has asked me for one more name so I'll say Neil age is 90 male or female male yep so one more name I need to type in say I would type type in geography ages 98 male or female female and yep it'll print all those details for me so now it is asking have you forgotten someone so if I type in and say yes then it'll again Ask me the number of people traveling that means it is again executing the while loop so if I would say the number of people traveling is one and I would type in the name of the person that was travel so I would just say sidart whose age is 77 and he's a male y again it is asked me so if I type in anything apart from yes say if I type n so yep it'll come out of the loop and this is how you can actually use a for Loop inside a while [Music] loop why we actually require functions so guys here I have for you a really simple use case consider this program right so let's say you actually write a program in which you input a Celsius number let's say a temperature of course and this must be converted into fahrenheat multiple number of times what would you do we first obviously start with the logic right so fahrenheat is equal to 9x 5 into the particular Celsius number + 32 so this is the logic to calculate for Fahrenheit well here's the program to calculate for the fahrenheit right so we collect the input from the user we enter the Celsius value again the value in Fen has been converted and it has been printed well it is really good right so this has been done once but what if you actually want to do it multiple number of times well let's say you have 1 Celsius value then definitely it is really easy right so let's say you have 3,000 5,000 let's say you have one lakh values that needs conversion in real time as well so when this happens you wouldn't want to repeat the same lines of code right well this is the number one cause of inefficiency and ambiguity guys well I'm sure you already would have guessed it right so python function functions definitely solves this let us check out how it can be done well just before that let me give you another instance we have something called as dry well dry is don't repeat yourself in the sense when you're actually writing your own functions or when you're writing any methods make sure that the reusability of it is really high functions not only reduce the code in your main program but they also tend to make your code run faster and better so what does this do this actually increases the readability of your code and then makes it very easy for debugging and documentation as well consider this easy example right so every time you turn on or turn off your television there are certain functions built in right so as soon as you flip the channels or say adjust the volume there isn't anyone in the back end writing a code at every point of time to actually adjust the volume or flip the channels for you there is a particular code which repeats itself and doesn't create any new instances when you actually adjust the volume or flip the channels correct so this is again very good efficiency so as soon as you change the channel there is a function which runs which says that the user has changed the channel so let me do these certain set of codes again similarly for the volume as well so if you have increased the volume or decreased the volume the function gets an input of what you're actually doing and then this function actually works on your input and then decides an action in this case it's volume pretty simple right well now that we know why we need functions right so let us check out what functions actually are so what are functions guys I'm pretty sure everyone of us has an idea at this point of time so head to the comment section below and let me know what you think of it I'd love to interact with you guys there so coming back what are functions guys functions are actually a way to break problems or processes down into smaller and independent blocks of code straightforward right so it's basically a block of organized reusable code that is used to perform some similar task in our last case in television it was either add the volume or change the channel well coming back to python it can be called by its name name when it's task need execution you can also pass the values to it and have return results to you do not worry I'll be walking you through a range of functions so you'll have a better Clarity at this point of time well at this time all I want you guys to do is to know about the def keyword guys def stands for Define and this keyword is actually used before the name of the function and its name is followed by the parenthesis before a colon well this defines an entire function check out this particular example code I have for you so we have def function underscore name and this function doesn't nothing and pass definitely right so we haven't passed anything there for the function to do anything well def is the keyword here function underscore name can be anything it can be my name an it can be edu Rea it can be anything that you like right it's as simple as that so let me quickly introduce you to another concept called as doc string so guys what is Doc string it's actually called as a documentation string so it's basically the first string after the function header is called right so this particular thing is called as the doc string again as I mentioned is short for documentation string check this particular example out right so we have print greed doore doore what does this do well this function greets to the person passed into the name parameter so if there's a name parameter in the documentation the function actually greets the person which is passed by the name simple now check out this important flow I have on the left side for you guys again we're going to come back to this but right now you need to know this we actually first Define a function followed by the function name we have certain set of code right in the function or away from the function or anything well good we actually wrote the code until now well later when we actually need to use it we actually call the function by its name as you can see Define function name will actually Define that particular function and what that function actually should do to call the function and by calling the function I mean to actually make use of the function we just use the name of the function so here instead of Def function name in the second line of code we have function name so this is where we'll actually be using the code so this sort of flow is actually important and I want you guys to note this at this point so now that we're good with the basics next up I want you guys to check out the different types of functions available in Python so guys there are two main types of functions in Python first one is built-in functions and the second is user defined functions well as you might have already guessed it built-in functions are already available with python natively and user defined functions are something which are defined by the users right so there are a number of built-in functions for almost all the users guys so for the sake of Simplicity in this tutorial I have considered the buil-in functions which are actually part of the Native python package well with python as you already know there are n number of libraries so each library has multiple functions on its own so as soon as you go into the depth of one single Library you'll get access to all of their functions as well and in user defined functions again as the name suggests you can Define whatever you want so basically let me give you a quick simple example right so consider the example of a calcul calculator so let's say 3 + 5 is equal to 8 right so let's say a + b equal to C you've written the function you're going to pass the values a plus b and then you have a function which automatically gives you the value of c and you don't have to write a plus b equal to C all the time so we are basically trying to reduce the amount of effort put in by the user and increase the readability as well so there is absolutely nothing to lose when you make use of functions and its advantages from the start to the end and it's all advantages from start to the end so let me quickly walk you through some of the major built-in functions which are available in Python so guys the first function I have for you is the absolute function I am sure many of you can guess what the absolute function already is but let me walk you through this function right so first I have the definition for you so basically the absolute function Returns the absolute value of a specified number well that's confusing what do we mean by absolute value consider this particular syntax and example well this syntax is ABS of n here ABS is the keyword and N is the parameter pass so n can be anything right so it can be zero it can be 100 it can be anything it has to be an integer here guys that is really important to note so here all we trying to do is we're trying to return the absolute value of a number so consider the example right so we have a complex number here x equal to Absolute of 3 + 5 J well what does this do so this basically calculates a number for us which is actually a complex number in this particular case and then provides the absolute value for it so let's say instead of a complex number we had absolute of Min - 7 what do you think was going to be the answer right so it's going to be seven obviously so in this case it was minus 5.83 and since it's absolute minus has been removed and it's here so what does the answer to x equal to Absolute of -10.5 can you guys take a guess well definitely it is 10.5 right so there is no negative in terms of absolute simple and straightforward here the parameter which is n it is actually a number so make sure you don't put any string input such that you can avoid all the errors so the first function is done so let's quickly move on to the second one so guys the second function I have for you is the all function well the definition actually says this the all function returns true if all of the items in an iterable are true otherwise it returns false but what are the iterable items that we talking about right so it can be lists it can be tles and it can be dictionaries as well right but you need to note one thing about the all function so if the iterable object is actually empty then the all function will still return true so let's quickly take a look at the syntax and the example and the output as well so syntax all of ible you already know that the all here is actually a keyword check out the example we have a list here right so my list is equal to true true true and X is equal to all of my list well it's basically telling us that all of the elements present inside our list is actually true and it matches so as soon as you check out the output it say true why because everything is true this is the same case for lists stles and dictionaries as I mentioned so make sure you know that as well pretty simple right so we already covered two functions in such a short amount of time let us quickly jump into the third one so this third function is a very interesting function guys so it is asky I am pretty sure many of you actually know what aski is right so aski actually stands for American Standard code for information interchange and all it does is it is a character encoding standard guys so it is basically used by computer to actually understand so we understand a to zed and 1 to, or one to million right so how can computers make sense of this information in their lowlevel language is exactly through asky so what does the asky function do in Python well the asky function actually returns a readable version of any of the objects here objects are strings tles and list or anything right and the asky function will actually replace any non asky characters with Escape characters so if it's an asy character well and good but if there is no asky character it'll actually Escape that particular character guys so here's the syntax check it out we have aski of object again aski being the keyword and object being the parameter so what is the object parameter here right so it's a string it can be a list it can be a tle it can be a dictionary and anything you want in your particular instance check out this simple particular example here so we have an x equal to aski and my name is tal the a here is spelled very differently and it does not have an asq value map to it right so if it was just stale then you would actually get the aski output well since here it is not a valid aski character python actually puts an escape sequence there and it actually escapes that particular thing and prints out the rest of the thing simple right well I'm going to assure that all of the functions are actually as easy as this and then it would make your life easier to know what each of these function and at the end of the day it'll make your life really easy knowing what all of these functions do so on that note let me quickly move to the next function so the next function I I have for you guys is bull well bull actually stands for Boolean can you quickly take a guess again cool I'm sure you would have guessed it by now the bull function actually Returns the Boolean value of a specified object well it will actually return true unless it meets empty parentheses or empty brackets or if the object is false if the object is zero and if the object is none as well so all these cases are when it can be false but any other time it'll actually return true so let me quickly walk you through the syntax B of object bu being the keyword object being the parameter so what is the object in this particular case it can actually be string it can actually be list it can actually be number or anything right so check out this particular example X is equal to B of one so as soon as that happens since it doesn't match with any of our false criteria we got it as true simple right and now that we are actually done with the bu function let us quickly check out our next function next function is enumerate and this is definitely interesting guys so the short form of enumerate actually stands for enum but in Python we actually use enumerate so in case you're coming into learning python from an other different language say C C++ or Java or something you would realize that enumerate here is the same as inum there again let me quickly show you the definition of it so the enumerate function actually takes a collection again a tle is a collection right and it takes this as input and returns it as an enumerate object the enumerate function adds a counter as the key of the enumerate object let me give you some better Clarity on this check out the syntax right so enumerate and it has two parameters instead of one so one is iterable and the other one is start what is iterable so we already checked out what iterable objects are and then we have something called a start well start is actually a number we're trying to define the start number of the enumerate object well if you don't put anything it'll be zero we'll check out this particular example right in front of them later as well but this point of time all you need to know is this python starts from zero and the first object there first iterable object object there actually starts from zero so Apple Maps to zero and so on from there guys simple right and now that we are actually done with the enumerate function let us quickly check out the format function so what does the format function actually do so guys the format function actually formats a specified value into another specified value well that was a bit complex right so let me quickly simplify that for you guys so check out the syntax we have format and we have two parameters value and format well a value can be a value of any format guys and then we have something called as format guys I'm talking about the parameter here and not the keyword so basically we have multiple parameters guys so we have lesser than so lesser than basically left aligns the result and then we have right align and this actually aligns the space then we have is equal to so this basically places the sign to the leftmost position and we have minus so this is actually used for a negative value as you already know it then we have b b actually converts it to Binary C converts it to UNI code D to decimal F to a fix Point number G to General point and so on guys and if you want more it is basically o for octal lowercase x for hex uppercase X for uppercase hex format n for number format percent for percentage format but in this particular case check out this example we X is equal to format of 0.5 and percentage right so 0.5 in percentage is what so it is basically 50% right so it's is basically half so 50% is your answer another function done so what is the next function I have for you guys guys the next function is actually get attributes so basically it is shortly called as get at again a keyword so what does this function do guys the get ATR function actually Returns the value of the specified attribute from a specified object again looking at the syntax we have get ATR we have the object attribute and the default parameter the object is compulsorily required because we need to know what we're working on right so this is a compulsion and then next we have the name of the attribute right so basically it is a name of the attribute from which you want to get the value from and next we have default guys default is optional here so what does it do basically it is the value to return if the attribute does not exist so in case there isn't any particular attribute to return to it returns to this default value check out this really simple example I have for you guys so we have a class called person name equal to John age equal to 36 country equal to Norway X is equal to get at attribute of person and age so at this point of time what we're trying to do is move into the class person find me the age of this particular instance so in this particular case John's age is 36 and that is exactly what we see in the output pretty easy right and now that we're done with the get attribute function let's see what's next so this is an interesting function guys so I have the identity function for you guys so guys this is an interesting one the identity function also for short it's called as the ID function so what is the definition of this function guys the ID function actually returns a unique identifier for any specified object so all the object in Python has its own unique identification right so the ID is actually the object's memory address and it will be different for each time you run the program check out the syntax and the example guys so ID of object as you can already tell ID is the keyword object is the parameter here the object can be anything guys it can be a literal object it can be a string it can be a number it can be a list it can be a class or anything as such check out the example X is equal to apple banana Cherry Y is equal to ID of X well this particular object created has the identification as this so when you actually perform operations on this particular object on X and then python basically tracks it in the background using this particular identification guys so every object in Python is actually mapped to a particular unique identifier it may not be the same if every time but what is important to know that it actually has a unique identifier so if I probably ran this program twice and then showed you the same output it would actually not be the same because another identification number would have been provided so identification function is as simple as that guys so let us quickly move on to the next function so the next function I have for you guys is the length function and it is shortly called as Len so guys the length function basically Returns the number of items in an object so consider this right so when an object is a string the length function actually Returns the number of characters in that particular string check out the syntax so we have Len of object right again object is definitely a mandate here because we need to know what we're trying to find the length of right so either it must be a sentence it must be a string or it must be a generic object or it can be a sequence or even a collection as well so check out the easy example I have keed for you right so my list equal to hello so we have one word here right so it consists of five letters so as soon as you print X equal to Len of my list it realizes that this is a string and it prints the number of characters in hello there are five characters and as you can see the output is five well length function done guys the next function I have for you guys is the map function can you quickly take a guess on what the map function actually does so guys the map function basically executes a specified function for each item in an iterable sequence so this basically tells us that the item is sent to the function as a parameter right so we need to know what we actually doing here so check out the syntax map is the keyword and we have two parameters which is function and iterable guys so function is definitely required because we need the function to execute for each particular item right and then we have iterable well in this particular function again iterable is also a compulsory requirement because we need to know right we need a sequence or we need a collection or an iterator object or something to work on so you can basically send as many iterables as you can and just make sure that the function has only one parameter for each iterable quickly check out the example and the code and I'm pretty sure you guys will figure it out on your own so basically we have defined my function of n again defining the function name name of the function is my function my funk and then the parameter is n what we actually going to do is we are actually going to return the length of our input parameter check out the next line x equal to map of my funk and then we passing it three values apple banana and Cherry right so what do you think is the length of this so each individual object can be iterable right so Apple has five characters hence five banana and Cherry has six each so six characters each again pretty simple and widely used guys so this function actually helps you a lot when you're actually trying to map something to a key or when you're practically trying to make use of this so now that that's done let me quickly walk you through the next function so guys the next function I have for you is the Min function Min or minimum right so this function basically Returns the value with the lowest value or it returns as an item with the lowest value in a particular iterable so if the values are strings then alphabetic comparison is actually done let us quickly check out the particular syntax here so we have Min of N1 N2 N3 right so it can be any number of numbers and then we can have another syntax actually right so what do we do if it's an iterable so instead of N1 N2 and N3 or so on we're going to have Min of iterable guys as simple as that but check out the example here for Simplicity sake we have x equal to Min of 5 and 10 so it's as simply as saying what is the minimum of five and 10 so which value is minimum here five right so if I had to give you another example what is the minimum of th000 and 10,000 so whatever is the minimum number is output right so in that case it was actually th000 so it is as simple as that right so I don't think I need to explain more about this and you guys got a pretty good idea about it already so moving on next I have for you guys is the p w function p w stands for power so it's basically exponentiation here guys so the function actually Returns the value of x to the power of Y well in this particular case X raised to the power of Y guys so there are two parameters present right so what is the base and what is the power so if there is a third parameter then it Returns the X to the power of Y and the modulus of Zed quickly check out the syntax and the example guys so we have three parameters in this case right so it is pretty simple to pick up two parameters so let's say what is 2^ 3 the answer is 8 correct so what is 4^ 3 again it is 64 if you actually required the modulus of this answer then that would become our Z parameter in this particular case since we haven't done it 4 part three remains to be 64 and that is the exact output that we get simple right it is as simple and as easy as that guys so next up I have for you is the print function this is probably the first thing that you would learn as soon as you start Python and this is definitely not the last thing and this is definitely not as easy as it sounds so it basically prints the specified message to the screen or any other standard output device well most of the time it is the screen but you can actually do it to other devices as well so print so print we have objects then we have separators and we have files and we have flush and we have so much around here right so let me quickly walk you through all of these in a simple way so objects basically refers to any object right you can have any number of objects you want all of these will be converted to a string before it is printed and then we have separator guys guys separator is actually an optional parameter and you specify how to separate objects if there is more than one object present the default separator is actually double quotes and then we have end well again another optional parameter so we use this to basically specify what to print at the end the default is actually sln because again we're going to move to the next line as soon as something is printed and then we have file all of these are optional guys so basically file and flush everything is optional here as well so file is basically an object with the right method that's it but what if there isn't anything well the default of the file well the default default parameter of the file object is actually sis. STD out so it is the system standard out guys and lastly we have flush well again flush is another optional parameter as I told you and this is basically a Boolean specifying if the output is true or false well here is a computer science term for you guys if the output is true we call it to be flushed or if the output is false we call it to be buffered what is the default output it is buffered so what is buffered buffered is false so the default output is false guys again probably the most widely used example ever I have for you guys is printing hello world so print our separator is the double quotes and we're printing hello world again I hope no more explanation is needed for this particular function guys so moving on we have the set attribute function guys so this function basically sets the value of the specified attribute of a specified object again object here is required because we need to know what we're working on the attribute is required because we need to know what attribute we have to set and the value right so we need to know what value to what attribute we set so if you already remember we actually did the get ATR right so we actually outputed a function and now we're going to basically set it so check out the simple code I have for you guys we have class person and then name equal to John age equal to 36 country equal to Norway same as the previous example right so here we're using another syntax at ATR of person age and 40 all we're trying to do is instead of John having his age to be 36 change the age of John to 40 so go into the class person look for age so whatever is there change it to 40 as simple as that again this is a fairly straightforward function guys and next up I have for you is the sorted function what does this do well the sorted function actually returns a sorted list of the specified iterable objects you can actually specify the ascending or the descending order as per your requirement the strings are actually sorted alphabetically and the numbers are sorted numerically as simple as that but you need to know that you cannot actually sort a list that contains both string values and numeric values so you either can do it for numeric values alone or string values alone got it right so check out the syntax so we have something called a sorted and we have three parameters right so iterable key and the reverse iterable is definitely required because we need to know what sequence we need to sort either if it's a list dictionary or a tle key is optional guys key and reverse are actually optional here the key is basically a function which is used to decide the order well what if you don't give any order default is actually none and then we have reverse so what is a reverse guys again it is an optional function as I told you and it definitely is a Boolean function so false will actually sort it to be ascending and true will actually sort it to be descending default is false so default is actually sorting via ascending method so check out this particular case I have for you right so we have a set of values for a we have b g a d f c h e whatever right so X as soon as we print sorted of a the default value is false so in this particular case what happens is that so it actually orders it for us in the descending order so the values actually went from being gibberish to be neatly sorted as shown here again another easy function done so this brings me to the last built-in function for the session so I have the type function for you guys so the type function basically Returns the type of a specified object so again check out the syntax we have type which is the keyword we have object basis and dictionary right so the first one is required we need the object here because we need to know what we're trying to find out the bases and the dictionary are actually optional so bases actually specifies the base class and the dictionary actually specifies the name space with the definition of that particular class well it is not that you need to worry about this right now but then definitely this particular and definitely this particular parameter which is Dick is actually useful later but for now since I'm introducing functions to you let's stick to the basics so check out this particular example I have a equal to apple banana and Cherry b equal to hello world and c equal to 33 so as soon as you try to find out the type of a b and c you can realize right so the first one as soon as soon as you look at it it's a tupple second one as soon as you look at it it's hello world and third one is definitely an integer right so ABC respectively so again as easy as that guys so those were a lot of functions and I actually recommend that you guys pause the video at every point of time and make a note of all of the important stuff that I've said here guys so this will actually take you a long way in learning python well now that we're actually done with the built-in functions again let me quickly give you a recap and then start with userdefined functions guys so what are user defined functions again as I said we'll be taking the code first approach here so here I have for you guys two simple syntaxes so the first one is how we can actually create a function and the second one is on how we can actually call the function so we've actually looked at this when I previously told you but let me quickly tell it again so again Define my function define is the keyword my function is the name of the function so what what do I want this function to do I wanted to print hello from a function so this is actually the body of the function and as as soon as the function has been called it's going to print hello from a function well instead of print I could actually make it to calculate something for me or print my name or print urea's name or anything as such as well right so the body of the function is completely left to your freedom but make sure you check the number of parameters you require and all that you need right so now quickly look at how we can actually call a function so we have defined my function and then hello for function as well to call this particular function all you need to do is call the last line guys so basically we need to type out my underscore function and you good so how more simpler can you expect python to be right as simple and straightforward as that so then again parameters guys so parameters are the information passed to functions that you already know so let me quickly step up the complexity of our examples to one little Notch higher right so again check out so we have defined my function and F name here F name is actually the parameter of us so we're printing F name plus refs nness so what we trying to do we're trying to pass so this is our function body so next what we actually trying to do is we're actually trying to to print it so we have mycore function we've provided three names there we have ml we have Tobias and we have Linus right so as soon as you go ahead and check out the output on the right side so that is actually concatenated to ress right again simple so you can have any number of parameters and then work on all of these parameters and get whatever return output you want guys and get whatever meaningful return output that you want so what is the default parameter value that we stumble upon all the time again check out the example here so we have defined my function country equal to Norway so I'm specifying in my parameters that the country is equal to Norway so no matter what you do right so in case you haven't provided any value the default value it should actually consider is Norway so we're printing I am from and then concatenating a value of the country we've called the function four times in which the first time we have said Sweden so it's printing I am from Sweden the second time it's India so I am from India third time we have just called the function without passing any parameter to it right so this is the exact case where it moves to the default value since nothing is provided it is actually printing I am from Norway and then fourth one my function of Brazil again it is as simple as that you've already provided the parameter so print it as it is so default parameter value you got all of this right and then the important concept you need to know is the return value guys check out the example we have my function and X is our parameter we're returning 5 into X so basically as soon as we pass a number five is going to be multiply to that number and we going to get the output my function of three 5 3 is are 15 15 so next we have five 5 5 are 25 25 is correct 95 are 45 again we have 45 as the output so we're good with that right so let me quickly recap on how the function actually works so we have one set of code and we actually keep using it in recursion so recursion is basically a function calling itself so check out this particular example I have for you guys we have defined a function we call it tror recursion we have a par parameter K so if K is equal to0 then add the result to whatever is lesser than the next iteration of this function I know it sounds complex but how you can actually analyze a recursive function is to take a pen a paper so to write down the values of K at every point of time and then to actually Trace what is available in the memory guys so if that is the case and if the result in which K is greater than zero we print the result else we print it to be zero and we return the result and lastly we actually print the recursion example results and we call the function by passing it a value of K right so K is six in our case so if K is equal to Z absolutely 6 Plus TriCore recursion of five again come into the function is 5 greater than zero correct again go back check if four is greater than zero yes keep doing it until the result is zero right so it's pretty much obvious at this point of time guys so every time you actually run this one value gets added to your program right so again check out the output and you will understand this better so we have one and then two values were added we got three three values were added we got six four values were added we got 10 five values 15 six values 21 so all we're trying to do is add one extra value and print it using recursion and that's about it guys so I hope this has been a good introduction to the python user defined functions as well so next quickly let us check out the Lambda function in Python guys so what is the Lambda function guys it is extremely simple a Lambda function is basically one small Anonymous function so it can take any number of arguments but it can have only one expression at the end of the day check out the syntax here so we have Lambda arguments and expressions well still nothing is working right check out the example we have x equal to Lambda of a where the expression is a + 10 so as soon as we want to print something all we need to do is we need to print X of 5 as soon as we hit X of 5 a gets the value to be five so since a gets the value to be five 5 + 10 becomes 15 and we printing X of 5 which is now 15 pretty simple right so you basically can create tiny functions all on your own using the Lambda keyword here so do know that the syntax is really important because we have the arguments on the left side and the Expressions on the right side so guys here's another example I have for you so we have a Lambda function that multiplies one argument with another argument in our case it is a and b and we want to print the result so check it out x equal to Lambda of a comma B because a and parameters here so what is the expression you want to perform between A and B it is multiplication so A cross B and we're printing X of 5 comma 6 so a is 5 b is 6 what is 5 sixa it's 30 right again the output is 30 pretty simple check out this other example so basically we're trying to find the sum of three parameters a b and c and from the result so x equal to Lambda of a b and c three parameters a b and c hence three and then we have a plus b plus C so what is the sum of 5 6 and 2 it is 13 and that is exactly what you've got in the output so these are tiny set of functions that you can make use of any time without any dependencies guys so that is the important part I want you guys to notice at this point but when you can have all the regular functions why would you want to make use of the Lambda function right I'm pretty sure this was the first thought that came into your head as soon as I explained the concept well the power of Lambda is actually better shown when you use them as an anonymous function inside another function check this particular thing out we have defined a fun function which is called as my funk I have one parameter n and then we're actually performing another function inside our function and this is basically telling us to return the Lambda of a cross n so whatever n value has been provided we're going to use the Lambda function as in a tiny function there to provide us the multiplication of a value with a particular n value which is the parameter simple so check out this example guys so we basically use that function definition to make a function that always doubles the number that you send in so we have defined find my function of n Lambda of again a into n right so it's pretty much the same example and then we have a variable called my doubler which actually is sending the parameter 2 to our n value so what do you think is going to happen as soon as I print the value of my dou of 11 pretty much simple 11 gets multiplied by two because 11 is passed as a function into the my funk through my doubler so here A and N becomes 2 and 11 so what is the product of 2 and 11 it's 22 pretty much simple right so here's another last example so basically we can use the same function definition to make a function that actually triples the number that you send in so what I want you guys to not is that check out the function definition it Remains the Same so Define my function of n return Lambda of a into n perfectly same my doubler my function on how many times you want to double it so double is two so two my tripler equal to my funk of three so I want to triple it so multiply three times as soon as I print my dou of 11 11 into 2 22 is the the first output that I get print my tripler 11 into 3 33 that is the second output that I get so what I'm trying to convey to you guys is that you wouldn't have to rewrite all of these functions at every point of time right so the definition of the function remained same how we call it and how we get our output is actually depending on us and that is what makes functions efficient and one of the biggest Concepts that you need to learn in Python [Music] guys what exactly are Lambda functions python Lambda functions are functions that do not have any name they're are also known as Anonymous or nameless functions the word Lambda is a keyword that specifies what followers is anonymous now that you're aware of what these Lambda functions are let's move on further to see why they're actually used the main purpose of anonymous functions comes into picture when you need some function just once they are created wherever they needed due to this reason python Lambda functions are known as throwaway functions they're also used within higher order functions which take a function as an input or return it as an output another very good advantage of using these Lambda functions is reducing the size of code and I'm going to be showing y all in this session how to do this so now let's move on towards our next topic which is to see how to write Anonymous functions Lambda functions are created using the Lambda operator and its syntax is as follows the first thing that you'll need to specify is the Lambda keyword and following that you'll have to specify the arguments or the inputs and finally after the colon symbol you'll have to specify the expression that needs to be solved Lambda Expressions can take any number of arguments so as you can see over here my first example is a Lambda expression without any arguments in the second example I've taken a Lambda expression which just has one argument A1 finally I'm demonstrating a Lambda expression which has inputs from A1 to a n just remember that all your arguments or inputs need to be separated by a comma so now let's move on towards our Jupiter notebook and see how we can actually write these Lambda functions I'll just create a new notebook over here guys I'll just open a new notebook over here guys and I'll rename it as Lambda functions so guys now just let me write the syntax as a comment over here this is going to be good for a referral purpose so first is the Lambda keyword like I already told y all sorry guys for the spelling mistake Lambda and the arguments followed by the expression okay so the first thing I'll have to write down is the Lambda keyword following that I'll have to specify the arguments so let me just give one argument to this and after that I'll just say I want to multiply this a with itself so this is my Lambda function but before executing this I'll require some variable that can hold the value of this so I'll just specify that X is equal to this Lambda function and after this I'll say x of 3 and I'll hit run so as you can see over here when I used X of 3 it returned the output to be 9 so guys if I had to solve the same expression using a normal function then I would have had to write a larger piece of code than this let me just demonstrate this to you guys over here so I'll just Define a function say new and I'll pass the parameter as a now I'll be using return to return the value of a St a to call this function I'll have to use the name of the function and specify some value to the argument that I've already passed so in place of a I'll pass the value as four or let me take it as three itself like the previous function and I'll hit run as you can see over here using Lambda functions I just required two lines of code whereas while using the normal function I had to make use of three lines of code along with this return statement over here okay so guys I hope you all have understood how to write these Lambda functions so guys as you all have just seen I've used Lambda expressions in the example along with some other variable X now I did this because these functions are nameless and require some name to be called but doesn't it seem confusing as to why assign a name to a nameless function and what is the need of it it's a legitimate question guys but the point is this is not the right way of using Anonymous functions Anonymous functions like I've already told y all are best used within higher order functions these functions either take a function as an input or return it as an output to demonstrate this let let me just move on towards our next topic of this session the first thing that we're going to study is anonymous functions within user defined functions so as you all can see on the screen I have a user defined function that takes the Lambda function as an input in this example over here the normal python function which in my case is new funk takes one argument X this argument is then added to some unknown argument which is supplied through the Lambda function let me just jump onto my bit a notebook to explain this in detail let me just first create a heading over here guys just remember the number of prefixed hashes tells which heading it is so if it is H1 it will have one hash prefixed to it if it has two then it's H2 and so on so I'll just create a heading over here of H1 level and I'll give the name as user defined functions or Lambda within user defined functions Lambda Within user defined so I hope you guys know what are user defined functions okay so as you all know when you have to create a function in Python you'll have to use the DF keyword followed by the name of the function I'll just name the function as a and I'll pass one parameter to it say X after that I'll be using the return statement and within this I'll specify a Lambda function I'll pass y as the argument to this Lambda function and I'll use x + y as my expression now let me just give some value to the variable X and store it in another variable T So a of 4 and I'll I'll print the value of T of 9 or sorry 8 and I'll hit run so as you all can see on the screen the Lambda function that is used within the function a is called Whenever I Call this higher order function and the first thing that I'm doing over over here is passing a value to the variable X and then I'm printing the value of x + y so I hope everyone's understood this part okay now let me just copy this function and pass a few other values to this as well I'll just store a new value in the variable U and I'll pass the value for x as say 7 and I'll print of five and I hit run okay so I hope everyone's clear with this if you have any doubts please do let me know in the chat box and my team is here to help you now let's move on towards the next topic which is using Lambda functions within filter map and reduce functions the filter method is used to filter the given iterables which can either be lists sets Etc now this is done with the help of another function which is passed as an argument to test all the elements to be either true or false after applying the function to the set of iterables if the value is true then that value is returned in the output now let me just jump onto my Jupiter notebook and use the Lambda function within the filter method I'll just give a new heading over here again just for your reference guys I'll create a heading of H2 level by prefixing it by two hash characters so I'll just use Lambda within filter and since this a method like I already told yall the filter function needs iterables now here I'll just create a list I'll say my list and I'll specify some elements to this the filter method is going to check for all the elements to either be true or false in accordance to the expression that is passed within the Lambda function like I already told yall the filter method is going to check if the expression that is specified within the Lambda function is either true or false for all the elements that are present within my list and it returns all the elements that satisfies the expression so as a result I'll have a new list so I'll just name it as a new list and I'll use the filter function and within this I'll be using the Lambda function like I already told y all let me just specify the input as a and it'll give an expression over here say a I just want to check if any of these values when divided by 3 will equate to two and I'll pass my list and I'll pass my list as an argument to this and since the output is going to be a list I'll just use the list method over here and one more point I want you guys to note is the syntax of the filter function and since I forgot to specify this before beginning the demonstration let me just specify the syntax of this function over here I'll say syntax you'll have to use the filter keyword along with some function followed by the itals okay so as you can see over here I have the filter keyword I have the function which is Lambda and then I've passed my list as the iterables to this function now let me just print the new list and I'll hit run uh oh sorry guys I've made a spelling mistake over here so it's f l t e r so as you can see over here I've passed the elements 1 2 3 4 5 and six to this filter method and after checking if this expression is true for all the iterables it has returned six as the output now let me just cross check over here so if I divide 1 by 3 I will not get a result which is equal to 2 similarly if I divide 2 by 3 I'm not going to get the result as two again finally as we all know know if I divide all these elements by three only for six I'll get the output as two so I hope everyone's clear with how to use the Lambda function within the filter function so now let's get back to our presentation and see how we can use the Lambda function within the map function guys the map function in Python is a function that applies a given function to all the iterables and returns a new list so I hope you're understanding what exactly it does it takes function as the input and some iterables as well and then it applies this function to the set of iterables that are passed and it returns a new list so now let's get back to Jupiter and do the same I'll create a new heading of course and it's going to be Lambda within map and I hit run so before moving on towards the demonstration I'll just write the Syntax for the map function so it has to have the map keyword of course and the parameters that are passed to it is a function and the iterables now let me just make use of the same list which I was using previously and I'll copy that over here and then I'll just store the output in a new list p and I'll use the list function since I want the output to be a list within this I'll use the map function and to that map function I'll pass a Lambda function as the input I'll just pass one argument to this as a and the condition I'm going to specify is to check if any element present within my list divided by 3 is not equal to two and I'll pass my list as the set of itals and then finally I'll be printing this now let me hit run so as you all can see over here it has returned a list with some Boolean values the expression that I wanted to check is a by3 not equal to 2 now let me just move on to my list and see what happens when I divide 1 by 3 of course I'm not going to get two so I have true as the output now when I divide 2 by three again I'm not going to have the output as two and therefore I have true as the Boolean value for this similarly all the elements present in my list are divided by three and it's going to check if the condition is true therefore only for six it's going to be false and I have the output as false okay so I hope you guys have understood how to use the Lambda function within the map function if you have any doubts please do let me know the chat box guys and my team will revert to you okay now let me just jump on towards our next topic which is using Lambda function within the reduce function so guys the reduce function is used to apply some other function that is passed as a parameter to it to a list of iterables and finally it returns a single value and to demonstrate this I'll just jump onto my Jupiter notebook again and I'll create a heading this is going to be Lambda within reduce and I'll hit run so first let me just write the syntax of this function it is the reduce keyword and it takes a function as a parameter the first parameter is a function and second is a sequence now to make use of the reduce function you'll have to import the fun tools library or from that Library you'll have to import the reduce function to do this I'll be using from fun tools import reduce you can also directly import the fun tools Library as import fun tools tools or you can also use from fun tools import Star these are just the alternate ways that you can also use over here okay so now I'll be using the reduce function and within this I'll specify a Lambda function and I'll pass two parameters say A and B and let me just print the sum of A and B and finally it has to have a sequence as its input so I'll just specify a list over here I'll say the list of values to be 23 56 43 98 and 1 okay so now let me hit run and see what it returns so as you can see over here guys I have 221 as my output so what this reduce function is actually doing is it first adds 23 to 56 so guys the first thing that the reduced function does is ADD 23 and 56 and the output of this is going to be 79 now after adding these two values to this output it will add 43 so to 79 it's going to add 43 and it'll produce a new output after finding the output of 79 and 48 it's going to add the result to 98 and finally it's going to add that result with one and Returns the final output which is 221 so I hope you guys have understood how this function is recursively adding each value to the next value that's present in the sequence guys if you have any doubts please do let me know in the chat box okay now let's move on to see how you can actually use these Lambda expressions or Lambda functions to solve some algebraic expressions for this I'll just go back to my Jupiter notebook book and I'll create a heading over here uh oh sorry guys I'll just delete these two kernels and I'll create a heading I'll just say Lambda for algebra so guys first we'll be taking some linear equations so I'll just create a heading as linear equations so as we all know algebraic linear equations consists of variables of degree 1 which means they'll have the power as one so now let's just use the Lambda function to solve some linear equations I'll just say s is equal to Lambda and I'll pass the argument or the input as a and the expression I'll specify as a star a and I'll use S of 4 and I hit run so as you all can see over here 4 cross 4 is 16 now let me take a different linear equation so let me just write the linear equation over here as a comment so I just want say 3x + 4 y okay so if I want 3x + 4 y so I'll have to specify some name to this say d is equal to Lambda and X and Y as the inputs and since I want three star x + 4 star y okay I have 3 into x + 4 into y okay and then I'll pass the values as 4 comma 7 and I'll hit run as you all can see 40 has been returned as the output when I pass the value of x as 4 and the value of y as 7 now let me take some quadratic equation so I hope you all know what is a quadratic equation it's an algebraic equation with the degree 2 let me just give the heading as quadratic equation okay so now let me just try to solve the very famous a + b the whole square a + b the whole squ so I'll just say part two okay now let me write a Lambda function that's going to return the value of a + b the whole Square I'll just save it within a new variable X and I'll use the Lambda function and the arguments or the inputs are A and B the expression that I'm going to use is A+ B and like we all know we'll have to use double star when we use powers and then I'll print this x I'll pass the values of a A and B as 3A 4 and I hit Run Okay so as you all can see I found the value of a + b the whole squ using the Lambda [Music] Expressions what exactly is an array an array is basically a data structure guys with ordered series of elements any variable that is declared as an array can hold more than one value at the same time all values in an array have a particular address which is specified by its index number here as you can see on the screen I've declared a variable a as an array and I've stored values from 1 to 100 in my array which is a also make a note of the index values indexing always starts from zero and not from one therefore a of0 holds 1 a of 1 holds 2 a of two holds 3 and similarly a of 99 holds 100 when the length of my array is n the index value will be nus - 1 it is always one less than the length of the array so I hope you're clear with this topic now let's move on towards our next topic for this session which is the difference between Python lists and arrays python lists and arrays have the same way of storing data but there is a key difference that you all have to note arrays can store only single data type values whereas lists can store any data type values so for example if I have an array of integer values all values present in that array will be integers and nothing else on the other hand if I have a list then it can store integers together with float and cars and strings Etc now when you try to perform some operations like slicing or looping it will be similar for both but when you try to perform different operations like if you want to multiply your array values by two or you want to divide it by two you can do so easily when you have an array but if you try to do the same with a list it will throw an error because naturally you cannot multiply cares and strings or divide them so I hope you're clear with the difference between these two so now let's move on towards the next topic how to create arrays in Python unlike other programming languages if you want to create arrays in Python you will have to import the array module now this module has all the functions that are necessary for creating and Performing various kinds of operations on arrays to import the array module you can use three ways first is to import the array module by its original name which is array the second method is to use an alas name this is the most widely and commonly used method as well in my demonstrations I'm going to be using this method the Third Way is using star from array import Star what this does is it's going to import all that is present in your array module now let's go to our Jupiter notebook and try to do this please do it along with me guys I'll open a new notebook and I will use the first method to import the array module as you can see when I hit run no error has been returned which means this is executed now let's try to create arrays using this method as you can see over here I've created an array a using the first method so I have used array do array of integer values and I've specified some random integer values here the first array is the name of the module the second array is the array Constructor and then I've given a type code this type code specifies what type of elements my array will hold as you can see I've created an array so I hope you're clear with how to create arrays using the first method now let me open a new notebook and try the second method the reason to open a new notebook is because I've already imported array module in this notebook I'll rename this notebook because I'm going to be using this method in further demonstrations as well as you can see over here when I use import array as ARR ARR here is my alas name when I use this and hit run it does not throw any error now let me try to create arrays using this method I'm using int as my data type you can use any data type of your choice as you can see that an array has been created with some random integer values in place of the mod module name array I've used ARR now let's try the third method I'll open a new notebook again as you can see over here when I'm creating arrays using the third method I've just specified The Constructor name and then the type code and the value list so I hope you're clear with how to create arrays using all three methods okay so now let's get back to our presentation our next topic for this session is accessing array elements to access array elements you'll have to make use of the index values each index value holds a unique element like I've told you before indexing starts from zero and not from one so therefore a of0 holds 1 a of 1 holds 2 and so on this is is the normal traversal order which starts from the left hand side and moves towards the right hand side but here I want you all to note this point as well that negative indexing also exists so negative indexing will start from the right hand side and move towards the left hand side therefore 100 will be at a of minus1 99 will be at a of minus 2 and similarly 1 will be at a ofus 100 so I hope you're clear with negative indexing now let's go to our jupit notebook and try to access some array elements as you can see over here I've created a heading to create a heading all you have to do is go to code select heading and prefix your text with the number of hashes based on the heading level you want so if I want h H1 I will use one hash if I want H2 I will use two and so on here I've created a heading of H1 so I prefix my text with one hash now let me try to access some array elements as you can see over here when I access a of two my output is three let's see where three is present in my array one is present at a of Zer 2 at a of 1 and three at a of two so therefore when I access a of two 3 has been returned are you guys clear with this just give me a quick confirmation before I move on towards the next topic Shashank says yes prti says please try to access something using negative index values okay pry let me show you that as well so here I've tried to access a of minus 2 a of minus 2 has returned five let's get back to our array a and see where five is present six is at a of minus1 and five is at a of minus 2 therefore my output is five so prey are you clear with this okay prti says yes now let's move on towards our presentation and see what is the next topic our next topic of this session is basic array operations here I just want to tell you all something arrays are mutable which means they are changeable so in case you want to add or remove elements from it you can do so easily now coming back to our presentation our first operation under this section will be finding the length of an array the second is adding or changing elements of an array and then we will try to remove or delete elements of an array and then guys we will try to perform array concatenation slicing and finally we will be looping through an array now let's move on towards the first operation to find the length of an array you will have to make use of the Len function now this function returns an integer value which is equal to the number of elements that are actually present in your array the Len function takes one parameter which is the name of your array as you can see in the example over here I've created some random array a and I've given some random values as you can see I have three values over here and when I use the Len function and I specify the name of my array the output is three which is equal to the number of elements that are present in my array now let's move towards a Jupiter notebook and see how this works as you can see over here when I use the Len function and I've given the array name which is a an integer value is returned which is six six is the number of elements that are actually present in my array which is a okay so I hope you're clear with this so our next operation is adding elements to an array in case you want to add elements to an array you can do so using the append extend or the insert function the append function is used when you want to add an element to the end of your array the extend function is used when you want to add more than one elements to the end of your array the insert function specifically adds a new element to a particular position in the original array let's have a look at a small example of these three as you can see on the screen I've created some random array a and then I've used the append function and I've given the value that I want to add to my array as you can see in the output 3.4 which is the value I've specified to the pen function has been added to the end of my array which is a and then I've used the extend function please make a note over here that when you use the extend function you will have to specify the values between Square square brackets if you don't use the square brackets it will throw an error I've given three new values and all three values have been added to the end of my array and then I've tried to use the insert function over here I've specified the index number as two and the value to be inserted at that position as 3.4 as you can see in the output 3.4 has been inserted at C of two now let's go to our Jupiter notebook and we'll do the same I'll reprint my array which is a so that it's easy for us to refer to it since my array a holds integer values I will give some new integer value to it as you can see I've given eight as the value that I want to insert in my array and eight has been added to the end of my array now let me try to give some float value to this array and see what happens as you can see when I try to append some float value it gives me a type error therefore I can add only those values which is specific to the type of elements that are present in that array now let me try to use the extend function like I've told you all when you're using the extend function you will have to make use of the square brackets as you can see in the output all the values that I've specified within the extend function have been added to the end of my array now let me use the insert function I've used the insert function and the first parameter I've specified is the index value and the second is the element to be inserted at this index position so at index value a of two six will be inserted as you can see in the output six has been inserted at a of two so I hope you're clear with how to add elements to an array now let's move on towards the next operation our next operation is removing ele elements of an array in case you want to remove elements from an array you can make use of the pop or the remove function the pop function removes the element and returns it whereas the remove function will remove it but it will not return it the pop function can either take no parameters or one parameter the parameter it takes is the index value of the element to be removed if you do not specify any parameter it will remove the last element from the array the remove function takes one parameter which is the element to be removed itself now let's have a look at a small example of this as you can see on the screen I've created some random array a and then I've used the pop function without specifying any parameter as you can see in the output when I do not specify any parameter the last value present in my array has been popped and returned as well second time I've used the pop function and I've given some index value now this pop function will remove the element that is present at this index position as you can see it has removed 3.1 and returned it as well after that I've used the remove function like I've told you before the remove function takes one parameter which is the element to be removed and you can see in the output that the remove function has not returned any value after that I've printed my array and you can see all the values that are removed using these two functions are no more present in the array a now let let's go to our Jupiter notebook and do the same let me reprint my array which is a first I will use the pop function without specifying any parameter to it as you can see over here when I do not give any parameter it removes the last element that is present in my array now let me give some index value to it I'll reprint my array a because I've removed an element from it now let me give some index value to the pop function as you can see when I use a. pop of 2 it removes the element which is present at a of two which is six and it returns it as well let me try to use some negative index value for the pop function a pop of minus1 removes the the last element that is present in my array which is five now I'll use the remove function let me reprint my array guys as you can see all the elements that have been removed using the pop function are no more present in my array a like I've told you before when using the remove function you will have to specify the element that is to be removed here let me remove eight because there are two occurrences of eight and let's see what the remove function does does when I hit run remove function does not return anything now let me print my array a and see what is present as you can see the first occurrence of eight has been removed but the remove function did not return the value so I hope you're clear with the remove and the pop functions now let's get back to our presentation and perform the next operation our next operation is array concatenation concatenation means joining so in case you want to join different arrays you can make use of the plus symbol as you can see on the screen I've created some random array A and B and then I've created some random array C which is empty also make a note that all three arrays have the same type code and then I've concatenated A and B into my empty array which is c as you can see in the output all the elements present in A and B have been concatenated into my array C now let's go to our Jupiter notebook and perform array concatenation I'm using int as my data type you can use any data type of your choice to create an empty array all you have to do is specify the type code and leave the value list empty here I've created an empty array D and I've concatenated B and C into my array which is D now let me hit run when I hit run all elements present in B and C have been concatenated into my new array which is D so I hope you're clear with array concatenations let me just show you what happens if I try to concatenate arrays of different data types I'll just change one of my previously created arrays type code and I'll give it as float let's see what happens when I change the type code and I try to concatenate these two arrays you can see that I've encountered an error and it's says type error which means it cannot concatenate two arrays of different data types so just be careful when you're concatenating arrays you cannot concatenate arrays which hold different data type elements okay now let's move on towards our presentation and see what is the next operation our next operation is slicing and array slicing actually means fetching some particular values from your array to do so you can make use of the colon symbol as you can see see on the screen I've created some random array a and I've sliced it from 0 to 3 0er specifies from where fetching has to start and three specifies where it has to stop so it will start from zero it will go on till three but it will not include the value which is present at three as you can see in the output I have a of0 a of 1 and a of two that is present in my output now let's go to our Jupiter notebook and we will try to slice our ARR I'll reprint my array D over here and then I will try to slice it as you can see I've sliced my array from 0 to five so it will start from index number zero it will go to five but it will not include the value which is present at index number five you can also see the same in the output over here let me try to use some negative index value my output when I use Z colon min-2 contains all the values between 0 and min-2 but it will not include the value which is present at min-2 okay so I hope you're clear with this topic just give me a quick confirmation before I move on okay annel says yes Shashank has a question he asks what does colon colon minus one mean Shashank unlike what many of us think colon colon minus one does not reverse my array but it prints a reversed copy of my array so let's try to do this over here as you can see all the elements present in Array D have been reversed now let's print our array which is D the original array D is the same but colon colon minus one has reprinted a reversed copy of my array which is D this method is actually not preferred because it exhausts the memory so I hope I've cleared your doubt Shashank just give me a quick confirmation okay Shashank says yes now let's get back to our presentation and perform the last operation of this session which is looping through an array you all might be familiar with these two Loops which is the for and the while Loops the for Loop iterates over the items of an array specified number of times whereas the while loop iterates Ates until some condition is met or some condition is true when you're using the Y Loop you will have to keep three things in mind one is initializing your iterator second is to specify a condition third is to increment your iterator remember that if you do not increment your iterator your while loop will go on forever let's see a small example of this as you can see on the screen I've created some random array a and then I've used the for Loop to Loop through my array a I've used for X in a print X which means go to every element that is present in a and print it let's try to do the same on our Jupiter notebook let me reprint my array D now let me use the for Loop to Loop through this array for X in a print X which means go to every element in D and print it as you can see all elements present in D have been returned one after the other now let me try to slice this array and print specific elements as you can see over here I've sliced my array from index number 0 to minus 3 and I've printed only those specific values using the for Loop now let's go to our presentation and have a look at the while loop like I've told you before when you're using the while loop you will have to do three things one is initializing your iterator second is to specify the condition and third is to increment your iterator let's try to perform looping on our Jupiter notebooks please do it along with me guys let me reprint my array d here I'm using the iterator as temp you can use any name of your choice as you can see over here I've initialized my iterator to zero and then I've specified a condition wherein I have told whenever the value of my temporary variable is less than D of 2 then print that value and then I have iterated the value of temp I can also use temp plus equal to 1 which is same as temp equal to Temp + 1 now let's try to use the Y Loop using the Len function here I will use my array a so let me reprint it over here as you can see over here when I use the Len function my temporary variable will go to all elements present in my array a and print them one after the other [Music] what is inheritance in Python if we take a look at a real life example inheritance is when a child inherits properties from their parents it can be the genes or it can be any other property characteristic so looking at it from the programming point of view inheritance is when a class inherits properties from another class the class which inherits the properties is known as child class or the derived class and the other class is known as the parent class or base class now let's take a look at a few advantages of using inheritance in Python so first of all it is mainly used for code reusability so that you don't have to write the same code again and again and it provides a transitive property and also resembles a real life relationship between classes so these are the advantages of using inheritance in Python guys so let me take a simple example to explain what is inheritance and how you can do it in Python so let's take it up to pyam guys and now I'm going to enter the presentation mode for better visibility so now what I'm going to do is I'll just take one class guys and name it as parent let's say now give it a function so I'll name the function as function one and after that let me give it a statement let's say print function number one okay let's say this is a parent function now what I'll do is I'll take one more class guys and name it as child and for this in the Declaration I have to specify y this parent over here this is also known as subclassing guys we do the subclassing because a child class is not able to identify which class it is inheriting the properties from so we do this by declaring the parent class inside the Declaration of the child class only now what I'm going to do is I'll just make one definition of a function let's say function number two and in this I'll give the print statement as let's say this is a child function now when we are declaring a child class it must understand from which class it is inheriting the properties from so we have declared the child class with the parent class in the Declaration and that's how the child class differentiate between a parent class guys so now to call the parent class methods we use the child class objects so what I'm going to do is I'll just make a object of the child class guys now to call this parent function which is the function number one let's see if I can do that and I'm getting function one and function two as well so I can call both the functions so first of all I'll call the function one and after that I'll call the function number two let's see what the output will be now the output is this is a parent function and this is a child function so basically what we have done is we have taken two classes named it as parent and child and from the child class we are inheriting the properties of the parent class here which is the function number one and this is the basic example of inheritance guys this is how you can do it now that I've shown you a simple example let's talk about the init function in Python so what exactly is init function init function automatically gets called each time an object is created for a class now when we add an init function to a parent Class A Child class will not be able to access the parent class method so to overcome this the child class init function overrides the parent class init function so now let's take a look at an example to understand this now to show how init function works let me first clear this we'll write a init function over here yes and instead of self I will add one more variable let's say F name and FH now what I'll do is I'll just write self do name is equal to F name and self Doh is equal to FH now I'll take one more function guys so I'll just name it as let's say View and for view I want to print the value of f name self do name and self. AG so this is my parent function guys and for the definition in the child class I will also make one init function so I'll just write it as init itself and again I have to add F name and F age and let's say self. name is equal to F name and self do AG is equal to F AG now this init function is going to override the parent in function guys so let's make one more function so this is going to be view as well and in this let's say I want to print okay before we move on we have to specify the parent function with the init function as well and we don't have to mention these variables instead I'm going to add one more let's say I want to add last name as well and I'm going to add it as let's say Eder now I'm going to print over here self do age self dot last name and self dot let's say name all right I have to remove these let's make one more object and give it the value let's say I want the age to be 23 and last name is going to be p py in the inverted commas name we have already specified so we don't have to give it again and I'm going to just call the function let's see what happens what is the output will be so as you can see I'm getting the output as python Eda and 23 so this is how you can override the parent class in it method using the child class guys so this is all about the init function now let's talk about the next topic that we have which is the types of inheritance so depending upon how many types of parent class and child class are going to be there inside the program following are the types of inheritance in Python guys so there is single inheritance there is multiple inheritance then there is multi-level inheritance hierarchial inheritance and there is Hybrid inheritance as well so we going to talk about each of them in detail guys so first of all let's talk about what is single inheritance so when The Inheritance involves one child class and one parent class only it's going to be a single inheritance the example that I have showed you before is the example of single inheritance guys and talking about multiple inheritance it is going to involve more than one parent class which means we are going to have at least two parent classes and the child class is going to derive properties from both of them and this is called multiple inheritance now talking about multi-level inheritance let's say we have a single inheritance going on we have a parent class and we have a child class but the child class also act as a parent class for another child class that is going to be a multi-level inheritance guys and talking about hierarchial inheritance hierarchal inheritance actually involves multiple inheritance from the same parent class and talking about Hybrid inheritance if a program has more than one types of inheritance let's say we have a single and multiple inheritance going on simultaneously it's going to be a Hybrid inheritance guys so let's take a look at examples for each of them so let's go to Pyon guys and I'll remove all this so I'll start with single inance guys so I'll just take a parent class and give it a function let's say function one and give it a state statement let's say this is function one now let's take another class guys class child and give it the value of the parent class and after that we can move for another function let's say function number two and give it a statement let's say print this is function two now make a child class object and call the function so we can call both of these functions so I'll just call function one give it the output and we getting the output as this is function one so this is a basic example of single inheritance guys now talking about multiple inheritance what I'm going to do is I'm going to make one more class over here let's say class parent 2 now what happens is let's say give it a function number three let's say and print statement this is function three and in the Declaration over here I'm going to just specify two classes and when I call the functions I can call function number one and I can call function number three as well guys so let's see what is happening here in the output I'm able to call the function number one and function number three as well from the child class object so this is the example of multiple inheritance guys now talking about multi-level inheritance so what I'm going to do is I'm just going to specify parent over here and from this let's say it doesn't have any Declaration of parent one which is this class so it is not able to inherit any properties from that but what I'm going to do is I'll just specify parent two over here and I can still call these functions because there is an inheritance going on between them which is multi-level inheritance which basically means this class the parent one class is actually giving properties to parent two class and parent two class is giving properties to the child class so this is example of multi-level inheritance guys now talking about hierarchial inheritance guys what I'm going to do is instead of pairing two over here I'll just specify parent so for these two child classes I have the same parent class and now let's see if I can have the same output or not I'm not going to get the properties from the parent two because I have not specified it in The Inheritance so I'll just remove this and let's see what happens I'm getting the output as this is function one okay let's make the object for PIR in two as well all right give it one and ob1 do let's say what functions we can have I can have the function number three and I can have the function number one as well so I'll just print function number one and from both the child classes I'm able to print the function number one so this is the example of hierarchial inheritance guys I'm using the same parent class to give the properties to both parent two and child class as well and now talking about Hybrid inheritance Hybrid inheritance actually involves more than one inheritance going on in the single program so we have a single inheritance over here so let's say I want to have multiple inheritance as well so I'll just specify parent two now and this is the example of Hybrid inheritance guys because we have a single inheritance going on over here and in the child class I'm inheriting both the parent one and parent two and this is the example of multiple inheritance so this is a Hybrid inheritance example guys okay we are getting a type error which is saying cannot create a consistent method resolution order for base space parents parent one and parent two okay let's say instead of this I'll make one more class over here give it a value let's say parent number three give it a function function let's say four and print statement this is function 4 now instead of parent number two what I'm going to do is I'll just take parent number three guys and with the child class operator let's see if I can actually get the function number one and let's say I want function four as well I'm able to do that so in the output let's say what I get I'm getting the function one and function four so this is a simple example of Hybrid inheritance guys now that we done with the type of inheritances let's take a look at what all we have now so we're going to talk about the super function guys super function actually calls the parent class method directly so let's take a look at a simple example guys how it works so I'll just remove all this now what I'll do is I'll make a let's say parent class and give it a function function one again the print statement now what I can do is instead of calling the parent function from the object I can just use the super function and over here I can just call the function one now I'll just print this is function two let's make a child class object now let me see if I can call the function two or function one I can do that so I'm going to call the function two guys let's see what the output will be so I'm actually able to call the function one directly from the super function over here so this is the function of super function guys it can directly call the parent method and now that we done with the super function let's take a look at the next topic that we have which is method overriding in Python so method ring can also be achieved to change the functionality of the parent class function so let's take a look at an example to understand how we can actually achieve method overriding in Python guys so instead of naming them separately I'll just name them as same so when I create the child class Constructor and if I call the function now okay function one which is so when I'm calling this function Tech technically it should call the parent function but what happens is it overrides the parent method okay to specify this run it again now I'm getting the output as this is function two even though I'm calling this function one which should be ideally calling the parent function but it is printing out the function which I have specified in the child class so this is how I have overridden this parent class method and this is how we can perform method overriding in Python guys why we need exception handling well to understand the concept let me quickly walk you through a very simple scenario consider the case where you're dividing any number by zero what will happen can you divide a number by zero you cannot correct but how would a kid approach this problem the kid would think that you can actually use regular division to divide something by zero so you're dividing something by nothing so it stays the same something like this right little does the kid know that this is wrong now check out how a programmer would approach the same concept well it is established that dividing by zero is not possible mathematically so as programmers we know it is not correct so this basically leads to two cases either an exception or an error so a Python program terminates as soon as it encounters an error well in Python an error can be either a syntax error or an exception and it is as simple as that well let us actually divide by zero for on guys so this is what happens in python python presents us with this beautiful error message saying that it cannot divide something by zero well in fact let us head to py Cham which is my ID for coding in Python so we can actually walk through this practically so guys here I am in my pycharm so let me quickly go ahe and type out some simple code for you guys so let us start with y equal to 10 we can actually print y by 0 now so let me quickly go ahead and run this in the console so as you can check out it say zero division error division by zero is not possible so you cannot divide anything by zero so let's establish that at this point guys that was good enough right so let's get started then let us quickly check out what exception handling actually is well guys to get a clear picture of what that is we need to start with understanding the definition for both exception and exception handling well what is an exception guys an exception is an event which occurs normally during the execution of a program that disrupts the normal flow of the program programs instruction so instead of the program actually executing evenly and nicely there is actually a disruption in between the code so this we basically call as an exception so next what is exception handling guys this is the process of actually responding to An Occurrence of an exception as simple as that so basically the process of responding to the occurrence during compilation of exceptional statements which require special processing is actually what is exception handling guys so this is basically done because of the change in the normal flow of the program execution well it is it is a very simple concept and I'm sure at the end of the session you guys will agree well at this point what do you think of exception handling in Python guys head to the comment section and let me know what you think so we can interact there and now that we know what exception handling is we need to understand how the entire process of exception handling works so let us check it out first let us walk through what happens in Python so let us say the user makes an error what happens next guys when you think about it it is either the user that finds out the error or python will actually tell you what the error is correct so once you know what the error is and you know where it is you can basically start analyzing it to know if it is fixable or not well if it is fixable how can you go about doing that well if it isn't nothing to worry guys python is so good that no matter on what level you mess up there is always a way out of it guys so now if it's fixable how can we handle the exception check out the image on the right side so we need to First find the error if we cannot do it python will find out for us well next is to make sure you take caution while coding if you think that there is going to be an error in that particular piece of code well remember the word try at this point all right and later we'll actually fix the error and we call this as catch just remember the keywords for now and we'll be walking through these in detail in the following slides in fact let us Begin by checking it out right now so Guys these are some of the important terms that you need to take a note of before starting to code and understanding exceptions so first we have try well try is basically the keyword which is used to keep the code segment under check so whatever code you put under try is actually the code under scrutiny guys and next we have accept well accept is a segment which is actually used to handle the exception guys so how do we know that the exception is occurred if our piece of code in the Trib block has actually caus an exception then we can handle it in the except block and how do we use else well else is basically run when no exceptions exist guys and lastly we have finally finally we'll run even if there is an exception finally will run if there is not an exception as well well so I want you guys to note this at this point of time and this will help you a lot in the coming slides guys so now coming back to python this is what it visually looks like we first have the tri block where we run a piece of code and next we have the except where the code actually runs only when there is an exception in the tri block and if there are no exceptions then we run the else block guys and lastly no matter what exception or no exception We Run The finally block always guys sounds simple right well it actually is and now that you're familiar with the process of of exception handling let us check out how we can go about understanding this using python practically guys well syntax errors occur when the parser detects an incorrect statement well check out the tiny arrow on the left side here well this is basically to indicate where the parser ran into the syntax error well in this particular example there were too many brackets right so there was an extra closing bracket well if you actually remove it and run it you will actually get a different error this time we check on the right side part of the code well this time you actually ran into an exception error well this type of code occurs whenever syntactically correct python code results in an error guys so the last line of the message indicated is what type of an exception error that you actually ran into well in this particular case it a zero division error and it says that the integer division or modul by zero is not possible so this is basically the same case that I showed you in py Cham a while ago but instead of showing the message exception error python details what type of exception error was encountered well as I mention mentioned in this case it was a zero division error right well python comes with various built-in exceptions as well as the possibility to create self-defined exceptions as well and now that we're clear with that let us check out how we can go about raising an exception on our own so we can basically make use of rays to throw an exception if a condition occurs the statement can be complemented with a custom exception well if you want to throw an error when a certain condition occurs using Ray you can actually go about it like the following piece of code you actually see on the screen and when you run this piece of code you'll be greeted with the following output guys so it says exception X should not exceed five and the value of x was 10 so let me quickly jump back to pyam and we can execute this there so guys I'm in my py Cham right now so I quickly typed out the code for you guys so let me quickly go ahead and run it and we can check out the error as you can see it says exception X should not exceed five and the value of x was 10 perfect right so let's quickly jump back to the presentation to check what's next so the program basically comes to a halt and displays are exception to screen right so this basically offers clues about what went wrong and now that we're clear with that we can actually proceed to checking out the assertion error guys so instead of waiting for a program to crash Midway you can actually start by making an assertion in Python well we assert that a certain statement is actually met and if this condition turns out to be true then excellent the program can continue but if the condition turns out to be false you can have the program to throw an assertion error exception for you guys well have a look at the following example on your screen well this is basically where it is asserted that the code can be executed in a Linux system guys so if you run this code on a Linux machine the assertion passes if you were to run this code on a Windows machine the outcome of the assertion would be false and what would happen well definitely the result would be something like this right so it would basically present us with an assertion error and says that that this code runs only on Linux right so I'm using a Windows justop in this particular case so let me quickly open up my pycharm and then we can actually run this piece of code to check out our exception guys so I'm back in my pycharm and I've typed out the code guys so let me quickly go ahead and run this as you can check out since this is a Windows machine it says this code can run on Linux only but this particular case this assertion would be true if you were actually running this on a Linux machine base so if any of you guys are using Linux machine make sure to try it out and on that note let's quickly come back to the presentation guys well in this particular example throwing an assertion error is the the last thing that the program will do the program will definitely come to a halt and it will not continue guys but what if that is not what you want what if you want something different from this or that you want the program to continue while displaying an exception for you guys well now that we actually have a fair idea about how we can go about understanding exceptions let us Dive Right Into the Heart of exceptions now yes I'm talking about the try and except block guys so basically the try and except Block in Python is used to catch and handle exceptions well python basically executes code following the try statement as a normal part of the program the code that follows the accept statement is the program's response to any exception in the preceding Tri Clause guys well as you saw in the earlier example that I actually showed you when syntactically correct code runs into an error python will throw an exception error and this exception error will actually crash the program if it is unhandled guys so the except Clause determines how your program responds to these exception guys so instead of actually crashing our program we can actually tell python what to to do when this particular exception occurs we'll check out the following function guys so this function can basically help us understand the try in the except block while the Linux interaction function can only run on a Linux system the assert in this function will actually throw an assertion error exception if you're using it on any other operation system apart from Linux well we can actually give this a try in pycharm so let me quickly open up that so guys I'm quickly here in my py Cham so let me quickly go ahead and select this and run it well this won't say anything right now because well if it was actually a Linux platform the message would have been printed right so let's quickly jump back to the presentation so basically if we run the tiny piece of code you see on the bottom the way we actually handled the error in this particular case is because we gave out a pass right well if you were to actually run this code on a Windows machine you would get the following output which is already on your screen right so it is a blank output you get nothing well the good thing here is that the program actually did not crash but it would be nice to see if some sort of exception occurred when you actually ran your code code well to this end you can actually change the pass into something that would generate an informative message like this check it out so let me quickly go ahead and run this in pyam well as you can check it out it says the Linux function was not executed right so basically this happened because I am on a Windows machine guys so let's quickly go back to the presentation and guys when an exception occurs in a program running this particular function the program will continue as well as inform you about the fact that the function call was not successful ful well what you did not get to see was the type of error that was actually thrown as a result of this function call guys so in order to see what exactly went wrong you would need to catch the error that the function threw well check out this particular piece of code I have for you guys so here is basically where we actually capture the assertion error and we print the output message to the screen so we have a tri block we have the same function and we have accept as error right so we're going to print what type of an error it is followed by a message for us right so running this particular case on a Windows machine will actually give us this following output guys so it says function can only run on Linux system and whatever you have chosen in the print block is actually being printed for us so let me run this in pycharm so as you can check it out we have the message printed right so it was the same as I told you so it was the same message as I told you before heading back to the presentation well in the previous example we actually called a function that we wrote ourselves right and after that when we actually executed the function we CAU the particular assertion error exception and then we printed it on the screen well I have for you another example where you actually open a file and use a built-in exception guys in fact let us dive into pyam and we can actually run this and we can come back and take a look at the output so guys I actually came into py Cham and I actually ran that particular piece of code why does it say could not open file log well guys basically if the file. log does not exist the block will give you this output right so it says the file could not been opened well this is an informative message and our Python program is actually still able to continue continue to run it is not crashed right well actually in the python documentation you can see that there are a lot of built-in exceptions that you can make use of and here is one exception that I have for you guys that you can check it out so it says exception file not found error right so basically this is raised when a file or directory is requested but that doesn't actually exist and this is just one among many that python offers to us guys well to catch this type of an exception and to print it on the screen we would actually use this following piece of code guys so in the tri block we actually going to open it as file and we're going to read something present in it guys we're going to accept it by pushing a file not found error as an object and later we actually end up printing that object right so let me quickly go into pycharm and we can check it out well as you can check it out we have error number two which says no file or directory right so it's perfect well you can have more than one function call in your Tri Clause actually guys and anticipate catching various exceptions as well well a thing to note here is that the code in the tri Clause will actually stop as soon as an exception is encountered but but there's one important thing that I want you guys to note down is that while catching an exception it basically hides all of the errors guys well definitely there are some unexpected errors in your program right when you're catching an exception and even these are completely hidden from you well this is why basically you should avoid using bare except statement Clauses in your python programs guys so instead basically what you can do is you can refer to the specific exception class that you want to catch and then handle those particular exceptions guys so look at this following piece of code I have for you so here BAS basically you first call the Linux interaction function and then later actually try to open a file guys so let us quickly jump into pyam and we can actually check it out so as you can see I actually executed the program guys again if the file doesn't exist running this code on a Windows machine will give you that output right so it's basically says that the function can run only on a Linux machine and then we have actually printed something guys well what we actually did here is inside the tri Clause we directly ran into an exception immediately and we did not get to the part where we'll actually try to open our file. log right right now basically when you actually run it on Linux machine it's going to says no file or directory found check it out here guys so this is the windows output that you get and this is the Linux output guys well at this point let us take a quick break and check out what are the key takeaways of this particular module guys so basically a tri Clause is executed up until the point where the first exception is encountered and inside the except clause or the exception Handler what we basically call it we can actually determine how the program actually should respond to that particular exception and we can actually manipulate multiple exceptions and differentiate how the program should respond to each of them right and lastly we actually learned why we should avoid using bare except Clauses guys and now that you're clear with this next step I have for you is the else Clause guys so basically in Python using the else statement you can instruct a program to execute a certain set of code only in the absence of exceptions guys well check out this following diagram on your screen so basically in the Tripod we have the code except when we actually want to handle the exception or else in case there is no exception we're going to run the piece of code that comes in under else guys so let me quickly walk you through the code guys so here is basically the same Linux interaction function and we have an assertion error here and we actually are going to print the error right so else if there is no assertion error so we're going to have an else clause and we're going to print saying executing the else Clause right so let me quickly open up my pyam and we can run this piece of code guys so as soon as I go ahead and run this it says function can only run on a particular Linux system right so as you can check out the output it says the function can run only on Linux systems right so in this case I have a Windows machine so we actually not printing the except Clause here but if you are running this on a particular Linux machine this is the output that you would get guys so it says doing something and executing the else Clause correct this happened because the program did not run into exceptions any exceptions right so else Clause was actually executed well you can actually try to run the code inside the else clause and catch possible Exceptions there as well well again we have the same Linux interaction here we're going to print it right so the Linux output says doing something and no such F directory found well this is basically for the else clause and we're using the same file example here but then we're actually trying to catch an exception in the else block as well so the output is pretty much straightforward as well right well from the output you can actually tell that the Linux interaction function actually ran well because no exceptions were encountered an attempt to open file. log was actually made and that file did not exist and instead of opening the file you actually CAU the exception which is the file not found exception pretty simple right and I hope you guys are clear with all of the concepts that we discussed until now and next up we need to check out how we can actually use the finally Clause guys well to begin with imagine that you always had to implement some sort of action to clean up executing your code guys well python basically enables you to do so using the finally Clause have a look at the following example guys check out the workflow on your screen I'm pretty sure we all familiar with try except and else at this point of time right so this is the chronological order that you need to go into first we have to try to check for an exception then we actually handle if there is an exception using the accept or else if there is no exception we're going to print whatever is there in the else block and lastly we have something called as finally guys so no matter what we're going to execute this in case if there is an exception the finally block is going to run in case there is no exception finally block is going to run there as well so let us quickly check out a coding example guys so basically this is the particular code I have for you guys and let me jump back into my pycharm and we can actually execute this so guys I'm back into my py Cham so let me quickly go ahead and run this well as you can check it out it says that the function can only run on Linux machines this is because I'm on a Windows machine right now and no matter what right so exception or no exception we have this message which is cleaning up irrespective of any exception guys so this is basically the use of finally guys and that is as simple as it gets so it's pretty much simple right well try accept else finally all of this is a really simple concept and once you understand what it actually does you can go about doing a lot using python guys well no matter what the python application is there is a very good chance that you'll actually make use of exceptions [Music] guys what is python request request in Python is a module which can be used to send all kinds of HTTP requests it is very easy to use and has features like passing param meters in the URL and passing custom headers as well it is a humanfriendly HTTP Library as it also suggests on the official documentation page as well and it is one of the most downloaded python library with more than 400,000 downloads every day so you can imagine the popularity of python request guys it was written by Kenneth rites and has a license under Apache 2.0 so this is about python request guys I hope you are clear with what exactly is python request so now that we have learned what is python request let's take a look at the advanced features that python requests come with so first of all we have keep alive and connection pooling and then we have Elegant key value cookies then coming on to the next one we have international domains and URLs and automatic decompression as well and then we have sessions with cookies persistence and uni code response bodies then we have browser style SSL verification and https proxy support as well there is automatic content decoding and multiart file uploads and then there is basic authentic ation and streaming downloads as well and last but not the least we have connection timeouts and chunk requests so these are all the advanced feature guys don't worry we'll be learning about how we can use Python request so let's talk about why exactly do we need python request or why are we using python requests so the reason behind this is pretty simple guys we use Python request because you don't have to manually add the queries to your URLs and form encode post data and that is a reason enough to use Python request guys and if you're not able to understand this reason behind using the python request you'll be able to understand this later on in the session when I'll be showing you the use case so hang in there now that we are done with the reason behind why we are using python request let's try to understand how you can install python request on your system or on the project that you're working on so first of all you have to install the PIP environment on your system and then you have to run this command that I'm going to tell you inside the terminal which is PIP install request guys so let's take it to pycharm and I'll show you how you can install request on your project or on your system guys now as you can see I have a new project over here inside pycharm so I'll show you guys how you can install request on your system so you have to open the terminal here just write pip install requests and you will see that the files are being installed here it will take some time guys and there is one more approach to this if you're using py charm you can open the settings and inside the settings you will find the project interpreter and over there you can simply add the request library and that will do the job for you guys it is same as running this command inside terminal okay I'll show you that as well so you open settings and here you will go to the project where you will find the project interpreter and over here you can simply just add your file let's search requests here and this is by author kenth rites that I have told you guys so I'll just install this package and it will not take so much time because we have already installed it and as you can see we have request over here inside the so you can see request over here which has all the files and everything for our project so I'll just type okay close the terminal and over here I will make one demo file guys to show you how you can make get and post request so we have this file let's enter okay let's first of all talk about making get and post request guys so we have installed request on our system guys so let me talk about get request what exactly are they used for and how are we going to use them inside the tutorial I'm going to show you or the use case as well so get request are basically used to request the data from the server so what you have to do is I'll show you the syntax as well so you just have to import the request library and then you will have to make one response object in which in this case r is the response object and you are going to use the request library and make the get request and inside the get function you have to mention the URL that you're going to make the request from so let's take it up py charm guys I'll show you how you can make the get request so let's enter the presentation mode for better visibility so I'll just import the request Library yes and I'll make one response variable or response object and inside this I will use request.get and inside this I will have to specify one URL guys so I'll just show you what URL I'm going to use so what I'm going to do is I'll just copy the URL over here and when I make the print statement I will use the response object and let's get the text over here using the response object whatever we have on the server and when when I run this I will be getting the text for this URL that I've just provided as you can see over here so this is how you can make a get request guys now let me show you how you can actually pass parameters inside your url also so what I'll do is I'll just make one variable let's say payload and inside this I will specify a dictionary with some values let's say key one and value one okay so when I pass this inside the get function with the parameter that is params is equal to that is the parameter variable guys so I'll just pass payload over here and when I get the URL now I should be getting this parameter that I have just passed inside of URL so you will be getting this dictionary of the value that I've just passed as a parameter inside the URL so this is how you actually pass parameters inside your url guys while you're making the get request similarly you can also pass custom headers as well I'll be showing you that later on in the session so we have talked about making get request guys so let me just talk about how you can make the post request as well so we'll talk about what exactly post request are used for so basically post request are used to submit the data to be processed to the server so let me show you an example with the syntax first of all you have to import the request library and then you have to use the post function as well using the request keyword after that you have to mention your URL and the data that you want to give it as a payload or something like that and then you have the response variable as well or response object so let me take it up to pycharm guys you'll be able to understand this better in pycharm so instead of get request we'll get post over here and instead of parameters we'll just pass this as data so instead of uh text okay we'll just get the text again so let's see what are we getting so we don't have post request allowed in inside this so instead of get we'll just write post over here run it again as you can see so we have this dictionary that I added using the post request Guys these are the headers and then we have cookies as well no we don't and this is how you can actually make a post request guys I hope you are clear with making the post request and get request guys so this is all about making a post and get request guys also I want to show you what are all the content or the response content that you get using these requests so let me just take one response object again I'll make a get request so I'll just pass the URL over here and we're good to go so we will print whatever we'll get so using the response object let's see the content type or what all content we can get so we can get text we can get headers cookies and then there is status code let's check the status code first guys if the status code is 200 we are good to go it is 200 guys so we'll get some more content from this so we'll get cookies now now it will be showing us the cookies inside our cookie jar yes we are getting the cookies inside the cookie jar and we can also pass our own cookies I'll be telling you about that later on in the session don't worry guys so then there is headers as well you'll see all these headers inside a dictionary guys so this is how you actually access the headers inside a URL and then there is the content format also you can get the content in the format of Json as you can see we're getting the content as Json objects so this is all about response content guys now let me get to the next topic that we have which is cookies and headers guys so I've just shown you how you can get the cookies and headers inside using the response variable so let me just talk about what are cookies and headers guys so we can view the servers response headers using a python dictionary which I have just shown you guys so the dictionary is special though it just made for the HTTP headers and similarly you can get the cookies as well if a response contains some cookies you can quickly access them and to send your your own cookies to the server you can just use the cookies parameter and cookies are returned inside a request cookie jar which acts like a dictionary but also offers a more complete interface suitable for over multiple domains or parts so let's try to understand this inside py charm guys so what we'll do is we'll just get some cookies here and when I run this we're getting a cookie job okay guys what I've done is I have actually changed the URL so what I'll do is I'll run this command now and you'll be seeing the cookies over here which is inside the reest cookie jar and what I can do is I can also pass my own cookies so I'll take a variable name it cookies and give it a dictionary guys and inside the dictionary I'll specify some values let's say key 1 is equal to Value one and then over here using the cookies parameter I will just pass cookies here so I'll just write text over here and when I run this I should be getting a dictionary with the cookie jar which actually has the cookies name named as key1 and value one so this is how you can actually make your own cookies inside a web server and then I'll show you what you can do with headers as well so let's get the headers here when I run this I should be getting the headers inside this URL which are all these headers over here inside the dictionary so this is how you can actually access the headers inside the URL guys so let me talk about session objects guys which is the next topic that we have so session object actually allows us to persist certain parameters across request so it also persist cookies across all requests made from the session instance and we'll use the URL Library 3 Connection pooling and if you're making the several request to the same host the underlying TCP connection will be reused which can result in a significant performance increase and also a session objects has all the methods of the main request API as well so let's try to Pur some cookies across the request guys so we'll move to the py charm again so we have come to the py charm now what I'll do is I'll make one variable let's say session object that is so I'll use request do session and after that I'll just use a get statement where I'll just use S.G pass the URL and we're good to go then what I'll do is I'll make a response object and inside the response object I will pass one more variable or URL which is this one now what I'll do is I'll just print response. text let's see what happens as you can see we have persisted one cookie so this was all about session objects guys and any dictionaries that you pass to a request method will be merged with the session level values that are set and the method level parameters override the session parameters as well and you have to consider that method level parameters will not be persisted across request even if making a session as well so now that we are done with session objects let's take a look at errors and exceptions that we have for python request guys so first of all in the event of a network problem for example a DNS failure or a refused connection request will raise a connection error exception and it will raise a HTTP error if the HTTP request returned an unsuccessful status code which is actually 405 or 404 as well now if a request times out a timeout exception is raised and if a request exceeds the configured number of Maximum redirections are too many redirects except is raised so this is all about errors and exceptions that we have in Python request guys so now that we are done with errors and exceptions let me talk about a small use case that I have made using the Django app so we will exit the presentation mode over here and I will open the project that I had made so as you can see I have ajango file over here with all the init settings.py urls.py so I have over here the URL configuration ations and everything and I have a views.py file inside my app that I had created so this has the code for making a request to a web API and inside that I will be able to search for a particular City and I will be getting the description of the weather and the temperature over there and the condition of the weather over there so I will show you this and this is my template guys so let me run the server guys and if you're wondering how you can actually make a web app I suggest you check out the Eda tutorial for Jango web applications where you will be able to learn how you can make a web app from scratch guys so when I run my server I should be getting a URL guys so that's where I have to actually make the request so as you can see I have City London over here specified first of all so when I open this first of all I have a heading which is saying what is the weather like then I have the icon which is saying it has broken clouds then there is London city name and the temperature in fahrenheit so let's see if I can do that since I'm making request to the API when I change the city over here I should be getting the temperature of the city again so when I refresh this now as you can see I've changed the city as well but I'm getting the temperature of the city that I have just specified guys so as you can see how easy it is to make request to a web server and get these values so I'm making this request to this URL guys which I have specified over here open weather map API this provides us the API and I'm able to search for cities over there inside the request that I'm making and this is how you can actually make a simple Django app for calculating or getting the weather of a city using the request Library [Music] guys let's take a quick look at the time module and its built-in methods the time module consists of all time related functions that are required to perform various operations using time it also allows you to access several types of clocks required for various purposes the time module begins recording time from the epoch Epoch literally means a time in history and it begins on 1st January 1970 as you can see on the screen I've listed down some of the most important functions that are available in this module so talking about the first function which is time it Returns the number of seconds that have passed since the epoke the C time method Returns the current data and Time by taking the elapsed seconds as its parameter sleep stops the execution of a thread for a given period of time the local time method Returns the date and time in the struct time format by taking the number of seconds passed since the epoke as the parameter GM time on the other hand Returns the current date and time in the UTC format the MK time method is inverse of local time it can take a tuple of Maximum 9 parameters and Returns the seconds pass since the epoke and finally the ASC time method which can also take up to nine parameters and returns a string representing the same okay so now I'll jump on to my Jupiter notebook and show you all how this works so the first thing that you'll have to do is import the time module so I'll say import time and I'll hit Run Okay so first I'll be making use of the time method present in the time module so I'll just say time. time and I'll hit run so like I've already told yall this method return learns the number of seconds that have passed since the epoke so I'll hit run okay so here is the number of seconds that have passed since the epoke now if I want to convert it to the current date and time I can make use of the C type method so I'll just have to use time. C time now if I want to convert this into the current date and time I can make use of the C time method to do this I'll just use time do c time and I'll have to pass the number of seconds as a parameter to to this so as you all can see on the screen I've passed the number of seconds which was returned by the time method to the C time method and I've got the current date and time if you want more information about these functions and methods all you can do is use the help method so I'll just say help time and if I want to fetch data about the time method I'll just say time. time and I'll hit run so as you all can see it says that this this method Returns the current time in seconds since the epoke similarly if you use help on any method or function it will return the corresponding information about that function okay so moving on I'll be making use of the local time method and see what it returns so I'll just say time do local time and I'll hit run so as you can see on the screen I've got the current date and time in the struct time format now to make youall understand what the struct time class is let me get back to my presentation okay so as you all can see the stru time class basically has nine attributes starting from the year whose value starts from zero and goes up to 99,999 the second attribute is month whose value will be anything from 1 to 12 after that is mday which means the day of the month and the values can range anything from 1 to 31 similarly our minutes and seconds have the values 0 to 23 0 to 59 and 0 to 61 the weekday value can be from 0 to 6 where 0 is Monday y day or your day can be anything between 1 to 366 and the final attribute is daylight saving time it's one if it's summer zero if not and minus one if unknown okay now that you're aware of what this stru time format is let's come back and see what our output actually means year is 2019 month is 8th or August the date today is 8 as well and so on so I hope you're clear with this okay so now in case you want to fetch the number of seconds that have passed since the epoke so to do this you can make use of the MK time method okay so all I'm going to do over here is first store the local time in a variable say a and after that I'll take another variable B wherein I'll be using the MK time method and I'll pass the parameter to to it as a finally I'll just print out B so as you can see on the screen the MK time method has returned the number of seconds that have passed since the epoke till now so now in case you want to fetch the current date and time from the struct time format and return it in local format you can make use of the ASC time method so like I've already told you all before the local time method Returns the current time in the stru time format I'll be using the variable a which stores the struct time format of the current date and time to convert it to the local format and to do this I'll create some new variable say C and then I'll use ASC time method so time. ASC time and I'll pass the parameter to it as a and then I'll just print out C okay so as you all can see over here it has taken the struct format and has returned the local format of date and time python also allows you to format and pass strings using the SDF time method and the S strp time method the sdrf time method can take a tuple containing nine parameters and returns a string representing the same depending on the format code used the Str strp method passes a string and returns it in the struct time format to show youall what these format codes are I'll just use the help method and I'll pass time. sdrf time as a parameter to this and I'll hit run oh I've made a spelling mistake over here sorry guys it's s strf time so as you all can see over here I have a number of format codes which I can use to format my date time strings okay so first let's make use of the sdrf time method so as we all have seen I've created a variable a in which I've stored the current time in the stru time format so let me just print this out again okay so now I want to format this output in a manner which which is convenient for me so to do this I'll use the S strf method so I'll just say time I'll take another variable say x is equal to time do s strf time I'll use the S strf time method within which I'll be using the format codes to format the date as per my choice so first I want to print out the month so I'll use the format code mod M it's small M guys please be careful because this is case sensitive and then between this I just want to see a slash after this I'll just print the date I'll use mod D and then the year so I'll use mod Y sorry for the spelling mistake I'll just print X so as you can see on the screen I've returned the current date in the month day year format similarly you can also pass a string and return it in the struct time format to do this you can make use of the St strp time method to do this I'll take another variable say Y and I'll pass a string to this I'll say 8th August 2019 and then I'll make use of the S strp method to pass this string so I'll just say time. sdrp time and I'll pass y as a parameter to this and the format of it which is day month and year capital b means the full name of the month and now hit Run Okay so as you all can see over here I've passed a string using the Str strp method and returned it in the struct time format so now let's get back to our presentation and take a look at the date time module the date time module is similar to the time module consisting of all the required methods that are essential for working with date and time some of the important methods present in this module are the date time method which is a Constructor and it consists of two methods which is today and now that will return the current date and time the date method will help you create a date and the time method will help you create time which includes hour minutes seconds micros seconds and the time zone information the datetime module also consists of a front PR stamp method which converts the seconds to return the corresponding date and time another very important function that's present in this module is time Delta time Delta basically returns a duration which will be the difference between two different times okay so now let's get back to our Jupiter notebook and see how this module actually works I'll just create a heading over here say date time and I hit run the first thing that I'll have to do is import this module so I'll say import date time after this I'll be making use of the date time Constructor to create some date so I'll say date time and then I'll pass some values to this starting with the year which is 2019 and the month as 6 date as 7 time as 4 minutes seconds and milliseconds and then I'll just print this out so as you can see on the screen I've created some random date using the date time Constructor now in case you want to fetch the current date and time all you can do is use the today and the now methods like I've already told you all previously so I'll say date time so I'll just copy this and then I'll use the today method so as you can see over here it has returned the current date and time along with milliseconds which is very precise I can also use the now method instead of today method over here so I'll just say now and I'll hit run so as you can see both these methods return the current date and time now in case you want to access some particular value from this date all you can do is use the attribute name to access that particular value so in case I want to access the year and the month all I can do is save this into a variable say V and then I can use v.e to access the year so as you can see I've accessed just the Year from the current date similarly you can access anything of your choice so if I say print v. Monon and then I'll say print v. R and I'll hit run so as you can see the month and the hour have been returned similarly you can access any values of your choice using these attributes now in case you want to set only date without time you can use the date method so you just say date time do date and then pass the date as a parameter the time method can be used to create time without date so I'll just say it's 3 hours 45 minutes and 23 seconds so now moving on towards time Delta which is a very important method that's present in this module like I've already told y all this method represents a duration which can either be the sum or the difference between two different dates now to make use of this method I'll just take some variable say B1 and I'll say date time do time Delta and I'll pass the number of days as 20 to this you can also pass other parameters like micros minutes Etc okay I'll take another variable say B2 and I'll use the same method again and to this I'll pass the number of days as 30 now I'll take another variable say B3 within which I'll just store the difference of B1 and B2 so I'll say B1 minus B2 and then I'll print what is B3 and the type of B3 so as you can see over here the difference between B1 and B2 is minus 10 days and the time since I've not specified anything is zero and the type of B3 comes under the time Delta [Music] class why exactly we use regular Expressions we are going to focus on various problems that regular Expressions helps us to solve so this is the first problem guys so over here what happens you have a log file and from that log file you want only date and time and as you know with the format of the log file is not understandable to everyone so here's a guy who only wants date and time from that log file so what he can do he can make use of regular Expressions identify the pattern and he can actually get the date and time for that particular log file so this is the first problem we'll see a lot of more problems which a regular Expressions help us to solve so we'll move for forward and we'll see the second problem now this is the second problem that it helps us to solve imagine yourself as a salesperson and you have a lot of email addresses and many of them are fake so what you can do you can make use of regular expressions and all the email addresses have a particular pattern they have a format so with the help of regular Expressions you can verify that format and you'll get to know what all email addresses are correct and what all email addresses are fake ones so as you can see obviously s.com is a fake email address so using the pattern that is there in all the email addresses you can verify whether that email address is correct or not with the help of regular Expressions all right so let's move on and see what are the other problems so this is the third problem that it helps us to solve we'll take the same sales guys example that we have taken in the previous slide now over here that guy has a data of various customers phone numbers and we know that a lot of time people don't give the correct phone number because nobody wants a sales guy disturbing them at all timings right so that salesperson has a problem he wants to know that what all phone numbers are correct and what all phone numbers are fake so every correct phone number will have a particular pattern or a format so using regular Expressions he can identify what all phone numbers are correct and what all phone numbers are wrong and at the same time different phone numbers that belongs to different countries or different location will have a different format so again taking that thing into consideration he can again use regular expressions and identify to which area it belongs to now in this particular example what I've shown you is I've shown you two geographical areas one is India another is USA so with the help of regular Expressions you can actually find out what all phone numbers belong to USA and what all phone numbers belong to India so I hope this is clear to you so this is the fourth problem guys now over here what happens you have a student database and that contains name age address now what happens there are a lot of students who live in a particular particular area and the area code of that area is 596 but what happened the area code has been updated and it has become 5976 now if you go manually update that for all the students that'll take a lot of time so instead of that what you can do you can make use of regular expressions and find out all the area codes from the student database and after that you can replace it with the updated area code or the PIN code so as you can see with regular Expressions you can even replace a particular string we'll move forward and we'll see one more reason of using regular expression so now this is one more reason to use regular expression because even if you don't have a python background if you have any other programming background like Java PHP C gry Swift Ruby you can use regular Expressions because it is compatible with all of these programming languages and it is even compatible with JavaScript although it is not written here but yeah it is even compatible with JavaScript so till now we have seen various reasons to use regular Expressions but I know you guys must be wondering what exactly regular Expressions is right so don't worry guys we'll move forward and we'll see what exactly regular Expressions is so a regular expression is basically used for describing a search pattern so you can use regular expression for searching a specific string in a large amount of data you can even verify that that string has a proper format or not you can find a string and replace it with another string and you can even format the data into a proper form for importing for example so these are all uses of regular Expressions now over here I've shown you an example here there's a string which is present in which they have written Janice is 22 and Theon is 33 Gabriel is 44 and Joe is 21 so as you can see what all useful data that I can find from here only name and age I don't want the rest of the things that are present in this string so what I can do I can identify a pattern and with the help of regular expression I can convert that to a DI iary and in that dictionary only the name and age will appear now my question is to you all guys can you identify a pattern in order to get the name and age just give a guess you just need to identify the pattern all right so Sarah says all the ages are in the form of integers all right Sarah that's a very nice observation but what about the names all right so I've got a couple more answers and they say the same thing that the age is represented with the help of integer numbers so you can get the integers for the name they have no idea how to get it don't worry guys it's totally fine just give a guess all right guys so let me tell you so basically if you notice that every name starts with an uppercase first letter so the first letter of all the names is an uppercase letter and when we talk about age all of the age are represented in numbers so what you can do with the help of regular expression you can actually find out the two-digit numbers and at the same time you can find out the words that starts with an uppercase letter and then it goes onto lowercase letters and the star actually represents all of them so you don't need to worry about the syntax that is there in front of your screen I'll be actually explaining you these things in detail so you don't need to worry about that so I hope you have understood the pattern that I'm telling you all right so we have a question from Devon he's asking what is this may contain a name of a place all right Theon so this string does not contain a name of a place that's all I can say right now if there was a place present here then we have to look for a different pattern at that time so basically with regx you have to make sure that you identify the pattern for the string that you want so it is very important so let us move forward and I'll open my pie charm and I'll tell you how to get age and name all right so let's do it so this is my pie charm guys so over here what I need to do is I need to First import the module for regular expressions for that I'll type import re and now I'm going to type in the string that was there in the example so I'll type in name age equal to and now I'm going to write it Janice is 22 and Theon is 33 now the next line I'll write Gabriel is 44 I forgot the space and Joey is 20 1 now close it so now for age I'm going to type in ages equal to now you don't need to worry about the syntax that I'm going to write I'm going to tell you all these things in detail so this is just an example that I'm giving you that with the help of regular expression you can actually get the name and age after that I'm going to discuss each and every operation in detail so what I'm going to type in you don't need to worry about that I'm just showing you that you can actually do it I'm just giving an example all doind all are 1A 3 because all my ages are two digits are of two digits and then write the name of the string and for names what are you going to do you're going to type in names equal to similar to that re doind all R and my first letter is an uppercase letter that is between a to zed after that I have lower cases so I'm going to type in A to Z in lower cases and then I'm going to put in a star that will actually include all of those and finally the name of that string that is name H now what I'm going to do is I'm going to Define a variable and I'm going to name it as age dat equal to curly braces let's keep it empty right now and now one more variable I'm going to Define that is X is equal to Z now I'm going to use a for Loop I'll type in for for each name in names age dict each name ages X X+ equal to 1 now print this age dict yeah so if you're not able to understand what exactly for loop is and what are dictionaries so you can go and watch the previous tutorial videos on python and then come back again now let's go ahead and execute this so yep you have got the dictionary that includes only name and age of that person so what I've done here is what is a pattern I'm repeating it again so for all the names the first letter is an uppercase letter and all the ages are expressed in terms of numbers so using that pattern I have got only name and AG so this is just an example we'll actually see how to perform various operations using regex or regular Expressions so I'll go back to my slides now let's move forward and understand the cursor operation of regular expression so basically in both strings and regular expression both of them have their own cursor so what happens is suppose you are searching for R and your regular expression includes R so your cursor is at R in the regular expression but for text it'll check for B it is not equal to R then e then e then again when it comes to R it will see see that this is what you are searching so it'll stop right there the moment you search for S it'll go on and it'll come towards s and same goes for n as well so this is a pretty basic operation but it will really help you to visualize how things work with regular Expressions so we'll move forward and we'll look at various operations that you can perform with regular Expressions so first we'll find a word in a string so we have a string that says we need to inform him with the latest information and we need to find the word in form so let us go ahead and execute this practically I'll again open my py charm so this is my py charm guys let me just first clear it so let's begin by importing our regular expression module so for that I'll type import re and now we have a sentence and we want to find a word in form there so what I'll do I'll just type in if r do search inform and our string is we need to inform him with the latest information so this is our string give a colon and now I'm going to type in print there is in so let me tell you what I've done here first I've imported the regular expression module and after that I've defined a condition which says that if it is able to search in in this particular string that is we need to inform him with the latest information then it'll print there is inform otherwise it won't print anything so as you can see that inform is present here so it should print there is in so let us go ahead and execute this so yep you can see that it has printed there is in now let's be more specific and suppose we want to find all the matches here so let's write a method that will do it for us so for that I'm going to use find all which will return a list of matches for me so for that what I'm going to do is I'm going to write in here let me first remove this and now what I'm going to do is I'm going to write in all in form which is nothing but a name that I've given you can give whatever name you want ar. find all and I've told you earlier as well find all will return a list of matches now over here what I need to find I need to find inform from which string the string is we need to inform him with the latest information all right guys and so now I'm going to type in a for Loop which says that for I in all inform form give a colon print I that's it so now what I've done here you can see that I've imported the regular expression module then I'm using find all in which it will return all the matches and what I'm finding I'm finding inform and from which string I'm finding inform this is the string that I'm using and I finally I've used four in order to find all the matches so let's go ahead and execute this so yep it has found ined twice because you can see that it is available here as well in and even information over here you can see that inform is present so this is how basically you find a word in a string by the help of a regular expression so let me first go back to my slides so we have seen how to find a word in a string next we are going to understand how to generate an iterator so basically what I'm going to do is I'm going to get the starting and the ending index for the word that I'm searching over here I'm searching inform so it'll give me the starting as well as the ending index for all the matches that it finds so let us go ahead and execute this practically I'll open my py once again let me remove all of this and over here I'm going to define a string so what I'm going to write here is I'm going to write here we need to inform him with the latest information yeah so now what I'm going to do is I'm going to generate an iterator by using find iterator and it is going to return the iterator of the matching objects so basically I'll be printing the starting and the ending index of the matching object so for that I'll use the for Loop so I'll write in for I in re. find iterator and I want it for inform so I'll just type in inform and now I'm going to type in the name of the string that is Str Str colon now I want that to be converted into a topple so for that I'm going to type in locktop equal to I do span and finally print lock top so basically it will print the starting and the ending index of the matching object so let us see if it does that so yep so as you can see that for inform the starting index is 11 and the ending index is 17 for information it starts with 38 and ends at 44 so this is how you can actually generate an iterator by using find iterator and this will return the starting as well as the ending index of the matching object so if you have any doubts you can write it down in your chat box it's pretty easy guys so we'll move forward and we'll have a look at the other operations that you can perform with regx so now what I'm going to do is I'm going to match words with a particular pattern so we have a string in which we have sat hat mat and part so now my question is to you all guys what is the pattern that you can see in this particular string that is sad heart Martin part so let me go ahead with it and I'm going to show you how you can match words with a particular pattern using regx so first I'm going to open my py charm let me remove all this so let me Define a string once more I'll type in SD so we have sat Matt hat Matt and we have Pat this is our string now I want to match anything that ends with at so for that I am going to Define a variable all Str Str and now what I'm going to do is re doind all now what I want I want words that starts with S H M and P all right and after that it should end with at so I've written add it's very simple guys and now what I'm going to type in I'm going to type the name of the string that is Str Str so this bracket where I've written sh MP it shows that I'm matching specifically for the words that starts with s hm and P now I'm again going to use a for Loop in which I'm going to type in for I in all string give a colon print I it's that simple go ahead and execute this and see what happens so yep it prints hat mat and Pat but one thing to notice here guys it will not print sat why it is not printing sat because it is an uppercase letter so if I go here and I make it as capital S and go ahead and execute this so yep you can see that even sat has been printed so let me go back to my slides and see what all other operations that we can perform with regular Expressions now over here what I'm going to do is I'm going to match series of range of characters now what does that mean let me explain you with the example that is there so now what I want here is I want all the words in which the first letter is between the range HED to M so all the words whose first letter comes in the range H to M should be printed and it should be ending with at so basically it should print hat and mat because P comes after M so does s so it won't print sattin part now let's go ahead and see in our py charm whether this happens or not now over here what I'm going to do is I'm going to remove this s so plus P throw in a hyphen here and execute this so yep it has printed hat and mat so if I want all these things to be printed what I can do is I can write in here Zed and execute this so yep it has printed hat mat and Pat but it has not printed s why because it starts with an uppercase s so if I make it as s and I'm going to print this so yep it has printed sat as well because between H to Z all S H M and P lies so it has printed all the range of characters whose first letter is present in the range H to Z so now I can use a carrot symbol that will include all the words apart from ones which have the first letter between the range H to M so let me first make it as M and now I'm going to use a carrot symbol yeah so so when I use this carot symbol this means everything apart from this range that is H to M go on and execute this and you can see it has printed sat and Pat but not hat and mat because of the carrot symbol that I've just shown you right so I hope you all are clear with how to actually find a range of characters in a string and now what I want I want to replace a particular string so as you can see in the example I have a string in which we have hat rat mat and part but I want to replace this rat I don't like rats all right so jokes apart I want to replace this rat and I want to replace it with the word food now let me show you how actually you can do that I'll again open My Pie charm let me first remove all of this I'll Define a string I'll name it as food although there won't be any food items in it but yeah just for the demonstration purpose hat rat mat and Pat now I want to replace rat with food so now I'm going to compile a regular expression into what are called pattern objects and pattern objects provides us with additional methods and one of which is substitute let me show you how you can do that just type in regx for example yeah regx equal to re do compile and I want to compile R which ends with at and now in the Str food I want to substitute it so I'll type in regex do SUB what I want to substitute I want to substitute food in which string I want to substitute I want to do that in the string food itself so yeah and now finally go on and print it so as you can see we have hat food mat and Pat so we have replaced rat with food so this are pretty simple examples that I'm giving you right now but with these Basics you can actually go on and find out a pattern and you can perform a lot of operations now there's one very important thing that I want to talk about it is called solving the backs slash problems now let me show you what I'm talking about so I have a string let me name it as a random string so I'll write it Rand s Str equal to here is drogar all right so when I go on and print this let me show you what comes so we got only single backslash but we have typed in here double backs slashes so this is what I'm talking about now in order to solve this problem what I can do is I can make use of regular Expressions so let me show you how you can do that so all you need to do is you need to type in re. search so this is re is basically we call it as rawring so raw string will treat back slashes as special so let me show you how it'll do I'll just type in re. search then R quotation drogar comma and the name of the string that is random string go ahead and execute this so y it has solved our problem and you can see that double backs slash appears over here so this is what I'm talking about the backs slash problem all right so we have no doubts let me open my slides again and we'll have a look at the other operation that you can perform with random strings with that you can perform with regular Expressions now what I'm going to do is I'm going to match a single character so I have 1 2 3 4 five is my string and now I want to print only the fifth digit so it should print five but before that we'll have a look at a couple more examples in which I'll be telling you about how to deal with white spaces so let me open my py charm again let me remove all of this yeah so now I'm going to Define one more string let me name it as random string equal to keep the blue flag flying high and Chelsea this is nothing but my favorite club and let me just print this random string so that's printed keep the blue flag flying high now let's go ahead and remove the new lines with the space so for that what I need to do is again I'm going to Define a variable let me L it as regx equal to re. compile we have talked about compile in the previous example so I hope you can remember it so the symbol for new line that is a back sln then I'm going to type in the random string equal to regx substitute it with what what do you want to substitute it with just a space right so just give a space in between that's pretty easy and just type in here random string now go ahead and print this random string yeah so as you can see earlier it was like this keep the blue flag in one line Flying high in one line Chelsea in one line but with with the help of regular expression I've replaced the new lines with a space so this is how you can work out with the white spaces so this is how you can work with white spaces now there are other white spaces that you'll be able to work with for example let me just show you so you have a back SLB that stands for what it stands for backspace then you have back slf for form feed then you have back SLR for carriage return similarly you have back SLT for tab and you have have vertical tabs as well so for that we have back SLV vertical tab so these are the white spaces that you can work with now let's see how to match a single character that we have discussed in our slides so let me first remove all of this all right so let me Define one more variable I'll name it as random string again and I'm going to type in here 1 2 3 4 5 and now what I'm going to type in I'm going to type in print matches I'm going to give a colon then I'll type in length re. findall back SLT in random string now over here you can notice that we have back SLT so basically this back SL D will match any numbers any any numbers which are present but if I write a capital D over here so it will match anything but a number so let me keep it as back SL D and print this and show you what it'll give us so we have five matches as you can see that we have five digits now if I make it as capital D so that will return me everything but the digits so let me just show you I've converted this to Capital D go on and execute this so we have zero matches now so now if I I want to match a specific digit I'll be using curly braces so let me show you how you going to do it so in the curly braces just type in five and it should print that for us so y it has found one match that is present over here so I'll now what I'll do I'll clear this again and I'm going to Define in one more variable say Let It Be num over here I have 1 three then I 1 2 3 4 then I 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 and 7even now we can even match digits with a certain range let's say our range is between 5 to 7 so for that what I'm going to type in I'm going to type in a print statement and again I keep it as matches give a comma and now I'm going to type in length re do find all what I'm searching for I'm searching for digits so back slash d and what is my range range is between 5 to 7 give a comma and then type in the string name that is num go ahead execute this and it finds three matches because over here we have five over here we have five and six over here we have five six and seven so it has found the three matches that we were talking about so this is how you can perform basic operations using regular Expressions now we have seen all basic operations that you can perform with regular Expressions now how about I show you what are the various applications of regular Expressions I'm going to take in some use cases a problem statement and I'm going to show you how you can use regular expressions in order to solve those problems how about that guys all right so I see a lot of people are interested in that so without any further Ado let us move forward and see how you can do that all right so we have a first problem in which we need to verify the phone numbers so we have a list of phone numbers and the general format of a phone number is it should start with three digits then a hyphen sign then three middle digits and the three digits in between and a sign and then four digits in the end so this is what a basic format of a number is so let us go back to our py charm and verify these numbers with the help of regular Expressions let me remove all of this so before I start writing out the code let me explain you something so we have back slub so basically what it does is it is same as writing down lowercase A to Z uppercase A2 Z even numbers and underscore so lowercase w is actually equal to this thing that I have typed here so it will match anything that is inside the brackets when we talk about uppercase back slw it will match anything but the things inside the braces so it is similar to using a carrot inside the brace so I'll just show you so it'll match everything but the things that are present inside the braces so this is what I'm going to use in in order to write the code for my validating phone numbers so let me just uh make a string that has my phone num so I'll just type in as phone and I'll put here 412 Tri 5 and say 1 2 1 two all right now I'm going to use a condition statement that says if re. search so anything that has three digits then it should have a hyphen sign then again anything that has three digits then again a hyphen sign and anything that has four digits give a colon go on and print it is a phone number now basically instead of back slw I can even use as back SLT because all of these are digits but let me just keep it as back slw only and go on and print this so yep it is a phone number but what if I am going to change here and I'm going to make it as triple 5 one let me show you it is not printing anything because so because our regular expression says that it'll take only three digits now as I've told you earlier as well W Takes A to Z lower case and upper case as well so even if I replace it with d it won't matter let me show you go on execute this so y it has not printed anything but if I go on and remove this one and print it again so yep it has printed a phone number now if I were I've added here a as well it won't print that so yeah so basically I wanted to tell you what back slw is that's why I've written it over here now let me tell you a use case of this black slw as well so if you want to see that a full name is a valid full name or not so what you can do is you can use back slw at that time so basically why I've explained you back slw is this is how you can use back slw all right I'll give you one more example so now let me remove all of this and now we'll see if the full name is valid or not basically there should be a space between first and last name so for that I'm going to use a back SLS which is same as writing down back slf that is form feed or a back sln that is a new line or a back SLR that is a carriage return or a back back SLT that is tab or vertical tabs so it is pretty much similar to that so it is exactly the same similarly if I write here an uppercase s can you guess what it'll be all right so everybody almost guessed it so basically it will be equal to a carrot and form feed new line back SLR tab as well as vertical tabs so it will include anything but back slf back sln back SLR back SLT and back SLV let me just comment this all right so now let's go ahead and validate whether a valid full name is present or not so for that what I'm going to do is I'm going to type in a condition if r. search now over here what I need to do is can you guys tell me all right so first I'm going to type in w and range of w should be between 2 to 20 then in between I want a space so I can just type in as now in between what I want I can have either a new line or a tab or a vertical tab anything like that so I can give a back slash S and one more back slw for the last name and the range of the last name can be from 2 to 20 and finally I'm going to write the name I'm going to write my own name give a colon and print full name is valid go ahead and execute this so yep as you can see that it has printed the full name is valid so if I convert this lowercase s to an uppercase s it will basically be equal to a carot and form feed new line vertical tabs tabs so anything apart from all of these so I hope you have understood the concept that I've explained you till now all right so we have no doubt so now I'm going to move back to my presentation again so now this was one use case now our Second Use case is we need to verify the email addresses so our email addresses should have 1 to 20 lowercase and uppercase letters numbers plus period underscore percentage plus minus it should have an atate symbol then again 2 to 20 lowercase and uppercase letters numbers plus period and a hyphen then we should have one more period and then two to three lowercase and uppercase letters so let me go ahead and show you practically how you can do it let me remove all this and let me first write a list of emails so you can write it as SK atate aol.com I can have MD ad.com adate seo.com now I can have one more and I can name it as DC add.com let me just remove the space all right so now what I'll type in I'll type in print email m give a colon and after the space I'm going to type in length re do find all open the parenthesis and over there in the Square braces what I want first I want anything then I want a period I want underscore percentage plus hyphen close the braces and it should range between say 1 to 20 after that what I want I want add theate after add theate I want anything for that I'll type back slw dot that is period Then I want a hyphen and what should be the range it should be between 2 to 20 characters after that I want a period and then I want uppercase and lowercase letters then it should range between two 2 three email all right guys go ahead and execute this so it has found only one match so which is SK aol.com now if I actually copy paste this once more in the end and print this so yep we have two matches so this is how you can match the email address you just need to identify the format of it and you can do it all right so we have no doubts now let us move move forward and see the third application of python regular Expressions so you can perform web scraping using regular Expressions now what is web scraping let me explain you with the flow diagram that is there in front of your screen so we have websites right and obviously these websites contains multiple web pages now if I want to scrape some useful information from those web pages I can use web scraping and extract that content and I can save it wherever I want I can save it in an XML format or a CSV basically however I want to save so you can do that with python regular Expressions so in our case what we are going to do there's a website and on that website we have phone numbers we have uh addresses so what we are going to do we are going to write a code in order to get only the contact numbers from there and how are we going to do that we're going to do that with the help of a regular expression so for that I'm again going to open my py charm so guys I've already written the code so over here uh I've imported a couple of libraries in the beginning which are used to perform web scraping I mean they used basically to read the web page uh then we have find all which we have discussed earlier as well now this is the URL that I'm talking about right so let me just uh copy this and I'm going to show you to you in my browser how it looks like so this is how our web page looks like so we have name here we have the address then finally we have the phone numbers now notice a pattern in the phone number we have three digits inside the parenthesis then again we have three digits then we have a hyphen and then finally we have four digits just keep this pattern in mind and we are going to write a code in order to scrape only the four numbers from this particular website let me close right now all right fine so now you've seen the URL right so apart from that whatever things that are there actually we have covered that in the web scraping tutorial so you're pretty much aware about it so what is this we are basically reading the content that is there in our particular website now we'll focus on the regular expression part so you know what exactly find find all is we have discussed that now since we want only digits so we are going to make use of this particular identifier and inside the curly braces I have written three which means that there'll be three digits inside the parenthesis after that there's a space then again we have three digits then we have a hph and then finally we have four digits then save it in HTML St Str and then finally print it now let us go ahead and execute this and see what happens so we have scraped all the phone numbers that were there this is how it looks like and we made use of regular Expressions so you can use regular expressions for web scraping as [Music] well why we require file handling well guys think about this situation you have a server running and you need to give access to someone who needs to access it remotely right or basically you need to work with these files in general and say you need you need to have some import into your program where you're actually configuring an input for it well if I had to answer this personally how would I go about it I would be using files for a lot of my deep learning models to import my data sets into the program at all the time so basically by doing this I'll be making use of files a lot so coming back to the basics again how can we input something into python the standard input the usual keyboard input right so you can key in anything you want well there's another case as well maybe even commandline arguments to input some parameters into the code correct but what if you had to read lots and lots of data which is not practical to type in at every point of time or even that it doesn't make sense at all to type it all the time the easiest way out of this here is to actually store whatever input you want in one place and keep keep using it as long as your requirement is met so what's the answer the answer is files guys so guys this concept is actually very easy I'm sure everyone who's joined will take away some really interesting stuff about python that you can practically use working with files basically opens another door among thousands and each door with python opens up to a number of opportunities again so now that we're good with this and we started off on a really high role let's keep it going next we'll be taking a look at some of the types of files available in Python so guys let's keep the session very interactive head to the comment section and keep the energy going so can you quickly name all of the types of the files that you know so again let me help you here so we have images we have audio we have video we have text we have script and so much more right there is a key difference here I want you guys to note check this out windows supports all of these files that I told and more well virtually unlimited to be honest but again I won't get ahead of myself here because there are limitations here as well now check out python there are two types of files binary and text so whatever is not text is binary as simple as that so either a file gets to be a binary which contains obviously zeros and ones or it can have some literal text well text as in strings or anything valid for that matter text files are structured as a sequence of lines where each line includes a sequence of characters and this is what we call as code or syntax right and every line is terminated with a special character what we call as the EOL what is the EOL guys it's the end of line character a binary file is any type of file which is not a text file as I mentioned and because of their nature binary files can only be processed by an application that knows and understands the files system structure in other words they must be applications that can read and interpret binary right and now that we know what are the types of files are so we can start looking into more of this guys formally speaking I think it can be Consolidated as the operations performed on a file right so what all can we do check this out it's called as the crud operations what is crud well create read update and delete Guys these are the operations which can be done with files well again there are many other operations as well such as copying a file changing the properties of file or anything but for Simplicity sake I have planned to keep this session really easy for all of the audience so let us dive into python now so let us quickly see how we can go about understanding the file handling capabilities of python check out this flow diagram to give you a quick picture of what needs to be done first we create a file and later we open that file we work on the file well working on it is basically reading or writing or anything for that matter and lastly we close the file when we are done using it coming to python now creation of a file can be done manually by the user or through python for now let us consider that we will do it manually by going into the location and creating a file say like a text file later in this session I'll show you how easy it is to make python create the file for us so make sure you stay tuned till the end so now we concentrate on how we can open a file with python guys it is very simple so we have an inbuilt function called as the open function which is used for this exact purpose the open function takes in two parameters as seen here one is the file name and other is the mode and this on your screen is the Syntax for the open function so what do the two parameters mean one is the file name that you want to open it can be anything at this point of time right so it has to contain the extension of the file type as well so this is compulsory and important so make sure you keep this in your mind and second we have the mode we know this means something that has to do with opening the file right so check the check the screen so basically these are various modes available to open a file we can open it in the read mode the write mode the append mode and even the create mode as well pretty straightforward but do not that that the default mode is always the read mode and guys note that you can open a file in read mode only if it exists as well if you try to read a file or something that doesn't exist then python will greet you with a beautiful error message and in addition if you want to specify if the file should be handled as a binary or a text mode then along with the mode as well we can actually push in two categories for it check this out so we have t for text and B for binary T is the default value and B is for binary modes so we can have something as WT so what does it mean so it basically says that the F needs to be opened in the right mode and the file that the python opening is a text file so we're basically making the job for python to be really easy in this case so check out the example code I have here for you guys even if we do not give a mode as I said the default is the read mode so these two tiny Snippets I have for you are one and the same and yet make sure something exists before reading that guys or else you're sure to hit that error so check this out right so I have F equal to open and the file name is demo file. text I don't have a mode here this is exactly equal to having the read mode in the next example and it is as simple and straightforward as that so now we know how we can actually create a file and open it right so let's begin working on it let us start with creating on how we can actually do a read operation using python so the read operation is very easy guys so basically just like all the other functions and handling files in Python we make use of the read function for this very purpose but then again there are so many ways to read a file too so let me so let me break it down for you all right so we can do many things here we can read an entire file or read a few characters or read a few lines or any number of characters and lines together and so on check out this example code here so we have a variable right we call it file and then we opening the file it's called text file.txt and opening it in the read mode later we are actually going to make use of the print function to read the contents of the file and print the same on the python console now similar to the previous code that I just showed you we have something slightly different here can you notice that five on your screen what do you think the five indicates here guys let me help you out so basically tells the python interpreter to read the first five characters from the file and nothing else now check the old syntax so here we don't have the five so we are basically telling The Interpreter to read every single valid character present in our file and print it out for us so you got a good picture of what is happening with the file now well let's quickly take a tour into seeing this practically so I'll open up my py Cham now which I use as the IDE to work with python where I'll show you the code for all of these guys you can basically make use of any IDs that you're comfortable with and even the console as well it is your call I am comfortable working with py Cham so I'll go ahead with that so let me quickly open up the ID and we can see the basic code to read the file so as you can check out I'm here in my pycharm right now importing the OS module is the first Line This is currently being unused right now but I'm going to talk about this in the future session so basically we're opening a file right so we have a variable file we're going to open it and we providing the entire path to the file present so here the file is demo file.txt this is the name of the file W is the right mode that I already told you about let me quickly go ahead and run this that ran fine but you cannot see anything on the output right that is because we just opened it over wrote everything that is present in the file and we just closed it so we're not seeing absolutely anything here check out example two importing the OS module file equal to open again we providing the same file here we had right mode and now we having the read mode and here we are actually printing it using the file. read again I'll be walking you through file. read in a second but right now all you need to know is this will print the output on the screen for us if I go ahead and actually run it not nothing is going to be printed why do you think this happened this happened because we actually used the right mode and we actually blanked out the entire file Quick Fix is to actually open the file as you can see nothing is present there so let me type in something so we have edure Rea let me save that quickly minimize it go back to my py charm now as soon as I run this it is supposed to give me that EX exact output which says we love edureka perfect right so with absolutely three to two lines of code you can actually print something on your screen how simple is this guys well check out the next case so again it's the same thing opening the file using the read mode and then you're printing file. read of Five Guys file. read of five right so I actually explain this to you so let me just go ahead and run it we'll check the output we got the first five character right so w e a blank space l and O uh let us try to make it more nice to look at let me just quickly open up the file uh hello world this is a perfect candidate for our example uh yep the quick typer uh save it exit back into the code now I'm going to run this old code right so you're going to get the entire output here check it out hello world perfect because we have read of blank now we're going to actually run this because we're telling The Interpreter to read only the first five characters of the first line for us it is just printing hello how simple is this guys so now that we are actually done with this let's quickly jump back to the presentation to see what's next well guys next I want you guys to check out this syntax right so this is what I love about python amazing readability and you can almost guess what it means and you can be certain of it say 90% of the time so we saw how to read characters now let us look at how we can read lines well read line will actually read each of the lines and give us a line by line output read line of three will tell the python interpreter to read only the third line and absolutely nothing else but what if you wanted to return every line in a file which is properly separate and nice to look at well you can actually use the same function only in a new form this is called as file. read lines function again let's take a quick jump back into the code so we can see this in action so check out example four on your screen I have the same file demo file. text I'm opening it in the read mode and I'm using read line soon as I go ahead and run this I will have the same output as hello world okay let us step it up a little let me just change this hello world we love Python and we love edura so I have three lines here all good right so I'm going to save it I'm going to close it I'm going to come back here I'm going to run the read line again we reading only the first line cool check out example five now this is where things get interesting thing UT again it's it's all the same guys so basically instead of the read line I have read lines over here so the S is extra and it's going to read all of these lines and it is going to put it in a really beautiful way for us to see check this out hello world is an entity we love python is an entity we'll have edu Rea as another entity and that's nice to look at right so instead of just get getting a gibberish output of everything in the line separating it actually makes a lot of sense so now that we're done with this this another quick hop back to our presentation so guys next we need to check out a tiny concept on how we can actually Loop over a file object well this is another simple concept so let me teach you this we're using the for Loop for this very purpose as shown but why would we want to do this when you want to read or return all the lines from a file in a more memory efficient way and in a faster manner you need to use the loopover method so the advantage of using this method is that the related code is both really simple and easy to understand right so let me take another quick jump into pyam and check the single snipp it out so we are back into pycharm and check out this for Loop example I have for you so again we're sitting on the same file demo file. text reading it in the mode so for every line in our file object print read lines so is going to print the output for us containing all of the documents of our file as you can see we have Python and we love edure Rea that's really simple right and with this we are actually done understanding how to open the files and read them in multiple ways next we need to actually take a look at the right operation which is again very simple and almost similar to the read method let's start writing to a file is as simple as using the write function guys similar to the read again so to write to an existing file we make use of two modes one is the append and the other is write which we already checked out earlier by upend we actually add the contents of the right to a new existing content and with write we just add it to the blank file now I am sure there is a question in your mind right now so what if the file that I'm trying to work on is not blank what if it contains some previous data and we make use of w instead of aend guys this will basically delete everything in the file and overwrite it with what we've just put and this is exactly the first example case that I actually showed you so make sure that you use the mode very wisely in every situation right so here's a quick example before we dive right back into code as you can see we open a file in the right mode and we write two lines into it and we close it it is as simple as that right now again taking you back to pycharm and let me walk you through the code so guys we back into pyam right now so check out the first example I have for you import OS I already explained opening it in the mode of right so since we already have some content all of that content will be overridden with hello world and hello world again so we're opening it we're using file. write to write two content into it and then we're going to close it let me run this again no output because we not printing it to our console how do we check if everything is working open the demo file hello world and hello world again right so this is exactly what we wanted but then again this looks a bit gibberish as I told you right so we use read lines to actually make it look nice for us heading back to py Cham again check out the next example I have for you so now this is to prove that we're actually overwriting it again which I've already told you and it is simple but still let me walk you through this so we working on the same file we using the W mode and then we writing it to it saying oops we have overwritten it run it it ran no output but in the sense the file is changed so it says oops overwritten instead of hello world and that old message that we print Ed let me head back to the pycharm again and this is for creation and we'll be coming back to this in just a second so let's quickly go back to our presentation and then we're going to be coming back to pyam again so we're down to the last two concepts that we will check out in this session the first one among that is for us to create a new file using python well guys even for the creation of the file we need to make use of the open method we already know that a is used for upend and W is used for right and now we have X which is used to create a file but do not that you cannot create two files of the same name guys so basically it means that you cannot create a file which already exists check out the example on your screen and you will definitely get a better picture of it right there so you cannot have two files with the same name test file. text this is not right so again python will give you a beautiful error message and it won't be so beautiful at the end of it let me dive back to pyam and we can check out the example over here uh let me change this we have something else right so let me type ua. text I'm going to print new file into edura opening it we opening it in the X mode so we actually creating this file physically write mode to write something into it and close it so on it no output check out the file where is the file called edure it is here new file of edura so it's perfect right let me quickly head back to the presentation and we can check out what's next and then the next simple concept I have for you guys is showing you on how deletion operation works with respect to files using python guys deleting a file is very simple and straightforward we'll requ require the os. remove function just for this purpose so yes importing the OS module is a compulsion here I've been using it all along because it is just a good programming Habit to use the OS module when you're playing around with the file system the biggest mistake I have seen people do is that they usually go on to delete a file which doesn't exist again python pushes in an error message and that is not good at the end of the day right so it is very vital to know for sure that the file exists to do this we just check if the path exists and if it does cool remove the file else print that the file doesn't exist so you cannot remove it what does this say guys this is a very simple case of an exception handled by the user and this is a really good programming habit too so all good but can we delete a folder yes we definitely can a folder with all the content inside it can be deleted using the rmd command what does rmd stand for it basically stands for removing directory and the parameter here is basically the path and by the path it includes the name of the folder which needs to be removed and this is all of the operations you can do with python by making use of [Music] files introduction to Python and my SQL well as you already know python is one of the most widely used programming language and in demand among developers but you can't merely develop any application without having to know where to store the data so this is where database management system comes into picture and one of the most widely used dbms server that is also used in the industry practices is MySQL DB server let me move ahead and talk a bit about integrated connection all right guys this is just a glimpse of how the bigger picture will look like what I'm trying to do is I'm basically trying to integrate my python with my backend database server and then do the further manipulations now let me move ahead and explain you the internal working logic behind when you establish a connection between Python and MySQL DB server or I can say that I'm basically going to explain you the internal working logic that how does it actually connect or work so as you can see there are numerous pictures given in this slide on my extreme right hand side there's a picture of a database and on my extreme left hand side there's a picture of a python application and in between lies the MySQL connector python API so MySQL connector python API is nothing but acts as a bridge between my front-end python application and my backend database server now when I see this word python application think of any python application let's say suppose think of a website which a user has developed which lies in the front end now my frontend python application will basically send a connection request to the MySQL connector python API then the API will forward the same request to the database further which the database will accept the request and send the connected message and then my python application will send a cursor connection request so over here think of connection as a method and cursor as an object which basically lets you communicate through your entire MySQL DB server and also lets you create your own database finally my python application will execute a certain SQL statement or a certain SQL query and for that my backend database will fetch the result data so guys this was it with the internal working logic that basically Works behind when the python and MySQL DB server connection is taking place now let me move ahead and talk a bit about operations which I'm going to perform with python and my SQL so guys I'm basically going to perform crud operations which simply means create read update and delete with my Python and my SQL so let me move ahead and show you the implementation of crud operations from coding point of view so let me quickly open my pie chart all right guys before diving deep into the coding perspective my first and foremost step is to basically import the my SQL connector package and how do I do that I'll click on file click on settings click on Project interpreter on the extreme right hand side there's a picture of a plus sign and then type here my SQL hyphen connector all right click on my SQL hyphen connector click on install package it will basically take a minute or two to get installed essentially our first step is to install the my SQL hph connector package which forms the stepping stone for our further operations which I'm going to do again it is it'll take a minute or two to get installed all right so as you can see our package my SQL hyen connector has been installed successfully now one of the other approach to install the same thing is suppose this is for those set of users who don't use any python ID suppose you're using a python command line so for them quickly open command prompt once your command prompt is opened quickly go there and type pip install my SQL hyphen connector which is the package and press enter well it is going to show me or give me a message that is requirement already satisfied since I've already imported the package on my pyam ID but this demonstration I'm showing for those set of users who simply use Python command line so for them you can go to your command prompt type pip install my SQL hyen connector your MySQL hyen connector package will be installed and then you can import your same package on your python command line and then you can move on with the further operations so now it's saying me a warning this is none of our concern guys let me just quickly close it and as I've already created a file under the name of python DB creation I'll move on on with my coding demonstration so let me enter the presentation mode and start off by writing my first line of code so the first line of code which I'm going to write over here is I'm simply going to import the package which I installed just now that is my SQL hyphen connector I'm sorry it will be my SQL do connector now I'm going to create a local DB instance that is my DB under which I'm going to write MySQL do connector do connect under which I'm going to pass in the three parameters that are host which is equal to your local host then comes the username that is equals to root and then comes the password which is equals to password 1 2 3 so root is the default username which I had given guys and password 1 2 3 can also be customized so this is the password which I gave now I'll mly print this local DB instance that is my DB quickly go ahead and run this code yeah all right yeah so as you can see the connection has taken place but how do I verify whether the connection has been established successfully or not so in order to do that I'm just going to write two more lines of code that is if my DB then print that is if it falls or if the a local DB instance that is my DB then I'm going to print connection successful else I'm simply going to terminate this or I'm going to write print connection that is if there's a local DB instance that is my DB is coming or under my DB it is coming or the parameters are there then my connection will be successful else my connection will be unsuccessful let me move ahead and run this code give me a minute guys well as you can see our connection is successful now I'll just change one parameter and see whether it's verifying that the connection will be established successfully or not now I'm going to change a parameter suppose I'm changing the root which is a default name of user to ROM and let's see if the connection is being established successfully or not or it is being verified or not well as you can see guys it says access denied for user Ram as because our default username was root which I had given so it will not be getting connected to our MySQL DB server now let me quickly write back root over here and move on by creating a database so in order to do that I'll quickly open file I'll create a new python file with the name as python DB creation let me just name it python DB1 creation Now I'm going to write my first line of code that is importing my SQL do connect Conor which is the package then I'm going to write my DB is equals to my SQL do connector do connect under which I'm going to pass in the three parameters that are host which is equals to your local host then comes your username that is equals to root and then comes my password that is equals to password 1 2 3 finally I'll move on and I'm going to show you what I'm going to do over here so I'm going to create an object the name as my cursor under which I'm going to pass my DB do cursor which is pointing towards the location and finally I'm merely going to print this object that is my cursor. execute under which I'm going to write create database and let me say my database name is H DV all right and then I'll quickly move ahead and run this code let's see what is the output all right so it says that the database has been created that is har DB has been created now I'm going to show you whether this database is existing in our MySQL workbench or not so in order to do that I'll just change few parameters and add a for Loop I'll explain you about the for Loop part later so let me just omit this part hdb and instead of create I'll simply write show over here and then over here I'm going to write show database is now I'm going to write for DB in microf I'll simply print the database all right move on and run python DB1 creation file well guys as you can see from the output the reason why I used a for Loop over there is because once my code enters the for Loop it is basically printing all the databases which are existing in our MySQL workbench along with the database hdb that we have just created which is there in our output so it's very clear now I'll quickly open my MySQL workbench and I'll write the same SQL query over there so that it's crystal clear to you all right let me quickly maximize it 1 second now let me simply write show databases and click on execute 1 minute guys well as you can see our database H DB has been created along with all other databases which are existing in our MySQL workbench now let me just refresh the schemas all right so let me just minimize this and I'll move ahead by creating a table under the database so I think you guys are pretty clear with the concepts which I explained just now I basically started off by creating or by establishing a connection between our Python and MySQL DB server further I moved on and created a database and now I'm moving on and creating a table under the database hsh DB now how do I do that well let me create a new file first one second I'll name it as Create TV and python all right all right guys before moving ahead I'm going to tell you one thing the first three or four lines of code are going to be same everywhere I don't find it useful by repeating the same first three lines of code everywhere again and again I don't want to bore you guys with the same thing so let me just copy paste it enter the presentation mode yeah control V now I'm going to write my cursor. execute it's going to be create table suppose I give my table's name as employee under which let me pass two parameters there'll be name varar 200 salary integer 20 we just close it move on execute this code well I have done a very silly mistake guys and the error is also very self-explanatory it says no database selected I'm going to pass a parameter over here it's going to be database is equals to hsh DB and let me quickly move ahead and run this code one second yeah all right so when I run this code our table has been created now I'm going to show you the table which I have just created in our database H DV so I'm going to write the same code what I wrote while I was showing you the database so let me just omit these things and add an S over here and instead of create let me just add a show and let me type A for Loop so it'll be for TV in my cursor print your table all right now let me move ahead and run this code so as you can see see we have created only one table under the database har DB so it is showing that table which is employee now I'll show you the table in our MySQL workbench so one minute guys so instead of databases let me just write tables let me just select the hdb thing over here instead of databases let me just write tables select hdv and then execute this yeah so once I execute this well as you can see it is fetching the table and our table employee has been created on the extreme left hand side under the schemas column now I'll quickly minimize this and I'll move ahead by doing the read operation so before I dive deep into the read operation guys let me give you a small introduction about this so our read operation will basically happen in two stages in my first stage I'm going to populate my table with some values and in my second stage which will be the main stage where I'm going to do the read operation consisting of two fetch functions so I'm going to talk about the fetch functions later first let me just in order to populate my table I'm going to make use of insert command from the SQL query so let me just name it as insert op table and move ahead yeah control V before there's one more parameter which I left it's going to be database is equals to hsh DB now moving on let me create a new variable with the name as SQL form and under SQL form I'm going to write the SQL query insert into employee one second guys pass in the two parameters which I declared which were name and salary values and make use of or I'm going to write percentage comma percentage well guys these percentage are nothing but placeholders you can replace them anytime you want with any other value now I'm going to create a tupple now the reason why I'm creating a tupple is because again in my second stage where I'm doing the read operations I'm going to make use of two fetch functions so there's one fetch function called fetchall so when I'm making use of fetchall I need not populate my table with a single value because that will not look good that's the reason why I'm creating a tle over here so that to give you more clarity into what actually happens now let me just name it as herid let me give my salary as 20,000 comma Amit salary as 30,000 comma an and her salary is 40,000 comma space yep I think I need to put this back yeah now when I move on I'm going to Simply write my cursor dot exit execute many because if I use execute it is just going to execute a single value but since I'm making use of a tle over here I need to write my cursor. execute menu and call in SQL form comma employees and finally I'm going to write my DB do commit which is going to save my changes from my last executed SQL statement now let me quickly move ahead and run this code so as you can see our insert operation has taken place and our tble has been inserted into the table employee under the database hsh DB now I'll quickly exit the presentation mode and show you the same in our MySQL workbench it's going to be the same thing I'm going to write select star from employee and click on employee on execute one second guys yeah I'll click on the result grid over here well guys as you can see this select star from H db. employe well it changes nothing Our concern is to basically view the topple which we have created and that topple is being viewed in the result grid that is under name and salary the values which we passed that are harid Amit anida with the salary as 20 30 and 40,000 let me quickly minimize this and moving ahead I'll be creating a new file and let me just name this file as read op table all right now into the presentation mode once again copy the first three lines of code over here what I'm going to do is yeah let me pass the parameter database which is equals to H DB and then I'm going to start off my read operation by the first function that is fetch one so let me write my code and then side by side I'm going to explain you also why am I writing that part so I'm going to Simply write my cursor. execute let's say I'm going to write a SQL query that is Select name column from employee okay this is a very simple SQL query and let me create a new variable and let me name it as my result under my result I'm going to write my cursor. fetch one now guys this is one of the most important part of my code because this is the first function of read operation what fetch one is going to do is it is going to fetch me one value from the column name under the employee table all right now when I move on let me write the for Loop that is for Row in my result print the row and when I run this code as you can see it has fetched the value harit from the row name under the table employee which is again under the database H TB as you can see this code is pretty self explanatory when I make use of the fetch one function it basically fetches a single value from the column name under the table employee now let me just quickly move on with my next function of fetch in order to do that let me just make few changes in this code so instead of name suppose I'm going to fetch the whole tupple it's going to be select star from employee and instead of fetch one let me write here fetch all and let me run the for Loop so it will be for Row in my result print row all right now when I run this code I'll explain you why am I doing it or what does fetch all do well as you can see from the output the whole tle has been fetched now when I make use of the fetchall function guides it basically fetches the whole tupple for us as because we writing the SQL query select star from employee so all the values which are there under employee table which was in the form of for tle when we inserted it a while ago while we were writing the insert code that whole tupple gets fetched now I'll exit the presentation mode I'll go to the MySQL workbench and rerun this code once again well as you can see there are no differences the name is still there with the values haritan and salary 20,000 30,000 and 40,000 suppose I write this thing select name from employee one second I'm going to show you the changes of the fetch one functions in order to do that let me just write name again over here name and instead of fetch all let me write fetch one all right let me quickly go ahead and run this code so as you can see is fetching here now I'll go to the MySQL workbench and click on same thing well guys uh these changes are not taking place over here because uh we were showing you the changes in the pie charm and since we applied a for Loop over there that's the reason why it is doing it but since when we are using the backend database server we need not do that because we are not using any Loop or we are not doing anything we are simply fetching the name whole column from the table employee now I'll move on and uh do the update operation so in order to do that let me just create a new file a new python file and name this as update pop table yeah let me close this just click on this enter presentation mode yeah control V let me pass the parameter which was database that is equals to H DB moving on let me create a new variable with the name as SQL under which I'm going to write update employee Set uh 1 second salary let me set the salary to 70,000 making use of the wear Clause where name is equals to let's say I'm updating an's salary to 70,000 and finally I'm going to write my cursor dot 1 second execute call in the SQL thing and then write my DV do comment quickly go ahead and run this code our update operation has taken place and in order to see the changes let me quickly go back to the read operation I'll show you the changes in both places in the py charm and MySQL workbench 2 so the last code which I wrote let me remove this and right here select start from employee this was fetch all okay Enter the presentation mode once again yeah run this code as you can see from the output the changes have taken place and Anita's salary is updated to 70,000 which was 40,000 before I'll quickly move on and show the same changes in the MySQL workbench so is quite easy let me just redun this code uh this should be star over here yep and then execute it as you can see an's salary over here is again updated to 70,000 now I'll move on and do the last operation that is delete operation which will also conclude my tutorial it's going to be delete op let me name it as table all right enter the presentation mode once again copy the first three lines of code I'll pass here the database parameter database is equals to H DB I'll move on and let me make use of the same variable which we used that time say suppose delete from employee where name is equals to suppose I'm deleting my record guys which is harit and then my cursor. execute all in SQL and then write my db. commit which is going to save the changes from my last executed SE statement let me quickly go ahead and run this code well the database spelling which I use is wrong I guess well as you can see from the error it's quite Justified it says unsupported argument data BC so the data B spelling is wrong let me quickly Rectify that one second guys yeah I'm going to write a over here I missed it my bad and run this code once again so as you can see our delete operation has taken place and I'm going to show you the changes exit this we'll go to ROP I'll enter the presentation mode once again run this code well as you can see the record herid has been deleted from here and the same thing would be or the same changes would have taken place in the MySQL workbench let me quickly open it and run this code once again well as you can see the record har is deleted from here too [Music] what exactly are sockets sockets are basically interior endpoints built for sending and receiving data a single network will have two sockets one for each communicating device or a program these sockets are a combination of IP addresses and port numbers guys a single device can have n number of sockets based on the port number that is being used different ports are available for different types of protocols take a look at this table for common port numbers and the protocols as you can see over here the HTTP protocol uses the port number which is 80 similarly FTP uses 20 n ntp uses 119 Etc let me just jump on to my browser to show you all how this actually works as you all can see over here in the address bar I have the address of google.com now we've just learned that google.com makes use of port number 80 let me just give the address of Google along with port number 80 and see how it works so I'll just use google.com colon and I'll use 0 as you all can see on the screen when I used google.com with the port number 80 it has returned the same Google page now let me change the port number and let me give 20 as the port number which I know is for FTP protocols so I'll just type Google over here google.com colon2 so I hope you're clear with this this guys when I gave google.com colon 80 it returned the same Google page but when I gave google.com col2 it has returned that this site cannot be reached okay so I hope everyone's clear with this so now let's get back to our presentation and see how to create sockets in Python to achieve socket programming in Python you will need to import the socket module or the framework this module consists of built-in methods that are required for creating sockets and help them associate with each other some of the important methods present in this module are the socket to socket method which is used to create sockets this method is required for both clients as well as servers in order to create sockets for both of them the socket. accept method this method is used to accept a connection it returns a pair of values of connection and address where the connection is a new socket object for sending or receiving data and address is the address of the socket present on the other end of the connection socket .bind method this method is used to bind the address that is specified as a parameter the close method is used to Mark the socket as closed whereas the connect method is used to connect to a remote address specified as the parameter socket. listen method is used to enable the server to accept connections now that you've understood the importance of socket module let's move on to see how it can serve to create servers and clients okay so guys before creating them let's first understand what exactly are servers and clients a server is either a program a computer or a device that is devoted to managing network resources servers can either be on the same device or computer or locally connected or even remote there are various types of servers such as databased servers network servers print servers Etc servers commonly make use of methods like the socket method bind and the listen method Etc to establish a connection and bind to the clients a client on the other hand is either a computer or a software that receives information or services from the server in a client server module client requests for services from the server the best example for this is a web browser such as Google Chrome Firefox Etc these web browsers request web servers to send required web pages and services as directed by the user other examples of clients include online games online chats Etc now that you've understood the concepts of clients and servers let's move on to Pyon to create them using python so I have a new project over here I'll create a new python file and I'll name it as server or I'll say server one like I've already told you before to achieve socket programming you'll have to import the socket module so for that I'll just use import socket okay so after this what I have to do is create sockets using the socket method so I'll just use S equals socket. socket and within the bracket I'll specify the address family as aore inet so socket. aore inet aore inet refers to address from the internet and it requires a pair of a host and port number where the host can either be a URL of some particular website or its address and the port number is an integer the second parameter I'll specify is Socket dot sock stream socket unor sock stream is used to create TCP protocols okay so the next thing we have to do is write the bind function now this bind method accepts two parameters as a tuple which is a host and a port number okay so now I'll be using the bind method so s do bind and as a parameter I'll use get host name now this method is used when the server as well as the client are on the same computer or device and after this I'll just specify a port number I can specify any of the port numbers greater than 1023 which are non-privileged port numbers so I'll just give the port number as 1024 now I've used the bind method because I want my server to bind to the client now I'll use the listen method so I'll just pass the parameter as five okay now while the connection is true so while the connection is true I want my server to accept the client socket and its address so I'll just use CLT and ADR for address and I'll use the accept method for this now once I've received this from the client I want to print it so I'll use the print method and over here I'll use the F string so guys F string is basically a literal string prefixed with an f and it contains python Expressions within braces so over here I'll just give some message I'll just say say connection connection to and within bases I'll specify the address established so once the connection is established I want to pass some message from the server to the client so I'll just use CLT do send and since I want it to be bytes I'll use bytes and within this I'll write some message say for example socket programming in Python so this is the message I want to pass from the server to the client and since I've used bytes over here I have to specify what kind of bites so I'll say utf8 okay so our server is ready now now let's move on to create our client so I'll just open a new file in the same directory and I'll give the name as client client one again okay so as you all know the first thing you'll have to do is import the socket module and after that use the socket method to actually create a socket for the client I'll just copy this from here and I'll paste it over here so now I want my client to connect to the server so I'll use the connect method so s. connect and I'll specify the parameter as get host name and over here as well I have to specify the same port number which I've used in my server so I'll just cross check what number I've used over here it's 1024 I have to specify the same port number on the client side as well after this all I have to do is save the message that I'm receiving into some variable so in my case I've just named it as MSG and then I'll have to use the receive method to this receive method I can specify any number of bytes as a parameter and that many number of bytes will be received in one communication between the server and the client so in my first case I'll just use one24 don't get confused guys here it's just the bytes number and this is the port number okay after this I'll just print the message that I've received so MSG dot since the message being transferred was encoded I'll have to decode it using the same UTF 8 bytes Okay so our server and client is ready for execution to execute these two files I'll just open up the command prompt and over here I'll just use py server file name. py so server 1. py sorry guys I've just missed out braces over here okay so I'll just save this and I'll run this again so as you can see this cursor blinking over here which means our server is ready but it does not show anything as of yet because we have have to execute the client as well so to execute the client I'll open up another command prompt window and this time I'll just use py client 1py oh our client has not received any message let me just go to the window where I executed the server and see what is the issue as you see over here I've made a spelling mistake I'll just jump onto pycharm and this is not S N D It's s n d sorry for that I'll just save this and execute my server again I'll execute the client again okay so congratulations guys our server and client have communicated and the server has passed the message we specified from the server to the client as you can see on the window where we executed the server it says connection to the IP address and the port number has been established and the client has received the message that we wanted our server to send but wait a minute guys let me just go back and change the number of bytes that we've specified so so I had originally specified the number of bytes as 1024 now let me just change it to some other number say for example 7 I'll save this and I'll rerun the client again I hope you see the difference the first time we specified was 1024 bytes which was a very large amount of bytes that my client was accepting from the server in one communication when I reduced the number of bytes to seven what I've received is just socket and the remaining message is not received and the communication has been terminated as well but this is actually an error in real scenarios now what should I do to make my client communicate again and again until the message is received from the server let's go back to our pycharm and make our client server communicate until the complete message has been received I'll keep the number of bytes a seven itself so all I have to do over here is include a while loop so I'll just use while true this needs to be indented into to the block I'll save this and I'll rerun the client so I hope you see the difference guys the first time we gave one24 bytes we received the complete message in a single communication in the second example I just changed the number of bytes from one24 to 7 and all I received was just socket and the remaining message was not received and the communication was terminated as well as you can see after this when I included the while loop within the client you can see over here that my client and server have communicated multiple times and the complete message has been received in a number of communications so first thing that I received was socket after that I received program in in Python so I hope you're clear with this client and server communication okay so as you can see over here the complete message has been received in seven bytes per communication our client and server have communicated multiple times so as to receive the complete message but there is the issue again the client has not been terminated and we actually will not know when this client will be terminated to avoid this all we can do is go back to our client program and make a small change so over here I'll just specify complete information to be received I'll just store an empty string into this variable and this will actually allow my client to accept any information that is being sent from the server now after using this receive Method All I'm going to do is use an if block if the length of the message is less than equal to zero then I want this Loop to break and if not I want the complete info to be printed in seven bytes per transfer so instead of using this message. decode over here in the print statement I'll use it over here and after this all I have to do is print the complete message now why I'm decoding this over here is because I'm receiving one chunk of the message at once so I want to decode it as in how I'm receiving it after that I'll just print the complete info that's been received I'll save this program get back to my server and once this is done I want the communication to close so I'll use CLT do close method over here I'll save this and I'll rerun my server and client again one more change you'll have to do over here is the port number since one communication is already present on the port number 1024 I'll just change it to 1025 I'll have to do it on the client as well as the server side so I'll just change it to 1025 I'll save this I'll open up the command prompt and I'll execute our server first now I'll open up another command prompt to execute the client I've missed a colon sign in the if flow just open up the client save this and I'll rerun again sorry guys I've just missed a less than and a colon size over here okay I'll just save this and I'll go back and I'll rerun my client so I hope everyone's understood this I've received the complete message from the server it's done in 7even bytes per communication as well as the client has been termin ated okay till here you've just got The Knack of transferring strings but socket programming in Python allows you to transfer python objects as well these objects can be anything like sets tuples dictionaries Etc to achieve this you will need to import the pickle module of python python pickle module comes into picture when you're actually serializing or deserializing objects in Python before moving on to show youall how objects can be transferred from server to the client let me just show you how how these objects are actually serialized using the pickle module I'll jump on to pyam again I'll open up a new file python file I'll just name it as pickle example okay I'll import the pickle module and then I'll just specify some list over here as my list so I want to serialize this python object which is a list I'll just store it into a new variable as s I'll have to use the dumps method which is present in the pickle module to do this as a parameter I'll specify the list that I've already created I'll just print the new list which I've saved in SE okay now let's execute this one so as you can see in the output over here guys I have the serialized list I have the numbers one 2 3 4 presents somewhere in some serialized Manner and this is prefixed with a B now this B actually means that this is in bytes now that you have a brief idea about the pickle module let's move on to see how you can use this in socket programming to transfer objects from the server to the client okay I'll just open up the file where I've already written this program and I'll explain this to you all one after the other the first thing that I'll have to do is import the socket module as usual and I'll have to report the pickle module as well after this all I'm specifying is some header size as a equal to 10 you can give the header size as anything of your choice after this I've just used the socket method again to create a socket for the server side and the bind and the listen method which I've already explained previously following that I just have a while loop just like before and in this case instead of passing the message as socket programming in Python I've just given some dictionary over here which is M and I've used the dumps method present in the pickle module to serialize this object which is a dictionary so after this all I'm doing is incrementing the my message and finally sending it to the client using the send method now I'll just open up the client file and explain what's going on over there the first thing as we all know is importing the modules that are required and then specifying the header size which we've already done in the server side after that as usual I have created a socket for the client and I've used the connect method to connect to the server now the complete information that is received from the server to the client is supposed to be in bytes so I've just specified B with an empty string then I've just specified that the message being received from the server to the client as true now while that is true I want to receive 16 bytes per transfer and if the message being received is completed then the receive message variable will be set to false if not it will be be incremented and the next chunk of message will be received in 16 bytes per transfer again after this all I'm checking is for the condition if the complete information received is equal to the length of the message that's originally present on the server side I'm just printing a message saying receive the complete information as I already explained before I Ed the dumps method of the pickle module to serialize the information I was sending from the server to the client now to deserialize the same message I'll have to use the loads method so to this method I've passed the complete information that I've received from the server to the client as a parameter and after that all I'm doing is printing the message that I'm receiving from the server to the client now let me just open up the command prompt and execute these files so first I'll execute the server and I'll open another command prompt to execute the client so as you can see over here guys the complete size of the message Mage is 38 bytes and this is the serialized form of it and this is our dictionary that we've passed from the server to the [Music] client so what is numai numai is basically a module or you can say a library that is available in Python for scientific Computing now it contains a lot of things it contains a powerful n-dimensional array object then tools for integrating with cc++ it is also very useful in linear algebra for Trans form and random number capabilities now let me tell you guys numpy can also be used as an efficient multi-dimensional container for data for generic data now let me tell you what exactly is multi-dimensional array now over here this picture actually depicts multi-dimensional array so we have various elements that are stored in their respective memory locations so we have one two three in their own memory locations now why is it two dimensional it is two-dimensional because it has rows as well as columns so you can see we have three columns and we have four rows available so that is the reason why it becomes a two-dimensional array so if I would have had only one row then I would have said that it is a one-dimensional array but since it contains rows as well as columns that is it is represented in a matrix form that is why we call this as a two-dimensional array so I hope we are clear with what exactly two- dimensional array is so let me open my py charm and I'll tell you practically how to actually create an umpai array so this is my py guys over here the first thing that you need to do is first install the numpy module and how you're going to do that click on file go to settings tab and you'll see the project interpreter option and on the right hand side you'll see a plus symbol just go on and type the module that you want to install so I'm going to install numpy just go there click on it and install package I've already done that so I'm not going to repeat it so this is my pie charm so first thing that I need to do is import numpy as NP now after that I need to create an npy array so for that I'm going to Define a variable let it be a and I'm going to type in here np. array and certain elements inside it so I'm going to put in 1 two three and print it that's all this will actually print a single dimensional array so 1 2 3 has appeared now if I want to convert this to a 2d array so for that I'll keep this in parenthesis and after a comma I'll add one more element and I'm going to give certain values inside that so I can give say four five and six now go ahead and print this so as you can see that it is now a two-dimensional array so this is how you actually create arrays using numpy module so now I'm going to open my slides and we'll move forward and see what is our next topic now let us see why are we using numpy instead of a list all right so many of you might be thinking why are we using numpy when we have list all right so basically we use numpy because of three main reasons the first thing is it occupies less memory when compared to list then it is actually pretty fast when you compare it with list and at the same time it is very convenient to work with numpy so these are the three major advantages that numpy has over list and that is the reason why we use numpy instead of list now don't worry I'm actually going to prove it to you practically by opening my py charm so guys this is my py charm again so first thing that I need to do is import numpy as NP and now what I'm going to do is I'm going to import a couple of more modules I'm going to import time and I'm going to import sis all right done so our first step is to actually Define a list and the name that I'm going to give to my list is say s and I'll type in here range th000 so what this will actually do it'll actually take all the integer values between 0 to th000 and it will give it to a variable s so this list will contain the integer values between 0 to th000 but it won't include th it'll be only there till triple9 that is 999 and now I'm going to print the space occupied by this particular list so for that what I need to do is I need to type in print says. get size of any one element Okay so you can give three four five anything I'm going to give it as five any one element and multiply that with the length of my list that's all so this will actually give me the space that has been occupied by the list because sis. get size off will actually give me the memory occupied by one element and when I multiply that with length of my list I get the entire memory that has been occupied by my list now the same I'm going to do with my numpy array as well let me give a name to that I'm going to type in d np. a range and the range will be th000 now a range function is pretty much similar to the range which is there so the same thing will happen here the integer Valu is between 0 to th000 but it won't include thousand will be present in my variable D so we have created a numpy array now let us print the space occupied by it so the first thing is I'm going to type in here D dot size so this will actually give me the space occupied by one single element and when I multiply that with the length of my numpy array I get the entire memory that is occupied by the npy array so I'm going to type in here D do item size that's all now go ahead and print this so This actually shows the memory that has been occupied by my list and this shows the memory that has been occupied by my numpy array so as you can see there's a quite a lot of of difference between both of them so we have proved our first point that it actually occupies less memory now when I talk about numpy array is faster and more convenient than a list so the next step is I'm going to prove it to you that numpy array is actually faster and more convenient than list so I'll remove all of this and so now I'm going to show you that numpy arrays are faster than list and at the same time it is easier and more convenient in order to work with numpy arrays when compared to list let me show you practically so first what I'm going to do is I'm going to Define a variable say size which is equal to say 1,000 and then I'm going to Define two lists now what I'm going to do is I'm going to add those two lists as well as I'm going to add two numpy arrays which I'm going to Define now and then I'm going to compare the time taken in order to find the sum for list and the sum for numpy ARS so first let me Define two lists and two arrays so my first list will be equal to range size same goes for my second list as well just change the name to L2 and now I'm going to Define to nump virus A1 equal to np. AR range size go ahead and do the same for the second numpy array as well and change the name as A2 so we have two lists and two arrays and we need to compute the sum of both of these list as well as arrays now before that I'm going to Define a variable say start with which is equals to time do time and now I'm going to calculate the sum so I'm going to save that in result and first I'm going to calculate the sum of list that is L1 and L2 so for that what I need to do is I need to run a for Loop because if I directly write L1 plus L2 it is going to give me a result which is nothing but the concatenation of both the lists so in order to calculate the sum I need to use for Loop let me show you how to do that first I'm going to type in X comma Y and we have already studied Loops in detail for X comma Y in zip and the name of the two list that is L1 comma L2 that's all so what will happen here it'll first take the first element of list L1 and then it'll take the first element of list L2 it'll go it will calculate the sum and it'll store in result and it'll keep on repeating until the range has been exceeded now this is how you calculate sum in list but when you talk about arrays what you need to do you need to just write in A1 Plus A2 that's all that's why I'm saying that it is more convenient in order to work with numpy arrays when compared to lists now our next step is now our next step is to define the same variable start and initialize it with time. time and now I'm going to find the sum of my two numpy arrays which is nothing but A1 Plus A2 that's all and now print the time taken so print time. time minus start and then multiply it with thousand because by default it'll take it in seconds and I need to convert it into milliseconds now I forgot to actually print the same thing for my list so I'm going to do it over here so this will actually give me the time taken by my list in order to compute the sum and this statement will give me the time taken by my numpy array in order to compute the sum so let us go ahead and execute this and we'll see what happens so it gives me 0 milliseconds because the size is small let me just start a couple more zeros uh let's make it a million now go ahead and execute this now you can notice the difference that there's a significant change lists took 208 milliseconds whereas numpy took almost 67 milliseconds so there's a huge difference between the compute time of list as well as numpy array that's why I say that numpy array are faster convenient and at the same time they occupy less space when compared to list so that is the reason why we choose numpy arrays over list so let us go ahead and move forward towards the next topic that is numpy operations so let me go back to my slides so now is the time to see various operations that you can perform with the numpy arrays so you can find the dimension of your array whether it is a two dimensional or a single dimensional array then you can even calculate the bite size of each element it is pretty easy I'm going to tell you that practically you don't need to worry about that and you can even find the data types of the elements that are stored in your array so if you want to know what is the data type of the elements you can do that as well so let me show you these three operations first and then we'll move forward to the other operations I'm going to open my py charm once more guys let me remove all of this so we have imported the numpy module now what I'm going to do is I'm going to define a numpy array I'm going to name it as a and I'll write here NP do array 1 comma 2 comma 3 put that in parenthesis now add one more element say 2 comma 3 comma 4 all right so it's a two dimensional array now if I want to know whether it's a two-dimensional or a single dimensional array so I'm just going to type in print a do endm and it'll give me the dimension so let me show you that and I'm going to run this so it says two that means it is a two-dimensional array so what if I move this part and make it as a single dimensional array it should give us the result as one let's see if it does that or not and yep it gives us one as a result so this is how you actually calculate the dimension of your array now if you want to find the bite size of each of the elements so what you need to do is instead of endm you can call a function called item size go ahead execute this and you'll get so each element occupies four bytes after that if you want to know the data type that is stored in the array so you can just type in here dtype go ahead execute this it should give us integers integers 32 bit all right so this is how you can actually perform these three functions that I've told you in my slides so let us proceed with the presentation now let us move forward and see what are the other operations that you can perform with numpy module so by using numpy array you can actually find the size of your array how you can do that that I'll show you practically you don't need to worry about that so when I say size of the array that means the total number of elements that are present in the array so if this is an array so the total number of elements become four 1 2 3 and four now you can even find the shape of your array now what do you mean by shape so basically the total number of columns and rows now over here we have three columns and four rows so our shape is actually three columns and four rows now let me show you practically how you can do that again I'm going to open my pie charm and show you let me remove this print statement from here now if I want to find the size of my numpy array I just need to type in print a DOT size that's all you have to do and it'll give the size of your array so there are three elements so if I go on and add some more elements say four 5 6 7 then if I execute this you'll see that seven elements have appeared that means the total number of elements in my array is seven then comes the shape part that I was talking about so in order to find the shape what you can do is you can just type in here a do shape and it'll give you the shape so let us see what happens so it has seven columns but there are no rows so it has given seven comma blank so what I can do is I can close this in parenthesis and I can Define one more element say 8 9 10 11 12 134 go ahead and execute this and you can see 2 comma 7 because we have two row rows and seven columns available with us so this is how you can actually find the size of your array as well as you can find the shape of your array now let us move forward and see what are the other operations that you can perform with numpy module so we saw how to find the size and the shape of an array so we can perform reshape as well as slicing operation using numpy array now when I talk about reshape what do you actually mean by reshape geography all right fine so that is absolutely correct guys now over here there is an example so we have three call colums and two rows which we have converted to two columns and three rows now let me show you that practically how you can do it in a variable and I'm going to start a numpy array and I'll have two elements in that my first element will be 1A 2 comma 3 comma 4 and my other element will be say 3 comma 4 comma 5 comma 6 so I have a two dimensional array that contains two rows and four columns now I can convert that to four rows in two columns let me show you how to do that you're just going to type in a is equal to a. reshape and I want to convert it to say four rows and two columns go ahead and print this so it has converted that to four rows and two columns as you can see in front of your screen now let me show you that earlier this is not the case I'm going to type in a print statement here as well in order to show you that how it has reshaped so earlier we had four columns as well as two rows but now we have two columns and four rows so this is how you can perform the reshape operation now let us talk about slicing so slicing is basically extracting particular set of elements from your array and the slicing operation that happens here is pretty much similar to the one which is there in list as well so suppose if I want only this particular element that is three so for that what I need to print I'll show you print a and the index value of three which is present at 0 comma and the index is two let me tell you how indexing happened so this element will be zero this element will be one now if I want three from here the from the zero element I want the index two indexing starts from 0 1 and two so that's why I've written 0 comma 2 and it should print three for me let us see if it does that or not and yep it prints three now now say if I want to print four and six now for that what I need to do is I need to remove this two here and I'm going to put a colon that says all the rows including zero and in that row I want only index three so we have only two rows so if I were have written Zer colon one then it wouldn't have included this particular row so if I have one more element here so I can actually write here two so that W actually include the element which is present at the second index so when I say Zer colon this this actually means all the rows that include zero as well so we have only two rows it will include both the rows and at the same time it actually going to print the third index from both of these rows so let me show you if it that happens or not and yep it happens we have four and six available with us now just to remove confusion what I'm going to do is I'm going to add one more element and I'm going to give values to it say 7 8 9 and 10 so now if I want four and six I can't just write Zer colon because if I do that it'll print 10 as well let me show you that yep it has printed 10 now in order to avoid that what I can do is I can write in here two so I've told you this is the zeroth element first element and the second element so when I write 0 col 2 it won't include the second element it only includes zero as well as the first element now inside that we have index three from both of these rows that is why we'll actually get 4 and six let us see if that happens or not and you can see 4 and six is now available so this is how you can perform slight sync when we in numpy arrays now what I'm going to do is I'm going to type in a equal to np. line space and now over here first I write one comma say three and I want say five values between 1 comma 3 what this will do is it'll actually print the five values which are equally spaced between one to three so let me print this first I'm going to type in print a and go ahead execute this and you can see that we have 1 then we have 1.5 then we have two 2.5 as well as three so if I would have written here 10 it will actually give me 10 values between 1 to three and yep you can see we have the 10 values between 1 to three so this is how you can perform line spacing as well so we saw how to perform reshaping and slicing now let us see what are the other operations so we are now going to find out the minimum maximum as well as the sum of our num py add so let us go ahead and execute that practically let me remove all of this and now I'm going to Define one more numpy np. array and I want elements and let's say 1 2 3 now if I want to find the element which has the maximum value so what I can do is I can just type in here print a. Max that's all and it'll give me the maximum value in my a numpy array which is three obviously if I want to find minimum value so I'm going to just going to type in here Min and it'll print the same for me which is one now if I want to calculate some it is pretty easy just go on and type sum and it'll give you the sum that's all guys it's that simple whatever I'm telling you these are all the basics that you require after that whatever your requirement is there on that basis you need to use those basic knowledge that you have and implement it now the best way to do that now the best way to understand any programming language is to play around with it so you know the basics with the help of those Basics just install py Cham first then try out new things like how should I get that how should I get this and if I'm not getting it what is the reason behind it so go on and try to discover new new things so the conclusion is you need to actually perform things practically you need to make sure that you're not only getting the theoretical knowledge you need to perform it practically that's why I always say that side by side when I'm executing these practicals you need to do that as well although you might find it pretty basic but with the help of this knowledge you can perform a lot bigger task as well all right so Janice looks happy now so let us move forward with our slides so now comes the axis concept here guys it is pretty similar we have a numpy array which looks like this and the rows are called axis one and columns are called axis zero now you must be thinking what is the use of this axis suppose if you want to calculate the sum of all the rows then you can actually use these axis and you can do that now let me show you practically how it happens I'm going to open my pie charm again and I'm going to show it to you let me remove this and let me add one more element here 3 comma 4 comma 5 all right so if I want to find the sum of axis zero it's very very easy just go on and type print a do sum and type in axis equal to 0 go ahead and print this and you can see 4 6 and 8 1 + 3 is 4 2 + 4 is 6 similarly 3 + 5 is 8 if I make this as AIS one and print this so it gives me 6 and 12 because 3 + 2 + 1 is 6 similarly 4 + 5 + 3 is 12 so this is pretty easy I know now let us go back to our slides and see what are the other operations so there are many mathematical functions that you can perform with numpy that is to find the square root of each element you can even find the standard deviation so these two operations can be performed with the help of numpy you can find the square root you can find the standard deviation now let me show you that how you can do it this is my py charm again so now I'm going to remove this print statement here and I want to print the square root of each of the elements that are there in my num pyr which is actually assigned to a variable a so I'm going to type in here print NP do sqrt that is square root of of my npy r a go ahead and execute this and it is actually printed the square root of each of the elements the square root of one is one for two it is 1.414 for three it is 1.73 again for three it is 1.73 then for four it is two then for 5 it is 2.23 this is how you can find the square root of each of the element now when I talk about standard deviation so you can find that by typing here so now if I want to find the standard deviation what I need to do is instead of sqrt I'll just type in here St D and it'll give me the standard deviation that is how much each element varies from the mean value of my numpy array and this is the standard deviation guys it's that simple so this is how you find standard deviation now let us go back to our slides and see what are the other operations that are still left now these are the basic mathematical functions that you can perform with numpy arrays like addition multiplication subtraction and Division and that will actually happen element wise so basically you are performing matx matx addition matrix multiplication Matrix division as well as Matrix subtraction let me go ahead and show it to you practically it is very very simple guys so similarly I'm going to Define one more array and let me name it as B let me remove this print statement now if I want to calculate the sum so what I need to do is I need to type in print a + b that's all you need to do but when I talk about list again I'm telling you that when I talk about list if I do that it'll concatenate both lists so if I want to print list that is the addition of two lists I need to use for Loop so that is where numpy array stands apart and it is pretty convenient go ahead and execute this and you'll see that element wise addition has happened 1 + 1 is 2 2 + 2 is 4 3 + 3 is 6 similarly 3 + 3 is 6 then 4 + 4 is 8 5 + 5 is 10 all right so this is how you can perform addition you can perform subtraction by using the subtraction operator go ahead execute this and you'll find all zeros because 1 - 1 2 - 2 3 - 3 3 - 3 4 - 4 5 - 5 will be zero only right no rocket Signs Now go ahead and multiply it as well and see what happens so you have one into 1 is 1 2 into two is 4 3 into 3 is 9 again 3 into 3 is 9 4 into 4 is 16 5 into 5 is 25 if I go ahead and divide this it'll give me all ones and yep it does so this is how you can actually perform addition subtraction multiplication and division using numpy arrays now let me go back to my slides and see what are the other operations present so now guys let me tell you one more thing if I actually want to concatenate two arrays I don't just want to add those two arrays you can say that if my one array is a box then I want another array on top of it so let me show you how you can do that actually there are two ways to do that one is called vertical stacking and another is called horizontal stacking let me show it to you one by one first I'm going to show you vertical stacking for that what I need to do is print np. vstack and a comma B let us see what happens when I run this so we have 1 2 3 3 4 5 then again we have 1 2 3 and 3 4 5 so this is called vertical stacking if I want that horizontally I'll just write in here Ed stack and I'm going to run this now and you can see that we have 1 two 3 then again 1 two 3 that means these two are present added horizontally and we have 3 4 five again we have 3 4 five so this is how you you can perform stacking as well now there's one more thing that I want to show you if I actually want to convert this particular n numpy array that is a to say a single column so how I can do that just type in here print a. Revel that's all you have to do go ahead and execute this so you have 1 2 3 3 4 5 so you have 1 2 3 3 4 5 let me go back to my slides and see what are the other topics that we are going to cover now come certain numpy special function now I'm going to talk about s function and cosine function first so what I'm going to do is I'm going to use this cosine and S function and I'm going to plot s and a cosine graph so for that I'm going to import a module called mat plot Li so you don't need to worry about that module because I'm going to discuss about M plot lib in the upcoming sessions so there'll be a detailed session especially on matplot Li so you don't need to worry about what exactly matplot lib is and how it works and all those things because that'll be covered in the upcoming session for now what we are going to do we are just going to use sign and cosine function in order to print their graph so for that I'll open my py charm and let me remove this and import M plot lipt pyplot as PLT now we are going to Define two coordinates that is X and Y first X is equal to np. AR range 0 comma 3 into np. Pi comma 0.1 uh now I'm going to define y so for that I'll type y NP do s x now I'm going to use PLT in order to plot the graph x comma y now finally show the plot for that I'll type plot. show uh you must plot. show to make a graphics appear so plot. show and here we go go ahead and execute this and you might be able to see a graph and yep it is here similarly what if I change the sign to C and should give me the cosine graph go ahead and run this and you can see we have a cosine graph as well similarly if I write in here tan that is any other trigonometric function and I print this so I get the graph for Tan as well so I'll open my presentation once more and we are going to see what are the other special functions that we can use with numpy now numpy comes with two very good functionalities I would say that is called exponential function and logarithmic functions now exponential this e value is somewhere equal 2.7 and we all know log so when I talk about log it is actually log base 10 and when I'm talking about natural log that is log base e I will write it as Ln so instead of that I've written log that means log base 10 so you can perform these operations with the help of num py let me show you how you can do that so I'll open my py charm let me remove this and I'm going to define a npy array let it be ar equal to np. array 1 comma 2 comma 3 now I want to calculate the exponential value I'm going to throw in a print statement and I'm going to write in here NP do exp AR and this will calculate the exponential value for me and let us see if it does that or not so yep as I've told you earlier as well value of e is 2.71 so e to the^ 1 is actually equal to e so it has returned the E value but e to the^ of two will be somewhere equal to 7.38 e to the^ of 3 will be somewhere equal to 20.85% just type in here log now this will give you natural log so when I talk about natural log it is nothing but Ln or you can say log base e but if I want to calculate log base 10 so I need to type in here 10 first let me show you how you can find the natural log just go ahead and execute this all right so when I talk about one so e to the^ of 0 will be equal to one right so log or Ln AR equals to zero similarly the other values as well now if I want log base 10 instead of Ln or you can say natural log I can just write in 10 and go ahead execute this and you'll find log base 10 values so obviously when answer is one that means anything to the power of Z is equal to one so the answer will be zero here and similarly we have other answers as well if you are pretty if you're unsure about it you can open your calculators and do [Music] that so let us go ahead and take a look at what exactly is pandas so pandas is a python Library which is used for data manipulation analysis and cleaning and python pandas is well suited for different kinds of data such as we can work on table data with heterogeneously typed columns we can work on ordered and unordered time series data arbitrary Matrix data with rows and column labels we can work on unlabeled data and we can also work on any other form of observational or statistical data sets now I'm going to tell you how you can install pandas on your system guys it's very easy to install python pandas you just go to your command line or terminal and just type pip install pandas or if you're working on an IDE such as pycharm you can just simply type in P install pandas in your terminal over there or you can just open the project interpreter and add the library over there since we're going to work on Jupiter notebook I'm going to tell you how you can install python pandas on Anaconda so you just have to do one thing so I'll just show you guys how you install python Pand on your system you open the Anaconda prompt we'll wait for the prompt to set up so you type cond install it's already there in my system because I have already installed pandas since I have already worked on various data analysis projects and it's a very integral part of it because to work on a data set to read a data set you require pandas and it's just that you cannot work without pandas if you are working with any data related project so this is how important python pandas actually is I'm going to tell you a few applications of pandas as well so first of all you can just say that python pandas is an integral part of data whichever project you're working on so you can work on economics you can use Python pandas for stock prediction you can use it for recommendation systems then you can use it on neuroscience and statistics also you can use it for and then there is advertising so many data coming from different platforms you can just analyze the data using pandas you know clean the data for irrelevant you know inconsistencies in your data you can do that using pandas and then you can use it for analytics as well that's the very basic use of python pandas that I can think of so right now that we know how pandas actually is in Python Programming and what it is used for let's go ahead and take a look at the very integral part of pandas that is data frames and CDs we'll wait for this to install guys meanwhile I'll just tell you what are data frames and series guys so data frame is a twood dimensional and the size of the data frame is mutable potentially heterogenous data or we can call it heterogeneous tabular data so the data structure which is data frame also contain labeled axis which is rows and columns and arithmatic operations align on both rows and column labels it can be t as a dictionary like container for series objects now what exactly is a series guys so a series or a panda series is a one-dimensional labeled array capable of holding data of any type which is integer can be string float python objects Etc and the access labels are collectively called index and Panda series is nothing but a column in an Excel sheet so let's just take a look at a few examples and then you'll be able to understand this better what I'm talking about like pandas and series what exactly they are we'll be working on a example now so I'll just take it to jupyter notebook guys so we have already created a jupyter notebook if you are not familiar with jupyter notebook guys we have a full tutorial on how to use jupyter notebook all the cheat sheet and everything so you can just refer to those in our adeda YouTube channel and make sure you subscribe to ad for more exciting tutorials because we have a lot of content on python guys so if you want to Learn Python inside out or any other technology for that matter we have lot of content on YouTube you can refer also if you are at it be sure to check out our courses on ed. go and we have a full Python Programming certification program that you should also check out so since I have already installed python pandas I'm just going to import pandas as PD run this I'm not going to face any problem running this command because I have already installed pandas okay so what this command will do over here it's going to install the package the latest release of pandas for me I think that's why it's not running okay we have our first statement over here and we have successfully imported pandas as PD so I'm using the alas as PD I hope hope you guys know what alas is so let's just say I'm importing this Library so alas is going to be this one that is PD so for importing I'll tell you why I'm using this now if I want to create a data frame I'll just use DF as my variable name for data frame so I'm going to use the Alias now when I type tab over here okay I'll just so this is how I can use my alas to create a data frame so this is just to tell you how you use an alas now I'm going to show you how you can create create a series and a data frame so first I'm going to okay I'll have to import num as well because I'm going to use it to create a null value all right so DF is equal to I'm going to make a series so I'll just write it as s all right series and I'm going to pass a list of values let's say 1 2 3 4 5 6 and I'm going to use my nump Now to create a null value like 8 n and one more value let's say 10 so it's going to create a series now when I print s so we have a series which has indexes which are already there and all these values that I passed inside a list so this is how you create a series in Python guys using panda does after this I'm going to tell you how you create a data frame so for that also I'm going to tell you how you create a data frame using a dictionary object and how you can create a data frame using series as well so now what we are going to do is we are going to create a data frame by passing a numpy array with a daytime index and label columns so I'll take one variable let's say date or dates I'll just type it as D and I'm going to take PD dot so we're going to the date range and after this I'm going to pass a few value let's say 2020 and I'm going to pass values like we're in the month of March so I'll just write it as March and after this I'm going to take periods which is equal to let's say 10 so this is my date range guys okay I have an invalid syntax right should work fine now so when I print D over here so I have all these values in our date range format after this what I'm going to do is I'm going to take one data frame which I'm going to take as DF for obvious reasons to make it clearer and I'm going to take data frame and inside this I'm going to pass a few values so first of all I'm going to take a few random values so I'm going to use np. random dot random number and inside this I'm going to pass 10 let's say four and now I'm going to get the index values as D and I'm going to have to pass a few more values which is columns so I'll pass the columns as a list and I'm going to take let's say four columns so I'm just going to take okay wait a minute a a b c d all right do we have any errors no so now I'm going to print my data frame so I have a data frame guys which I have created using you know passing an umpi array and I have a daytime index with labeled columns which are a b c and d this is my index guys and I have all these random values using NP ARR so this is how you create a data frame guys just a simple example and I'm going to show you how you can create a data frame by passing a dictionary of objects that can be you know converted into a series also so I'll take let's say again DF is equal to pd. data frame and I'm going to pass a dictionary over here now so I'm going to take a few values first of all so first value is let's say a and after this I have to pass something right okay I'm going to write let's say a list of 1 2 3 and four after this my next value is going to be let's say B and I'm going to pass a Tim stamp let's say and for time St I'm going to use the same I have used over here 2020 03 01 I'll use the right and after this I'm going to pass one more value let's say C and I'm going to use a series now a series object and inside this I'm going to pass one and the index is is going to be let's say range all right index is equal to a list with a range of four because we have only four values over here we don't want any null values and after this I have to type in the data type as well the data type of the series guys so for that I use D type is equal to let's say float 32 all right after this I provide my next value which is D now for D I'm going to use uh an nump array and for this I'm going to pass a value let's say not three let's say five multiplied by four and let's take the d type is equal to integer 32 yes all right now I take my final value which is going to be e and inside this I'm going to pass uh a data frame or we're going to use the categorical object guys we're going to talk about this later on in the session so don't worry I'm just showing you how you can create a data frame using all these objects that we have at our disposal guys instead of test and train we can just call it as true or false doesn't matter we are taking categorical object so it has to be either true or false or it can be zero or one but it has to be decisive in a way that there are only two values so I take another value and for this let's just say I give the value add all right so our dictionary is done over here so we have created our data frame guys there's no error now when I print this so we have our data frame guys so a b CDE E and F so we have all these values using different data types or we can call it objects as well so for that also we can check the data frame and we just write D types and it's going to give us all the data types that we have so we have date time stamps over here integer float integer category and an object because I have used a string over here that's why it is giving us an object but in the new release that is python 1.0.0 it's not going to be an object it's going to show you it is a string so don't worry guys and we have already made a video on python pandas 1.0.0 with all the features that have come with the new release the new stable release released last month you can check that out as well to check for the new features that we have come across so now that we have done this let us take a look at the next topic that we have which is how to view data so viewing data is basically you know how you actually look at the data or how you going to look at the data using pandas Library so we'll just jump right to jupyter notebook guys and I'll tell you what kind of functions or all those things do you have at our Bay that we can use to view our data so we'll do one thing first of all we have data frames already that we have over here so I'll do one thing I'll just change it to df1 so that we have different data types or I'm sorry different data frames guys I'll run this this and this as well when I check for DF do D types should be different guys because we have already made a data frame using that all right it's not so I'll run all the wait so I'll do one thing I'll restart and run all the cells so that we have two different data frames so first of all the first very basic thing that you can do for your data frame is to use I'll just tell you guys you write DF do head so what this function is going to do is give you the first five values inside your data frame or the first five rows and similarly for the last rows you can use the tail method so this is how you get the first and last values inside your data frame so it's going to display all the five values that you have at your beginning and the end of your data set after this we have DF do index so what this will do is it will give you all the values from your index and similarly we have DF do columns which is going to give you all the columns from your data frame so this is how you view your data guys and then we have data frame. two numi which is going to give you an Umpire representation of the data so I'll just tell you how you can do that so just write DF do2 numpy wait a second guys yes so I'm going to create a npire array using this this I actually created an npire array and for dfr data frame of all floating Point values data frame. 2 numpy is actually fast and does not require copying the data so it is a very best deal for we have and then okay I'll just remove this I don't want this and then we have data frame. describe D which is going to give you somewhat like this which is going to give you the count the mean the standard deviation minimum 25% 50% 70% and maximum so these all values using the describe you can have which is going to give you an idea or a perspective of how your data actually is and what kind of calculations are already there that you can think of then we have sorting by an axis we can sort our data using an axis so for that you have to just write okay I'll just show you guys you just have to write d dot sort by index and inside this you you have to give the value of the axis I'll just give one and then let's say ascending you want it to be ascending no I just write it as false so it has given me the data frame by sorting the index similarly I can sort it by The Columns as well or I'm sorry sort it by values so I'll write as values and I want to give the value as say values and I'm going to give the value as let's say by I want to sort it by C it has sorted the values depending on C so this is how you sort your data frame guys and now that we know how we can actually look at our data I'm going to tell you how you select particular values inside your data guys I'm going to show you how you select a single column from your data frame so we'll write DF it's very simple guys to get a value from your data frame using only a single column you can write a or let's say C it's going to give all the values from C over here it has actually given you frequency it has given you name data type as well so this is how you actually get a single column from your data frame now let me show you how you can slice the rows as well so for that we going to use the slicing if you have actually worked on list comprehension so we have slicing the data over here we are going to follow the same principle here as well so I'll just write DF now I want my first starting from my first value to third value so it has given me only three rows starting from the zero row and it has given me third row and it does not actually include the third row because it starts from zero so I'm giving three values if I write six over here I'm going to get the sixth value but it's not going to be at the sixth row because the first row over here is going to be zero throw I hope you understand this guys so I have shown you how you can slice your data you know to get particular number of rows now let me tell you how you can uh select the data using the labels guys so for that you have to use DF dot there has to be location that Lo and inside this you're going to pass the values by labels guys all right so let's pass D that is n zero so let's see what the output is guys all right right so we have got all the values using the label that is D over here which we have passed over there in our previous section where we have declar the data frame and I'm sure this is visible to you guys now the next thing is uh selecting data on a multiaxis by label so what we'll do is we'll write df.loc and after this we write hyphen and we going to create let's say a and you're going to pass C right so it's going to give me the values accordingly which I passed over here so instead of a I can write b or I can write D so this is how you can select multiaxis using labels guys and I have written this over here so I can just write let's say 0 to three let's see what happens oh we have an error guys you cannot do this so we'll now move on to the next topic that we have is showing label slicing both end pints are actually included so how do we do that so instead of this we can just let's say copy this paste it over here and copy this paste it over here remove this so this is just to show you guys how you can work with it I'm very certain that the data that you work on is not going to be like this it's going to be very complicated so this is just to give you a perspective as a beginner how you can work with pandas now I'm going to tell you how you can reduce the dimensions of the returned object as well so for that you just type one thing guys remove all this get just one value and this is going to give you the column number at over there and this is how you get the values inside ided data frame so moving on let's say we want to get a Scala value so for that we just write okay let's say d0 right let's see if it works we're getting the same values guys only from the zero throw for getting the fast access to a scaler you can just write as uh DF instead of Lo you can just write at okay we have an error guys I'll just remove this and let's see if it works yes so I'm getting the exact value at the zero throw at the column number c now I'm going to tell you how you select a value from using the position inside your data frame so for that we use DF do ioc all right so let's say three okay so I'm getting the all the values from third column and similarly we can slice the data you know you can just get it like 3 to five right and we can add more values to this like 0o to two so this is how you select the values from your data frame guys now we have Boolean indexing as well inside our data frames so I'll just tell you quickly what it is so for that you just write DF now I'm going to check if DF column number a let's say this is in interesting guys so it has given me all the values inside a where a is greater than zero if I write here let's say two I have no values because none of the values are greater than two so this is how you can get the Boolean indexing this is actually important when you applying functions to your data frame guys so Moving On Let's uh take a look at another method which is is in method I'll just tell you how it works Works guys so it's basically used to check if the particular value inside your data frame is in there or not now there's one more thing we can set new values inside our data frame we can set a new column which automatically aligns the data by the indexes so for that we can make the series and we can set the values by label we can set the values by position and we can set the assigning with a nump array as well and the result of the settings will actually align with the data where a new operation with the setting can be followed where you can simply align the data frame with the existing data frame now let's go ahead and take a look at the next topic that we have which is handling the missing data inside your data frame so let's jump right to it guys we'll go to jupyter notebook and we'll work with our missing values now so pandas primarily use the value np.nan to represent missing data It Is by default not included in the comp competions and we're going to see the missing value right now so first of all you have to reindex I mean you have to do reindexing which is going to allow you to change add delete the index on a specified axis so which is going to return a copy of the data as well all right I'll just take df2 is equal to DF do reindex so this is how I'm going to do the reindexing guys so index is equal to let's say d 0 to 4 yes I'm going to get the columns after the indexing which is going to be equal to list of DF do columns and I'm going to add one more column which is going to be let's say e all right now what I'm going to do is I'll do L I'm going to check a few values so instead of dates I've taken D guys so D of Z and D of 1 at e is equal to one now let's check what is df2 is we have two null values over here so this is how I'm going to show you how to handle missing values inside your data frame so we have done reindexing so first of all I'm going to check for null values so we have true over here and we can get the count as well is null and we count these null values all right right now we're going to drop a few columns so we're going to drop the na that is the na values so as you can see from our data frame all the values that had null values are dropped actually the whole column has been dropped or we can do one thing fill in the missing data guys just do one thing okay check df2 we have so we can do one thing we can fill the missing values and we going to provide some value let's say value is equal to two right so we have actually filled the value with some of the value wherever there is a missing value we have given a value that is going to fill over there so this is how you get or you know check for all these uh missing values inside your data frame after this you can actually get a Boolean mask where values are n n which is null so for that you do PD dot okay is na df2 so this is going to get you a Boolean mask which I've already told you how you can check using DF do is null same thing but different processes to run this now let us move ahead and to the next topic that we have which is Panda operations guys Panda operations are nothing but a few operations that you can apply on the data frame or any other pandas object so we have descriptive statistics that we can apply we can apply functions histogram is there and string Methods is also there so I'll tell you what histogramming is when we are talking about it so let's take it up to jupyter notebook again guys I tell you how you can actually work with p's operations so first of all I'm going to tell you a basic operation which is that is a descriptive statistics so it's going to give us all the mean values similarly we can get one value like DF do mean provide one value over there so this is how you get it guys or we can write it as two right all right so this is how operating with objects that have different dimensionality and need alignment in addition P automatically broadcast along the specified Dimension so for that let's make a series I'll tell you just how it works PD do series uh give it a few Valu let's say 1 2 3 NP do Nan 5 or I'm sorry 4 five and then give the index value index is going to be dates and let's just shift all this two places right we have made an guys length of pass value is six and index implies 10 so we have to uh actually put more values so write 6 7 8 9 yes now when I print s over here we have all these values now we can do one more thing so we write it as DF do sub and we pass the S over here which is our series and we make an axis write it as index so we have operated with objects that have different dimensionality and needed alignment so in addition pandas actually helped us automatically broadcast the specified Dimension so now I'm going to talk about applying functions to the data so we already have data frame let's see what we have so we have this data frame guys so what I'm going to do is I'm going to apply a few functions so first of all I'm going to use the apply method over here and let's see what all do I have let's just check we have absolute absolute input all all close a Max a minimum angle any append all these functions that I can apply on this so let's say we have commum let's see what this does guys all right so this is how it works now let's apply a few more functions guys so I'll write Lambda X so we are talking about Lambda functions here I'm sure most of you must be aware of the Lambda functions that we have in Python if you don't have any prior knowledge on Lambda function guys there is a full tutorial on how Lambda function works Works in Python guys so this is how we applied Lambda function to get the subtraction between the X Max and X minimum right for all the columns we have the subtraction between all these values so this is how we apply functions to our data guys now I'm going to talk about histogramming so histogram is a representation of the distribution of data so this function we have which is map li. pip loot. Hest on each Series in the data frame resulting in one histogram per column so what we do is uh we'll make a series and it's going to give us value counts for histogramming how do we do that actually so we can just write s do value counts right let's see if it works right we have one value for each so this is how we get or do the histogramming with our data guys now I'm going to talk about the string Methods so series actually is equiped with string processing methods in the string attribute that actually makes it easy to operate on each element of the array so let's just move ahead with the example so I'll make a series guys PD dot let's say series and inside this I'm going to pass a few string values so I'm going to start with ED Rea write python next let's write Jupiter give a few null values as well to make it a little or slightly different from perfect all right so give it a few value let's say football and looking at the current scenario let's write WT so we have a series over here guys now what we'll do we'll take or use the string Methods so I'm going to make it all to lower which are already lower so I'll make to Upper guys upper letter words so everything we have changed using the string Methods inside our uh Pand series these are all the operations that you can perform on pandas guys so let's move ahead to the next topic that we have which is merging so in merging we are basically going to merge two data frames together so we have two functions which is concat and join so concat Panda's objects along particular axis with optional set logic along the other axis and it can also add a layer of hierarchical indexing on the concatenation axis which may be useful if the labels are the same or overlapping on the past AIS number so let's take a look at the example for this so we have our data frame I'm going to use PD do data frame give it a few values let's say NP do random do random number from let's say 10 and 4 all right so we have all these values now what I'll do is I'll break it into the pieces so how to do that so let's say I write it as df2 is equal to DF from first to third row the next one is DF from third to let's say seventh row and then we have DF from 7th to the end of the data so this is how I am going to break into the pieces now when I write it as df2 we have three data sets now I'm going to right now I'm going to use the concatenation function using concat all right I'm going to concatenate df2 so this is how I have concatenated the missing pieces not the missing pieces the several other pieces together using the pandas concat function now let's talk about the join function that we have so for this basically I'm going to tell you how you can uh do the left and right join so for the left join I'll write left is equal to PD do data frame and and let's see first value let's say a give it a few values let's say one two and then say B we have 3 4 all right we make the for the right as well so I'm just going to copy all this paste it over here okay I'm just going to write it as D this is going to be let's say C and change a few values so we have left and right and I'm going to just type left and then we get the output for right so I'm going to join all these two using the join function so for that I'm going to use the merge function and inside this let's say left we have right and on is equal to I'm going to join it at uh let's say a let's see how it works okay it doesn't so we have a key error which is a so change it to a guys so we actually joined using the merge function over here and another example that I can think of is uh let's say you have left and right we can change the values differently and we can just group them together now what I'm going to talk about is grouping guys so how you do the grouping of different data inside your data frame using pandas first of all for that you have to split the data into groups based on some criteria that you have and then after that you apply some functions on them and then later on you can combine the results into a data structure so first of all you have to get a data frame guys so let's see we have a data frame over here so we can actually group it by let's say right I'll just write Group by let say a and just going to sum right we have an error guys so we have a key error that is a because we don't have over there so I'll write it as let's say two all right we don't have two as well we have a key Arrow we don't have two over there because it's not an string value it's an integer value so we have our values right over here so we have grouped the data using the column that is number two now we can actually group the data by multiple columns and form a hierarchial index so for that I'll just copy this guys or over here only I'll just do one thing two and let's say three so this is how you actually combine multiple columns to form a hierarchial index but here again we don't have actually categorical values for these columns if we had we'd be able to do that you know if we had like let's say true and false it is going to have created a hierarchial index which will have different values for True column and for false columns so that's how you do the grouping or that's where you actually need grouping using pandas now let's move ahead and take a look at the next topic that we have okay we have already talked about operations then I have talked about merge where we have talked about concat and join and then I've talked about splitting the data how you apply the functions and then combining the results together merging we have talked about grouping now I'm going to talk about stack and pivot table so what exactly is a stack guys in panda I'm sure most of you must have heard about some other definitions of Stack so I'm going to tell you about this with perspective of Panda's Library here so the stack function is used to stack the Pres Skype levels from columns to index and it returns a reshaped data frame or a series having a multi-level index with one or more new innermost levels compared to the current data frame so you're going to understand this with an example so I'm just going to take uh let's say my topple is equal to I'm just going to take a list and inside this let's see we have values and we provide or we get two lists over here a list inside a list guys so let's take a few values take one 2 3 4 five and then again let's say 6 7 8 9 and 10 or we'll add few more values guys let's say 11 12 and 13 over here I will add few more values let's say 17 18 and 19 so we have our tole I'm going to create one more variable index so we'll have multi-index so from topples and inside this I'm going to pass my tle pass the names as well so the names are going to be let's say first and second so we have our index now I'm going to create the data frame guys so for this we write PD dot data frame and inside this I'm going to pass a few values NP do let's say random dot [Music] random number so we have eight values and two columns all right so and the index is going to be index and columns is is equal to A and B all right we have an attribute guys I made a mistake so there's no error now and I'm going to make one more data frame so inside this I'm going to pass this value now I check my data frame so I have reshaped my data frame with this first and the second and all these values now we're going to talk about the stack method guys so which compresses a level in the data frames columns so we're going to do one thing we're just going to do df2 do stack right this an attribute Arrow so this is how we stack or compress a level in the data frames column guys and with a stack data frame or series is having a multi-index as the index inverse operation of Stack is unstack by which default it's going to unstack whatever you have done with using stack so we'll do df2 do stack and this is how you unstack now we'll talk about the pivot tables guys or wait let's put it inside a variable let's say a all right so this is how you unstack guys we getting different values there now we're going to talk about the pivot tables that we have in pandas so it is nothing but the levels in the PIV table will be stored in multi-index objects on the index are columns of the result data frame so we'll take a look at one example guys which is going to be pretty clear so we'll take DF again dot data frame and inside this we're going to take a few values or we're going to take a list inside a dictionary so let's go with a few values let say one or let's say a all right we write a b c d we'll remove a few values over here multiply to three now we take another value which is going to be B and for this we're going to take a list again so let's say multiply to four take another value let's say C now and inside this uh we're going to pass a few values again so we're going to pass Six values so let's write p p and p q q and Q multiply to two because you want the number to be 12 and now D is going to be NP do random dot random number and the number we want is 12 and we take one more value e and we take the same values for this as well random. random number 12 no errors I guess so we have an invalid syntax guys so we forgot to add a comma over here and here and here right so I think it should work fine now without any errors so we have printed the data frame over here guys so this is how it looks now we can produce pivot tables from this data very easily guys the very reason of creating this data frame was to get the pivot tables now what I'll do is I'll just write PD dot pivot table and I'm going to pass DF over here DF and the values is going to be let's say d and index is equal to a and b and columns let's say is equal to C so this is how you create a pivot table guys now that we done with pivot tables uh let me talk about the next topic that we have which is time series and categoricals so we have done reshaping merging grouping as well now we going talk about the time series and categoricals so pandas has simple powerful and efficient functionality for performing resampling operations during a frequency conversion which is for example converting secondly uh data into five minutely data and this is extremely common in but not limited to financial applications so we're going to take a look at a few examples and for categorical data data that you collect can be either categorical or numerical so numbers often don't make sense unless you assign meaning to those numbers so for categorical data is when numbers are collected in groups or categories and categorical data is also the data that is collected in an either or yes or no situation for example we have zero or one we have true or false so that's going to be the category over there so let's take a look at a few examples to understand this guys the time series and categoricals so we'll take a look at a few examples for time series so first of all what I'm going to do is I am going to make a Time series guys so first of all what we'll do is we'll convert the data into five minutely data so it's very common guys so I'll just take the range first of all so we'll take the dates all right I made a mistake just cut this so I'll just run this again we'll get one more column and now what I'll do is I'll make one variable using the pd. DAT range yes and I'm going to provide the range as let's say 2020 01 or 03 01 we want the periods is equal to 100 and let's say frequency is equal to S all right let's print dates okay so we have dates over here I'll just remove this for now so now we take one more variable let's say TS is equal to PD do I'm I'm going to take a Time series and inside this I'm going to use NP do random and random integer which is going to be 0 to 500 and the length is of dates and then we have index is equal to dates so we have okay we have number attribute no random randant all right so we have an unsupported date time so I'm going to have to change this over here so we'll write it as let's say 3 1 or going write 33 2020 right so let's see if it works now doesn't so we getting the unsupported D type okay so we just mention a few more stuff over here so we'll write it as 0 0 0 0 so I think I figur out the problem over here so I've changed this to the this format and now I'm going to add one more parenthesis here and remove this one and now I'm not getting any errors guys so I'll run it again right so now what I have to do is I will have to make a few changes to that TS which is time series I've made so I'll write it as ts. resample and I want to make it to 5 minutes right and I have to sum this so no errors there guys so now I'm going to do the time zone representation so for that I'm going to get one more okay I'll just copy this guys this little change to this only so I'll just copy this paste it over here and here I have to make a few changes that is 0 0 rest everything is going to be fine and we change this to five all right so now we make another time samp and we just use PD do CVS and inside this I'm going to use NP do random dot random number and the random number is going to be the length of dates and dates over here all right so now when I print timestamp I'm getting the output as somewh like this so this is how you get the time zone representation guys so you're getting the date the time and everything and the data type is float 64 and then you can also get the UTC as well so for that I'll just write TS UTC and we just write time st. DZ localize and we get UTC right it's UTC now I'm going to print this so we get the UTC as well so this is how you create the time zone representation and now after this I want to show you how we can convert to another time zone so for that we don't have to do anything we just write TS dot all right TS UTC dotz which is time zone convert and we write us eone so we have converted into the US time zone and converting between time span representations also we can do that let's just say that's your exercise so you have to convert between between the time spans representations so that you'll be able to understand this better for that you have you don't have to do anything you just have to take the date range the period is going to be five and then you write the frequency instead of s you write it as M the rest all is going to be the same guys okay I'll just do it here as well just copy this paste it over here instead of s it's going to be M we have to remove this all right now I make the time stamp is going to be the same guys now print the time stamp this is how we convert or after this we take one more variable let's say PS and I'm going to do period all right now when I print PS over here this is the output I get so the frequency is m i have changed the frequency and now I can just convert it to time stamp guys see where it is this is how you create it into a time stamp now converting between period and time stamp enables some convenient arithmetic functions to be used so for that we have period range and all those things that you can add now moving on to the next topic that is categoricals so for that let me just take one more data frame guys so I'll write it as DF is equal to PD do data frame now I'm going to take a few values inside this uh dictionary the first is going to be let's say ID and now I'm going to pass a list with a few values precisely six so I'm going to use six values over here now my next topic is or the next key is let's say Raw grade or just grade we'll write so we're going to write a b c let's say again the guy is getting B and this one's getting a so we have five and one more let's say one guy has failed so we have our data frame let's print this okay so we have our data frame what I'm going to do is I am going to get the grade right [Music] so we'll get the grade is equal to DF grade now this is going to be the category I'm going to make all right now you print DF grade so we have grades like ABC B AE now I'm going to rename the categories to more meaningful names so what I'll do is I'll make the change over here only so I'll write cat dot categories and I'm going to write the categories as let's say good instead of pass fail I'm just going to write very good and then there is excellent all right so we have very good very bad and excellent all right we have an error guys there's something we have done so we have new categories needs to have the same number of items as the old categories so how many categories do we have over there let just copy this so we have categories four so we're going to have to make four categories guys so I'm just going to add good as well there shouldn't be any errors now what I'll do is I'll just set the categories so I'm going to set the categories now let's say here I'm going to have to give uh six values guys sry very good let's say bad very bad medium good very good any sort of categories I have to give over here so I'll just write very bad good and let's say medium right so after this I have set the categor as well now I'll just write DF great okay so we have good very bad then we have very good very bad and good and then we have n because we have not given any other category for that and we have the five objects so we are getting very good bad very bad good medium so that's how you use the categoricals in pandas guys now I'm going to talk about plotting using pandas so that's going to be very simple guys for that I'm going to have to import one more library that is matplot lib do pip plot I want to use it as PLT all right same thing goes with this as well all right this P plot all right so there should not be any errors now okay so I will close all now I'm going to make one series guys and inside the series I'm going to provide a few random values like NP do random do random number until let's say th000 or let's say 500 or yes 500 and the index is equal to PD do date range I'm going to take the date range as 1 3 2020 and let's take the periods is equal to 1,000 wait we have to take it as 5 500 because we have 500 values over there I hope no errors yes now I'm going to take the time stamp and we going to get the come sum all right now TS do plot so we have a plot over here using pandas guys this is how we have created one uh series using the random numbers from numpy library and using the P plot we have plotted a graph for random values which we have taken from 0 to 500 and the random range as well so this is how you take or get a plot using pandas guys now last but not the least we have another topic which is reading and writing to files so inside this I'm going to show you how you can can read from a file and how you can actually create a file over there so we have our data frame guys or we have our TS this this is RTS guys so I can just you know convert it to a CSV file guys make it or give it a file name as let's say ts. CSV and it's going to save the ts. CSV file somewhere in my directory and similarly I can read from a CSV file so for that I can just write PD do read CSV and I'm going to have to give the file location for that so I'll just copy one file location from one of my data sets so this is one data set that I have so I'm going to check for the properties or wait I'm going to copy this paste it over here okay we have an uni code error so I just write R over here and I'm able to read from the Cs v file guys look at this instead of CSV I can write Excel and it's going to create a Excel file or a CSV file it's going to read from so that's how we actually read and write from files like a CSV file which is a comma separated file basically or we read from a [Music] file so why we need data visualization now let us take an example suppose you work in an organization as an analyst and you have made certain analysis now you need to show that analysis to your boss or your CEO whatever now now you can understand it by just looking at the Excel sheet and the numbers but the other person is not an analyst he's not that technically sound as you are so what you need to do you need to present your analysis or your data in such a way that the other person can understand and how you can do that you can do that by representing it in the form of a graph and it is a well-known fact that human brain can process information easily when it is in pictorial or graphical form so that is one of the key reasons why we use a data visualization in order to understand the trend better and make better decisions so let us move forward and understand one more reason why we need data visualization now basically it allows us to quickly interpret the data like whatever the trend the data is showing us now basically it allows us to quickly interpret the data so just by looking at the graph we can understand what the data is about and what is a trend but if you are not from an analytics background and you see just the Excel sheet then it is very tough for you to understand it so if it is there in the graphical form you'll quickly understand it and you can experiment as well now what happens if you want to change certain variables and see what will be the outcome so with the help of the graphical representation of your data that will give you a very good idea as to what all variables are useful for you and what all variables are useless you don't need to include in your analysis in order to perform that experiment in an efficient and a quick way you need data visualization so Guys these are the two key reasons why we need a data visualization if you have any questions or doubts you can ask me or if you understood everything then you can give me a thumbs up so that I can move forward Brian says move on so does Niha Puja sidhar Theon Jason fine guys so we'll move forward and understand what exactly is data visualization so what is data visualization so data visualization is nothing but the presentation of your data in a pictorial or a graphical format now why we do that we do that in order to enable the decision makers of an organization to see the analytics presented visually so that they can grasp some difficult Concepts or identify new patterns now since we have discussed a lot about data visualization so guys I want answers from you all give me some example where I can use data visualization in an organization I don't need a lot of examples just two or three that'll be enough I just want to know whether you have understood it or not all right Devon says you can use it in finance or in what way can I use it in finance can you please tell me that Devon all right so he says in order to find out where should I invest my money yeah that is one perfect use case I would say all right fine guys so I'll give you a couple more cases where you can use it you can use it to identify the areas that need attention or Improvement in your organization you can even use it in order to clarify which factors influence customer Behavior now it might depend on the season or anything else basically now you can even use it to understand which products to place where now this is very important guys obviously you won't be selling jackets or sweaters in summers right you and you won't be selling shaving cream to kids so you should know what are your target audience and where you your where you are selling your product and to whom are you selling your product so that is one very important uh field where we need data visualization and you can even use it to predict sales volume as well fine guys so we have looked at a lot of examples for data visualization now let us understand the Tiger that is there in front of your screen so it basically tells you about how to find insights from your data now for that we need data visualization it plays a very very important role and it is one of the major applications of data visualization in order to find insights in data now there's a process that has to be followed in order to do that so it starts with visualize the first step is visualize so basically you understand what your data is you visualize it in a form of a graph and you understand what that data is actually talking about now we'll take an example suppose you have a data set that tells you about the youth percentage of unemployed youth country-wise from 2010 to 2014 all right so you have that data then you visualize it the moment you visualize it you'll get to know that okay it's fine this country has a lot of unemployed youth whereas this country is doing pretty good so accordingly you just you just grasp it pretty easily when you visualize it after that you make certain analysis now what sort of analysis suppose if I take the same example forward if I want to find out the change in the percentage of unemployed youth between 2011 to to 2010 so that will come under analysis phas so I'll analyze it after that what comes is document Insight now you have done that analysis but what is the outcome of that analysis now I'll see what all countries are doing good I mean the percentage of unemployed youth have gone up and which country it has gone down and for what all countries it is stable there's no change so we'll see that and make a document after that we transform our data set now there might be certain fields in my data that I don't need so at that time what I'll do I'll Transform my data set I'll remove those fields or there might be certain fields that I need and which is not there so I'll add those fields so accordingly I'll Transform my data set and once I've done that I'll again visualize it in order to understand what my data is now talking about and this process will keep on repeating so this is how you find insights in data where visualization plays a very very important role guys so I hope you have understood this flow so we have understood what exactly is data visualization if you have any questions or doubts you can ask me me all right so n is asking mat plot lib is used for data visualization yes Nia it is used for data visualization we going to see that in the next slide any other questions all right so we have no questions so we'll move forward and understand what exactly is matplot lip now it is very important for us to understand as to how matplot lip Works fundamentally now it is pretty easy and pretty basic you have some data then your computer will draw that data to a canvas of some sort but it is only in the computer memory now once your computer has drawn that data you can show that data so this is so the computer can first draw everything and then perform the more laborous task of showing it on the screen very basic I know you might find it pretty lame but it is very important for us to understand how fundamentally Matt plot Li works so you have an example in front of your screen so again we have that data in which we have country-wise percentage of unemployed Youth and I've done some analysis and I've shown it graphic ically the percentage change in the unemployed youth country-wise between 2010 to 2011 so this is how that plot looks like it is nothing but a bar plot now there are various types of plots available we'll have a look at few of those in the next slide so we'll move forward and see what are the various types of plots so I've listed down six types of plots first is the bar graph then histograms scatter plot P plot hexagonal bin plot and area plot we are going to plot all these things in today's session with the help of matplot lib module so we'll move forward and we'll get started with it we'll write our first matplot lib code so here's the most basic code in order to generate one of the most simple graph so let me explain you this code first of all what I've done I have imported P plot from matplot Lab as PLT then I'll use this P plot in order to plot the graph in the canvas and then finally I'll use this PLT which is nothing but P plot in order to show what we have got all right so pretty basic guys so what I'll do I'll open my pycharm and execute this practically all right so before I open my pycharm we have a question from Brian he's asking what are the tools that are used for data visualization all right so I'll tell you we have a lot of tools available in Market nowadays we have Tablo we even use R python so for python you need to import the M plot lab Library so there are many many tools available in the market right now so I hope this answers your question Brian all right so he says yes fine so I'll open my pycharm and I'll execute this practically so this is my py charm guys so over here I'll first create a python file with py extension and I'm going to name it as first code now over here I'll first import py plot from matplot lip so for that I'll type from Matt plot lip import pyplot as PLT now this PLT is pretty much similar to NP that we were using in numpy if you can recall so it is not mandatory to use PLT only you can use whatever you want but a lot of people use it as PLT and I'll keep it that way now our next step is to plot our graph to the canvas so for that what I'll do I'll type in PLT dotplot and I'll write in here my X and Y AIS value so for x axis I'll keep it as 1 comma 2 comma 3 then for y axis I'm going to add values such as four five and one so this will plot our graph in the canvas now final step is to show that plot how we'll do that I'll just type in here pl. show and we'll execute this and see what happens run first code and Yep this is how our first basic graph looks like so with three lines of code you have got this graph so we'll move forward and we'll see how to actually add title labels to our graph for that I'll open my slides once more so now of course there are some problems with our graph first off we have actually learned in school that we are supposed to put labels on each axis and we need to also add a title to our graph it actually doesn't make sense our graph doesn't make sense if there are no X and Y axis labels and a title so you don't know what is Axis you don't know what is x axis you don't know what is y axis and what is a graph about Plus in terms of programming it is very unlikely that you'll be actually filling it data to the PLT dotplot function now over here you can notice that we are actually filling in data like 1 2 3 4 5 1 instead you will be passing variables into it all right so it should be something like PLT do plot and you can say x comma y if you have defined variables X comma y all right so now let us show plotting variables as well as adding some descriptive labels and a good typ title how does that sound guys all right so I can see a lot of people are pretty excited so we'll move forward and see how do add title labels and plotting variables now the code is pretty much similar to the previous one but it has few extra Fields let me explain you that so of course I've imported pip plot from matplot lib after that what I've done I've defined two variables X and Y and I have certain values in it like 5 8 10 and 12 166 you can put whatever values you want and this can even contain a data frame a panda's data frame that has a data set after that what I've done I've defined PLT do plot function in which I have X and Y instead of filling the data I'm putting in variables that actually contain data after that I've added a title which is info then y Lael which is nothing but y axis and X label which is nothing but x axis after that I need to show my plot so for that I've done PLT do show and the result is there in front of your screen we have a title we have labels for xaxis and Y axis now let me open my py charm and execute this practically so before I open my py charm and execute this practically we have a question from sidhart he's asking X and Y contains list yeah X and Y actually contains list we have list of numbers 5 8 10 and in X variable and in y variable we have similarly 12 16 and six so I hope this answers your question sidart all right so he says yes fine so I'll open my py charm and execute this practically now this is my py Cham again guys so I'll remove till here and obviously I need P plot function from M plot lip so now I will Define X and Y variable so in X it'll have a list of numbers say 5 8 and 10 and now I'll Define one more variable Y which again has list of numbers 12 16 and 6 12 16 and six yep now what is our next step guys can anyone answer me all right so I've got a correct answer from Theon he's saying a PLT dotplot function which is absolutely correct so I'll write in here PLT do plot and instead of filling in data I'll be passing variables all right and now our next step is to add title and X label as well as y label so in order to add title I will write here PLT do title and the title of My Graph so I can just write here info and now I'm going to Define X and Y labels for that I'll type in PLT y label and I'm going to name it as Y axis similarly for X Lael I'm going to do the same thing xaxis all right now finally I'll type in PLT do show now go ahead and run this so you can see that we have a graph that contains the title info y AIS as a y label as well as xaxis as the X label now I'll again open my slides and we'll see what next are we we going to see so any questions or doubts still here guys you can ask me any questions so we have no questions so what I'll do I'll open my slides and we'll move forward now this graph is pretty much incomplete or ugly I would say because what if I want to change the width of this particular line what if I want to add some grid lines what if I want to change the color so all those things how will I do that no worry the next slide will actually tell you the same how to add style to your graph so how to add style to your graph for our first thing that you need to do is import the style function from matplot Li module after that you need to call it style. use GG plot so whatever plot that you want to use I'm using GG plot right now then again I've defined four variables X Y X2 and Y2 and all of these contains list of numbers again I'm telling you it can be a data frame as well which contains a data set then what I've done again I have used PLT do plot function but over here apart from X and Y there are many other fields so don't get confused I'll explain it to you it's very very easy so this thing G actually tells me that the color of my line should be green then this is label so this label is not for your x axis or Y axis it is for your curve which is line one you can give whatever name you want then we have line width I want the width of my line to be five so I have put in here five similarly I've have done for the next plot as well then I've added title which you know how to do that similarly y Lael and X Lael now after that I'm using this Legend function in order to add a legend to My Graph over here you can notice that we have it present in our graph where I'm pointing with my highlighter right now so we have line one and line two after that what I've done I have added grid lines to my graph which is of the color k which is nothing but black so you can see it in my graph as well we have grid lines available and finally PLT Dosh any doubts questions still here guys we are going to execute this practically in my pie charm so before that if you have any questions questions or doubts you can ask me all right so we have no questions so I'll open my py Cham again and I'll show you how you can do that practically so this is my py Cham again so this is my pie Cham again guys uh let me first remove all of this and I need to import the style function so for that I'll type from matplot lip import style that's it style. use GG plot Now define the variables first will be X add certain values to it l in 5810 58 10 then y I'm going to add list of numbers 12 16 6 now one more variable X2 again it'll have list of numbers which will be 6911 then I'm going to use Y2 variable and I'll add some numbers to it 65 and 7 all right so now I'm going to use PLT do plot function and over here first I write X comma y then now given a color to it I'm going to give it as green next label line one now line width is equal to 5 now again for X2 and Y2 I'm going to type in PLT do plot X2 comma Y 2 comma the color then label so I'm going to type in line two then comes line width all right so now our next step is to add title to our plot so plot pl. title and the title that I'm going to give is info again then X and Y labels PLT do y label ya AIS then PLT dox label xaxis PLT do show now go ahead and run this so over here I've not added any grid lines I can do that as well so for that let me first close it fine so I've defined title X label as well as y label now our last task is to actually add grid lines so for that I'll type PLT do grid and just write in here true comma give a color color I want it black so I'll keep it that way finally show your plot so PLT Dosh that's all go ahead and run this so you can see that this is our graph so we have changed the default linewidth we have added grid lines we have added labels titles plus we can add Legend as well so let me show you how you can add a Legend So for that I'll go back to my code once more just write in here PLT DOL Legend go ahead and run this again and you'll see that Legend has been added so here we have line one and line two as our Legend and we have changed the default line width title Y axis x-axis labels plus we have grid lines so this is how you can uh customize and add style to your graph so I hope you all are clear all right so everyone says they're clear so what I'll do I'll open my slides again and we are going to look at how to plot various types of graphs for example a bar graph or a histogram all those things fine so first we'll look at how to plot a bar graph so these are all the linear graphs that we have seen till now now we'll see how to plot a bar graph so before I tell you how to plot a bar graph using Matt plot Li let us understand why we actually use a bar graph so bar graphs are basically used to compare things between different groups and when we are trying to measure changes over time bar graphs are very well suited when changes are larger so this is why we use bar graph now let us understand how we can do that using matte plot lip now since we know why we use bar graph let me explain you the code and how you can do that using mat plot lip so first import P plot like we do every time after that instead of PLT do plot I'll use PLT doar and I have filled in data here you you can fill in variables as well that contains data then I have defined a label example one after that I have one more plot in which I have filled in data plus a label and I've given a color as well I don't want the default color I want some change so I've written a as green after that I have plotted the legend after that comes Legend and then X label and Y label that you have seen in the previous graphs as well then I've added a title and then finally show it don't worry guys I'll execute this practically in my py charm now so this is my py charm guys so over here what I have done first of all I have imported the PIP plot function then instead of PLT do plot I'm using PLT dobar and inside that I have data filled in instead of that I can even have variables that contains data then comes label in which I am using example one then comes label for this instance it is example one then again for one more plot I have PLT dob bar inside that I have data filled in then label and then in order to differentiate between both of them I'm using a color green for this particular bar graph then comes Legend and then after that and then after that I've defined X label as well as y label after that I have title and then finally show it now go ahead and run this and see if it works or not and yes it does so we have Legend here we have x x lab we have y label as well as X Lael and we have a title for our graph so any questions any doubts guys all right so we have a question from Dave he's asking what is the difference between histogram and Par plot all right Dave I'll tell you in histograms we have quantitative variables all right and when I talk about bar plot they have categorical variables so let me explain you this with an example so if suppose if I want to plot the GDP growth of every city in a particular country so at that time I'll use a bar plot because it has a category this particular City like New Jersey New York all those things now when I talk about I use histogram when I'm talking about quantitative variable that means if I'm talking about age group in that same example if I want to calculate how much each age group is actually contributing towards GDP growth so at that time I'll be using histogram so I hope this answers your question all right he wants me to repeat it fine I'll do that so when I talk about histogram it actually has quantitative variables when I talk about bar plot it has categorical variables Now with an example if I explain you you can take the example of GDP growth so if I want to calculate how much GDP growth is there for every city in a country so at that time I'll use barplot but if I want to calculate how much each age group is contributing towards the GDP growth of a country then at that time I'll use a histogram so over there we had a category in barplot like cities but here we have a quantitative variable that is age group so I hope this answers your question all right he says yes fine all right so let us move forward and focus on the code that you have in front of your screen so let me explain you this code so over here we have population ages so we have defined a list in which we have multiple numbers after that we have defin one more list or a variable bins in which we have multiple numbers again now instead of using PLT bar for barplot for histogram we use hist PLT doist then comes population ages instead of data I'm filling in here variables that contains data then bins then his type I want it to be a bar type now when I talk about his type I want it to be bar and then the width should be 08 that's all that's all you can understand it is X label y Lael title Legend and finally show the plot now I'll go ahead and execute this practically in my py charm so this is my pycharm guys and just to save time what I've done I've actually copied it already so over here you can see we have imported the M plot Li module then we have a variable population ages and bins now instead of using pl. bar I'm using PLT doist for histograms then comes the two variables and then hist type is equals to bar and then I've defined width similarly I have defined X label y Lael and title like the previous examples then comes Legend and then finally show the plot so let us go ahead and execute this and see if it works or not yep it does with so we'll move forward and understand scatter plot as well now before we understand how to plot a scatter graph we need to understand why we actually use Scatter Plots usually we use Scatter Plots in order to compare two variables or three if you're plotting in three dimensions looking for a correlation or groups so basically you try to find out how much uh two or three variables related to each other so over here as you can see there is one point here and there's another Point here so these two are pretty much dissimilar to each other right now when I talk about these two points are pretty close to each other so you can say that these two are pretty much similar and basically what we are doing here we're trying to find the relationship between the two variables and that and that is actually called your correlation now let us understand the code now let me explain you the code so first of all we have imported the PIP plot function from the mat plot Li module then we have two variables X and Y which contains a certain set of values now instead of calling PLT dotplot or PLT do bar or PLT do H I'm using scatter because I'm doing it for a scatter plot right now inside the parenthesis I have X and Y variables these variables contain data and I can even write the data instead of just writing the variables as well now I've defined a label and then color then rest of the things you know we have X label y Lael the title then Legend and then show that's all now let's go ahead and execute this practically in my pycharm now this is my pycharm and over here I've already pasted the code so I'll just go and run this and we'll see what happens so this is how our scatter plot looks like so here we have our Legend then we have the title then we have our y AIS then x- axis these two are the labels for that so let me close it and see what is the other plot that we are going to focus on now once scatter plot is done we'll have a look at the area plot or you can even call it as a stack plot so basically these area graphs are very similar to the line graphs okay they can be used to track changes over time for one or more groups area graphs are good to use when you are tracking the changes in two or more related groups that make up one whole category you can take an example of say public and private groups okay so before I explain you the code let me tell you one thing guys the problem here is with polygons we cannot actually have labels for our data in order to solve that problem all we did here was plot some empty lines giving them the same color and the correct labels in accordance with our stack plot we also gave them a line width of five as you can notice here to make the lines a bit thicker in the legend now we can easily see that we are spending our day sleeping eating working and playing so this is how the code works now let me execute that practically in my py charm so I've copied the code already just to save time again now this is our code over here I've already explained you why are we defining these empty lines just to add labels now let us go ahead and execute this and see what happens so this is our graph guys and again we have Legends title to our graph and Y Lael as well as X label so we'll move forward and look at various other plots this is called a pi plot so let me tell you about pie charts a bit so pie charts are pretty much similar to stack plots only they are for a certain point in time typically a pie chart is used to show parts to the whole and often a percentage share you can consider the example of percentage of market share and things like that right now let me tell you guys matplot lib handles the sizes of the slices and everything so all we need to do is just feed it the numbers that's what we have done here so let me explain you the code here so over here we have so over here we use pl. piee in to plot a pie chart we have slices we have specified slices now slices which are nothing but the relevant sizes for each part then we specify the labels and then the colors list color list for the corresponding slices next we can optionally specify the start angle for the graph this lets you start the line where you want in our case we chose a 90° angle for the pie chart which means the first division will be a vertical line next we can optionally add a shadow as well and after that we can even use explode to pull out a slice a bit so we have exploded that say 0 comma 0.1 comma 0 comma 0 so because of that you can see that eating has been pulled out a bit now basically we have four total slices so with explode if we didn't want to pull out any slice at all we would have just written here 0 0 0 0 now if we want to pull out the first slice a bit we would do 0.1 comma 0 comma 0 comma 0 and if you want to pull out the second slice a bit what we can do is 0 comma 0.1 comma 0 comma 0 like in our case now finally we do an autopack now this autopack overlay the percentages on the graph itself so you can see that we have 8.3 29.2% 54.2% all those things written on the graph itself and finally add a title and then show it now let me execute this practically guys now just to save time I've already written the code so I'm just going to go ahead and execute this and we'll see if it comes not yep here is our pie chart so we have seen multiple types of plots we have seen Pi plot bar plot histograms area plot now what we need to understand is how to handle multiple plots so at that time what will you do let me open my slides again and we'll move forward with that so this slide actually tells you how to deal with multiple plots now you can notice on the figure as well we have two plots available now let me help you out with the code that is there in front of your screen so in the beginning we are importing a couple of modules one is matplot lip. pyplot which you are already familiar with after that there is a numpy module as well so we have covered numpy modules in the previous sessions if you have any doubts you can go through it now after that you might find this part confusing if you're not familiar with numpy now let me just tell you what it does it will create a numpy array that will have elements between 0 to five in these types of of 0.1 now similarly T2 will also be an numpy array that will have elements ranging from 0 to 5 and in the steps of 02 now after that the code is pretty much similar to the previous examples that we have seen But there is one New Concept that is called subplot now let me tell you what this subplot does it helps us to plot multiple plots all right so when we write subplot 211 this means that we have two plots horizontally we have only one plot present and vertically we have two plots and in that vertical position this plot will our first graph now you can notice that we have one more subplot that is 212 so so vertically we have two plots horizontally we have one and this is the second plot so what I'll do I'll open my pie charm and I'll play around with it a bit so that you'll be able to understand it better so now this is the code guys first I'll go ahead and execute this and we'll see the result so we have a graph something which is like this right we have two plots which are aligned vertically now what I'm going to do is I'm going to just minimize this and I'm going to change the subplot value so that you'll be able to understand better how we are dealing with multiple plots now over here if I make this as two and this one as well as two so let us see what we get so we have got the same graph but it is aligned horizontally if you can notice this is our another graph and this is our first graph so you can notice the difference between both of these graphs right so this is why we use subplot so we have certain values in the subplot right 2 two and one now what does two means that we have two graphs available with us after that whether I'm aligning it horizontally or vertically so if I'm doing that horizontally horizontally I have two graphs then among those two graphs this is my first graph this this is what it means now similarly when we have 222 here this means that we have two graphs horizontally we have two graphs available and this is the second graph that is present so similarly if I make a change here and I make this as 211 and this as 212 this means that horizontally we have only one graph and these both graphs are aligned vertically so we have this was the first graph and this is our second graph this is how it works so this is what basically subplot [Music] is basic python question so when you're talking about basic python questions there are a couple of specific questions which keep repeating okay now uh this is the first question that we have what is the difference between a deep and a shallow copy so uh even before I get into this uh let me just check with you all so can you give me a background as to which particular background do you come from do you come directly from Python and you know what do you want as a takeaway from this particular session so I would be happy if you could reply in the you know questions box please a quick introduction into you know basically what you are looking for in this session as such and you know what do you want as a take away okay while you answer that okay let's tackle a couple of questions there okay so as I said basic python questions my suggestion would be to get yourself familiarized with the various data structures that you have in Python okay that is a most heavily asked you know it's heavily asked as such so you've got questions like you know what is the difference between list and tuples what exactly do you mean by mutable what is immutable so you've got couple of questions like that okay all right so Roma you basically come as a Java developer you're expecting to Learn Python and know what you should focus on so as to move into python that's a great thing to do because you would find it pretty easy to you know move into python as compared to working in Java so we'll take a look at this okay let's go into the basic python questions as such what you typically come across so what is the difference between deep and shallow copy okay so let's uh do one thing okay let me open up uh python prompt okay now so even before we get into this let me just give you a background so what do you mean by a shallow copy it means that it is used when a new instance type gets created and it keeps the values that are copied in the new instance so basically we are copying okay copying whatever is of the new instance from an existing instance okay so it is used to copy reference pointers you know uh it copies just the top level values it doesn't copy each and everything thing and create a new object so there would be references okay for example okay I have L1 is equal to 1 2 3 4 okay this is a particular list okay I have a list L1 1 2 3 4 now L2 is equal to L1 I'll create a you know I wouldn't say it is a copy it's just a reference of L L1 and L2 so if I even do l2. append of 10 so it would reflect both in L2 and L1 okay this is a no this is basically a referencing python you know python referencing now when you're talking about copy we usually have uh you know a particular uh module named as copy so the shallow copy works this way so I've got L1 okay I've got L3 okay I'll create L3 copy do copy of L1 okay if you look at L3 it is the same as what we have in L1 now l3. append of 11 let's take a look at L1 comma L2 comma L3 now okay L1 is at 10 L2 is at 10 and L3 has moved to 11 which means to say it has created a copy okay this is a shallow copy okay so it is used to copy all the reference pointers it copies all the values but it creates a new new original by itself in a separate memory location so to check the memory location you can do this L1 ID of L2 ID of L3 okay you see L1 and L2 are the same but L3 is different which means to say that there is a copy which is happened but you know the admin structures in copy uh so the admin structures of L1 are not copied to l three okay so we will not delve into that depth because there's a whole lot more happening okay there's a whole lot more happening but what I can tell you is if you are to tackle the question this is what you need to say okay L1 so basically your shallow copy copies all the reference pointers all the values and it creates a new memory location so as to you know you have a copy of the entire set which you created whereas a deep copy it takes care of all the references of the references and a whole lot more which means to say it has to make you know pointers to each and everything it has to make clearcut copies for each and everything that does not happen in Shallow copy please remember that does not happen in Shallow copy okay so deep copy gets deeper and deeper and when you take you know what do you say copies of instances as such that is where issues come in when you're using deep copy so it makes it much more slower to run deep copies okay it makes it much more slower to because it has to copy all the references all the admin structures all the values each and everything whereas shallow copy does not copy all the admin structures just the top level references and the original values that's it are you all clear on the copy and deep copy yeah so the example for deep copy would be something like this okay L4 copy do deep copy in case you were asked about that so L2 okay L4 ID of L4 you'll see it's got a whole different ID Al together okay so this is how you create a shallow copy and this is how you create a deep copy please remember this because it does come up not frequently but you know it's good to know something like this particular thing what is the difference between copy you know a regular copy. copy what is a deep copy and what is a regular reference are you all clear all right great so let's move on okay let's move on so the next question this is a very frequently Asked question that is what is the difference between a list and tup okay what is the difference between the two of them now we already took a look at list as such no let's do it again these are all lists over here 1 2 3 these are all lists animals marks and commands are all lists and the pupils are the one Within These list so let's go back here let's create X or L1 again 3 4 okay and T1 as a t 2 34 okay so here's a difference okay list are data structures which can be modified at runtime whereas your tuples cannot be modified at run time okay we have list methods which allow us to do that for example list. aend of 10 will allow you to do it this happens at runtime okay but but we cannot do that to a tuple okay we cannot do that to a tuple we'll see that you know it does not have such attributes so you might wonder why do you need tuples when you have lists Okay the reason being when you have fixed data and you know exactly the size of the data which is coming into to you okay or coming into to your server or coming into to your application that is when you use lists because these take up fixed or static space okay whereas your lists are allocated space which you know is dynamic in nature we can append we can remove for example list. L1 remove of three and you'll see that you know three has been removed so you can keep on modifying it whereas your tuples are fixed in nature when you're thinking about fixed amount of data coming in fixed amount of data going out and you are thinking about memory intensive tasks always go for tuples when you are thinking about buffers when you're thinking about cues when you're thinking about you know uh Stacks always think about lists am I clear on that so the first question which might come up would be what is the difference between a list and a tuple basically a list is mutable a tuple is not am I clear could you reply with the wire n so for example okay still unclear as to where to use tuples now you have got a database okay my DB okay so for example I've got fixed columns okay column with ID column with name and a column with mark C okay so the list represents the rows okay so this is one row this is the second row something like this okay when you know exactly how many columns you have you go for a tup when you don't know how many rows you have you go for a particular list that is how it has been you know it is structured even all the apis my SQL post SQL SQL light are all structured this way okay where the columns are represented by pupil and the lists are represented by rows okay so when you are writing in data you have fixed number of columns so you have a fixed number of data you cannot simply tell I want to write n number of columns into an N minus one number of you know column database does that make it more clear Roma all right okay so that is about list and tup and there is one more thing okay you might get a question like this okay say L1 is equal to 1 2 1 3 4 comma and I would put in a tuple inside this okay so let me ask you a question will you be able to modify the list okay so modify the list or will you be able to modify the Tuple the Tuple exists within a list which is a mutable thing okay and here I'll give you something like this 1 2 three okay so you've got L1 and you've got L2 okay will you be able to modify this tup could you all reply from my explanation you should be able to answer this so here's the thing Tuple you will never be able to modify will you be able to modify this list which is inside the Tuple though so you say I don't think so but it is you are able to modify it because it is referenced as I said earlier so if you see L2 now it would have been modified remember lists are referenced when even inside this the structure the attributes of a list do not change even if they are inside a duple it's a tricky question for you know beginners it does come across all right now how do we achieve multi-threading in Python is a question which comes up okay so python does have a Threading package okay so as to it's got a Threading package it's got a multiprocessing package there are two different concepts threads are related to in a specifically threads and multiprocessing is specifically related to the number of CES which you have on your system okay now python has something called a Gil Global interpretor LW this is a question which comes up okay typically when it comes to threading this is a question which comes up as to what is a g the Gil is a global interpreter log which makes sure that only one thread can execute at a certain point of time in Python okay so what happens is that the thread acquires the G it completes the work and moves on to the next thread so if you're talking about multi- thread this is what typically happens okay you created multi- threads using the threading package okay what happens that it moves from one thread to the other thread to the other thread so that it seems as if it is executing in parallel okay it's executing in parallel but what is happening is it is actually taking turns using the CPU core it looks like it is executing faster okay but not necessarily all the time but there is a particular way it's a dirty hat if you might say you can however turn off of the G over here okay it mimics okay so it does not happen parall it mimics parallelism is what I would say okay it mimics parallelism but there is a way to do it parall also where you can turn off the Gil please remember this is a very uh what do you say dirty way of doing it so if you turn off the Gil your memory management can get messed up so typically they don't do that there are other packages which have come in which makes better use of threading okay so you have to be very careful while using the threading package as such so when you're turning off your Gil you have to make sure that you write your semaphor your muxes properly so that it does not mess with your memory management okay because python memory is internally managed by you know the python interpreter itself unlike you know something like C or C++ where you know you have to do the memory management part okay so is that clear to you all so Gil can always add an overhead to your execution okay I'm not saying don't use you know multi-threading okay I am saying multi-threading is heavily used okay it's just that it is used depending on context so in case you have a lot of codes it makes sense to use something like you know threading okay is that clear to everyone okay beside M multi threading you can make use of multiprocessing where multi processing takes advantage of the number of codes you have so if you say I have got four C processor you can make use of all those four CES and run parallel jobs on all those four CES okay so you've got something called multiprocessing for that okay Library called multiprocessing for threading you have something called threading everyone are you all clear okay great now basic python questions when it comes to Turner operation so I wouldn't call it exactly turn it behaves like aary operation okay so where you know you have conditionals If X or Y okay something like this so how does it work okay so basically this how you do it so you've got X and Y okay X comma Y is equal to 25 comma 50 let's take up this example okay 23A 50 we need to find the largest among these okay so you have to be sure as to what you're doing x if x greater than y else y okay if x greater than y else Y which means to say it evaluates for X so it will give x if x is greater than y else it will give y so when you take know big you would have got this particular answer okay so the expression gets evaluated like this x less so in this particular case this is not X greater than y it is X lesser than y this is for X you know the smaller one okay uh so it's not big it should be small instead okay so is true so if x is greater than y return X else return y that is what basically happens here are you all clear how to use you know how to mimic a Turner operation great now what is monkey patching in Python this is something which appears when you have gotten into classes as such okay now so basically it means to say that you know you're applying a patch at runtime okay or you know basically Dynamic modifications of class or a module at run time so basically what happens here so let's quickly create this example recreate this example over here and then I'll tell you what is a use of monkey patching so please remember do not follow this particular thing follow this particular you know syntax because I am using new style classes this is a question which comes up again as well okay do I use old style classes or new style classes this is something called an old style class this is using new style classes always use new style classes it is only Legacy architecture which makes use of old style classes new style classes where the classes inherit from objects and object parent class as such def F self okay I'm creating a particular you know function print so it says that you know just print this in case you are in working me now okay I will import something like M so typically I would import from a separate python script as such over here I'll show it to you directly in practice what exactly you're going to do okay monkey self print monkey F so basically this is going to be your patch which we want to apply to this particular function in this particular class okay so what you're going to do is my class so if you do my class. f now let me instantiate this class first X is equal to my class x.f okay so you see that you know f is printed over here okay now my class. f okay so F becomes a function attribute of this particular class okay now is equal to Monkey F so I am forcing this onto this particular function okay now what happens I will create an object OBG okay which is my class okay obj do F will give you monkey F you know it would have applied this particular patch onto this particular function are you clear as to what happened exactly so we are applying a dynamic change for a particular function in a particular class at runtime did you all understand how it is applied as such we are you know forcing a particular function into a particular function attribute of a particular class at run time did you all understand so what happened here is that you have a particular class which contains a particular function f okay so this particular function is completely independent of everything else which is part of this class it is only part of this class now you create an additional function called monkey F so which want to patch into this particular function of this particular class do you follow me so far Roma okay now what you going to do so initially if you are calling directly from that particular class my X is equal to my class x.f it will give you this F and this is the one which was found earlier which is naturally a part of that particular class okay now what you do is you are going to create another function which is not part of this class remember not part of this class you create a function okay and you have a different working of that particular function and you want to patch it for this particular function which is part of the class okay you are going to force this particular function which is why you do my class. f is equal to Monkey F where f is the function of this particular class monkey f is your uh what do you say patch function following me so far everyone okay now what I do I create an instance of that particular class after patching and you see that patch has applied over here so are you clear everyone are you clear now as to what exactly I did okay so why it is used is basically you know it is used as a quick fix in case there are issues you know in the code self is basically for the you know similar to a con know it's not a Constructor it is a self-definition for a particular Constructor okay so to create instances as such okay now so basically what you do is you create you know patches so as to get Dynamic attributes at runtime which you were initially not thinking about or you know applying quick fixes so as to you know at runtime so that things do not go wrong maybe you know something there was a functionality and it is not working right so you go ahead and apply a patch for that something like this okay this is how you do it without changing the class itself okay now here is a question okay how can you randomize the items of a list in place in Python okay so this is a question which makes use of you know something called a random we have a particular uh module in Python called random from random import Shuffle okay you've got a whole lot of stuff within random but we not going to take it up so what we'll do is we'll create a list just a moment okay so we create this particular list X is equal to keep the blue flag individual items and you want to shuffle this thing in place okay you want to shuffle the original item you do Shuffle of X when you print X you see that the you know items have been shuffled you do it again you see that again it has been shuffled okay this is how you Shuffle items in place are you all clear all right super now write a sorting algorithm for a numerical data set in Python so maybe the question is not totally clear but what we are trying to say here is write a sort for a data set which is a string so basically integer strings basically okay so 1 4 2 10 5 are a string you know set of strings or list of strings okay which are all actually integers so how do you do that so there are two different ways of doing this okay so let me do this L1 is equal 145 so 1 4 2 and five okay so one particular method is doing this now you have a string so if you do L1 do sort let's do this okay now L1 sort will give you something like this okay it is not the right thing that this is not right at all okay and you wouldn't want something like this okay what you want is 1 2 4 5 10 so when you do a sort like this it applies on the asky characters as said so it applies on the first found asky characters which is why 10 will also come out of place so when you want to do an integer one this is what you do this is the first one L1 is equal to int of I or I in you might be wondering what this is so and L1 do sort so now you see that it has been sorted in place so what we did here is we use something known as a list comprehension which can you know Traverse over a particular list quickly and you know shorten your code you create a list in place itself okay which means to say for I in L1 that means we are traversing over L1 first and we are creating an integer out of that particular thing we are forcing it into an integer type int of I for I in L1 first we change all the items into an integer and then we sort it in place are you all clear regarding this now okay similarly you can do the same thing in a different way by using another one so L2 is equal to you use map okay of int comma L2 okay L2 do sort not L2 do sort let's see first we CH it into an integer this is a very short one instead of using a particular list comprehension you can use a map okay map of in comma L2 and L2 is equal to 1424 and one L2 do sort will give you the sorted items so there are two ways of doing it this is one way and this is the other way okay are you all clear so map what it does is it takes up any function okay and it Maps it to a particular list of item does that answer all your questions that you have in mind R madun all right now you have another you know set of questions over here which basically are you know simple programmatic questions so let's take a look at that so D1 is equal to a b c d and D2 is equal to 1 2 3 4 5 okay there is a data structure so you want to create a dictionary out of two different lists or tuples which have matching number of arguments this is how you do it zip of T1 comma T2 okay what you get is a list of tuples which are zipped together A B C D E when you do dat of zip of T1 T2 you actually get a particular know zip dictionary which is Created from the list order CU is it clear with everyone okay so this is the first thing you want to create a list of items okay a list of integers from 0 to 9 you do it like this using range range of 10 okay now A2 is equal to sorted of I for I in A1 if I in a not okay what it means to say is first we are going to Traverse over A1 which is 0 to 9 okay 0 1 2 3 4 if I in A1 so I will pick the value 0 1 2 3 4 till 9 it will check whether the value of I is present in a not okay a not where is a not so basically your a is equal to just a moment yeah so you'll not get that a not okay so what happens is that you get it as an empty list reason being it searches for you know when you check for a not a not is a dictionary okay and it checks for the keys to be 1 2 3 4 5 and it is not it is the values which are 1 3 2 5 4 whatever it okay it is the values not the keys all right which is why you get an empty list similarly A3 is equal to sorted of I for sorry a not of S for S in a not okay so we are traversing over a not first okay we are traversing over a not we are traversing over the actual Keys as such a c b e d okay so it checks for a not of s okay we check for a of s so what happens we get it has 1 2 3 4 5 you get the values for all of them similarly A4 is equal to I for I in A1 if I in A3 okay so we are traversing over A1 this time and we checking if you know this 0 1 2 3 4 5 is present in A3 you know which is actually present okay and when you do A4 you see that it will give you the values for 1 2 3 4 5 now A5 is a dict comprehension this was a list comprehension this is a dict comprehension now I colon so I would be your particular T okay I colon I into I that is we are creating a particular square of the item for I in A1 A5 will give you 0 0 1 1 2 4 3 9 416 so on till 9 okay so we are creating a dictionary comprehension by using this for I in A1 okay we are traversing over A1 which is a particular range of items and finally A6 is equal to we are creating list of items I into I for I in A1 A6 will give you the same thing as a list of a list so are you clear with how these work as such from a very top level are you clear with how these work very nice now there are some more things now when it comes to regular expression in Python okay how do we do various things in you know python so we've got an re module so we've got split uses a reix pattern to split a given string into a list okay so split it uses a reix pattern sub will find all the substrings where the reix pattern matches and replace them with a different string okay and subn will replace it like it does in sub but it will give you how many Replacements happen Okay for import R okay my S is equal to something like blah blah blah something like this okay you have a Rex pattern like this pattern is equal to BL something like this okay so what you do is my is not R do split of my S patter okay okay so the best way of doing it is typically doing a normal split okay so let me do this okay so what it does it splits over the pattern bla okay bla bla bla the first one is an empty string then it gives you H H and H pattern comma the string which you wanted with okay substitution okay I'll do it for S subn because it works similar to sub okay R do subn okay of your pattern that is block comma your string okay which is my S so you've got a pattern okay replace BL with sorry I missed out this a with pattern and my S okay so it will replace the blah with you know BL okay it will replace with know a particular a so if we take it the other way around pattern a so you it replaces BL a by a only a okay did you get it how it replaces a particular pattern everyone are you clear with how it replaces a particular pattern okay so let me go back again okay so you've got your string my S you've got the pattern which needs to be replaced okay and you have an IDE item replacement item a okay R do subn okay R do some n your replacement item your pattern which needs to be replaced and your string okay would be this way but if you go this way your pattern your replacement item and your string you'll do it this way take your pattern replace it you know replace all the items in your pattern with the replacement item a and you get ah ah ah so bla bla is replaced with a AA and it gives you how many times it has been substituted are you clear now so Roma okay so let's continue with our questions what is inheritance in Python okay inheritance is basically gaining the attributes and methods from one class into another so from the parent class to the the child class or from the base class to the subass as such okay now so there are different types of inheritances okay so there is single there's multi-level there is hierarchical and multiple so typically the highly used ones are either single inheritance or multiple inheritance now how does it actually function so where a derived class acquires the members of a single super class is known as a single inheritance multi- level is you know one class derives from you know another class which derives again from another class so basically a derived class is inherited from a base class and the other one inherits from the previous BL no previous class as such the so D1 inherited from Base Class B1 okay and D2 is inherited from there's a mistake here it you know what do you say inherits again you know from that particular class hierarchical is based in you know one based class you can inherit multiple child classes so basically it might be something like this hierarch so class my class class M Class my class M2 class of my class so this is hierarchical inheritance you have to be careful while using hierarchical inheritance because if you are instantiating both the classes you have to be sure as to you know which one you are going to use you know if it is going to be using from M class or is is it going to be from M2 class if it is deriving from the same one okay you have to be very careful especially with the functions which have been derived from the base class as such okay now multiple inheritance is where you derive from multiple class classes okay it is from multiple classes so my class so another class will derive from my class comma my C2 something like this so child will derive from two different you know super classes as such are you clear as to what exactly inheritance is so you've got an example here V1 V2 child class parent class pass and V1 and V2 okay so this is basically what you mean by inheritance in Python okay moving on to the next part it might not be relevant to everyone but you know people who are moving into web application development and specifically to Jango might find it relevant okay now this is a question so mention the Jango architecture so there are various patterns which are available so you've got MVP you've got MVC you've got mvt now mvt is you know a model view and template it is similar to that of MVC which is model view controller but here what happens is that model is similar to both of them both MVC and mvt the view over here functions as a controller itself okay and this V the view in Jango functions as a regular controller it takes care of all the controller aspects whereas the template the T okay is similar to The View you know MVC so basically view it is the T that is template is similar to the v in MVC that that is the view in MVC because T is basically the templating part where you have your HTML templates your text templates you've got different types of templat typically your HTML templates now you've got your model which is part of your database where you know you define your database as such your database schema is defined in the model The View and the template Maps the model so basically the developer provides a model and the view and the template then Maps it to the URL and Jango as the magic to serve it as such okay you've got the model you've got the template it is then taken up into the view and given as a URL through Jango to the user are you all clear regarding the model view template structure as such so templates would be your regular HTML Pages which contain Dynamic elements in them okay for example let me just give you a simple template as such for example I have something like this HTML SL HTML head SL head body slbo okay your head can contain Dynamic elements okay my title something like this a variable called my title where let me just do this one second my body something like this so my title and my body are information this is a template okay so the view will pull up information from the model probably all the titles and body information is in the model just an examp example okay is in inside the model which is inside your database so what it does is it takes up the title it will populate it with the title it takes up the body it populates with the body respective body title body title body so you can have hundreds of items and you can have a single template serving different types of templates does it answer your question Roma okay so now explain how you can set up a database in jago so we'll go with a default database which is SQL light which stores data as a single file in the file system so you can also have databases like post myql Oracle Ms SQL other than SQL light but then you need to use your administrative tools to create a new database you know for example you might have to use something like SQL Alchemy or something like that okay to create your database as such for your project so basically either way with your database in place it tells Jango how to use it okay okay this is where your settings.py files comes in so you in your setting.py file is where you do all the configuration regarding that particular database or you know uh your web server information all these things go into your setting.py file so you've got your databases so databases will tell you the default you know or you know your standby or your slave or whatever it might be so the default database you typically have a SQL light so you give the name of the engine database DB backend SQL 3 because SQL 3 is your particular database and you direct it to a particular now base directory name so base Di has to be also mentioned over here and db. sq3 is your actual database okay it is going to be the name of your particular database okay similarly there might come another question as to where do you you know start your where do you have information regarding starting your web server or anything like that so that comes under something called as manage.py you've got another file called manage.py so be sure about setting.py and manage.py are you all clear on that all right now how do you write a view in Jango so we talked about templates we I also showed you a simple template which we have okay how do you write a view okay A View is similar to a controller which pulls up information from your model and your template and serves it to the user an example is like this so you you have a you know views.py file where you put from Jango HTTP import htttp response because you are going to give out an HTTP response import date time so what this particular view does is that it Returns the current date and time as an HTML document so basically you'll create you'll get an HTML respond an HTML file which shows the current date and time so it'll contain it is now blah blah blah okay say it is now I don't know what time it is so somewhere around 11:10 it'll give you an output say it is now 11:10 a.m. or whatever it may be because it pulls the information from date time. date time. now puts it in the HTML and replaces this percentage s by that information and returns you the HTML response now you might be thinking what if I have template files available so in that particular case you have to first use a loader to load in the template by doing a get template okay and then you have to serve the response over here by rendering the template okay are you all clear okay so you have you know getting the template rendering the template serving your responses and how to write a view in Jango so what does uh Jango template consist of so it is a simple text file as I showed you it can be of XML type CSV type HTML type and you know a number of other types as well it can contain variables like I just showed you earlier my title my body it can be replaced by various values when the template is evaluated and the tags as well you can change the tag information that controls the logic of the template so you've got your database mod you know which is controlled by the models.py file you've got your. py file you've got separate template folder which contains the various templates and you have your url or you know your url views which is your particular so basically you create your root these are all your roots which are your URL and you pull up your you know this URL route through the browser and it will render the template and give it back to your browser you know this is the workflow of a particular you know HTTP request as such but what does a template contain XML CSV and HTML as well are you all clear now this is a question which comes up explain the use of session in Django framework okay now it is possible that you might get a question like okay so I have a particular user who's logging in at say you know 11:15 okay and uh he probably keeps himself logged in okay and he leaves the particular place okay and after like another two days he comes back and he he sees that you know he still logged in okay so how do I you know this is another way of doing the same thing regarding session how do I see to it that the user logs out after a particular time so that is where you make use of something known as sessions in Jango okay you know Jango provides a session that will allow you to store and retrieve data as per site visitor basis so you know you will create a random session it could be a random keyword as such which would have you know the session data for example it might contain cookies it will contain session ID cookie so so that you know the uh it will be put from the client side push to the client it will serve on the server side so in case your session cookie has a timeout it will tell after two hours you're supposed to log out by default so even if the person comes after two days and tries to enter that without a cookie is a session okay you know it will tell that you have been logged out please you know log in again that is where session IDs and cookies come into picture okay so this is where good for security as such okay it is very good for security that way you are not pulling data from the server and storing it on your client site are you all clear what a session is so it is a certain you know uh basically an ID which allows you to keep your users in check now what are the different types of inheritance Styles in Jango okay so there are three possible inheritance Styles so you've got abstract based classes also known as ABC you've got multiple table inheritance and you've got prop model so what is an ABC so the style is used when you want parents class to hold information that you do not want to type out for each child model so basically you will have a particular parent class okay and you would want the you know parent class to hold information and you do not want to create it for each and every child class as such that is where you use an ABC as such so ABC is also used to create particular classes okay which are not complete completely you know what do you say implemented as such it is a better approach than you know just creating empty classes as such exactly you cannot instantiate an ABC in python as well so you can you know you have your child classes derived from ABC because that is because it is an abstract based class and you will not be able to you know what do you say instantiate a based class as you know abstract class as such okay it has to come from the child classes which are created from that okay similarly you've got multiable in inheritance so you've got an existing model and you want to you know create a model based on you know that existing table okay another particular model based on the existing table so you don't have to write each and everything down you can inherit from multiple table as such okay you want to say you know you have a class called book you've got an article you've got two different classes you do not want to create a particular class which has got all information regarding book and AR from scratch instead you can just derive from those particular classes as such you can directly derive from those particular classes but please remember one thing you have multiple table inheritance so by default so it allows you to override basically existing functions as such but in multiable inheritance it does not allow you to do that okay are you all clear on that now what are proxy models now proxy models so you can use this model if you want to modify the python level Behavior without changing the models field so you can create propes where you are making changes to the actual database via an existing you know what do you say a proxy of the existing class so you create a particular model so model defines a particular table now you want to make changes to the table okay you have different behaviors for that table you can just create proxy models for that by doing proxy models you can have multiple behaviors without changing the actual behavior of the you know base model itself that is where proxy models come into picture now what is contained in a field class so as simple as that what is contained in a field Class A J field class so it will contain the database column type if it is integer string we care or anything like that the default HTML widget Avail while rendering a form field whether it's a text box whether it's uh you know uh whether it's a particular you know uh email you've got an email text text field something like that you've got different types of field classes okay you've got a password field anything like this so that is the default HTML widget and the minimum validation requirements so number of characters which you can enter number of lines which it can which can be in the text box all of these come in the field class please remember this minimum validation requirements default HTML widget and and the database column type okay coming to the next section that is web scraping using python these questions so how do you save an image locally using python whose URL address you already know okay so there are various ways of doing it so this will work directly if you're using from you know your local so your local it would actually work directly so typically what I would do is something like this Pyon for requests my URL is equal to www HTTP Google from this requests get URL okay so you see that you know you are getting response R is equal to request.get uh let's do this with open open of image.png comma WB as f f Do Right sorry about that it should be let see it read right this sorry sorry about that made a mistake in the syntax okay we need to do encoding also because it is not allowing me to do r. text it give you the encoding of the image okay so this is the encoding of the entire image we need to encode it and then write and then you would have your image ready you would be able to save your image as such okay to sa your image all right now how can I get the Google cach age of any URL okay how do I get the Google cach age of any URL let's do one thing using the following format so if you use webcast. Google user content so I'll open this up you can actually do this so your URL for example www.ed edu r.co so you'll be able to get this information so you'll be get the cash content this is the cash content okay which has been provided by your CDN okay so this is your cash content and you'll be able to get the cach content by doing this webcast. googleusercontent.com search so and so okay you can do this now you are required to scrape data from IMDb top to 50 movie Pages it should have Fields movie names year and rating how do you do this so this would probably come up as a coding question okay typically not as complex you might get a simple one so how do you approach this so you would have to get into imdb.com first okay and you know get the information first this particular thing are you all following me so far so you got imdb.com top know 250 movies okay so there are various ways of doing it okay but for this you have to start with beautiful soup on this beautiful soup from bs4 import beautiful soup beautiful soup is a passer so basically an HTML passing Library okay it's used for web scraping it's heavily used for web scraping import request request will get your you know information okay so URL is equal to so you have your url so soup so you create your response that is by using request.get URL so you'll see that you will have a response 200 which means to say you've got the response object which contains the text which contain contains all the information regarding that particular page okay and you need the text from that and you need to pass the text from that for passing it this is how you go about first open up the page inspect this page okay and check what you need so what you need is the fields movie name year and rating there are various ways of one such way is this particular thing soup. find children for movie in TR movie. find find is a command to get all the instances of this TD the TD class containing column name title column so class containing title column similarly here will be in title okay under your secondary info again you have you know rating column you've got a whole lot of information but that has to be first passed over here okay so typically I would come here go over here and I would first do a check on this so what I need is the TR the TR content you will see that you have got various TRS under list list uh just a second Romo let me you know let me just go through this I'll tell you what exactly beautiful soup is used for there so this is how you do it first first you get this list list okay you have a list so let's do soup is equal to beautiful soup so you've got soup is equal to beautiful soup right so you are going to do this soup is equal to beautiful soup of r. thxt so what is going to happen it is going to create an object called Beautiful soup out of the response that you have gotten response that the text part of it okay so if you check soup it would have pulled up all the information and created a soup of it are you following me so far okay now what you need is soup dot so your question was soup is equal to beautiful soup you need to find something so soup. beautiful soup this is what it does now we'll do this okay uh I want list list and list is equal to soup. find okay so do find class so you can do this something like this we don't want TD we want tbody okay class is list- list list so this is supposed to find me one single item which contains the class list list okay L list you'll see you it would have pulled up that information regarding the T body okay now the T body we need TR okay so we have got L list sub dot find all so TR my TRS are going to be s. find on TR let's check what we need under TR okay TR we just need the TR information okay we just need the TR information within the this okay TR not do find all so we want list. find okay I think this will not go in with that or you can use find Cap TRS okay it'll give me all the TRS in this all the TR information in this okay now under this we need specific information so this is how you go about it under this okay for TR n TRS okay print I'll just give you one example okay post column let's take this one for example okay expect you have something called title column okay class title column now you get to the point of title column print tr. find class Co find we want TD for class title follow so one by one we enter into this part put it within quotations okay so it fetches me all the title column information okay so let's refine this some more dot text okay it will give me all the title columns you see all the title columns here have been pull up this is how you pull all the information are you all clear on how to you know approach this particular problem okay next data analysis using now numpy is one of the modules which are heavily used in Python okay so how do you go about using this so you will get a particular question saying how to get indexes of n maximum values in a nump array okay so you have to use these particular things import numpy as NP AR is equal to np. array you are going to have you know a number of items please remember this 1 3 5 4 5 4 6 5 something like this okay now you can make use of AR sort there is an particular thing called AR sort ar. AR sort will give you the indices the indices for this particular thing okay in a proper order okay sorted indexes so what do you need the top three positions okay top three items so - 3 col so 2 4 and three of colon col minus one so 3 4 and 2 is the I know answer which I get over here okay what is the difference between the numpy array and a list so let me tell you this okay just give me one second so let me recreate this array 1 3 2 4 5 ar. sort of- 3 col of one okay 4 2 3 is the output that you want now what do you want so what do you want is the difference between numpy array and list so we'll go get to that just give me a second for numai as NP a is equal to np. array so we'll first do this particular how do you calculate a percentile with python so we'll go to this first okay and then we'll move on to that particular question we have got a couple of things something like this p is equal to or we'll just do this np. percentile np. percentile of a comma the percentile level the 50th per percentile would be this five okay so you can set return the 50th percentile so you can directly feed in the percentile level and you will get the information regarding the pertile level so this is a question which you asked Roma okay what are the differences between nump and lists okay so it is much more efficient because all the structures have been written in C with python you have vector and Matrix operations for free so basically something like this okay I have got L1 is equal to 1 2 3 something like this okay so L1 RR is equal to np. array of L1 okay so we'll create L2 as well okay now if you do L1 + L2 say you want to add items of L1 to L2 so 1 + 1 2 + 2 3 + 3 4 + 4 5 + 5 and 6 + 6 L1 plus L2 regularly would give you something like this but if you create an array with a nump array this is what would happen L1 ARR plus L2 ARR would give you 2 4 6 8 12 does it answer one of your questions uh Roma okay so this is one of the examples okay so you get a lot of vector and Matrix operations so that you know you have to you can avoid unnecessary work of recreating those Vector operations nump AR is faster you get a lot of builtin and builtin with nump ffts convolution searching linear algebra histograms Etc you've got a whole lot of stuff which is included so what about the list so lists are know general purpose containers effective in regular insertion deletion appending concatenation Etc and it is dynamic in nature okay but it does not support vectorized operations like element wise addition multiplication division all these particular operations are not supported which is what the advantage actually is does that answer your question Roma yes uh so Roma so numai cannot store different data types in a single array it is homogeneous when it comes to a single array but you know you can have arrays of gulans you can have arrays of strings you can have arrays of integer float Etc separately within a single array it has to be homogeneous okay now what is the difference between numpy and sipi you've got something called numpy you've got something called CI these are two different modules numpy is a much more raw module as such if I were to say so NPI would contain nothing but the array data type and the most basic operations like indexing Etc your numerical code is typically inside CPI okay but you would see that numai and CPI are quite compatible with each other because CPI is built on top of numpy so where CPI has retained all the what do you say operations of numai but is it's got a whole lot of its own operations for example nump contains linear algebra functions even though they are more properly utilized and they more properly belong to CPI you can contain integration differentiation all these operations are provided in CPI okay and it's much more easier CPI contains more fully featured versions of linear algebra modules so if you're going to do s scientific Computing it is advised to use both numai and CPI together okay which is a recommended thing so instead of using it independently you do not want so it it makes more you know sense of doing something like that okay how do I make 3D plots or visualizations using numpy or ccii so like 2D plotting you have 3D plots but you've got a basic you know 3D plot called mlot 3D sub package which is part of your M plot lip package okay there is another package called Maya VII which provides high quality 3D visualization features okay for 2D there is another uh beautiful charting uh Library called bouet it is based off of B.J B.J Library if you want simple charting just charting okay you've got nvd3 you've got different types of you know libraries which can be used you've got M emplo 3D sub package for 3D like you know 3D you've got for 2D you've got B which is dependent on the poke. JS Library you've got so with 3D you've got Maya VI as well you also got NV D3 which is part of the d3js library okay so you've got different beautiful charting uh apis which you can use okay now how do I find indexes of an array where some condition is true so you can simply do this np. array of you have an array of 1 2 3 4 5 6 7 8 9 print a greater than 3 you will find all the you know what do you say you you will automatically create a Boolean you know Boolean Matrix as such np. non Z of a greater than 3 will give you the indices so the output comes up this way so let me just take that up okay so a is equal to NP np. array of 1 2 3 4 5 6 comma 7 8 9 okay you do this print a greater than three and you get the Boolean array print np. nonzero of a greater than three all the nonzero numbers which are greater than three will be created as a nonzero index so you might be wondering what does this represent so if you look at this 1 1 1 1 2 2 2 1 and 22 are the actual indices okay these are the indices so this is 1 0 1 1 1 0 2 0 2 1 2 2 are you all clear how that comes into picture so these are the ones which are the indices as such okay so you see this array okay let me do this so you see what we wanted is for all the items greater than three so how it comes up is you'll get two separate tuples you have to take it you know parall with each other one Zer you see one Z index is for four 1 one is for five 1 2 is for 6 two is for 7 2 1 is for 8 and 22 is for 9 okay that is how it comes out now how do you rename column headers in Panda's data frame so let's do this okay import pandas as PD we'll take up this data this piece of data where we have a dictionary okay we have a dictionary which contains list so you've got these would be your column names Commander date and score would be your column names and these would be your entries Jason Molly whatever it may be okay data is so and so enter next you create a data frame out of the existing data with a certain index whatever in different locations okay P Maroma Etc so you create a data frame with a specific index so when you type DF you will see that you will get your values this way okay you get your values this way you want to rename your columns so DF do columns will give you all the column details that is Commander date score you just simply do this theer time score you do this and check for DF it would have changed column names this is how you change your column names in pandas are you all clear okay now what is a function used to iterate through values in a manner that one also retrieves the index so for index comma Row in DF do VOR rows print index row okay you will see that it gives you both the index as well as the column information you have the index coaches and you've got the column information leader time code leader time code are you all clear this is how it you can get the information okay so these are the various ways of doing it one other question so how do you convert how do you convert a list of dictionaries into a panda data frame so we had that question earlier and we did that know directly so you had uh wait let me go back data so you've got data over here which is a dictionary containing all these items so print PD do data frame of data so you'll get a data frame which is created just doing cre d. data frame okay that is how you simply create a data frame out of your dictionary okay now so there's a question here let's say that I have a you know data frame DF which is a know PD data frame of this particular type PD data frame okay so you see DF is like this and how can I subset based on a list of values okay list of values is something like this so it is a list containing values simply a list containing values we simply do y is equal to so and so okay let's do this one sec list is equal to DF of DF of a so we are know searching for a in list of values we are searching for 3 and six let's do one thing one second so DF of a will give you this we need in list of values okay DF of a for series is ambigous use a do item a. any or a. DF so we can do this think we have done small M take DF we can also do this forf of a s list of values so this is a little longer way for if items in if of a right items so you can do this but that does not give you the value okay so we have to you know uh maybe you know get it to a certain point where it is of a of items or the other way around so I need to look into to this there's a slight mistake in this particular question that's what I see okay so basically you would be able to you know subset it based on the values okay you can also do this okay DF DF off okay by doing DF of DF of a DOT is in 3A 6 so if you pass the subset directly so through this particular method it is a little bit difficult but it makes sense to use something like this DF of a is in 3 comma 6 it will give you whatever is there for that particular subset wherever there is three and six okay in a okay so if you change it to B you would probably get the value for only B subset for B are you all clear you can also do this okay now you've got a data frame which contains both you know what do you say you've got not number so you know that in outward particular this thing okay in a particular data frame we can have regular numbers as well as not a number if it is not been given or there are you know what do you say mixed up numbers or mixed up with strings and all that it takes it up as not a number okay in that missing numbers it takes up as not a number in that case how do you get only the ones which are not a number so in that particular case you just give this DF do is infinite is is finite of so so for example if we do it here so it will give you only the finite values this is just an example of what we had done earlier so I don't know if I can replicate this just give me a second let me see if we can replicate this particular thing
Info
Channel: edureka!
Views: 16,031
Rating: undefined out of 5
Keywords: yt:cc=on, python for beginners, python full course, python tutorial for beginners, python tutorial, python full course for beginners, python full course 2023, python programming full course, python, python course, python programming, best python course, full python course, python full tutorial, python 2023, python for beginners 2023, learn python, python training, python programming tutorial, python full course edureka, python edureka, edureka, Edureka python
Id: 0eE5ofiuNHs
Channel Id: undefined
Length: 715min 0sec (42900 seconds)
Published: Thu Aug 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.