Python Full Course - Learn Python in 12 Hours | Python for Beginners [Full Course - 2024] | 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 a channel we also have a lot of training programs in 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 to install it and start working with it and after that we can see some of the Core Concepts in Python like comments operators Tuple 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 object-oriented 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 matplotlib 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 head and get started with the first topic which is python fundamentals [Music] python has been ranked the number one programming language in Tob 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 to learn powerful programming language it has efficient high level data structures and is simple but effective approach to object-oriented 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 Drive the number one point will be the applications python is a very versatile programming language ranging from application development to web design game design UI ux development data science machine learning automation Etc there's also the point of simplified syntax we usually work with languages like Java C plus plus 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 well-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 to stress on is the highly supportive Community look I get it programming is not always easy and you get stuck sometimes but with Python's Community you get a lot of support for the issues that you face and I'm telling you is one of the most engaging and interactive communities that I've 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 get 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 Sublime Text Eclipse spider 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 ecosystem [Music] 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 are not able to find this link then just follow the URL given above anaconda.com products slash 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 are here just click download and Save now once downloaded just click on the file and open it up now once you're over 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 checkbox 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 Ides they will be able to recognize the python that is installed by anaconda in your system after all this you 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 Ides and code editors that you get with Anaconda 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 it's just one line all right simply type print all in small letters and then Open brackets round brackets and then single or double quotes and then type sub 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 got until here then congratulations because you just created your first python project [Music] thank you 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 the 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 body so in this statement what you're doing is you are assigning the value party to a variable name called age now if we run this again with the Run button or shift plus enter Then you can see in the left hand side that this 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 equals to 50 and I execute the statement then if I come back to a second statement just H you can see that the execution gives me the result of 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 ederica and you can see over here that I have surrounded it with quotes the reason why I do this is because whenever you write string values or sequence of characters python requires you to surround it with quotes 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 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 even 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 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 stir or 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 helpful in many situations for example if I want to say print age of x y z is plus h 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 off H this will convert age into string and will allow python to connect these two strings now if we 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 here of a particular individual and using that information I 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 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 here now obviously you will need to store this in a variable right so let's just save it under both here now if we run the statement python will ask you to enter the input for your birth your 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 here 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 typer unsupported operand types for INT and stir now let's try to break down this error let's check the date type of birther type 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 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 age equal to this expression and we'll execute this again and after that we can say print age of x y z plus h now if we execute this statement we again get an error this is again a type error and can concatenate strings with strings not end 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 25. [Music] 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 tattoo equal to python for beginners now after executing the statement I want you to remember a few things 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 lower over here and if you execute this you can see that it returns what's this means that a string is not completely in the lower case so you have another method to do that you can type title dot DOA and if you execute this now you can see that all the characters all the individual characters have been converted to the small case letters now if you type dot is lower continuing this then you can see that it returns true you also have other methods like title Dot Upper which returns all of the characters in uppercase letters you also have other interesting methods for strings in Python like title dot bind which will help you find a particular character or a sequence of characters in your string like say if I say Y in quotes then it will return the value 1. 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 one two three four five six seven 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 a sequence of characters like say four and you can see that it has written 7 over here if you go back over here you can see the F character starts at 7 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 par with poor and then you can see that the string has changed right now python for 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 final expression that we talked about earlier there is another replacement for for example you can type print bar 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 in the variable then obviously it will return true this kind of result is desirable in a lot of cases [Music] 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 plus 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 3 from 10 you have the multiply which is an asterisk which multiplies ten by 3 30 and then you have the division and technically there's two types of tuition 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 divided by 3 which is 1. so we also have the option to make powers or something for example if you want to say 10 to the power 3 then you can put in two asterisks and then if you execute it you will get the result of 10 to the power 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 equal to 10 x equal to X plus 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 are loading x two times right so a simpler way to do this would be to say x equal to plus equal to 3. now this new statement is exactly the same as the old statement only differences is a lot simpler to write and it's faster all right now let me ask you a question suppose I said x equal to 10 plus 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 operative 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 plus 10 which is 20. let's execute to verify this now operator precedence in Python also works the same way as it doesn't matter the only thing is that you can change this using brackets so for example if you want to execute 10 plus 5 first you can just put this in Brackets and then 10 plus 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 done [Music] 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 3 is greater than 2 or not this operator that we have over here is called the greater Lan 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 compare this with the assignment operator all right this thing is checking for equality it is checking if 3 is equal to 2 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 2 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 quality and not equal these operators combined make up the comparison operators [Music] 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 h less than 30. make note that the and word over here is The Logical operator so now we have H25 must be greater than 10 and H 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 to Pi and then if we say this and then if we come to the equation you have H greater than 10 which is false or you have age less than 30. now it is definitely less than 30 right so this equation must be true there we go the last thing is do 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 a is equal to 5 and age is greater than 10 so this is false right but if you used not operator over here the result will still be true now you can obviously invest 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 r operator where at least one side must be true and then you have not operator which will give you the inverse of your result [Music] 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 party 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 it's so if I put single quotes over here also then python will think that you are just making a mistake while writing so double quotes 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 Cola 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 note 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 alif and then you can come up with another statement say if temperature is less than 30 then print it's a 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 see it's a nice day and done great right now let me ask you a question what if I asked you to print the numbers one to five how would you go about by doing that obviously you say print one and then print 2 print 3 print 4 and then print by but I'm telling you that this approach is wrong the reason is because I asked you to just print from one to five but what if I asked you to print from 1 to 100 or 1 to 1000 million billion perhaps would you go about the same way writing it down individually obviously not right that's a very time 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 while 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 will type the while 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 1. so let's say I plus equal to 1. this will start incrementing the value of I by 1 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 1000 remember that if you want to increase readability of your numbers you can always separate it by an underscore so this is lag one comma zero zero zero so now we have the output 1 2 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 1 then this will be printed once and if I equal to 2 it will be printed twice I equal to 3 then it will be printed Thrice and so on okay earlier in the video I introduced you to the date 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 less a list is nothing but a group of objects let me show you how you can do this first you 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 Alton and then you have Sam Bob and M you can use either single quotes or double quotes 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 0 1 and 2 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 negative one so this is 0 and then a negative value would be the last element of your list so if you execute this you must get M and if you do a negative 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 0 and you will print all the elements until you reach 2. remember that if you execute this the last value like the ending parameter won't be printed so for example over here I have 0 1 and this 2 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 we execute that you will still get the same result early 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 a modifying an element Etc let us see how to do this let me first and 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 in 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 append is method that is used to add numbers to the end of the list so if I say append 6 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 6 at the end see there you go we also have another method called as insert now the problem with append 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 0 and the value would be 0. then if I execute the statement and execute it you can see that 0 has been added we have 0 1 2 3 4 5 and 6. 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 1 in numbers and then if you execute the statement you can see it gives you a Boolean value of true you also have other methods like numbers dot 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 [Music] 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 we 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 fault 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 colon and after this you have a separate code block and over here you can see print items now if we execute this you will have one two three four five all in separate rows you can also do this with while Loops but it will just be a bit longer let me show you you start with i equal to 0 because that's your initial iterator after which we'll say while I is less than length of numbers and then cool now if you're wondering what line of numbers is Len 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 files use for Loops for iterations [Music] now in this section of the video we will see at a function called range so range is 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 per number in range 5 print number and we execute this you will get number 0 to 4. note that the index starts from 0 so you won't get the written value over here but the written value minus 1. now we can also give two values over here say pi 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 1 over here right but if you see 2 then it will go from PI to 7 to 9 but execute this there you go Pi 7 and 9. foreign [Music] we will be looking at tuples now tuples are kind of like less but tuples are immutable what this means is that once you declare a tuple it can't be changed let us see how to do that we'll first start with the variable name as always let's say numbers equal to 1 2 and 3. now till now you've been using less right and when you do less you use a square bracket but with tuples 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 see numbers dot count three now you can see that numbers dot count has given you an output of two what this means is that 3 occurs twice in your Tuple 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 tuples well I agree that we will be working with lists 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 tuples [Music] what are comments no they are not something there just to increase the size of your code but guys they are indeed very meaningful comments a programmer current 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 comments actually increase the readability of the program not just to the programmer but to the others as well now that you know what our comments let's move on and see when to use them the best practice is to write comments as and how you proceed 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 our comments and their 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 the jupyter notebook and see how this works now let's move on towards the 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 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 come and start with a hash character in Python so here I'm going to write a comment 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 returned 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 comments whatever is present after your hash character is going to be omitted by your interpreter now what if the hash character was present inside the string let's go to our jupyter 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 ahead and see what are the types of comments 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 comments 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 the Jupiter notebook and write some single line comments 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 you are 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 comments 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 now let's get back to our presentation and see how multi-line comments work like I've already told you multi-line comments 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 a 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 Ctrl 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 Ctrl 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 a hash character in a single goal 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 dog strings are not actually comments but they are documentation strings these strings are written within triple quotes they are not assigned to any variable and many a times they are used as comments as well now you'd be thinking what is the difference between doc strings and comments doc strings are not omitted by The Interpreter unlike comments 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 dog 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 doc strings and comments now let's go to a Jupiter notebook and see their functionality like I've told you before dark 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 comment now these doc strings are efficiently used when you want to describe something about a class or a function or something else now these doc strings can either appear in your output or they can be omitted based on where you are 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 dock string has not been returned whereas when I use the dock string individually without any code and I executed it you could see that it had returned the string itself but when I include the dark string before the code and 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 dark 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 a times these strings are called rather than writing it again and again so I hope you've understood what are comments and docs rings as well [Music] foreign 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 operands now you must be wondering what is an operand so the value that we use in an operation are known as operands these operands can be variables or any data type that we have in Python so let's take an example here so we have two operands operand one and operand 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 equations 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 Tuple and asset 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 alphabets we can use the single audible quotes 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 2 it is going to give me the value at the index number 2 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 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 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 one 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 Tuple Tuple is immutable in nature which means you cannot change value in a tuple once you have declared and we use the round brackets to declare a tuple 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 Tuple over here I'm going to get these values that I have declared in the Tuple so the difference between a tuple and a set is not index and it does not contain any duplicate values but a tuple 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 edureka 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 flow 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 let's 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 will 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 am 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 flow 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 value hundred now when I write this X Plus is equal to 10 it is going to mean the same as X is equal to X Plus 10. similarly we can write x minus 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 to 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 5 over here it is not showing any error which means I have assigned 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 5 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 Phi so we have 5 over here and we have added 5 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 am getting the exponentiation to the value of 5 since a is equal to 10 and we are doing the exponentiation until 5 digits so we are getting the output as hundred thousand so this was all about 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 compare origin 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 am going to write the statement as just I have written over here x is equal to Y so when I am 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 statements 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 else 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 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 else 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 statements now that we are done discussing conditional statements let's talk about logical operators so I have told you logical operators are used to combine 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 jupyter 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 what happens if I execute this statement I am getting false because one of these statements is false now when I write instead of 10 if I write 8 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 or 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 am getting true as well so one of the statements has to be true when we are using the or operator now are 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 am 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 our 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 jupyter Notebook so what I'll do is I'll take let's say a list I'll give it of 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 1 is list two even though when we have used the same values in list one and list 2 let's see what the output will be it is false because these are the not the same objects right similarly I can use list one is not list two so here I am going to get the output as true because list 1 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 1 in list 2 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 am 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 2. 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 am getting true because this sequence is actually present in the object now to understand the difference between is 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 Opera editors guys so this is actually what happens when we are using these operators so let's try to understand bitwise and operator first in jupyter 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 1 it is going to be 1. so in this case only first bit is 1 over here so the output will be one zero zero zero and if we convert this to decimal it is going to be the number eight 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 1 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 one zero one zero 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 2. 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 am 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 the value as one zero one zero zero zero 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 are 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 are using the bitwise and operator it will set each bit to 1 if both bits are one as I've shown you and then before bitwise or operator it is going to be one if one of the bits is one so similarly for xor it will set each bit to 1 if only one of the 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 guys [Music] let's understand what exactly is a tuple in Python apple 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 tuple 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 tuple in Python so let's take it up to jupyter notebook guys as you can see now we are in the Jupiter notebook and I have a file name Tuple in Python over here so I'll just show you how you can declare a tuple 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 one two three four and five because this is a very basic example guys and when I print a over here this is my Tuple 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 tuple 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 tuple in Python so this is how we can create a tuple in Python let's take a look at the next topic that we have which is accessing elements in a tuple so accessing items in a tuple 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 tuple with five natural numbers that have one two three four and five so the indexing will start at the value 0 where 1 will be stored and it will go until the end of the Tuple that is the number five and the index value at number 5 will be 4. so in Python we can use the negative indexing as well to access elements in a tuple 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 adopt so we have a over here so inside the square brackets I'll just specify the index number let's say 4 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 the range because we have only three values over here so I'll be getting to this also like reassigning the values to a tuple so I'll just write two now I'm getting the value 30 over here because in my Tuple that is the variable a we have values 10 20 and 30. so the index number at 10 will be 0. so similarly at the element number 30 will have the index value as 2 so we're getting this output now talking about negative indexing so if I write minus 1 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 1 will be at 30 and for 20 it will be minus 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 tuple 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 tuple 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 0 and then I want to go until the end of the Tuple so when I print this I'm getting all the values inside the topple so in this Tuple the ending index is basically eight so I'll just write 8 over here and we are getting the values until the index number eight so you can see that number nine is not included here so when you 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 write minus eight 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 tuple so even though tuples are immutable in nature but a nested object in a double can be changed or in general a tuple 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 nested objects let's say I have a tuple 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 Tuple when I print a metabol looks like this so the first three elements in the Tuple are 10 20 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 topple so I'll go to the third index which is the list and inside the list I want to change the value that is 52 let's say 55 so I'll just specify 1 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 Tuple 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 uh item inside or trouble but for these objects the nested 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 tuples so it might sound a little overwhelming but joining two tuples is a very easy task so you just have to assign the addition of the two tuples to another variable and it will return the concatenated Tuple with the values of both doubles so let's say you have a tuple with the variable a and then you have another variable B with another Tuple 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 tuple 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 Tuple so I have actually concatenated both the tuples inside one Tuple 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 table so being an immutable data type a tuple in Python does not allow any changes and you cannot even remove an element from Tuple after the Declaration but there is a keyword which is Del and which will delete the whole Tuple all together so I'll just show you guys so we have a double C over here which is the concatenated Tuple 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 Tuple over here so this is how deletion works with the Tuple guys you cannot delete just one element from a tuple instead you can delete the whole Tuple all together so let's just go to the next topic which is Tuple methods so there are not many methods like list or you know other data types that we have in Python there are basically account 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 tuple 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 with apple and let's just say two three four and five no errors now when I print my eight here I have this double now to show you how you can use these methods you just write a DOT count and inside this I'll just write one and it will return the number of times one has been written inside the Tuple 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 5 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 DOT index I already know that one is written multiple times inside double I'll just write one over here so it's going to return 0 because it will return the index value of the first item that is being encountered even though we have the count as 6 for the element 1 it will give the index value as 0 only so this is all about the Tuple methods let's take a look at the next topic that we have which is list versus double so I'll give you a few different points so first of all a list is used for homogeneous data types and a tuple is generally used for heterogeneous data types a list is mutable in nature and Tuple is actually immutable in nature which helps in Faster iteration as well so Tuple is basically faster when it comes to iteration like you are working on for Loops or any other Loops for iteration statements so if you are using any iteration statements Tuple is much faster than a list and a list does not have any immutable elements but immutable elements can be used as a key for a dictionary while we are using a tuple last but not least we have no guarantee that the data is right protected inside a list but if you are using a tuple implementing a double with data that does not change guarantees that it is write protected so these are a few basic differences between list and Tuple so let's take a look at the next topic that we have which is the Tuple Constructor it is possible to create a tuple using a tuple Constructor as well I have shown you the two different approaches you can follow to create at Apple but there is a double Constructor which we can use to actually create a tuple so let's take it up to jupyter notebook again I'll show you how you can use the Tuple in Constructor okay let's just say we have a list guys and I have these values one two three so this is my list and when I print my list over here I have the values one two and three so using this Tuple Constructor I can actually change the list into a tuple guys so when I run this no errors now when I print B over here it will have all the elements inside the list but this time it's a tuple so we can actually use a tuple Constructor to change a data type for type casting and it can be used to create droplet as well so this is all about Tuple Constructor let's 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 tuple and a membership test on a double so let's take it up to jupyter notebook first of all I'll iterate through a tuple so let's just say I have a tuple guys with a value until nine and let's say for I in a a print I so this is how you can iterate through a tuple 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 tuple or not so the following example is going to show you how you can check if an element is present in a tuple 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 a tuple like we have used the list they drive as well [Music] what is a set a set is basically a data structure guys which 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 onsets 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 instructor now let's move on towards the jupyter 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 are clear with how to create sets now let me just show you all how to 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 are clear with how to create sets in Python now let me just show you all what are all the methods you can use along with sets to do so you can use the dir method and Supply the name of the set as a parameter to it when I used dir 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 will 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 passed 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 7 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 Jupiter notebook and show you all how to access set elements I'll reprint myself 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 3 again and 3 has been added just once and it's not been repeated again this is because sets contain unique elements and there is 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 DOT remove 6 which is not present in the set a it has thrown and key error and it's specified that 6 is the key which is not present in the set a so I hope you have understood how to use the remove method now let's use the discard method and see what happens when I used B dot 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 dot discard 90 it has executed but it has not thrown any error so I hope you are clear with both these methods now let's try to use the pop method when I used a DOT 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 offsets 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 a jupyter notebook and see how this works I'll reprint my sets ABC 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 ways One is using the Ampersand 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 Ampersand 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 4 and ABC 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 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 minus 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 dot add 3 it has returned an attribute error which says Frozen set object has no attribute as 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 Python 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 not that we have understood what a dictionary is in Python and why we use it and how it is different from our 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 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 a 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 eduraco 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 and 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 4 because we don't have 4 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 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've 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 our 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 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 edureka 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 is 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 will 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 a Eureka 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 tuple of each key value pair guys so let's run this so when I run this I'm getting couple 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 tuple 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 one two three 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 values over here as you can see I'm getting the Tuple 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 1. now when I print this I'm getting edureka 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 it 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 pycharm again so I'll show you what a 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 a Eureka now I can give another key value let's say 2 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 DOT 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 values so as we don't have any key one key to inside this so we're not we are getting the none value over here so this is what from keys does inside a dictionary guys so now that we have actually learned what is the 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 pycharm guys so okay let's exit the presentation mode so over here I have the value or the dictionary that I've implemented so let's check it up to when 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 alias 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 an estate dictionaries so first key is batsman inside the batsman key I have all those batsman like Roy Sharma Secret Garden and Virat Kohli 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 all rounder and we have Wicket Keepers and Bowlers as well so this is my dictionary guys 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 Shikhar Dhawan and Virat Kohli so as you can see we have average highest score matches and runs over here so for Virat Kohli we have more than 10 000 runs Chicago one has five thousand runs and Roy 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 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 bhuvaneshwar 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 just piece brumra or for all rounder I want hardik pandya all these statements I can use it for getting all those values inside a data frame [Music] 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 are the things you need you need the salary of those employees the bonus then 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 in total and you are going to write that logic for all the employees that are there in your office there 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 that 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 your first employee when the loop runs for the second time it will print the salary of the second employee similarly it'll 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 gets 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 exactly 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 will go on and execute the conditional code that is here this is nothing but the statements that are present inside the loop so it will execute that and again it'll go back and check the condition so if the condition is still true then again it will execute the conditional code of 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 will 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 infinite Loops what happened 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'll 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 post test Loops what happened the controls will first enter the loop and then in the end it will actually check 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 stress 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 Vibe 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 will execute the body of the loop and then again it will go 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 the Syntax 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 while loop basically I've told you earlier as well while 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 while Loops now let me show you that with the help of an example I'll open my pycharm and I'll give you a very basic example of using while 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 while loop in which I am going to print the integer values between 0 to 9. that won't include 9 so it will print 0 1 2 3 till 9 but it won't include 9. so let me open my pycharm and show you how to do that now this is my pycharm 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 a task count and I'm going to initialize value to it that is 0. 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 9 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 plus one 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 have defined a variable and I've initialized the value 0 to it after that I have used a while loop in which I have given a condition that says that count should always be less than 9 because I want to print values between 0 10 lines 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 9. the moment it becomes greater than 9 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 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 the 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 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 while 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 while 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 while loop is a perfect solution to this problem so without wasting any time I'll actually open my pycharm 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 have taken one more variable and I've named it as to be guessed and over here it should be an integer types of written int 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 will be generated it will be generated with the help of random module that we have imported so it'll be n random dot random plus one this will actually generate a random number between 0 to 20. so after that what happens I have defined one more variable guess and I have assigned a value to it or initialize the value to it 0. all right so now comes the while loop now in while 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 get again now if that guessed number or the number that you are guessing is greater than 0 then again there will 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 is 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 are giving up so it will break it will 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 guessed 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 while 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 and 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 opposed 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 while loop and it has printed congratulations you made it so we'll move forward and see four Loops in Python so for Loop is basically used when you know the number of iterations that are required and like while 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 the for Loop 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 syntax as 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 will execute the statement go back again and then from the range it will pick up the next item again it will execute the statement again go back and then from the range it will 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 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 pycharm 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 variable 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 will iterate only Thrice first per mango then for grapes then for Apple so this is the difference between the vial and for Loop in for loop I know that will 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 root first is mango then comes grapes then comes apple and then finely 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 required because if you see if I'm calculating the factorial of 5 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 while loop and where we should use for Loop so let me go back to my pycharm 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 the value to it which is 1. now I have defined a condition if the number that I've entered is less than 0 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 0 then factorial is equal to 1. and then else condition says that when number is greater than 0 then I've used a for Loop which says that for I have defined a variable I in range 1 to num Plus 1. when I write num plus 1 the range will be between 1 to num plus one but it won't include num plus one so it will only be including one till num all right so after that would have written factorial is equal to factorial into I so factorial was 1 earlier then it will multiply by I I will also be 1 in the beginning and then again it will go back and it will become 2 similarly it will keep on increasing until it becomes numb so till that time it'll keep on executing the loop and the moment it becomes equal to num it will 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 3 which is 6 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 pins 24 which is absolutely correct so we'll move forward and we'll see what our nested Loops in Python so python basically allows us to use a loop inside and another loop for example you can use a for Loop inside a for Loop or you can use a while loop inside a while loop and at the same time you can use a while 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 am 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 deposit some money and you can even if you don't want to do anything you can tell the machine to return your car as well so let me show you how it is done I'll open my pycharm once more so this is the code that I've 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 of assigned a value that is three and this is the balance that I'm going to initialize that is 67.14 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 I'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 one two three four then it will 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 Tuple that is the value of restart should not be n capital no small no or capital N so it should not lie within this particular sequence that is n capital no no and N but we know that the restart value is y so it will enter the while loop now over here what will happen it'll give you four options first option is in order to check your balance for that you need to press one to make a withdrawal you need to press 2 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 Tuple then it will break and it'll print a thank you statement but if you want to continue or you want to go back then you can press anything else apart from small n no 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 will give restart y because you need to enter the amount once more so again it will go back and it'll give you all four options so you can choose that withdrawal option again and you can choose a number which is present in this particular list now when you choose withdrawal equal to equal to 1 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'll ask you for restart if you press anything apart from this particular Tuple then it will go back to the while statement and it will 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 one two three four 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 will decrease your chance and it'll make it as two again if you write the incorrect password then again it will make it as trans is equals to Chance is 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 will 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 one two three four and give me four options which is one for balance two for withdrawal three for pain 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 67.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'll break and if I press anything apart from small n capital no small no or capital N it'll actually go back and ask give 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.14 to 57.14 so would you like to go back so I'll again press Y and it will give me all four options if I want to paint 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.14 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 one incorrect pin if I write that pin again incorrect again incorrect so we have no more tries it'll come out since I've exceeded the chances that I was given so I was given three chances I couldn't write the correct pin in those three chances so it'll come out of the loop and it will stop so this is an example of nested while 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 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 plus b square is equals to C Square so at that time it will print all the Pythagorean numbers that are possible between 1 to 20 three four five you can see that three Square Four Square is equals to 5 Square 9 plus 16 is equals to 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 Pie charm and I'll explain you how you can do that so this is the example of uh 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 am going to import the square root function after that last 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 will print the Pythagorean numbers between 1 to n so whatever the number n that I'm going to put in or give an input after that it'll run a for Loop and it'll say that for a which is a variable in range 1 to n plus 1 that means 1 to n that means 1 2 N plus 1 but don't include n Plus 1. after that there is one more variable B which is in the range a comma n that will not include n it will only include till n minus 1. so the square root C Square I've defined one more variable C square is equal to a exponent 2 or a to the power of 2 similarly B raised to the power of 2. 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 Square so if that c square is minus C square is equal to equal to 0 then print ABC it's pretty simple logic guys okay I'll explain it once more Neil so this is an example of nested Port 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 have given it as 20 so it will print the Pythagorean numbers between 1 to 20. over here also it will print the Pythagorean numbers between 1 to n so whatever the value of Nu input that will be depending upon it so the particular numbers will be depending upon the values that you input so it'll be printing the python 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 2 N plus 1 that will not include n plus 1 guys it will only include n so 1 till n plus 1 but don't include n plus 1 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 till only n minus 1. I have defined one more variable here which is equal to C underscore square that is equal to a square plus P Square now after that I've defined one more variable C and this will be actually the integer of the square root of the C underscore Square all right so whatever value that you will 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 C underscore Square minus C square 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 to 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 5 and it'll give me all the pi ticket numbers present between 1 to 5. so we have only one Pythagorean number that is three four five in that range now I'll execute this once more and I'll type in 10 here so we have two Pythagorean numbers there are three four five and six eight ten 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 as 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 while 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 while loop which says that while the condition is traveling equal to equal to yes that means if you are 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 1 to num plus 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 that again it will execute the loop again It'll ask you the number of people traveling in all these details if you write in anything apart from yes that means you have not forgotten anyone and it will come out of the loop so it is a very small example but a very good example to understand how a 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 saurabh ages 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 in geography ages 98 male or female female and yeah petal 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's 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 traveling so I would just say Siddharth whose age is 77 and he's a male yeah again it has 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 loop [Music] 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 fahrenheit multiple number of times what would you do we first obviously start with the logic right so far on it is equal to nine by five into the particular Celsius number plus 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 Fahrenheit 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 your one Celsius value then definitely it is really easy right so let's say you have three thousand five thousand 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 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 in 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 course again similarly for the volume as well so if you have increased the volume or decrease 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 every one 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 straight forward right so it's basically a block of organized reusable code that is used to perform some similar tasks in our last case in television it was either others the volume or change the channel well coming back to python it can be called by its name when its 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 dev keyword guys depth 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 if you so we have def function underscore name and this function does 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 another it can be edureka 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 it is short for documentation string check this particular example out right so we have print grid dot underscore underscore doc underscore underscore 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 are 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 that 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 n number of built-in functions for almost all the users guys so for the sake of Simplicity in this tutorial I have considered the built-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 add 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 calculator so let's say 3 plus 5 is equal to 8 right so let's say a plus b equal to C you've written a 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 an example well the syntax is ABS of n here ABS is the keyword and N is the parameter path 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 are trying to do is we are trying to return the absolute value of a number so consider the example right so we have a complex number here x is equal to Absolute of 3 plus 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 minus seven what do you think is going to be the answer right so it's going to be 7 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 is the answer to x is equal to Absolute of minus 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 inputs 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 will be talking about right so it can be list it can be tuples 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 I treble you already know that 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 this is true why because everything is true this is the same case for lists tuples 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's quickly jump into the third one so this third function is a very interesting function guys so it is ASCII I am pretty sure many of you actually know what ASCII is right so ASCII actually stands for American Standard code for raw information interchange and all it does is it is a character encoding standard guys so it is basically used by computers to actually understand so we understand A to Z and one to thousand or one to million right so how can computers make sense of this information in their low-level language is exactly through ASCII so what does the ASCII function do in Python well the ASCII function actually returns a readable version of any of the objects here objects are strings tuples in list or anything right and the ASCII function will actually replace any non-ascii characters with Escape characters so if it's an ASCII character well and good if there is no ASCII character it likes the Escape that particular character guys so here's the syntax check it out we have ASCII of object again ASCII 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 tuple 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 ASCII and my name is thale the a here is spelled very differently and it does not have an ASCII value map to it right so if it was just stale then you would actually get the ASCII output well since here it is not a valid ASCII 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 will 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 have for you guys is Bool 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 pull 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 0 and if the object is none as well so all these cases are when it can be false but any other time it will actually return true so let me quickly walk you through the syntax pool of object bull 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 numbered or anything right so check out this particular example X is equal to Bool 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 Bool 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 are different languages C C plus plus or Java or something you realize that enumerator 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 tuple 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 I triple and the other one is thought what is iterable so we've already checked out what I treble objects are and then you 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 will be zero well check out this particular example right in front of you right so we have X is equal to apple banana cherry and Y is equal to enumerate of X so again the default value is 0. so python already Counts from 0 instead of one right so obviously Apple becomes zero banana becomes 1 and Cherry becomes 2 2 and you can actually play with all of these key mapping values and then operate on them later as well but at this point of time all you need to know is this python starts from 0 and the first object there first iterable 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 case 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 write a line and this actually aligns a space then we have is equal to so this base 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 Unicode D to decimal F to a fixed 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 X4 uppercase hex format n for number format person for percentage format but in this particular case check out this example if 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 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 ATT R again a keyword so what does this function do guys the get attr function actually Returns the value of the specified attribute from a specified object again looking at the syntax we have get attr we have the object attribute and the default parameter the object is compulsory 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 the 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 is equal to 36 country equal to Norway X is equal to get 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 idea of object as you can already tell ID is the keyword object as 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 i 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 every time but what is important to note 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 Lin 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 lenov 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 keyed 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 lend 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 are actually doing here so check out the syntax map is the keyword and we have two parameter meters which is function and I travel 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 eye trebles 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 func and then the parameter is n what we are 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 func and then we are 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 6 gallon vectors 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 I treble so 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 into n3i 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 I treble guys as simple as that but check out the example here for simplicity's sake we have X is equal to Min of 5 in 10 so it's as simply as saying what is the minimum of 5 and 10 so which value is minimum here 5 right so if I had to give you an another example what is the minimum of thousand and ten thousand so whatever is the minimum number is output right so in that case it was actually a thousand 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 Pow function pow 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 Z 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 two power three the answer is 8 correct so what is 4 power 3 again it is 64. if you actually required the modulus of this answer then that would become RZ parameter in this particular case since we haven't done on it for part 3 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 okay separator is actually an optional parameter and you specify how to separate the objects if there is more than one object present the default separator is actually double quotes and then we have of end well again another optional parameter so we use this to basically specify what to print at the end the default is actually slash n 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 you are 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 parameter of the file object is actually assist dot 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 are separators 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 you'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 ADTR right so we actually outputted a function and now we are 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 is equal to 36 country equal to Norway same as the previous example right so here we're using another syntax attr of person age and 40. all we're trying to do is instead of John having his H to be 36 change the age of John to 40. so go into the class person look for H 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 distorted function actually returns a sorted list of the specified I treble 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 I triple is definitely required because we need to know what sequence we need to sort either if it's a list dictionary or a tuple 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 told you and it definitely is a Boolean function so false will actually sort it to be ascending and true will actually sorted 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 solid 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 ascending 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 basis and the dictionary are actually optional so base is actually specifies the base class and the dictionary actually specifies the namespace 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 dict 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 you look at it it's a tuple 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 user defined 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 have actually looked at this when I previously told you but let me quickly tell it again so again Define my function define as the keyword my function is the name of the function so what what do I want this function to do I want it to print hello from a function so this is actually the body of the function and 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 ID request 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 formula 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're 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 fname is actually the parameter of us so a printing F name plus ref SNES so what we're trying to do we're trying to pass so this is our function body so next what we're actually trying to do is we're actually trying to print it so we have my underscore function we have provided three names there we have ml we have two bias 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 roughness 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 set 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 already provided the parameters 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 are returning 5 into X so basically as soon as we pass a number Phi is going to be multiplied to that number and we're going to get the output my function of 3 5 3 is a 15 15 so next we have five five fives are 25 25 is correct nine fives are 45 again we have 45 as the output so we could do 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 try underscore recursion we have a parameter K so if K is equal to 0 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 the recursive function is to take a pen a paper so to write down the values of K at every point of time and then 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 0 we print the result else we print it to be 0 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 6 in our case so if K is equal to 0 absolutely 6 plus try underscore recursion of 5 again come into the function is 5 greater than 0 correct again go back check if 4 is greater than 0 yes keep doing it until the result is 0 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 value is 15 6 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 use the 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 plus 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 Phi a gets the value to be 5 so since a gets the value to be 5 5 plus 10 becomes 15 and we're 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 is 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 B are 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 6 what does 5 6 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 print 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 five six and two 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 case 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 function which is called as my func I have one parameter n and then we are actually performing an other 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 you have defined 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 doubler of 11 pretty much simple 11 gets multiplied by 2 because 11 is is passed as a function into the my func 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 note 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 2 so 2 my tripler equal to my func of 3 so I want to triple it so multiply three times as soon as I print my doubler of 11 11 into 22 is the first output that I get print my tripler 11 into 333 that is the second output that I get so what I am 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 remains 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 guys [Music] what exactly are 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 a keyword that specifies what followers is anonymous now that you are aware of what these Lambda functions are let's move on further to see why they are actually used the main purpose of anonymous functions comes into picture when you need some function just once they are created wherever they are needed due to this reason python Lambda functions are known as throwaway functions they are 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 you 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 you 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's 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 star 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 4 or let me take it as 3 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 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 on to my jupyter 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 def 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 Plus 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 here is passing a value to the variable X and then I'm printing the value of x plus 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 U of 5. 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 it's a method like I already told you all the filter function needs it rebuilt 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 y'all 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 you all let me just specify the input as a and we'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 2 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 iterables 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-i-l-t-e-r so as you can see over here I've passed the elements one two three four five and six to this filter method and after checking if this expression is true for all the iterables it has returned 6 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 2 again finally as we all know if I divide all these elements by 3 only for 6 I'll get the output as 2. 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 are understanding what exactly it does it takes a 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 it reveals 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 enter 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 2 and I'll pass my list as the set of iterables 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 pi 3 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 2 so I have a true as the output now when I divide 2 by 3 again I'm not going to have the output as 2 and therefore I have true as the Boolean value for this similarly all the elements present in my list are divided by 3 and it's going to check if the condition is true therefore only for 6 is 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 jupyter notebook again and I'll create a heading this is going to be Lambda within reduce and I 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 radius function you'll have to import the func tools library or from that Library you will have to import the reduce function to do this I'll be using from Funk tools import reduce you can also directly import the func tools Library as import tools or you can also use from Fung 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 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 reduce 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 will 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 1 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 and I'll create a heading over here 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 plus 4y okay so if I want 3x plus 4y 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 3 star X plus 4 star y okay I have 3 into X plus 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 a degree 2. let me just give the heading as quadratic equation okay so now let me just try to solve the very famous a plus b the whole Square a plus b the whole Square so I'll just say part two okay now let me write a Lambda function that's going to return the value of a plus 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 plus 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 and B as 3 comma 4 and I'll hit run okay so as you all can see I found the value of a plus b the whole Square using the Lambda expressions foreign [Music] 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 0 and not from 1. therefore a of 0 holds 1 a of 1 holds 2 a of 2 holds 3 and similarly a of 99 holds 100 when the length of my array is n the index value will be n minus 1. it is always 1 less than the length of the array so I hope you are 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 cares 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 2 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 the list it will throw an error because naturally you cannot multiply calves and springs or divide them so I hope you are clear with the difference between these 2. 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 alias name this is the most widely and commonly used method as well in my demonstrations I am 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 jupyter 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 dot 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 have created an array so I hope you're clear with how to create arrays using the first method now let me open the 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 Alias 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 am 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 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 are 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 0 and not from one so therefore a of 0 holds one a of 1 holds two and so on this 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 minus 1 99 will be at a of minus 2 and similarly one will be at a of minus 100 so I hope you are clear with negative indexing now let's go to our Jupiter 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 H1 I will use one hash if I want H2 I will use 2 and so on here I've created a heading of H1 so I prefixed 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 2 my output is 3. let's see where 3 is present in my array 1 is present at a of 0 2 at a of 1 and 3 at a of 2. so therefore when I access a of 2 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 Priti says please try to access something using negative index values okay Priti let me show you that as well so here I've tried to access a of -2 a of minus 2 has returned 5. let's get back to our array a and see where 5 is present 6 is at a of minus 1 and 5 is at a of minus 2. therefore my output is 5. so pretty are you clear with this okay Priti 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 3 which is equal to the number of elements that are present in my array now let's move towards our 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 and integer value is returned which is 6. 6 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 element 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 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 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 2 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 2. now let's go to a 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 8 as the value that I want to insert in my array and 8 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 2 6 will be inserted as you can see in the output 6 has been inserted at a of 2. so I hope you are clear with how to add elements to an array now let's move on towards the next operation our next operation is removing 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'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 DOT pop of 2 it removes the element which is present at a of 2 which is 6 and it returns it as well let me try to use some negative index value for the pop function a DOT pop of -1 removes the last element that is present in my array which is 5. 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 it because there are two occurrences of 8 and let's see what the remove function 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 8 has been removed but the remote 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 am 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 are 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 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 an 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 on the screen I've created some random array a and I've sliced it from 0 to 3. 0 specifies from where fetching has to start and three specifies where it has to stop so it will start from 0 it will go on till 3 but it will not include the value which is present at 3. as you can see in the output I have a of 0 a of 1 and a of 2 that is present in my output now let's go to our Jupiter notebook and we will try to slice our arrays 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 5 so it will start from index number 0 it will go to 5 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 0 colon minus 2 contains all the values between 0 and -2 but it will not include the value which is present at -2 okay so I hope you're here with this topic just give me a quick confirmation before I move on okay Anil says yes Shashank has a question he asks what does colon colon -1 mean Shashank unlike what many of us think colon colon -1 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 -1 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 four and the while loops the for Loop iterates over the items of an array specified number of times whereas the while loop iterates until some condition is met or some condition is true when you're using the while 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 -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 10 plus 1. now let's try to use the while 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] now the question is who is a python developer so let me tell you that first of all a python developer does not um is a very subjective term I would say why because you know there are a lot of domains there are a lot of different types of job rules available in Python right so when I say python developer does not necessarily mean I'm talking about a software engineer does not necessarily mean I'm talking about a web developer or a mobile app developer right so a python developer is a very vast field I mean something which doesn't exist right because there are certain fields or subdomains in Python that you can you know come up with a job rule around that that's what I mean right so you know he can be a software developer a software engineer he can be developing web apps uh mobile apps it might be even into testing right because you can use Python with selenium then he can become a data analyst as well or he can be a data scientist machine learning engineer artificial intelligence engineer so there are different types of job rules available in the market next up we are going to focus on you know why become a python developer because everything exists for a reason right and let's try to figure out why one should even consider python as a programming language when you have Java JavaScript and other different programming languages and the best answer given to this question is by stack Overflow so have a look at the graph guys right I don't have to explain anything here because the red line the red curve that you are seeing is of python right so from 2012 till 2018 you can see that it's it's an exponential uh growth right uh this is it's a straight line I'm sorry right uh so yeah I mean it's growing pretty fast and I would also tell you one thing guys it always makes sense to learn something when you know there's a demand there's no you cannot you know surf when there are no Tides right so uh just to explain you that now python comes with really really cool features so let me just talk about those features first of all it is very very easy to learn so all the people who are actually scared of coding uh don't you don't have to be scared of python I'm I'm being really honest because it is very very easy to write code in Python the syntax is very easy right then tons of libraries guys if you are talking about machine learning web development uh numeric computation scientific computation whatever comes to your mind any type of application python gives you a very good Library around that whether it's computer vision anything you can think of right so there are a lot of libraries available uh with python that you can use for multiple purposes right the next is it is free and open source that I think a lot of you might already know right and the best part is it has a very active Community guys right high demand as I've just proved with the stack Overflow graph apart from that uh since python is not only used for one particular domain or one particular purpose so if You Learn Python you can go into any field right it's like you know a typical Indian household uh you know parents who tell their kids to go ahead and study engineering and then he can decide what he wants to do well something similar but uh this makes more sense right so uh these are the various features guys right and a few companies few Giants who are using uh Python and let me tell you guys each and every company out there in the market nowadays use Python right I've written down few big names so that you can actually relate Google NASA you know Firefox YouTube Netflix Facebook Dropbox Raspberry Pi IBM Walt Disney all of them are using Python and are very happy about the services and uh you know basically whatever python comes with next up we are going to talk about salary Trends this is uh something that you can see it on your screen but I would definitely tell you guys it is our average number and uh generally a data scientist is not going to earn uh you know eight lakh per annum uh eight lakh rupees per annum it will be definitely more than that it's just an average we have taken it from a pay scale it is definitely more than that I'm telling you that through my experience right and devops engineer also earns a lot more than what it is here apart from that there is also a start uh around the python developer salary in popular cities right uh uh so we have San Francisco which is paying somewhere around 137 800 389 US dollars to python developers which is pretty great if you ask me and I'm definitely sure it is way above than 150 000 in us as well how should I start how to go about becoming a python developer obviously everything starts from Basics right you have to understand the fundamentals to reach to a certain level so first of all install python go ahead type on Google install python right you'll go to their official website you'll find a lot of things out there I can actually show you that let me just open it so if you click on install python you'll be directed to this website download python here just select the version select the OS that you're using and you are good to go right choosing an ID is uh uh you know it varies from person to person I prefer pycharm you can also download python let me just take you to one more link and download pycharm you'll be directed to a jetbrains website and here we go so just go ahead uh click on the community version because the professional version is you know you have to pay some amount for that so go ahead click on the community version download it and it's very cool guys because it gives you auto complete features and a lot of other benefits as well you don't have to manually go and install the libraries using CMD you can just go ahead use Python and do it right choosing an ID is done a simple hello world program right so I've already installed python I have python I'm just gonna show you how easy it is to write code in Python right uh so let me just quickly open my buy charm and all I'll do here is print hello world all right I just have to click and select The Interpreter as well let me just go to the settings uh I'll just go to The Interpreter just have to select The Interpreter and we are good to go now let's go ahead and try executing again it is again saying that you haven't selected an interpreter I just did that's weird let's see yep I can see there is an interpreter so just waiting two minutes to process uh let's just wait for that and we'll go back to our slides till then and uh we'll continue so I'll tell you how to execute a simple hello world program just give me like five minutes uh after that let's see what we have next so we're going to talk about a little bit about the basics so obviously as I've told you earlier as well to become a python developer to become a data scientist or you know to go to a level in your uh in your career you have to start everything from Basics right so we have talked we all we already talked about how you can install python how you can install python ID which is python right now let's talk about what are the basics that you actually need to learn now people who are from uh programming background can easily understand this but uh people who are not uh let's just focus on them so variables and data types you need to understand how to declare variables how to uh what are the different types of variables that we can declare so there are numeric uh variables there can be uh you know list Tuple sets dictionaries all those data types you need to study there is a separate tutorial on all of these you can go ahead and check out uh that in the python playlist but for now let's focus here what I'm talking about then we have functions exceptions and input output operations so how your input output operations basically mean how you can open a file how you can delete a file how uh you can give our input to the uh programming language right if you want to input something the user wants to input some variable uh value right so input output operations plus how to open close files delete files add certain things append certain things in a file so all those things apart from that exception handling right what about one by zero of some some term like that comes which has no answer right so how will you handle those exceptions that will be covered in exception handling there are functions which are nothing but useful code blocks right so that you don't have to again and again write the same thing right if you have for example if you want to calculate factorial 10 times in a program you don't have to write the logic 10 times you can write a function and execute it 10 times just an example guys then Loops control statements and conditional statements which are very important right I am pretty sure you guys know what are Loops right if you have a set of you know if there's a logic that you want to repeat again and again and you have a condition defined then you can go ahead and use Loops for that right so there are different types of loop there is there is a while loop right there's a for Loop uh and uh yeah so there are nested Loops as well where inside one Loop you can Define one more Loop uh condition statement if LF if statement so if a particular condition is true execute this if it is not then execute the next statement so those kind of things are called conditional statements uh data structures then you have oops Concepts what uh oops Concepts basically you know your polymorphism encapsulation classes objects how to declare them what are Global variables uh you know all those things uh it's it's basically a very vast field oops and it is very very important and python is one of those programming languages which have object oriented as well as procedural Concepts right and file handling I've just told you in the beginning itself how to open close delete add append all those operations with a file right how you can do that with the help of python right so now let me just go ahead open my Python and again and I'll execute that hello world program so I think Kobe can execute it now uh just a second guys it is just indexing right now or how about I go ahead and create one more python file let us name it as test and I'll type here print hello world and here we go yep so with one line of code I can do this try doing that in Java so guys that was a very simple hello world uh program that I've written all right so now let's talk about the job rules which I'm pretty sure everyone is waiting for it as I was mentioning so there are software engineers and developers right these people might be writing apis for your web app right these people might be you know helping with the mobile application these might be mobile application developer web application developers right so basically what our desktop GUI developers or game developers all those people right so what sort of skills you know Industries look for for such a profile right so code python uh Basics right which I've just mentioned at the beginning where I was talking about data types variables functions oops Concepts file handling conditional statements Loops control statements all those things right apart from that you need to have basic knowledge about the web Frameworks right object relational mappers restful apis you need to understand all these terms guys I cannot go into detail of all of these terms but uh uh just to give you a knowledge basically you need to understand how the web works you should you should know the basics of web front and backend and you should be aware about of libraries such as Django and flask which are very famous python libraries for web application development right database knowledge is a must because where is your data anyway going after you know somebody does any sort of activity on a website it'll go to a database right so that database knowledge is very important for any developer out there in the market when I talk talk about script writing so scripting is a little bit different than programming right so scripting basically is um suppose I want to execute seven programs sequentially right so what I can do is I can write a script that can execute those seven programs in an order that I have defined right so it basically automates a lot of tasks right in this case I can write a script that will automatically take my data and you know create a schema and store in the database so there are a lot of other things that you can do with a particular script right so just wanted to tell you the difference between a scripting and programming and I hope you have I hope you have understood what you need to understand what you need to learn what are the skills required in order to become a developer whether you are a web application developer or any sort of developer right so let's move forward guys and let's see which is the job role we are going to focus on next uh so we're going to talk about a python web developer right we just spoke about it web developer uh like uh in the previous slide as well so here it's all about uh you know understanding the web HTML CSS Django flask MVC MBT architecture model view controller template so these are all things that you need to understand right uh um I mean these are a few things which you will know once you start going and researching in that particular field right database knowledge as I've told you it is a must for any sort of developer right script writing is also very important because without scripting guys there's no automation right any automation tool that you use now also uses script right so just to clear the doubt uh you know scripting is also very very important right uh now let's see which job role we are going to focus on next all right uh so a data scientist sexiest job of the 21st century right now what you need to learn to become a data scientist what skills are Industries looking for in a data scientist so you definitely require code uh skills right core python skills which I've just discussed mathematics and statistics is the core or you can say the foundation of a person who wants to become data scientist that is very very important these these fundamental topics are very very important guys and people generally ignore them right so basics of programming core python skills mathematics and statistics apart from that you also require uh skills like you know data visualization uh data wrangling you might have to clean the data before processing it right then there's a skill called Eda data exploratory data analysis where you just figure out you just look at the data understand the data what it is talking about what should I predict what I can't predict which rows are useful for me which columns are useless for me can I subtract a couple of columns and come up with a new column that might be more useful for me so these are the things that you know uh uh you should have that it only comes with experience and that's how it works apart from that a very important skill is machine learning and artificial intelligence so you need to be very uh you know you need to understand all the machine learning algorithms you need to understand where I can use which sort of a machine learning algorithm and if you want you might come up with a different algorithm as well so how you can achieve that with python so with python there are a lot of libraries that you can work on so there's a library called pandas which is you know for creating data frame so you can import any sort of data with the help of Partners whether it's a tsv file CSV file Excel file doesn't matter you can import that data you can perform basic operations joining concatenation merging any sort of operation then you have numpy because python does not support any thing called arrays there's no there's nothing called arrays right so there are numpy arrays that you need to create with the help of numpy package right so any sort of Matrix operation you can perform the help of numpy scikit learner is in the library used for machine learning so it has predefined methods for different algorithms whether it is KNN K means all those algorithms there there are predefined functions or methods in this particular library that you can use and very cool feature about this library is that it comes with sample data sets so you can practice those data sets you can you can practice those data sets the way you want I mean you you're getting data sets guys you don't have to download it what else you what else do you want right so let's move forward guys so uh now we're gonna talk about uh data analyst right so data analyst is someone who understands data and uh who understand a lot of things who understand the entire data life cycle uh who can take care of data wrangling who can take care of data exploratory analysis right all those things but they have only implemented simple linear regression models right there uh you know are not aware of those complex algorithms or or some neural networks or some complex machine learning algorithm which can be unsupervised for example K means so they build prediction models simple regression models you know sales forecasting and things like that uh they are very good with data manipulation and they have this skill which is very important and it is called as uh data driven problem solving approach so you need to understand you know what sort of approach should I take for this particular data and that only comes with experience right so uh that's how things work because um you know you cannot just apply one algorithm to all the data sets that are there in the market right you need to figure out what sort of a problem is it is it a clustering problem is it a you know all those things so basically I don't want to go into too much detail here but that's what a data analyst is right so let's see uh emerging job roles so Ai and ml engineer artificial intelligence or popularly also called as machine learning Engineers these people are not bothered about data pre-processing they get the good data they get the data where they have to predict something it might be a regression problem where they're predicting something it might be a clustering problem where they might be clustering different people uh right it might be uh a classification problem where they are classifying whether the given input is X or Y right so that's what the major job role of machine learning engineer or an AI engineer is so they require programming skills data science skills and machine learning right so that that is what basically an AIML engineer should have right uh apart from that neural networks is what people are talking about right now it is a buzzword in the industry and it takes machine Learning To The Next Step and it is called Deep learning I cannot go into detail here I hope you guys can understand but uh deep learning is very very interesting guys and there are a lot of libraries that python has for deep learning one is tensorflow right and it is a it is given to us by Google so I hope you guys trust Google right so you know it's it's a very good library and I find it pretty interesting uh I have separate tutorials on tensorflow as well right I have worked on tensorflow so you can go ahead and check out my videos uh next is python automation testing so what is python automation testing is basically you know you're using okay let's talk about selenium because I can see only selenium in the slide um so selenium uh you can write scripts in Python using selenium right uh you can perform functional testing uh basically you know testing login of any website testing any feature whether it's login whether it's search engine or or any sort of feature whether the link is clickable uh the button is working all sort of functional testing or you can say user acceptance testing can be done with the help of python in a very automated fashion next is a deep learning engineer so deep learning engineer focuses mainly on neural network so he'll need to understand different types of neural networks whether it's a recurrent neural network it can be a convolutional neural network it can be RBM Auto encoders uh you know fully connected networks or or single layer multi-layer perceptron so they should be they should have that thing where they can understand all these different types of networks and have good experience in working on applications such as video processing image processing computer vision um you know speech to text text to speech image recognition so how do you think that the Facebook auto tag feature works right so it works with the help of deep learning only right so a machine learning engineer and deep learning engineer python text automation engineer python automation Testing Engineer so I hope you guys have no uh doubts here now comes the roadmap guys okay so I have um you know I'll talk about each and each and every job role one by one so how you can Master Core python so what you need to understand you need to learn variables and data types file handling exception handling oops concept data structures iterators generators rights these type of things where basics of programming you need to understand right then well let's talk about master web Frameworks so Django or flask whatever you prefer right uh teak enter is basically for GUI based the web apps right uh then we have MBC MBT architecture that is basically a model view controller and template that you need to understand HTML CSS is a must for any web developer guys right it's it's and there's no rocket science here I think people should be aware about it that web developers have to learn HTM and CSS okay uh just a second towards data science if you want to go statistics applied math data visualization analysis basically any sort of data manipulation you need to understand that on top of it you need to understand machine learning and uh you have you know python libraries to make your life easy machine learning and artificial intelligence you need to be very good with the machine learning algorithms understand uh you know libraries like scikit-learn tensorflow probability of Statistics you should be very good with apart from that neural networks and deep learning if you're looking to become a neural network uh deep learning engineer right so deep learning again there is a neural network architecture that you need to understand so machine learning and deep learning there's a very thin line here but deep learning is nothing but an advanced machine learning right so don't get confused here uh both are the subsets of artificial intelligence right artificial intelligence is an umbrella term which encompasses machine learning and deep learning together so this is though I don't want you to guy I don't want you guys to uh be confused about it I mean it's very easy to understand machine learning the next step after machine learning is deep learning which uses neural network so it's nothing but Advanced machine learning an opencv for computer vision web scraping with bs4 predictions models Etc is what you should be knowing so opencv is basically a library which is used for computer vision apart from that web scraping is nothing but scraping data or collecting data from different uh websites web pages that you can easily do right and uh prediction models is is I think you guys are already aware of now [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 pycham guys and now I'm gonna 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 rename 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 enabled as child and for this in the Declaration I have to specify 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 not stand 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 operating 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 an 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 2 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 a 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 and it 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 f h now what I'll do is I'll just write self Dot name is equal to F name and self dot h 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 Dot name and self dot age so this is my parent function guys and for the definition in the child class I will also make one in a function so I'll just write it as in itself and again I have to add F name and FH and let's say SL Dot name is equal to F name and cell dot h is equal to FH now this unit function is going to override the variant 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'm on my last name as well and I'm going to add it as let's say edu breaker now I'm going to print over here self Dot h 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 a value let's say I want the H to be 23 and last name is going to be python 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 and Eureka and 20c so this is how you can override the parent class init method using the child class guys so this is all about the unit 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 in child class are going to be there inside the program following other types of inheritance in Python guys so there is single inheritance there is multiple inheritance then there is multi-level inheritance hierarchical inheritance and there is Hybrid inheritance as well so we are going to talk about each of them in detail guys so first of all let's talk about what is signal 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 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 gonna 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 hierarchical inheritance High radical inheritance actually involves multiple inheritance from the same pain in 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 pycharm guys and I'll remove all this so I will start with single inheritance guys so I'll just take a parent class and give it a function let's say Function One and give it a statement let's say this is Function One now let's take another class guys class child and give it the value of the painting 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 2. now make a child class object and call the function so we can call both of these functions so just called function one give it the output and we are 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 here 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 am 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 2 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 pair into class and pair into class is giving properties to the child class so this is example of multi-level inheritance guys now talking about hierarchical inheritance guys what I'm going to do is instead of pairing 2 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 too 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 pairing two as well all right and give it one and ob1 dot 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 hierarchical inheritance guys I am using the same parent class to give the properties to both parent 2 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 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 pair in number three give it a function function let's say four and print statement this is function four now instead of period 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 4 as well I'm able to do that so in the output let's say what I got I'm getting the function 1 and function 4. so this is a simple example of Hybrid inheritance guys now that we are 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 uh 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 over here I can just call the function one now I'll just print this is function 2. let's make a child class object now let me see if I can call the function 2 or function 1 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 are 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 riding 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 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 1 which should be ideally calling the bearing function but it is printing out the function which I have specified in the child class so this is how I have overwritten this parent class method and this is how we can perform method overriding in Python guys [Music] 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 0 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 fun 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 pycharm which is my IDE 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 ahead and type out some simple code for you guys so let us start with y equal to 10 we can actually print y by zero now so let me quickly go ahead and run this in the console so as you can check out it says 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's instruction so instead of the program actually executing evenly and nicely there is actually a disruption in between the curve 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 a very simple concept and I'm sure at the end of this 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 you have accept well accept this 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 try block has actually caused an exception then we can handle it in the accept 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 so I want you guys to note this at this point of time and this will help you a lot on the coming slides guys so enough coming back to python this is what it visually looks like we first have the try block where we run a piece of code and next we have the accept where the code actually runs only when there is an exception in the try block and if there are no exceptions then we run the else blog 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 we are familiar with the process 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'll 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 is zero division error and it says that the integer division or modular by 0 is not possible so this is basically the same case that I showed you in pycharm a while ago but instead of showing the message exception error python details what type of exception error was encountered well as I 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 are clear with that let's check out how we can go about raising an exception on our own so we can basically make use of race 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 grace 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 5 and the value of x was 10. so let me quickly jump back to Python and we can execute this there so guys I'm in my python right now so and 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 next should not exceed 5 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 our 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 this code runs only on Linux right so I'm using a Windows desktop in this particular case so let me quickly open up my Python and then we can actually run this piece of code to check out our exception guys so I'm back in my Python 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 assumption would be true if you are 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 assortion error is 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 Index app Block guys so basically the try and accept 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 accept statement is the program's response to any exception in the preceding try class 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 accept Clause determines how your program responds to these exception guys so instead of actually crashing our program we can actually tell python what 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 accept block well 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 Python so let me quickly open up that so guys I'm quickly here on my pycharm 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 present annotation so basically if we run the tiny piece of code you see on the bottom the way we actually handle 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 well to this end you can actually change the path into something that would generate an informative message like this check it out so let me quickly go ahead and run this in pycharm 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 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 through 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 try 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 such as 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 so 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 got 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 Python and we can actually run this and we can come back and take a look at the output so guys I actually came into Python and I actually ran that particular piece of code why does it say could not open file log well guys basically if the file.lock 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 to run it has 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 the 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 try block we're 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 python 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 try Clause actually guys and anticipate catching various exceptions as well well a thing to note here is that the code in the try Clause will actually stop as soon as an exception is encountered but there is 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 accept statements 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 you are basically you first call the Linux interaction function and then later actually try to open a file guys so let us quickly jump into Python 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 their output right so it 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 try cross 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 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's take a quick break and check out what are the key takeaways of this particular module guys so basically a try class is executed up until the point where the first exception is encountered and inside the accept 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 bear except Clauses guys and now that you're clear with this next up I have for you is the else class 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 gonna have an else clause and we're going to print saying executing the else Clause right so let me quickly open up my Python 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 are actually not printing the accept Clause here but if you were 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 class 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 class 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 file 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 caught 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're all familiar with try accept and else at this point of time right so this is the chronological order that you need to go into first you have the 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's 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 Python and we can actually execute this so guys I'm back into my pie charm 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 except 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 guys [Music] what is python requests 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 parameters in the URL and passing custom headers as well it is a human friendly 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 writes 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 unicode response bodies then we have browser Style sslr verification and https proxy support as well there is automatic content decoding and multi-part file uploads and then there is basic authentication and streaming downloads as well and last but not 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 where 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 up 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 request 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 are using pycharm 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 Kenneth writes 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 requests 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 the 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 requests 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 are going to make the request from so let's take it up to python guys I'll show you how you can make the cat 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 dot 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 the 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 requests are used for so basically post requests 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 will 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 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 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 it will be showing us the cookies inside our cookie jar yes we are getting the cookies inside the cookies are and we can also pass our own cookies I'll be telling you about that in later on in the session don't worry guys so then there is headers as well you'll see all these headers inside the 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 own cookies to the server you can just use the cookies parameter and cookies are written 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 pycharm guys so what we'll do is we'll just get some cookies here and when I run this we are getting a cookie jar 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 will be seeing the cookies over here which is inside the request cookies are 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 one is equal to value 1 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 named as key one 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 persists cookies across all requests made from the session instance and we'll use the URL Library 3 correction pooling and if you're making the several requests to the same host the underlying TCP connection will be reused which can result in a significant performance increase and also our session object has all the methods of the main request API as well so let's try to purchase some cookies across the request guys so we'll move to the pycharm again so we have come to the pycharm now what I'll do is I'll make one variable let's say session object that is so I'll use request dot session and after that I'll just use a get statement where I'll just use s dot get 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 dot 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 return 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 exception 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 and I will open the project that I had made so as you can see I have a Django file over here with all the init settings.py urls.py so I have over here the URL configurations and everything and I have a views.py file inside my app that 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 are wondering how you can actually make a web app I suggest you check out the edureka tutorial for Django 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 a 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 requests 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 guys [Music] 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 epoch the C time method Returns the current date and time by taking the elapsed seconds as its parameter sleep tops 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 epoch 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 passed since the epoch and finally the ASC time method which can also take up to 9 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 dot time and I'll hit run so like I've already told you all this method Returns the number of seconds that have passed since the epoch so I'll hit run okay so here is the number of seconds that have passed since the epoch 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 dot C time and I'll have to pass the number of seconds as a parameter 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 dot time and I'll hit run so as you all can see it says that this method Returns the current time in seconds since the epoch 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 dot 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 y'all understand what the struct time class is let me get back to my presentation okay so as you all can see the struct time class basically has nine attributes starting from the year whose value starts from zero and goes up to 9999 the second attribute is month whose value will be anything from 1 to 12. after that is MD 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 Year 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 1 if unknown okay now that you're aware of what this struct 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 I have passed since the epoch 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 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 epoch 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 struct 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 variables AC and then I'll use ASC time method so time dot 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 strf time method and the strp time method the strf time method can take a tuple containing nine parameters and returns a string representing the same depending on the format code used the strp method passes a string and returns it in the struct time format to show you all what these format codes are I'll just use the help method and I'll pass time dot strf time as a parameter to this and I hit run oh I've made a spelling mistake over here sorry guys it'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 struct time format so let me just print this out again okay so now I want to format this output in a manner which is convenient for me so to do this I'll use the strf method so I'll just save time I'll take another variable say x is equal to time dot strf time I'll use the 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 called 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 strp time method to do this I'll take another variable say why and I'll pass a string to this I'll say 8th August 2019. and then I'll make use of the sdrp method to pass this string so I'll just say time.strp 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 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 R minutes seconds microseconds and the time zone information the date time module also consists of a from 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 six date as 7 time as four 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 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 of year so I'll just say now and I 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 in the month all I can do is save this into a variable say V and then I can use V Dot year 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 Dot month and then I'll say print V Dot r and I'll hit run so as you can see the month and the r 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 dot 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 three 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 you 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 variables say B1 and I'll say date time dot time Delta and I'll pass the number of days as 20 to this you can also pass other parameters like microseconds 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 class [Music] 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 with irregular Expressions help us to solve so we'll move 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 our email addresses are correct and what all email addresses are fake ones so as you can see obviously scash.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 sales person 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 four numbers are correct and what all four 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 area and the area code of that area is 59006 but what happened the area code has been updated and it has become 59076 now if you go manually update that for all the students I'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 phpc hash groovy 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 expression 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 Janus is 22 and Theon is 33 Gabriel is 44 and Joe is 21. so as you can see what are 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 dictionary 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 it just give a guess you just need to identify the pattern 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 on to lower case 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 does 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 reg X 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 pycharm 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 Janus is 22. and Theon is 33. now the next line I'll write Gabriel is 44 so I forgot the space and Joey is 21. 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 re dot find all are one comma 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 gonna do you're gonna type in names equal to similar to that re dot find all r and my first letter is an uppercase letter that is between A to Z 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 age now what I'm going to do is I'm going to Define a variable and I'm going to name it as age dict 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 0. now I'm going to use a for Loop I'll type in 4. each name in names age date each name ages X plus 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 in 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 age 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 cursors so what happens is suppose you are searching for R and you're a regular expression includes R so your cursor is at R in the regular expression but for text it will check for B it is not equal to R then e then e then again when it comes to R it will see that this is what you are searching so it'll stop right there the moment you search for S it will go on and it will 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 Express 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 our 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 pie chart so this is my python guys let me just first clear it so let's begin by importing our regular expression modules for that I'll type import r e 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 re dot 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 form 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 form in this particular string that is we need to inform him with the latest information then it will 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 inform so let us go ahead and execute this so yeah you can see that it has printed there is info 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 Ari dot 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 in form 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 in 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 in form and from which string I'm finding in form this is a 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 in form twice because you can see that it is available here as well in form 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 pycharm 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 4 I in Audi dot 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 en now I want that to be converted into a tuple so for that I'm going to type in locked up equal to I dot 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 regex 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 path so now my question is to you all guys what is the pattern that you can see in this particular string that is sad hat 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 regex so first I'm going to open my pycharm let me remove all this so let me Define a string once more I'll type in Str ategic so we have sat matte heart Matt and we have bat 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 and now what I'm going to do is re dot final now what I want I want words that starts with S H M and P all right and after that it should end with r 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 so this bracket where I've written shmp it shows that I'm matching specifically for the words that starts with S H M 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 part but one thing to notice here guys it will not print sat why it is not printing sad 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 slide 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 Edge to M so all the words whose first letter comes in the range h2m should be printed and it should be ending with at so basically it should print hat and matte because P comes after M so does s so it won't print satin path now let's go ahead and see in a python whether this happens or not now over here what I'm going to do is I'm going to remove this s so does p throw in a hyphen here and execute this so yep it has printed hat and Mark so if I want all these things to be printed what I can do is I can write in here Z and execute this so yep it has printed heart matte and part but it has not printed such 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 h2z all S H M and P lies so it has printed all the range of characters whose first letter is present in the range h2z 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 when I use this carrot symbol this means everything apart from this range that is h2m go on and execute this and you can see it has printed sat and part but not hot and matte 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 heart rat Martin part but I want to replace this Rod I don't like that all right so jokes about I want to replace this rat and I'm going 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 [Music] now I will replace rat with food so now I'm going to compile our 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 regex for example yeah reg x equal to re dot compile and I want to compile R which ends with at and now in the string food I want to substitute it so I'll type in regex dot 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 heart Food Mart and pot so we have replaced rat with food so this is a 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 this one very important thing that I want to talk about it is called solving the backslash 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 around Str equal to here is Drogba 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 backslashes 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 dot search so this is re is basically we call it as a raw string so raw string will treat backslashes as special so let me show you how it will do I'll just type in re.search then are quotation Drogba comma and the name of the string that is random string go ahead and execute this so yep it has solved our problem and you can see that double backslash appears over here so this is what I'm talking about the backslash 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 one two three four five as much 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 python 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 key the blue flag flying hat 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 name it as regex equal to re dot 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 backslash n then I'm going to type in the random string equal to regex substituted with what what 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 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 backslash b that stands for what it stands for backspace then you have backslash F or form feed then you have backslash R for carries return similarly you have backslash t for tab and you have vertical tabs as well so for that we have backslash we 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 one two three four five and now what I'm going to type in I'm going to type in print matches I'm going to give a colon and then I'll type in length re dot find all backslash t in a random string now over here you can notice that we have backslash D so basically this backslash 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 backslash T and print this and show you what it will 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'll return me everything but the digits so let me just show you I've converted this to capitally go on and execute this so we have zero matches now so now if I want to match a specific digit I'll be using curly braces so let me show you how you're going to do it so in the curly braces just type in five and it should print that for us so yep 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 so let it be num over here I have a 103 so now one two three four then our one two three four five one two three four five six one two three four five six and seven now we can even match digits with a certain range let's say our range is between five to seven 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 dot find all what I'm searching for I'm searching for digits so backslash d and what is my range range is between five to seven give a comma and then type in the restring 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 assign 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 pycharm and verify these numbers with the help of regular expressions let me remove all this so before I start writing down the code let me explain you something so we have backslash W so basically what it does is it is same as writing down lowercase A to Z uppercase A to Z even numbers and underscore so lowercase w is actually equal to this thing that I've typed here so it will match anything that is inside the brackets when we talk about uppercase backslash W it'll 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 validating phone numbers so let me just make a string that has my phone number so I'll just type in as phone and I'll put here four one two triple five and say one two one two all right now I'm going to use the condition statement that says if re dot 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 as a phone number now basically instead of backslash W I can even use as backslash D because all of these are digits but let me just keep it as backslash W 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 five one let me show you it is not printing anything because so because our regular expression says that it will take only three digits now as I've told you earlier as well W Takes A to Z lowercase and uppercase as well so even if I replace it with d it won't matter let me show you go on execute this so yep 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 would have added here a as well it won't print that so yeah so basically I wanted to tell you what backslash W is that's why I've written it over here now let me tell you a use case of this backslash W 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 backslash w at that time so basically why I've explained you backslash W is this is how you can use backslash W 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 backslash s which is same as writing down backslash F that is form feed or a backslash n that is a new line or a backslash r that is a carriage return or a back backslash t 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 backslash R tab as well as vertical tabs so it'll include anything but backslash F backslash n backslash R backslash T and backslash V 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 re dot 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 backslash s and one more backslash W 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 carrot 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 a second use case is we need to verify the email addresses so our email addresses should have one to twenty lowercase and uppercase letters numbers plus period underscore percentage plus minus it should have an address symbol then again 2 to 20 lowercase and uppercase letters numbers plus period and 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 of this and let me first write a list of emails so you can write it as SK at the rate aol.com I can have MD address dot com address SEO dot com now I can have one more and I can name it as DC address.com let me just remove the space all right so now what I'll type in I'll type in print email matches give a colon and after this space I'm going to type in length re dot find all open the parentheses and over there and 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 the rate after other rate I want anything for that I'll type backslash w 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 to three email all right guys go ahead and execute this so it has found only one match so which is SK at the rate 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 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 where 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 create 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 is a website and on that website we have phone numbers we have 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 pycharm so guys I've already written the code so over here I've imported a couple of libraries in the beginning which are used to perform web scraping I mean they are used basically to read the web page then we have find all which we have discussed earlier as well now this is a 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 number now note is 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 let's keep this pattern in mind and we are going to write a code in order to scrape only the phone numbers from this particular website now 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 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 hyphen and then finally we have four digits then save it in HTML 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 well [Music] 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 user 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 command line 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 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 am 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 n number of opportunities again so now that we're good with this and we started off on a really high roll 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 this file 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 a file or anything but for simplicity's 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 floor 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 right mode the append mode and even the create mode as well pretty straightforward but do note 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 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 p 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 fine 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 of 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.txt 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 a term 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 let's we call it file and then we are 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 5 on your screen what do you think the five indicates here guys let me help you out so it 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 pie charm 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 pycharm so I'll go ahead with that so let me quickly open up the IDE 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 are opening a file right so we have a variable file we can open it and we're 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 overwrote everything that is present in the file and we just closed it so we're not seeing absolutely anything here check out example 2 importing the OS module file equal to open again we are providing the same file here we had write mode and now we are 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 nothing is going to be printed why do you think this happened this happened because we actually use 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 love edureka let me save that quickly minimize it go back to my pycharm now as soon as I run this it is supposed to give me that 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 dot read of Five Guys file dot read of five right so I actually explained 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 oh yep the quick typo uh save it exit back into the code now if I'm gonna 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 gonna 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 the 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 readline will actually read each of the lines and give us a line by line output read line of 3 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 the file which is properly separated and nice to look at well you can actually use the same function only in a new form this is called as file.redlines function again let's take a quick jump back into the code so we can see this in action so check out example 4 on your screen I have the same file demo file dot text I'm opening it in the read mode and I'm using readline as 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 edureka so I have three lines here all good right so I'm gonna save it I'm gonna close it I'm going to come back here I'm going to run the read line again we're reading only the first line cool check out example five now this is where things get interesting input 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 angle 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 love edureka as another entity and that's nice to look at right so instead of just 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 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 a loop over 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 pycham and check the single snippet out so we are back into pie charm and check out this for Loop example I have for you so again we're sitting on the same file demo file dot text reading it in the mode so for every line in our file object print read lines so we're just going to print the output for us containing all of the documents of our file as you can see we'll have Python and we love edureka 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 right 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 Right which we already checked out earlier by append we actually add the contents of the right to a new existing content and with right we just add it to the blank file now I am sure there is a question in your mind right now so what of 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 a pen guys this will basically delete everything in the file and overwrite it with what you'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're back into pycham right now so check out the first example I have for you import OS I already explained opening it in the mode of write 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 are 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 pycharm again check out the next example I have for you so now this is to prove that we are actually overwriting it again which I've already told you and it is simple but still let me walk you through this so we're working on the same file we're using the W mode and then we're writing it to it saying oops we have overwritten it run it run 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 printed I made 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 pycharm 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 append and W is used for right and now we have X which is used to create a file but do note 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 dot 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 Python and we can check out the example over here uh let me change this we have something else right so let me type edureka.txt I'm going to print new file into edureka opening it you're opening it in the X mode so we are actually creating this file physically right mode to write something into it and close it so is it right no output check out the file whereas the file called add Eureka it is your new file of edureka 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 require the OS dot 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 rmdir command what does rmdir 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 files [Music] instruction to buy 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 say 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 front-end 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 server 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 MySQL so guys I'm basically going to perform credit 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 pycharm all right guys before diving deep into the coding perspective my first and foremost step is to basically import the MySQL 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 MySQL hyphen connector all right click on my SQL iPhone connector click on install package it will basically take a minute or two to get installed essentially a first step is to install the MySQL iPhone connector package which forms the stepping stone for our further operations which I'm going to do again it is it will take a minute or two to get installed all right so as you can see our package MySQL hyphen 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 IDE suppose you are 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 MySQL 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 have already imported the package on my pycharm 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 MySQL iPhone connector your MySQL hyphen connector package will be installed and then you can import your sim 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 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 MySQL hyphen connector I'm sorry it'll be mysql.connector now I'm going to create a local DB instance that is mydb under which I'm going to write MySQL Dot connector Dot connect under which I'm going to pass in the three parameters that are host which is equal to your localhost then comes the username that is equals to root and then comes the password which is equals to password one two three so root is the default username which I had given guys and password one two three can also be customized so this is the password which I give now I'll merely print this local DB instance that is mydb 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 there's a local DB instance that is my DB then I'm going to print connection successful else I'm simply gonna terminate this or I'm gonna 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 the 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 but as you can see guys it shares 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 where the name as python DB creation let me just name it python DB1 creation now I'm gonna write my first line of code that is importing MySQL dot connector which is the package then I'm going to write my DB is equals to mySQL dot connector dot connect under which I'm gonna pass in the three parameters that are host which is equals to your localhost then comes your username that is equals to root and then comes my password that is equals to password one two three 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 dot 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 gonna write create database and let me say my database name is harshdv 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 harshdb 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 gonna write show databases now I'm gonna write for DB in my cursive 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 hash DB 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 one second now let me simply write show databases and click on execute one minute guys well as you can see our database hash DB has been created along with all the 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 a 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 hash DB now how do I do that well let me create a new file first one second I'll name it as create PB and python all right all right guys before moving ahead I'm gonna 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 Ctrl V now I'm going to write my cursor dot 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 will be name bar chart 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 gonna pass a parameter over here it's going to be database is equals to harsh DB and let me quickly move ahead and run this code one second 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 hash DB 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 and my cursor print your table all right now let me move ahead and run this code so as you can see we have created only one table under the database hash 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 hashtv thing over here instead of databases let me just write tables select hush DB 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 gonna 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 off table and move ahead yeah Ctrl V before there's one more parameter which I left it's going to be database is equals to Hash DB now moving on let me create a new variable with the name as SQL form or 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 percent as comma percent as well guys this percent as are nothing but placeholders you can replace them anytime you want with any other value now I'm going to create a tuple now the reason why I'm creating a tuple 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 tuple over here so that to give you more clarity into what actually happens now let me just name it as harshit let me give my salary as 20 000. comma Amit salary as thirty thousand comma Ankita and her salary as 40 000 comma space yep I think I need to put this back yeah now when I move on I'm gonna simply write my cursor Dot execute many because if I use execute it is just gonna execute a single value but since I'm making use of a tuple over here I need to write my cursor.execute many and call in SQL form comma employees and finally I'm gonna write my DB Dot commit which is Gonna 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 Tuple has been inserted into the table employee under the database hash 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 gonna write select start from employee and click on employee click 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 hash DB dot employee well it changes nothing Our concern is to basically view the Tuple which you have created and that Tuple is being viewed in the result grid that is under name and salary the values which we pass that are horses 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 readop table all right now into the presentation mode once again copy the first three lines of code over here what I'm gonna do is yeah let me pass the parameter database which is equals to First DB and then I'm going to start off my read operation by the first function that is such 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 dot 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 harshit from the row name under the table employee which is again under the database hash DB 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 Tuple it's going to be select start from employee and instead of fetch one let me write here fetchall and let me run the for Loop so it will be four row in my result print row all right now I'm going to run this code I'll explain you why am I doing it or what does fetchall do well as you can see from the output the whole Tuple has been fetched now when I make use of the fetchall function guides it basically fetches the whole Tuple for us as because we are writing the SQL query select star from employee so all the values which are there under in probability table which was in the form of a tuple when we inserted it a while ago while we were writing the insert code that whole Tuple 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 how should the methane and salary twenty thousand thirty thousand and forty thousand suppose I write this thing select name from employee one second I'm gonna show you the changes of the fetch one functions in order to do that let me just write name again over your 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 it 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 pi 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 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 of table yeah let me close this just click on this enter presentation mode yeah Ctrl V let me pass the parameter which was database that is equals to harsh 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 one second salary let me set the salary to seventy thousand making use of the where Clause where name is equals to let's say I'm updating Ankita salary to 70 000. and finally I'm gonna write my cursor dot one second execute call in the SQL thing and then write mydb Dot comment you 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 on both places in the pi charm and MySQL workbench too so the last code which I wrote let me remove this and right here select star from employee this was fetchall okay Enter the presentation mode once again yeah run this code as you can see from the output the changes have taken place and ankita'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 it's quite easy let me just rerun this code uh this should be star over here yep and then execute it as you can see ankita's salary over here is again updated to seventy thousand now I'll move on and to the last operation that is delete operation which will also conclude my tutorial it's going to be delete off let me name it as table all right enter the presentation mode once again copy the first few lines of code I'll pass here the database parameter database is equals to purge DV I'll move on and let me make use of the same variable which we use that time say suppose delete from employee where name is equals to suppose I'm deleting my record guys which is harshit and then my cursor dot execute all in SQL and then write mydb dot comment which is Gonna Save the changes from my last executed SQL 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 BSE so the database spelling is wrong let me quickly Rectify that one second guys yeah I'm gonna 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 redo I'll enter the presentation mode once again run this code well as you can see the record hardship 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 hardship is deleted from here to foreign 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 and ntp uses 119 Etc let me just jump onto 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 80. as you all can see on the screen when I used google.com with a 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 colon 20. so I hope you're clear with this guys when I gave google.com colon 80 it returned the same Google page but when I gave google.com colon 20 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.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 the 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 have 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 database 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 pycharm 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 dot socket and within the bracket I'll specify the address family as AF underscore inet so socket dot AF underscore inet AF underscore inet refers to address from the internet and it requires a pair of a host and a 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 underscore 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 dot bind and as a parameter I'll use get hostname 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 one zero two four 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 5. 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 connection connection to and within places 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 dot send and since I wanted 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 bytes so I'll say utf-8 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 dot 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 one zero two four 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 one zero two four 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 filename.py so server one dot 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 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 client1.py 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 on to pycharm and this is not sned it's s-e-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 I had originally specified the number of bytes as one zero two four now let me just change it to some of the numbers 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 one zero two four 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 7 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 7 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 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 1024 bytes we received the complete message in a single communication in the second example I just changed the number of bytes from 1 0 2 4 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 ing in Python so I hope you're clear with this client in 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 a 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 0 then I want the slope to break and if not I want the complete info to be printed in 7 bytes per transfer so instead of using this message.d code 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 and 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 Dot 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 one zero two five I'll have to do it on the client as well as the server side so I'll just change it to one zero two five 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 seven bytes per communication as well as the client has been terminated okay still 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 you all how objects can be transferred from server to the client let me just show you how these objects are actually serialized using the pickle module I'll jump onto pycharm 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 ser 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 Ser okay now let's execute this one so as you can see in the output over here guys I have the serialize list I have the numbers 1 2 3 4 presence 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 this 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 import 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 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 use 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 is 38 bytes and this is the serialized form of it and this is a dictionary that we've passed from the server to the client [Music] so what is numpy numpa 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 C C plus plus it is also very useful in linear algebra Fourier transform 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-dimension array so we have various elements that are stored in their respective memory locations so we have one two threes 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 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 pycharm and I'll tell you practically how to actually create a numpy array so this is my python guys over here the first thing that you need to do is first install the num py 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 num py 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 python so first thing that I need to do is import numpy as NP now after that I need to create a numpy array so for that I'm going to Define a variable let it be a and I'm going to type in here NP dot array and certain elements inside it so I'm going to put in one two three and print it that's all this will actually print a single dimensional array so one two three 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 4 5 and 6. 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 num py 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 lists all right so basically we use num py because of three main reasons the first thing is it occupies less memory when compared to lists then it is actually pretty fast when you are compare it with list and at the same time it is very convenient to work with num py so these are the three major advantages that num py has over lists and that's 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 pie charm so guys this is my pycharm again so the first thing that I need to do is import num py as MP 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 sys 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 thousand so what this will actually do it'll actually take all the integer values between zero to thousand and it will give it to a variable s so this list will contain the integer values between zero to thousand but it won't until thousand it will be only there till triple nine 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 dot get size off 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 sys dot 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 dot a range and the range will be thousand now a range function is pretty much similar to the range which is there so the same thing will happen here the integer value is between zero thousand 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 numpy array so I'm going to type in here D dot 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 is a quite a lot 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 1000 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 RS so first let me Define two lists and two arrays for 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 two numpy virus A1 equal to NP dot a 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 lists as well as arrays now before that I'm going to Define a variable say start which is equals to time dot 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 will first take the first element of list L1 and then it will take the first element of list L2 it will going it will calculate sum and we'll store in result and it'll keep on repeating until the range has been exceeded now this is how you calculate some in list but when you talk about Ra's what you need to do is 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 num pyras which is nothing but A1 Plus A2 that's all and now print the time taken so print time dot time minus start and then multiply it with 1000. because by default it will 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 num py 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 zero milliseconds because the size is small let me just start a couple more zeros and let's make it a million now go ahead and execute this now you can notice the difference that there is a significant change lists took 208 milliseconds whereas numpy array took almost 67 milliseconds so there's a huge difference between the compute time of a 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 byte 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 pycharm 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 dot array 1 comma 2 comma three put that in parenthesis now add one more element say 2 comma 3 comma four all right so it's a two-dimensional area 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 DOT end them and it'll give me the dimension so let me show you that and I'm going to run this so it says 2 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 a bite size of each of the elements so what you need to do is instead of endem 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 D type 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 numpyra 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 one two three 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 in four rows so our shape is actually three columns at four rows now let me show you practically how you can do that again I'm going to open my pycharm 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 five six seven then if I execute this you'll see that seven elements have appeared that means the total number of elements in my array is 7. 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 DOT 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 13 14. go ahead and execute this and you can see two comma seven because we have two 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 lary 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 reship geography all right fine so that is absolutely correct guys now over here this is an example so we have three columns 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 one comma two comma three comma four and my other element will be say three comma four comma five comma six 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 gonna type in a is equal to a DOT 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 have 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 3 which is present at 0 comma and the index is two let me tell you how indexing happened so this element will be 0 this element will be 1. now if I want 3 from here the from the zeroth element I want the index two indexing starts from 0 1 and 2. so that's why I've written 0 comma two and it should print 3 for me let us see if it does that or not and yep it prints three now say if I want to print 4 and 6. 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 0 and in that row I want only index three so we have only two rows so if I ever have written 0 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 would actually include the element which is present at the second index so when I say zero colon this actually means all the rows that includes 0 as well so we have only two rows it will include both the rows and at the same time it'll actually going to print the third index from both of these rows so let me show you if if that happens or not and yep it happens we have 4 and 6 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 0 colon because if I do that it'll print 10 as well let me show you that yup it has printed 10. now in order to avoid that what I can do is I can write in here two so as I've told you this is the zeroth element first element and the second element so when I write 0 colon 2 it won't include the second element it'll only includes 0th as well as the first element now inside that we have index 3 from both of these rows that is why we'll actually get four and six let us see if that happens or not and you can see 4 and 6 is now available so this is how you can perform slicing when we in numpy arrays now what I'm going to do is I'm going to type in a equal to NP dot line space and now over here first I'll write 1 comma say 3 and I want say five values between one comma three what this will do is it'll actually print the five values which are equally spaced between one two 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 one then we have 1.5 then we have 2 2.5 as well as 3. so if I would have written here 10 it will actually give me 10 values between 1 to 3 and yep you can see we have the 10 values between one to three so this is how you can perform line spacing as well 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 arrays so let us go ahead and execute that practically let me remove all of this and now I'm going to Define one mode numpyra NP dot array and I want elements and let's say one two three 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 DOT Max that's all and it'll give me the maximum value in my a num pyra which is three obviously if I want to find minimum value so I'm just going to type in here Min and it'll print the same for me which is one now if I want to calculate sum it is pretty easy just go on and type sum and it'll give you the sum that's all guys it's start 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 pychamp 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 particles 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 tasks as well all right so Janice looks happy now so let's 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 axes and you can do that now let me show you practically how it happens I'm going to open my pycharm again and I'm going to show it to you let me remove this and let me add one more element here 3 comma four comma five all right so if I want to find the sum of x is 0 it's very very easy just go on and type print a DOT sum and type in Access equal to zero go ahead and print this and you can see four six and eight one plus three is four two plus four is six similarly three plus five is eight if I make this as axis one and print this so it gives me 6 and 12 because 3 plus 2 plus 1 is 6 similarly four plus five plus three is twelve so this is pretty easy I know now let's go back to our slides and see what are the other operations so there are many mathematical functions that you can perform with num p y 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 pie 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 pyra which is actually assigned to a variable a so I'm going to type in here print NP dot sqrt that is square root of my num p y r a a go ahead and execute this and it's actually printed the square root of each of the elements the square root of 1 is 1 for 2 it is 1.414 so 3 is 1.73 again for three it is 1.73 then for four it is two then for five 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 STD and it'll give me the standard deviation that is how much each element varies from the mean value of my num pyra 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 numpyras like addition multiplication subtraction and Division and that will actually happen element wise so basically you are performing Matrix Edition 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 and 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 plus 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 will 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 Edition has happened 1 plus 1 is 2 2 plus 2 is 4 3 plus 3 is 6 similarly 3 plus 3 is 6 then 4 plus 4 is 8 5 plus 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 one minus one two minus two three minus three three minus three four minus four five minus 5 will be 0 only right no rocket science now go ahead and multiply it as well and see what happens so you have 1 into 1 is 1 2 into 2 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 once and yup it does so this is how you can actually perform addition subtraction multiplication on division using a 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 Dot vstack and a comma B let us see what happens when I run this so we have one two three three four five then again we have one two three and three four five so this is called vertical stacking if I want that horizontally I'll just write in here Edge stack and I'm going to run this now and you can see that we have one two three then again one two three that means these two are present added horizontally and we have three four five again we have three four five so this is how 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 in numpy Array that is a to say a single column so how I can do that just type in here print a DOT arrival that's all you have to do go ahead and execute this soil one two three three four five so you have one two three three four five 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 functions now I'm going to talk about sine function and cosine function first so what I'm going to do is I'm going to use this cosine and sine function and I'm going to plot sine and a cosine graph so for that I'm going to import a module called matplotlib so you don't need to worry about that module because I'm going to discuss about matplotlib in the upcoming sessions so there'll be a detailed session especially on matplotlib so you don't need to worry about what exactly matplotlab 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 sine and cosine function in order to print their graph so for that I'll open my pycharm and let me remove this and import myplotlib dot Pi plot as PLT now we are going to Define two coordinates that is X and Y first X is equal to NP dot a range 0 comma 3 into NP dot pi foreign now I'm going to define y so for that I'll type y n p dot sine 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 dot show you must plot dot show to make a graphics appear so plot dot 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 cos 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 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 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 pycharm let me remove this and I'm going to define a numpy array let it be a r equal to NP dot array 1 comma two comma three 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 dot 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 power of 1 is actually equal to e so it has returned the E value but e to the power of 2 will be somewhere equal to 7.38 e to the power of 3 will be somewhere equal to 20.0855 now in order to calculate log what you can do is you can 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 1 so e to the power of 0 will be equal to 1 right so log or Ln a r equals to 0 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 1 that means anything to the power of 0 is equal to 1 so the answer will be 0 here and similarly we have other answers as well if you are pretty if you are unsure about it you can open your calculators and do that [Music] thank you so let's 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 tabular 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 systems guide 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 are working on an IDE such as pycharm you can just simply type in install pandas in your terminal over there or you can just open the project interpreter and add the library over there since we are going to work on jupyter 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 pandas on your system you open the Anaconda prompt will wait for the prompt to set up so you type conda 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 gonna 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 a 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 series 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 two-dimensional and the size of the data frame is mutable potentially heterogeneous data or we can call it heterogeneous tabular data so the data structure which is data frame also contained labeled axis which is rows and columns and arithmetic operations align on both rows and column labels it can be thought 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 will be able to understand this better what I'm talking about like pandas and series what exactly they are we'll be working on our example now so I'll just take it to Jupiter notebook guys so we have already created a jupyter notebook if you are not familiar with Jupiter notebook guys we have a full tutorial on how to use Jupiter notebook all the cheat sheet and everything so you can just refer to those in our edurica YouTube channel and make sure you subscribe to your direct 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 a lot of content on YouTube you can refer also if you are at it be sure to check out our courses on edureka.com 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 gonna import pandas as PD but on this I'm not gonna face any problem running this command because I have already installed pandas okay so what this command will do over here it's gonna 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 Alias as PD I hope you guys know what Alias is so let's just say I'm importing this Library so Alias 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 taboo here okay I'll just so this is how I can use my Alias to create a data frame so this is just to tell you how you use an alias now I'm going to show you how you can create a series and a data frame so first I'm going to okay I'll have to import numpy 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 all right series and I'm gonna pass a list of values let's say one two three four five six and I'm going to use my numpya Now to create a null value it 9 and one more value I say 10. so it's going to create a series now when I print s so we have a CV 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 pandas 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 the date time index and label columns so I'll take one variable let's say date or dates then just type it as D and I'm going to take PD dot so we're going to take the date range and after this I'm going to pass a few value let's say 2020 and I'm gonna pass values like where 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 am going to take one data frame which I am going to take as DF for obvious reasons to make it clearer and I'm going to take data frame 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 dot random Dot random number and inside this I'm going to pass 10 let's say 4. now I'm going to get the index values as B and I'm going to have to pass a few more values which is columns so I'll pass the columns as a list I'm going to take let's say four columns so I'm just gonna take okay wait a minute a B c c D all right do you have any errors no so now I'm gonna print my data frame so I have a data frame guys which I've created using you know passing a numpy array and I have a date time 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 array so this is how you create a data frame is 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 VD dot 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 now to this I have to pass something right okay I'm gonna write let's say a list of 1 2 3 and 4. after this my next value is going to be let's say B and I am going to pass a timestamp let's say and for timestamp I'm going to use same I have used over here 2020 1 I have to use the right and after this I'm going to pass one more value let's say c and I am going to use series now a series object inside this I'm gonna pass one and the index is going to be let's say range all right index is equal to a list with a range of 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 in numpy array so this I'm going to pass a value let's say not three let's say five multiplied by 4 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 a data frame or we're going to use the categorical object Okay so 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 foreign 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 and Eureka all right so r dictionaries down 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 C D 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 timestamps 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're going to look at the data using pandas Library so we'll just jump right to Jupiter notebook guys and I'll tell you what kind of functions or all those things 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 Dot 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 I'll be 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 dot 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 dot index so what this will do is it will give you all the values from your index and similarly we have TF dot 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 dot two numpy which is going to give you a number representation of the data so I'll just tell you how you can do that so I'll just write DF dot to numpy we the second guys yes so I'm gonna create a number array using this this I actually created an Umpire array and for dfr data frame of all floating Point values dataframe.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 dot describe 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 are 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 access we can sort our data using an axis so for that you have to just write you can just show you guys you just have to write DF dot sort by index and inside this you you have to give the value of the access I'll just give one and then let's say ascending you want it to be ascending no I'll 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 sorted 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 gonna show you how you select a single column from your data frame so we'll write BF it's very simple guys to get a value from your data frame using only a single column you can add a or let's say C so 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're 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 zeroth row and it has given me third row and it does not actually include the third row because it starts from zero so I've been giving three values if I write 6 over here I'm gonna 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 0 through 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 select the data using the labels guys so for that you have to use DF dot there has to be location Loc and inside this you're gonna pass the values by labels guys all right so let's pass d that is n0 so let's see what the output is guys all right so we have got all the values using the label that is do here which we have passed over there in our previous section where we have declared the data frame and I'm sure this visible to you guys now the next thing is uh selecting data on a multi access by label so what we'll do is we'll write df.loc and after this we write hyphen and we're gonna create let's say a and we're gonna pass C right so it's going to give me the values accordingly which I pass over here so instead of a I can write B or I can write d so this is how you can select multi-access using labels guys and I have written this over here so I can just write let's say 0 to 3. 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 endpoints 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 row here remove this so this is just to show you guys how you can work with it I am 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 gonna 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 gonna give you the column number at over there and this is how you get the values inside a data frame so moving on let's say we want to get a scalar value so for that we just write okay let's say D 0 right let's see if it works we're getting the same values guys only from the zeroth row for getting the fast access to a scalar you can just write as a DF instead of loc you can just write at okay we have an arrow guys I'll just remove this and let's see if it works yes so I'm getting the exact value at the zeroth row add 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 Dot iloc 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 three to five right and we can add more values to this like 0 to 2. 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 interesting guys so it has given me all the values inside a where a is greater than 0. if I write here let's say 2. I have no values because none of the values are greater than 2. so this is how you can get the Boolean indexing this is actually important when you're applying functions to your data frame guys so Moving On Let's uh take a look at another method which is is in method and just tell you how it works guys so it's basically is 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 a series and we can set the values by label we can set the values by position and we can set the assigning with a numpy array as well and the result of the settings will actually align with the data frame 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 dot Nan to represent missing data It Is by default not included in the competitions and we're going to see the missing value right now so first of all you have to re-index I mean you have to do re-indexing which is going to allow you to change add delete the index on a specified access so which is going to return a copy of the data as well all right I'll just take the F2 is equal to DF Dot re-index so this is how I'm going to do the indexing guys so index is equal to let's say d 0 to 4 yes I'm going to get the columns after the reader indexing which is going to be equal to list of DF dot columns and I am 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 loc I'm going to check a few values so instead of dates I've taken B guys so D of 0 and D of 1 at e is equal to 1. 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 re-indexing so first of all I'm going to check for null values so we have 2 over here and we can get the count as well is null and we count these null values all right now we are going to drop a few columns so we're gonna drop the any that is the any 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 are going to provide some value let's say value is equal to 2. 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 now actually get a Boolean mask where values are Nan which is null so for that you do PD Dot okay is any df2 so this is going to get you a Boolean mask which I've already told you how you can check using DF dot 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 pandas operations guys final 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'll tell you how you can actually work with pandas 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 Dot 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 neat alignment in addition panel automatically broadcast along the specified dimension so for that let's make series I'll tell you just how it works PD Dot series I'll give it a few values let's say one two three NP Dot N A N five or I'm sorry four five and then give the index value the index is going to be dates and let's just shift all this to places right we have made an error guys length of pass value is 6 and index implies 10. so we have to actually put more values so write six seven eight nine 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 dot sub and we pass the S over here which is RCS and we make an axis write it as index so we have operated with objects that have different dimension idn needed alignment so in an 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 gonna use the apply method over here and let's see what all do I have let's just check we have absolute absolute import all all close Amax a minimum angle any append all these functions that I can apply on this so let's see we have come some 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 in Python guys so this is how we applied Lambda function to get the subtraction between the X Max and X minimum all right for all the columns we have subtraction mean 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 matplotlab.pipelot dot hist on each Series in a data frame resulting in one histogram per column so what we do is 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 as dot value counts right let's see if it works as we have one value for each so this is how we get or do the histogramming without data guys now I'm going to talk about the string Methods so series actually is occupative 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 it 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 Eddie Reika right python next right 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 values let's say football and looking at the current scenario let's write vault so we have a series over here guys now what we'll do we'll take or use the string Methods so I'm gonna make it all to lower which are already lower so I'll make it up to Upper guys upper AdWords so everything we have changed using the string Methods inside RC uh Panda 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 pandas objects along a particular axis with optional set logic along the other axis 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 access number so let's take a look at the example for this so we have our data frame I'm going to use PD Dot data frame give it a few values let's say in P Dot random dot 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 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 all right now I'm going to use the concatenation function using concat all right I'm gonna 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 gonna 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 Dot data frame 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 further 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 gonna join all these two using the joint 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 gonna join it at let's say a let's see how it works okay it doesn't so we have a key error which is a so I'll change it to a wise 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 nice 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 say a and I'm just gonna 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 data using the column that is number two now we can actually group the data by multiple columns and form a hierarchical 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 hierarchical 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 hierarchical index which will have different values or two columns 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 in 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 the stack guys in pandas I'm sure most of you must have heard about some other definitions of stats so I'm gonna tell you about this with perspective of pandas Library here so the stack function is used to stack the prescribed levels from columns to index and it returns a reshape data frame or a series having a multi-level index with one or more new innermost wet levels compared to the current data frame so you're going to understand this with an example so I'm just gonna take uh let's say my topple is equal to I'm just gonna take the list and inside this let's see we have values and we provide or we get two lists over here a list inside list guys so let's take a few values take one two three four five and then again let's say six seven eight nine and ten or we'll add few more values guys say 11 12 and 13. over here I will add few more values let's say 17 18 and 19. so we have our double I'm gonna create one more variable index so we'll have multi index so from tuples inside this I'm going to pass my Tuple 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 in P dot let's say random dot random number so we have eight values and two columns all right so and the index is going to be index and columns is equal to a and B right we have an attribute here 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 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 gonna do one thing we're just gonna do df2 Dot stack right this an attribute Arrow so this is how we stack or compress a level in the data frames column guys and with the stack data frame or series having a multi index as the index inverse operation of Stack is unstacked by which default it's going to unstack whatever you have done with using stack so we'll do df2 dot stack and this is how you unstack now we'll talk about the pivot tables guys oh wait let's put it inside a variable let's say a all right so this is how you unstack guys we're 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 pivot 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 PD 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 say one or let's say a all right so we write a B C d now we will remove a few values over here multiply it 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 see now and inside this we're going to pass a few values again so we're going to pass Six values so let's write B B and B Cube Q and Q multiplied to 2 because you want the number to be 12. and now D is going to be NP dot random dot random number and number we want is 12. and we take one more value e and we take same values for this as well random not 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 wait DF and the values is going to be let's say B 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're done with pivot tables 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're going to 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 data into five minutely data and this is extremely common in but not limited to financial applications so we're gonna 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 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 and just cut this so I'll just run this again we'll get one more column and now what I do is I'll make one variable using the PD dot date range yes and I'm going to provide the range as let's say 2020 zero one or zero three zero one you 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 just remove this for now so now we take one more variable let's say TS is equal to PD dot I'm going to take a Time series and inside this I'm going to use NP dot 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 random all right so we have an unsupported date time so I'm going to have to change this over here so we write it as let's say three one or I'm gonna write three three twenty twenty right so let's see if it works now doesn't so we're getting the unsupported D type okay so we just mention a few more stuff over here so write it as zero zero zero zero so I think I figured out the problem over here so I've changed this to this format and now I'm going to add one more parenthesis here and remove this one and now I'm not getting any arrows 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 five 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 gonna 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'll change this to five all right so now we make another timestamp and we just use PD Dot series and inside this I'm going to use NP Dot random Dot random number and the random number is going to be the length of dates and date so here all right so now when I print timestamp I'm getting the output as someone 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 float64 and then you can also get the UTC as well so for that I'll just write TS UTC and we just write timestamp dot TZ localize and we get UTC right it's UTC now I'm gonna 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 dot t z which is time zone convert and we write us Eastern so I 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 the time spans representations so that you will be able to understand this better for that you have to you don't have to do anything you just have to take the date range the pH is going to be 5 and then you write the frequency instead of s you write it as m the rest always 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 will have to remove this all right now I make the timestamp which is going to be the same guys now print the timestamp this is how we convert or after this we take one more variable let's say PS and and I'm going to to 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 timestamp guys see where it is this is how you create it into a timestamp now converting between period and timestamp enable 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 dot data frame now I'm going to take a few values inside this 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 great or just grade will write so we're gonna 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 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 we print DF grade so we have grades like a b c b a e 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 fair I'm just gonna write very good and then there is excellent all right so we have very good very bad and excellent all right we have another base there's something we have done so we have new categories needs to have the same number of items as the bold categories so how many categories do we have over there I'll just copy this so we have categories four so we're gonna have to make four categories guys so I'm just going to add good as well and 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 gonna have to give six values guys sorry 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 all right so after this I have set the categories 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 Nan 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 gonna have to import one more library that is matplotlib Dot Pi plot I'm gonna use it as PLT all right same thing goes with this as well all right this one mb5 plot so there should not be any errors now okay so uh I will close all no I'm gonna make one see these guys and inside the series I'm gonna provide a few random values like NP Dot random dot random number until let's say a thousand or let's say 500 or yes 500 and the index is equal to PD Dot date range I'm going to take the date range as 1 3 20 20. and let's take the periods is equal to 1000 wait we have to take it as 500 because we have 500 values over there I hope no errors yes now I'm going to take the timestamp and we're gonna get the com sum all right now TS Dot Plot so we have a plot over here using pandas guys this is how we have created one series using the random numbers from numpy library and using the pi 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 uh or get a plot using pandas guys now last but not least we have another topic which is reading and writing to files so inside this I'm going to show you how you 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 our TS 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 dot CSV file somewhere in my directory and similarly I can read from a CSV file so for that I can just write PD dot read CSV and I'm gonna have to give the file a 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 gonna copy this paste it over here okay we have a Unicode error so I'll just write R over here and I'm able to read from the CSV 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 file [Music] 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 station 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 the 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 were 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 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 that 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 everyone 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 the 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 yours 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 diagram 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 any employed youth between 2011 to 2010 so that will come under analysis phase 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 from what all countries it is stable there is 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 all right so Neha is asking Matlock is used for data visualization yes Neha it is used for a data visualization we are going to see that in the next slide any other questions all right so we have no question so we'll move forward and understand what exactly is matplotlib now it is very important for us to understand as to how matplotlib 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's 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 laborious 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 matplotlib works so you have an example in front of your screen so again we have the data in which we have country wise percentage of unemployed Youth and I've done some analysis and I've shown it graphically 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 Pi plot hexagonal bin plot and area plot we are going to plot all these things in today's session with the help of Mart plot lab module so we'll move forward and we'll get started with it we'll write our first smart plot 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 By plot from matplotlib as PLT then I'll use this Pi plot in order to plot the graph in the canvas and then finally I'll use this PLT which is nothing but Pi 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 practical 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 Tableau we even use R python so for python you need to import the matplotlab 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 pycharm guys over here I'll first create a python file with DOT py extension and I'm going to name it as first code now over there I'll first import pipelot from matplotlib so for that I'll type from mat plot lib import as PLT now this pld is pretty much similar to NP that we were using in numpy wire 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 Dot Plot and I'll write in here my X and Y axis values so for x-axis I'll keep it as 1 comma 2 comma 3 and then for y-axis I'm gonna add values such as 4 5 and 1. so this will plot a graph in the canvas now final step is to show that plot how we'll do that I'll just type in here PLT dot 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 diagraph 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 access you don't know what is x-axis you don't know what is y-axis and what is the graph about Plus in terms of programming it is very unlikely that you'll be actually filling it data to the plt.plot function now over here you can notice that we are actually filling in data like one two three four five one instead you will be passing variables into it all right so it should be something like plt.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 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 to 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 the pipelot from matplotlib 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 16 6 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.plot function in which I have X and Y instead of filling the data I am putting in variables that actually contain data after that I've added a title which is info then y label 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.show and the result is there in front of your screen we have a title we have labels for x-axis and y-axis now let me open my pycharm and execute this practically so before I open my pycharm and execute this practically we have a question from Siddharth 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 6. so I hope this answers your questions all right so he says yes fine so I'll open my pycharm and execute this practically now this is my time again guys so I'll remove till here and obviously I need pipelot function from matplotlib 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 6. yep now what is the next step guys can anyone answer me all right so I've got a correct answer from Theon he's saying a plt.plot function which is absolutely correct so I'll write in here PLT Dot 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 dot 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 dot y label and I'm going to name it as y-axis similarly for X label I'm going to do the same thing x axis all right now finally I'll type in PLT dot show and now go ahead and run this so you can see that we have a graph that contains the title info the y-axis is a y label as well as x-axis as the X label now I'll again open my slides and we'll see what next are we going to see so any questions or doubt 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 craft so how to add style to a graph for that first thing that you need to do is import the style function from matplotlim review after that you need to call it style.use ggplot so whatever plot that you want to use I'm using ggplot right now then again I've defined four variables x y x 2 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.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 5 so I have put in here five similarly I have done for the next plot as well then I've added title which you know how to do that similarly y label or X label now after that I'm using this Legend function in in order to add a legend to My Graph over here you can notice that we have it present in a 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 dot show 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 or doubts you can ask me all right so we have no questions so I'll open my pycharm again and I'll show you how you can do that practically so this is my pie chart again so this is my pie charm again guys let me first remove all of this and I need to import the style function so for that I'll type from matplotlib import Style that's it style dot use ggplot Now define the variables first will be X add certain values to it a lot in five eight ten five eight ten then y I'm going to add list of numbers 12 16 6. now one more variable X2 again will have list of numbers which will be 6 9 11. then I'm going to use Y2 variable and I'll add some numbers to it 6 15 and 7. all right so now I'm going to use plt.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 which is equal to 5. now again for X2 and Y2 I'm going to type in PLT Dot Plot X2 comma Y2 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 PLT dot title and the title that I'm going to give is info again then X and Y labels PLT dot y label y-axis then plt.x label x axis PLT dot 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 finds out to find 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 dot grid and just write in here true comma give a color I want it black so I'll keep it that way finally show your plot so PLT dot show that's all go ahead and run this so you can see that this is our graph so we have changed the default line width 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 decide in here PLT dot 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 customize and add style to your graph so I hope you all are clear all right so everyone says they are 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 matplotlib 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 matplotlib now since we know why we use bar graph let me explain you the code and how you can do that using matplotlib so first import pipeline like we do every time after that instead of plt.plot I'll use plt.par and I have filled in a data here 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 it as a green after that I've 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 pycharm now so this is my python guys over here what I've done first of all I have imported the pipelot function then instead of pat.plot I'm using plt.bar 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 1 then comes label for this instance it is example one then again for one more plot I have plt.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 slave we have y label as well as X label 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 parplot 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'll use histogram when I'm talking about quantitative variable that means if I'm talking about age group in the 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 we'll use power plot 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 bar plot 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 the final list in which we have multiple numbers after that we have defined one more list or a variable bins in which we have multiple numbers again now instead of using plt.bar for bar plot for histogram we use hist PLT dot hist 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 then the width should be 0.8 that's all that's all you can understand it is X label y label title Legend and finally show the plot now I'll go ahead and execute this practically in my pie chart so this is my python guide 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 matplotlab module then we have a variable population ages and bins now instead of using pld.bar I'm using plt.hist for histograms then comes the two variables and then his type is equals to bar and then I have defined width similarly I have defined X label y label 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 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 they're 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 pipelot function from the matplotlib module then we have two variables X and Y which contains a certain set of values now instead of calling plt.plot.pld.bar.pld.hist 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 label the title then Legend and then show that's all now let's go ahead and execute this practically in my python now this is my Python 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-axis 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 on 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 with the 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 pycharm so I've copied the code already just to save time again now this is our code over here I've already explained to 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 label 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 stat plots only they are for a certain point in time typically a pie chart is used to show paths 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 matplotlib 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 plt.pie it's 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 degree 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 explored that say 0 comma 0.1 comma 0 comma zero 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 zero zero zero zero now if we want to pull out the first slice a bit we would do 0.1 comma 0 comma zero comma zero and if you want to pull out the second slice a bit what we can do is zero comma 0.1 comma 0 comma zero like in our case now finally we do an auto part now this Auto Part overlay the percentages on the graph itself so you can see that we have 8.329.2 percent fifty four point two percent 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 gonna 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 in 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 couple of modus one is matplotlib.pi plot 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 wire now let me just tell you what it does it will create a numpy array that'll have elements between 0 to 5 in the steps of 0.1 now similarly T2 will also be a numpy array that will have elements ranging from 0 to 5 and in these steps of 0.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 upload does it helps us to plot multiple plots all right so when we write subplot211 this means that we have two plots horizontally we have only one plot present and what particularly we have two plots and in that vertical position this plot will be our first graph now you can notice that we have one more subplot that is two one two 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 pycharm 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 gonna 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 2 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 a subplot right 2 2 and 1. now what this 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 2 2 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 2 1 1 and this as two one two 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 is [Music] let us understand what exactly is the meaning of computer vision so before I explain you anything guys I believe all of you are on Facebook and the moment you upload any picture on Facebook there's a feature called Auto attack right so Facebook will give you suggestions to tag people who are there in the image right so how do you think that happens that happens all because of computer vision so computer vision is an interdisciplinary field that deals with how computers can be made to gain high level understanding from digital images or videos so the idea is to automate tasks that the human visual systems can do so a computer should be able to recognize that this is a face of a human being this is what a Lamppost looks like this is basically a statue so things like that right so I hope I'm clear what is the meaning of computer vision and I don't think so I need to explain to you what are the applications of computer vision right so uh let's move forward guys and we're going to see how a computer reads an image now this is very interesting so notice the image that is there in front of your screen right so a normal human can easily tell that there is a New York skyline in this image but can computers really see this well the answer is no computers see a matrix of numbers between 0 to 25 0 to 255 right so for a colored image there will be firstly three channels red green and blue and there will be a matrix associated with each of these channels right and each element of this Matrix represents the intensity of brightness of that pixel all of these channels will have their separate matrices and these will be stacked onto each other to create a three-dimensional Matrix so a computer will interpret a colored image as a 3D Matrix I hope I'm clear so when I say that the size of an image is 700 cross 700 and it is a colored image that means there are 700 rows 700 columns and there are three channels because it's a colored image similarly if I say 300 cross 700 that means there are 300 rows and 700 columns and if it's a colored image there'll be three channels now in the example which is there in front of your screen we have an image which has a size of B cross a and since it is a colored image there'll be three channels one thing to note here guys for a grayscale or a black and white image there's only one channel so if there's a black and white image and I say that the size of the image is 700 cross 700 it means there are 700 rows and 700 columns in one single channel right so I hope I'm clear for a black and white image there's only one channel for a colored image there are three channels popularly called as RGB and each element of the Matrix represents the intensity of the brightness of the pixel so this is how basically a computer reads an image it will read an image in the form of Matrix it depends if it's a colored image then it'll be a 3D Matrix and if it is a grayscale or a black and white image then it will be a 2d Matrix similarly if I want to calculate the number of pixels all I have to do is multiply the number of rows number of columns and the number of channels so if I say that the size of the image is 700 cross 700 cross 3 because it's a colored image then you can go ahead and multiply these numbers and you'll find the number of total pixels so I hope you have understood this let us move forward and we are going to focus on what exactly is opencv now first of all opencv is a library which is used for computer vision it was first developed in the year 1999 at Intel by Gary bradsky and the first release came out in 2000. let me tell you that opencv supports a wide variety of programming languages such as C plus plus python Java Etc and also supports different platforms including Windows Linux etc etc now opencv python is nothing but a python wrapper for the original opencvc plus plus implementation and in opencv all the images are converted from all to numpy RS right so if there's any image then opencv will first convert it into a numpy array right and this makes it easier to integrate it with other libraries that use is numpy for example sci-fi and matplotlib but for now understand that all the images will be first converted to a numpy array right so that's how opencv Works since we saw that computer will first convert the image to a matrix in this case it will convert it into a numpy matrix or a numpy array you can say now we'll directly jump into certain basics of opencv so what I'll first teach is how you can read an image how you can perform basic operations for example resizing the image or displaying the image and things like that's that that's what I'm going to first focus on so for that what I'll do is I'll open my Python and I'll execute the code there but let me just first explain you what I'm going to show you so first of all I'm going to teach you how we can read a particular image the first thing you need to do is obviously import the opencv module then we are going to read the image with the help of this IM rate function so this one represents that it will be a colored image if it is a colored image and if we put 0 here then it'll convert it into a black and white or a grayscale image right and let me tell you that opencv will read it as a numpy array so basically python stores the images as numpy arrays or Matrix of numbers so if it's a colored image it will be a 3D Matrix and if it's a grayscale image it will be a 2d Matrix so what I'll do I'll just quickly open my pycharm and I'll show you certain basic operations there itself so guys this is my python so what I've done here is I've imported the CV2 module and what I'm doing here is I'm using this IM read function and I've given the path to the image so you can see the name of my image is frank.jpg which is present in this particular path and one basically tells it to keep it as a colored image itself right so since I was telling you that all the images are converted to numpy array so it is a colored image it will convert it into a 3D Matrix let us see if that happens or not so I'm just going to print this and let me go ahead and execute it and you will obviously see a 3D array here right similarly if I go ahead and convert this to a grayscale image if just all I have to do is put 0 here and we have a grayscale image here so let me just go ahead and run this and you will notice a 2d array notice the difference guys right and if you want to see what kind of a matrix this is all you have to do is type in here type and it will show you the type which is a numpy n-dimensional array all right so I have told you earlier as well that opencv will convert all the images to a numpy array now what if I want to know the size of my image for that all I have to do is use the shape function and even know the size of the image since it's an Umpire all I have to do is IMG dot shape and let us see what is the shape of our image so it is 600 cross 800 because we have converted this colored image to a grayscale image that means we have 600 rows 800 columns and one single Channel because it's a black and white image now if I convert this to a colored image you will notice the difference in the shape and you can see that 3 is added here which basically means there are three channels RGB so there are 600 rows 800 columns and there are three channels so I hope you guys are clear how opencv reads an image uh how to know the shape or size of the image how do we know that whether it's a colored image or a black and white image what is the difference what do you mean by RGB channel so I hope you guys are clear with that so we saw how to read an image we saw how to print an image how to print the n-dimensional array then we also saw how to print the shape of the image and what will happen if we convert it into a colored image then we'll saw that three was added in the end which basically means three channels right now what we are going to do is we are going to display the image how we are going to do that we are going to use this weight key function in which if we don't specify any parameter and we keep it as 0 it means that the moment user presses a key the window will be closed and if we specify a time frame for example 2000 milliseconds then the window will wait for 2000 milliseconds and then it will be destroyed so this destroy all windows basically closes the windows based on my weight for key parameter so if it is zero the moment any person presses any key then all the windows will be destroyed and if I have given certain parameters like 2000 milliseconds then it will wait for 2000 milliseconds and it'll automatically close the window so I hope you have understood this let me just go ahead and open my pycharm and I will show you there as well so guys this is the pycharm all I will do here is I basically want to print it or I want to display my image so I'll use this IM show function uh to give a name to this image so let it be a legend and we're gonna type in here the name of the image here this is my file name and this is the image name right so I have given the path here if you can see now what I'm going to do is I'm going to add the weight for key parameter so for that all I have to type here is CV2 dot weight key and for now I'll just keep it 0 which means that the user has to press any key to close the window right now what I'm gonna do is destroy all windows function and let us go ahead and execute this and we will see the image and the moment I press any key it is gone similarly if I type in here 2000 milliseconds right so it'll appear for 2000 milliseconds and it'll automatically go away right so I hope you have understood how we can you know display the image and how we can close the windows by using weight key and destroy all windows function so this is pretty easy guys I hope you have no doubts here so now we saw how to basically read an image how to perform basic operations how to show the image how to close the windows and things like that now I'm going to tell you how we can resize a particular image so let me just go back and what I'll do is I'm going to resize this image and I'll go back to my pycharm again and let me just create one more variable Let It Be resized which is equal to CV2 dot resize the name of my image and the shape that I want so probably I want 600 cross 600 right let me just go ahead and run this I want 600 cross 600 so instead of IMG I'll type in here resized and let us see what happens once I do this so you see that I have a resized image here right now obviously that is not symmetrical right so what can I do to make it symmetrical I can just go ahead and divide it by two or three the way I want to reduce the size so for now I'm just going to divide it by 2 and let us see what happened so basically if you see the code here as well what I'm doing here I'm deducing it by two right so image shape divide by 2 and I'm converting it into an integer because even if it turns out to be a float value I will convert it into an integer so New Image shape is equals to all image hey buy two that's what I'm doing basically right so again I'm going to use the resize function for that I'll open my pie chart so all I have to do here is Type in here in to convert it into an integer IMG dot shape function I'm going to use 1 divided by 2 and I'm going to close the parenthesis similarly here as well and open the parenthesis IMG dot shape 0 divided by 2 and close the parenthesis and I think we are good to go so the size of the new image will become half of the old image let us go ahead and run this so yes we can see that we have reduced the size let me just keep it at zero so that if I press any key then only the window will be closed now similarly I can increase the size as well so let me just go ahead and multiply it by 2 and increase the size so my new image will be twice the size of the old image let us see if that happens or not we'll run this and yep it's it's a pretty huge image let me just close it for now alrighty then fine so we have learned how to basically resize an image let me go back to my slides and we are going to focus on what we are going to discuss next so we saw how to resize the image next up we are going to see how we can detect a face in a particular image so it's pretty easy guys what you need is an image obviously you need to create a Cascade classifier and it will contain the features of the face right so that your code can determine okay where is the face then we will what we'll use we'll use opencv that will read the image and the feature file and it will convert the image into numpy arrays so it will search for the row and column values of the face right so numpy and dimensional array the phase coordinates and it will display the image with the rectangular phase box let me repeat it again for you guys what we need for phase detection we need an image then what we are going to do is we have a Cascade classifier which contains the features of the face then we will use opencv that will read the images and the features so it will convert that image into a nump by array and it will search for the row and column values of the faces of the phase numpad and dimensional array basically the phase rectangle coordinates and then what we'll do is basically we'll display the image with the rectangular face box right so what we are basically doing here is we are going to first read this Cascade classifier uh we'll give the paths to it then we are doing are doing is we are reading the image that we are talking about will give the path to it as well we're converting it into a grayscale image CV2 dot color bg2 BGR to gray is basically going to convert it into a black and white image or a grayscale image then what we are going to do is search for the coordinates of the image right so we have this gray image we are going to search for the coordinates with the help of detect multi-scale this is basically to figure out where is the face right let me just drill it down a bit so this will create a Cascade classifier object right and this is the path to the XML file which contains the face features then we are converting the colored image to the grayscale image after that what we are doing we are using a method to search for the phase rectangle coordinates so this detect multi scale is basically a method which will help us to search for the phase rectangle coordinates and scale factor basically decreases the shape value by five percent and until the face is found so smaller this value the greater is accuracy so scale factor is something I don't want you to focus too much right now this decreases the shape value by 5 percent until the face is found it keeps on decreasing it right the smaller this value the greater is your accuracy right and after that we are just printing the type and the face and how we are going to add the rectangular face box for that we are going to use a for Loop and a method to create the face rectangle this is nothing but a method to create that rectangle this is our image object right and this is nothing but the RGB value of the rectangle outline so I am going I have given it as some color you can give it whatever color that you want now this is the width of the rectangular face box and these are nothing but the coordinates X comma y then we have X Plus width and Y plus h so this is nothing plus X plus W plus comma y plus h right so I hope you guys are clear with it and let me just quickly open my pycharm and I will execute the code there I've already written the code so let me just take you through it it's very easy guys I have a Cascade a classifier object here that I've created which will contain the face features then I am reading the image which I want to create a rectangular face box around or detect the face I'm converting it into a grayscale image then what I'm going to do is I'm going to determine the coordinates of the face for that I'm going to use this detect multi-scale method then I'm going to add rectangular face box around it for that I'm going to use this for Loop and this rectangle is nothing but a method to create that rectangular face box after that I'm resizing my image and that's all I'm doing so what I can do is I can divide it by 2 so that it becomes easier for you to see but I don't think so I need to resize this image what I'll do is I'll just keep it the way it is and we're going to see that particular image itself I don't think so I need to resize it so I'll just type in here IMG and we have our code ready that will determine that will detect the face in the image yep it successfully did that so I hope you guys have understood how we can detect faces in a particular image using opencv so guys this is nothing but the type right so we have a numpy n-dimensional array so this is nothing but the coordinates of the face now if I go back to my slides you can see here these are nothing with the coordinates which I have displayed here right it's that easy guys now let me go back to my slides again and we are going to see how we can capture videos using opencv interesting right let's see how to use opencv to capture video with computer webcam now before I move forward guys let me just tell you a few things so what we'll be doing we'll be using opencv for reading frames or images one by one so what basically a video is video is nothing but multiple images or multiple frames which are displayed very quickly so that it looks like a video so what we'll be doing we'll be using Loops to build a window where images will appear really fast so that you can see it as a video something like this guy he's basically smoking a cigarette it is nothing but two images but since I'm doing it pretty fast you can see it as a video like he's smoking although I don't want you guys to smoke now let us see how we can capture video using opencv the first thing we need to do is import opencv obviously then what we are going to do is create a video capture object and this number basically tells the computer to use our built-in camera right if I want to use an external camera for example I have an external Cam and I can put one to use that similarly if I have two external cameras and I want to use a third cam I can put two here apart from that even if I have a video file I can give the path to that video file as well then this will basically release the camera in some milliseconds right so let me just quickly execute this and you will see what happens there right I'll quickly open my pie chart let me just go to the basics of py file and I'll just type in here video equal to CV2 dot video capture and I'm typing in here zero to use my built-in camera and after that I'm going to release this so I'm just going to type in here video dot release that's all I have to do go ahead and execute this run it again notice that the camera light will be on for split seconds again I can execute it'll be on for split seconds right so let me go back to my slides and I'll explain you there what I'm talking about so basically when you execute the code you will notice that your cam light switch is on for split seconds and then it turns off so what we need to do is we need to add a time delay and for that what we need is a Time module right so what I'm going to do is I'm just gonna type in here time dot sleep3 which will stop the script for three seconds for three seconds your camera will be on right so when you execute the code the webcam will be on for three seconds let us see if that happens or not I'm going to execute this now so for that I'll open my python again so this is my pie time again guys I need to First add time module here and now I'll add that time delay time dot sleep for three seconds keep my webcam on and here we go so we have successfully added the time delay it was that easy guys now let's add a window that shows the video till now we didn't see the video we didn't see what's happening right so how can I add that window so it's very very easy guys I hope you guys know this this is basically a video capture object now this check is a bold data type that returns true if python is able to read the video capture object this is just basically a true and false kind of a variable that will return true if python is able to read the reader capture object otherwise it'll return false frame is a numpy array it represents the first image that video captures right since we saw that video is nothing but multiple images which appears really fast and it looks like a video right so the first image or the first frame is basically what we will store in this particular frame the numpy array of that particular image so it is an Empire array it represents the first image that video captures we are going to print both check and frame and we're gonna add a time delay as well so let us just quickly open uh the pie charm and we're gonna execute this code there let me just open it for now what we are doing here is uh check comma frame equal to video dot read let me just print check and I will also print frame and let us go ahead and execute this we'll see what happens all right so what happened here python was able to read the video capture object that's why we have got the output as true basically our check has returned true because python was able to read that particular video capture object apart from that this is nothing but the numpy array for my frame right which is pretty visible I don't think so I need to explain you why it is an Umpire array why it is three-dimensional I think we have discussed quite a lot about numpy Ras in the previous slide so for now I think you have understood this what check-in frame is check is basically a boole data type it will return true if python is able to read the video capture object and frame is nothing but the n-dimensional array so guys now we'll see how we can actually create a window right so what we need to do is we need to create a frame object which will read the images of the video capture object and we will recessively show each frame of the video being captured so all I'm doing here is using this IM show function again right and I'm going to give a name to it and I'm going to show all the frames which are going to come till I close the window all right so it's pretty easy guys for three seconds you're gonna see a video being displayed on a window and for that I'm just quickly going to open my pycharm and I just need to add this IM show function there and let us go ahead and do that CV2 dot IM show I'm gonna give a name say let us call it as capture and I'm going to type in here frame now uh we need to use this wait for key function wait key uh let us keep it as zero so that whenever I press any button it will close the window and finally I'm gonna type in here CV2 destroy all windows so now let's go ahead and execute it so we'll see a window that will capture our first frame and let's see if that happens or not here we go so yes the moment I press any key the window will disappear all right so this is how we can display the video on the window right uh let me just go back now how to capture the video inside of the first frame right for till now what we did we just captured the first frame that appeared in front of the camera right because that is basically what video is basically what it is nothing but multiple frames which appear really quickly and that comes out as a video so what we need is now we need to capture the entire video so for that what we'll do we'll use a loop because I've told you earlier as well that is what we're gonna use uh in order to capture the video we will be using a while loop right and the condition will be such that unless check is true python will display the frame because what is check check will basically return true if python is able to read the video capture object and if it is not able to read it then it becomes false so our condition will be such that if check is equals to true then python will display the frames right it'll display the frame on a window so let me just take you through the code that I have written Here video is equals to cv2.video cap capture equal to 0 which is basically a video capture object I'm not going to repeat that I have a variable a which is equal to 1 while true while python is able to read the video capture object it'll enter the loop a is equal to a plus 1 which will keep on increasing check comma frame so it will print the frame basically the numpy array object and what I'm doing here is converting the frame the image into a grayscale image then I'm going to show that grayscale image and I have given it a time frame that is one millisecond so it will be the this will generate a new frame after every one millisecond so this will basically generate a new frame after every one millisecond right so it is nothing but a weight key and after every millisecond the frame will change all right guys and the moment I press Q this break statement will come out of the loop and the window will close right print a this will print the number of frames that we have captured right and uh we did not release you know destroy all windows you know right so let me just take you through the code again we are importing CV2 and time module then what we are doing we are creating a video cap your object after that we have a variable a equal to 1 which is nothing but it will tell us the number of frames that we have captured while true which means that while python is able to read the video capture object increase the a value by one then you know these two statements we are printing nothing but the frame the n-dimensional array object then we are converting that frame to a grayscale image then we are showing it and after that we are generating a new frame after every one millisecond that's what it is doing here then once we press Q we are out of the loop and console will print the number of frames and all those things right so let me just quickly open my py charm and I will execute it there all right so for that let me just remove these things for now right so after this I have a video capture object that you can see below this I have a variable a equal to one now I'm going to use while loop while true enter the loop a equals a plus one now over here I'm just going to print the frame and now I'm going to convert this to a grayscale image gray is equals to CV2 dot CVT color frame comma Now I'm going to type in here CV2 dot BGR underscore CV2 dot c-o-l-o-r underscore b g r to gray all right next up we are going to use this IM show function uh let me just show you what I'm gonna do here I'll just type in here CV2 dot IM show uh Let It Be captured again and I'm going to show you the grayscale image and then finally I want a new frame after every one milliseconds for that I'll type in here CV2 dot wait for weight key and after every one millisecond and over here I'm just going to use a conditional statement which says that if key is equal to equal to Ord Q and then a colon break the loop right and now I'm going to type in here print a which is basically the number of frames Now video dot release and then finally CV2 dot destroy all windows right so I hope you guys have understood the code I'll just quickly take you through it import couple of modules CV2 and time then we are creating a video capture object we are defining a variable equals to one and while the condition is true that is my python is able to read the video capture object we are going to create two variables check and frame where check is a pool type variable frame is nothing but an n-dimensional array which will read the video right after that we are printing it printing the n-dimensional object converting it into a grayscale image we are showing it we are also mentioning that after every one millisecond the new frame should appear and then once I use a press Q then the window will disappear and the control will come out of the loop right and then we are printing this number of frames video dot release we know we know what is destroy all windows so let us see what will happen once we execute this and here we go oops so there is an error so all I have to do is Define this variable key and here we go so let me go ahead and execute this so yep you can see the frame that is behind me and you can see my hand as well so let me just quickly press q and you'll see that window will disappear all right so I hope guys you have understood that how we can actually uh you know capture the video and how we can display it on the window and then things like that so I hope it was clear to you all guys so let me go back to my slides again and now we're going to talk about a motion detector right so this is our use case guys and I know all of you must be waiting for it right so let me just take you through the problem statement why we are executing this use case right everything exists for a reason so let us understand what are the problems that this motion detector will help us to solve so you have been approached by an organization that is studying human behavior now your task is to give them a camera that can detect any Motion in front of it and it should return a graph which should contain for how long the object of the human was in front of the camera so this is how the graph should look like this should be the starting time right the moment object appeared in front of the camera for how long it stayed in front of the camera then it went off then again it appeared at this particular time then again it went off so this is how it should look time at which object appears in front of the camera and the time at which the object moves away from the camera so how we are going to execute it so this is the flow diagram for that guys and first of all we need to save the initial frame so what will happen since I've told you earlier as well video is nothing but fast moving images all right so what we'll do the moment we switch on the camera the first frame the first image that will appear we will save it right then we'll convert that image to a gaussian blur image you don't need to worry about it I'll explain you what is gaussian blur image then we'll take the frames with the object and convert it into gaussian blur image so basically this is done to give us the accurate results right it's gaussian blur image is something like that you don't have to go into too much detail here to execute it but just understand it just to increase the efficiency we are converting our images to gaussian plot so we are converting our first image also to gaussian blur and the subsequent frames as well that will appear will also be converted to gaussian blur image then what we'll do we'll calculate the difference the difference between the first frame and the frames that will appear after the first frame since the first frame is stored already we are going to subtract that with the frames that will appear after that right then what we need is now because of this we know okay there's a change in the frame the initial image looks like this but now since there's some movement or some motion we can see that there's a difference in the subsequent frames right so we know that there is some motion or there is some object but that object might be too small that we don't want to capture that for that what we do we Define a threshold that will remove the shadows and then two small objects or other noises so basically we will Define a threshold or the size of the pixel that should appear as and it will be considered as an object right I don't want a flying housefly to be considered as an object there just an example Now define the borders of the object right so we need to give a border or a box there right and basically a rectangular box around the object and then we are going to calculate the time when object appears and exits the frame I hope you guys are clear again guys I'm repeating the logic what happens you first save the initial image in a frame what happens the moment you switch on the camera the first image that will appear that will be saved that will be converted to a gaussian blur image similarly whatever frames that appear after that initial image will also be converted to gaussian Bar image now the difference between the first frame and the subsequent frames will be calculated now that difference will tell us whether there is an object in front of the camera or not because if there's a change from first frame then there is definitely some object but that object might be too small and I might not want it for that we will Define a threshold all right so this should be the size of the object in order to consider it as an object or an emotion in front of the camera then we are going to define the borders around that object we'll add a rectangular box and then finally we are going to calculate the time when object appears and exits the frame right we'll save that in a data frame and after that we are going to visualize the data frame that's it we are going to create a partner's data frame for that now the question is how I'm going to execute it right so guys this is pretty much from what we did last time as well so what we have here is we are going to create a video capture object then we are converting uh the frame color to grayscale then we are converting the grayscale image to gaussian blur image and this if statement will basically store my first frame or my first image the moment I switch on the camera whatever image comes first it will be stored in this particular first frame variable right so the statement says that if first frame is none when there is nothing in the first frame it will enter the Roop the first image that will appear will be stored in the first frame and after that continue means it will come out of the loop right let's see what we are going to do next after that what we are going to do is we are going to to calculate the difference since I've told you earlier as well we're going to calculate the difference between a first frame and the subsequent frames which I have stored in Gray right so this will calculate the difference after that I have defined a threshold now it provides a threshold value such that it will convert the difference value with less than 30 to Black if the difference is greater than 30 it will convert those pixels to White so basically whatever object is there and from the screen it will convert that to White right whatever the difference is so that's why I have given the color as well and this is basically uh the difference value that I've defined a threshold here similarly I've defined one more threshold right you can see it over here as well thresh Delta that I'm using here after that what I'm doing is I'm defining the Contours or you can say the borders so basically the borders around that object which appears now I'm adding one more threshold guys this will remove noises and shadows or even a house fly which I don't want to detect right so basically it will keep only that part white which has area greater than thousand pixels so if my object has area grade smaller than 1000 pixels it it won't be detected because I don't want to detect it right so you can change it the way you want you can keep it 5000 you can keep it ten thousand you can keep it 200. that totally depends on you right and this is basically to create a rectangular box around the object in the frame so I hope you guys are clear with it and finally we are just displaying all the things and uh yeah you can see it over here as well and you know what it is basically frame will change after every one millisecond this will basically close the window and this will close all the windows now what we need to do is we need to calculate the time for which the object was in front of the camera for that we will create a pandas data frame right so pandas.data frame is what we are going to use here that will have two columns start and end basically when object appears in front of the camera and when it went off and we are going to tweak a couple of things we are going to Define this variable called status status at the beginning of the recording is zero and as the object is not visible right so if there is no movement then it will be zero because there is no object that is there in front of the screen right but it will change the status when the object is being detected the status will become one right similarly we were going to append the status list the status for every frame for every frame we are going to append it so this basically tells us the second last status list value all right these two condition statement will basically record date time in a list when change occurs let me tell you how so if my state is underscore list the last value of this list is actually equal to equal to 1 which means there is a object but my second last valued says that there was no object before in the previous frame that means there is an object which has appeared right so that basically the time at which the object has appeared will be stored here again which will be stored here after that if you notice the second conditional statement which says that my last frame does not have anything uh I mean there's no object in front of it in front of the camera but in my second last frame there was an object that means the object went off right so it will note that time as well when the object was not there in front of the screen that simple guys so this will basically create a pandas data frame that will have two columns start and end which will tell us when the object appeared in front of the camera and when the object went off from the camera right so this is how it is and then what we are going to do is we are going to create a use this for Loop in which we are storing the time values in a data frame right I think you guys are aware of pandas if you aren't aware of pandas I'll leave a link in the description box below which is nothing but a detailed tutorial about pandas and data analysis using it so go through it if you are not aware of pandas it's very important Library I think every pythonist should know about it and then we are going to store it in a times.csv file so it would be a CSV file right so time I N times equals to I plus 1 ignore index they basically we don't have any index values here right we don't want to display the index so there is no index value here this for Loop is pretty self-explanatory so so let me just go ahead and execute this first and then I'll tell you how we have visualized it right so what I'll do is I'll first you know won't come in front of the camera I will go away because it has to First capture the first frame then only it can identify the difference between the subsequent frames right so guys I'll execute it now and what will happen it will first capture the first image which is there in front of the camera and any movement that will happen it will capture that right so first frame will be stored then the difference between the first layer and the subsequent frame will be calculated in order to determine if there is a object in front of the camera or not let me go ahead and execute this so guys you see a plain background there's nothing right so this will be saved the first frame and then let us see uh what will happen if I bring in my hand so it is there's a rectangular box around my hand you can see right so it is basically uh capturing the motion around the camera and all of these things are saved in my pandas data frame all right so you can notice my hand is coming in and then it is going off so all of the the time at which the hand appears in front of the camera and the time at which it went off will be stored in my pandas data frame because I have two columns start in it right so if I just minimize this let me show you the gaussian blur image as well right so yeah in that gaussian blur image I have defined that you know to keep the object white if it is greater than thousand pixels right so this is what comes up right yes this is gaussian blur image which is this is gaussian blood yeah and this is a gray frame which you can see over here let me press Q it will close all the windows now let me show you this times.csv file so Yep this is how it looks now let me go back to my slides again now basically what we are doing is we are going to plot it on the browser using the bokeh library so this is my code that will help us to visualize the graph right so it is basically using the motion detector which I have here right from this file it will take DF the data frame which we have created so let me just go ahead and run this right so again uh I'm just going to put my hand couple of times and I'm going to put my other hand as well yes so yes I think it's a good enough size of the data frame and it should appear in the browser the moment I press Q you'll see that there'll be a graph that will appear on a screen and here we go guys so this is the graph that I was talking about and time start time so let me just increase the size for you guys so yeah this is how the graph will look like start time end time so we have everything here [Music] let's try to understand what exactly is a chatbot a chatbot is basically a computer program which conducts conversation between the user and the computer through auditory or textual methods it works as a real world conversational partner now let's talk about a real world example so that you can relate to it I'm sure most of you people are familiar with the self-learning language app called Duolingo so in the initial stages the users had a problem of not having to implement whatever they have learned in a real-time conversation to overcome this problem the app came up with an interactive chat bot which was actually a blessing in disguise because after this you could actually have a conversation with the bot which eventually helped you a lot in the learning now this may sound very shocking but chatbots are not something that are very new to us the first of its kind came in 1966 called Eliza it was made by Joseph wesenbaum but for obvious reasons it wasn't something that we are familiar with today like Siri Cortana or Alexa Etc now talking about evolution of chatbots it took a very drastic turn during these decades and the future also looks very promising as well chatbots are expected to complete around 80 percent of all the work in the coming decades but for now the chatbots are almost completing 30 percent of the task which is a lot actually now with the increasing demand it has become very important to learn machine learning and artificial intelligence and let me tell you guys it's not the time to take a backseat and if you miss out on this wonderful opportunity we will be watching the progress on Sidelines and I'm sure you don't want to do that so start learning machine learning and you can actually learn machine learning from scratch on edureka to become a leader today now coming back on the topic guys the tasks that are completed by the chatbots include like simple tasks like giving out information or booking tickets Etc we can even categorize these chatbots into self learning and rule-based chat boards so talking about self-learning chatbots self-learning chatbots use machine learning and AI algorithms to save the inputs they get from the users and use them later while the rule-based Bots actually follow certain rules on which the responses are based so now that we know what a chatbot is let's take a look at the chat about library in Python so whenever we are making a chat bot we have to consider a few things like we must know the target audience and we must understand the natural language of communication as well also we have to provide the responses that are required for the target audience so coming on to chat about guys chat about is a library in Python which generates responses to user input it uses a number of machine learning algorithms to produce a variety of responses and it becomes easier for the users to make chatbot using the chat about library with more accurate responses the design of the chatterbot is such that it allows a bot to be trained in multiple languages which includes Spanish you know there is English and there are a lot of regional languages as well on top of this the machine learning algorithms that we use make it very easier for the bot to improve on its own using the user's input chat about makes it easy to create software that engages in conversation and every time a chatbot gets an input from the user it saves the input and the response which helps the chatbot with no initial knowledge to evolve using the collected responses with the increased responses the accuracy is going to be increased and the program also selects the closest matching response from the closest matching statement that matches the input it chooses the response from the known selection of statements from the response I'm sure guys this is very confusing for you but you will understand this better when we are working on the use case so hang in there and we must also consider the limitations that a chatbot has to overcome in order to be accurate with increasing advancements there also comes a point where it becomes fairly difficult to work with the chat Bots following us a few limitations that I want you to know guys so first one is domain knowledge since true artificial intelligence is still Out Of Reach it's only seen in the movies and the books we still have to go there guys it becomes fairly difficult for any chatbot to completely pay them the conversational boundaries when it comes to conversing with a human you know the conversation between two humans and a computer and a human is going to be very different because we still have to reach that level of intelligence you know and that is one limitation that we come across when we are working with the chatbot and talking about the next one is personality so not being able to respond correctly and fairly poor comprehension skills has been more than frequent errors of any chatbot so adding a personality to a chat bot is still a benchmark that seems kind of far away but we are more than hopeful with the existing Innovations and progress driven approaches so now let's try to understand how we can install chat about in Python guys and chat about comes with a data utility module that can be used to train the chatbots and at the moment there is training data for more than a dozen languages in this module so I was telling you that we can work on different languages while working on chat about guys so as of now there are more than dozen languages that we can have for chat about module in Python days so let's take a look at how chatterbot actually works so first of all it will get the input from the user and then it will process the input and return the value that is generated with the highest confidence that is actually you know the matching of the statement the most relevant statement is going to be processed and then you will get the response from the input so this is our chat about in Python actually works guys so I'll show you how you can install chat about as well now talking about training the chatbot so you have made a chatbot how is it going to produce the responses that you want so for that there is a library called chat about Corpus so it basically has all these responses that you can actually have while you're working on chatbot or you know trying to talk to the chat bot so this is the library that is going to help you so we can use the chat about Corpus train the bot and then we can have the responses that we want the relevant responses so you'll see in the use case when I'm working on it guys so whatever data we are getting for the response is it's coming out from the chat about Corpus Library guys so now that we know how we can train the chatbot as well so we will see how we can install all these libraries in pycharm guys when we are working on the use case and talking about the use case let's talk about a flash chatbot that we are going to make now so what exactly is a flask guys flask is a micro framework so first of all what we are going to do is we will set up a flask app and using the flask framework that is and if you are not familiar with the Frameworks in Python check out the detailed tutorials on Frameworks like flask Django Etc on edureka after we are done creating the app we will make two more directories for the you know the templates and the CSS which is going to have the HTML and CSS files guys so we will train the bot and then we get the responses when we are making the conversation with chatbot in the flask app guys so let's take it up to python guys and we'll make a flash chat about from there so we are in pycharm now guys and let me tell you a few things before we begin with the project guys I have already made the style and index that is my HTML file so my template is ready guys I'll show you the code from the beginning for the app and I want to tell you one more thing so when you are making this flask bot what you have to do is you have to install four libraries so I'll show you what all libraries are there so you go to the project interpreter as you can see there is floss Ginger 2 and we have chat about Corpus so you have to keep in mind what version you are actually installing so while I was working on this chat about Library the most persistent error that I was getting was I was not able to actually install all these libraries for this project so what was happening is actually the reason was I was not using the version that is suitable for this I was actually using the latest version so I was getting those errors guys so make sure you actually install the versions that are compatible here so let me show you guys what all errors I was getting while installing all these files so I'll make a new project guys for you and let's name it as demo base okay not demo let's make it as chatbot one and we'll take a new window so it's actually creating content environment so we'll change that as well so it's gonna take a while so I just want to tell you guys you have to check for the requirements that you have for all these libraries that I was telling you about like flask and then there is chat about then chat about Corpus and SQL Alchemy you have to specify the correct version to actually work here otherwise you will get an error so I'm going to show you that error but I was getting while installing all these libraries guys so it's going to take a while so meanwhile I just want to tell you guys chatbot is something which is not very new to us it has come a long way before in 1966 we have encountered the first chatbot but we have come a long way from that and right now we have chatbots like Alexa and then uh make conversations and then we can also book tickets for example and then we can get information from the web as well and using that Google Assistant we can actually play music as well so this is the combination of machine learning and AI that we have using these chatbots and in the future it's going to go beyond that so we you don't have to sit on the sidelines I mean you don't want to be on the sidelines because this is a booming industry and there's a lot of jobs created for this purpose and I also told you that we're gonna have okay we already have these libraries installed here okay let me check guys if I can still install it using the terminal so I'll just write pip install chat bot because I'm seeing that Library already there in the project interpreter I don't think I'm gonna get any errors but let me tell you guys if you don't have already installed chatterbot the correct version that is 1.0.0 and if you're trying to install it on pipe using the Pi Pi you know chat about documentation you're gonna have to install it with the correct version otherwise you'll be getting a lot of Errors like open SSL and all those things and using different environment as well you'll be able to install those libraries like I did for conda environment I used a conda environment for a while I was able to install those libraries but it wasn't working for me because there are a few modules that you need separately and for that I just want to tell you guys by installing all these libraries you have to keep in mind the correct version that is required for this project so for chat about it is 1.0.0 the version and I'll tell you about the chat about Corpus also and for chat about Corpus it's 1.2.0 and for SQL Alchemy it's 1.2 so keep in mind about all these versions and you will find no difficulty in downloading or installing all these libraries that we need for the project so let me go to the project again guys and we'll start from the beginning and I will tell you what all we have inside this flash chat about and in the end we'll be having a chat up bot which we can use for making conversations using the Corpus or the chat about coppers that we are going to use to train the chatbot here I'm sure you guys must be having difficulties understanding this because it's a very New Concept and it's not very complicated guys and let me tell you because we are using flask flask is a micro framework and it is very easy to use you just have one python file this is my python file where I'll be writing the code for that and we have a style CSS that will be the static directory in which we have the style CSS and this is my template guys so to tell you how the template looks like let me just copy this code and I'll make a new file here okay I'll just go to the desktop and let me okay just take this one for this I will just copy the code here save it as say chatbot template dot HTML and you will be able to see the template that I've made guys so I'll just type chat about template the file that I just made guys it's not showing me any results guys so I'll just directly go to desktop and over here they did it this myhtml file so this is going to be my template guys the HTML file that I have made so it has a heading then there is the text for Chatterbox and this is a text message which I'm going to send like hi I am Wasim whatever I'll just send it and it will get a response from the chat board and for better visuals or the style sheet so as you can see I'm getting this error which I was not getting before in the project that I have so for this purpose I'll show you how you can use the correct version guys so I'll go to this type chat about here here it is so I'll specify the version as well okay so right now it has so many versions so we are going to be using 1.0.0 so you will see that I will not get any errors while installing this version because this is the correct version that is going to support the flask framework here that we are using and similarly for chat about coppers also we'll have the specific version that we want to use so this is how you should start your project guys so I'll just close this one and let's go to pycharm and we'll make the flash chat about now so we have to install flask library then there is chatbot then chat about Corpus as well so these are the libraries that you have to install and SQL Alchemy if that is not coming here so you have to install SQL Alchemy as well so we'll start from the beginning guys so I have uh app.poi file so in this I'm going to make the flask app guys and I have two more directories that is static and templates guys so templates actually have my HTML file and static has my style.css file so let's start from the beginning guys first of all we have to install flask guys so from flask I will import flask and there's one more thing we have to do that is render template and request as well now we have to install chatterbot as well so from chatbot we will install I mean I'm sorry guys we have to import chatbot and from chatbot.train so we have to import chat about copies guys so this is what we are going to use to train our chat about guys so I have to name my app as well so I'll name my app as let's say flask so this is my app I have created an App here so now we begin with the bot guys so I will name my board as English bot and use chat okay I'm sorry use the chat bot here and in here I'll name it as chat bot and Storage give it storage adapter is equal to chatbot Dot storage dot SQL storage adapter if you want the code after the session ends guys please mention our email ID so that we can easily find you guys and give you the code as well so don't worry it's a very easy process guys and you can actually find the same code I mean similar looking code on the official documentation of chatbot as well so you will see how easy it is to actually make a chat about app using flask guys because flask is a micro framework as we all know and talking about Frameworks guys there is Django as well but for Django it becomes very complicated but it has its own advantages because it is a full stack framework guys but for this we can actually use flask as well because so it's going to make our task very easier because we don't have to write so much in this so now we have trained the data as well so I'll just give the root and over here I'll just mention a backslash yes and now I'll Define a let's say a function home so it is actually returning the template I'm using render template and in this I want to mention the template that I have made over here which is index.html so I'll just mention it here guys now we'll make one more URL and that is about root okay I'm sorry guys there's an arrow app dot root and over here I'll just write backslash get and we will make one more function which is for the bot responses case user text okay this user text why I'm writing this I'll tell you guys so you will understand this better request Dot arguments dot get message yes and we want to return string which is for English bot dot get response and if over here we will have user text so now we are done guys this is what you have to write to make a chat about I mean this is the easiest way you can do this guys and if you are familiar with the flask framework guys it's going to be very easy for you to make this the only difficulty that you will have for using this flash chatbot in pycharm is while installing the libraries guys because while I was working on chatbot in pycharm I was having a fairly difficult time installing these libraries but then I checked the requirement that was on the official documentation and I checked for the versions that I have to install and it was done in no time guys so I'll tell you what user text normally is here so this is my style sheet guys CSS so over here you can see the different IDs that is chat box bot text then there is text input user input all these things and this is my script guys which is using the bot response so I will have this using the nltk so we're good to go I will just run this server okay we are not getting any errors so it should be good we'll wait for a while and it will give us the address where we will be running this code of ours okay as you can see it's downloading the package all these packages it is training the AI Dot yml so we have bought profile computers conversation emotion food gossip greetings Health money movies politics there is sports as well so we have a server ready guys I'll just click here so as you can see we are in the chatbot app guys so I'll just write hi first of all let's see what the response will be okay he's asking me how am I doing so I'll just write I am doing good okay I made a spelling mistake let's see what brings us the response after this okay I'll ask him what is soccer so if you have noticed let's see what he is going to tell us okay he's saying a game played with a round ball by two teams of 11 players on a field with a goal at either end so yeah he's basically giving us the definition of what is actually soccer guys so now let's ask him one more thing let's say I'm asking him what is AI so he's gonna give me some answer for this if it's there so this guy is saying artificial intelligence is the branch of engineering and science devoted to constructing machines that think okay I'm gonna ask him what languages do you speak let's see what all languages this part can actually speak and what is the response he's gonna give us okay he's saying python because we have actually made him in Python guys okay let me ask him one more thing again I'm asking can you feel let's see what the response will be okay he's saying maybe I can I'm a fairly sophisticated piece of software so as you can see guys this conversation it's going somewhere and it's actually asking me what is AI so this is a flask chat about we have actually made using the chat about Corpus library that we have used to train the chatbot here so as you can see there are a few limitations to this so if I'm giving him some gibberish okay so let's say I'm just telling him my name let's see what the output will be or the response that he's going to give us see so this is the limitation I'm talking about guys so whenever I'm conversing with a real person it's not going to be the ambiguity that I'm facing right here because if I'm giving him any gibberish questions or answers he's just coming up with this what is AI thing so I think this is something we have to work on while working on python guys because we have come so far with the technology and the Innovations the approach that we are taking for these artificial intelligence and machine learning things or the algorithm that we have come across so far it's gonna take a very long time not very long time it's going to take a while to actually get the hold of it in the chatterbots thing and if you want a real-time artificial intelligence chatbot you can actually use tensorflow [Music] the map filter and reduce functions are inbuilt functions of python these functions enable the functional programming aspect of python in functional programming the arguments passed are the only factors that decide upon the output these functions can take any other function as a parameter and can be supplied to other functions as parameter as well the map function takes another function as a parameter along with the sequence of iterables and returns an output after applying the function to each of the iterable that's present in the sequence the filter function as the name says is used to create an output list consisting of values for which the function returns true the reduce function on the other hand applies a given function to the iterables and returns a single value okay so now let's move on and take a deeper look at each of these functions and how you can actually write them in Python like I've already mentioned before the map function will take a function and a sequence as a parameter and it applies that function to the iterables that are supplied to it as you all can see on the screen I have a map function to which I've supplied a Lambda function as a parameter and a list of iterables all I want to do over here is ADD 3 to each of the iterables that are present in this list as you all can see I'll have an output list with 3 added to each iterable that's present in the original list so now let's move on towards our Jupiter notebook and see how this works okay so I'll just create a heading over here okay so before I begin writing this function let me just write the syntax first I'll just write it as a comment so basically the map function takes a function and a set of iterables as a parameter now these iterables can be either a set Tuple or anything here the function defines an expression that is in turn applied to the it ripples the map function can take user defined as well as Lambda functions as a parameter let us first take a user-defined function as a parameter to the map function so I'll just create a function of here so I'll use the keyword def okay so I'll just name my function as new and I'll pass a parameter as a to this and I just want to return a star a now I'll take a variable as X and then I'll use the bump function I'll take a variable X which will be my map object and then I'll write the map function to which I'll pass the new function as a parameter and the list of it dribbles all I'm going to do after this is print X just to show you all that this is a map object and then I'll print the list of x so as you all can see on the screen X is basically the map object and the list of X consists of all values multiplied to itself so I hope everyone's clear with this instead of using the list function you can also use other functions such as Tuple sets Etc let me show you what happens when I use Tuple as a function over here so as you can see previously I had an output list and now I have an output Tuple similarly for the input it rebuilds you can supply anything such as lists tuples sets Etc okay now let's try to take a Lambda function along with the map function map function can take more than one list of parameters as well let's see what happens when we try to do that so I'll just copy this over here and I'll paste it and I'll pass another list as a parameter and I'll make a small change to the function over here so I'll pass two parameters so I'll say a comma B and I'll say a star B now I'll hit run okay so as you all can see on the screen these values present in the iterables have been multiplied and I have a resultant Tuple so I hope everyone's clear till here if you have any doubts please do let me know in the chat box and my team is here to help you so now let's see how to use Lambda functions along with the map function Lambda functions as we all know are functions that do not have any name they are also known as Anonymous or nameless functions these functions are generally provided as an input to other functions so now let's move on and see how to use Lambda functions along with the map function okay so I'll just take a list over here say LSD and then I'll pass some random values to it I'll take a new variable say why and then I just want to print the list of and then I'll use the map function along with the Lambda function Lambda functions are single line functions so we have to provide the expression along with the function itself so I'll just say X plus 3 and then I'll pass the parameter as LSD which is my list and then I'll just print this okay so as you all can see on the screen 3 has been added to all the elements that are present within this list over here now let's get back to our presentation and see how the filter function works like I've already mentioned before the filter function is used to create an output list consisting of values for which the function returns true so as you all can see on the screen I have a small example with a condition where I want to print the values which are greater than or equal to 3 where I want to print the values which are greater than or equal to 3 within this list present over here so now let's jump on to our Jupiter notebook and see how the filter function works I'll just create a new heading over here the syntax of this function is similar to the map function so basically it takes a function and the set of it Rebels as a parameter okay just like the map function filter function can be used along with user defined as well as Lambda functions so first let us take a user defined function as a parameter to this so I'll just Define a function over here so I'll say new one and I'll pass a parameter say I now if I is greater than or equal to 3 I want to print the value now I'll take another variable J which is going to be my filter object and then I'll use the filter function and I'll pass new 1 as the parameter and I'll Supply a tuple of random iterables to this and then I'll just print out what is J okay so as you all can see over here I just printed J to which the output says that this is a filter object now if I want to return the values that are present within J I just have to use print Tuple of J okay as you all can see all the values that return true for this condition has been returned as the output of this function so now let's try to use the filter function along with Lambda functions I'll take a new variable say Z and then I'll use the filter function to which I'll Supply a Lambda function as a parameter so I'll say Lambda X where X is greater than or equal to 3 sorry I have to put this within brackets and then I'll pass the same Tuple as a parameter I'll just copy this this time I'll print out the list of C okay as you all can see the output has been returned just make a note over here guys that last time I wanted my output to be a tuple and this time I just said print list of C so I have a list as the output over here okay so I hope everyone's clear till this now let's get back to our presentation and take a look at the reduce function the reduce function as the name describes applies a given function to the iterables and returns a single value so here I have a small example wherein I want to add all the values of these iterables and return one single final value so in the first iteration I'll have 1 as X and 2 as Y and then I'm going to add X Plus Y which is 1 plus 2 and it gives me 3. after the first step this output 3 becomes X and this input 3 over here becomes Y and then I get an output as 6. this process Will Go On Till all the values present in the iterables have been reduced to a single value now let's jump on to our Jupiter notebook and see how this works so I'll just create a heading just like before I'll write the syntax first so reduce it takes the function as a parameter and a set of interibles now one thing you'll have to note over here is to import the radius function from the funk tools module so all I'm going to do is say from Funk tools import reduce sorry I've just made a spelling mistake over here just like filter and map you can use reduce functions also with user defined as well as Lambda functions so first we'll take a user defined function say a to which I'll pass the parameters as X and Y within this I'll just say return X Plus y now I'll just take a new variable say s and I'll use the radius function to which I'll pass a as the parameter and I'll pass a list of values and then I'll just print s oh sorry guys I've added an extra comma here and now I hit run as you all can see all the values have been added to each other and I have the output as 49. now let's try to use the reduce function with Lambda functions I'll use the radius function directly over here and then I'll use the Lambda function within this I'll pass parameters and X and Y or this time I'll change the names I'll say q and P and then I'll just use Q star p and I'll pass a list of values to this and then I'll hit run okay so as you all can see all the values that are present in this list have been multiplied to each other and I have the corresponding output okay I hope everyone's clear till here 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 get back to our presentation and see how you can use these functions along with each other so first we'll be using the filter function within the map function when I use the filter function along with the map function it filters out the values that satisfy the condition which is present within the filter function and IT supplies that output list as a parameter to the map function so as you all can see on the screen I have a filter function which says I want to filter out the values that are greater than or equal to 3 from the Tuple of values that are supplied as a parameter to this I'll have 3 and 4 as the output of this function which will be supplied as the input to the map function okay so now let's move on to our jupyter notebook and see how this actually works so I'll just take a variable say c and then I'll use the map function to which I'll Supply a Lambda function first as a parameter so I'll say Lambda X I'll just say X Plus X you can specify any expression of your choice and then I'll use the filter function to which I'll Supply another Lambda function as a parameter and as a condition I'll just say x greater than or equal to 4. I'll pass a list of values to this say 2 3 4 5. and then I'll just print the Tuple of C so as you all can see the first thing that happened was filtering out the values that are greater than or equal to 4 present within this list after that 4 and 5 which is the output of the filter function will be supplied as an input to the map function and then using the map function I'll add 4 plus 4 and 5 plus 5 which is 8 and 10 respectively okay now we've used the filter function within the map function so now let's move on and see how you can actually use the map function within the filter function so when we use the map function within the filter function the set of iterables that are supplied as an input will be operated upon first by the map function and the output of this map function will be supplied as the input list to this filter function so now let's get back to our Jupiter notebook and see how this works so I'll just take a new variable say d and then I'll use the filter function I'll use the same condition again so I'll just copy this and then I'll use the map function within the filter function and I'll use Lambda within the smart function so I'll just say X Plus X and I'll pass a list of values then I'll just print the Tuple of D or this time I'll just say set of d so as you all can see on the screen I have an output set which is a resultant of applying math function to the given set of iterables and then applying this condition within the filter function to the output of the map function so now let's move on towards the final topic of this session wherein we'll be using map and filter within the radius function okay so as you all can see on the screen I have a small example wherein I have a radius function to which I've supplied map and filter as the input functions now the innermost function is the filter function which says I want all the values that are greater than or equal to 3 from the list of iterables that are supplied to it so as an output of the filter function I'll have 3 and 4. to this output list of the filter function the math function produces a new list of iterables which is 6 and 8 which are obtained after adding each of these values to itself and finally 6 and 8 will be supplied to the reduce function as a parameter so now let's get back to our Jupiter notebook and do the same so I'll just say map and filter within reduce okay so I'll just take a new variable say r and then I'll use the reduce function to which I'm going to supply a Lambda function as a parameter so I'll just say Lambda X comma y and then I'll just say X Plus y now I'll take the map function to which I'll Supply another Lambda function as a parameter and then I'll just say X Plus X after this I'll be using the filter function and again I'll use a Lambda function within this filter function I'll say X I want to return all the values that are less than or equal to 4. to this I'll pass a list of iterables say 1 2 3 4 5 6 7. and then I'll just print out r oh sorry guys I've made a small mistake over here okay so now let me hit run okay as you all can see on the screen first my filter function will filter out all the values that are less than or equal to 4. the output of this is supplied to the map function and all the values are added to each other finally the output list of the map function will be supplied as an input to the reduce function and all the integers will be added to each other and I'll have one output which is 20. okay so I hope everyone's clear foreign [Music] as we all know is a data visualization Library available in Python it's based on matplotlib and allows creation of statistical Graphics it also has a number of functionalities such as an API that is based on data sets allowing comparison between multiple variables it also supports multi-plot grids that in turn ease building complex visualizations it has univariate as well as bivariate visualizations available to compare between subsets of data the availability of different color palettes to reveal various kinds of patterns it also estimates and plots linear regression automatically so now if you were wondering why you see Bond when you already have matplotlib here is the answer to it Michael vascom who is the creator of seabon says if matplotlip tries to make easy things easy and hard things possible seabon tries to make a well-defined set of hot things easy too factually matplotlip is good but c-bond is better there are basically two shortcomings of matplotlip that seabon fixes matplotlib can be personalized but it's difficult to figure out what settings are required to make plots more attractive on the other hand c bond comes with numerous customized themes and high level interfaces to solve this issue when working with pandas matplotlab doesn't serve well when it comes to dealing with data frames while c-bond functions actually work on data frames now that you have an idea about c-bond let's move on to see how you can actually install this Library to install c bond you can use the simple pip install C1 command or if you are using an anaconda platform you can just go to the Anaconda prompt and type conda install c bond since I already have it installed over here I'm not going to redo it again now let's get back to our presentation and take a look at the dependencies of C1 C1 basically has four mandatory dependencies which is numpy sci-fi matplotlip and the pandas Library it also has a recommended dependency which is the stats models library of python so just like we've installed C1 we'll have to install numpy sci-fi matte plot lib and the pandas library to install these libraries you can use the same commands which I showed earlier with the name of that particular Library okay so I hope everyone's clear till here if you have any doubts please do let me know in the chat box and my team is here to help you so now let's move on and take a look at the various plotting functions in cbon C1 has a number of functions that are available for visualizing statistical relationships plotting with categorical data visualizing the distribution of a data set let's begin with visualizing statistical relationships the process of understanding relationships between variables of a data set and how these relationships in turn depend on other variables is known as statistical analysis in today's session I'll be using the Rel plot function to plot various statistical relationships Rel plot is a figure level function that makes use of two other access functions for visualizing statistical relationships these access level functions are scatter plot and line plot which can be specified using the kind parameter of the Rel plot to explain this in detail let me jump on to my Jupiter notebook from here I'll just open a new python notebook I hope everyone's familiar with Jupiter okay I'll just rename this to Python c-bond tutorial I'll just say python C1 and I'll rename this before we begin data visualization with c bond we'll have to import c bond along with its dependencies so the first thing I am going to import is numpy import numpy as NP then I'll import pandas as PD import matplotlib Dot py Plot as PLT and then I'll import C bone import c bond sorry for the spelling mistake guys this is import C bone import C1 as SNS and I'll just execute this like I've already told you all before you can load any data set present on the GitHub and make use of it with c bond c bond also allows you to load any data set from git using the load data set function so let me just load the flights data set so I'll just say sns.load underscore data set and I'll specify the name which is flights okay so now let me just go to the GitHub website and open the flights data set over here as you can see there are a number of data sets that are available which you can use along with c bond so let me just open the flights data set and scroll down so as you can see over here there are three columns for the year month and the number of passengers so I'll just come back to my Jupiter notebook and I'll use sns.plot and I'll specify what I want my x-axis to be so I'll just give x-axis as the number of passengers and to the y-axis I'll give month as the parameter and for the data I'll use a which is the variable wherein I'm loading the data set and I'll execute this okay so I hope you're clear with this so as you can see over here the y-axis has month and the x-axis has the number of passengers these points are plotted in two dimensions to add another dimension you can add the Hue semantic so I'll just copy this Ctrl C and I'll paste it over here and I'll give another parameter over here which is Hue and I'll say that is year to be the Hue semantic and I'll hit run so I hope you see the difference over here the year has been marked as the Hue semantic month which is in the y-axis and the number of passengers which is on the x-axis so as you can see on the screen this is nothing but the scatter plot so in case you want a line plot all you have to do is specify the kind parameter of Rel plot with the keyword line so let me just load another data set over here so I'll say B is equal to SNS dot load underscore data set and I'll load the tips data set this time after this I'll use sns.rel plot I'll specify the x-axis to be the time and y-axis to be the tip data is nothing but B and kind is line now let me hit run okay so I hope you're 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 get back to our presentation and see how you can plot categorical data in c bond this approach basically comes into picture when a main variable is divided into discrete groups to achieve this we'll be using the cat plot function available in c bond like Rel plot cat plot is also a figure level function characterized by three families of access level functions which are Scatter Plots distribution plots or estimate plots okay now let me jump onto my Jupiter notebook and show you all a few examples regarding this okay so here I'll use the same tips data set and since I've already stored it in the variable B I'll just use sns.cat plot and I'll specify the x axis to be the day y-axis as the total bill and data is B now let me hit run okay as you can see over here this is the scatter plot that we've got using the cat plot function scatter plot is basically the default type of cat plot however you can change the kind of plot you want by specifying the kind parameter like we've done previously you can choose between any options which is strip plot swarm plot box plot violin plot Etc let me just show you what happens when I change the kind parameter of this cat plot so I'll just copy this and I'll paste it over here and I'll specify kind to be as violent plot so I'll specify the keyword violin and I'll hit run okay so I hope you see the difference this is our scatter plot and this is our violin plot so if I change the kind parameter our plot will change correspondingly let me just give the type as box in now okay so I hope you're seeing the difference guys so now let's get back to our presentation and see how to visualize the distribution of a data set visualizing data set basically deals with understanding data sets with context to being univariate or bivariate okay so now let's get back to our Jupiter notebook and see how you can actually plot univariate and bivariate distributions so before doing this I'll just import stats from the scipy library so from sci-fi import stats and I'll hit run okay so first let us plot univariate distributions to plot univariate distributions you can use the dist plot function so let me just take an example C is equal to NP dot random I'll just use random.normal functions to draw random samples from a normal distribution so I'll just give the corresponding parameters to it you can give any parameters of your choice I'll say size equal to 100 and scale I'll specify the scale to be S2 and I'll hit run I did not specify the dist plot function so I'll use SNS Dot dist plot and I'll pass the data to it as C okay so this is how the dist plot function works so this is an example of a univariate distribution so now let's move on and see how a bivariate graph is going to look to plot bivariate distributions you can make use of the joint plot function I already have a small example typed over here so I'll just copy paste that and I'll show you all how it actually looks okay so this is basically a bivariate distribution so I hope you see the difference this was our univariate distribution and this is our bivariate distribution okay if you have any doubt please do let me know in the chat box and my team is here to help you moving on Python's Evon also allows you to plot multiple grids side by side these are basically plots or graphs that are plotted using the same scale and access to eight comparison between them this in turn helps the programmer to differentiate between the plots and obtain large amounts of information to plot multiple graphs you can either use the facet grid function or the pair grid function of C Bond let me jump on to my Jupiter notebook and show you all an example of each of these so first I'll be using the facet plot function okay so here I'll load another data set so I'll just say a is equal to SNS Dot load data set and this time I'll load the iris data set and I'll say B is equal to SNS Dot facet grid which is the name of our function and I'll specify a when wherein our data is present and I'll specify the column to be the species I'll specify the column as the species of flowers and then I'll just use the map function and I'll specify all of them to be as histograms and I'll give the parameter as simple length now let me hit run oh sorry I made a spelling mistake over here it's capital F okay so I hope you're clear with this I basically have three graphs that are available for the three species of flowers which is setosa versicolor and virginica as you all can see it becomes very easy to differentiate between them you can also make use of the paragraph function when you have a pair of variables to compare let me show you a small example of that as well so here I'll just say a is equal to load underscore data set and unload the flight status set and then I'll specify b as SNS dot pair plot pair grip sorry and I'll specify the data to this this as a and I'll use the map function again to plot them this time I'll use scatter plot so plt.scatter sorry guys I forgot to specify SNS over here SNS dot load underscore data set okay so I hope you're clear with this so as you can see the output clearly Compares between the year and the number of passengers in different ways c bond also allows you to make your plots more attractive and delightful so till now as you've been seeing I've just had a white background for all my plots so in case I want to change this or if I want to change the color palettes that are available in My Graph I can do so easily let me just jump on to my Jupiter notebook to show you all how to do this so here I'll just copy paste the previous example I'll just copy this and I'll paste it over here and to this I'll just use another function which is the set function and I'll specify the style the style of my plot to be dark grid and I'll hit run I hope you see the difference between the two here I have a white background whereas when I specify dark grid I have a different background with dark grids that are seen clearly you can change the style parameter to any of the available themes which is dark grid dark white Etc so if I just specify dark and I hit run you can see that the grids have vanished but the background is dark not just this guys you can make many other changes to your graphs such as adding or removing access the colors that are available Etc now let me just show y'all how you can remove the axis lines that are present in your graphs I'll take another example of here so sns.set I'll set the background this time as white I'll say Style equal to White and I'll specify the color code parameter to be true I'll load the tips data set again sns.load data set and I'll load the tips data set then I'll use a boxing plot or I've used the boxing plot so this time I'll use the box plot function and I'll specify x-axis to be the days and sorry it's day not days and the y-axis to be the total bill and data is nothing but a oh sorry guys I've made a spelling mistake over here it's color codes okay so here as you can see my plot has access all around it now if I want to remove any of these access all I can do is use the display function so I'll just copy paste this and I'll use the display function I'll specify the offset parameter to be 10 and I'll specify trim parameter as true and I'll hit run okay so I hope you see the difference so as you can see over here I've specified the offset to be 10 I do not have the access that are present before that c bond also provides a number of color palettes and to check the color palettes that are available in C1 you can just use the color palette function so I'll just use C is equal to S and S dot color palette and this time I'll use the pal plot function and the data is C okay so as you can see these are the color palettes that are currently available in c bond for deeper variations you can use other functions such as the hls palette husl palette Etc foreign [Music] 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 you want as a takeaway 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 notable what is immutable so you've got a couple of questions like that okay all right so Roma you basically come as a Java developer you are 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 would 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 a reference pointers you know uh it copies just the top level values it doesn't copy each and everything and create a new object so there would be a references okay for example okay I have L1 is equal to one two three four okay this is a particular list okay I have a list L one one two three four 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 L1 and L2 so if I even do L2 dot 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 a module named as copy so the channel copy works this way so I've got L1 okay I've got L three okay I'll create L3 copy dot copy of L1 okay if you look at L3 it is the same as what we have in L1 now L3 dot 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 is 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 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 has happened but you know the admin structures in copy uh so the admin structures of L1 are not copied to L3 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 person this is what you need to say okay anyone so basically your Shadow copy copies all the reference pointers all the work 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 clear-cut copies for each and everything that does not happen in Shallow copy please remember that does not happen in Shadow copy Okay so so deep puffy gets deeper and deeper and when you take you know what 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 because it has to copy all the references all the admin structures all the values each and everything whereas Shiloh 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 the Deep copy yeah so the example for deep copy would be something like this okay L4 copy dot 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 altogether okay so this is how you create a shallow copy and this is how you create a date 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 dot 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 a two group 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 one two three these are all lists animals marks and commands are all lists and the tuples are the one within these lists so let's go back here let's create X or L1 again two three four okay so here's a difference okay list are data structures which can be modified at runtime whereas your tuples cannot be modified at runtime okay we have list methods which allow us to do that for example list dot append of 10 will allow you to do it this happens at runtime okay but we cannot do that to a tuple okay we cannot do that whatever we will see that you know it does not have such attributes so you might wonder why do you need two parts 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 in to you okay or coming into your server or coming into 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 dot L1 dot remove of three and you'll you see that Row 3 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 pupils where you are thinking about buffers when you're thinking about Q use 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 Y and 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 have got fixed columns okay columns with ID column with name 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 tuple when you don't know how many rows you have you go for an up particular list that is how it has been you know it is structured even all the apis MySQL postgres equal sqlite are all structured this way okay where the columns are represented by pupils and the lists are represented by a 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 lists and tuples and there is one more thing okay you might get a question like this okay say L1 is equal to one two one three four 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 you will 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 one two three okay so you've got L1 and you've got L2 okay will you be able to modify this Tuple could you 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 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 tuple 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 multi-processing package there are two different concepts threads are related to in a specifically threads and multiprocessing is specifically related to the number of cores which you have on your system okay now python has something called a gin Global interpreter log this is a question which comes up okay typically when it comes to writing this is a question which comes up as to what is a Gil 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 Gil it completes the work and moves on to the next thread so if you are talking about multi-threading this is what typically happens okay you have created multi-threads using the threading package okay what happens is 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 hack if you might see you can however turn off the Gil over here okay it mimics okay so it does not happen parallely it mimics parallelism is what I would say okay it may mimics patriotism but there is a way to do it partially 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 Gin 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 setting package as such so when you are turning off your Gil you have to make sure that you write your semaphores your mutexes 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 CRC plus plus 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 made sense to use something like you know threading okay is that clear to everyone okay beside multi-threading you can make use of multi-processing where multi-processing takes advantage of the number of codes you have so if you say I have got four core processor you can make use of all those four cores and run parallel jobs on all those okay so you've got something called multi-processing for that okay Library code multiprocessing for threading you have something called ready everyone I want clear okay great now basic python questions when it comes to ternary operation so I wouldn't call it exact returning it behaves like ternary operation okay so where you know you have conditions 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 23 comma 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 say you 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 ternary operation great now what is monkey patching in Python this is something which appears when you have gotten into classes as such okay now 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 runtime 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 the use of monkey patching so please remember to 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 has only Legacy architecture which makes use of course type classes news type classes where the classes inherit from objects as object parent class as such depth self okay I am creating a particular you know function print so it says that you know just print this in case you are invoking me now okay I will import something like M so typically I would import it from a separate python script as such over here I'll show it to you directly in practice what exactly you are going to do okay monkey self print on key 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 are going to do is my class so if you do my class dot f now let me instantiate this class first X is equal to my class x dot f okay so you see that you know f is printed over here okay now my class dot f okay so F becomes a function attribute of this particular class okay now is equal to Monkey underscore F so I am forcing this onto this particular function okay now what happens I will create an object obj okay which is my class okay obj dot f will give you monkey FD 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 runtime 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 you want to patch into this particular function of this particular class do you follow me so far Roma okay now what you are going to do so initially if you are calling directly from that particular class my X is equal to my class x dot 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 dot f is equal to Monkey underscore F where f is a 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 itself is basically for the you know similar to a country 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 module in Python called random from random import Shuffle okay you've got a whole lot of stuff within Randle but you're 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 know 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 one four two ten five are a string you know set of strings a 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 to 1 4 10 5 so 1 4 2 and 5. okay so one particular method is doing this now you have a string so if you do L1 dot sort so let's do this okay now L1 got sort will give you something like this okay it is not the right thing that is not right at all okay and you wouldn't want something like this okay what you want is one two four five ten so when you do a sort like this it applies on the ASCII characters I said so it applies on the first found ASCII 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 L one is equal to end of I or i n you might be wondering what this is so and L1 dot 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 or which can you know Traverse over a particular list quickly and no shorten your code you create a list in place itself okay which means to say for I and 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 end of I for I in L1 first we change all the items into an integer and then we sort it in place I 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 dot sort not enter dot sort let's see first we change 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 end comma L2 and L2 is equal to one four two four and one l two dot 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 items does that answer all your questions that you have in mind from a mother 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 T1 is equal to a b c D and E2 is equal to one two three four five 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 pupils which are zipped together A B C D E when you do date of zip of T1 comma T2 you actually get a particular you know zipped dictionary which is Created from the list or the QR codes 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 F2 is equal to sorted off I for i n a 1 f i in a naught 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 naught okay a naught where is a naught so basically you are a naught is equal to just a moment yes so you will not get that a naught 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 naught a naught is a dictionary okay and it sets for the keys to be one two three four five and it is not it is a values which are one three two five four whatever it might be okay it is the values not the keys all right which is why you get an empty list similarly A3 is equal to sorted off I oh sorry a naught of S for S in a naught okay so we are diagnosing over a naught first okay we are traversing over a naught we are traversing over the actual Keys as such a c b e d okay so h x for a naught of s okay we check for a naught of s so what happens we get it has one two three four five 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 a one this time and we are 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 one two three four five now A5 is a dict comprehension this was a list comprehension this is a date 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 and even F5 will give you 0 0 1 1 2 4 3 9 4 16 so on l 9 okay so we are creating a dictionary comprehension by using this for I in A1 okay we are reversing over a 1 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 expressions in point okay how do we do various things and you know fighting so we got an re module so we've got split gives us a regex pattern to split a given string into a list okay so split it uses a regex pattern sub will find all the substrings where the rejects pattern matches and replace them with a different string okay and sub n will replace it like it does in sub but it will give you how many Replacements happen okay for import re okay my S is equal to something like blah blah blah something like this okay you have a rejects pattern that is equal to blah something like this okay so what you do is my is not split off my S come up back up 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 BL a okay BL a b l a b l a the first one is an empty string then it gives you H H and H pattern comma the string which you watered good for okay substitution okay I will do it for S Sub n because it works similar to sub okay re dot sub n okay of your pattern that is block comma your string okay which is my S so you've got a pattern okay replace blur with sorry I missed out this a with pattern and minus okay so it will replace the block with you know b l okay it will replace with you know a particular a so if we take it the other way around pattern comma a minus so you it replaces bla by a only a okay here you get it how it replaces a particular pattern everyone are you clear with how it replaced is 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 item replacement item a okay re dot sub n okay re dot 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 will 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 Ahah so b l a b l a is replaced with a a a 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 subclass as such okay now so there are different types of inheritances okay so there are single there's multi-level there is hierarchical and multiple so typically the highly used ones are either single inheritance or multiple linearity 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 plane you know previous class as such okay 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 inheritance again you know from that particular class hierarchical is based you know one Base Class you can inherit multiple child classes so basically it might be something like this hierarchical so class all right class Plus n Plus my class 2 plus 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 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 very derived from multiple classes okay it is from multiple classes so my class so another class will derive from my class comma right 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 Django might find it relevant okay now this is a question so mention the Django 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 modern view controller but here what happens is that a model is similar to both of them both MVC and mvt the view over here functions as a controller itself okay and this V view in Django functions as a regular controller it takes care of all the controller aspects whereas a 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 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 templates 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 in the view and the template then Maps it to the URL and Django does 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 Django 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 HTML add slash head body 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 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 Java so we'll go with a default database which is sqlite which stores data as a single file in the file system so you can also have databases like post gray MySQL Oracle my Ms SQL rather than sqlite 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 Django how to use it okay this is where your settings P dot py file 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 backends sqlite3 because SQL at 3 is your particular database and you direct it to a particular now base directory name so base dir has to be also mentioned over here and DB dot sqlite tree 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 dot py you've got another file called manage.p right so be sure about setting.py and manage.p1 are you all clear on that all right now how do you write a view in Django 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 you know views.py file where you put from django.http import HTTP 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 it will create you'll get a HTML respond and an HTML file which shows the current date and time so it will 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 will give you an output say it is now 11 10 AM or whatever it may be because it pulls the information 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 jet 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 Django so what does Java 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 n number of other types as well it can contain variables like I just showed you earlier my title Mind 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 views dot py file you've got a separate template folder which contains the various templates and you have your url or you know your your views which is your particular so basically you create your root these are all your routes which are your URL and you pull up your you know this URL root 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 sees that you know he's still logged in okay so how do I you know this is another way of doing this same thing regarding session how do I see to it that the user logs out after a particular type so that is where you make use of something known as sessions in Django okay you know a Django 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 that you know the 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 login again that is where session IDs and cookies come into picture okay so this is very good for security as such okay it is very good for security that way you are not pulling data from server and storing it on your client side 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 Django okay so there are three possible inheritance Styles so you've got abstract base classes also known as ABC you've got multiple table inheritance and you've got proxy 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 parents class okay and you would want the you know parent class to put 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 completely you know what 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 here you can you know you have your child classes derived from because that is because it is an abstract Base Class and you will not be able to you know what is instantiate a base class as you know abstract class as such okay it has to come from the child classes which are created for okay similarly you've got multi-table 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 article 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 multi-table inheritance it does not allow you to do that okay are you all clear on that so 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 model screen so you can create proxies where you are making changes to the actual database via an existing you know what 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 the field Class A Django field class so it will contain the database column type if it is integer string where care or anything like that the default HTML widget available while rendering a form field whether it's a text box whether it's you know whether it's a particular you know uh email you've got a 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 this come in the field class please remember this minimum validation requirements default HTML widget and the database column type okay coming to the next section that is web scraping using python these questions are 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 are using from you know your local so your local it would actually work directly so typically what I would do something like this python Port requests my URL is equal to www .http from this requests get water okay so you see that you know you are getting response R is equal to request dot get let's do this let's open off image dot PNG comma WB as f f dot right oh sorry about that it should be we'll see okay read course okay right yes sorry sorry about that I made a mistake in the syntax okay we need to do encoding also because it is not allowing me to do r dot text will 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'd be able to save your image as such an image all right now how can I get the Google cache age of any URL okay how do I get the Google cache of any URL let's do one thing using the following format so if you use webcast.google user content so or open this up you can actually do this so your URL for example www.edu which has been provided by your CDN okay so this is your cache content and you'll be able to get the cash 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 250 movie Pages it should have Fields movie names here 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 are you all following me so far so you go to imdb.com top not 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 parser so basically an HTML passing Library okay it's used for web scraping it's heavily used for web script import requests requests will get your you know information okay so URL is equal to so you have your url so AP so you create your response that is by using request dot 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 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 parsing it this is how you go about it first you know open up the page inspect this page okay and check what you need so what you need is the fields movie name your and rating there are various four ways one such way is this particular thing so dot find children for moving in TR movie dot 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 year will be in title okay under your secondary info again you have you know rating column IMDb you've got a whole lot of information but that has to be first passed over here okay so typically I would come here to over here and I will first do a check on this so what I need is the TR the TR content you will see that you have got various DRS under Lister list uh just a second Roma 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 Lister 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 so is equal to beautiful soup of rest.text so what is going to happen it is going to create an object called Beautiful soup out of the response that you have got in the response that the text part of it okay so if you check two 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 super simple to beautiful soup you need to find something so suit or beautiful so this is what it does now we'll do this okay uh I want Lister list a list is equal to soup dot find okay so dot spine class so you can do this something like this we don't want TD we want the body okay class is Lister Dash list so this is supposed to find me one single item which contains the class Lister list okay and list we'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 a list Dot find all so TRS Mighty r's are going to be super final t r let's check what we need under TR okay TR we just need the tri information okay we just need the ER information within this particular okay okay find all so we want a list.s okay I think this will not go in with that or you can use find capital T r's okay it will 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 t r ntrs okay friend I'll just give you one example okay post column let's take this one for example okay you have something called title column okay class title column now you get to the point of title follow friend TR dot find class school fine we want TD it's almost class oh no title 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 pulled up this is how you put all the information are you all clear on how to you know approach this particular problem okay so that should be about it for our you know interview questions thank you so much for attending today's session thank you have a great day I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist And subscribe to edureka channel to learn more happy learning
Info
Channel: edureka!
Views: 55,891
Rating: undefined out of 5
Keywords: yt:cc=on, python full course, python for beginners, python tutorial for beginners, python tutorial, python, python programming, python full course for beginners, python full course 2023, full python course, python full tutorial, python programming full course, python for beginners 2023, python course, learn python, edureka python, python training, python programming tutorial, best python course, Edureka, python full course edureka, python 2023, Python edureka
Id: ynE7p65jLts
Channel Id: undefined
Length: 712min 5sec (42725 seconds)
Published: Wed Apr 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.