Python for Beginners [Full Course - 2024] | Python Full Course - Learn Python in 12 Hours | 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 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 job so at the end of this video I have included a few interview questions and answers now this is pretty much it for the agenda of this video so let's go ahead and get started with the first topic which is python fundamentals [Music] what is python python well python is a general purpose programming language which is high level easy to learn and dynamically initialized the development of python started out as a hobby for its creator Judo vandrosm because he wanted to make a language that was beautiful to look at and easy for everybody to read to do this he used indentations instead of curly braces to basically describe the blocks of code that came inside the language the language went on but did not receive love as it was slow compared to the other languages now with the rise of machine learning and artificial intelligence python has come into the spotlight because it makes the work much more productive and much more easy why is that well it is because of the amazing computing power that we have we just want work to be much more productive even if it takes a lot of time so that basically wraps up this topic of what is python having that understood let us move ahead and understand why python is popular comparing it to the other languages python is the best language to get started off with it's easy syntax and high level features make it one of the best language for anybody to get started off with programming may it be kids or veterans it is the language for everybody the next reason is that python is an open source language meaning that it is free for anybody to use every day there are thousands of programmers who come together online in making python better and efficient you modify the code you break the code and you make the code you're allowed to do it without any problems moving ahead python can be used to make almost anything GUI applications web applications mobile app server-side coding artificial intelligence machine learning algorithms anything that you want to make python will always have the way to do it lastly there is a huge community of people who come together to make the libraries or modules that can be used to obtain your Solutions just for an example machine learning algorithms are already available and you can use them to obtain your Solutions better your productivity right up there your code is much more lesser you can see why people around the world prefer python so much features make a language attractive and that is what python is made up of features that make anybody want to start coding with it let us now see the different features that python has in store for us the most well-known feature of python is its Simplicity python has made programming fun because it is simple it makes you think move for your solution rather than the syntax the next important feature is that python is an open source language which means that it is free for anybody to use modifications to python are also allowed without any problems which brings us to its next feature portability python supports portability which means that you can write your code and share it with anybody that you want to and it would work the same way as it does for you when you ran the code this makes movements of projects and systems much more easier our next feature are the embedding properties chances are that certain languages May perform certain functions better than python but that does not stop you as python suppose adding code of other languages into itself to run those functions which makes python all the more powerful with that let's move over to the next feature interpretation python is interpreted line by line which means the management of memory and CPU and debugging of the code much more simple if you wonder by now whether python is the language to get started off with let me tell you that it is python has a huge Library support which would help you to obtain solutions to your problems easier you can get started off with data science web development on the go with python you may have had questions like how do developers make applications that are so well related to a domain that is because of object oriented Concepts python supports oops meaning that you can model a real world problem into your code and have security for it such that access to them is restricted which helps you replicate real world scenarios now that we know all the amazing features that python has for us why don't we look at a few of the most well-known companies who use python Google is the most known Tech Giant who uses python to provide better search features to its users Dropbox is a cloud platform to save data online its client and server applications have been coded using python Netflix uses machine learning with python to learn the taste of its users cluster them into groups and then recommend them shows to retain them for longer the National Security Agency uses python for cyber security analysis and for the encryption and decryption purposes the BitTorrent application which is used for peer-to-peer communication started out as a simple python file NASA scientists use Python to perform calculations which helps them reduce time and makes their work easier so these are the major companies who use python as you may know python is a very important language and it would definitely be beneficial if you start learning with now you would wonder how do we get started off with learning python let me help you with that so to get started off with learning this amazing programming language you would firstly need the basics which are variables data types and operators which helps you to understand data and the different data types that you can use such as numeric lists tuples and so many more naming data and Performing operations which can help you to obtain logic a simple addition or subtractions and so forth once you understand that you can move ahead to understand res which can be used to store data in a structured manner nextly understand the flow control working and how you can manipulate it using Loops such as the for Loop and the while loop and conditions which are the FL statements you can then study about how to make methods which are basically used to perform skirt and functions which can be as simple as adding numbers to performing the most complex of work that you can think of later learn how files are handled where you can read data from files and write data to the files you can then learn about oops by understanding classes and objects and what their importance is and finally to master it all you need to practice python as much as you can because practice is what makes you better at it once you are done understanding the basics what do you do you master it there are a variety of careers that are at your disposal stick to the one that you feel is your goal and start mastering it which brings us to the career opportunities that python holds for us web app development is an option that you can choose you have framework such as Django and flowers that can be used to create web applications game development is also a career option that is available where you can create AI enemies and bosses and graphic textures accordingly Big Data analysis is a booming field which can be used to obtain data helpful to make decisions and help take risks for better profits web app testing is also an option where you can report bugs and make sure that a website works as needed data scientists machine learning engineer artificial intelligence are some of the fields that everybody wants to get into and it is clearly one of the jobs that looks like it would have a long life cycle smart iot devices are another place where you can use Python and build better infrastructure for connecting the world so those are some of the most well-known career opportunities that python has for you there are many more such as a software developer automation engineer and much more [Music] python can be obtained from the python software Foundation website at python.org typically that involves downloading the appropriate installer for your operating system and running it on your machine now to do that all you can do is open up your browsers and just type in python.org go to downloads and download python 3.81 for any of the operating systems such as Windows Linux Mac OS or any others once you have selected your operating system just click on download and you will see that the installer gets downloaded on your PCS once the installation is completed you can run the installer to do that just click on the downloaded file check the option that says add python 3.8 to path and then click on install now I'm not going to install this right now on my system since I already have python 3.7 on it once the installation is done just click finish and you have python ready to operate on your systems so now moving on towards the Anaconda installation please make a note guys that installing Anaconda because I'll be using the Jupiter notebook for all the codes and examples that I'll be showing in this session so before I actually install this let me just brief you guys with what exactly is an IDE IDE stands for integrated development environment it is a graphical user interface where programmers write their code and produce their final products an IDE basically unifies all essential tools required for software development and testing which in turn helps the programmer to maximize his output some Ides are generic meaning they can support a number of languages such as Sublime Text atem Visual Studio Etc language specific IDs support a specific language only they also help you understand when you make syntax errors some examples of these are pycharm for python J creator for Java Ruby mine for Ruby on Rails talking about some more important ideas for python it includes Jupiter spider atom Etc however in this tutorial I'll make use of Jupiter and therefore to make use of Jupiter I'll have to install the Anaconda platform so in order to install Anaconda all you can do is go to the official website that is Anaconda anaconda.com distribution so as you can see over here I have an option called as download when I click on this you will see that the Anaconda platform is available for Windows Mac OS and Linux choose the one that will suit your operating system and then click on download here I'd like to advise you guys to download python 3.7 version as Python 2.7 has no support anymore so that was all about the installation now moving on towards the naming conventions Python's naming conventions outline the scheme for naming some of these are giving lowercase names to methods instance variables Global variables packages modules Etc class names should have the first letter in uppercase followed by lowercase letters constants should be completely capitalized if a name contains two words it's good to separate those words by an underscore a single underscore before a name means non-public and the double underscore tells the python interpreter to mangle those names of a class so as to avoid a clash with names defined in the other classes so now moving on towards variables in Python so what exactly is a variable in any programming language a variable is a memory location where you can store a value the value that you have stored may change in future according to the specifications variables in Python can be created as soon as a value is assigned to it it does not need any additional commands to declare a variable in Python so now let's move on and take a look at the variable definition and declaration to understand how we declare a variable in Python to do that I'll jump onto my Jupiter notebook so as mentioned earlier python has no additional commands to declare a variable as soon as a value is assigned to it the variable is declared say for example I have a variable X and I assign a value say 10 to it now the variable X has been declared since I've assigned a value to it however there are certain rules that we have to keep in mind while declaring a variable the variable name cannot start with a number it can only start with a character or an underscore variables in Python are case sensitive they can only contain alphanumeric characters and underscores no special characters are allowed for naming variables so as you can see over here I've assigned a value of 10 to this variable X now in case I want to print this all I can do is just say print X to run the code on Jupiter you can either press shift enter or Control Plus enter when you press shift enter the code gets executed and the command is given to the next kernel when you press Ctrl enter the code gets executed but the command remains in the same kernel so here I'll just hit shift enter and as you can see over here I have the value printed as 10. variables can also hold other values such as float values string values Etc so taking a few examples of these I'll take a new variable say a and I'll assign a value of 20.3 I'll take another variable say B and I'm going to assign string value to this I'll just say hello world so now let me just print out a and b so as you can see over here both the values of A and B have been printed I'll be showing you all the in detail functionality of the print function later on in this session variables can also be redeclared in Python this means that you can assign new values to them as and how you proceed with your program so as you can see over here I've assigned 10 to X so in case I want to modify the value of x all I have to do is just declare x with the new value so this time I'll just say x equals at Eureka and now let me print X so as you can see over here previously when I printed X it returned the value 10. now I've re-declared x with a new value and the new value has been printed you can either have Global or local variables Global variables are visible throughout the program but local variables are confined to the class or the function to which they belong so taking examples of both of these I'll just take a variable over here say c and I'll assign a value to this as hello world now I'll create a function over here I'll just call it as funk however I'll be discussing about functions later on in this session so within this function what I'm going to do is I'll take the same variable C and I'll assign a different value to this say for example edureka now I'm going to run the function I just created as well as print the value of C so I'll just call the function and then I'll say print C just a moment guys I forgot to print the value of the local variable C so for that I'll just say print C and now I'll run this again so as you can see over here the local variable C has returned at Eureka whereas the global variable C has returned hello world also if you can notice over here I've declared the value of C right in the beginning and then printed it right at the end of this program this is because Global variables are visible throughout the program in case I removed this print statement from here and just call the function then the global variable value will not be printed so as you can see over here only the local variables value has been printed and this local variable is not visible outside the function so I hope you guys are clear with variables in Python so now let's move on towards the next topic which is data types in Python so before we actually talk about data types in Python let's discuss about the difference between data types and data structures a data type is a type of data element defined by the value it holds some examples of these are int float double Etc data structures on the other hand deal with data organization enabling the user to store access or modify the data the important data structures of python are lists tuples sets and dictionaries we'll be discussing about the data structures later on but for now let's come back towards the data types in Python the data types available in Python are numbers Boolean values and strings numbers are nothing but the numerical data types numerical data type holds numerical values and it can be divided into integers Floats or complex numbers so just to show you guys an example of this I'll take a new heading I'll just say data types taking an example of the numerical data type I'll just take a variable say X and I'll assign a value to this as 19. so as we all know 19 is a whole number and this is an integer value also you can assign negative values say y equal to minus 4. so what I'm going to do is print out the type of these variables so I'll just say print type of x and the type of Y so as you can see over here that they belong to the class integer so as we all know float values contain decimal numbers so in case I want to declare a float variable I'll just take a new variable say r and I'll assign some decimal values to this say 45.6 now I'm going to print the type of r so as you can see over here it says that this belongs to class float complex numbers are used to represent imaginary values imaginary values are denoted by J at the end of the number say for example I'll take a variable s and I'll assign a value to this as 10 plus 6 J and then I'll just print out the type of it let me just copy the statement and instead of R I'll say s so as you can see over here it has returned that this is a complex number so now moving on towards the Boolean type the Boolean data type is used for categorical output since the output for a Boolean is either true or false now in order to show you guys an example of this let me just take a variable say V and over here I'll just take two comparison values say 2 greater than 6. once I'm done with this I'll print out the type of v as well as the value of V so first I'll print out type I'll just say type of V and the value of V so therefore it belongs to the class boole and the value is false which means 2 is not greater than 6. so now talking about strings strings in Python are used to represent Unicode character values python does not have a character data type A Single Character is also considered as a string string values in Python are declared using single or double quotes so for an example of a string I'll just say my string equals edureka and then I'll just print out my string so as you can see over here it has returned the value of my string you can also access the values of a string by making use of the index values so in case I want to access any element that is present in edureka what I'm going to do is just type in my string and pass on the index value of the letter that I want to retrieve so I'll just say 4. so as you can see over here at index number four is e you can also make use of negative indices so let's take an example of that as well so I'll just say my string of -2 so as you can see over here at index number minus 2 is a which means starting from the special character which is present at -1 a is present at -2 here I want you guys to note that positive indexing starts from 0 and increases by 1. negative indices on the other hand start from -1 and move on as minus 2 minus 3 Etc so I hope you guys are clear to here so now moving on towards flow control in Python flow control statements basically decide the order of execution of the program generally accomplished through Loops conditional statements and function calls so taking a deep look at each of these let us first discuss what are Loops in Python Loops allow us to execute a group of statements several times suppose you're a software developer and you're required to provide a software module for the employees in your office for this you must print the details of payroll of each employee separately printing the details of all the employees will be a tiresome task so instead you can use the logic for calculating the details and keep on iterating on the same logic statement by making use of the for Loop this will not just save you time but also it will make your code very efficient the illustration that you see on the screen is a flowchart for the for Loop the execution starts and then it checks for a condition to be either true or false condition over here can be any logic that we want to test in our program so if it is true it will execute the body of the loop and if it is false it will exit the loop we shall discuss more about the for Loop further on but for now let's talk about the conditional statements in Python before talking about the conditional statements let's talk about what are conditions conditions in Python are logical conditions which we use in if and else statements some of the conditions in Python are the equals condition not equals less than less than or equal to greater than greater than or equal to these are the conditions we often use while declaring a test expression for if and the else statements in Python so what exactly are the if and else in Python an if statement is used to test an expression and execute certain statements accordingly programs can have one or more if statements in them the subsequent or the penultimate statement that follows is an else statement this executes a statement in case all the test expressions in the programs are false to understand the control flow of an if and else statement take a look at the flowchart of how the execution takes place so as you can see the execution starts with a test expression when this is true the execution will enter the body of if and execute all the statements present there but when this expression is false the execution will move to the body of the else statement and execute the statements in the else block just like an if statement we can also add an else if or an alif statement in Python to test out more expressions in order to optimize the code and increase the efficiency of our code so now I'll get back to my jupyter notebook in order to show you guys the syntax of the if else statements I'll just create a heading over here I'll say conditional statements so like I've already told you all the if statements come with a test expression so what I'm going to do over here is just type in the syntax so it starts with if So within this you have the test expression now we've entered the body of this if statement the body of the if condition is going to contain all the statements that are going to be executed if the condition is met so this will contain all the statements when the condition is met in case this is not true and if I have another if statement to be checked for I can make use of the else if statement so for that I'll just write LF and within this will be test expression 2. so what I'm going to do here is statements when the else of condition is met so in case both these conditions are not met which means if they are false okay I'm sorry guys I just made a mistake over here okay so in case both these conditions are not met I'll have an else block so else and then I'll have the final statements so I hope you guys have understood this so now to show an example of this I'll take another kernel and over here I'll just take some variables say X and I'll assign a value to this as 12. then I'll check if the value of x when divided by 3 is equal to 0 or not so for that I'll just type in if X Mod 3 is equal to 0 I'll just print X is divisible by 3. else I'll just copy this print statement and I'll say X is not divisible by 3. so now let me just execute this so as you can see over here it says X which is 12 is divisible by 3. so in case I want to check for this condition to be true for other Expressions what I can do is just add an else if statement so for that I'll just say l if X Mod 5 equal to equal to 0 I'll say print X is divisible by 5. let me just change the value of x to a number that is divisible by 5. so now let me just execute this program so as you can see over here I have a result saying X is divisible by 5. now let me take a number that is neither divisible by 3 or 5. so say for example 11 so guys the else block has been executed and since I've not made any changes to this print statement I have the output as X is not divisible by 3. let me just add on another part of the statement which is X is not divisible by 3 or 5 and executed once again so as you can see over here the else block has been executed saying X is not divisible by 3 or 5. let me just explain this once again to you all so what exactly is happening over here is my variable X holds the value of 11. after this my program checks if the resultant of x divided by 3 is equal to 0 or not we all know that if we divide 11 by 3 we're not going to get a whole number which means the quotient is not going to be 0 and therefore 11 is not divisible by 3. then my program will enter the else if block here it will check if 11 divided by 5 is equal to 0 or not as we all know 11 is not divisible by 5 and therefore this block is also omitted finally it gets into the else block and over here it prints the final statement so I hope you guys are clear with this so now let me take another example and check whether a number is prime or not using the if else block so what I'm going to do here is I'll take a new variable as a and I'll assign the value to this as 10. and after this the first thing that I have to do is check if a is greater than 1 or not so what I'm going to do is type in the if statement so if a is greater than 1 then I'm going to proceed with the program so if a is greater than 1 for all the values that are present after 1 I'll have to check if a mod x is equal to 0 or not so for X in the range of a comma 2 which means starting from 2 I have to go until a I will check if a mod x is equal to 0 or not in case a mod x which means a divided by X here the value of x is going to start from 2 and it will go on till 10 without including 10. so the values of X will start from 2 and go on till 9. in case 10 is divisible by any number present between 2 and 9 then a is not a prime number so what I'm going to do over here is just say print not Prime this way I'll be able to check for all the numbers that are present between 2 to 10. so once this is done what I'm going to do is just break through the slope in case this condition is not met I'll have to type in else wherein I'll be printing that it is a prime number so as you can see my internal if else Loop is completed now coming back towards this if statement which checks if a is greater than 1 or not so if a is not greater than 1 I'll just print out so if the value of a is not greater than 1 I'll just inform the user that the value of a is not greater than 1. so I'll just say value of a is less than or equal to 1. so as you can see over here the value of a that I've given over here is 10. therefore when I execute this block of code I get the output as not prime which means 10 is not prime so instead of 10 if I assign the value of a as 1 and execute this program you see that the output says value of a is less than or equal to 1 which means the final else block has been executed now in place of this if I Supply some prime number say 3. so as you can see over here when I supplied the value as 3 the output has returned saying that this is a prime number so I hope you guys have understood this so now let's talk about the shorthand for if and else in case you just have one statement to execute or multiple else statements to check for you can make use of the shorthand if and else so to show you guys an example of the shorthand notation of if what I'm going to do is take two variables over here say A and B and I'll declare a as 5. and B as 7. now I'll make use of the if statement so if a is less than b I'm going to print a is lesser than b so previously if you've noticed the body of the if statement is starting in the next line with an indentation in the shorthand notation you can make use of the statement to be executed in case the condition is met within the same line so what I'm going to do is just give a space over here and then I'll say print a is less than b so as you can see over here my if block has been executed similarly you can also have a shorthand notation for the else statement now this is going to be a little different what I'm going to do is first type in print a is less than b if a is less than b else I'll say print a is greater than b so as you can see over here since a is less than b the if block has been executed so I hope you have understood the elsif blocks completely so now moving on towards the next flow control statements that is the for Loop and the while loop so what exactly is a for Loop a for Loop is used to execute statements once for each item in the sequence the sequence can be anything such as a list a dictionary a set or a string for Loops basically have two paths the block where the iteration statement is specified and then there is a body which is executed once for every iteration in a for Loop we actually have to specify the number of iterations that must execute so let me just get back to my Jupiter notebook to show you guys the syntax of the for Loop before that I'll just create a heading and I'll say for Loop so unlike other programming languages the for Loop in Python is a far in Loop so basically for any iterator variable in the sequence I will execute the body of the for Loop that will have all the statements so for every iterator present in this sequence I can execute a single or a number of statements so just to show you guys an example of the for Loop what I'm going to do is take an example list I'll just say my list and I'll give some random values to it we'll be seeing how to create lists and what our lists further on in the session but for now let me just create a list over here so in case you guys are wondering what exactly is an iterator a python iterator is a container having countable number of values values in a container can be traversed using the iterators so in my case I've taken the list as an iterator over here next I'll just open up the for Loop and for every element that is present in my list I'll say print that element so as you can see over here the for Loop traverses each iterable one at a time and then it executes the body for all the iter builds present in my list so therefore for every element in my list I'll print all elements one after the other so now let me take another example over here I'll say my string equals hello world so for every element that is present in my string I'll print that element sorry guys I missed in over here okay so as you can see all the elements present in my string have been returned in case you want to retrieve any specific element you can make use of the index values so what I'm going to do is just copy this and over here I do not want to return all the values I just want to return the value present at some index number say for example 3 and I'll execute this so as you can see at index number three is l in case I want to selectively print a section of the values present in my string I can make use of the for Loop and then slice the values that I want from my string so for that I'll just copy this I'll paste it over here and I'll say print all the values from 0 to 3. so as you can see from index number 0 to index number three I have all the values that are printed please make a note over here that it starts from 0 goes on till 3 but it does not print the value present at index number three similarly in case I want to print all the values that are present after three I can slice my string and print all the values that are present after 3. so as you can see I've not given any value after this colon which means I want to print all the values that are present after three similarly you can also make use of negative indices so I'll just copy this I'll say print the value that is present at -3 so for our coincidence the element that is present at minus 3 also is L that is because this special character is present at -1 D is present at minus 2 and L is present at minus 3. so I hope you guys are clear with this so now moving on towards the while loop the while loop executes the set of statements as long as the condition is true it consists of a condition block and the body with a set of statements the while loop keeps on executing until the condition becomes false so in case you want to make use of a while loop you'll have to keep three things in mind the first thing is to initialize your iterator the second is to specify the condition and the third is to increment your iterator in case you do not increment your iterator the while loop will go on forever so what I'm going to do over here is take a small example of the while loop before writing down the example let me just write down the syntax of the while loop so that is while some expression and then the body which contains the statements so now let's move on towards an example of the while loop so I'll take an iterator over here say I and I'll initiate the value of I to 0. so now my iterator value has been initiated so this was the first rule for my while loop which is initializing the iterator the second is to specify the condition so I'll just take in the while loop I'll specify the condition as while the value of I is less than 4 I want to print the value of I and the last bit of this is to increment the value of this iterator in case I do not increment this it will keep on printing the value of I in finite number of times so I'll just say I plus equal to 1. so I'm incrementing the value of I by 1 each time so now let me just execute this so as you can see over here it prints the value of I unless and until it is less than 4. so therefore the initial value of I is 0 and the first value in the output is 0 as well the second value will be taken by incrementing the value of the iterator by 1. therefore I becomes 1 and since I is still less than 4 I'll print I next I'll increment the value of I again by 1 which is 1 plus 1 and that will be 2. 2 is still less than 4 therefore I'll print 2. after that I'll add one to two again which will give me three the value of 3 is still less than 4 I'll print 3 and then I'll increment the value of I this time the value of I will be 4. so 4 is not less than 4 which means I'll exit the while loop so I hope you guys have understood this so now moving on towards something very important which is the loop control statements I'm sure many of you all would have come across the statement such as break pass continue Etc these statements are nothing but the loop control statements the loop control statements are used to control the flow of the loop or to alter the execution based on a few specified conditions in Python we have break continue and pass as the loop control statements the break statement is used to terminate the execution of a loop containing it as soon as the loop comes across the break statement the loop terminates and the execution is transferred to the next statement here I'll say for X in edureka if x is equal to R I'll break the loop which means when I encounter r i want to break through the loop and I do not want to continue the loop for the rest of the elements and then I'll just say print X and finally I'll just say print Loop has ended sorry guys I just made a mistake in the indentation over here so as you can see over here all the elements that are present till R have been returned which means edu have been returned and then the loop has ended so now talking about the continue statement the continue statement is used to skip the rest of the code in the loop for the current situation the continue statement however does not terminate the loop like the break statement and continues with the remaining iterations so basically when a continuous statement is encountered it only skips the remaining Loop for that iteration so let me just try this out of here so instead of break I'll say continue so when R is encountered the value is skipped which means when R is encountered the print statement is not executed so now talking about the path statement the past statement is a null operation it basically means that the statement is required syntactically but you do not wish to execute any command of code so in case if I change this from continue to pass and add a print statement that is to be printed when R is encountered I'll just say pass execute it so as you can see when R is encountered this print statement is executed and then the loop continues so talking about nested loops python also allows us to use one Loop within another you can use a for Loop within a while loop and a while loop within a for loop as well so to take an example of this let me just say for x in range of 1 comma 10 and then I'll take another for Loop within this for Loop I'll say for y in the range of x I'll say print X so instead of ending the Sprint statement with the next line character I'm going to use the end parameter as a space character so for all the values that are present between 1 and 10 each time I take the value and in that range I'll print the value so for the first time I'll have X as 1. so for 1 in the range of 1 I'll say print one therefore 1 is printed just once for the second iteration I'll have the value of x as 2. so for 2 in range of 2 I'll print 2 which means I'll print 2 twice now since my output does not look too formatted what I'm going to do is add another print statement to create some beautiful looking output so as you can see over here I've created a number Pyramid so I hope you guys are clear with this so now let's take an example of a nested while loop so what I'm going to do here is take a variable say a and I'll assign a list of values to this so while a is true what I'm going to do is just print a DOT pop minus 1. and then I'll take another variable within this while loop I'll just say B is equal to some list of string let me just take a symbol that will differentiate between these two loops so next I'm going to take another while loop and I'll say while B is true print B dot pop of 0. so as you can see both my while Loops have been executed so I hope you guys have understood this so next what I'm going to do is print the Fibonacci series using both the for Loop and the while loops I already have a code for that so let me just copy it and I'll paste it over here so as you can see what's happening over here is I've defined the Fibonacci function and over here I'm assigning the value of first and second as 0 as 1. then while this is true I'm going to yield the value of f in case you want to know more about the yield function you can actually check out generators in Python video so moving on once the value of f has been returned which means if the first value has been returned I'll have to change the value of first as second and the second will be first plus second so therefore first becomes second and second becomes f plus s next I'm taking the for Loop for every element that is generated by my function in case the value of x is less than 50 I'm going to print the value and I'm going to end it with the space character in case the value of x becomes greater than 50 all I have to do is break the loop so I hope you guys have understood this okay so now moving on towards the next topic of the session which is functions in Python before discussing what exactly are functions let's figure out why do we need python functions functions are used to manage inputs and outputs in a computer program programming languages are designed to work on data and functions are an effective way to manage and transform this data the modifications are generally done to drive outcomes like performing tasks and finding results the set of operations or instructions required to do so comes from logically functional blocks that can be reused independently from the main program in fact the main code is also a function just a very important one every other function is logically aligned and maintained to functionally execute your main code but if the function has not been defined previously you'll just have to Define one yourself before using it this is because the function definition lists the steps of operation so basically functions are nothing but toss that the user wants to perform repeatedly by defining it just once with the name the user will be able to use the function anywhere in the program this not just reduces the size of the code but also helps in debugging so now coming towards what exactly are python functions the functions in Python are classic example of such reusability so to serve a wide range of applications from GUI and mathematical Computing to web development and testing Python's interpreter already comes equipped with numerous functions that are always available for the use also you can add on libraries or modules to your program that contain predefined functions readily available for use so now talking about the types of functions in Python you can either have built-in functions Lambda functions or user-defined functions so taking a look at each of these in detail so python built-in functions built-in functions are nothing but the functions that are provided by the python interpreter some examples of these are the print function the sum function min max functions Etc now let's get back to our Jupiter notebook and see how you can make use of these built-in functions the first function that I'll be discussing over here is the print function I'm sure you all know the print function print in Python is a standard function that is used to print the output to the console let me just create a heading over here I'll say functions to show the syntax of this function to you guys I'll make use of the help function so help on the print function will return all the documentation about the print function in Python so as you can see on the screen the print function takes a number of parameters value is nothing but a value or a set of values that has to be printed out to the console a separator will decide how the values are separated from each other the end parameter is used to specify what is to be printed at the end of the output default value for the end parameter is slash n meaning next line next is the file parameter this is an optional parameter which comes with the right method the default value of this is sys.std out flush is also an optional parameter that is used to specify if the output has to be flushed or is it to be buffered if the output has to be flushed the Boolean value will be true and in case if it is buffered the Boolean value will be false the default value for this is false so now let's take a few examples of the print function so the first thing that I'll print is hello world next I'll take the same function and over here I'll print it as two different strings what I can also do is add a plus sign in between this so I'll just copy this and I'll add a plus sign over here so as you can see over here the first statement is printing the hello world string as it is in the second statement over here I'm printing hello world as two different strings in the third example I've made use of a plus operator which means I want to add the first ring to the second string I can also add in some numerical values say 2020 and then print it so here it is hello world 2020. now what do you mean by a separator in case you want to use a separator what you have to do is just I'll just take this example and instead of separating them by a space let me separate them by something else so I'll just say separator should be a hyphen okay so as you can see over here in place of a space character I've got a hyphen so if I have two print statements one after the other I can change the N parameter from next line to something else so what I'm going to do is I'll say hello world edureka and in the first print statement I'll make use of the N parameter and I'll specify it to be a dashed line so as you can see over here the outputs of both these print statements have been printed in the same line but they are separated with this dashed line over here so I hope you guys are clear with this so now let's talk about the min max functions the min max functions are also built-in functions that are present in Python so in case I have a list of values say a and if I want to find the minimum and the maximum of this all I can do is make use of these functions directly so I'll just say print the minimum of a and the maximum of a so as you can see over here the minimum value that's present in a is 1 and the maximum value present in a is 7. another commonly used function of python is the sum function this function will return the sum of all the iterables that are given as a parameter to it so I already have my list over here as a I'll just say sum of a so as you can see over here it has summed up all the values present in my list and it has returned the value as 28. one more thing I want you guys to note over here is the sum function actually has two parameters now to show that to you guys let me just type in help for the sum function so as you can see over here the sum function has two parameters which is the iterables and the start it rebels over here is the sum of iterables whose value needs to be summed up the second parameter is the start parameter which is by default zero meaning the sum of all the iterables should be added to zero so it will be 0 plus sum of all the iterables in case I specify some value to the start parameter it is going to add that Value Plus the sum of all the iterables so in the previous example if I have sum of a comma 10 it's going to add 10 plus 28 and return the amount so as you can see over here it has returned 38 which is 28 plus 10. so I hope you guys have understood this so now moving on towards the next topic which is python Lambda functions python Lambda functions are functions that do not have any name they are also known as Anonymous or nameless functions the word Lambda is not a name but it is actually a keyword this keyword specifies that the function that follows is anonymous how do you actually write Lambda functions in Python a Lambda function is created using the Lambda operator okay so now let me just type in the syntax of a Lambda function so basically it will be the Lambda keyword followed by the arguments and then the expression here arguments are nothing but the variables that are used with the Lambda function expression is a mathematical expression that will decide the output Lambda functions can take any number of arguments but they can take just one expression arguments can be anything starting from zero and there is no upper limit just like any other function it's perfectly fine to have Lambda functions with no inputs so now let's just take an example of a Lambda function so I'll just take a variable a and now I'll make use of the Lambda function I'll just take one argument for the first example I'll say Lambda X return X star X and now I'll say print a of 3. so as you can see over here I've got the output for X star X when a is 3 and therefore the output is 9. let me take another example with two arguments I'll just say B equal to Lambda X comma y and I'll say print X Plus y I'll pass the parameters for X and Y as 1 and 2. I'm sorry guys instead of taking B of 1 comma 2 I took a of 1 comma 2. so I found the result for X plus 5 is 1 plus 2 and that is 3. so I've taken two examples of here but please make a note that both the functions have a single expression so now moving on towards user defined functions functions that we Define ourselves in a program in order to achieve some tasks is referred to as a user-defined function so the Syntax for a user defined function will be the def keyword followed by the name of the function and then the arguments you can have any number of arguments there comes the body of this function the body of this function can have any number of statements so I'll just write statement one two Etc so as you can see over here the first thing that you'll have to do is make your DF keyword now this keyword will actually help you to Define your own function next up will be the name of the function then you'll have to pass the arguments of your function which can be any number of arguments starting from zero the body of the function can have any number of statements in them so now let's move on and create some user defined functions so what I'm going to do over here is create a function to add two numbers so I'll just say Define add function I'll specify the parameters as X and Y and I'll just say the sum is equal to X Plus y and I'll return the value of sum after this I'll just have to call the add function and pass the values of X and Y I'll say 2 comma 4 and I'll run this so as you can see over here I have the output for 2 plus 4 which is 6. so now that you have seen what our user-defined functions as well as Lambda functions let's take a small example wherein you can make use of the Lambda function within a user-defined function so what I'm going to do is Define a function over here say new function and to this I'll just give one parameter as X now this function will just return one statement that is the Lambda function the arguments for this function will be Y and I'll specify the expression as X star y now T and now I'll have to call user defined function which is new function what I'm going to do is just print t or 5. so as you can see over here my function takes one parameter as 3. so X over here is 3 and Y is 5. now if you're wondering why 3 is assigned to X and 5 is assigned to Y this is because I'm specifying 3 as the parameter of my new function then within this function I've made use of the Lambda function which takes one parameter therefore this parameter is supplied the value as 5. so I hope you guys are clear with this so now moving on towards the next very very important topic of the session which is data structures in Python so what exactly is a data structure data structures allow you to organize your data in such a way that enables you to store collections of data relate them and perform operations or them accordingly python has implicit support for data structures which enable you to sort and access data these special data structures of python are nothing but lists dictionaries tuples and sets python allows users create their own data structures as well the most prominent data structures of this section are Stacks queues trees Lucas and so on so now talking about the built-in data structures of python lists are used to store data of different data types in a sequential there are addresses assigned to every element of a list which is called as the index the index variable starts from 0 and goes on until the last element is called as the positive index negative indexing starts from -1 that is from the last element that is present in your iterable therefore positive indexing starts from the left hand side and moves towards the right hand side whereas negative indexing starts from the right hand side and moves towards the left hand side so now let's get back to our Jupiter notebook and see how to create lists in Python to create a list in Python you can make use of the square bracket and add elements in accordingly in case you do not pass any values the list is going to be emptied so just take a new list over here I'll say my list and I'll not assign any value to this then I just print the values of both these so as you can see over here new list contains a list of elements starting from one to seven and my list is empty not just this you can also make use of the list function to create a list so I'll just take a new variable say a and I'll say this is equal to list of I'll specify an empty list now let me just print out a so as you can see over here I've created an empty list making use of the list function now since lists are mutable you can add on values or delete values from lists in order to add elements to a list you can make use of the append extend or the insert functions the append function can be used to add a single element to your list the extend function will add a number of elements one after the other to the list the insert function on the other hand will insert an element at a specified index value in your list so let's take an example of all these three so I'll just say a DOT append and I'll specify a value say 3. next to new list I'll add on a number of values I'll say new list.extend and I'll give a list of values also I'll make use of the insert function I'll say new list dot insert I want to insert the value at position 2. and here I'll insert a string I'll say edureka now let me just print all these I'll print a so as you can see previously a was empty but after making use of the append function I've added 3 to my list so now in case I want to see what's present in new list I'll just say print new list so as you can see 9 and 10 have been added to the end of my list and edureka has been inserted at index number two so one is at index number zero two at index number one and edureka is present at index number two so that was about adding elements to a list elements can also be deleted from a list in case you want to delete elements from a list you can either make use of the pop function the remove function or the delete keyword now let's take an example of each of these for my new list what I'm going to do I'll say new list.pop of two and then I'll say print new list as you can see here the values you have to specify the value to be removed instead of the index number so here as the new list start to move and a specified value to B10 and then I'll just print it so as you can see 10 is not present in my list anymore similarly if I make use of the Del keyword I can delete some element that is present at some particular index number I'll say delete new list of 5. and then print new list so as you can see over here the element that is present at index number 5 has been removed elements of a list can also be accessed by using the for Loop so in case I want to access all the elements that are present in new list all I have to do is use the for Loop so for x in your list I'll say print X so as you can see all the elements have been returned one after the other you can also slice the new list from some particular range so I'll just print all the elements that are present after index number two sorry guys I said I want to print all the elements that are present after two but here I specified colon 2 which means I've printed all the elements starting from the index number 0 to 2. there are a number of other functions that you can also use which is the length function to find the length of the list the index function which finds the index value of the value passed as a parameter and the first occurrence of it the count function which finds the count of the value passed to it you can also sort the list by making use of the sorted or the sort functions so now moving on towards the next data type which is a dictionary dictionaries are used to store key value pairs to understand it better think of a phone directory where hundreds and thousands of names and their corresponding numbers have been saved here names will be the keys and the phone numbers will be the value pairs for that keys in Python dictionaries can be created using the flow brackets you can also create them by making use of the dict function so now let's get back to our Jupiter notebook and create some dictionaries over here I'll just think about it will say a and then I'll open the flaw brackets I'll take the keys as numbers so I'll just say one is hello and 2 is world and then I just print out a in case you do not specify any parameters an empty dictionary will be created so I'll just take an example of that over here I'll say V is equal to flaw braces and then I'll print B so as you can see over here an empty dictionary has been created you can also create dictionaries by using the dict function so I'll just say C is equal to ticked off and I'll copy the same thing so as you can see over here I've created a dictionary by making use of the dict function so now let's move on and see how you can actually change and add values to dictionaries values of a dictionary can be changed by using keys so the first thing that you'll have to do is access the key and then change the value accordingly so in case I want to change the value of 2 over here what I'm going to do is C of 2 is equal to edureka and then I'll print C so as you can see over here the value for 2 has been changed from world to edureka in case you want to delete items from a dictionary you can make use of the pop function the pop item function or the clear function the pop function will return the value that has been deleted the pop item function will return a tuple of the key and value to clear the entire dictionary you can make use of the clear function so let me just show you an example of this I'll say C dot pop the value present at index number one so as you can see over here hello has been removed from my dictionary in case I make use of the pop item function it's going to return both the key value pair so as you can see over here the pop item function has returned both the key value pair that has been removed now my dictionary is empty in case I want to delete this dictionary I can make use of the clear functions so I'll just say C dot clear my dictionary is already empty and I don't actually have to make use of this function I just type this function over here to show you guys how to use it the values of a dictionary can also be accessed now to access the values you will have to make use of the keys only to do this you can either make use of the get function or directly specify the name of the key I'll just create a new dictionary over here I'll say d is equal to 1 is to python 2 is to Java and 3 is to C sharp now I want to retrieve the values that are present for the keys 2 and 3. so what I'm going to do is use the print function and within this I'll specify the name of my dictionary and then in the first example I'll just specify the key value and in the next example I'll make use of the get function I'm sorry I haven't executed this line over here so as you can see over here the value present for key 2 is Java and that for Key 3 is the sharp so now talking about stuples tuples are actually the same as lists but with one exception that the data once entered into a tuple cannot be changed no matter what to create a tuple in Python you can make use of the regular brackets you can also create a tuple by making use of the Tuple function I'll just create my Tuple and in the first case I'll create an empty Tuple next I'll create another Tuple say Z and I'll assign some values to this I'll take another variable y and then I'll make use of the Tuple function in order to create a tuple now let me just print out each of these I'm sorry guys I've missed regular braces over here so as you can see an empty Tuple has been created followed by a tuple using the regular braces and then a tuple making use of the Tuple function like I've already told you before you cannot alter the values of a tuple so in case I use any function such as an append function with the Tuple it's going to throw an error so now just to show this to you all I'll say why not append I'll say three so as you can see over here it has thrown an attribute error saying Tuple object has no attribute append in case you want to access the elements of a tuple all you have to do is make use of a for Loop so I'll say for I in y print I so as you can see all the elements of the Tuple have been retrieved one more thing I want you guys to note over here is that in case you want to add on some elements you can make use of the plus operator so now moving on towards the next data structure which is a set s are a collection of unordered elements that are unique meaning that even if the data is repeated more than one times while specifying the values the set data type is just going to take one entry of that element sets in Python actually resembles the sets that you've learned in arithmetic the operations are also the same such as intersection difference Union Etc to create sets in Python you can make use of the flower braces and instead of adding the key value pairs you just have to specify the values now let me just get back to my Jupiter notebook and show you all how to create sets in Python like I've already told you all sets are created by making use of the flower braces I'll just take a variable over here as X and to this I'll specify some values and then I'll just put in the set so as you can see over here I supplied eight values but only five values are present in my set now this is because I have three repeated values and those values are not printed again in my set in case you want to add some value to a set you can make use of that function so I'll just say x dot add and I'll pass the value as 8. now let's just print out X so as you can see over here it has been added to my set you can perform many operations such as interactions unions different Etc now what I'm going to do is Define another set over here I'll say y is equal to some random values so in order to find the union of X and Y I can either use a pipeline character or make use of the Union function in case I just want to find the union of X and Y I'll see Z is equal to X pipeline Y and then our 20. sorry I've taken cap C over here which is small actually so as you can see I have the union of X and Y present in z similarly I can also make use of the Union function to do that I'll just make use of the print function and then I'll say X Union y equals x dot Union of Y so as you can see here I Found the Union of X and Y similarly also make use of the difference function the intersection function Etc so let me just change this from Union to addiction and see what happens so as you can see here I change the value from Union to intersection and therefore I found the intersection of X and Y I just have to make a change over here as well okay so I hope you guys are clear with this so that was about the built-in data structures of python now moving on towards the next topic which is user-defined data structures user-defined data structures can either be arrays Stacks cues trees latest graphs and other typical data structure which can hold more than one value at the same time it is a collection or a series of elements of the same type we need to worry easily and fetch the required by specifying the numbers manipulations operations structures which are based on the principle of Lost in first out here the data which is entered last will be first be assessed it's built using array structure and has options may be pushing or adding elements or dealing elements accessing elements only from one point is called as the top the top second commonly used in applications such as recursive programming recursive words under mechanisms in word editors under mechanisms in word editors and so forth cues a queue is also a linear data structure first and first out here the data in the first will access first it is operations operations such as adding and deleting elements are called as NQ and DQ you are used as Network buffers for traffic control management using operating systems data structures which have roots and nodes the root is a node from where the data originates and the nodes are the other data points that are available to us the book that precedes the parents and the doctor is called as a child levels of a tree shows the depth of emission philosophers the leaves which produces a lot of real world applications these structures are mostly used in image viewing applications music player applications and so forth graphs graphs are used to a data collection of points called as vertices and edges graphs can be called as the most accurate representation of real world map they are used to find the various cost or distance between various data points called as the nodes and hence find the least path many applications such as Google Maps Uber and many more make use of graphs to find the least distance and increase process in buffets populate data according to this and much more so now let's move on and discuss what are algorithms in Python algorithms in general are rules or instructions order to solve problems not language specific how do categories on standard language to formulate algorithms but you will need to keep the following exactly the problems and finally review your steps say for example if you have to formulate an algorithm to check if it has passed in an example say X and Y and then you're going to store the marks obtained by student in X and for the minimum passing score in y then check if x is greater than or equal to Y if yes I've returned else return fail and finally you'll have to stop so this is algorithm to check if a student has passed or failed however you can manipulate the steps according to your preference you can find values to both variables X and Y in a single step rather than two steps and depends on the problem and program it is good algorithms need to finite description yeah important algorithms such as tree traversal algorithms and I started so like I've already told you I'll write down the code to create a tree first so for that let me just create a heading this would be an H2 so I'll have to have two hashes and then I'll just be creating the tree so the first thing I'm going to do here is to find a class called node so class node in case you have any issues understanding classes and objects don't worry guys because I'll be discussing each of this in detail later on in this session so for now I'm just defining the class within which I'll Define a function that is the init function and then the mandatory parameter which is self followed by the value parameter now this self parameter will allow me to access the variable of this class all the details about classes I'll be discussing later on in the session when we go on to oops important so now just write itself dot left child or I'll just say equals none now I've specified none over here because right now there is no left child the first value should be assigned to the root note and therefore the left child and my child will contain nothing and therefore I'm assigning none of it similarly I'll write self dot right child equals really self dot no data value so let's move on and create an object of the class root and you guys have seen already that it might treat the first rule or the root note is one so therefore it's done left child will be equal to node of and root dot right child so in my tree the node 2 has another left child and right side to create that what I'm going to do is write down root dot left child dot left child is equal to node four and finally the right childhood so I hope you guys are clear with this let me just draw this and explain this to you guys so at the first step I'll have my root note that is created and actually node one for like that I have a left type this node and then a right child will be created for one that is three now again node 2 will have two choices a child will be known for and right side will be node because in other travel refers to travel by the root and then the white clothes so you have to implement in order to do is let's create a heading assign audio since I have my base tree already created over here all I'm going to do is just to find the other option so the first step of in order traversal will be to Traverse through the notification then at step two I'll have to visit the root and finally the right so now I'm going to Define function I call it as in order and then I'll pass a parameter as root so if a body for root is present what I'm going to do is call the inordination and over here I'll first visit the left child so root of left eye and then I'll print the value for the root okay sorry guys if root I just missed that so once I visit the left child positive so in order of root so as you can see earlier the in order travels will start from 4 moves on to node two then five one and three so in this review here for another chances and the leftmost node in my tree is no this is true and finally the right node once I'm done with four two and five my left sub 3 is completed once the left subtree is completed I'll have to apply the same formula again which is left root right so I'll visit node one which is foreign for the right child so once this is done I just have to call your function so as you can see here the first node to be visited is the root node one followed by the left subtitude and then the left side of two which is four and then I'm going to visit root five which is the right subscribe two and finally when the root and the left is done I move on towards the right subtree and then three okay with this okay so now let's move on towards the next topic which is the post order traversal the poster tussle begins from left then to the right and finally the root so in this example here what I'm going to do is start from 4 1.5 to 1 and 3. now this is because 4 is if I have a right node towards the right of 45 I travel five and then when I come here I have to move towards the right sub tree and then Traverse it in my right subtree I just have one node which is three and after I copy the left and the right sub tree I apply the Left Right logic again and then I visit through this one okay foreign for left child so root dot left side and then post order of the right finally I have to print the value mistake sorry guys so let me just type in this chapter all this and then I'll execute it so here it is sorting algorithms selections the first algorithm I'll be discussing sessions foreign is if I place the value of left list of I into my list of K I'll have to increment the value of I or else if I place the value of right list of J into my list of K I'll have to increment the value of J finally when I've placed some value for K I'll have to increment the value of K as well so once I have all sorted elements what I have to do is start merging my list the final part of my program will help me to merge the sorted sub lists that is true all I have to do is swap the elements so a of Y will become a four plus one and a of Y plus 1 will become a of Y so as you can see I've given some random list over here in this case the length of this list is 4 therefore the value of B will be 3. so for every element present in the range of 3 I'm going to take that element and check if that element is greater than the element respected what's the right side so first I'll take 3 and check if it is greater than 6. since 3 is less than 6 I'm not going to swap these values next I'm going to check if 6 is greater than 1 or not so as we all know 6 is greater than 1 I'm going to place the value of 1 in place of 6 and I'll Place 6 in place of 1. therefore 6 and 1 will be swapped and minus will contain 3 1 6 and then eight for the next iteration I'll check if 6 is greater than 8. so as we all know six is less than eight therefore they will not be five so for the second iteration my list is going to look like three one six and eight so what my algorithm will do is check if 3 is better than one or not since 3 is greater than 1 the position of one and three will be swapped and my list is going to look like 1 3 6 and 8. in the next iteration my program will check if 3 is greater than 6 or not as we all know that these are already sorted they will not be swapped this process is repeated until the complete list is sorted so I hope you guys have understood program okay so now moving on towards the next that is the insertion sort insertion sort picks up one element of a given list at a time and places it at the exact place where it's supposed to be since I already have a program for this I'll just copy it over here and explain it to you guys so what I'm doing over here is first I'm going to place my pointer at the second element so what our input list I have I'm going to place the pointer at the value that's present at index number one now to do that I've made use of this for Loop so for X in the range of 1 comma the length of a which is the element present at 2 and throughout the length of a I'm going to set the element present at a of X as the key and then I'll store the value of J as x minus 1. so in the first case k will be 56 so 56 is going to be k and J will be 1 minus 1 so that is 0. so in the first yes K is 56 and J is 24. the next thing we want to do is check if J is greater than or equal to 0. here in the first case J is equal to 0 so therefore the first half of Y Lo is true the next thing to be checked if K is less than a of J so if the value present at K which is 56 is less than the value present at a of J which is 24 I'll have to swap these elements now since these elements are already sorted I'm not going to do that now once that is done I have to decrement the value of J now this steps because I'll have to check the value of the key with all the elements that are present in the left sub list in the first case I just have one element present towards the left side but for the second iteration I'll have two Ms present so therefore if I do not decrement the value of J I'm not able to check this value with all the values represent towards the left of it so finally I'll place the value of a of J plus 1 into K and then I'll move on towards the next value of x which will be index number two so this Erase this after the first iteration of my program the list is not going to change since the value of 56 is greater than that to 24. for the next iteration I'm going to make 1 as the T element is present at index number two so one will become the key and the value of J will be 56 and 24. now this is because 56 is present at a of 1 and 24 is present at a of 0 and for all the values that are greater than or equal to 0 I'll have to check if the value of the key is less than that or not so I know that 1 is less than 56 and 24. so what I'm going to do is just remove one from here repeat and then I'll Place 56 in place of 1. I'll Place 24 in place of 56. and finally I'll Place 1 in place of 24. so for the situation I'll have one followed by 24 and then 56 which is my sorted list and for the unsorted list I have 50 and 17. so once this is done what I'm going to do is take the next element as the key which is 50. when 50 becomes the key I'll have to check this key with all the values that are present in the left side so I know that 50 is less than 56 but greater than 24 so therefore I'm going to shift 56 to the place of 50. and I'll Place 50 in place of 56. after this another list looking like one followed by 24 then 50 56. and in my unfolded list I'll have to do an element into 17. finally I'll take 17 as the key and then I'll check this key value with all the elements present towards the left of it since I know that 17 is less than 56 it's less than 50 and less than 24 what I'm going to do is Place 56 in place of 17. 50 will come to the position of 56 24 will come to the place of 50. and finally 17 will be placed in the place of 24. so I hope you guys are clear with this even though there's a lot of mess on the screen I hope you guys have understood this okay so let me just erase that for now and execute this problem so I can see on the screen I saw to my list in the ascending order so now moving on towards the next which is the selection sort algorithm the selection sort algorithm divides a given list into two halves where the first half will be sorted list and the second half will be an unsorted list at first the sorted list is empty and all the elements are present in the unsorted list the selection sort algorithm will look at all the elements present in the unsorted list pick up the element that is supposed to come first and then it places it in the sorted list the step is then repeated by sorting the lease element present in the unsorted list and it places it next to the first element of the sorted list so in my examples since I'm arranging all the elements in the ascending order if I have a list of numbers I'm going to select the smallest element and then place it as the first element of the sorted list then I start to search for the next smallest element and place it towards the right of my first element so I'll get back to my Jupiter notebook in order to implement this I have the code for this already so I'll just copy it so initially what we have to do is assume that the first element is the minimum element then what I'm going to do is check the first element is less than any element present towards the right of it so in my list I'm going to first assign 34 as the minimum value so what I'm going to do next is I'm going to check if the value of this minimum is less than any value that is present towards the right so for every element that is present towards the right of minimum that is X if that value is less than the minimum value I'm going to place the value present at my area of Y at minimum value so for the first iteration I'm going to check if 23 is less than 24 or not since 20 is less than 34 23 becomes minimum so after the first equation 23 becomes the minimum value next I'm going to check if 1 is less than 23. since 1 is less than 23 1 becomes the minimum value so after the third iteration I'll have 1 as the minimum value finally I'm going to check if one is smaller than 67 or not this one is smaller than 67 the minimum value will remain one finally I'll check if 1 is smaller than 4 or not since 1 is still smaller than 4 the minimum value is one so the first element to be placed in the sorted list will be one my sorted list will have one and answered list will have all the elements which is 34. 23 67 and 4. so this is my sorted list and this is my answer list I'm going to repeat the same thing for the unsorted lit again at this time 34 will be minimum and after the first iteration 23 will come minimum and for the third iteration 23 is going to remain the minimum following which I'm going to check if 4 is less likely or not since 4 is less than 28 4 is going to be removed from the outside list and it will be placed towards the right of one which will become the second element present in the sorted list so I hope you guys all these drawings and I'll run my code as you can see over here I have a sorted ascending order list algorithm allows you to spot elements that are apart from each other the original sequence of the soft elements follows the N by 2 and by 4 sequence where n is the number of elements present in the unsorted list for example if you have a list of eight elements the length of that list will be divided by two for the first iteration as we all know 8 divided by 2 is 4. now the first element will be compared to the element that is present at index number four and then got to be produced by dividing eight by four this time the elements present at these intervals will be prepared finally I thought a by 8 which gives me Gap as one so when I have the Gap as 1 I compare autism then if the value of Gap is greater than 0 for every element that comes in the range of Gap if the elements present after the Gap elements are greater than y we'll have to follow the insertion for algorithm and swap the elements finally the Gap is divided by 2 again since my case n is seven which is my list contains Seven Elements I'm going to divide 7 by 2 to find the Gap so 7 by 2 which is equal to G or the gap will be nothing but 3. now I'm taking 3 because it's the whole part of 3.5 so the Gap year will be 3. so what I'm going to do is first take this element 23 and then I'm going to check with the element that is present at index number three so at index number three is 17. since 17 is less than 23 these values will be swapped so after the first iteration 17 will be at the first place and then write down the file list and then after the first equation I'll have 17 followed by 12. 1. 23 which is what is 17 45 2 and 13. so for the next iteration I'm going to check 23 with 13. now this is because I'll have to check after the gap of three so since 13 is less than 23 I'm going to swap these elements so after that I'm going to have 13 in case of 23 and 20 multiplayers at last in place of 13. so once this is done I'm going to divide the Gap by 2 again so my Gap in this case was 3 so therefore if I divide this by 2 I'll get the Gap to be 1.5 wherein I'm going to take the whole number which is 1. so Gap in this case will become 1. so next up I'm going to check all the elements that are present at Gap of 1 and then replace them I'm going to repeat this until my list is sorted so let me just clear all these drawings and I'll execute this problem so as you can see I have all the elements sorted in here so that was all about sorting algorithms now moving on towards the searching algorithms searching for some elements present in some given data set there are many types of search algorithms such as linear search binary search exponential search interpolation search Etc in this session I'm going to be discussing about the leader search and the research problems the linear search algorithm is used to successively search for a given element by comparing it with each element of the given array it is one of the simplest searching algorithms but very important to understand other searching algorithms so what I'm going to do over here is get back to my Jupiter notebook and over here let's create a heading again I'll call this as searching algorithms the first one over here is linear search and I'll just take the code that I have felt so first thing that I've done is create a function that is key element so if any element present in the list matches the key element I'll have to return that element in case the element is not found I just have to return minus one so for every element present in the rate of 0 comma n that is starting from the zeroth index number and for all the elements present in the list I'm going to check if any element present in my list is equal to the key or not in case the element is present I will return X else I'll return -1 okay the key that I supplied over here is 16 and micro over here contains some random numbers next thing that I've done over here is assign the value of n as a length of my array match to which I'm going to assign the value of the linear search function if the value of match is equal to minus one then e is present at the end of index so let me just delete these drawings and I'll execute this problem so as you can see over here my key is 16 which is not present in my array in case I change the value of this to 12 and then I run this program you see that the key is present at index number zero so I hope you guys have understood this so now moving on towards the next algorithm which is the binary search algorithm my research is used to search for some different elements by making you of the decrease and Conquer algorithm here the key is looked for by first comparing it with the middle element and then dividing the array into half the left half Source if the elements to be source is smaller than the middle element the appropriate subarues are again divided into half and the process is repeated again say for example if you have a sorted list of Seven Elements the key will be compared with an element that is present at the middle or the element present at 7 by 2 which is 3.5 so in this case the middle elements will be assigned as the element present at index number three so in case the key element is smaller than that of the element present at index number three I am going to check a left sub list if the value of key is greater than that of the value present at index number three I'm going to search the right sublet so I already have a problem I'll just copy paste it so in my program a Creator function called research function to which I'm supplying the list as well as the clear element left variable is going to point at first element which is present at index number 0 and the right element is going to be length of my list minus one which is the last element in my list then I've taken the variable to which I've assigned a Boolean value as false initiate value of this is going to be false so until my list is not empty I am going to divide my list by two so if the value of the middle element is going to be equal to the key I'll change the value of match from false to True else it will give you is less than that of the value present at the middle I'm going to make the value of R minus 1. towards the left list and if the value of key is greater than that of middle I'm going to change the value of left as middle plus 1 which means I'm going to check only towards the right of the middle element finally I'll return the value of the Matched variable for this problem and the second I've I've taken an element that is not present in my list okay so as you can see over here for the first case I have two and for the second case okay so I hope you guys have understood this so now comment about something that all of you need to know which is object oriented programming in Python object oriented programming is a way of computer programming that makes use of the idea of objects to represent data and methods it is also an approach used for creating neat and reusable code instead of a redundant one here the program is divided into self-contained objects or several mini programs every individual object represents a different part of the application having its own logic and data to communicate within themselves so what exactly is the difference between object-oriented programming and procedural oriented programming object-oriented programming has a bottom-up approach whereas procedural oriented programming has a top-down approach in oops the program is divided into objects whereas in procedural programming program is divided into functions oops make use of access modifiers such as public private protected best procedural programming does not have any access modifiers oops languages are much more secured compared to those which are procedural languages the objects created an object oriented programming language can move freely within the member functions whereas in procedural programming language data can move freely from function to another function object-oriented programming supports inheritance whereas procedural oriented programming does not support inheritance so now moving on towards classes objects and the pillar of oops in detail so first let's discuss about objects an object is a real world entity such as a pen book person animal Etc objects have States and behaviors in Python everything such as variables functions Etc are considered to be an object a class on the other hand is a collection of objects or you can say that it is a blueprint of objects defining the common attributes and behaviors now the question arises how do you do that well the class logically groups the data in such a way that the code usability becomes easy so for example consider an office going employee as a class and all the attributes related to it like employee name employee age salary ID as the object in Python so all the details of the employees will be placed in the class employee this way you'll not have to write the code repeatedly for all the employees instead a single class will be enough to determine all the details of each employee so now that I want my Jupiter notebook and show you guys how to write down a class in Python so here I'm going to write down the Syntax for a class the first thing that I'll have to specify is the class keyword followed by the name of the class I've already told you in the naming conventions class names begin with the capital letter followed by small letters so in case the name of my class is my class my is going to be with capital M followed by a small Y and cap C followed by small L AWS so this is my class name within this class I can have n number of statements so I'll just say statement one 1 to statement n of a class so now talking about methods methods are nothing but functions that describe the attributes and behaviors of an object now let's take an example to demonstrate the class an object and the third please that is mandatory for python class functions in Java sales is nothing but a reference recorded instance of the class and it is used to access the class variables so let's create a class over here I'll say class new class and within this I'll Define a function and I'll call it the function so like I've already told you all the first parameter to this by default should be the self parameter so when this function is invoked I just want to return hello world now once this is done I'll come out of my class and I'll create an object of my class so I'll just say object is equal to class so this is how you create an object in Python you just have to give a name to your object and then make use of the class name sorry the name of my class is new class not just class next I'm going to make use of this object of new class to call the new function so I'll just say print obj dot new function so as you can see over here with the help of this object I just created I've called the functions present in money class and therefore it doesn't have a world similarly you can have multiple objects and functions in a single class so now let's take another example in which I'm going to have multiple functions and objects I'll just copy this okay so here and I'll take another function named my function I specify the self parameter as usual and for this function I'll just have one return statement that says welcome to edureka so now I have two functions and one object I'll create another object as the objects do and it's a new class object and through this object I'll call my function so as you can see over here I have two functions two objects and with the help of both these objects I'm calling both the functions present in new class therefore I have an output saying hello world and welcome to edureka so now let us take a more complex example of this what I'm going to do is create a class named calculating here I'm going to find the sum difference product and quotient for different values using more than one object so I'll just say class calculating and within this I'll Define a function say add the first parameter should be the self parameter followed by the variables that is a and b now this function is going to return the sum of A and B so I'll say return a plus b I'll say a minus B now let's create a function to find the product so add will become product and from plus I'll change it to Star finally a function to find the quotient so this one add will change to divide and it'll be a divided by B so now I've done this I'll create an object called as o1 and this will be an object for the calculating class Now using this object I'm going to find the result of each of these so I'll just say print o1 dot add and I'll specify some random parameters say 4 comma 5. I'll copy this paste it here paste it here I paste it here and I'll change the names of these functions accordingly so subtract and divide let me change the values as well I'll just say 12. comma 5. product of 32 and 55. and divide 44. by 5 or 15. so I have one object that's calling all the functions that are present in the calculating class in case I want to create another object I can do it in a similar manner so what I'm going to do is just copy this I'll paste it over here and I'll change the name of this object from o1 to O2 wherever I have o1 in this I'm going to change it to O2 so I have two objects and four methods now I want to execute this seems like I've made some error I've made a mistake in the spelling over here it's not CLA it's C-A-L so as you can see over here I've got the output in case I want to differentiate between the results of both these objects I'll just write down a print statement over here and I'll just print a dashed line so as you can see the first half contains the result obtained by object 1 and the second half contains the result obtained by object two so I hope you're clear with this so now moving on towards something very important which is the init method in Python the init method is a very important method that is always executed whenever a class is initiated so whenever you create an object of a class this method will be invoked the init method is always used in real world applications unlike the previous examples that we just saw so now to show you on an example of the init method I'll get back to my Jupiter notebook and over here I'll define a class and I'll just call it as Python programming I'll just use a shortcut So within this I'm going to call the init function or the init method the init method has two preceding underscores prefix and suffix to it so I'll just say double underscore initial scope and the first parameter should be the cell parameter so here I'm going to pass in two other parameters which is name and the age of python once that is done I'll just bind the self parameter with the name and the H attribute I'll just say self.name is name and self dot age is age I'm going to define a new function over here so I'll just say Define new function and again I should specify this parameter and with this I just have one print statement that is going to press both the name and the H so I'll just say hi my name is python so that is self.name and my age is self.age sorry guys I have to specify a plus sign over here so once this is done I'm going to create an object of this class so I'll just say P1 equals python programming and then I'll use this object to call the new function so P1 dot new function I'm sorry guys I forgot to pass the parameters itself so I'll have to pass the parameters I'll just say name is Python and H is 31. so as you can see I have it output saying hi my name is Python and my as31 so now moving on towards object oriented programming methodologies object-oriented programming methodologies are nothing but the pillars of oops which are inheritance polymorphism encapsulation and abstraction so now let's move on and discuss each of these in detail the first thing that I'm going to be discussing over here is inheritance so what exactly is inheritance the method of enhancing the properties of a parent class into a child class is known as inheritance it has a number of benefits such as code reusability where we do not have to write the same code again and again and we can just inherit the properties we need in the child class it also represents a real world relationship between the parent class and the child class inheritance is transitive in nature if a child class inherits properties from parent class then all other subclasses of the child class will also inherit the properties of the parent class so now let me just show you all an example of this I'll get back to my Jupiter notebook so I'll just take two classes over here the first will be the parent class and second will be the child class so I'll just say class parent and then I'll Define a function and I'll call this function as first the self parameter as usual should be specified and within this function I'll just have one print statement that informs first function so next I'm going to declare another class which is called this class as child and this child is going to be a subclass of the parent class so to enhance the better task I'll have to specify the parent class within the braces so I'll just type in the name of the parent class within this I'm going to define a function named second and a cell parameter to it now this function will also have one statement that is the print statement saying second function so once this is done I'll create an object of the child class I'll just say obj equals child and then using this object I'm going to call the first and the second function so I'll just say obj DOT first and obj got a second so as you can see over here this object is able to access both the functions that is first and second the first function is not present within the child class but since the child class is enhancing parent class this function is visible to the child class and therefore it is able to execute it so what we're actually doing over here is subclassing the child class calling a Constructor of the parent class by mentioning the parent class name in the Declaration of the child class is known as subclassing a child class identifies the parent class by subclassing so now moving on towards types of inheritance depending upon the number of child and parents classes involved there are four types of inheritance the first is single inheritance second is multiple third is multi-level and finally hierarchical single inheritance is when a child class inherits only a single parent class so in the previous example my child class is inheriting just one parent this is an example of single inheritance in case of multiple inheritance the child class will inherit more than one parent so what I'm going to do is copy this I'll paste it over here foreign and the self parameter and then I'll just print multiple inheritance so here my child class is going to inherit parent one as well as parent 2. then my object is going to invoke both these classes so as you can see over here in the first example I just had one parent as a base class but in this example I have two parent classes and a single child class the object of this child class is able to invoke the functions present in both the parent classes so I hope you've understood this the next is multi-level inheritance multi-level inheritance is when a child becomes a parent class for some other class I just paste the previous code and over here I'll create another child class I'll say class Childs do and I'll inherit the child class I'll Define a new function I'll just call this as function and the self parameter and I'll print multi-level inheritance so here instead of creating an instance of the child class I'll create an instead of child 2. and then I'm going to invoke all the functions using this object I'll say obj Dot function and I'll run this so as you can see over here all the functions are accessed by this object so now talking about the hierarchical inheritance hierarchical inheritance involves multiple inheritance from the same base or parent class so in this case I'll have two different child classes that are inherited from the same parent class so what I'm going to do over here is just paste the code and then I'll have another child I'll call this as child to itself and this is also going to inherit the band class this is the hierarchical inheritance and then I'll create two objects that is for the child class and the child 2 class so obj dot one will be an object for the child class 2. let me just change the name of this function I'll just copy this paste it over here and I'll change the name of the function from second to function okay sorry guys I just forgot to change the name of the object over here as you can see over here both the child classes have executed and returned the results accordingly similarly you can also have Hybrid inheritance Hybrid inheritance involves multiple inheritance taking place in a single program so just to show you all an example of this I'll take this code I'll paste it over here and I'll create another class I'll say child four or I'll say child three and this is going to inherit the child class as well as the parent class as you can see here I have more than the single type of inheritances the first step you see is a single inheritance and now I have multiple inheritance as well similarly you can have any other type of inherence also so the final function say function two the cell parameter and then I'll print hybrid okay so now let me create an instance of this class just copy this I'll paste it over here I removed these objects since we've seen their output so as you can see I can have multiple inheritance as well so now talking about the super function the super function in Python allows you to call a method from the parent class so to show that to you guys what I'm going to do is take the original two classes which was the parent and the child class I just paste it over here and within this function I'm going to type in something called as super dot the name of the function that's present in the parent class so that is first so now let me create an object of this I'll just say obj equals child and I'll execute the function present in the child class so I'll just say obj dot second sorry guys I just made a mistake I specified Capital as it is small s so let me just execute that again so as you can see here in the previous example I had to explicitly specify my object to call both functions present in the child class and the parent class but over here my object is accessing the function present in parent class by making use of the super function so now let's move on towards the next pillar of oops which is polymorphism defines the ability to take different forms by making use of polymorphism you can redefine methods in the child class with the same name as that of the parent class polymorphism is taken from the Greek word poly which means many and morphism which means forms it means that the same function can be used for different types this makes programming more intuitive and easier in Python we have different ways to Define polymorphism a child class inherits all the methods from the parent class however in some situations the method inherited from the parent class doesn't quite fit into the child class so in such cases you will have to re-implement by talking to child class this can be done in many ways such as using different functions plus methods or objects to Define polymorphism so to understand this clearly I'll get back to my Jupiter notebook and show you all an example of this so I'm going to have different classes which will have functions with the same names so let me just Define the class call this apple and this class will have a function called type with this as a software statement that's going to tell me if this is the vegetable or fruit so as we all know apple is a fruit I'll just type in fruit and then I'll create another class called as potato here I'll create a function with the same name but with a different implementation and instead of I'm going to type in vegetable let me just change the name from type since type is a very general keyword in Python so just say type off so now I'll create two objects I'll say obj1 and obj2 and by using these objects I'm going to invoke both these classes so I'll say obj1 Dutch type of and then obj2. type of now I'll just run this code so as you can see over here even though I have two functions with the same name my objects are able to access the right function and return the output you can also have polymorphism along with inheritance polymorphism like I've already told y'all defines method in a child class that have the same names as the method in the parent class so as discussed in inheritance the child class inherits the methods from the parent class also it is possible to redefine a method present in the parent class in the child class this process of re-implementing a method in child class is called as method overriding so now let's take an example of that I'll just define a class called as bird and within this I'll Define a function called as information now within this I'll just keep a general print statement that is to say that birds birds are very pretty so now I'll create a subclass of this class I'll just say class and I'll specify the birth as parrot I'll inherit the birth class over here and I'll Define a function and I will redefine the function information here I'll just print parrots can fly so now I'll just create an object of the Paris class and I'll say obj dot information so as you can see over here when I've called the function information which is present in both the base class and the inherited class the function present in the inherited class has been executed so that means I have redefined or overwritten my method that is present in the base class and that method has been executed so that was all about polymorphism now let's move on towards encapsulation encapsulation deals with binding up the data together with the code that is used to work on that data this not just makes our code clear but it also helps in preventing data from being modified as well in oops a classic example of encapsulation is a class A Class encapsulates all the data and the code such as variables methods Etc data that is encapsulated can have three types of access specifiers in oops these access specifiers are protected private and public members having Public Access specifiers are visible throughout the code protected members are visible just within the class and the subclasses whereas private members are visible only inside the class in which they are present even the inherited classes will not be able to access them so one thing I want you all to know over here is that python in real has nothing such as privacy model it does not have exclusive access to modifiers such as C plus plus C sharp or Java therefore there are no truly private or protected attributes in Python now I'm sure you'd be thinking then what is the use of preceding single and a double underscore python members that have names with the preceding double underscore are mined in order to protect them from clashes with the inherited classes subclasses are also allowed to Define their own private members which will not interfere with those of the parent class this is why they are considered to be private they are accessible outside the class but less is the chance of a clash mangling in Python is achieved by prefixing any such name with an extra underscore and the class name this in turn provides an effective namespace so in case I have a class named parent so I'll just say hash parent and if this parent contains a member with a double underscore so I'll just say underscore double underscore private member at the compile time this private identifier is going to convert to underscore parents double underscore private member so as you can see over here the name has been mangled so therefore in case I try to access this private member anywhere outside the class by just making use of a double underscore followed by the private member name I'll get an attribute error similarly if I have a child class I'll just say child and if this child has a private member then at compile time it's going to be something like underscore child double underscore private member so I hope you guys are getting this the names of members proceeding with a double underscore are muggled such that you have a underscore followed by the name of the class and then the name of the member that has a double underscore so what I'm going to do over here is I'll define a class say a and within this I'll call the image method through this init method I'm going to invoke the so-called private method so therefore I have self dot double underscore hi the name of my method is high I'll declare another method called as hello world so this method is just going to print hello world so here I've declared a method named hi but I've not defined it so I just Define underscore high so I double underscore High to which I'll specify cell parameter again and I'll say print welcome to edureka now I'll create an object of this class and first by making use of this object let me try to call the hello world function so as we all know this object will be able to access the Hello wall function so I'll just execute this so as you can see over here I have both the outputs which is welcome to edureka and hello world as you've seen I've not called this function which contains a double underscore but I still have the output to it now if you guys are wondering why the output is seen now this is because I'm calling the double underscore High method through the init method as I've already told you all the init method is always executed whenever I create an object of this class so in case I do not write this method so I'll just delete this from here and then if I execute this problem you'll not be able to see the output for the high function in case I want to invoke this function I cannot directly use obj dot high I'll have to type in obj dot underscore a double underscore hi and then execute this so I hope you guys deal with this the name of high has changed from double underscore High to underscore a double underscore High so what happens when you have a single underscore let me take another example to explain this to you all so I'll just say class base and then I'll Define a single underscore class I'll just say single and I'll just print saying single underscore next I'll Define another class called as double underscore or just double and then I'll print a message saying double underscore now I'll create another class which is the child class of Base Class and through this class I'll try to access both the functions present in the base class so here I just type in self dot the name of this function which is single sorry I forgot to define the function over here so I'll just say Define function one where the sales parameter I'll Define another function with the self parameter and then I'll say self.double so now let's create an object of the child class so I'll say obj equals child and I'll call obj Dot function1 and function2 so now let's see what happens so as you can see over here the function having a single underscore has been inherited and executed by the child function whereas the function that contains a double underscore throws an error saying child object has no attribute named so in case you want to execute this all you have to do is just type in the name of the Base Class preceded by an underscore so I'll say underscore base and then I'll hit run so as you can see now both the functions have been executed using the object of the child class so I hope you guys are clear with this now moving on towards the final topic of this session which is data abstraction in Python so abstraction basically means to show the necessary data and hide the implementation details this not just increases the efficiency of the code but it also reduces its complexity for example consider your washing machines you know what happens when you press certain buttons such as on off save water set water level change program Etc but the actual implementation of these programs is hidden from you you do not know how it's being turned off or turned on you do not know how it knows to take less water or more this is called as data abstraction abstraction can be achieved by creating abstract classes any class that contains an abstract method is called as an abstract class so what exactly are abstract methods abstract methods are those methods which just have names but do not actually contain any function definition so that means that you clear an abstract class but you do not Define it these functions are also called as unimplemented methods therefore an abstract method does not have any body in Python this can be achieved by making use of the past keyword however an abstract method needs to be redefined in a subclass where it's being used to achieve abstraction in Python you can make use of the ABC module that is the abstract Base Class module so to show an example of this I'll get back to my Jupiter notebook I just type in a heading so the first thing that I'm going to do is import everything that's written in the ABC module 30 from ABC import Stop so this is going to import all the necessary functions that are present in the ABC module I'll create a class over here called as Bank within which I'll have a function that describes the name of the bank I'll have another function that is going to find the location of the bank but this function is going to be abstract or I'm not going to have any function definition next I'm going to define a child class called as location through which I'm going to redefine the abstract method IST sorry class bank so the bank class is going to inherit the ABC class in case you want to know what this ABC class does I just add in a kernel over here and I'll say help on ABC so as you can see this is a helper class that provides a standard way to create an ABC using inheritance so now moving on and if I do functions having Bank name and the cell parameter I just went the name of the bank I'll take HDFC and then I'll Define another function called as Bank location I'm not going to provide the body of this so I'll just write pass now I'll create a child class I'll say bye underscore location this is going to inherit the bank class and then I'll redefine the bank location function I'll press the location as my law finally I'm going to create an object of the bank location class I'll just say obj equals Bank location and I'll call both the functions so as you can see both the functions have been executed successfully [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 are 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 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 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 different language guys and do not repeat your comments as they become redundant now that you know what are comments and their importance let's see how to write them in Python comments in Python are received 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 a Jupiter notebook and write some comments All I'm going to do over here is I'll open a new python notebook and I will rename this notebook to rename your 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 presentation and see how iterated 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 under 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 string let's go to a Jupiter notebook and see what happens I'm including a hash character within a string and I will run this as you can see when I run this the string is returned which means a hash inside a string means a hash itself and nothing else so you cannot write comments within a string okay so I hope you've understood how to write comments now let us move 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 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 prefix 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 commit 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 the hash character just like how I'm showing here and then type it once and it appears wherever I have inserted the cursor as you can see on the screen 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 go so these are some Shortcut methods to comment multiple lines many of us think that comments and dog strings are same now let's move ahead and see what the doctrines and whether they are actually comments or not blockchains are not actually comments but their documentation strings these strings are written within triple quotes they're 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 are omitted by your interpreter and nothing present after the hash character is going to be read by it on the other hand docstrings 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 our Jupiter notebook and see their functionality like I've told you before doctors as you can see in the output when I execute this it Returns the string itself whereas in case of a comment when I include the 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 dark 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 dark string initially before the code starts as you can see my output the dock string has not been returned whereas when I use the doc string individually without any code and I executed it you could see that it had returned the string itself but when I include the dock string before the code and after 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 dock string has been returned as my output now let's try to include a dark string after some good it has returned the output for the code as well as the spring 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 dog strings as well [Music] so let's try to understand what exactly is an operator an auditor 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 locations where we store our 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 clear a list in Python list is also indexed just like a string so you 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's 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's 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 a 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 don't get these values I am declared in the double so the difference between a couple and a set is not index and it does not contain any duplicate values but a double 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's a list of all the Auditors that we have in Python so first of all we have arithmetic operators then we have assignment operators comparison operators logical operators then you 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 automatic operator symbols that we use in Python so we have addition subtraction multiplication division modulus exponentiation and floor division so let's take it up to 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'll try to perform addition between these two variables so I'll just write plus over here and when I execute this I'm going to get the addition as my output not to perform subtraction I am going to use the subtraction operator and it is going to be the value as the subtraction over here similarly I can perform multiplication then I can also perform exponentiation so I'll use two asterisks instead of one for exponentiation it is that easy in Python guys now we will perform division over here so it is going to be 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 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 automatic 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 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 understand what all these are used for for example let's say we have a real X which has value 100 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 you 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 5 so we have 5 over here and we have added five more to the statement which means we are getting the result as 10 over here now let's try to understand other assignment orbitals as well so let's say exponentiation n is equal to 5. so let's see what the output will be so append this I'm 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 operative so comparison operators are nothing but the operators that we are using to compare two values or objects right so we have comparison operators like equal not equal greater than less than greater than or equal to and then we have less than or equal to as well so let's try to take an example over here so when I use the equal operator I am going to write the statement as just have written over here x is equal to Y so when I'm using the comparison operator I'm actually using double equal to so this is the basic difference between assignment operator and comparison operator so let's take it up to jupyter notebook to understand these comparison operators guys so let's say I have two variables value is equal to 10 and number is equal to 20. so to understand the comparison operators let me take one more variable I'll say this will be our Boolean variable so what I'm going to do is I'll write Val is equal to number one what I'm checking is or comparing these two values so what exactly I'm doing is comparing these two variables or values that we have that is Val and number one so when I print this compare variable what I'm going to get is either true or false so it is going to be false because value is not equal to number so I can do the similar thing I can check whether it is greater than or I can check if it is less than so when I write 10 over here so I can similarly check if it is greater than or equal to I can check if it is less than or equal to so these are the comparison operators that we have in Python which we can use to compare two values or two objects now let's try to understand the next operator that we have which is The Logical operators so logical operators are used to combine conditional 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 if statement then we have an as if statement and we have an else statement 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 as 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 take this condition if it is true it is going to print the 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 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 Jupiter now let's say I have two statements all right X is equal to 10. and what I'm going to do is I'm going to check if x is greater than 10 all right n 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 the statement I'm 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's showing me the value that is false but let's see what happens if both of these statements are true I'm getting true as well so one of the statements has to be true when we are using the or operator now talking about logical not operator what I'm going to do is I write not in the beginning now I'll specify the condition let's say x is greater than 10 and X is greater than 5 this is the same statement that we have used before and for this we have got the output as false so let's see when we use the logical not operator what is going to be the output I'm getting 2 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 the statement so this is the purpose which fulfills by using the not operator or 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 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 output that I am going to get I'm getting true because X is actually the same object as list one now let's write list one is list two even though when we have used the same values in this one and this 2 let's see what output will be that is false because these are not the same objects right similarly I can use list one is not list two so here I'm going to get the output as true because this one is not exactly list two even though we have the same values it is not going to be the same object so this is the concept about the identity operators now let's talk about the next one that we have is membership operators in Python so membership operators are used to check if a sequence is present in an object or not now to understand the difference between an identity and a member operator okay let's first understand what are the operators in membership Gap 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 interpreter notebook we have already specified these objects so let's try to do if x n list one let's see what happens it is going to show me that it is false because you have no values in X so when I do the same thing with list one in list two now you see we have the same value over here let's see what the output will be it is false again okay let me do one thing now check a particular value or a particular sequence what I'm going to write is I'll check if 10 in list one so it is going to check if this is going to be present over here or not it is showing me that it is true so what if I write like this 10 20 30 in list two it is false so this is the basic difference between is that is the 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 I'm going to 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 a basic concept of identity and membership operators guys now let's understand bit by 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 bitmizer operators that we have in Python so these are the bitwise operators like bitwise and bitwise or so these are the symbols used for these operators guys so this is actually what happens when you're using these operators so let's understand bitwise first in Jupiter notebook so what I'm going to do is I'll just write 10 and 12. so what is going to happen over here let's say it is showing me the out output as 8 but it is not clear why you are getting the Prototype 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 I use the logic and operator what is 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 has happened 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 it is going to be one so all of these bits in the output will be 1 1 1 which is exactly is equal to 14. so this is the concept of using the bitwise operators still 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 images numbers is 1 0 1 0. so when I right shift these two bits I'm going to be left with one zero and when I convert this to to Binary I am going to get the value as 2. so this is the concept of friendship 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'm getting this value so 10 if I write it in binary is going to be 1 0 1 0. so when I do the left shift what is going to happen is it is going to shift two values and I'm going to get the value as 1 0 1 0 0 0. so what is happening here if I convert this binary number to decimal I'm going to get the value as 40. so this is why I'm getting the value 40 over here so this is the concept of bitwise operators guys I hope you're clear with the concept of bitwise operators it is a bit complicated because we are using the binary numbers even though I am telling you these binary numbers you need not mention them while using the operators for example for these bitwise operators all these are 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 have shown you and then before beta is all 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 0 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 in Python and it is in terms of indexing which means you can access the elements of whatever using indexes and they can also have duplicate numbers just like the list as well and it is a collection data type that stores python objects separated by commas and let me show you an example it will explain how we can create or declare a tuple in Python so let's take it up a Jupiter notebook guys as you can see now we are in the jupyter notebook and I have a file name Tuple in Python over here so I'll show you how you can declare a table in Python so just take variable here and inside this let me add a few values inside round brackets so just go with one two three four and five because this is a very basic example and when I print 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 double 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 fine for both of these approaches to actually declare a tuple in Python so this is how we can create a tuple in Python the second next topic that we have which is accessing elements in a double so accessing items in a tuple Works similar to a list and we can access items 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 not 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 listing Etc so what happens is let's see for example we have a double 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 into the end of double that is the number five and the index value at number five will be four so in Python we can use the negative indexing as well to access elements in a double 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 all so we have AO here so inside this web access I'll specify the index number let's say 4 over here because we have five elements so it should really you know give me the value number five okay it says index out of the range because the only three values over here so I'm getting to this also like reassign the values so I'll just write two nothing the value of 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 we'll 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'll 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 our technique in which we use a slicing operator which is colon and it is used to get a range of elements from a couple or any other data type that supports indexing for accessing elements so let me just show you an example this time I want the video to be distinct and I'll just specify the numbers as let's say okay these numbers from one to nine so when I print this I have a tuple which has values ranging from one to nine now talking about slicing it more similar to indexing only it's just that you'll have to add that slicing operator so I'll start at the index number okay zero and I want to go into the end of the top so when I print this I'm getting all the values inside the Tuple so in this Tuple the ending index is basically eight so I'll just write 8 over here and we are getting 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 like 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 such as minus eight now press 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 doubles are immutable in nature but an estate 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 I'm 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 50 to 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 list guys now let me try to do this with other elements so let's try to change 30 to 35 essay so I'll specify the index number two let's say I'll write 35 object does not support item sign so it's basically we're on this arrow 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 purpose is very easy task so you just have to assign the ID number two numbers to another variable and it will return the concatenated Tuple with the values of both apples so let's say you have a couple of the variable a and then you have another variable B with another double 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 couple 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 double so I have actually concatenated both inside one Tuple so this is how concatenation works with tuples guys so now let's take a look at the next topic that we have which is so being a new data type a tuple in Python does not allow any changes and you cannot even remove an element from part of 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 table over here so this is how deletion applies you cannot delete just one element from an Apple instead you can delete the whole Tower 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 they are basically account method and an index method so can't basically Returns the count of the items and index will return the index of the item specify 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 once over here because we can have duplicate items in that couple and let's just say two three four and five virus 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 1 and it will return the number of times one has been written inside double which is 6. similarly the next method that we have is index so to check the next value at a particular element I'll just write 5 over here and it will return the value of the next 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 the other count as six for the element one it will give the index value as 0 only so this is all of the Tuple methods let's take a look at the next topic that we have which is list versus double points first is used for homogeneous data types and a tuple is generally used for heterogeneous data types a list is mutable in nature and apple is actually immutable energy which helps in Faster iteration as well so Tuple is basically faster when it comes to iteration like you're working on for Loops or any other Loops for iteration statements so if you're using any iteration statements double is much faster than our list and at least does not have any immutable elements but immutable elements can be used as a key for addiction while you're using a double last but not least we have no guarantee that it is right protected inside a list but if you are using a tuple implementing a tuple with data that does not change guarantees that it is write protected so these are a few basic differences between less and double so let's say double Constructor it's possible to create a tuple using a double instructor as well I have shown you the two different approaches you can follow to create at Apple but there is a couple of startups you can use to actually create a double so let's take it up to jupyter notebook again I'll show you how you can use the double Constructor 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 on 20 so using this double Constructor I can actually change the list into a tuple guys so when I run this no errors now when I print V over here it will have all the elements inside the list but design it's a double so we can actually use a tuple Constructor to change a data type for type passing and it can use 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 double so I 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 double or not so the first example is going to show you how you can check if an element is present in a double 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 nine in a which is true and I'll just try to check F10 in a which is false and I'll check if a string in a and forgot the data it's not necessary that you have to use the integers only you can use the string or any other data types like I've used the list data type 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 your water sets let's move on and see when the U sits in Python sets can be used when 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 basis and the second is to use the set Constructor I have small example where I create a set a using curly braces and a set B using the set Constructor now let's move on towards our 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're 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 one now when you print this set you can see that the set is empty if I return 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 the 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 bir 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 the session today first of all we will be finding the length of 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 set you can use the land method the length 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 but 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 Lin function and pass the name of the set as parameter now let's go to our jupyter notebook and see what happens when I do this I've already created set a b and c I'll just rebuild 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 use 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 that contains are all elements you cannot use index values to access its elements but you can Loop through it to access the element surface set I'll just jump onto my typical book and show you all how to access set elements or we print my set 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 show you how to use both these methods in this session so here is my jupyter notebook when I use the update method I've passed a list of values to be added to that c but here I've passed three again and three has been either just once and it's not been repeated again this is because effects contain unique elements and there's no second copy of one element present in the same so I hope you 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 move 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 that element from your set now let's get back to our Jupiter notebook and use these methods I'll read all these over here first I'll 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 a it has thrown and key error and it's specified that 6 is the key which is not present inside a so I'm going to understand how to use the remote method now let's use the discard method and see what happens when I used B dot discard T it has removed three from the set T now let's try to give some parameter to the discard method which is not presented at B when I used B dot discard 90 it has executed but it has not thrown any error so look at your body methods now let's try to use the pop method when I use a DOT pop it has popped some random value from the set a and I return 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 concatenation of two or more sets into a single set but in case you have any common values in this set the resulting set will contain just one copy of 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 as for 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 the notebook and see how this works I'll rewrite my sets a b c over here I've used the pipeline symbol to find the union of A and B all the elements that are present in A and B has been returned as we said 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 presets using both these methods as you can see in the output I've used pipeline symbol to find the union of presets and then I use the union method to find the union of a b and c so I hope you're here 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 you never found this operation in two ways One is using the Amazon symbol and the second is to use the intersection function I'll be showing you all how exactly to do this on a Jupiter notebook I use 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 result is a string which is present in all these sets 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 your understand this concept clearly let me jump onto 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 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 use 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 done 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 Frozen set a frozen set to set whose values cannot be modified which means when you free the 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 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 the set now let's see what happens if I try to add some values to this Frozen set when I used D dot add three it has returned an attribute error which says Frozen set object has no attribute therefore Frozen cells are immutable and you cannot change the values present in the Frozen set Frozen set helps to serve as a key in the 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 means 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 addition 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 but dictionary does not have any replicate members as well now that we know what it is let's talk about why we use the dictionary in Python first of all the reason would be is alert and stores data like a map so it is one unique feature that python digitally has and it also contains the key value pairs unlike any other data type or object in Python it's 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 mentioned duplicate members in the values but the keys has to be unique in order to access these members since there are no indexes also 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 our list in Python so I've listed down these differences so first of all list is ordered but the dictionary is not there 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 you 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 order data but dignity is preferred when the data has unique key values so this actually differentiates a list from additional device and the next one is List have duplicate elements as well but in dictionary we do not have any duplicate elements when you 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 particularly is in Python and why we use it and how it is different from a list let's try to understand how we Implement auxiliary in Python first of all to declare retention 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 Italy 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 Implement dictionary in Python let's enter the presentation mode guys so we will try to implement a dictionary here let's take the name particularly 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'm specifying the key value pairs I have to use the cooling to select the key from the value now I give one value over here in part dictionary we have to separate these key value pairs with a comma I'll give another key give it a value you would say data science okay one more key and give it a value let's say python so this is my dictionary guys this is how we actually Implement recently I will create 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 you can see when I run this I will get the value that is specified in the first key value which is I can simply just print all of my dictionary guys so this is how I'm going to get the output as when I printed actually but I've just specified now to access this elements I can also use the get function and inside the 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 Edition e 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 just released now I'm going to print my dictionary here let's see what all values I'll get I'll get one more value and I just added which is data science so this is how you actually Implement dictionary guys and to access these values you can either use the square package and inside those square brackets you can specify the key value or you can just use the get function inside the get function and inside the Assumption you have to specify the key value as well so now that we have understood how can implementation in Python let's try to take a look at the operation that we have in Python dictionary guys so as I've told already told you how you can actually add an element to your dictionary just like this I've specified a key value and given it a value and then there is a replacing element which I 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 article 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 X-ray first so you will understand what all these operations are going to do so I have this dictionary which are 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 I run this okay so now I print my dictionary let's see what our values I'll get it is saying that it is empty because the reaction has actually deleted or removed all the elements from the dictionary so this is what clear does this now let's take a look at the another one which we have also I would want to declare this dictionary once again and when I print this I should be getting all these values again yes I'm getting these values so it would be fine if I do one more thing so I'll just use the copy over here so what copy does is it returns a copy of the dictionary guys so let's say when I print p over here what is going to happen it's going to show me a copy of this dictionary that I've just specified over here using the copy function so let's take a look at the next one we have which is 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 how do you make a 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 values of the dictionary with the specified key value pairs so let's say if I use updates 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 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 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 couple 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 no key so this is a double of each key value pair which you can get using the items function now going 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 just pop this from the audition 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 I'm using the PowerPoint 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 that default does is it Returns the value of the specified key it does not exist we can insert the key with the specified value so I'll specify the value over here let's say one now when I print this I'm getting a UK as well so when I specify the next key it's showing me okay it's a syntax error I'm getting the next value so this is what set default is used for which is return the value of the specified key guys so this is all about the operations that you have in dictionary guys 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 values I will use the keys so let's check key one only and I'll give the value let's say and Eureka now I can get another key value let's say 2 and I'll give the value let's say python now let's see if I can get additionally from this so using the dictionary Constructor let's see if we have actually implemented additionally over here so as you can see I have declared additionally using the dictionary Constructor guys so when on this I should be getting a dictionary here so as you see I have declared a dictionary using the digital Constructor also one more thing I have missed over here when I was explaining operations guys so which is the front Keys operationally over here give it a value let's say one two I'm giving you the random values guys don't be alone now when I'm using the front Keys keyword what I do is I just say a DOT from okay we don't have AO here so 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 additionally with the specified keys and values plus we don't have any key one key to inside this so we are getting the none value over here so this is what prompties does inside a dictionary guys so now that we have actually learned what is the dictionary and how you can implement it in Python and all sorts of operations that can perform on a dictionary let's try to understand what a nested is very nice so for this I have a problem statement guys so now that you know that ICC Cricket World Cup is just around the corner guys so what we'll do is we will Implement additionally with the stats of the 15 members from the Indian cricket squad for the World Cup 2019 and we will use the concept of next dictionary in the use case so what we do is we implementary 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've also import a pandas package and convert those data into a data frame for better display statistics so let's take it up to python guys so okay let's exit the presentation mode so over here I have the value originally that I've implemented so let's check it up to when presentation mode again so as you see I have this module that I've actually imported from pandas I have given it an alias as PD so we'll talk about this later so this is magical guys in which I have made four more dictionaries guys which is actually on aesthetic Series so firstly is batsman inside the batsman key I have all those batsmen like Rohit Sharma security 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 Batman key I have two more dictionaries so this is the concept of nested dictionary so similarly I made for all around and we have so this is my dictionary guys that have actually stored all the data using the dictionary or as you can say nested dictionary and then I have all these statements I have written in which I have converted the data inside the dictionary into a database 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 word of God so as you can see I'm getting a data frame over here so I have these values and Virat Kohli as you can see we have average high score matches and runs over here so for that we have more than 1000 runs six other one has 5000 runs and root Sharma has around 8000 runs guys so let's try to understand more from the design that I've just mentioned over here we have got all the batsman now we will try to get all the bowlers so so I'll just print the statement and it will show me all the polar statistics 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 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 nice so let's say inside or foreign [Music] why we need Loops now let's understand this with an example that is there in front of your screen suppose you are a software and you are required to create a software module for payroll processing of all the employees in your office number things you need you need the salary of those deploys the bonus and the total amount that they'll get at 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's see what are those two ways so this process is actually pretty hectic you need to repeat the same logic 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 will 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 hydrating that logic for the total number of employees are there so when it's new brands once it will start your first employee for the second time similarly it'll keep on repeating so if you have 100 employees it will repeat 400 times we get here the major advantage that we get there is we don't need to write the logic for each employee 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 coefficient 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 of thousands of exams that you can think of by any Loops right so this is just one simple example I've shown you in order to make you understand why we need loops so let's move out and see what exactly are loops let me explain this is now what are loops loops basically allows us to execute a statement or a group of statement multiple times let me explain you this with the help of an example that is present in front of your screen now over here what happens the control comes here and checks the condition so if this condition is true it'll go on and execute the condition code that is here this is nothing but the statements that represent title Loop so it'll execute that and again we'll go back and check the condition so if the condition is still true then again it will execute the conditional code the statement was inside and it'll keep on repeating until the statuation becomes false and the moment this condition becomes execute statements are present after that Loop now one thing to notice here guys is that there can be two kinds of Loops one is fine this is actually 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'll keep on repeating and it'll never stop that is what infinite Loops are so basically the condition will never be false and until that Loop is equal to infinite number of times now there's one more way in order to categorize Loops that is called Hostess and pretest in postness Loops what happens the controls will first enter the loop and then in the end it'll actually check the condition but in pre-test group the control will enter the loop only when the condition is true so the condition is checked at the end of the loop now in Python there are no process Loops present there are only three test Loops so we'll focus on details session Loops that are present in Python so python daily supports three kinds of Loops one is fine forward and nested 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 by Loop is basically used when you don't know how many iterations are required so let me explain you this with the help of before there in front of your screen now over here what happens a control will move inside the loop only when this wild condition is true and when it is true it'll execute audio group and then again it'll go back and see whether the condition is still true or not it is still true and again it will execute the body I'll keep on doing that until the condition becomes false and the moment the condition comes false it'll come out the loop and execute the statement represent after Loop now this is a syntax for while loop first you need to write y then you write the expression of the condition then you can call it and the statements now why we use basically I've told you earlier as well why Loop is used and 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 the loop so that's 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 and after that I'm waiting please the complexity of the code so first we'll see a very small introopy example in which I'm going to print the integer values between 0 to 9. that would include nine so it'll print zero one two three nine but it won't Infuse nine so let me open my Python and show you how to do that now this is my python guys integer values between 0 to 9 but that won't include 9. so for that what I need to do is I need to First Define a variable let me name it.com and I'm going to initialize value to it by zero now after that I'm going to use a while loop and I 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 account 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've 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 nine 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 and the control should come out of the loop after that it's just a general syntax I have given a colon and then I've written a print statement how to print the count value and after that I have increased the value of my count by one finally when the control comes out of loop I want to print the good part so let's see whether it works or not I'm going to run it and as you can see that it works zero one two three four five six seven eight and finally when the control went out of the loop it has printed goodbye this is just 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 increase the complexity of it and I'll give you one more example so let's 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 case okay 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 a little bit number is too small if you have guessed the number that is greater than the random number then the number is too large and the moment you pay the right answer you'll exit the while loop and you have on the game basically so it's just a pretty simple example but it's a perfect example I got to show you why you use by Loop because if you notice here I don't know how many iterations are required in order to catch the correct answer so at that time while loop is a perfect solution to this problem so without time will actually open my Python and show you how to execute this now what happens first I need to import a module that is called random in order to generate a random number now I have defined variable n and I've given evaluate that is 20. because I want the random number that is to be generated should be between range 0 to 20. now I've taken one more variable and I've named and over here it should be an integer types of an end 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 I'll be generated with the help of random modules that we have about so it'll be n random dot random plus y 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 evaluated initialized zero all right so now comes 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 okay now if that guessed number or the number that you're guessing is greater than zero then again there will be one more if condition which is 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 the print number is too small now there's no condition that is Health 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 is over and you've lost it finally if you have guessed the correct number then it will print congratulations you have made so I hope you have understood the code that I was explaining it so let us go ahead and execute this and see what happens all right so ask me for a new number so that's 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. once more that's a new number so I'll just type in say 14 it says number two large so I'll type in 11. again the number is too large that means it's between 0 to 11 so how about eight guys number is two small so now now as you can see that quality three iterations are occurred and uh for the over that direction definitely the random number will be somewhere between 8 and 11. so how about ten so congratulations you have made it and the control came out of the while loop and that's printed congratulations you made it so we move forward and see four Loops in Python support Loop is basically when you know the number of hydrations are required unlike where you are not sure about the number of iterations are required you know how many times the statement represents need to be executed the for Loop provides you with the 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 accounting variable and then the incrementation of the county variable so as you can see from the syntaxes when 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 store diagram itself the control answer this and you'll see the item from the sequence it will execute the statement go back again and then from the range it'll pick up the next item okay execute statement again go back and then from the range it'll pick up the third item and it'll keep on executing until range becomes over or the last item from the sequence when there are no identity the statements represent after the follow so this is how for Loop Works guys now let me just give you a very small example of how for Loop works I open my python again and I start with a very small example and then I'm going to increase the complexity of the 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 I'm going to name it as fruits so what 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 a three fruits mango grapes and apple now I'm going to define a for Loop and over here what I write I'll Define a variable say fruit for fruit in fruits give a cool lip print current fruit and type in fruit now when you come out of the loop just print again goodbye so I have to get 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 amplify the for Loop it says that for a variable name fruit in fruits so this will give you 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 viral for you and for you I know the table is three iterations after that print the current fruit that's all and when the control moves out of the loop okay goodbye it's pretty easy guys so go on execute this and see what happens so yeah the current root first is mango then comes grapes it 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 factor here now one thing to note is here guys again in factorial also I know how many iterations are required because if you see if I'm calculating the factorial and I know that I need multiplication five times four by three then three with two and then two with one so this way I require only four iterations so I know the number of operations 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 Python and execute this practically so this is the code in order to find the factorial of a number so here what happens first I need to input the number that I want to calculate the factorial off initialize the value to it which is one now I have to find a condition if the number that I've entered is less than zero then print the number must be positive because I can't calculate the factorial for negative numbers right and again one more condition which says that if number is equal to equal to zero then factorial is equal to one and then else condition says that when number is greater than zero then I'll use a for Loop it says that for I defined a variable I in range 1 to num plus one but right now plus one the rate will be between 1 to 9 plus 1 but it won't include num plus one so it will only be including one till num all right factorial is equal to factorial into I so factorial was one earlier then it will multiply by I I will also be one in the beginning and then again it'll go back and it will become too similarly to keep on increasing 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 factors of 3 which is 6. so if I want to enter one more number so what I do is I can start once more so what I like I can type again four seconds 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 another loop for example you can use a for Loop inside a for Loop or you can use a by Loop inside a while loop 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 a nested Loops now when I look at the example of nested Loops as well don't worry so let us move over and see one example of nested Loops 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 because you need to enter four digit PIN and then you can perform the following functions you can check the balance you can make withdrawal you can deposit money and you can even if you don't want to do anything you can tell the machine to return the car as well so let me show you how it is a quote that I've written in order to simulate Bank ATM lines so first line there's nothing to print statement that says welcome to unbank browse ATM all right and then I have a variable restart and I'll send 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 67.14. notice you over here you won't get more than three chances in order to write the correct password now comes the value 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 bit 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 n o small no or capital N so it should not lie within this particular sequence that is n capital no and N but we know that restart value is y so it will enter the while loop now over here what happened lq4 option first option is in order to check your balance for that you need to press one so maybe at all you need to press two in order to pay in some cash or you need to deposit something you need to press three or you want your card back you need to press four so these are four options that will be given to you so when you choose option one it will print account balance so already that you can see that your account balance is and the balance that is there in your account after that it'll ask you for a restock so you need to input that value so that will say would you like to go back and if you restart value lies in this particular Tuple then it will break a little bit 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 and o or this node or capital N so if there's any other button apart from these it will actually go back and the why statement as you can see it says that it is not present here so I'll give you all these four options once more now after that what if you choose the other option in which you make a return so for that what will happen you need to enter the amount that you would like to withdraw which can be a float number so that's why you slot here so you need to enter that module and once you enter that value it'll check whether it is in this particular list or not that is now by default it will restart by because you need to enter the amount once more so again it'll go back and it'll give you all four options so you can choose the draw option again and you can choose a number which is present in this particular list now when you choose withdrawal equal to equal to one at that time it'll again ask you please enter the desired model so this is for option two now when you talk about option three it's pretty much similar to the couple of options that we have discussed before over here again whatever you want to pay in now how much amount that you would like to pay and balance plus the amount that you are depositing so it will print the balance that will be equal to balance plus the amount that you're depositing again apart from this particular couple then it'll go back to the while statement and it'll give you all these four options similarly for option four as well now what happens when you enter the indirect plane so there's one more condition in the end which says that if the page that you have entered is not equal to one two three four then it will print that entire password and it will decrease the chances by one so only you at least if you press they make pin once your chance and it'll make us make it as minus one which will become one and if the third time also you do the same thing then the chances becomes you and the more it becomes you they will print no more twice break it'll come out of the loop that's all guys let's 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 I'll give you four options which is one for balance two for patrol three for pain four to return a card so if I wanna check my balance at point one so it has printed my balance to 65 7.14 and then the restart value that I was talking about 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 will come out of the loop and it'll break and if I press anything apart from small n capital I know small n or capital N it'll actually go back and not give me all the four options give me all the four options that are there you can see I press one so again if I want to make it roll I'll press two how much amount that I would like to 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 again press Y and it will give me all four options if I were paying something I want to deposit something I'll press T how much I would like to pause it say 10 so my balance became I get 67.1 so again that mean we 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 so let me show you what happens if I didn't say five one one one interact in if I write and again again correct again correct so we have no more tries it'll come out since I've exceeded the chances that I was given so I've given three chances I couldn't write 10 in those three chances so it'll come a little I will stop so this is an example of nested by now let me go back to my presentation and show you an example of follow as well so this is an example of nestle for Loop so over here what I'm going to do is I'm going to write a code in order to print the python number between a particular range so what happens is suppose if I want to enter the number 20 that means I want the python numbers between 1 to 20. so by the way numbers are nothing but the integers which satisfy a squared plus b square is equal to c squared so at that time it will print all the Pythagorean numbers that are possible between one to twenty three four five you can see that three Square Four Square is equals to Pi Square 9 plus 16 Z equals to 25 similarly it goes for 5 to 36 again so first over here I need to import some modules in order to use the square root function so from math module I'm going to import the square root function after that ask me for the number in the example you can remember the slides that are written 20 so it has printed the python number between 1 to 20. that same number it will ask me and it depends by the 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 available in range one to n plus one that means one to ten that means one to n plus one but don't include n plus one after that is one more variable B which is in the range a comma n that will not include n it only include till n minus 1. so the square root of c squared I will find one more variable C squared is equal to a exponent 2 or a to the power of two 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 squared so if that c square is minus c squared is equal to equal to 0 then print a b c it's pretty simple logic guys okay I'll explain it once more name so this is an example of nested part so what I'm going to do is I'm going to first import the square root function from the math module numbers will be depending upon the values that you input so it'll be printing the pi to numbers present between one to n after that a lot of problem would say that for a should be in the range one to n plus one that will not include n plus one guys it only include n so 1 till n plus 1 but 2 into n plus one underscore square that is equal to a square plus b squared 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'll get here the square root of that value will be converted to an integer value and will be assigned to C now once a condition I'll be using here which says that if C underscore Square minus c squared is equal to equal to 0 then print APC that means that will be a Pythagorean number so this is a pretty simple logic guys if you have understood it just give me a thumbs up or you can ask me any questions that you have in your mind all right so I hope you have understood the code that I've explained you here now let us go ahead and execute this and see what happens yeah so the first number I'm going to type in say five I'll give me all the paterial numbers between one to five so we have only one by two numbers three four five in that range now I'll execute this once more and I'll type in 10 here so we have two particular numbers there are three four five and six eight ten Zero by here once more and a type a big number say 25 yeah so we have these many numbers 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 plane tickets so I have a group of people who are traveling somewhere and I want to give their details such as name age and six I want that to happen 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 and I 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 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 so first I have to find 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're willing to travel enter the number of people who are traveling so you need to enter number of passengers that are traveling and that will be assigned to a variable now now there'll be a for Loop which says that for Num it range one to not plus one but it won't include it down plus one enter the name enter h and finally whether a person is male or 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 for about someone so at that time you can write in yes if you have forgotten someone then again it like again It'll ask you the number of people traveling in all these details if you type in anything apart from yes that means you have not forgotten anyone so it is a very small example but a very good example to understand how uh for Loop can be used in Silo one so let me go ahead and execute this but you're telling I would say yes I am traveling number of people running I would say three name is saurabh ages 78 male all right so I get it ask me for one more name so I'll say Neil it is 90. male or female male yeah so one more name I need to type in say I would type in jagadi ages 98 male female female and yeah it'll print all those details for me so now it is asking have you forgotten someone so if I type in and say yes so if I would say the number of people traveling is one and I would type the name of the person that was traveling so I would just say Siddharth whose ages 77 and he's a male again he's asked me so if I like anything apart from yes say if I type n so yeah I'll come out of the loop and this is how you can actually use a photo inside a while loop [Music] why do you actually require functions so guys here after your 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 temperature of course and this must be converted into a 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 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 fourth iteration definitely the random number will be somewhere between 8 and 11. so how about 10. so congratulations you have made it and the control came out of the 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 so 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'll execute the statement go back again and then from the range it'll 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 yeah 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 note is 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 5 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 off 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 zero 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 1 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 num so till that time will 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 six so if I want to enter one more number so what I can do is I can start it once more so what I'll type I can type again four set 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 a 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 I've assigned a value that is three and this is the balance that I'm going to initialize that is 67.1 phone 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 0 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 two in order to pay in some cash or you need to deposit something you need to press three or you want your card back you need to press four so these are the four options that will be given to you so when you choose option one it will print the account balance so over here as you can see that your account balance is and the balance that is there in your account after that it'll ask you for restart so you need to input that value so that will say would you like to go back and if your restart value lies in this particular 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 will 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 chance 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 0 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 2 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 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 squared plus b square is equals to c squared 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 3 Square 4 square is equals to 5 Square 9 plus 16 is equals to 25. similarly it goes for 5 12 13 16 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 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'll 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 I'll run a for Loop and it will say that for a which is a variable in range 1 to n plus 1 that means 1 to n that means 1 to 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 a b c it's pretty simple logic guys okay I'll explain it once more Neil so this is an example of nested fault 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 will be printing the Pythagorean numbers present between 1 to n after that I'll run a for Loop which says that for a should be in the range 1 2 N plus one that will not include n plus 1 guys it'll 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 b 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 once 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 five and it'll give me all the Pythagorean numbers present between one to five 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 one 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 then again it'll execute the loop again It'll ask you the number of people traveling in all these details if you type 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 uh 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 ages 90. male or female 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] 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 there are certain functions built in right so as soon as you flip the channels or say adjust the volume there isn't anyone in the back end writing a code at every point of time to actually adjust the volume or flip the channels for you there is a particular code which repeats itself and doesn't create any new instances when you actually adjust the volume or flip the channels correct so this is again very good efficiency so as soon as you change the channel there is a function which runs which says that the user has changed the channel so let me do these certain set of codes again similarly for the volume as well so if you have increased the volume or 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 I have for 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 under Earth 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 have 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're 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 we're 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 I treble 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 us 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 asked you 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'll make your life really easy knowing what all of these functions do so on that note let me quickly move to the next function so the next function I have for you guys is Bool well Bool 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 different language say C C plus plus or Java or something you realize that enumerate here is the same as enum 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 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 two 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 zero 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 basically really 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 then 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 a 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 is the parameter here the object can be anything guys it can be a literal object it can be a string it can be a number it can be a list it can be a class or anything as such check out the example X is equal to apple banana Cherry Y is equal to ID of X well this particular object created has the identification as this so when you actually perform operations on this particular object on X and then and 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 I triple 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 I 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 five right so if I had to give you another example what is the minimum of 1000 and 10 000 so whatever is the minimum number is output right so in that case it was actually 1000 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 2 power 3 the answer is 8 correct so what is 4 power 3 again it is 64. if you actually require the modulus of this answer then that would become r z parameter in this particular case since we haven't done 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 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 print it and then we have file all of these are optional guys so basically file and flush everything is optional your 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 this 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 either build 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 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 sorted 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 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 edureka's name or anything as such as well right so the body of the 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 good so how more simpler can you expect python to be right as simple and straightforward is 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 area 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 5 5 5 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 to actually Trace what is available in the memory guys so if that is the case and if the result in which K is greater than 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 user 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 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 we 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 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 will 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 jupyter 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 you 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 2 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 iterables now here I'll just create a list I'll say my list and I'll specify some elements to this the filter method is going to check for all the elements to either be true or false in accordance to the expression that is passed within the Lambda function like I already told 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 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 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 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're 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 the iterables now let me just make use of the same list which I was using previously and I'll copy that over here and then I'll just store the output in a new list p and I'll use the list function since I want the output to be a list within this I'll use the map function and to that map function I'll pass a Lambda function as the input I'll just pass one argument to this as a and the condition I'm going to specify is to check if any element present within my list divided by 3 is not equal to 2 and I'll pass my list as the set of intervals 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 by 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 true as the output now when I divide 2 by 3 again I'm not going to have the output S2 and therefore I have true as the Boolean value for this similarly all the elements present in my list are divided by three and it's going to check if the condition is true therefore only for 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 on to 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'll 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 and I'll pass two parameters say A and B and let me just print the sum of a and b and finally it has to have a sequence as its input so I'll just specify a list over here I'll say the list of values to be 23 56 43 98 and 1. okay so now let me hit run and see what it returns so as you can see over here guys I have 221 as my output so what this reduce function is actually doing is it first adds 23 to 56 so guys the first thing that the 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 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 hit run okay so as you all can see I found the value of a plus b the whole Square using the Lambda expressions [Music] what exactly is an array an array is basically a data structure guys with ordered series of elements any variable that is declared as an array can hold more than one value at the same time all values in an array have a particular address which is specified by its index number here as you can see on the screen I've declared a variable a as an array and I've stored values from 1 to 100 in my array which is a also make a note of the index values indexing always starts from 0 and not from 1. therefore a of 0 holds 1 a of 1 holds to 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're clear with this topic now let's move on towards our next topic for this session which is the difference between Python lists and arrays python lists and arrays have the same way of storing data but there is a key difference that you all have to note arrays can store only single data type values whereas lists can store any data type values so for example if I have an array of integer values all values present in that array will be integers and nothing else on the other hand if I have a list then it can store integers together with float and 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're 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 are 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'm using int as my data type you can use any data type of your choice as you can see that an array has been created with some random integer values in place of the 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 1 so therefore a of 0 holds 1 a of 1 holds 2 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 index mixing will start from the right hand side and move towards the left hand side therefore 100 will be at a of -1 99 will be at a of minus 2 and similarly 1 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 Preeti 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 Preeti 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 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 our Jupiter notebook and we'll do the same I'll reprint my array which is a so that it's easy for us to refer to it since my array a holds integer values I will give some new integer value to it as you can see I've given 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 moved 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 jupyter 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 remove function did not return the value so I hope you are 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 3 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 five but it will not include the value which is present at index number five you can also see the same in the output over here let me try to use some negative index value my output when I use 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 for 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 foreign what is inheritance in Python if we take a look at a real life example inheritances 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 python 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 function1 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 a parent class guys so now to call the parent class methods 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 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 unit 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 unit 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 and instead of self I will add one more variable let's say F name and F age 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 function so I'll just write it as in itself and again I have to add F name and FH and let's say a cell Dot name is equal to fname and cell Dot H is equal to FH now this unit function is going to overwrite the parent in function guys so let's make one more function so this is going to be view as well and in this let's say I want to print okay before we move on we have to specify the variant function with the unit function as well and we don't have to mention these variables instead I'm going to add one more let's say I want to add last name as well and I'm going to add it as let's say eduraco 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 age 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 internet 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 are the 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 how hierarchical inheritance actually involves multiple inheritance from the same parent class and talking about Hybrid inheritance if a program has more than one types of inheritance let's say we have a single and multiple inheritance going on simultaneously it's going to be a Hybrid inheritance guys so let's take a look at examples for each of them so let's go to pie charm 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 am going to make one more class over here let's say class parent to 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 pair in 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 in two 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 Outpost this is Function One okay let's make the object for parent 2 as well all right give it one and ob1 dot let's say what functions we can have 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 inherence 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 2 and this is the example of multiple inheritance so this is a Hybrid inheritance example guys okay we are getting a pipe 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 write 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 a let's say parent class and give it a function Function One again the print statement now what I can do is instead of calling the parent function from the object I can just use the super function and over here I can just call the function one now I'll just print this is function 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 pairing function but it is printing out the function which I have specified in the child class so this is how I have overridden this variant 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 zero is not possible mathematically so as programmers we know it is not correct so this basically leads to two cases either an exception or an error so a Python program terminates as soon as it encounters an error well in Python an error can be either a syntax error or an exception and it is as simple as that well let us actually divide by zero for 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 code so this we basically call as an exception so next what is exception handling guys this is the process of actually responding to An Occurrence of an exception as simple as that so basically the process of responding to the occurrence during compilation of exceptional statements which require special processing is actually what is exception handling guys so this is basically done because of the change in the normal flow of the program execution well it is 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 we have accept well accept this segment which is actually used to handle the exception guys so how do we know that the exception has 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 Vlog guys and lastly no matter what exception or no exception We Run The finally block always guys sounds simple right well it actually is and now that you're familiar with the process of 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 modulo 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 us 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 assured 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 on 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 assertion 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 core that follows the extent web 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 that 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 rotation 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 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 pycharm and I actually ran that particular piece of code why does it say could not open file log well guys basically if the file.log does not exist the block will give you this output right so it says the file could not been opened well this is an informative message and our Python program is actually still able to continue 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 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 political exceptions guys so look at this following piece of code I have for you so here 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 Clause we directly ran into an exception immediately and we did not get to the part where we'll actually try to open our file.log right 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 going to have an else clause and we're going to print saying executing the else Clause right so let me quickly open up my 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 are running this on a particular Linux machine this is the output that you would get guys so it says doing something and executing the else 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 is an exception the finally block is going to run in case there is no exception finally block is going to run there as well so let us quickly check out a coding example guys so basically this is the particular code I have for you guys and let me jump back into my 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 requests 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 SSL 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 chunked requests so these are all the advanced feature guys don't worry we'll be learning about how we can use Python requests so let's talk about why exactly do we need python request or why are we using python requests so the reason behind this is pretty simple guys we use Python request because you don't have to manually add the queries to your URLs and form encode post data and that is a reason enough to use Python request guys and if you're not able to understand this reason behind using the python request you'll be able to understand this later on in the session when I'll be showing you the use case so hang in there now that we are done with the reason behind why we are using python request let's try to understand how you can install python request on your system or on the project that you're working on so first of all you have to install the pip environment on your system and then you have to run this command that I'm going to tell you inside the terminal which is PIP install request guys so let's take it 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 the 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 requests 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 pie charm guys I'll show you how you can make the get request so let's enter the presentation mode for better visibility so I'll just import the request Library yes and I'll make one response variable or response object and inside this I will use request 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 are 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 is 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 cash cookie jar and what I can do is I can also pass my own cookies so I'll take a variable name it cookies and give it a dictionary guys and inside the dictionary I'll specify some values let's say key 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 Connection 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 objects 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 or 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're 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 stops the execution of a thread for a given period of time the local time method Returns the date and time in the struct time format by taking the number of seconds passed since the 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 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 0 and goes up to 999. the second attribute is month whose value will be anything from 1 to 12. after that is M day which means the day of the month and the values can range anything from 1 to 31. similarly our minutes and seconds have the values 0 to 23 0 to 59 and 0 to 61. the weekday value can be from 0 to 6 where 0 is Monday y day or your day can be anything between 1 to 366 and the final attribute is daylight saving time it's one if it's summer zero if not and minus 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 here 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 say C and then I'll use ASC time method so time.asc time and I'll pass a 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 strf time method so as we all have seen I've created a variable a in which I 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 sdrf 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 sdrp 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 dot 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 odd 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 that'll take a lot of time so instead of that what you can do you can make use of regular expressions and find out all the area codes from the student database and after that you can replace it with the updated area code or the PIN code so as you can see with regular Expressions you can even replace a particular string we'll move forward and we'll see one more reason of using regular expression so now this is one more reason to use regular expression because even if you don't have a python background if you have any other programming background like Java 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 regex 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 three because all of 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 H now what I'm going to do is I'm going to Define a variable and I'm going to name it as age date 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 X Plus equal to one now print this age text 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 the 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 cursor 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'll 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 yeah this is my pycharm 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 modify in 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 call in 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 info 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 yeah 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 SDR so we have sat matte hat mat 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 start 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 site 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 what 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 H 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 part now let's go ahead and see in our 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 hat mat and part but it has not printed site why because it starts with an uppercase s so if I make it as s and I'm going to print this so yep it has printed sat as well because between H to Z all S H M and P lies so it has printed all the range of characters whose first letter is present in the range 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 foreign [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 reg X 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 part 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 there's 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 operations 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 pycharm 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 there's nothing but my favorite Club and let me just print this random string so that's printed keep the blue flag flying high now let's go ahead and remove the new lines with the space so for that what I need to do is again I'm going to Define a variable let me 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 the 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 for 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 will tell 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 our 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 of 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 a condition statement that says f r e 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 T 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 yup 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 has 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 will include anything but backslash F backslash n backslash R backslash T and backslash week 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'll basically be equal to a carrot and form the 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 1 to 20 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.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 the 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 that 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 any things 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 scrape some useful information from those web pages I can use web scraping and extract that content and I can save it wherever I want I can save it in an XML format or a CSV basically however I want to save so you can do that with python regular expressions so in our case what we are going to do there is a website and on that website we have phone numbers we have uh addresses so what we are going to do we are going to write a code in order to get only the contact numbers from there and how are we going to do that we're going to do that with the help of a regular expression so for that I'm again going to open my 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 the URL that I'm talking about right so let me just uh copy this and I'm going to show it 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 use of files a lot so coming back to the basics again how can we input something into python the standard input the usual keyboard input right so you can key in anything you want well there's another case as well maybe even 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 comments section and keep the energy going so can you quickly name all of the types of the files that you know so again let me help you here so we have images we have audio we have video we have text we have script and so much more right there is a key difference here I want you guys to note check this out Windows supports all of these files that I told and more well virtually unlimited to be honest but again I won't get ahead of myself here because there are limitations here as well now check out python there are two types of files binary and text so whatever is not text is binary as simple as that so either a file gets to be a binary which contains obviously zeros and ones or it can have some literal text well text as in strings or anything valid for that matter text files are structured as a sequence of lines where each line includes a sequence of characters and this is what we call as code or syntax right and every line is terminated with a special character what we call as the EOL what is the EOL guys it's the end of line character a binary file is any type of file which is not a text file as I mentioned and because of their nature binary files can only be processed by an application that knows and understands the 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 crowd 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 flow diagram to give you a quick picture of what needs to be done first we create a file and later we open that file we work on the file well working on it is basically reading or writing or anything for that matter and lastly we close the file when we are done using it coming to python now creation of a file can be done manually by the user or through python for now let us consider that we will do it manually by going into the location and creating a file say like a text file later in this session I'll show you how easy it is to make python create the file for us so make sure you stay tuned till the end so now we concentrate on how we can open a file with python guys it is very simple so we have an inbuilt function called as the open function which is used for this exact purpose the open function takes in two parameters as seen here one is the file name and other is the mode and this on your screen is the Syntax for the open function so what do the two parameters mean one is the file name that you want to open it can be anything at this point of time right so it has to contain the extension of the file type as well so this is compulsory and important so make sure you keep this in your mind and second we have the mode we know this means something that has to do with opening the file right so check the check the screen so basically these are various modes available to open a file we can open it in the read mode the 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 we 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 more here this is exactly equal to having the read mode in the next example and it is as simple and straightforward as that so now we know how we can actually create a file and open it right so let's begin working on it let us start with creating on how we can actually do a read operation using python so the read operation is very easy guys so basically just like all the other functions and handling files in Python we make use of the read function for this very purpose but then again there are so many ways to read a file too so limit 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 it'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 pycharm 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 on 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 two 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 Pie charm 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.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 I'm going to run this old code right so you're going to get the entire output here check it out hello world perfect because we have read of blank now we're 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 three will tell the python interpreter to read only the third line and absolutely nothing else but what if he 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.readlines 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 going to close it I'm gonna 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 import 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 I'm going to it's going to put it in a really beautiful way for us to see check this out hello world is an entity we love python as 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 Python and check the single snippet out so we are back into pycharm and check out this for Loop example I have for you so again we're sitting on the same file demo file 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 dot right to write two content into it and then we're gonna 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 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 python 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 pytham and we can check out the example over here uh let me change this we have something else right so let me type eureka.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 write mode to write something into it and close it so that's right no output check out the file whereas the file called edureka it is your new file of AD Eureka 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] introduction to Python and my SQL well as you already know python is one of the most widely used programming language and in demand among developers but you can't merely develop any application without having to know where to store the data so this is where database management system comes into picture and one of the most widely used dbms server that is also used in the industry practices is MySQL DB server let me move ahead and talk a bit about integrated connection all right guys this is just a glimpse of how the bigger picture will look like what I'm trying to do is I'm basically trying to integrate my python with my backend database server and then do the further manipulations now let me move ahead and explain you the internal working logic behind when you establish a connection between Python and MySQL DB server or I can say that I'm basically going to explain you the internal working logic that how does it actually connect or work so as you can see there are numerous pictures given in this slide on my extreme right hand side there's a picture of a database and on my extreme left hand side there's a picture of a python application and in between lies the MySQL connector python API so MySQL connector python API is nothing but acts as a bridge between my front end python application and my backend database server now when I 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 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 thread 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 our 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 foreign 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 am 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 DD 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 am 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 a 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 gonna show you what I'm gonna 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 Hajj TV 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 hush DB has been created now I'm going to show you whether this database is existing in our MySQL workbench or not so in order to do that I'll just change few parameters and add a for Loop I'll explain you about the for Loop part later so let me just omit this part hdb and instead of create I'll simply write show over here and then over here I'm gonna write show databases now I'm gonna write for DB in my cursor 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 TV 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 gonna write my cursor dot execute it's gonna be create table suppose I give my table's name as employee under which let me pass two parameters they'll be name bar chart 200 salary integer 20. you 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 will 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 and 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 search 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 harsh DB now moving on let me create a new variable with the name as SQL form are 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 percentage 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 double 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 double over here I need to write my cursor.execute many and call in SQL form comma employees and finally I'm gonna write mydb 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 going to 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 harshdb dot employee well it changes nothing Our concern is to basically view the Tuple which we 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 harshit Amit Ankita 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 going to do is yeah let me pass the parameter database which is equals to hush 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 fetch all 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 start 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 an 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 H1 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 will show 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 pop 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 purge 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 70 000. 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 read on 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 70 000. now I'll move on and do the last operation that is delete operation which will also conclude my tutorial it's going to be delete 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 harsh 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 equal 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 and go to read off 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 [Music] what exactly are sockets sockets are basically interior endpoints built for sending and receiving data a single network will have two sockets one for each communicating device or a program these sockets are a combination of IP addresses and port numbers guys a single device can have n number of sockets based on the port number that is being used different ports are available for different types of protocols take a look at this table for common port numbers and the protocols as you can see over here the HTTP protocol uses the port number which is 80. similarly FTP uses 20 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 the port number 80 it has returned the same Google page now let me change the port number and let me give 20 as the port number which I know is for FTP protocols so I'll just type Google over here google.com 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 bites I'll use bites 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 seven 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 till here you've just got The Knack of transferring strings but socket programming in Python allows you to transfer python objects as well these objects can be anything like sets tuples dictionaries Etc to achieve this you will need to import the pickle module of python python pickle module comes into picture when you're actually serializing or deserializing objects in Python before moving on to show 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 pycham 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 one two three four presents somewhere in some serialized Manner and this is prefixed with a B now this B actually means that this is in bytes now that you have a brief idea about 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 DC realize 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 foreign 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 location 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 is it is represented in a matrix form that is why we call this as a two-dimensional array so I hope we are clear with what exactly two dimensional array is so let me open my 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 numpy module and how you're going to do that click on file go to settings tab and you'll see the project interpreter option and on the right hand side you'll see a plus symbol just go on and type the module that you want to install so I'm going to install numpy just go there click on it and install package I've already done that so I'm not going to repeat it so this is my pie charm so first thing that I need to do is import numpy as NP now after that I need to create 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 so 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 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's 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 is the reason why we use numpy instead of list now don't worry I'm actually going to prove it to you practically by opening my pie charm so guys this is my pie charm again so first thing that I need to do is import numpy 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 0 to 1000 but it won't include thousand it will be only there till triple nine that is 999 and now I'm going to print the space occupied with 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 values between zero thousand but it won't include 1000 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'll first take the first element of list L1 and then it'll take the first element of list L2 it will going it will calculate sum and 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 arrays 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 numpy array in order to compute the sum so let us go ahead and execute this and still 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 numpyra 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 the py 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 python 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 two comma three put that in parenthesis now add one more element say 2 comma three 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 will 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 num pyra 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 eight nine ten eleven twelve thirteen fourteen 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 I 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 2. 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 2. 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 2 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 would have written 0 colon 1 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 four and six available with us now just to remove confusion what I'm going to do is I'm going to add one more element and I'm going to give values to it say 7 8 9 and 10. so now if I want four and six I can't just write 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 two it won't include the second element Illinois infused zeroth 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 1 to 3. 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 1 to 3. so this is how you can perform line spacing as well so we saw how to perform reshaping and slicing now let us see what are the other operations so we are now going to find out the minimum maximum as well as the sum of our num py arrays so let us go ahead and execute that practically let me remove all of this and now I'm going to Define one more 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 axis 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 3 plus 5 is 8. if I make this as axis 1 and print this so it gives me 6 and 12 because 3 plus 2 plus 1 is 6 similarly 4 plus 5 plus 3 is 12. 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 yeah 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 numpy array 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 is actually printed the square root of each of the elements the square root of one is one for two it is 1.414 so 3 is 1.73 again for three it is 1.73 then for 4 it is 2 then for 5 it is 2.23 this is how you can find the square root of each of the element now when I talk about standard deviation so you can find that by typing here so now if I want to find the standard deviation what I need to do is instead of sqrt I'll just type in here STD and it'll give me the standard deviation that is how much each element varies from the mean value of my numpy array and this is the standard deviation guys it starts simple so this is how you find standard deviation now let us go back to our slides and see what are the other operations that are still left now these are the basic mathematical functions that you can perform with numpy Aras 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 ones and yep 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 wanna 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 v stack 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 a 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 Num p y array that is a to say a single column so how I can do that just type in here print a DOT Rebel that's all you have to do go ahead and execute this so 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 and 2 NP dot pi comma 0.1 uh 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.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 I print this so I get the graph for Tan as well so I'll open my presentation once more and we are going to see what are the other special functions that we can use with numpy now numpy comes with two very good functionalities I would say that is called exponential function and logarithmic functions now exponential this e value is somewhere equal 2.7 and we all know log so when I talk about log it is actually log base 10 and when I'm talking about natural log that is log base E I will write it as Ln so instead of that I've written log that means log base 10. so you can perform these operations with the help of num p y 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 AR 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] so let's go ahead and take a look at what exactly spawn does 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 you 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 and that is data frames and CDs we'll wait for this to install guys meanwhile I'll just tell you what are data frames and series guys so data frame is a 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'll be able to understand this better what I'm talking about like pandas and series what exactly they are we'll be working on 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 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 aspd 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 eight 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 a 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 gonna take the date range now 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. and now I'm going to get the index values as d and I'm going to have to pass a few more values which is columns so I'll pass the columns as a list I'm going to take let's say four columns so I'm just gonna take okay wait a minute a B C D all right do we 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 it's just a simple example and I'm going to show you how you can create a data frame by passing a dictionary of objects but it can be you know converted into a series also so I'll take let's say again DF is equal to PD dot data frame and I'm gonna pass a dictionary over here now so I'm going to take a few values first of all the 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 one two three and four after this my next value is going to be let's say B and I'm going to pass a timestamp let's say and for timestamp I'm gonna use the same I have used over here 2020 0 3 0 1. I have to use the right and after this I'm going to pass one more value let's say c and I'm going to use a series now a series object and inside this I'm going to pass one and the index is going to be let's say range 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 and so this I'm going to pass a value let's say not three let's say five multiplied by four and let's take the d type is equal to integer 32 yes all right now I take my final value which is going to be e and inside this I'm going to pass a data frame or we're going to use the categorical object guys we're going to talk about this later on in the session so don't worry I'm just showing you how you can create a data frame using all these objects that we have at our disposal guys now instead of test and train we can just call it as true or false doesn't matter we are taking categorical object so it has to be either true or false or it can be zero or one but it has to be decisive in a way that there are only two values so I take another value and for this let's just say I give the value and Eureka all right so our dictionary is done over here so we have created our data frame ways 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 daytime stamps over here integer float integer category and an object because I have used a string over here that's why it is giving us an object but in the new release that is python 1.0.0 it's not going to be an object it's going to show you it is a string so don't worry guys and we have already made a video on python pandas 1.0.0 with all the features that have come with the new release the new stable release released last month you can check that out as well to check for the new features that we have come across so now that we have done this let us take a look at the next topic that we have which is how to view data so viewing data is basically you know how you actually look at the data or how you're going to look at the data using pandas Library so we'll just jump right to jupyter notebook guys and I'll tell you what kind of functions or all those things 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 learned this this and this as well when I check for DF Dot D types it 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 way 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 DF 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 numpy representation of the data so I'll just tell you how you can do that so I'll just write DF dot to numpy wait a 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 data frame.2 numpy is actually fast and does not require copying the data so it is a very best deal for we have and then okay I'll just remove this I don't want this and then we have data frame dot describe which is going to give you somewhat like this which is going to give you on 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 okay I'll just show you guys just have to write DF dot sort by index and inside this you you have to give the value of the axis I'll just give one and then let's say ascending you want it to be ascending no I'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 going to 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 D over here which we have passed over there in our previous section where we have declared the data frame and I'm sure it is 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'm very certain that the data that you work on is not going to be like this it's going to be very complicated so this is just to give you a perspective as a beginner how you can work with pandas now I'm 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 add 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 all 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 and 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 take a look at another method which is is in method I'll 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 Jupiter 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 gonna 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 axis so we're just 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 are going to 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 n a n 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 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 need alignment in addition panel automatically broadcast along the specified dimension so for that let's make a series I'll tell you just how it works PD Dot series uh 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 and next 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 6. 789 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 dimensionality and 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 me 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 say 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 between all these values so this is how we apply functions to our data guys now I'm going to talk about histogramming so histogram is a representation of the distribution of data so this function we have which is matplotlab.pipelot.heast 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 down s 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 with our data guys now I'm going to talk about the string Methods so series actually is equipped with string processing methods in the string attribute that actually makes it easy to operate on each element of the array so let's just move 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 let's write Jupiter give it 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 it will 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 you 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 and B 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 to third row the next one is DF from third to let's say seventh row and then we have DF from Seventh 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 and let's see yeah first value let's say a give it a few values let's say one two and then say B we have 3 4 all right we make the for the right as well so I'm just gonna 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 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 AIS Le so we actually joined using the merge function over here another example that I can think of is uh let's say you have left and right if we can change the values differently and we can just group them together now what I'm going to talk about is grouping guys so how you do the grouping of different data inside your data frame using pandas first of all for that you have to split the data into groups based on some criteria that you have and then after that you apply some functions on them and then later on you can combine the results into a data structure so first of all you have to get a data frame guys so let's see we have a data frame over here so we can actually group it by let's say right I'll just write Group by 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 a 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 for 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 a stack guys in pandas I'm sure most of you must have heard about some of the 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 a list and inside this let's see we have values and we provide or we get two lists over here at least 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 twelves and 13. over here I will add a 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 you 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 going to 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 will 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 no we'll remove a few values over here multiply to three now we take another value which is going to be B and for this we're going to take a list again so let's say multiply to 4 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 buy Six values so let's write B and p 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 all 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 n 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 uh 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 0 or 1 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 fall so we'll take the dates all right I made a mistake and just cut this foreign 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 0 1 or 0 3 0 1. we want the periods is equal to 100 and let's say frequency is equal to s all right let's print dates okay so we have dates over here just move this for now so now we take one more variable let's say TS is equal to VD 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 we'll 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 gonna 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 DZ 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 TZ which is time zone convert and we write us Keystone so we have converted into the US time zone and converting between time span representations also we can do that let's just say that's your exercise so you have to convert between 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 would 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 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 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 an arrow 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 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 see 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 foreign 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 n a n because we have not given any other category for that and we have the five objects so we are getting very good bad very bad good medium so that's how you use the categoricals in pandas guys now I'm going to talk about plotting using pandas so that's going to be very simple guys for that I'm gonna have to import one more library that is matplotlib Dot Pi plot I'm going to use it as PLT all right same thing goes with this as well all right this one MP5 plot so there should not be any errors now okay so uh I will close all now I'm gonna make one series 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 come sum all right now TS Dot Plot so we have a plot over here using pandas guys this is how we have created one uh series using the random numbers from numpy library and using the 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 our 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 PS dot CSV and it's going to save the ts.csv file somewhere in my directory and similarly I can read from a CSV file so for that I can just write PD 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 copy this paste it over here okay we have a Unicode error so 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] survive any 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 do you need to do you need to present your analysis or your data in such a way that the other person can understand and how you can do that you can do that by representing it in the form of a graph and it is a well-known fact that human brain can process information easily when it is in pictorial or graphical form so that is one of the key reasons why we use a data visualization in order to understand the trend better and make better decisions so let us move forward and understand one more reason why we need data visualization now basically it allows us to quickly interpret the data like whatever the trend the data is showing us now basically it allows us to quickly interpret the data so just by looking at the graph we can understand what the data is about and what is a trend but if you are not from an analytics background and you see just the Excel sheet then it is very tough for you to understand it so if it is there in the graphical form you'll quickly understand it and you can experiment as well now what happens if you want to change certain variables and see what will be the outcome so with the help of the graphical representation of your data that will give you a very good idea as to what all variables are useful for you and what all variables are useless you don't need to include in your analysis in order to perform that experiment in an efficient and a quick way you need data visualization so Guys these 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 Brian says move on uh so does Neha Puja Siddharth Theon Jason fine guys so we'll move forward and understand what exactly is data visualization so what is data visualization so data visualization is nothing but the presentation of your data in a pictorial or a graphical format now why we do that we do that in order to enable the decision makers of an organization to see 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 Devon says you can use it in finance or in what way can I use it in finance can you please tell me that Devon all right so he says in order to find out where should I invest my money yeah that is one perfect use case I would say all right fine guys so I'll give you a couple more cases where you can use it you can use it to identify the areas that need attention or Improvement in 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 use 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 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 and 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 matplotlib 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 I use 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 so 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 here I'll first import pipelot from matplotlib so for that I'll type from mat plot lib import Pi plot as PLT now this PLT 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 two comma 3 and then for y-axis I'm gonna add values such as 4 5 and 1. so this will plot our graph in the canvas now final step is to show that plot how we'll do that I'll just type in here 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 have imported the pi plot 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 she'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 with art all right so he says yes fine so I'll open my pycharm and execute this practically now this is my pie charm again guys so I'll remove till here and obviously I need Pi plot 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 are 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 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 don't 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 gz plot right now then again I've defined four variables X Y X2 and Y2 and all of these contains list of numbers again I'm telling you it can be a data frame as well which contains a data set then what I've done again I have used plt.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've 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 and 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 job again so this is my pycharm again guys let me first remove all of this and I need to import the style function so after that I'll type from matplotlab 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 5 8 10 5 8 10 then y I'm going to our 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 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 defined title X label as well as y label now our last task is to actually add grid lines so for that I'll type PLT 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 just write 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 a 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 levia 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 to 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 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 pycharm guys over here what I have done first of all I have imported the pi plot 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 of divine X label as well as y label after that I have title and then finally show it now go ahead and run this and see if it works or not and yes it does so we have Legend here we have x x lab we have y label as well as X label and we have a title paragraph 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 bar 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.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 pycharm 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 matplotlib module then we have a variable population ages and bins now instead of using pld.bar I'm using pld.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 yep it does okay 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 python 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 start 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 faded 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 dot pipelot 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 will have elements between 0 till 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 types 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 two two and one 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 two one one 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 foreign question so when you're talking about basic python questions there are a couple of specific questions which keep repeating okay so uh this is the first question that we have what is the difference between a deep and a shallow copy so uh even before I get into this uh let me just check with you all so can you give me a background as to which particular background do you come from do you come directly from Python and you know what do you want as a takeaway from this particular session so I would be happy if you could reply in the you know questions box please a quick introduction into you know basically what you are looking for in this session as such and you know what do you want as a 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 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 uh module named as copy so the shallow 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 byte 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 tackle the person this is what you need to say okay anyone so basically your Shadow copy copies all the reference pointers all the values and it creates a new memory location so as to you know you have a copy of the entire set which you created whereas a deep copy it takes care of all the references of the references and a whole lot more which means to say it has to make you know pointers to each and everything it has to make 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 deep puppy gets deeper and deeper and when you take you know when 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 Channel copy does not copy all the admin structures just the top level references and the original values that's it I will 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 this 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 three four okay and T1 as a tube 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 tuples when you have lists Okay the reason being when you have fixed data and you know exactly the size of the data which is coming 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 see that Row 3 has been removed so you can keep on modifying it whereas your tuples are fixed in nature when you think 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 wire in 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 add a column with maxi 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 a 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 tube could you all reply from my explanation you should be able to answer this so here's the thing Tuple you will never be able to modify will you be able to modify this list which is inside the Tuple though so you say I don't think so but it is you are able to modify it because it is referenced as I said earlier so if you see L2 now it would have been modified remember lists are referenced when even inside this the structure the attributes of a list do not change even if they are inside a 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 Gil Global interpreter log this is a question which comes up okay typically when it comes to adding 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've 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 chemical system 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're 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 C or C 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 decide multi-threading you can make use of multiprocessing where multi-processing takes advantage of the number of codes you have so if you say I have got four core processor you can make use of all those four cores and run parallel jobs on all those Workforce okay so you've got something called multi-processing for that processing for trading you have something called hey everyone are you all clear okay great now basic python questions when it comes to ternary operation so I wouldn't call it exactly turning 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 is 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 from a 50 we need to find the largest among these okay so you have to be sure as to what you are 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 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 do not follow this particular thing follow this particular you know syntax because I am using new style classes this is a question which comes up again as well okay do I use old style classes or new style classes this is something called an old style class this is using new style classes always use new style classes it has only Legacy architecture which makes use of course type classes new style classes where the classes inherit from objects and object parent class as such def F cell 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 KF 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 or 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 if 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 is a 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 construct 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 1 4 to 10 5 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 inside 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 of foreign will give you something like this okay it is not the right thing that this is not right at all okay and you wouldn't want something like this okay what you want is one two four five ten so when you do a sort like this it applies on the ASCII characters as well 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 you 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 if 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 chart 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 that's 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 K2 is equal to 1 2 3 4 5. okay there is a data structure so you want to create a dictionary out of two different lists or tuples which have matching number of arguments this is how you do it zip of T1 comma T2 okay what you get is a list of tuples which are zipped together A B C D E when you do 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 A2 is equal to sorted of I for I in A1 if 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 your a naught is equal to just a moment yes so you'll 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 checks for the keys to be one two three four five and it is not it is the 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 and 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 A1 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 dict comprehension now I colon so I would be your particular key okay I colon I into I that is we are creating a particular square of the item for I in A1 A5 will give you 0 0 1 1 2 4 3 9 4 16 so on l 9 okay so we are creating a dictionary comprehension by using this for I in A1 okay we are traversing over a one 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 Python okay how do we do various things and you know fighting so we got an re module so we've got split uses a regex pattern to split a given string into one list okay so split it uses a regex pattern sub will find all the substrings where the regex 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 block something like this okay so what you do is my s dot I need a split off my S comma backup okay okay so the best way of doing it is typically doing a normal split okay so let me do this okay so what it does it splits over the pattern bla okay 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 want to do it for okay substitution okay I'll 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 BL 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 if you get it how it replaces a particular pattern everyone are you clear with how it replaces a particular pattern okay so let me go back again okay so you've got your string my S you've got a pattern which needs to be replaced okay and you have an item replacement item a okay re dot sub n okay I got 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 inheritance now how does it actually function so where a derived class acquires the members of a single super class is known as a single inheritance multi-level is you know one class derives from you know another class which derives again from another class so basically a derived class is inherited from a business class and the other one inherits from the previous plane you know previous class as such okay so D1 inherited from BS plus B1 okay and D2 is inherited from there's a mistake here it you know what you say the inherent 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 my class Plus n Plus my class M2 Plus of my class so this is hierarchical inheritance you have to be careful by 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 that's my C2 something like this so child will derive from two different you know super classes as such are you clear as to what exactly inheritance is so you've got an example here V1 V2 child class parent class pass and V1 and V2 okay so this is basically what you mean by inheritance in Python okay moving on to the next part it might not be relevant to everyone but you know people who are moving into web application development and specifically to 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 model 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 a 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 other 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 files comes in so you in your setting.py file is where you do all the configuration regarding that particular database or you know uh your web server information all these things go into your setting.py file so you've got your databases so databases will tell you the default you know or you know your standby or your slave or whatever it might be so the default database you typically have a SQL light so you give the name of the engine database DB backends sqlite3 because sqlr 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 SQL I3 is your actual database okay it is going to be the name of your particular database okay similarly there might come another question as to where do you you know start your where do you have information regarding starting your web server or anything like that so that comes under something called as manage.py you've got another file called manage.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 uh 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 we'll 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 from date time and day time right now puts it in the HTML and replaces this percentage s by that information and returns you the HTML response now you might be thinking what if I have template files available so in that particular case you have to first use a loader to load in the template by doing a get template okay and then you have to serve the response over here by rendering the template okay are you all clear okay so you have you know getting the template rendering the template serving your responses and how to write a view in 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 type in 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 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 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 you 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 are 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 models 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 ABC 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 from that 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 assets 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 modules 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 Model 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 Avail 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 an email text text field something like that you've got different types of field classes okay you've got a password field anything like this so that is the default HTML widget and the minimum validation requirements so number of characters which you can enter number of lines which it can which can be in the text box all of 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 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 is something like this python Port requests my URL is equal to www .http from this requests get tomorrow 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.png comma WB as f f dot right oh sorry about that it should be we'll see edit of course 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 it's giving 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 edge 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 we so your URL for example www.edu raker.com so you would be able to get this information so you'll be getting cash card and this is a cash content okay which has been provided by your CDN okay so this is your cached content and you will 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 thing are you all following me so far so you go to imdb.com top you know 250 movies okay so there are various ways of doing it okay but for this you have to start with beautiful soup on this beautiful soup from bs4 import beautiful soup beautiful soup is a 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 go over here and I would first do a check on this so what I need is the TR the TR content you will see that you have got various DRS under Lister list uh just a second rumor let me you know let me just go through this I'll tell you what exactly beautiful scoop 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 soup is going to be beautiful so you need to find something so super beautiful so this is what it does now we'll do this okay uh I want Lister list and list is equal to soup dot find okay so dot find class so you can do this something like this we don't want TD we want the body okay class is Lister Dash list 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 TR let's check what we need under TR okay TR we just need the tra information okay we just need the ER information within the this particle okay find all so we want and let's start fight okay I think this will not go in with that or you can use find capital t r s okay it'll give me all the TRS in this all the TR information in this okay now under this we need specific information so this is how you go about it under this okay for trntrs okay friend I'll just give you one example okay post column let's take this one for example okay expect you have something called title column okay class title now you get to the point of title follow print TR dot find class school fine we want TD sum of class oh no title so one by one we enter into this point 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 so you see all the title columns here have been put up this is how you put all the information are you all clear on how to you know approach this particular problem okay next data analysis is usable now numpy is one of the modules which are heavily used in Python okay so how do you go about using this so you will get a particular question saying how to get indices of n maximum values in a numpy array okay so you have to use these particular things import numpy as NP ARR is equal to NP dot array you are going to have you know a number of items please remember this one three five four five four six five something like this okay now you can make use of ARG sort there is an a particular thing called Oxford ara.org sort will give you the indices the indices for this particular thing okay in a proper order okay sorted indices so what do you need the top three positions okay top three items so minus three minus 1. so 3 4 and 2 is the I know answer which I get over here okay what is the difference between the numpy array and a list so let me tell you this okay just give me one second so let me recreate this array one three two four five sort of minus three column of control plus one okay four two three as the output that is what now what do you want so what you want is the difference between numpy array and list so we'll go get to that just give me a second port number five as NP a is equal to NP dot array so we'll first do this particular how do you calculate a percentile with python so we'll go to this first okay and then we'll move on to that particular question we have got a couple of things something like this p is equal to or we'll just do this NP dot percentile NP dot percentile of a comma the percentile level the 50th percentile would be this 5 okay so you can set return the 50th percentile so you can directly feed in the percentile level and you will get the information regarding the percentile level so this is a question which you asked Roma okay what are the differences between numpy and lists okay so it is much more efficient because all these structures have been written in C with python you have vector and Matrix operations for free so basically something like this okay I have got L1 is equal to one two three something like this okay so L1 here R is equal to NP dot array of N1 okay so we will create L2 as well okay now if you do L1 plus L2 say you want to add items of L1 to L2 so 1 plus 1 2 plus 2 3 plus 3 4 plus 4 5 plus 5 and 6 plus 6 l 1 plus L 2 regularly would give you something like this but if you create an array with a numpy array I said what would happen L1 ARR plus L2 ARR would give you 2 4 6 8 12. does it answer one of your questions Roma okay so this is one of the examples okay so uh you get a lot of vector and Matrix operations so that you know you have to you can avoid unnecessary work of recreating those Vector operations number array is faster you get a lot of built-in and built-in with numpy ffts convolution searching linear algebra histograms Etc you've got a whole lot of stuff which is included so what about the list so lists are you know general purpose containers effective in regular insertion deletion depending on catenation Etc and it is dynamic in nature okay but it does not support vectorized operations like element wise Edition multiplication division all these particular operations are not supported which is what the advantage actually is does it answer your question Roma yes uh so aroma so numpy cannot store different data types in a single array it is homogeneous when it comes to a single array but you know you can have arrays of booleans you can have arrays of strings you can have enemies of integer float Etc separately within a single array it has to be homogeneous okay now what is the difference between numpy and PSI Pi you've got something called numpy you've got something called scipy these are two different modules numpy is a much more raw module as such if I were to say so numpy would contain nothing but the array data type and the most basic operations like indexing Etc your numerical code is typically inside site Point okay but you would see that numpy and PSI Pi are quite compatible with each other because PSI Pi is built on top of numpy so where sci-fi has retained all the RSA operations of numpy but is it's got a whole lot of its own operations for example numpy contains linear algebra functions even though they are more properly utilized and they more properly belong to sci-fi you can contain integration differentiation all these operations are provided in sci-fi okay and it's much more easier so if I contains more fully featured version of linear algebra modules so if you are going to do same scientific Computing it is advised to use both numpy and sci-fi together okay which is a recommended thing so instead of using it independently you do not want so it it makes more you know sense of doing something like that okay
Info
Channel: edureka!
Views: 154,497
Rating: undefined out of 5
Keywords: yt:cc=on, python for beginners, python full course, 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: X6bFJI9r-mM
Channel Id: undefined
Length: 715min 0sec (42900 seconds)
Published: Tue Jan 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.