Python Tutorial with Gen AI for 2026 | Python Tutorial For Beginners | Python Training | Simplilearn
Key Takeaways
This video teaches Python programming with Gen AI for data analytics using tools like Python and Generative AI
Full Transcript
[Music] Hey everyone and welcome to the Python full course by simple learn. Now Python is one of the easiest and most powerful programming languages out there. It's used everywhere from building websites and automating task to creating AI tools and analyzing data. And the best part is it's beginner friendly which makes it the most perfect first language if you are starting out your coding journey. In this course we will start from scratch what Python is, how to write your first program and then gradually move into the exciting stuff. You'll learn popular libraries like NumPy and Pandas for data analysis. Practice exploratory data analysis to understand and visualize data and even build a cool web scraping project to pull real information from websites. To make sure your learning sticks, we will wrap up with top five Python projects that you can actually use to showcase your skills. And by the end you won't just know Python, you will be confident enough to use it for real world task like automation, data analysis and building practical applications. So let's dive in and get started. Now if you want to watch similar videos like this, hit the subscribe button and check out our channel for more such information. If you're interested in mastering the future of technology, then the professional certificate course in generative AI and machine learning is the perfect opportunity for you. This is offered in collaboration with ENIC Academy, IT Kpur 11 month live and interactive program providing you hand-on expertise in cutting edge areas like generative AI machine learning and tools like Chad GPT D2 and even hugging face. You'll also gain practical experience through 15 plus projects, integrated labs and live master classes delivered by esteemed IT Kpur faculty. And alongside you'll earn a prestigious certificate from IIT Kpur and you'll receive official Microsoft badges for Azure AI courses and career support through SimplyLearn's job assist program. So hurry up and enroll now and you can find the course link below. >> In this video we will learn an introduction to Python programming. So before I begin make sure to subscribe to the Simple Learn channel for more interesting tech videos. In this introduction to Python video, we will learn what is Python, the features of Python and where Python is used. We will also look at the popular Python frameworks and libraries and get an idea about the best Python idees. Then we will see the top companies using Python programming and get some hands-on experience by learning the basics of Python on a Jupyter notebook. So let's get started with what is Python. Python is a high-level object-oriented programming language developed by Guido Van Roum in 1989 and was first released in 1991. Python is often called a batteries included language due to its comprehensive standard library. A fun fact about Python is that the name Python was actually taken from the popular BBC comedy show of that time Montipython's Flying Circus. Now let's look at the top features of Python first. So Python has a simple structure and a clearly defined syntax. This allows the learners to pick up the language quickly. So it is easy to learn and use. Python can run on different operating systems such as Windows, Linux and Mac, making it a portable language. It enables programmers to develop the software for several competing platforms by writing a program only once. Third, Python is freely available at the official website. Since it is open source, this means that source code is also available to the public. Now, Python uses an object-oriented approach that encapsulates code within objects. Python provides a collection of libraries for various tasks such as machine learning, web development, and data analysis. And finally, in Python, you don't need to assign the data type of the variable. when you assign some value to the variable, it automatically allocates the memory to the variable at runtime. Now with that, let's move on to the uses of Python programming. So, Python programming language is used to develop desktop applications and build web applications too. It is popularly used in the field of data science, machine learning and artificial intelligence to analyze data, build predictive models and make business decisions. Python is also widely used in game development. Now let's see some of the popular Python frameworks and libraries. Python can be used for web development using frameworks like Zango, Flask, Pyramid and Churi. Now you can build graphical user interfaces using libraries and frameworks such as TKER or just KER. You can also use PI GTK, PIQT or PYJS or Python JavaScript. Now, Python is also used to perform machine learning tasks using libraries such as TensorFlow, PyTorch, Scikitlearn, Mattplot Lib, and Scypi. You can also perform mathematical computations using numpy and pandas. Now, let's look at the best ids that you can use to write programs in Python and perform specific tasks. So, we have Jupyter notebook, which is part of the Anaconda distribution that is widely used these days. Even for our demo in this video, we'll be using Jupyter Notebook. I'll show you in a while. Then we have the visual code editor from Microsoft. This is also one of the preferred IDEs by learners and companies. Then we also have the popular text editor called Sublime Text editor. Then we also have PyCharm followed by Python and Spider as our top idees. Now let's look at the top companies that are using Python in our day-to-day work. So we have Google, Kora, Facebook, even Netflix, Spotify, and Instagram. Now there are other top product based, service- based and startups that also use Python programming. Okay. Now with this theory, let's move to our Jupyter notebook where I'll show you how to write basic Python code. So I have my Anaconda distribution of Python already installed and I'm working on a Windows system. So I'll search here for Jupyter notebook. You can see here I have my Jupyter notebook Anaconda 3 installed. Let me just open it. Now this will open on a web browser. You see here this is my Chrome browser. Okay. So here I have some Python notebooks already created. Now for our demo, let me go ahead and create a new one. So here I'm going to click on new and then I'll select Python 3. This opens in a new tab and here I can write whatever code I want. All right. So first let's see how you can print a simple sentence or a text in Python. Now to print something you can just use the print function. If I write print within brackets suppose I want to print my country that is India. Now India is actually a string. So I need to pass in within double quotes. So this is it. If you want to print something, you can just use the print function and pass within the function whatever you want to print. And now to run this cell, I just need to click on this run button or you can hit shift enter to run this cell. I'll just hit shift enter. You can see it here. It has printed India for me. All right. Now let's say you want to perform some simple mathematical calculations that also you can do it. Suppose I want to find the sum of 10 + 50. I'll just run this cell. You see here it gives me 60. This is very simple. Similarly, if you want to find the product of let's say three numbers, I'll write let's say 40 * 2 * 5. If I run this, you get the result that is 400 which is correct. Okay. Now to store our values, we can take the help of a variable. Suppose if I write x = 7. This means that my value 7 is stored in the variable that is x. You might be aware that in other programming languages also we have variables and data types which I'm going to tell you now. So here I have assigned my value seven to the variable x. Now let me go ahead and print what is there in X. If I run this cell, there you go. It gives me seven, which means X holds the value of seven. Now if you want to check the data type of X, you can use the type function in Python. So if I write type of X and if I print it, it will also result in the type of data that X is holding. You can see here the first print statement prints the value of X that is seven and the second print statement gives me the type or the data type of X which says it is of class integer or int. Cool. Now let me show you how you can use the input function to take user inputs. Suppose I have a variable x where I want to store some numbers. I'm going to use the int function and inside the int function I'll give my input function. So when I run it, it will ask the user to input a number. I'll write enter number x. Okay. Similarly, I'll just copy this. Come to the next line. I want to create another variable called y which will have another number. I'll write enter number for y and then I'm going to print the addition. So I'll write addition of x and y is give a colon and then I'll pass in the operation within brackets that is x + y. Okay. So essentially what I'm doing here is I am assigning the value of my first number to x. Then I'm assigning the value of my second number to y and then I'm finding the sum of x and y. Let me show you the result. If I hit shift enter, it is asking for the user input because I had defined my input function. So it is asking enter number x. Let me give my number as let's say 30. I'll hit enter. Again it is asking for my y input. Let's say I'll give 60 and then I'll hit enter. There you go. It has returned me the addition of 30 and 60 that is 90. You can see the message as well. Addition of X and Y is 90. Cool. Now let me show you how to add two strings and we'll compare the results when you add two strings and two numbers. So basically when you add two strings it is going to merge both the strings or concatenate both the strings. The way to do is I'll just copy the or let me copy all the three lines of code from the top. I'll paste it here. Now I'm going to remove the integer function that I had used. The integer function was actually used to type cast the input to integer. But now I'm going to take the values in terms of text or string. Now let's see the difference. If I run this, let's say I'll enter my number as 10 and then my second number would be let's say 20. If I add 10 and 20, it is going to merge both the numbers and return me 1 0 2 0. So when you add two strings, it is going to return you the merged value of both the strings. All right. Now, let's go ahead and explore some of the popular data types in Python. Now, I'm giving a hashtag and typing data types. This means anything followed by the hash represents a comment. So, in other programming languages also we have comments. Similarly, in Python, if you want to write a comment, you can give a hash and pass in the comment. So first let's say I have a variable a which has a value of seven or let's say 10. Now if you were to find the type of value that a variable is holding you can use the type function. If I run this it gives me integer because 10 is an integer. Let's say I want to find the type of value min -7 is. Now if I run this again - 7 is an integer. Let's say I want to know what 3.5 is actually. If I run this it gives me float because 3.5 has a floating point or a decimal point. Similarly let's see another example. I'll write -2.4566. If I run this again, this is also a float value. Similarly, now let's write a complex number which has a real part and an imaginary part. Let's say I write 7 + 10 J. If I run this, there you go. This is a complex number. Hence, it has returned the value as complex. Now if I just write 10 J let's see what it represents. Again it's a complex value. Now let's pass in a boolean value. Suppose I write true. Now true and false are boolean values. So ideally our Python should return boolean only. There you go. You can see it has returned us boolean. Now let's move ahead and explore some of the popular objects in Python. So we'll start with list. List is one of the popular objects in Python. Now to declare a list I'll create a variable called let's say L1. I'll give an equal to and within square brackets I'll pass in my list. list can hold data of multiple data types. I'll show you now. So, John here is a string. I'll give something like 102 could be the employee ID of John. I'll give USA. Let's say the country to which John belongs to. And I'll give a floating point value. Let's say 180.5 assuming it's centm. And this is John's height. Suppose if I run this you see I have created my list and if you want to print it I'll just write L1. There you go. So here I have my list L1 printed. Now suppose you want to check the type of L1. You can use the type function again and pass in the variable that is L1. There you go. It tells that L1 has the values in the form of a list. Similarly, you can create list of the same data type. Let's say only of numbers. Okay. Now, if I just print L2, you can see L2 has only numbers. Similarly, you can check the type of L2. If I run this, it is also a list. Okay. Now, another popular object in Python is called a pupil or tpple. Now the only difference between a list and a tpple is that lists are mutable which means once you declare a list you can change the values while tpples or pupils are immutable which means that once you declare a tpple you cannot change the values. Now another difference is that list is written within square brackets but when you assign a tpple it is within normal braces or round brackets as you say. So I'll create a tpple called t and within brackets I'll give the values. Let's say I want to give the values of vehicles or cars. So brands of different car we have in this tpple. Let's say BMW. Then we have Audi. Next we have Mercedes. Let's say Jaguar. Okay. Now you can use the print statement to print the tpple and let's say I also want to print the type. So I'll use the type function and pass in my variable that is t. If I run this, you can see the first print statement prints the values present in the tpple t and then we have the type. You can see it is of type or class tpple. Now next we have our third object which is called a dictionary. I'm giving comment here for readability. Okay. Now dictionary consist of key value pairs and are again mutable objects just like lists which means you can change the values of the dictionary once they are assigned and dictionaries are written within curly braces. Let's say I want to create an employee dictionary. So I'll give my variable name as employee and then start with my curly braces. First I'll give the key say the name and then we have the name of the person in terms of value that is John. Next we have his age. Let's say age is 29. Now one thing to remember here is the keys should be passed within double quotes and if the value is a string you can pass within quotes but if the value is an integer or a floating point number you don't have to pass within quotes let's say I want to have the salary of John as well again salary is in terms of number so you don't have to pass it within quotes. Let's say the third information or the fourth information I have about John is the company in which he works. Let's say John works at Oracle. So I have my dictionary defined. Now I'll write print my dictionary employee and then I also want to check the type. Okay, let's print. There you go. So the first print statement prints the dictionary employee and then we have the data type of my dictionary employee that is dicict which means dictionary or dict. The fourth Python object we are going to discuss is called a set. Now even a set is created using curly braces like how we have used in the dictionary. But the difference is sets do not contain key value pairs but they can only contain unique values. Now let me show you what I mean. Let's say I want to create a set that has only unique values of months. So within curly braces I'll pass in my month names. Let's say Jan. Then I have Feb. Then I have M for March. Let's say APR for April. So this is a set. And if I want to print, I can just pass in the variable name that is month. Let's run it. Okay. You can see it here. It has printed my set which is not in an order. Okay. Now you can also check the type of the variable that is month. If I run it, you can see it here. It is of type or class set. Now the difference I want to show here is as I mentioned set can only contain unique values. Suppose I have another value of month let's say February FEB. If I run this, I'll remove this. If I run it, you can see here still my set has only unique values. So it does not consider any duplicate values. All right. Now moving ahead, we'll explore how to extract certain values from the Python objects. We'll be using different techniques, be it indexing or slicing. Okay. So if I go to the top, I had created a list called L1. Let me print L1 again. Okay. So these are the values that I have in my list L1. Now let's say I want to print the first element in my list L1. The way to do is I'll use my print statement. I'll give my variable name that is L1 and within square brackets I'll give the index let's say 1. If I run it, it gives me the value 102. The reason being in Python the index values start from zero. So this the first value is at index zero. Then the second value is at index one. The third value is at index 2 and the fourth value is at index three. So if I to find what is there at index three, I just need to replace 1 with three. If I run this, you see it here, it gives me 180.5, which we had assumed to be the height of John in cm. Now you can also do this without even using the print statement. That is the beauty of Python. If I write L1 and within square brackets let's say I write two. If I run this it gives me USA because USA is the value that is present at index zero. So very important thing to remember while indexing or slicing is that the index values start from zero and not one. So if you are to find the value where John is present, you need to use zero instead of one. If I run this, it will return me John. Okay. Now you can also use negative indexing. Let's say I write L1 minus one. You can take a guess what this will return. If I run this, it returns me 180.5. When you use negative indexing, it will start from the last position. So minus1 is actually this index. Let's say I'll write minus3 and see the result. It gives me 102 which is fair enough. If you start from the back this is minus1, USA is at minus2 and at minus3 we have 102. All right. Now also you can use slicing. Let's say I'll write L1 within square brackets. I'll give let's say from 1 2 3. So I want all the values present from the first index which is this till the third index but excluding the third index. If I run this, it gives me 102 which is at the first index and then USA which is at the second index. Now in Python the last index is exclusive. It is not included. So in case you wanted to print 180.5 as well, you have to change this from 3 to 4. Now if I run this, you get 180.5 as well. Okay. Now moving ahead, there are some basic functions that you can use with Python objects. For example, if you want to add any new value to your list L1, you can use the append function. Suppose I write L1 dot append. Let's say I want to add the vehicle or the car that John has. Let's see. I'll write append Range Rover. If I run this and let's print L1 again, you can see it here. We have appended or added a new value to our list L1. Cool. Now you can also use the reverse function in Python if you want to print the list in reverse order. So let's say I want to print the elements present in L1 in reverse order. I just need to write my variable that is L1 followed by dot and the reverse function. This is very simple. If I run this and I will print L1. Now it prints my list in a reverse order. Here it was from John all the way till Range Rover. Once I reverse it, it starts from range rover and all the way to John. Cool. Similarly, you can also extract specific values from tpples and dictionaries. Let me show you. I'll just go to the top and okay so we had created a tpple called t let me print that tpple again so these are the values of the different brand of cars we have in the tpple let's say you want to find the car that is present in the second index so you can just write two within square brackets so I have my tpple name followed by the square brackets and then I have my index. If I run this you get Mercedes and again in Python tpples the index values start at zero and not one. So BMW is at index zero, Audi is at index one, Mercedes is at index two. You see it here and then Jaguar we have at index three. Again you can use slicing by using colon and you can also use negative indexing. Let's say I'll change the value from two to let's say minus3. See the result. You see here we have Audi because this is minus1. Mercedes is at minus2 and Audi is at minus3. Okay. Now you can extract the values in the dictionary by using the keys in the dictionary. Let me go to the top and see the dictionary that we had created. It is employee. Let me print it. Now in Jupyter Notebook, you can also use the tab to autocomplete a variable name or a function name is one of the features that Jupyter notebook provides. So here we have the employee dictionary. And let's say you want to print the value of salary. The way to do is I'll write print employee dot I can use the get function and within the get function I'll pass in my key for salary if I run this you see here it gives me 2 lakhs 50,000 which is the salary of John similarly if you want to let's say update any value in the dictionary you can use the update function. Suppose I want to update the company of John from Oracle to let's say Google. The way to do is I'll write my dictionary name that is employee followed by the update function and then within curly braces I'll pass in my key that is company give a colon and change it to say Google. Now I can just print my employee dictionary. There you go. So from company Oracle we have changed it to Google now. So these are some of the very crucial features or important features that are available in Python which helps you do a lot of calculations and data manipulations. All right. Next, let's see how to use an if else condition or an if else statement in Python. Like in any other programming languages, be it C, C++ or Java, you have if else statements. In Python also we have if else statements. So basically you check some condition and return a value. Suppose I have a variable called a which has a value of let's say 400. Then I have a variable called B which has a value of 250. Okay. Now I'll use if condition. I'll write if B is greater than A then print B is greater. Now you will also have an else statement. I'll write l if which means else if a is equal to equal to b colon then print a and b are equal finally I'll write my else statement else print a is greater So what I'm doing here is I have assigned two variables A and B to value 400 and 250 respectively. Then I'm writing an if else condition. So I'm checking a condition if B is greater than A which means I'm comparing if 250 is greater than 400. In case it is true then print B is greater. Else if both the values if they are same then print A and B are equal. Otherwise, print a is greater. Let's run it and see the result. This is fair enough. It prints me a is greater because b is not greater than a. So 250 is definitely not greater than 400. So this condition fails. Even the l condition fails. Only the final condition else is true that is a is greater than b. So this is a simple example of using the if else condition or the if else statement in Python. Now moving ahead let's see how to use a while loop in Python. Now using the while loop you can execute a set of statements as long as a condition is true. Let me show you a very simple example. I'll write I =0. So I have assigned value of 0 to I. Then I'll write while I is less than 8 then I'm going to add I. So I'll write I equal to I + 1. I'll use another condition. if I equal to equal to 4 give a colon and then I'm going to use a statement called continue. I'll print I. So using the continue statement you can stop the current iteration and continue with the next iteration. Let's print the result and see. Okay. So here we are checking if I is less than 8. So first value is 0. 0 is less than 8. Then I'm incrementing the value of I to 1. So I becomes one now. So from zero it becomes 1. Then I'm printing I. This condition continues until we have the value of I equal to 4. So if the value of I is equal to 4, I'm skipping the current iteration. So if you notice the result after three we have five. So we have skipped printing four and gone to the next value of i which is five. So the loop runs till this condition fails. So when i is not less than 8 the loop terminates. All right. Finally in our demo let's see how a for loop works. So a for loop is used for iterating over a sequence that is either a list a tuple a dictionary a set or a string. So let me go ahead and create my tpple t equal to let's say I have the same brand of cars. We have BMW then I have Audi. Let's see. I have Mercedes. I have Jaguar. And then I have BMW again. All right. I'm going to print the values present in the tpple one by one. So for that I'm going to use a for loop. So I'll write for a variable x in my tpple I'll say print x. So what it will do is this randomly assigned variable x will iterate through all the elements in the tpple and print them one by one. Let's hit shift enter to run it. There you go. You can see here I have all the different car brands printed one by one. Cool. So this brings us to the end of our demo session on introduction to Python. Let me go to the top and tell you what we did. So first I told you how to print any set of statements or any string, text, numbers in Python. You just use the print statement or the print function. You can also perform very simple mathematical calculations using Python. Then you can also check the data type of the variable using the type function. Then we saw how to use the input function. Here we inputed an integer and then we found the sum of both the integers X and Y. The value was 90. In the next example, we removed the integer function by considering our values as strings. And then we saw the difference in the results. So when you use strings, the addition operation will concatenate or merge both the strings or the numbers. Then we saw the different data types. We saw about integer data types, about floating data types, boolean data types. If I scroll down, we saw the important Python objects like list. Then we had tpple. Then we saw how to write a dictionary. How to create a set. Then we saw the difference between list and tpples and even how set can take only unique values. Then we used some indexing operations to extract specific values from list tpples dictionaries. We also learned some of the important functions. These are all inbuilt functions that Python gives us to perform manipulations and operations in Python. So you used append. Then we saw how to use reverse. Now let me just go ahead and show you another function and inbuilt function in Python. I'll just insert a cell below this. Suppose you want to find the length of your list. You can use the length function or len function that is inbuilt in Python. I'll just pass in len followed by my list that is l1. If I run this, it gives me the result five because you can see it here. My list has five elements in it. You can count it. So the length becomes five. Similarly, you can also check the length of the tpple. Let me insert a cell here as well. Suppose I write len of my tpple that is t. If I run it again, it says five. Now this was the earlier tpple that we had created. Later we actually created another tpple. Let me show you. Okay. So this was the tpple. It has taken this new tpple which has five values in it. So the length becomes five. Let me go to the top. Okay. And after that we learned how to use if else followed by how to write a while loop. And then finally we saw how to use a for loop in Python. All right. Welcome to linear regression. My name is Richard Kersner. I'm with SimplyLearn. Let's look at an example of a common use for linear regression. Profit estimation of a company. If I was going to invest in a company, I would like to know how much money I could expect to make. So we'll take a look at a venture capitalist firm and try to understand which companies they should invest in. So we'll take the idea that we need to decide the companies to invest in. We need to predict the profit the company makes and we're going to do it based on the company's expenses and even just a specific expense. In this case we have our company. We have the different expenses. So we have our R&D which is your research and development. We have our marketing. Uh we might have the location. We might have what kind of administration it's going through. Based on all this different information, we would like to calculate the profit. Now, in actuality, there's usually about 23 to 27 different markers that they look at if they're a heavy duty investor. We're only going to take a look at one basic one. We're going to come in and for simplicity, let's consider a single variable, R&D, and find out which companies to invest in based on that. So, we take a R&D and we're plotting the profit based on the R&D expenditure. how much money they put into the research and development. And then we look at the profit that goes with that. We can predict a line to estimate the profit. So we can draw a line right through the data. And when you look at that, you can see how much they invest in the R&D is a good marker as to how much profit they're going to have. We can also note that companies spending more on R&D make good profit. So let's invest in the ones that spend a higher rate in their R&D. What's in it for you? First, we'll have an introduction to machine learning followed by machine learning algorithms. These will be specific to linear regression and where it fits into the larger model. Then we'll take a look at applications of linear regression, understanding linear regression and multiple linear regression. Finally, we'll roll up our sleeves and do a little programming in use case profit estimation of companies. Let's go ahead and jump in. Let's start with our introduction to machine learning along with some machine learning algorithms and where that fits in with linear regression. Let's look at another example of machine learning. Based on the amount of rainfall, how much would be the crop yield? So we here we have our crops, we have our rainfall, and we want to know how much we're going to get from our crops this year. So we're going to introduce two variables, independent and dependent. The independent variable is a variable whose value does not change by the effect of other variables and is used to manipulate the dependent variable. It is often denoted as X. In our example, rainfall is the independent variable. This is a wonderful example because you can easily see that we can't control the rain but the rain does control the crop. So we talk about the independent variable controlling the dependent variable. Let's define dependent variable as a variable whose value change when there is any manipulation in the values of the independent variables. It is often denoted as y. And you can see here our crop yield is dependent variable and it is dependent on the amount of rainfall received. Now that we've taken a look at a real life example, let's go a little bit into the theory and some definitions on machine learning and see how that fits together with linear regression. Numerical and categorical values. Let's take our data coming in and this is kind of random data from any kind of project. We want to divide it up into numerical and categorical. So numerical is numbers age, salary, height, where categorical would be a description. the color, a dog's breed, gender. Categorical is limited to very specific items where numerical is a range of information. Now that you've seen the difference between numerical and categorical data, let's take a look at some different machine learning definitions. When we look at our different machine learning algorithms, we can divide them into three areas. Supervised, unsupervised, reinforcement. We're only going to look at supervised today. Unsupervised means we don't have the answers and we're just grouping things. Reinforcement is where we give positive and negative feedback to our algorithm to program it and it doesn't have the information till after the fact. But today we're just looking at supervised because that's where linear regression fits in. In supervised data, we have our data already there and our answers for a group and then we use that to program our model and come up with an answer. The two most common uses for that is through the regression and classification. Now we're doing linear regression. So we're just going to focus on the regression side. And in the regression we have simple linear regression, we have multiple linear regression and we have polomial linear regression. Now on these three simple linear regression is the examples we've looked at so far where we have a lot of data and we draw a straight line through it. Multiple linear regression means we have multiple variables. Remember where we had the rainfall and the crops? We might add additional variables in there like how much food do we give our crops when do we harvest them. Those would be additional information added to our model and that's why it' be multiple linear regression. And finally we have polomial linear regression. That is instead of drawing a line we can draw a curved line through it. Now that you see where regression model fits into the machine learning algorithms and we're specifically looking at linear regression. Let's go ahead and take a look at applications for linear regression. Let's look at a few applications of linear regression. Economic growth used to determine the economic growth of a country or a state in the coming quarter can also be used to predict the GDP of a country. Product price can be used to predict what would be the price of a product in the future. We can guess whether it's going to go up or down or should I buy today. Housing sales to estimate the number of houses a builder would sell and what price in the coming months. Score predictions. cricket fever to predict the number of runs a player would score in the coming matches based on the previous performance. I'm sure you can figure out other applications you could use linear regression for. So, let's jump in and let's understand linear regression and dig into the theory. Understanding linear regression. Linear regression is the statistical model used to predict the relationship between independent and dependent variables by examining two factors. The first important one is which variables in particular are significant predictors of the outcome variable. And the second one that we need to look at closely is how significant is the regression line to make predictions with the highest possible accuracy. If it's inaccurate, we can't use it. So it's very important we find out the most accurate line we can get. Since linear regression is based on drawing a line through data, we're going to jump back and take a look at some uklitian geometry. The simplest form of a simple linear regression equation with one dependent and one independent variable is represented by y = m * x + c. And if you look at our model here, we plotted two points on here. Uh x1 and y1, x2 and y2. y being the dependent variable, remember that from before. And x being the independent variable. So y depends on whatever x is. M in this case is the slope of the line where M equals the difference in the Y2 - Y1 and X2 - X1. And finally, we have C, which is the coefficient of the line or where it happens to cross the zero axis. Let's go back and look at an example we used earlier of linear regression. We're going to go back to plotting the amount of crop yield based on the amount of rainfall. And here we have our rainfall. Remember, we cannot change rainfall. And we have our crop yield, which is dependent on the rainfall. So, we have our independent and our dependent variables. We're going to take this and draw a line through it as best we can through the middle of the data. And then we look at that. We put the red point on the y ais is the amount of crop yield you can expect for the amount of rainfall represented by the green dot. So, if we have an idea what the rainfall is for this year and what's going on, then we can guess how good our crops are going to be. and we've created a nice line right through the middle to give us a nice mathematical formula. Let's take a look and see what the math looks like behind this. Let's look at the intuition behind the regression line. Now, before we dive into the math and the formulas that go behind this and what's going on behind the scenes, I want you to note that when we get into the case study and we actually apply some Python script that this math that you're going to see here is already done automatically for you. You don't have to have it memorized. It is however good to have an idea what's going on so if people reference the different terms, you'll know what they're talking about. Let's consider a sample data set with five rows and find out how to draw the regression line. We're only going to do five rows because if we did like the rainfall with hundreds of points of data, that would be very hard to see what's going on with the mathematics. So, we'll go ahead and create our own two sets of data. And we have our independent variable x and our dependent variable y. And when x was 1, we got y = 2. When x was uh 2, y was 4. And so on and so on. If we go ahead and plot this data on a graph, we can see how it forms a nice line through the middle. You can see where it's kind of grouped going upwards to the right. The next thing we want to know is what the means is of each of the data coming in, the x and the y. The means doesn't mean anything other than the average. So, we add up all the numbers and divide by the total. So, 1 + 2 + 3 + 4 + 5 over 5 equals 3. And the same for y, we get four. If we go ahead and plot the means on the graph, we'll see we get 3a 4, which draws a nice line down the middle, a good estimate. Here, we're going to dig deeper into the math behind the regression line. Now, remember before I said you don't have to have all these formulas memorized or fully understand them, even though we're going to go into a little more detail of how it works. And if you're not a math wiz and you don't know if you've never seen the sigma character before, which looks a little bit like an e that's opened up, that just means summation. That's all that is. So, when you see the sigma character, it just means we're adding everything in that row. And for computers, this is great because as a programmer, you can easily iterate through each of the XY points and create all the information you need. So in the top half, you can see where we've broken that down into pieces. And as it goes through the first two points, it computes the squared value of x, the squared value of y, and x * y. And then it takes all of x and adds them up. All of y adds them up. All of x squar adds them up. And so on and so on. And you can see we have the sum of equal to 15. The sum is equal to 20. all the way up to x * y where the sum equals 66. This all comes from our formula for calculating a straight line where y equals the slope* x plus the coefficient c. So we go down below and we're going to compute more like the averages of these. And we're going to explain exactly what that is in just a minute and where that information comes from. It's called the square means error, but we'll go into that in detail in a few minutes. All you need to do is look at the formula and see how we've gone about computing it line by line instead of trying to have a huge set of numbers pushed into it. And down here you'll see where the slope m equals and then the top part if you read through the brackets you have the number of data points times the sum of x * y which we computed one line at a time there. And that's just the 66. and take all that and you subtract it from the sum of x times the sum of y and those have both been computed. So you have 15 * 20 and on the bottom we have the number of lines times the sum of x^2 easily computed as 86 for the sum minus I'll take all that and subtract the sum of x^2 and we end up as we come across with our formula you can plug in all those numbers which is very easy to do on the computer you don't have to do the math on a piece of paper or calculator and you'll get a slope of 6 and you'll get your C coefficient. If you continue to follow through that formula, you'll see it comes out as equal to 2.2. Continuing deeper into what's going behind the scenes, let's find out the predicted values of y for corresponding values of x using the linear equation where m=6 and c= 2.2. We're going to take these values and we're going to go ahead and plot them. We're going to predict them. So y =6 * x = 1 + 2.2 = 2.8 so on and so on. And here the blue points represent the actual y values and the brown points represent the predicted yv values based on the model we created. The distance between the actual and predicted values is known as residuals or errors. The best fit line should have the least sum of squares of these errors also known as equare. If we put these into a nice chart where you can see X and you can see Y what the actual values were and you can see Y predicted you can easily see where we take Y minus Y predicted and we get an answer. What is the difference between those two and if we square that Y - Y prediction squared we can then sum those squared values. That's where we get the 64 plus the.36 + 1 all the way down until we have a summation equals 2.4. So the sum of squared errors for this regression line is 2.4. We check this error for each line and conclude the best fit line having the least e value. In a nice graphical representation, we can see here where we keep moving this line through the data points to make sure the best fit line has the least squared distance between the data points and the regression line. Now, we only looked at the most commonly used formula for minimizing the distance. There are lots of ways to minimize the distance between the line and the data points like sum of squared errors, sum of absolute errors, root mean square error, etc. What you want to take away from this is whatever formula is being used, you can easily using a computer programming and iterating through the data calculate the different parts of it. That way, these complicated formulas you see with the different summations and absolute values are easily computed one piece at a time. Up until this point, we've only been looking at two values, X and Y. Well, in the real world, it's very rare that you only have two values when you're figuring out a solution. So, let's move on to the next topic, multiple linear regression. Let's take a brief look at what happens when you have multiple inputs. So, in multiple linear regression, we have uh well, we'll start with the simple linear regression where we had y = m + x + c and we're trying to find the value of y. Now with multiple linear regression we have multiple variables coming in. So instead of having just x we have x1 x2 x3 and instead of having just one slope each variable has its own slope attached to it. As you can see here we have m1 m2 m3 and we still just have the single coefficient. So when you're dealing with multiple linear regression you basically take your single linear regression and you spread it out. So you have y = m1 * x1 + m2 * x2 so on all the way to m to the nth x to the nth and then you add your coefficient on there implementation of linear regression. Now we get into my favorite part. Let's understand how multiple linear regression works by implementing it in Python. If you remember before we were looking at a company and just based on its R&D trying to figure out its profit. We're going to start looking at the expenditure of the company. We're going to go back to that. We're going to predict his profit, but instead of predicting it just on the R&D, we're going to look at other factors like administration costs, marketing costs, and so on. And from there, we're going to see if we can figure out what the profit of that company's going to be. To start our coding, we're going to begin by importing some basic libraries. And we're going to be looking through the data before we do any kind of linear regression. We're going to take a look at the data to see what we're playing with. Then we'll go ahead and format the data to the format we need to be able to run it in the linear regression model. And then from there, we'll go ahead and solve it and just see how valid our solution is. So let's start with importing the basic libraries. Now I'm going to be doing this in Anaconda Jupyter notebook, a very popular IDE. I enjoy it because it's such a visual to look at and so easy to use. Um just any ID for Python will work just fine for this. So break out your favorite Python IDE. So, here we are in our Jupyter notebook. Let me go ahead and paste our first piece of code in there. And let's walk through what libraries we're importing. First, we're going to import numpy as np. And then I want you to skip one line and look at import pandas as pd. These are very common tools that you need with most of your linear regression. The numpy, which stands for number python, is usually denoted as np and you have to almost have that for your sklearn toolbox. So, you always import that right off the beginning. pandas. Although you don't have to have it for your sklearn libraries, it does such a wonderful job of importing data, setting it up into a data frame so we can manipulate it rather easily and it has a lot of tools also in addition to that. So we usually like to use the pandas when we can and I'll show you what that looks like. The other three lines are for us to get a visual of this data and take a look at it. So we're going to import mapplot library.pipplot as plt and then seabour as sns. Seabor works with the mattplot library. So you have to always import mapplot library and then seabour sits on top of it. And we'll take a look at what that looks like. You could use any of your own plotting libraries you want. There's all kinds of ways to look at the data. These are just very common ones. And the seabor is so easy to use. It just looks beautiful. It's a nice representation that you can actually take and show somebody. And the final line is the amberigned map plot library inline. That is only because I'm doing an inline IDE. My interface in the Anaconda Jupiter notebook requires I put that in there or you're not going to see the graph when it comes up. Let's go ahead and run this. It's not going to be that interesting. So, we're just setting up variables. In fact, it's not going to do anything that we can see, but it is importing these different libraries and setup. The next step is load the data set and extract independent and dependent variables. Now, here in the slide, you'll see companies equals PD read CSV. And it has a long line there with the file at the end. 1000 companies.csv. You're going to have to change this to fit whatever setup you have. And the file itself, you can request. Just go down to the commentary below this video and put a note in there and SimplyLearn will try to get in contact with you and supply you with that file so you can try this coding yourself. So, we're going to add this code in here. And we're going to see that I have companies equals PD.reader CSV. And I've changed this path to match my computer. C/simplearn/1000 companies.csv. And then below there, we're going to set the x equals to companies under the i location. And because this is companies is a pd data set, I can use this nice notation that says take every row, that's what the colon, the first colon is, comma, except for the last column. That's what the second part is where we have a colon minus one and we want the values set into there. So x is no longer a data set a pandanda's data set but we can easily extract the data from our pandas data set with this notation and then y we're going to set equal to the last row. Well the question is going to be what are we actually looking at? So let's go ahead and take a look at that and we're going to look at the companies. Which lists the first five rows of data and I'll open up the file in just a second so you can see where that's coming from. But let's look at the data in here as far as the way the pandas sees it. When I hit run, you'll see it breaks it out into a nice setup. This is what pandas, one of the things pandas is really good about is it looks just like an Excel spreadsheet. You have your rows and remember when we're programming, we always start with zero. We don't start with one. So it shows the first five rows 0 1 2 3 4 and then it shows your different columns. R&D spend, administration, marketing spend, state, profit. It even notes that the top are column names. It was never told that, but Pandas is able to recognize a lot of things that they're not the same as the data rows. Why don't we go ahead and open this file up in a CSV so you can actually see the raw data. So here I've opened it up as a text editor. And you can see at the top we have R&D spend, administration, marketing spend, state, profit carriage return. I don't know about you, but I'd go crazy trying to read files like this. That's why we use the pandas. You could also open this up in an Excel and it would separate it since it is a comma separated variable file. But we don't want to look at this one. We want to look at something we can read rather easily. So let's flip back and take a look at that top part, the first five row. Now, as nice as this format is where I can see the data, to me it doesn't mean a whole lot. Maybe you're an expert in business and investments and you understand what $165,34920 compared to the administration cost of $136,897.80 so on so on helps to create the profit of $192,26183. That makes no sense to me whatsoever. No pun intended. So let's flip back here and take a look at our next set of code where we're going to graph it so we can get a better understanding of our data and what it mean. So at this point we're going to use a single line of code to get a lot of information so we can see where we're going with this. Let's go ahead and paste that into our uh notebook and see what we got going. And so we have the visualization and again we're using SNS which is pandas. As you can see we imported the map plot library.pipplot piplot as plt which then the seabor uses and we imported the seabor as sns and then that final line of code helps us show this in our inline coding without this it wouldn't display and you can display it to a file and other means and that's the map plot library in line with the amber sign at the beginning so here we come down to the single line of code seabor is great because it actually recognizes the panda data frame so I can just take the companies core for coordinates and I can put that right into the seaborn. And when we run this, we get this beautiful plot. And let's just take a look at what this plot means. If you look at this plot on mine, the colors are probably a little bit more purplish and blue than the original one. Uh we have the columns and the rows. We have R and D spending, we have administration, we have marketing spending, and profit. And if you cross index any two of these, since we're interested in profit, if you cross index profit with profit, it's going to show up, if you look at the scale on the right, way up in the dark. Why? Because those are the same data. They have an exact correspondence. So R&D spending is going to be the same as R&D spending and the same thing with administration costs. So right down the middle, you get this dark row or dark um diagonal row that shows that this is the highest corresponding data. It's exactly the same. And as it becomes lighter, there's less connections between the data. So we can see with profit, obviously profit is the same as profit. And next, it has a very high correlation with R&D spending, which we looked at earlier, and it has a slightly less connection to marketing spending, and even less to how much money we put into the administration. So, now that we have a nice look at the data, let's go ahead and dig in and create some actual useful linear regression models so that we can predict values and have a better profit. Now that we've taken a look at the visualization of this data, we're going to move on to the next step. Instead of just having a pretty picture, we need to generate some hard data, some hard values. So let's see what that looks like. We're going to set up our linear regression model in two steps. The first one is we need to prepare some of our data so it fits correctly. And let's go ahead and paste this code into our Jupyter notebook. And what we're bringing in is we're going to bring in the sklearn pre-processing where we're going to import the label encoder and the one hot encoder. To use the label encoder, we're going to create a variable called label encoder and set it equal to capital L label, capital E encoder. This creates a class that we can reuse for transferring the labels back and forth. Now, about now, you should ask, what labels are we talking about? Let's go take a look at the data we processed before and see what I'm talking about here. If you remember when we did the companies.head and we printed the top five rows of data, we have our columns going across. We have column zero which is R&D spending, column one which is administration, column two which is marketing spending and column three is state and you'll see under state we have New York, California, Florida. Now to do a linear regression model, it doesn't know how to process New York. It knows how to process a number. So the first thing we're going to do is we're going to change that New York, California, and Florida and we're going to change those to numbers. That's what this line of code does here. x equals and then it has the colon comma 3 in brackets. The first part, the colon, comma, means that we're going to look at all the different rows. So, we're going to keep them all together, but the only row we're going to edit is the third row. And in there, we're going to take the label coder and we're going to fit and transform the x also the third row. So, we're going to take that third row, we're going to set it equal to a transformation. And that transformation basically tells it that instead of having a uh New York it has a zero or a one or a two. And then finally we need to do a one hot encoder which equals one hot encoder categorical features equals three. And then we take the x and we go ahead and do that equal to one hot encoder fit transform x to array. This final transformation preps our data for us. So it's completely set the way we need it as just a row of numbers. even though it's not in here. Let's go ahead and print x and just take a look what this data is doing. You'll see I have an array of arrays and then each array is a row of numbers. And if I go ahead and just do row zero, you'll see I have a nice organized row of numbers that the computer now understands. We'll go ahead and take this out there because it doesn't mean a whole lot to us. It's just a row of numbers. Next on setting up our data, we have avoiding dummy variable trap. This is very important. Why? because the computers automatically transformed our header into the setup and it's automatically transformed all these different variables. So when we did the encoder, the encoder created two columns and what we need to do is just have the one because it has both the variable and the name. That's what this piece of code does here. Let's go ahead and paste this in here. And we have x= x colon, one colon. All this is doing is removing that one extra column we put in there when we did our one hot encoder and our label encoding. Let's go ahead and run that. And now we get to create our linear regression model. And let's see what that looks like here. And we're going to do that in two steps. The first step is going to be in splitting the data. Now whenever we create a uh predictive model of data, we always want to split it up. So we have a training set and we have a testing set. That's very important. Otherwise, we'd be very unethical without testing it to see how good our fit is. And then we'll go ahead and create our multiple linear regression model and train it and set it up. Let's go ahead and paste this next piece of code in here. And I'll go ahead and shrink it down a size or two so it all fits on one line. So from the sklearn module selection, we're going to import train test split. And you'll see that we've created four completely different variables. We have capital X train capital X test smallerase Y train smallerase Y test. That is the standard way that they usually reference these when we're doing different uh models. Usually see that a capital X and you see the train and the test and the lowercase Y. What this is is X is our data going in. That's our R&D spin, our administration, our marketing. And then Y, which we're training, is the answer. That's the profit because we want to know the profit of an unknown entity. So that's what we're going to shoot for in this tutorial. The next part, train test split. We take X and we take Y. We've already created those. X has the columns with the data in it and Y has a column with profit in it. And then we're going to set the test size equals.2. That basically means 20%. So 20% of the rows are going to be tested. We're going to just put them off to the side. So since we're using a thousand lines of data, that means that 200 of those lines we're going to hold off to the side to test for later. And then the random state equals zero, we're going to randomize which ones it picks to hold off to the side. We'll go ahead and run this. It's not overly exciting cuz it's setting up our variables. But the next step is the next step we actually create our linear regression model. Now that we got to the linear regression model, we get that next piece of the puzzle. Let's go ahead and put that code in there and walk through it. So here we go. But we're going to paste it in there. And let's go ahead and uh since this is a shorter line of code, let's zoom up there so we can get a good look. And we have from the sklearn.linear_model, we're going to import linear regression. Now, I don't know if you recall from earlier when we were doing all the math. Let's go ahead and flip back there and take a look at that. Do you remember this where we had this long formula on the bottom and we were doing all this summization and then we also looked at setting it up with the different lines and then we also looked all the way down to multiple linear regression where we're adding all those formulas together. All of that is wrapped up in this one section. So what's going on here is I'm going to create a variable called regressor and the regressor equals the linear regression. That's a linear regression model that has all that math built in. So we don't have to have it all memorized or have to compute it individually. And then we do the regressor.fit. In this case, we do X train and Y train because we're using the training data. X being the data in and Y being profit what we're looking at. And this does all that math for us. So within one click and one line, we've created the whole linear regression model. And we fit the data to the linear regression model. And you can see that when I run the regressor, it gives an output linear regression. It says copy x equals true fit intercept equals true in jobs equal 1 normalize equals false. It's just giving you some general information on what's going on with that regressor model. Now that we've created our linear regression model, let's go ahead and use it. And if you remember, we kept a bunch of data aside. So we're going to do a y predict variable and we're going to put in the x test. And let's see what that looks like. Scroll up a little bit. Paste that in here. Predicting the test set results. So here we have y predict equals regressor.predict x test going in and this gives us y predict. Now because I'm in jupiter in line I can just put the variable up there and when I hit the run button it'll print that array out. I could have just as easily done print y predict. So if you're in a different IDE that's not an inline setup like the Jupyter notebook, you can do it this way. Print y predict. And you'll see that for the 200 different test variables we kept off to the side, it's going to produce 200 answers. This is what it says the profit are for those 200 predictions. But let's don't stop there. Let's keep going and take a couple look. We're going to take just a short detail here and calculating the coefficients and the intercepts. This gives us a quick flash at what's going on behind the line. We're going to take a short detour here and we're going to be calculating the coefficient and intercepts so you can see what those look like. What's really nice about our regressor we created is it already has the coefficients for us. We can simply just print regressor.cefficient underscore. When I run this, you'll see our coefficients here. And if we can do the regressor coefficient, we can also do the regressor intercept. And let's run that and take a look at that. This all came from the multiple regression model. And we'll flip over so you can remember where this is going into, where it's coming from. You can see the formula down here where y = m1 * x1 + m2 * x2 and so on and so on plus c the coefficient. So these variables fit right into this formula. y = slope 1 * column 1 variable plus slope 2 * column 2 variable all the way to the m to the n and x to the n + c the coefficient. Or in this case you have - 8.89 8 9 to the power of two etc etc times the first column and the second column and the third column and then our intercept is the minus one3009 point it gets kind of complicated when you look at it. This is why we don't do this by hand anymore. This is why we have the computer to make these calculations easy to understand and calculate. Now I told you that was a short detour and we're coming towards the end of our script. As you remember from the beginning, I said if we're going to divide this information, we have to make sure it's a valid model that this model works and understand how good it works. So calculating the R squar value, that's what we're going to use to predict how good our prediction is. And let's take a look at what that looks like in code. And so we're going to use this from sklearn.metrics. We're going to import R2 score. That's the R squared value. We're looking at the error. So in the R2 score, we take our Y test versus our Y predict. Y test is the actual values we're testing. That was the one that was given to us that we know are true. The Y predict of those 200 values is what we think it was true. And when we go ahead and run this, we see we get a 9352. That's the R2 score. Now, it's not exactly a straight percentage, so it's not saying it's 93% correct, but you do want that in the upper 90s, 0 and higher shows that this is a very valid prediction based on the R2 score. And if R squared value of N1 or 92 as we got on our model, remember it does have a random generation involved. This proves the model is a good model, which means success. Yay. We successfully trained our model with certain predictors and estimated the profit of the companies using linear regression. So now that we have a successful linear regression model, let's take a look at what we went over today and take a look at our key takeaways. First, we have an introduction to machine learning. We talked about some general setup and predicting crops and weather. We saw this that numbers are age, salary, so on. Then we have categorical, color. When we did our actual regression model, we saw that we had numbers which was dollar amounts and we had a location which was Florida and New York which was categorical that we had to convert. Next we have application of a linear regression model. We had some showing there some different applications you could use it for. We had our use case implementation of linear regression where we dug in deep and showed how those are set up. We had our multiple linear regression model so you can see the math behind it. And finally prediction using the regression line. So, we showed you how to predict things on a regression line setup and the actual scripting and code. That concludes our demo today. In this video, we will be introduced to the concept of variables and expressions. Let's start with expressions. Well, an expression in Python is anything that results in a value. its difference or the difference between an expression and a standard statement in Python is that a statement in Python is something that results in an action or an execution of a command. There is no calculation per se or any sort of manipulation or as I said earlier calculation resulting in between two different say numbers or other objects that results in a value and this happens in the case of an expression. So for example if I were to add two numbers say 5 + 10. This is an expression which should where two objects five and 10 are being added and a result is being given to me which is 15. Now let's see an example of a statement. Well a statement could be something like a print statement. So when I ask Python to print something for me it's not calculating anything. It's not evaluating anything. It's simply performing an action that I have or a command that I have given the system which is to print whatever I have entered over here as you can see. So this is a statement. Similarly there are uh another type of statement known as assignment statements which we will learn actually right in the next section. Well let's move on to variables right now. Now earlier you might have learned about objects in Python. So objects are essentially data uh that we can work with or manipulate in Python. Now variables are a place to store these different objects. Um, usually uh it's much easier to store objects in Python instead of using the objects explicitly themselves because objects may be very comp complicated whereas variables are very simple to use and very simply uh very are named in a very simple manner so that we can use them quite often in our programs. Now it's important to note that in Python we do not have to explicitly declare or define a variable to create them. In fact, variables are created in the same statement where we assign some object to that variable. In Python, the assignment of variables happens using the assignment operator which is the equals to sign. So if I were to if I wanted to assign a number say 100 to a variable say B. Well, it would be as simple as doing this. Now anytime I want to know what is stored in B, I just have to enter B and I will get the value that is stored in it. And we can store different types of objects inside a any variable. Let's say I store I want to store this particular word in the form of a string. Now this is stored in X. And if I want to know what is an X, I can just see what is here and it's a string hello which I had stored earlier. Now it's very important that we follow certain naming rules when it comes to naming our variables in Python or else we may be thrown errors or we might get some problems in our program. So the first rule is that you cannot start your variable name with a number. So if I were to give a variable name like for num is equal to something say the same string hello this would be an illegal variable name and I would get an error when I try this. So if I were to choose say num for is equal to hello. This would be perfectly fine. I won't get an error. The second rule is we are not allowed to use any non-alpha numeric characters except the underscore character while naming our variable. So for example, if I were to name my variable num_4 and I said random some random string, this is perfectly fine. However, if I change num_4 to num-en 4, this will throw me an error as you can see over here. Next, the third rule is that variable names are case sensitive. So if if I have a variable called num or say num. If this was one of my variables and I stored say the value 50, this is not the same as me writing num all in capitals and say storing some other value 60. These are two different variables. So it's very important that when you name your variables, you are aware of what case you are writing them in because Python variables are case sensitive. Now the final and probably one of the most important rules in Python is that you're not allowed to use what we call reserve words in Python. Now Python has a bunch of reserve words. These words essentially are performed or they are used to perform certain special functions and are used as identifiers for certain special functions in Python that we that have a special purpose like that particular word has a special purpose within the Python programming language and we cannot use them to name our variables. So a list of them as you can see over here some of them are like false deaf if uh del raise and there are like there are around 33 of them and uh sometimes some of these words are taken out sometimes there are more words added as python gets updated now let's let's move on to a concept uh where we try to really understand what do we mean by storing data inside a variable now when I say that we are storing something inside a variable able you get an image or you get an idea in your head that this variable is like a container where we store or we put a particular object inside it. So does this mean that variables have a place in the memory? Are they also an object that is placed in the memory where we can put other objects inside of? Well, I'll show you an example where this idea is kind of disproven or where I'll show you that the idea of variable is kind of different than what we understand by storing something inside a variable. Now let's say I'm storing the same object. Uh I will take this object as some random number. Let's say 100. And I'm going to store it into two different variables. So I take x is equal to 100 over here. And in the next line I will store 100 in y as well. Now if objects were I mean if variables were objects that we placed in our system then x and y should have two different ids. But let's see what happens when I check the ID of X and the ID of Y. They should be different in case variables were their own objects. However, as you can see, the ID of X and the ID of Y are exactly the same. So, well, obviously we know that two different objects in Python cannot have the same ID. So, does this so what does this mean when it comes to the nature of a variable? Well, the the fact is that variables are not actually objects in Python. In fact, variables are more like pointers or references to the actual object. So when I said x is equal to 100 and y is equal to 100, what I'm actually telling the system is that there is this variable name called x that I want to refer or point towards the actual object that is in my memory called 100. And I do the same thing with another variable y. So I am not exactly creating a different space in my system for these variables. These variables are more like placeholders or references that point to a particular place in the memory where this object is located. And that is what happens when I do ID of X and ID of Y. It's not giving me the ID of something called X or the ID of something called Y. It's giving me the ID of 100 each time because 100 is stored in X and Y. So as you can see this is something like a visual visual representation of what I'm trying to say. And similarly if if I were to so in this example on the left as you can see I've used the uh value of 50 to explain my point. So as you can see initially x and y are pointing towards this value 50 because I have stored 50 and 50 in both x and y. Now what if I changed the object that is stored in x? Well that means x is now pointing towards this new object and I have stored this new object called hello. the string called hello as you can see here. So now X is pointing to a new object in the system and it's called hello and I haven't changed anything with respect to Y. So Y will point towards the same thing and I can also make the pointer of Y different or what I'm trying to say is I can store something else in Y. See I stored this list in Y 50 60 and 100 and when I do that now Y is pointing towards a different object in Python and X is also pointing towards something else. So what happens to this object 50? Well, now that there's no variable referring to this object 50, it becomes what we call an orphaned object. And we can actually see that when we do the ID of X, now let's go back to my example on the right where I stored X is equal to 100. So as you can see, it gives me some ID represented by this number. Now let's say I stored X, I stored something different in X, say 200. Now if I do the ID of X, it should give me a different ID because as you can see the values are different and this is because this object 200 is what is stored in X and when I do the ID of X, I'm actually getting the ID of this new object now 200. So as you can see point as you can see variables are they should be and they are treated more like references or pointers uh instead of containers for a particular object. In this video, you will be introduced to the concept of objects in Python. Now, what are objects in Python? Well, all the data that we manipulate or use in our operations in our code are represented as objects or relations between objects. So, if you want an example, two numbers that you add in your code, well, both of them are objects and they're usually numeric type objects. Similarly, if you want to store someone's name or an address of your hometown in your code, you usually store it in a form of text and that text is also an object, usually a string. Similarly, you might want to create a container of different smaller objects inside them and that big container is also a type of an object and one of those containers are called lists. Similarly, we have many different types of objects which we will be covering during the duration of this course. The following pictorial representation gives us a broad classification of different Python data types and objects. As we can see there are five broad classifications. Numeric which contains integers, complex numbers and floats. We have the dictionary data type. We have the boolean which essentially consists of objects that have only two valid values true or false. Then we have sets and we have sequence data types such as strings, lists and pupils. Every object in Python has three important properties that kind of define that particular object. They are its identity, its type and its value. The identity of an object is it can be considered as a place in the memory or its address in the memory where an object is stored. An object's identity does not change once it has been created. So for example, if I took an example of say a string called hello, I can use the id function which is this is how you use the id function to find out the id of this object and it should be uh the answer should be in the form of uh a particular long integer. So as you can see the id of hello is this integer 2 3264 766 23024. Similarly all objects in python have an identity. We have another operation with uh regarding the identity of an object is the is operator and the is operator compares the identity of two objects. So if two objects have the same identity, it should give the answer true otherwise false. Now the next property of objects is the type. The type of an object is essentially uh what defines what kind of values and operations that that object can have. So for example, a numeric type in Python such as an integer would allow us to do arithmetic operations and similarly string object type in Python will allow us to do concatenation operations. Much like its identity, the type of an object cannot be changed as well. Now let's see what's the type of that particular object that I had shown you above. As you can see, it should be string. Str stands for string. Similarly, if I did a type for a number, say 50, it should give me int as you can see. Now, the third property of an object in Python is its value. Now, the value is the actual data that is contained in the object. It's the thing that we use that we display that we can manipulate and that we can perform operations on. So, for example, if I were to talk about this string, the string is hello. So the value of the string is the word or the sequence of letters that spell out hello. And similarly this particular object is a numeric object with the value 50. Let's see in this in this pictorial representation I have shown you that you can assume this dish blue area as the memory of the system. Now within this memory we have objects created called 50 and hello and this list containing smaller objects called cat pen and 40. Now what I'm trying to show you here is each of these objects have a type an ID and a value. And let's see what they are. So if I wanted to see as we had already seen the type of 50. Now let's find the ID of 50. As you can see, we get this unique ID for the object 50, which is obviously not the same as the ID of the object hello above. And the value of this object is the value of 50 itself. So that was a brief introduction into objects in Python. In this video, we will cover the topics of numeric object types and type conversions in Python. Now, let's get started. Numeric object types in Python are a broad classification of objects. Plenty of common real life applications that we associate with numerals such as arithmetic operations and calculus are done in Python using numeric object data types. There are three main types of numerals or numeric data types in Python. They are integers, float and complex. Integers as the name suggests represent all integer values in Python. These can be such as 100 minus 3 to 305 and so on. Floats are used to represent numerals that contain decimal points such as 100.3, 3.9, 4.8, - 6.8 and so on. Then there is the complex object type which is used to represent complex numbers in Python. And this is an example of a complex number 3 + 7 J. This is a complex number 3 + 7 J where the real component is 3 and the imaginary component is 7. All complex numbers have a real and an imaginary component. And this is how we denote them. Now it's important to note that this J is a very common symbol in maths and engineering used to denote the imi imaginary portion of a complex number. And it's the same in Python as well. So moving on, let's talk about type conversion in Python. Type conversion refers to the conversion of an object from one data type to another. For example, from string to an integer. And there are different types of conversions. And let's go through some of them. First of all, let's consider the conversion of something to an integer. And more specifically, let's consider the example where we convert a string to an integer. So let's consider my example string to be some something called 145. And I'm storing this within a string for a reason because I want to convert a string to an integer. So as you can see, I have stored this 145 within my uh within a variable x. And if I were to confirm that is actually an integer, I can use the type function and see that it is actually an integer str. Now let's convert this into an integer using the int function. So how would we use it is simply passing the int uh simp by passing the variable that we want to convert into the int function. And as you can see this is what I've done over here. But before this I will also store it into another variable. All right. Now let's see what's in Y. Well, Y gives us 1 145. But are we sure that it's an int? We can just check it by using the type function. And as you can see, the type gives us uh the answer int. That means that we've converted the string to an int. Interestingly, we can convert something and also specify what base we have to treat the number as. By default, when we pass something in the int function, the program or Python will treat whatever we have passed uh within the string as something in base of decimal. But what if we were to pass something that is well not in a decimal? Well, we could do it like this. Now, let's store y and in y let's store let's do this and I will show you what this means. X and I will give a comma and 8. What this tells Python is well I want to convert the string in X into an integer but I want to treat whatever is in X as a number of base 8. So 145 is actually 145 in the base 8. Now when we see what's in Y we get 101 which is different to 145 because well 101 is actually the decimal equivalent of 145 in the base 8. Similarly, if we were to give it in the base two, we would just replace 8 with two over here. Now, next, let's talk about converting something to a float. Now, as we know, float is just simply something that has a decimal point. So, let's convert an integer into a float. How would we do that? Simply by using the float function. So, if I give 120 and I pass this integer through the float function, I should get 120.0. These two are not the same though they may mean the same things to us but in for Python 120 is an integer and 120.0 is a different object which is a float and this is how we convert something to a float. Now what if we wanted to convert an integer to a string? Well, we can use the str function. We can also use the hex and the o function if we want to convert a particular number to a hexadimal or an octal string respectively. Let's look at all three examples. Say I have a number 688 and I want to convert this into a string. I just use the str function around this. So as we know just to confirm let's see what the type of 688 is. This is this should be an int as we can see. But when we pass when we let's say uh we convert it into a string 688 and let's store it into some variable let's call it a. All right. Now let's see what's an A. It gives a 688 in the form of a string. But we can confirm by checking the type of A. It is an S str. So it is a string. Similarly, we can use hex if we wanted to convert an integer into its hexadesimal equivalent in the in in in as an output of a string. As you can see this comp this sort of this sort of complicated thing is actually telling us that we have converted 688 in terms of hexa decimals. Now how do we read this output? This zero should not be treated as anything. It's more like an identifier which says that the letter after this first zero is the base of this whole number. So it's saying that x stands for hexadimal. So it's saying that 2B 0 is to be treated as a hexadimal number and if you did basic maths uh you would find that the decimal equivalent of 2b 0 is 688. Similarly if I did opt if I use the oct function for 688 it will give me the result something like this. So again how to read this the first zero is to be treated as an identifier and not part of the number. What it's saying is the letter after the first or after this zero is the base of the number following these two uh following these two characters. So as you can see as you can see what's this saying is that this whole number is an oct represented by this letter O and the number itself that we are dealing with is 1260 and 1260 in octal system is the equivalent of 688 in the decimal system. So this is how we've converted a an integer in uh an integer into a hexadimal or a an octal string. Now what if we wanted to convert two uh numeric objects into a complex data object? Well, for that we can use the complex function. As you can see here, if I passed complex five and say um say something like 3.1, let's see what this gives us. As you can see, it gives us an uh a complex object where our real part is five and our imaginary part is 3.1. So what this does is essentially uh you pass two numbers. The first one is the real component and the second is the uh imaginary component. It combines to give you a complex number as you can see here. Now let's look at some other type conversions. The next type we will be talking about is converting something to a pupil or a set or a list. So what if I wanted to convert a string into a pupil. I will use the pupil function and uh as you can see over here. So let's say I have a string called random. Let's call it random. All right. Now if I want to convert this to a pupil, I will use the pupil function. As you can see over here, the output will give me pupils where each individual component of this pupil are the individual components or the individual characters of my string random. Similarly, if I wanted to convert the string to a list, I would use the list function and this will give me a list of the individual characters of the word or the the string random. And I could do the same thing with the set function to create a set of individual characters of uh the string random as you can see here. So yeah, these were some of the basic Python uh type conversions that I have shown you. As we learn about more complex data types and different object types in Python, we will learn different uh type conversions and functions associated with them. In this video, you will be introduced to the concept of strings. Strings are an object type in Python used to store textual data. This textual data could be something like, for example, someone's name. It could be an address of a place or it could be something like a word or a sentence. Now, strings in Python are an object type that is actually a subtype of a broader classification of objects called sequences. Sequences are objects in Python which contain components placed one after the other where each component is given what we call an index a numeric index. This numeric index identifies the component in the sequence and is positioned within the sequence. Now since Python is a zero index based language the first object of any sequence is always index zero. The second is index one. The third is index two and so on. Now let's move on to defining strings. How do we define them? Well, strings are defined using what we call delimiters. And delimiters occur in pairs, an opening and a closing delimiter. In Python, these delimiters are pairs of single or double quotes. So, for example, if I were to use single quotes to define a string, I could I would do it like this. This is a string. As you can see, this is a string is printed over here. This is delimited using single quotes. Similarly, if I were to define a string using double quotes, it's as simple as doing this. This is also a as you can see. Now, a valid question arises. What if I wanted to include a single quote within my string? Well, if I try to, let's see what happens if I try to print a statement with a single quote naturally inside it. So say I'm saying it's my birthday. All right. Let's see what happens if I try to print this statement. It says an invalid syntax. And this is because as I had mentioned in Python single quotes and double quotes are delimiters. So they have a special function in Python and we cannot simply just use them within a string because Python will treat them specially for a special purpose. So what Python will do is it will pair this and this delimiters or single quotes and say okay this looks like a string to me and it's not going to understand the rest of this and then it'll find a random single quote and it won't find its pair and it will basically throw us an error because of this. So one way or one workar around to introduce single quotes into a string is simply to delimit our string itself using the other type of delimiter or double quotes. So if I want to include a single code, I will close my string using double quotes. So if I did this instead of what I had done earlier, this is a perfectly valid and legal way of defining my string. And I could I would do the opposite. If I had to introduce say double quotes within my string, I would use single quotes to close it. So if I were to write a speech statement, she said I am hungry. All right, I have I want these two double quotes to essentially show that this is a speech and I'm going to close the string using single quotes. And this is a valid way of doing it. As you can see, my string has these two double quotes. Now I had mentioned that strings are sequences. Well, let's describe how are they sequences. So, first of all, all sequences in Python are a um it's a essentially a sequence of smaller objects that form the sequence, right? So, let's try to understand like how do how are strings to be treated as sequences. Some sequences that we will learn later are like lists or pupils. And in all sequences we can retrieve any particular object or component in that sequence using its index. And how do we use indexes in Python? Well, we enclose a numeric index within square brackets. Now let's see what I'm trying to talk about by this example. Now say I'm storing a particular string called say random within. Now my variable name here is test and I'm storing the string random within it. All right. And this is a way of retrieving a particular uh index from test. I write the variable name and then next to it I put the square brackets. Now I'm supposed to put um a numeral within the square brackets to uh tell Python like what particular index I'm looking for. So let's try zero. Now as I said Python is zero index based language. So the zero index essentially means the first part of or the first component of a sequence. So let's see what happens when I ask Python to retrieve the first part of uh what is stored in test. It gives me the string R. In fact, it's just one character R and it's a string. So as you can see, it has given me the first character of the string random which is R. So what this shows is that this string random is essentially a collection or a sequence of one character strings. So this is so random is essentially a collection of the string r the string a the string n and so on together combined to form the whole string that we call random. Similarly if I wanted the character at the third position of this thing I would use well since it's the third position and since we're working in python I would use the index 2 and it should give me the letter n. As you can see it's given me the letter n. So this is how we can show that uh strings are also sequences in Python. Well, now let's move on to um certain string operations and functions that we can perform. Well, as you have seen already that we can retrieve any particular character within a um within a string using numeric indexes. Well, what if we wanted to know what index is a particular character at? And we can do it like this. We use the index function. So let's say my test and I want to know where or what position the character D is in my string random. So this is how I would use the index function index and within the index say I'm looking for the character D and now I it should give me the answer 0 1 2 it should give me the answer three. So if I execute this it gives me the index three. So this is uh the index function that we can use for our strings. Now what if we want to um say slice a string into a into its uh into a smaller part. We want to slice it and we want to retrieve only that particular slice. Well, here is an example. Say I'm going to I'm going to use the same string called random and it's stored in test. Right? So let's so one way of uh so the way of doing this is like this. Now let's see what this let's just see what this does first. As you can see, it's given me a and d. And if I went to my string random, a d is this particular slice. Now, let's try to understand what I have done by entering 1 colon 4. Well, what this has done is it's retrieved a slice from the first position uh the first index which is the character a up until the fourth position which is the character O. But it has not included O. So it's very important when I index like this every every time I put something to the right of this colon I am not including this itself. All right. So it's essentially one when I say one colon 4 what I'm telling Python is give me the first the second and the third character but not the fourth one. So it's given me a n and d and it's that's this is how we do a slicing operation in python. Similarly, if I were to say slice from the first, if I wanted only the first and the second character, I would do this is what I would do. If I wanted the first and the second character, I would do 1 2 3. So this way it tells Python that I want the first, I want the second, but not the third of. So as you can see, I get a and n. Now, so this is a basic slicing. What if I wanted the portion of a string from a particular point all the way to the right or all the way to the left? Well, let's consider the example of where I want the portion of the string random from the say fourth character until the left. Well, the way I would do this is test. Now, since I want it all the way to the left, I don't enter any index to the left of my colon sign. And since I want everything to the left of the fourth character, I enter four here. So as you can see, it gives me a r a n and d, which is everything from the fourth character of my string random till the left. Similarly, if I wanted something to the right of it, I would just do it like this. I would enter some index here and I would leave the portion to the right of the colon blank. So this will give me everything including the third character since this is how we treat the uh indexing in Python. Everything including the third character all the way to the right. So this is doom as you can see. Now another perfectly valid um slicing is say I want to slice or take out uh different characters from the string but I want to skip certain characters like I want this one then I want to skip one and I want to take the next one and then I want to skip one. Well in that case we have to introduce a step. So one way of doing this is now how do we read this? So as you can see I have written 0 col 6 col 2. So what this is telling Python is I want every character from my string test uh starting from the zero character up until the sixth but not including the sixth after skipping or taking two at a time. Taking two at a time can also be assumed as uh skipping one at each step or skipping one character at each step. So what this will do is it will go to random. It will take the zeroth one. It'll take r and then it will skip the next one. So it will not consider A and then it will pick N and then it will not consider D and then it will consider O up until the sixth character whichever that may whichever that may be. And if you see the output it gives us R N O which is exactly what we wanted. We wanted R we wanted N we wanted O where we skipped uh A and D or we skipped the every second character essentially. So this is a way of um slicing strings while also skipping certain characters or skipping characters in steps. Now Python is also an uh there's another interesting technique that we can use in Python to uh where we can start retrieving characters from the rightmost side of uh a sequence or a string. Uh we can use negative indexes. So if I were to for example show you let's see what this what this gives me. test what what is a minus1 index? It gives me the character m. Now remember a string is random. So minus1 gave us the last character of the string random which was m. So this is a way of using negative indexes. And similarly if I used test minus2 it would give me the second last character which should be the character o as you can see here. Now there are some other important functions that uh you may use in python. So for that let's create another let's create another string. Let's call it upper lower. All right. Now I'm storing it again in the same variable test. Uh let's see what happens when I use the upper function and what happens when I use the lower function. Now I'm going to use the lower function for this. So these two functions will respectively give or give us the result or give us our string in uppercase and lower case respectively. So as you can see the upper function created the first string in full uppercase and the second one created this the string in full lower case. Now another important and probably one of the most important methods that we use for strings especially when it comes to data science is the split function. So let's see what I mean by the split function. Let me create another string. Let's call it hello world. All right. And I'm going to use a split function and I'm going to see what happens when I use this function essentially. So what I'm doing in these in this these three lines is I'm creating the string hello world. I am storing it in test and then I am using the split function on the test and whatever result I get I'm storing another variable called sliced and then I'm just going to show you what is in sliced. As you can see, what's happened to the string hello world is that I have broken it into its components or its component words, if that makes any sense, into hello and world and I've stored both of these into what we call a list. Now, you might ask, well, how does Python know that it has to separate at a blank space? Well, the split function by default will break up a string at its blank spaces. But if we wanted, we could break it up using or at every comma. So for example if I wrote hello world using comma and if I put a comma inside over here and now if I try to execute this like for example I am going to find I'm going to look for commas in the string and then I'm going to try to split it. As you can see there's no splitting happening. I have one list with my original string itself because it's not found a comma. What if I introduced a comma over here? Now let's see what happens. As you can see the splitting has happened. It's found this comma and it's caused a split between hello and world. Similarly, if I in my string, if I put a comma again and I put something else, it should give me the split over there again. So, we can define our split using some whatever we put in between these uh circle brackets after the split function. It's just important to note that by default if in case we do not enter anything over here, it will use blank spaces to do the splitting. uh another very important operation and very common operation in strings is what we call concatenation. So if I have two strings first and say another string second and I want to join these together. Well the operator I would use to do this is called the plus symbol. This is the concatenating operator when it comes to strings. So what is the result of this? Let's see. Let's print this out. Let's print this whole thing and let's see what is the result. As you can see what uh this has done is it has combined the f string first and the string second into one string called first second. So this is the concatenate concatenation operator. Now finally uh the last topic in this lesson is the topic of escape sequences. Now escape sequences are special commands that tell Python to either suppress special meaning of some character or symbol in a string or give or give the special or give some special meaning to an otherwise ordinary character um in a string. So let's look at the first example. Now remember my in my for earlier in this video where I was I was telling you like uh what what is the way of writing or how do we introduce single quotes or double quotes into a string and I had told you the trick if I want to use a single quote in my string uh I just close it using double quotes or if I want to use a double quote I close it using single well another way of doing this is using what we call an escape sequence and an escape sequence in Python is the backlash character. All right. So let's see what happens when I use the backslash character where I want to introduce my single quote. So if I use my previous example where I wanted to introduce a single quote and say uh I'm going to close it using single quotes as uh as well. So I'm going to go back to my error example so as to speak. Now this would throw me an error right? As you can see it has thrown me an error. So what if I used the escape sequence that I mentioned the escape sequence is just back slash character. Now this is the point of the string where I want my my single quote. So I introduce a backslash. Now I have my single quote and I continue writing the rest of my statement. Let's close the string. Now will this work? As you can see this has worked. So what this has done is this backslash character has told Python to not treat this uh special character or the single code to not treat it specially and let it be as a part of the string. So as you can see that's what's happened here. Now the next case or the other uh use case of escape sequences is when we want to give a particular character that is otherwise just ordinary some special meaning. So um let's take the example of the character T and the character N. We use the character T to introduce tab space. Um and we use the character N to introduce what we call line break. Well let's see by an example. So normally the character T has no special function. So the character t within a string will just uh be outputed as the character t itself. However, as you can see here, I'm going to introduce a backslash before this character t. Let's see what it does. It introduces this thing that we called a tab space, which is the larger than normal space. Similarly, if I were to use the uh back slashn combination, this would introduce what we call a line break. And if you were to see the output of this, you can see at the point of the string where I have this combination of back slashn, a line break has occurred. So, uh there are plenty of other escape sequences that you may choose to learn. And u these are just two of the probably the most common ones that are used. In this video, I will give you an introduction into lists and pupils. Starting off with lists. Lists are a collection of ordered or bit arbitrary objects that are similar to what we call an array in other programming languages. But lists are way more flexible when it comes to Python. Python lists are a type of sequence. And we might have you might have seen what a sequence is when we were learning about strings. And much like in sequences or any other sequence, list objects are assigned a numerical index. And whenever we want to manipulate an object in a list, we use its respective index. How do we define lists? The lists are defined using square brackets. So this is a simple way or a simple list that I have just created and assigned to a variable A which contains three objects 1 2 and three. Let's talk about some of the features of Python lists. Python lists are ordered. And what this means is that even if two lists contain the exact same elements, if they if the elements are placed in a different order, the two lists are actually unique and different. And I can check that over here. I have created a list 1 2 and three. And say if I create another list two and one and three, the same elements but in different order. I will use the is function to check whether these two lists are the same. And if they are the same when I use is function it should give me the value or the answer true. And if they're not the same it should give me the value false. And as you can see I get the value false because even though I have two lists with the same elements they are in different orders and so they are different. And that's what we mean by lists are ordered. The next property for lists are that they can be manipulated using their indexes or the elements and the objects inside a list can be manipulated using the indexes and we will see that in the section where I show you list operations. The next property is lists are mutable. What this means is that once we create a list object, we can actually make changes and modify them without having to create a new list object. The next property is that lists are dynamic. And what this means is that as we are making or as we are programming, we can actually make changes to an already created list as we are programming without having to go through a compilation stage. So if we had to say increase the or we had to add certain elements to an already created list, that original list grows in size to accommodate for these new elements. The next property is that lists can uh contain any arbitrary objects. So if I created a list right now where say if I want to store some numbers say some floatingoint numbers now and I can put strings as well into my list. All of these are valid uh when it comes to objects that can be put inside a list. We have no such restrictions. So we can put any arbitrary objects inside our list. The final property that I want to talk about is that lists are uh they can be nested to any arbitrary depth. What this means is that I can put a list within a list and I can keep nesting it essentially by putting as many lists I want within my list and I'm only essentially restricted by my systems memory. So if I created a list like this and within this say I put another list and within this list I put another list I can keep going on to by keep going on doing this and I'm only restricted by the memory of my system that I'm using. So moving on let's move on and talk about some important list operations that we will be doing when it comes to lists. So let's create a basic list called a where I'm storing six numbers. So now say if I wanted to retrieve a particular element, say if I wanted to find out what's on the index three, this is how I would do it. If I wanted a slice of my original list, and much like strings, I do it the same way. So what this does is it tells Python that I want all the elements of list A from index one to index 4, but not including index 4. So it'll give me 1, two, and three index uh objects. So it should give me 2, three and four in this case. As you can see here 2, three and four. I can do the same thing by considering elements say two at a time. If I wanted to consider elements or consider every second element, what I would do is this syntax I would use this syntax. And when I give this command to Python, what it tells, what it understands is I want every element from a from index one to index 4 but not including index 4. But I am going to consider two uh every second uh uh element essentially. So what it will do is it will go here and then it will skip one and then go here and then skip one and that's essentially what we uh what it's essentially what we call slicing by using strides. So I'm taking strides. I'm not considering elements consecutively, but I'm skipping certain elements in between. If I wanted to consider every third element, I would change this to three. And fourth element, I would change this to four. All right? So let's see what happens when I take every second element, it should give it should give me uh it will consider say from one to index three, right? So it should consider essentially this section. But since it's going to skip one, it's going to consider this, skip this, and then consider this. It should give me two and four. As you can see, it has given me two and four. And like in other uh sequences, we can use negative indexes as well. Negative indexing essentially starts looking from the right hand side of the list. So when I say a minus one, it's essentially looking at the last element of the list. So it gives me the sixth or the last element which is the which is the object six. If I did a minus 2, it gives me the second last element. Similarly, another neat trick that we can use for sequences is the following syntax. Double colon and we give a minus one. What this tells the what this tells the system is I want the list A but with all the elements reversed. So as you can see I get my element my list A but the elements have been reversed. Right now let's move on to uh concatenation and replication. Well concatenation much like in strings is done by using the plus sign. So as we know on either side of the plus sign should be two similar objects. So if I'm doing a plus something a being a list on the other side it should be a list as well. So let's add a list like this. Now let's see what's in a as you can see my original list has 7 8 9 added to it. And I can also add things to the beginning of a list similarly. So if I wanted to add say zero, you know, at the beginning of a list, I just do it like this and I get the element zero added to it. It's very important to note that I I I actually added zero within a list because if I did zero like this, it would throw me an error because like this zero is just an int object and it's not a list. But when I put the square brackets, I make it a list and then this concatenation becomes valid because a is also a list. Similarly, if I did a star 2, this is essentially replication or a replication function where I'm repeating the values in a twice. If I did a start three, it's the same function, but now I'll have the elements repeated thrice as you can see here. Let's move on to modifying list values. uh this also shows the mutable property of lists that I had mentioned earlier. So let's consider this example. So I created a list with elements where there's the three strings eggs, apples and carrots and I have uh five int objects 1 2 3 4 and five. Now if I wanted to change say the first element all I have to do is take the zero with index and say if I want to replace eggs with say mango this is how I would do it. Now I've replaced eggs with mango. If I check what's in B you can see the eggs have been replaced with the element mango. Similarly I can use the slice syntax to replace uh a particular slice from my list with uh a slice essentially. So let's look at this. Um I'm replacing I want to replace the first three elements. I want to replace mango. I want to replace apple and carrot. So what I will do is I will consider this since this carrot is the second index. So I will consider 0 to three because third is not counted. Now I'm going to replace this with three different um let's see kv and say sponge. Now let's check what's in V. You can see my three elements have been replaced. In fact, I don't have to replace like for like. If I want to replace say three elements with only one or two elements, that is also possible. So now, if I want to replace the first three elements, KV, orange, and sponge with um just one element. This is how I'll do it. Let's say wine. Now, let's check what's here. As you can see, three elements have been replaced with one wine. I can do it vice versa as well. I can replace smaller number of elements with a larger number of elements. But here there is a slight caveat that I want to show you. So an obvious way of doing this would be say if I wanted to replace the first element wine with three different elements. An obvious way would be just consider that that element's index and then put whatever objects you want. Say I want to add instead of wine I want to add beer. I want to add ball and say club. Yeah. Well, let's see what happens. Now, if I say B, check what's inside my Now, you can see my elements have been added, but they seem to have been added as is. You know, uh I had entered a list over here and that whole list has been replaced uh or this wine has been replaced by this whole list in the first position. But that's not exactly what I wanted. I wanted the elements within this list to be individually placed into my list. And the way to do that is not this syntax but actually what I'm going to show you now. If I want to replace one, if I want to replace the index or the object at index zero, I would give this statement a slice statement where I will give 0 colon 1. So when I do this and if I um so let's create my old scenario again where I had one and I had 1 2 3 4 and 5. Now if I do this B 0 but I give a slice and this slice is essentially a single object slice it's essentially the zero index again but because of this uh formatting or this this type of syntax that I'm using what Python will do is now when I say beer and ball and club now what Python will do is it will not consider this whole list and add it to in place of uh wine or whatever I want to replace it will actually consider the elements inside of this individually and then replace. So if you see here now if you see my list I actually have the uh I've removed wine and in place of it I've put beer ball and club but these three are actually individual objects within my list and not objects within a list that is then placed within my original list. So this is an important caveat that uh you should take note of. So moving on, let's talk about certain methods associated to lists. Let's take a simple list. Now 1 2 and three. The first operation that I want to show you or the first method is the append method. So when I do a append and say if I want to append the list by one object called four. I just do it like this. Now let's see what's a I put. So as you can see the append has put the object four into my original list. Now say if I wanted to append three different objects. I want to append five, six and seven. Now let's see what append function does. Oh no. As you can see, it's done a similar operation to what we have seen earlier when we gave a single index or this single element replacement uh that we saw earlier. It essentially put this whole list into this particular spot into the list. And this is not what I wanted. I wanted five, six and seven to be individually placed. And we do have a way of doing that. And the way to do that is the extend function. That is what I'll show you next. Now if I want to extend say the original list by three different elements 8 9 10. When I use the extend function, it will not put this whole thing. It will consider each object inside this thing individually and then add it to a. So if you see my result, this is exactly what I wanted. Now the next method that I want to discuss is the remove method. So the remove method is uh what you how you use it is within the parenthesis you put the object that is present in the list that you want removed. So you don't give an index over here. You give the object itself. So if I want the object 8 all right this number. If I want 8 itself removed what I will do is put the value 8 here and it will remove it from my list. So if I check my list A, as you can see the value 8 has uh it's gone. Our next another similar function to remove is the pop function. Now the pop function by default will remove the rightmost value from the list. But unlike remove the pop function actually outputs a value uh and that value is what we have popped out of the list. So by default it will pop out the right route rightmost value of a list or rightmost element of a list and in this case that element is 10. So it should give me 10. But of course if we want to pop out any particular object in this case we do not give that particular object we give its index. So now say if I want to remove this uh this list object from my bigger list. Um as I can see it's in index 0 1 2 3 4th. It's the fourth index. So if I want to remove the list which is at the fourth index. I give the index value inside the parenthesis and it pops that out for me as you can see over here. So this was it for lists. Let's move on to pupils next. So pupils are another ordered collection of objects much like lists but they differ from lists in two key properties. First of all uh pupils are defined using parentheses instead of square brackets. So this is how I would define a pupil with elements 1 2 and three. And the next very important property that is different or that uh differentiates pupils from lists is that pupils are immutable. And what this means is that once we create an object of type pupil, we cannot make any modifications to it. We would have to only create a new object if we wanted something different. And this is obviously a key difference between a pupil and a list. But apart from this uh every operation that we have done on lists are actually possible to do in the case of pupils as well. So any operation that we did to lists that did not modify the lists or that does not do any modification we can actually apply to pupils as well. So if I wanted the first element of this dupal all I have to do is call it by the index zero. It should give me the value one. As you can see if I wanted a slice I can do that as well. It should give me two and three. And if I wanted to reverse my pupil, well, I can use my same neat technique that I've used for lists and that I can use for sequence, I mean strings as well. So it gives me my old pupil but with the elements reversed. So now the natural question is why do we use pupils over lists? Well, one important difference is that when we when we make a program execution where or where we are manipulating a pupil, if we did the same thing for the exact same objects in uh that if if it were present in a list, uh it would be much faster in a pupil. Essentially, program execution is just faster on pupils when compared to an identical list. So we use tupils over list when we want to be efficient while we're creating an ordered list of objects. So whenever we create an ob order ordered list of objects that we don't need to or we don't want to make any further modifications to we don't need to create a list we can create a pupil. And another very important area where we use pupils over list or why we need a pupil instead of a list is uh when we are using dictionaries. dictionaries as we will see later is a data type in Python that needs an immutable component uh while we are creating the dictionary. So if we want to use uh something that is like a list but since lists are mutable we can use a pupil instead and use that when we are creating our dictionary and we will see like what uh what I mean by uh the the immutable component for a dictionary when we actually uh learn about dictionaries but yeah these are some of the places where we would use a pupil over a list. uh a brief section on pupil unpacking and packing is what I would be doing right now and what that means is that when I create when I give this sort of a statement you can assume this to be me packing this pupil into a object or a variable t. Now what's very interesting is and this is something that's very key to pupils is that I can unpack a pupil onto another pupil. So let me show you what I mean by that. So now that I have packed 1 2 3 and four pupil into T. Let's unpack it into another pupil. Let's call this let's uh put these elements into this pupil and I put this into T. All right. Now let's see what I mean by unpacking something onto something. Let's see what's the what what what happens when I put A. If I just want to see what's in A, it actually gives me the value one. If I see what's in B, it gives me the value two. If I see what's in three, give me value three and D gives me value four. So essentially if you think of it what I have done is I have matched this pupil then put it into T and then put that pupil T into this pupil A, B and C D. So what it's what what has happened essentially is the element A has been or better way to say this would be the element one has been packed onto or unpacked onto A. Two has been unpacked on to B. Three has been unpacked onto C. Four has been unpacked onto D. To show you what I've done in a single line is essentially this. This is essentially what I've done. I have assigned one to A, two to B, 3 to C, four to D. I have matched these two pupils together like this. And this is a very neat feature of uh pupils. The only only uh important thing that you have to keep in mind is the elements of this pupil and the elements of this pupil should be equal. So if there are four elements here, there should be four elements here. All right. And in fact, another very important thing about or a neat feature is that we don't really need to mention the parenthesis when we're doing this uh unpacking or packing. So if I did x, y, z. If I did it like this and if I put say five and six and seven, this is also a valid way of packing and unpacking. So now if I see what's the value in X, it is actually five. If I see what's the value in Y, it is six. If I see what is the value of X and Z, it should give me a pupil containing five and seven as you can see over here. So this is a neat feature of pupils called packing and unpacking. In this video we will cover concepts surrounding dictionaries. Dictionaries are another type of composite data type in Python that are also a collection of objects much like pupils and lists and like lists they are also mutable, dynamic and can be nested. However, a key difference and property of dictionaries is that they are unordered. Items in dictionaries are paired using keys unlike in list and pupils where the objects are assigned numerical indexes. To define a dictionary, we use curly braces. Each key value pair in this dictionary in a dictionary is separated using commas and the key and values themselves in a pair are separated using a colon. Let's look at an example. This is a dictionary where I have three key value pairs. As you can see, they have been separated by colons and each of the pair themselves are separated by commas and these are enclosed in curly braces to define the whole dictionary. In this uh example, these are the keys. banana, apple and grape and their respective values are yellow, red and green. So this is very important that in this particular uh data type we have what we call a key and that key is assigned a value and we can have multiple such pairings and we have three such pairs in this particular example. Another example or the or another type of way to define a dictionary is using the dict function. This is how we use it. As you can see, I have inputed two value or two object pupils. This is a pupil. This is a pupil. And this is a pupil. And when I use it in a dict function, uh when I place it within a list, what Python will do is it will consider the first element of each pupil as a key and the second element of each pupil as its respective value. Now, if I check what's in D, as you can see, the key banana has a value yellow. The key apple has a value red. The key grape has a value green. So this is another way of defining dictionaries. An important note over here is that the the objects that we use for keys have to be of an immutable data type. So we cannot use for example lists uh as an example of keys. But we can use say integers or strings for our dictionary keys. Also keys have to be unique when it comes to a dictionary. You cannot have two different keys with uh or should I say the same key having two different values. You can have only one key and that one key will have a unique value assigned to it. Duplicate keys are not allowed. However, when we speak about values, we have no such restrictions. You can have a mutable or an immutable data type as a value in a dictionary and you can have duplicates as well. So if I created a dictionary with a key say banana and if I gave if I associated with it a value green, I cannot create another key banana and associate a value yellow. Let's see what happens when I actually do this. If you actually see what has happened here is Python has considered the second case. So it's it first looks at the first pair. It goes, okay, so my dictionary has a key banana with a value green. But right after it, it sees another key banana with a value yellow. So it overrides my first key banana with the second key. And it overrides the first value green with the second value. And we essentially get one key banana with the second value yellow. So obviously this is Python does this to avoid duplicacies. However, if I created a dictionary with a key banana again, if I created I gave it a value yellow or say green and if I created another key and if I gave this also a value green, this is perfectly fine and this will not nothing special will happen in this case because values do not have to follow such rules. Let's speak or let's see certain dictionary oper operations and functions now. So let's uh bring back our original dictionary which we had created earlier with the three pairs. All right. So what if I wanted to we can use keys to uh essentially do these operations and functions. Let's see what happens when we so much like how we use sequence how we use indexes in example of sequences we use keys when it comes to dictionary. So let's see what happens when we use a key over here. So we as we know banana is a key in this dictionary. Let's see what happens when we do when we give this command. It actually gives the value associated with that key which is yellow. What if we wanted to change a particular value associated to a particular key? I would give I would do this. I would give the key and assign it uh the value that I want to change it with. All right. Now if I check my dictionary, you can see the key banana no more has yellow value but the value gold. What if I wanted to add a new pair, you know, a new key with a new value completely. I would do it pretty much the same same way as I did in the previous step. I would um essentially write my new key whatever I wanted. Say if I wanted a new key called watermelon, I would assign it a value red. And now if I check my dictionary at the end of the dictionary you will see that my uh new key pair key value pair has been added watermelon red as you can see over here. Now what if I wanted to remove if I wanted to remove a key value pair from my dictionary I can use the delete function which is uh called by using this the deel function. Now how do I do it? Say if I want to remove the key value apple and red. So all I have to do is do d e l d and within square brackets I have to only mention the key. I don't even need to mention the value just the key. So you can probably draw parallels in how we use indexes when it comes to lists and pupils and even strings and how we use keys when it comes to dictionaries. They are quite similar. So if I did this and if I um oops I think red is actually not a key as you can see red is a value my key is apple actually it's not red it's apple as you can see now if I see my dictionary the key apple and its value red has been removed so these are some of the operations that we can do for dictionaries using keys what if we wanted to use certain inbuilt functions that are associated with dictionaries Well, some of the examples are as follows. There is a get function. When we use a get function, all we have to do is enter a key inside the parenthesis and it will get the value associated with that key. So, if I gave the key grape, let's see what value is associated with it. As you can see, it's green. Uh there is also a function called items that returns the key and value pairs in our dictionary as a list of two object pupils where the first object is the key and the second object is a pupil. So let's see what it does. As you can see, it gave me a uh list and in this list, the uh objects inside the list are two object pupils where the first object is always the key and the second is the value and it's the same for each of the pupils. So, this is what items function does. Let's see what uh the next so the next function is keys. Let's see what the keys function does. As you can see, it returns a list of all the unique keys within our list. I mean within our dictionary. Similarly, we can there is a function for values as well. And as you can see, it returns all of the different values that are present in our dictionary in the form of a list. Also, it's important to note that if we have a particular value that has occurred multiple times in a dictionary, it will appear as many times it has occurred in the dictionary or appeared in the dictionary in our list over here when we use the value function. So if say a particular value say I had another key where its value was green again. So I would have the value green appear in this list once more. Essentially our next function is pop item. What this does is much like the pop function in lists. It gives us the last entered um key value pair that is present in our uh dictionary. So the last entered pair was watermelon key watermelon with the value red and that's what has popped out when we give this function. Another important function associated with dictionaries is the update function. And the update function is a very neat function that is used to merge two dictionaries together. So say if I had a dictionary, let's say I'm creating a dictionary using the dict function where I have say some key. So as I had mentioned I will use two object pupils in this case and um let's say I give this also a value green. All right and another pair would be say mango and let's give it a value yellow. All right. So this is another dictionary that we have created. As you can see over here we have created a dictionary with a key called kiwi with value green and another key mango with a value yellow. And I already have a dictionary D with uh these values. All right. Now, if I want to merge these two dictionaries, I can use the update function. What I do is say if I want to merge D with X or X with D, only the ordering will change and that doesn't really matter when it comes to dictionaries. So if I want to merge D with X, I use D update and then inside it I put my target dictionary. Now when I check what's in D, I have the the the dictionary X and its key value pairs merged inside my original dictionary D. As you can see, I have my original ones here and I have the merged ones over here. So this was the update function. In this uh brief video, I will explain what we call the type hierarchy or the standard type hierarchy in Python. it it's also known as the data model in Python and it probably has other names. So essentially in the what we do or what we speak about in this is uh how the different types that we use in Python are categorized and classified and what are their subtypes. So it's important to note before we continue further that there in future versions of Python there might be new types that are introduced or the existing ones may be modified but it's safe to say that what I will speak about in this video will still form the core of what we call Python standard type hierarchy. So let's begin. So the first type is the none type. Uh it's accessed through the built-in name none. Its truth value is false. It's essentially used to denote um functions or it's it's returned as the value for functions that don't really return anything. So if you have an expression or a function that returns no particular value, you actually get a data type called none. That is how Python interprets it. Uh then there is the ellipses. Uh ellipses is something that you might encounter later. Uh it is accessed through the built-in name ellipses. um it is used to indicate the presence of this particular um syntax this particular symbol or expression uh or syntax in a slice. Its truth value is true. Again, don't worry too much about ellipses right now. Next is numbers. And numbers is something that we are very familiar with. We have seen we've worked with numbers a lot. They are an immutable type. They are created through numeric literal and they are the outputs of arithmetic expressions and built-in arithmetic functions. Its subtypes are inteious, floatingoint numbers and complex numbers. Um within integers you have plain integers that represent numbers in the range uh this number and this number. So this this number is basically the largest positive value that you can represent using 32 bit notation in two's complement and this is the equivalent for the the negative the negative side. Long integers on the other hand are used to represent numbers in an unlimited range that is only subject to the available memory of your system. So these are two types of integers and as we know floatingoint numbers represent machine level double precision floatingoint numbers or numbers with a decimal point and a component after that. Complex numbers are numbers that contain a floatingoint number that represents a real part and a floatingoint number that represents a an imaginary part. So these are the types of numbers. Then we have sequences. Sequences are finite ordered sets indexed by natural numbers. So each element in a sequence has an index and that index is a natural number. The different types of sequences are based on the mutability. Whether the objects can be changed or not is essentially what we mean by mutability, right? Once we create an object, whether we can modify it or not. Uh that is how we differentiate different types of sequences. So there are mutable and there are immutable types. Now the different types of immutable ones are strings. We know what strings are. There are sequences that contain characters. Uni codes is the other immutable sequence. And then there are tupils. Uh pupils are collection of arbitrary Python objects. And uniodes are sequences that contain uni-ode characters. Then we have mutable sequences and uh there is only one currently there is only one built-in uh mutable sequence in Python and that is lists. Uh lists are also again collection of arbitrary Python objects. Finally we have um well not finally but next we have mappings and mappings are again um sets of uh objects but these objects are now indexed using arbitrary index sets. You know the difference between a mapping and a sequence. You could consider um sequences are ordered but mappings are not ordered and the indexes given to the objects in sequences are always numbers. They're natural numbers. Whereas in mappings they are not natural numbers but they are certain arbitrary objects that we can set for ourselves. Again in Python there is only one builtin uh standard mapping type that is the dictionary. dictionaries are they're they represent finite sets of objects indexed by nearly arbitrary values. The only condition is that these indexes must be of an immutable data type. So these uh so what I have mentioned until now are probably the most common data types that we encounter uh or that we have encountered. Now the next types are like callable types such as userdefined functions, userdefined methods, built-in functions, built-in methods, classes and class instances. So again callable types as a whole are referenced using the call function and this is something that we will learn when we start learning about functions when we learn about object-oriented programming uh and methods and functions and such. The other type are modules. So modules are uh module objects or modules essentially are referenced or called using the import function. Again modules can be considered as these large libraries or larger pieces of code or applications that we uh that contain various different methods and functions and objects and classes within them. Um again modules are something that we will think about a little later. Files are data types that are called using the open function. So anytime we and as we will during the course of this uh tutorial we or during the course of this uh this this course essentially we will be working with lot of files these some of these files are text files some of them are comma separated files. So all of these files that we open using uh the function open or some other certain uh specific functions where we create file objects out of these files. These are what we call file data types. Uh finally there are internal types that are used by Python interpreter. Again this is something that uh does not concern us right now but just know that uh the interpreter for Python uses these uh data types for its functioning. In this video I will show you some of the categories of operations that are possible in Python. So let's start off. So the first category of operations are arithmetic operations. And as you can see in this table, there are different types such as addition, subtraction, multiplication. So let's go over them one by one. So a simple addition operation would be like 5 + 6 giving me the value 11. I can do 5 - 3 that's subtraction. Similarly, I can do a multiplication using 5 say 5 into 2. There's division where I use the backslash. So say I did division of 8 / two. Now you might encounter another type of division which is called the floor division where we use a double backslash and what this does is it returns us the largest whole number that is lesser than the result of our division. So the division difference between the float division and the normal floor and uh the floor division or the single and the back double backslash is this say I did 9 / 2 using a single backslash. It should give me as you can see 4.5 right so this is the actual value now let's see what happens when I do 9 double back slash 2 it will give me four which is the largest number lesser than or equal to 4.5 that is also a whole number so this is what we call floor division essentially finally we also have uh if we want to raise something to the power of something so if I want to do five say 6 raised to the power three so I would use double asterisk and this is how I would do it it give me 216 16. As you can see, uh if I wanted remainder between the division between two different numbers, I will use the percentage sign. So if I wanted the remainder of the division between 7 and three, this is how I would do it. I get the answer one as you can see. So the next category of operations are logical operations. The first one is the logical and operation where we use the operator and. All right. So let's see uh what we mean by this. I am creating. So as we know the and operation returns a value true if both the operants are true. So let's actually let's actually do this for an example. Let's give this variable the value true and this the value false. It should be done like this. True false. So now uh think of it like one and zero. So one and zero is obviously zero right? So if I did A and B, I should get the answer false or zero because I'm doing true and false. So it's like doing one and zero. So if you see this, I should get the answer false. If I change this to true as well. Now if I do this, I will get the answer true. As you can see uh the next logical operation is the or operation where well logical or is if either of the operants are true then we should get the uh answer to our or operation as true as well. So in this case obviously if I did or over here it will naturally give us true. If I change this to false it should again give us true and it will only give us false if both are false. As you can see here now it's false. The next operation is the logical not. Uh it's done by using the not operator. So in this case the value that we get is true if the operant is false. So let's take a as we know a is false over here. So if I did not a I should get the answer true. But if I change this to true. Now if I do not a I should get false as you can see. So this was the uh logical operations. Let's move on to the next set of operations that is comparison operations. So there are a bunch of them. Let's go over them one by one. If I want to check if two operants are equal. So if I did x is say equal to 2 and now y is equal to 3. If I want to check if they are equal I will use the double equals to sign. And if um they are equal I will get the value true. If they aren't it I'll get value false. As you can see since x is 2 and y is three. As you can see x and y are not the same. So there's false. they are not equal. If I change this also to two now I will get the answer to another the next comparison is the uh not equal to or where we use the exclamation sign. So over here this is this um the result of this is true if x and y are not actually it's the same. So I should get false over here as you can see. But if I change this to say five now I will get true because x is not equal to y in this case. The next one is greater than. So if I did x greater than y as we can see 2 is not greater than five I will get false over here. Similarly there's a lesser than if I did x lesser than y I should get true over here. As you can see there's also a greater than or equal to and lesser than or equal to we simply add the equal to sign here. So let's see now if I say x is lesser than or equal to three I should get true because x is two I and if I put two here I should also get true. But what if I put one here? Since x is equal to 2 and 2 is not lesser than equal to 1, this is a false statement. And I can do the same thing with like this using the greater than equal to. Now it's true because 2 is actually greater than equal to 1. And so this was comparison operations. The next set of operations that we can do in Python are assignment operations. We've already seen a whole bunch. We've already been assigning values to variables. We've been doing assignments throughout already, but let's go over them and see some of the assignment operations. So, as we already know, the equal to operation is assigning everything that is on the or the value on the right hand side of the equal to sign to what is on the left. So, if I did a is equal to 5 + 6, it will assign the value 5 + 6 or 11 to a. Let's check what's in a. As you can see, it is 11. There is a plus equal to sign. And what this does is so for example if I did uh say a now since a is 11 let's create another variable called b and call this five. All right so now if I did a plus equal to b. What this will do is it will add b to a and then store it in a. So let's see what this does once I do this. Now let's check what's in a. It should be 5 + 11 as you can see 16. So this a plus so this plus equal to sign is essentially the same as doing a is equal to a + b. It's essentially the same thing. They're both the same. Uh there's a similar thing for this the negative symb or subtraction as well. Minus equal to so now that a is 16 and b is still five. Of course if I did minus equal to b. Now I'm subtracting the value five from 16. So I should get a is equal to 11 again as you can see here. And this is the equivalent of doing a minus b. A is equal to A minus B. I mean as you can see so uh the next similar like assignment operation let's create A is equal to say 6 in this case and say B is equal to I will say two. Right? Now if I do a star star equal to B. What this does is it multiplies B to A and then assigns it back to A. So if I see what's in A now it should be 12 as you can see here. Similarly there is similar thing for subtraction as well. So if I did a is equal to 6 and b is equal to 2 and if I did a sorry for division. So if I did a division symbol and the equal to sign and then if I give b what it will do is it will divide a by b or 6x2 and it will assign that back to a. So my value in a should be three. As you can see here 3.0 obviously this is a single uh single backslash. So this is a float division. The resultant of a single backslash is always a float. I can do the same thing with a double backslash. So let's see if I did a is equal to 7 now and then b is equal to three. Now if I did a double backslash equal to b. Now it will do the same division but uh it will do a floor function. So it should give me two in this case. If I did the same thing with a single backslash just to show you the difference between single backslash and double back slash division. In this case it will give me um the value around 2.3 or something a float basically 2.33 as you can see here. Then there is the modulus you can use the modulus sign as well with this assignment. Um say a is equal to say five. Now b is equal to 2. Now if I do a percentage symbol equal to two I mean the it should give me it will assign the uh it will take the modulus in both sides and then assign it to the leftand upper end. Now let's see what's in a as you can see it it did a divided by two. The modulus symbol is basically the remainder of the division between two objects. So in this case it will divide a by b and then give me the remainder and then put the remainder back in a. And as you can see that the remainder of 5 / 2 is 1 and we get that over here. Similarly, we can also do exponential. So if I have a is equal to say 5, b is equal to 2 and I want to raise a by if I want to raise the value of a by the value of b and then store that back in a this is how I would do it. So it should be 5 to the^ 2 which is 25 as you can see here. So this is assignment operations. So finally we move on to uh two special sets of operators. One is the identity set identity operators and the other is the membership operators. Uh let's look at the identity operators. So when we talk about identity operators what we when what we mean is the is and the is not operator. So what the is operator does is it checks whether two operants uh have the same location in the memory. Essentially, this does not mean that it does not check for whether two objects have the same value. And this is very important. So let's look at this. If a is equal to 1,1 and b is equal to 1,00 + 1. All right. If I did a is equal to two, this is an equality check. Are they the same values? True. Now if I say a is b, let's see what happens. As you can see, it's false. This is because this object and this object are not the same. And this is what the is operation does. Uh we can confirm this by checking the id of a. The id of a gives us some value like this. And the id of b gives us as you can see a different value. So the two objects are not located in the same place in the memory as we have seen using the id function and that is what we check uh through the is operator and the is not is essentially just the opposite of that. So so that is the identity operator. Now the membership operator is used for checking whether something is present in a sequence or not. So let's create a sequence. I will create a list to show in this example. My list is 1 2 and three. Basically simple list with three integer objects 1 2 and three. I will check whether now say 2 in a. So is 2 in a. If it is it will give me true. As you can see is five in a it should give me false because five is not in a. And the not in operator is just the opposite of that. It checks the other condition essentially. So this is the membership operators in Python. In this video we will be looking at the math module in Python and we will look at some of the math functions that are associated with this module in Python. So starting off what is this math module? Well uh the Python math module is an important library or a feature that is uh designed to deal with mathematical operations. This math module is part of the basic Python installation. So you don't have to install this separately. Uh to use this math module, all you have to do is import this uh module. And this is how you would do it. You just do import math and this will import your math module for you to use in your programming. And in this in the course of this video, I will be showing you certain um concepts surrounding the math module such as what are the constants that come with the math module, some of the arithmetic functions, the logarithmetic functions, um power functions and even trigonometric functions that we can do using this math module and the functions inside the math module. So starting off, let's uh look at some of the constants that are part of this. So as you can see the standard constants that are available uh in this module are pi there is toao there is ulers's number n which is a special it's a it's a programming specific concept and infinity. Uh so let's look at these one by one. So starting off our first constant is pi. Um pi is as you know in maths it's the ratio of the of a circle's circumference to its diameter. And uh pi is an irrational number. Uh you know the value of pi is 3.14 something. Since it's an irrational number, it does not we cannot uh define it in terms of a specific fraction. But it can be approximated to the fraction 22x7. So how do we access this pi constant is as simple as this. Once you have imported maths uh the maths module, all you have to do is do maths.py and it will give you the value as you can see. So as you can see the value returned to us is a float. This is important to note. And the number of number of decimal digits uh the number of digits after the decimal point that we can see here are 15. And this is like a by default uh thing for Python that if you call the pi constant it will give you the value of pi correct up to 15 decimal points. So an example of where we can use this thing is say we have the radius of a circle three. It can be in any units 3 m or whatever you want. Um and you want to find out its say circumference. So as we know the circumference of a circle is 2 pi r. So if I had to find the uh circumference of a circle in my in my code for some application with the radius 3 or all I would have to do is use the formula 2 pi r and uh to call the value 2 pi r instead of specifically writing 3.14 something I can just use this function math.py pi over here and then I can enter the r as you can see it will return me the radius as you can see. So moving on the next constant that we will talk about is toao toao is similar to pi in fact toao is the ratio of a circle circumference to its radius. pi is the ratio of a circle circumference to its diameter and toao is the ratio of a circle's circumference to its radius. It's important to note this difference. So because of this definition toao is always two times the value of pi this is by definition and much like pi if you call the toao constant in you from the math module it will also return you a float number and you find that this is the function that we use or this is the syntax. So as you can see this is this is actually two times the value of pi 6.283 283 and even this is returned correct up to 15 places to the right of the decimal point and this is by default. Uh next constant is the uler's number. Uh the uler's number is a it's one of the most widespread constants used in the world of sc uh in the scientific community in the mathematical community. Um you will be using the uler's number probably a lot as if if you move into data science or AI or something like that. So what is the uler's number? It is uh it is the base of the natural logarithm. If you know what the natural logarithm is, it is used to calculate rates of growth or rates of decay. And as with numbers or constants like pi and toao, even the ulers's number is an irrational number with infinite decimal places. The value of the ulers's number in short, it's denoted by the small letter e. So this value of e is or usually approximated to around 2.718. So how do we call this uh constant is simply as math e. This is how we do it. So you get this value. Again this is also a float and it's correct up to 15 decimal places. So the next concept that we will be talking about is infinity. Now infinity is a it's more of a mathematical concept and this is very important to note because infinity doesn't is not really a number. There is no number called infinity. It is it is used to define something that is infinitely large or infinitely small. So positive infinity is something that is higher than any known number to us and negative infinity is lower than any known number to us. Um so this is the concept of infinity and how do we call this uh function? Well so let's see what happens when we call the math function math constant infinity. So this is how we do it and as you can see it will return us this um it might look like a string to you but this is actually a special type in Python um which is not a string this is also a float it's a type of a float in fact this uh infinity value it's it's a it's like a special data type it's a special data type that you can you can consider as an equivalent to a float but um it's not it's not a typical float that we know like you know 3.14 or 2.71 the value of math int will give us inf and this is this inf is actually not a string so we can we can confirm that over here as you can see when I do a type when I check the type you see it's actually a float in fact this was recently added in python 3.5 a special type known as math inf um it returns this INF data type which is the equivalent float uh so this is this is how we use infinity if you wanted to get the uh value of say minus infinity all you would have to do is math do inf with a minus sign preceding this uh constant and you will get minus I inf now let's check if the concepts or the properties of infinity are maintained you know like every a positive infinity is greater than any number that we know and negative infinity is greater than any number that we know so let's just confirm this um so I will take math in and I will do a check is this greater than say some really large number something that comes off the top of my head immediately. Okay. So let's see if it's if this is true then uh this is conceptually correct and as you can see it is true. In fact I can change this number to anything as large as I want. This will be true because infinity is true is larger than any number that we know. Uh and similarly if I did minusmath inf and I checked some very small number you know a very very small negative number. So just consider some minus uh 10 ^ something right this should also be true uh sorry this should be smaller sign symbol this should also be true because I'm checking minus infinity is smaller than any known number to us and it is true as you can see so it's very uh also if you did if you did something like so let's say if we do uh let's add something to this you know it's a number so we should be able to do arithmetic on this let's actually see what happens when I add say 10 to positive infinity it actually gives us the same value back infinity because intuitively we know that infinity plus anything is still going to be infinity right and this is exactly what Python does for us even if we add a number to this the math inf or the infinity constant in Python it will still give us uh the same value the same data type of INF or infinity similarly if we subtracted something from the negative of math inf say minus say again let's say minus 100. So if you subtract something from minus infinity, we're technically still minus infinity itself. So this will again give us minus and minus inf as you can see and this is absolutely what we need and this is a very very interesting feature in Python. Uh finally the last constant that we will discuss is the nan or the not a number constant. So this is not really a mathematical constant. Uh there is no constant in maths called not a number. This is a computer science related concept that it's it's used to it's used to denote invalid inputs or in it's used to indicate a certain places where say a variable that should have a number um it's been corrupted for whatever reason and it it now has a text or a symbol and our code throws an error then in these in these cases where Python expects a number from us or from your code and it does not get it, it will throw you a nan um value and this is usually used in cases where you have errors in your code. So again just to check like what this this constant like how to call it this is how you actually do it. math nan and it will give you a data type called nan. Also much like the infinity constant, let's see what's the type of this um this constant. Again, might look like a string to you, but of course this is not a string. This is a a numerical concept. And as you can see, it's still a float. So this is like um it's another special uh data type that was created for specifically this case where it's some it's the equivalent of a float, but um it's not really the typical float that we are that we associate with. So this is uh constants in the math module. So our next topic will be arithmetic functions that we can do in the uh using the math module. So some of the common arithmetic functions that are available to us are factorials, the ceiling operation, floor operation. Uh there's permutations, there's combinations, you can find the greatest common divisor or the GCD and the LCM or the least common multiple and you can truncate numbers. So let's look at them one by one. So starting off with the factorial operation. The factorial operation is done using the factorial function as you can see here. Actually it's defined like this math. Factorial. All right. So now what we pass in between the parentheses will be uh what we calculate the factorial for. As you know factorial is so factorial is always for a positive integer and the factorial for say a number 10 would mean 10 * 9 * 8 * 7 so on until the number one. Obviously factors are only defined for positive integers. Uh it is not defined for negative values or it's and it's also not defined for decimal values. So let's leave let's look at an example. So as we know um so let's say we pass math factorial for six and let's see what's the value. So this is exactly what we expect. So we expect the value of 6 * 5 * 4 * 3 * 2 uh that is actually 720. Let's see what happens when we pass zero. This should give us one since we know 0 factorial is one. Now let's see an error case where we pass say minus5. As you can see this will throw us an error because factorials are not defined for negative numbers. Now moving on let's uh so the next function that we'll talk about is the ceiling function. So the ceiling function is defined using math do seal as you see here. So again we pass the number that we want the ceiling for in between the parenthesis. So what is the ceiling function? It gives you the largest uh sorry excuse me it gives you the smallest number that is uh greater than or equal to what we pass over here. So for an example if I gave you 6.6 and I asked you okay what is the ceiling function for 6.6 it should give you 7 because 7 is the largest inte integral value that is greater than 6.6 or equal to it. Obviously if you give the ceiling function for an integer it will give you the integer itself. So let's look at an example 7.8. So this should give me the value 8 as you can see here. Let's look at another negative number. So -2.3 uh an integer that is greater than -2.3 is -2. The smallest integer that is greater than - 12.3. And this should give you 12 as you can as you can see here. Uh a similar function is the floor function which is defined like this. Again the floor function what it does it gives you the greatest number that is smaller than whatever value we pass between the parenthesis. So if I gave it 2.3 it should give me the value two. Um again if you pass integers it will uh just give us the integer itself. Again just to reiterate this uh ceiling and floor functions uh they give you inte integer values. So the floor function will give you the largest integer that is smaller than what we pass over here. So let's look at 6.7. So the largest integer that is smaller than 6.7 is six. And as you can see this is our answer over here. Uh the next function that we will look at is the truncate function. U the truncate function is defined using this. Truncate is very similar to what we have seen. This is how we define the truncate function. Uh it is very similar to the ceiling and floor. uh in the truncate function what we do is um we pass a decimal value a float number to the between the parenthesis and what we get back is the number without the decimal component so only the integral component so if I gave it 7.8 8 for example it will give me only the seven part it will return me only the seven part and it will chop away the8 part um if I gave it minus 12.3 it will chop away the.3 part and give me only -12 back so let's look at an example 8.9 it should give me only 8 as you can see here say I did minus 8 uh or minus 6.9 it should give me minus 6 so you can think of truncate function as it does the floor function for positive positive values and it does the ceiling function for negative values because it rounds down and rounds up respectively for positive and negative values. So now let's look at the next set of arithmetic functions. Um let's move on to permutations. Permutations are defined using this as you know. So in between the parenthesis I have to pass two values n and k. So I have to pass n and a k. The k has to be less than or equal to n because the permutation as you know is uh the number of ways in which we can permute from n objects k different objects. Uh so what this mathematically what this is is um permutation of n and k is n factorial divided n minus k factorial. So if I pass say 10 and 6 it will give me the permutation of 10 and 6. So let's look at what's the value. As you can see it's uh this very large value 15 15 1,200. Similarly, there's a combination function and this is uh combinations of permutations are used a lot in probability. So if you're going to move into that sphere which you will encounter a lot in um machine learning AI, you will be using these functions quite a bit. Um so the combination function again you pass an n and a k. Again the n should be greater than or equal to this k. What this evaluates is n factorial divided by n minus k factorial * k factorial. So again this is a mathematical concept. So let's look at the permut sorry the combination of 10 and six. So as you can see it gives me 210 which is the combination of 10 and seconds in the arithmetic function section. Um are gcd and lcm as we know gcd is the greatest common divisor. LCM is least common multiple. So as of the newest version of Python in 3 Python 3.9, the math module gets these two new functions that were very lately added actually um since uh I cannot show you that because I don't have the absolute latest version of Python on my system. But using the math.gcd function, this is a function and in between this we would what we would do if we had the latest version is we would pass two numbers or as many numbers in fact and what this function would do is find the common divisor amongst them. So if I gave these say for example if I gave these numbers it would give me the value 10. Similarly the LCM function again added in Python 3.9 finds the common multiple amongst what whatever we pass over here. So if I passed say 20 and 30 this will give me uh the value of 60 which is the LSM of 20 and 30. So again these are it's something neat that if say you have Python 3.9 or uh if you have Python 3.9 or after that you will have access to these functions as well if you when you import the math module. So that was for the arithmetic uh functions. Let's move on to the next section which are power and logarithmic logarithmic functions. So let's start off with the power function. Um there are two there are multiple ways you can actually do a power function in math uh in Python. So you can do something like this which is the equivalent of 2 ^ 3 or you can use the inbuilt uh module math and you can use the p or the power function which is this is how you do it and uh if I had to do same 2 ^ 3 I would just pass 2 and three as arguments like this and it should give me the value as you can see eight over here what if so there's actually one more function which is simply the p function and this is not part of the math module it's a function function not part of the math module but it's still part of the base Python use this as well and you can do the same thing p 2 and 3 it should give you the value 8 again the now you might ask what's the difference between these two functions well it's not much for at least in a beginner standpoint it's not that much uh just note that the math power function is slightly computationally faster than the simple power function so yeah moving on there next topic would be the next function that we'll discuss is the exponential function fun which is defined like this again exponential function is again e to the power something so if I did exponential of three this would give me e to the power 3 as you can see oh of course I did not define the module beforehand so math exp now when I do this it should it'll give me some value which is basically uler's number e to the^ 3 so this was the exponential function and let's look at uh an example so we will look at the example of rate of decay and we will use the uh this function in in that example. So the exponential rate of decay um uh for is used to calculate the amount of radioactive elements that is present in a substance after a certain amount of time. So the the the the expression is uh something like the initial amount of radioactive elements that were present right at the beginning times the exponent of - 693 * t t for time divided by the time period the halflife time. Uh so let's just write it down to make it simple. So again as you can see the uh so this will give me the remaining amount of radioactive elements right. So let's uh let's write down remaining let's assign this to a variable remaining. So the initial again um initial is the initial amount of radioactive elements. So let's give it some value say 100. um the time let's assume it's 10 years uh and let's say the halflife um halflife is around I don't know let's give some values say three or something again this is uh the details are not important so this will uh I'm using the math exponential function over here as you can see and for these values uh now when I check what's the value of remaining after I calculated it it's something uh so this is it's basically what this tells me is that I started off with 100 units of some radioactive element um and in 10 years this is what is left. So yeah so this is how we would use say the exponential functions in a particular example of ours. So next type of again functions are logarithmic functions. So let's look at some of the logarithmic functions that are part of the math module. So basic log function would be written like this math.log in between this I would pass the number that I want the log for. By default, if I just pass one number, it will it will calculate the log of 8 to the base of e. As we know, e is a uler's number. So let's see what it does in this case. Gives me some value. What if I wanted to specify a particular base? Well, I would pass a second argument in this case. I want to find the log of a to the base 2. This should be the value three. As you can see over here, uh there are two specific functions that are also provided in this math module that is log two. What this will do is again it will again I have to just pass one argument and this will automatically find the value the log of 8 to the base 2 as you can see here. Uh similarly there is a math.log 10 function. If I gave if I did this I will find the value of uh something here to the base 10 as you can see. So this was logarithmic functions. Then you have the finally you have the square root function. So how would we use the math uh the square root function is math.sqr. sqrt again we pass what we want over here. If you want the square root of say 49 this should give me the value 7 as you can see. Finally we move on to the final set of functions that we can do using this math module. Uh they are trigonometric functions and they are hyperbolic functions. So uh let's start off with some of the trigonometric functions. As we know um we can do s we can do cosine and tan. uh some of the basic functions. Um the way we would do it is math dot sign. Um in between this what we this is to calculate the sign of a particular uh angle and this angle must be passed in radians. Uh this is very important to note we do not pass it in degrees but in radians. So if I say pass sine of 3.14 as we know 3.14 is an approximation of the value pi. uh sin pi is uh supposed to be uh in fact it's supposed to be zero. So let's see uh we get something close to zero. Again since this is an approximation it won't give us the exact value. Uh similarly we can use math dot cause to find the value of a particular angle. Um if you want to find if you want to find the value of the sorry the cost of a particular angle we can use the tan function in case we wanted to find the tan. Similarly we have the next set for ark sign and the arc cosiness of uh the certain of angles. So to use to find the ark sign for example we would use a dot sign I mean sorry a sign. Uh for r cosine we would use this and for arc tan we would use this. The next set would be hyperbolic functions. Again hyperbolic functions are are basically the sign and the cosine and the time calculated not circles but on hyperolas. Uh again the details don't have to be you don't have to know the details. You just should uh you should know that if you wanted to say for example find out the uh hyperbolic sign of an of an angle we would use math dot sin h and we would pass what we wanted between this. Similarly, if we wanted to find the cosine, it would be cos h tan, it would be tan h. And if we wanted to find the inverse of uh the hyperbolic inverse, what we would do is what we would do is use a. So we would use a tan h. If we wanted to find the hyperbolic inverse uh of s, we would use a sin h. And similarly for cos, it would be cos a cos h. In this video, we will learn about conditional statements. Uh and we will learn about uh how do we create these control structures. where we have these conditional statements. Uh we will learn about the keywords that we use in Python to create a conditional statement block, the if keyword, the lf keyword and the else keyword. Uh we will also learn about indentation and its importance in Python. So let's get started. So the basic uh syntax if you will for a if statement in Python consists of the keyword if followed by an expression. As you can see from this uh pictorial uh there's an if statement there is a expression uh there is this uh block of code which if you can actually see is slightly indented to the rest of the code. Uh and then we have the rest of the rest of our code after that. So what we're looking at is this. So this is our if statement block. Um now let's let's break this down. So what is this expression that we're speaking of? uh an expression in this context is a boolean expression which evaluates to a true or false statement. So when we are speaking about the if statement we have an expression and if this expression evaluates to true then we ex execute everything below the if statement otherwise we skip that portion. As you can see if this statement is true we execute this statement or these statement blocks. uh otherwise if it's false we move on to the next the next statement block or the beginning of a new statement. Uh so let's look at an example to uh further understand this. Let's take this if statement over here. As you can see in this if statement I have this expression 50 greater than 30. So what Python is going to do now is it's going to check whether 50 is greater than 30 and it's going to check in a boolean context. If this is true then we can move on to the next portion which is this. But if it is false then we will skip this. So let's see what this uh what happens over here. So since we know that 50 is indeed greater than 30. This is a true expression. We move on to this block of code which is saying that print 50 is indeed a larger number than 30. So let's see if this actually gets printed. So as you can see we get this uh the result which is this is a true statement and we get our print statement getting executed. What if we gave a false expression over here? So let's look at uh what happens if I give a false expression. Now I'm checking if 50 is lesser than 30 which is a false statement. So if it's a false statement, we do not move on to this portion of our if statement block and we we directly in fact we directly skip. So if I have to execute this, you will see nothing gets executed and in fact my print statement is just not executed because we never entered this because our expression over here was indeed false. So yeah, this is how a basic if statement block looks like in Python and this is how the uh how we move on to how we check for the true the truth truthfulness or the falseness of a particular expression in the if statement. So moving on um let's move on to the topic of indentation in um in Python. So as you can see this uh this if statement is an example of a control structure. So without moving without defining what a control structure is uh in detail control structures consists of these uh conditionals or loops or uh iterations if you will. Uh so all of these control structures involve heavy indentation. So what does an indentation mean? Well, what if I gave you an example? Say I gave you some variable called age and I wrote 20 over here. Now if I gave you an if statement saying if age is greater than 10. Now uh it's very important to note that when I'm defining an if statement I need this colon otherwise this if statement is not defined. So this uh colon is important. Uh anyways so now I'm checking this expression if age is greater than 10. Now what is this? What happens if age is greater than 10? Now what if I want Python to do something only if age is greater than 10? Well, how do I tell Python that there is a piece of code or a section of code that I want executed only if this condition is met? Well, this is where indentation comes into play. So, if I want say if I want this line, this person is an adult. Again, this is some random line where I'm assuming age is pertaining to this adult. All right? Or this person. So if I have uh this variable age where I write this uh number 20 which denotes the age of some some person and I'm doing a check if age is greater than 10. Actually let's be specific here. Well the adult age for most people is 18 in most countries. So let's see if age is greater than 18. Now if I'm doing this check I want to print this statement only if this person is person's age is above 20 uh above 18. So how do I tell Python that I only want this statement to be uh executed only during this if this condition is met? Well, this is where indentation comes into play. And as you can see, exactly as you can see uh this part, this portion of the code is slightly indented compared to the if statement. So when this condition is met, then only do we move on to this. If this condition is not met, we move on to whatever the next section of code will be like whatever whatever uh code that we have after this if statement. So let's see. So this since this condition is actually met, as you can see this print statement is actually executed. So this is the importance of indentation. Now we will see uh in as we move as we learn more about if statements and else statements and l if statements how indentation plays a key role in defining what we want as an output when a certain condition is met. All right. So for now it's very important to understand that uh this is how indentation is used in Python. In other languages we might use uh we we probably don't use indentation. In fact popular language the C language does not use indentation at all. But some languages and Python probably the trademark language for uh indentation in programming. So you might say that uh so different people have different opinions on indentation. The general consensus is that indentation helps make the code look very presentable and very readable and this actually is one of the facets of Python programming is the indentation and the easy readability of it. Anyways, moving on. So what if we have um multiple conditions in our code or in our if statement block that we want to see. So let's create this um list or let's actually just copy this example that I have written over here. As you can see what I'm doing in this piece of code is I have a list X where I have this integer 50. I have two strings gat and apple. Now I want to check a bunch of things. So I want to check if 60 is an X and I also want to check if the string cat is an X. Uh and I want a final sort of like if none of these conditions are met, I want something to happen. So how do we do that? Well, we start off our state our if statement block with the standard if keyword. All right. Now if we have one more condition that we want met, then we can use the l if which is kind of a short form for else if. Uh so we use the l if keyword. So it so what this Python uh what Python reads is it checks this condition then it will check this condition. And then finally we have this else keyword and else is a keyword used in our block to tell Python that if none of our earlier expressions have been true then do what is in the uh that is within the else keyword that is indented within the else keyword. So again we don't pass an expression for else because else is only we only reach this l else statement in case all of the earlier conditions have not been met or have come to be false and not true. So let's look at uh this particular code example. So we have a list of 50 cat and apple and I'm my first expression that I'm checking is uh is 60 in x. So this is how we check actually. So if 60 and X should be read as is 60 in the list X. So is 60 in the list X? Well actually no. We don't have the object 60. So this part is not executed. We move on to the next condition. Is the string cat in X? Well, actually it is uh the string cat is indeed in X. So we move on to this statement block which is within the condition that is met which is print cat is in list X. And you will see that uh none of the other statement blocks will be executed because we have met our condition over here. So if I execute this as you can see that uh the print statement cat is in list x is what is actually executed and we do not uh execute this obviously because it is false and we do not even check or we do not even move on to the statement because well we have a condition met over here. So it's uh one thing that is important to note is that we can use as many L if statements to check for as many conditions that we we want to check for u but it's very important that we only use and if we are using where we only use one else statement and only at the end of our block. Okay. So moving on now I'm going to show you a very neat example about how Python actually does its uh these condition checkings. So it's very important to note that once a condition is met, none of the other conditions are even checked for. So um we do not even consider the next statements and we directly leave this uh if statement block. So let's look at this example over here. Now in this example that I have over here is where I've stored the string cat in x. Now I have three um I have three conditions that I'm checking. I'm checking is 50 greater than 60. I am checking if the value of x is indeed cat and I'm checking I'm some random expression 1 divided by 0. This is not even a check. This is literally just checking if 1 / 0 is true or false. And as we know we cannot divide anything by 0. So let's see what happens when I execute this statement. Now typically if I wrote 1 / 0, Python will throw me an error because one cannot be divided by 0. That is an error error in a statement. So let's see what happens when I do this. Actually as you can see I have a statement being printed but I have no error even though I gave a wrong I gave an error in a statement in 1 divided by 0. Well now you might ask why is that so? Well it's simply because I have actually never reached this condition. I have not even uh Python has not bothered looking at this because if you actually see there is a condition previous to this uh over here which is met and which is true. So we move on to this block. We execute this block and we forget about the rest of the code. So we don't actually get to uh this error statement which is an interesting thing and which is an example that I have shown uh to to show you that once a condition is met in an if statement block we do not look at any of the further conditions that are being checked. Okay. So finally um I have one u one small topic that I would like to share with you is the conditional expressions or the python turnary uh operator. Now this is slightly different to the if statement that we have talked about up until now uh in the sense that it is not a control structure. So what this actually is is more of an operator that defines some expression. So let's see what I am trying to say over here. So as you can see over here highlighted is the uh syntax for what I'm trying to talk about this turnary operator. So let's look at this example and let's see what I'm trying to do over here. So I have this variable age. All right. And this age is 26. Now x now just just read this uh statement x is equal to adult if age is greater than 18 else child. Now I could have done an elaborate control structure with an if statement then an indentation within that if statement and then um I could have written else and then another indentation and I would have written another statement over there but instead of that I have used this uh operator to assign a particular value to the variable x and this what value I'm assigning to x depends on if this condition is true or false. So uh as you can see the condition says is age 18 uh greater than 18. So if this is true then I assign this particular string. If it is false then I go on to the else statement and I assign this. So since age 26 is greater than 18 I should assign the string adult to x. And as you can see if I execute this and check what is an x. You can see adult is indeed an x. So this is a very neat a very needable readable and a very um it's a it's probably a more it's it's a better looking way uh to perform simple else uh simple if and else statements where you don't want to create a whole um if state if if else statement control structure block. So yeah this was the turnary operator. Finally we let's uh we we make a short note on the pass statement uh in Python. So the pass statement or the pass keyword is a a place where is is a placeholder of sorts. So what if I had an if statement and I'm writing a piece of code but I don't know what to do with this if statement. So if if a statement is correct if a statement is true um but I don't know what to do if this statement was true. How do I tell Python? Well, let's just keep it a hold for now and I might put something into this uh block later in the future. Well, I would use the past statement. So if I had this uh say there some condition again. So I'm checking age is greater than 18. But I don't know what to do with this. I would just simply use this pass statement and uh this basically does nothing. Uh it it's a way to it's it's like a way to create a placeholder where maybe in the future I might have I might write some sort of a say a print statement or some sort of uh different function or something else within this indentation. So but in the meantime like currently I don't have anything to place over here. So I just leave it with a pass statement. So this is uh a way where you can use a pass statement in if else statement or if else control structures. In this video we will be I will introduce you to the concept of iterations in Python and we will be speaking in specific about the indefinite iteration in Python otherwise known as the while loop. So let's look at what an iteration means. An iteration is executing the same block of code over and over potentially many times. A programming structure that implements an iteration is called a loop. Now in programming we have two types of iterations definite and indefinite iterations. And in this example we will be speaking about indefinite iterations. Indefinite iterations in Python is the while loop where we use the while keyword to define an indefinite iteration. So uh let's look at the basic syntax. As you can see this is the basic syntax where we have the while keyword followed by an expression and uh again the colon and below that is something indented. Again we encounter indentations just like in if statements uh a set of statements where um what happens here is following the while keyword we have this expression where um this expression is evaluated in a boolean context. Again we check if this expression evaluates true or false and if it is true we uh execute all the statements or the statement block that is indented below this while statement. Uh this is another example of control structure much like if and else statement blocks where we use a lot of indentation. So let's look at a basic example now of while loop. So let's copy this example and let's see what this does. So I have a variable n where I am assigning it a value zero. This is called an initializing process. And it's very important to note that most while loops consist of an expression which contains a variable, a control variable uh which is usually initialized prior to starting the loop and then this control variable is typically verif modified somewhere within the uh main loop body. So in this case my control variable is n and I am initializing it by assigning it the value zero. So let's uh move into the loop now. So the loop has this while keyword as you can see followed by a uh an expression n lesser than or equal to 5. Now what this is is an expression which is going to check if it's true or false. Is n lesser than or equal to 5? And as long as this expression is true, we will continue doing whatever is indented below this while statement. And what is below this while statement is a print statement first of all which says value of n is and it basically prints the value of n in that particular iteration. And as you can see this uh compound assignment where we add the value of n by one and we assign it back to n. So what this is uh let's see what this does actually let's execute this and see what this does. Uh important to note the value of n again is initialized at zero. So this while loop will keep going on until the value of n remains lesser than or equal to 5. So as you can see I have the execution of this uh particular block of code or this while loop where I get the a multiple different iterations of this print statement that you can see here. The first iteration it says value of n is zero. Then it says value of n is one. What I'm doing is I am writing I am assign I'm writing uh the value of n multiple times. uh and each time the value of n is written over here based on what iteration it is. So what is happening actually is uh that initially the value of n is zero and I print this statement value of n is zero. Now I add uh the value of what is in n by one. So now what is the value of n? So again we do this check. So after this first iteration we come back or like once this whole uh code block of code is executed we again come back to this expression and we again check if this statement is true. So now that the value of n is actually one this statement is still true. So again we execute this and that you can see over here value of n is one. Now again we add the value of n by one and again we go back here once this uh this block is executed and we check if this statement is true again. And as you can see the value of n when it's two is still making sure that this expression is true. And this keeps happening until the value of n is greater than five. So once the value of n becomes six, this condition is no longer met. And finally we do not execute this statement because this expression evaluates to false and we do not do this anymore and we finally leave our while loop. So let's check what the value of n is just to confirm this. As you can see the value of n is indeed six and that is why we have left this while loop over here. So this is an example of a basic while loop where we check for this an expression. If it evaluates to true we evaluate everything within it. Once this is done we go back and we evaluate this expression again. If it is again true we again go back and do whatever is indented within it. And we keep doing this until the expression gives us false. And interestingly, we don't uh we don't necessarily have to define these control variables always. We can actually use uh iterables and we will learn about iterables actually in a future video. But uh we can use iterables and an example of an iterable is uh the list. So let's look at this list and let's look at this whole block. All right. So I created this list a which contains five objects 1 2 3 4 and five all integer objects. And I have another variable x where I store the integer one. Now I'm this while statement. Now it might look confusing. Now it says it says while a. Now what is a? Well a is not uh your typical expression where you're making sure if some condition is being met or not. But this the way you read this is while a exists. So in the context of a list as long as a which is a list contains objects within it a is true. If it does not have any objects within it a is false. So while a basically what it means is while a has objects within it and if it does have objects within it or as long as it does we continue executing whatever is indented within this while statement and this while statement is a print statement again we uh do like we we have this value x where we um we're doing this this we're printing this is iteration number and uh we pass x. So the first iteration will obviously pass x is equal to one and then we add x by one and then the second iteration should pass this is iteration number two and then so on and finally we have another statement within this block called the a called the pop statement where we're popping the elements of a. Now as if you would have seen the list the video on lists the pop statement removes the rightmost value from a list. So if I do a pop initially it will remove five from this list and I will have the rest of them again. And if I do pop, it will remove four from this list and then you will have the rest of these. So let's actually see we'll actually check what's left in this list each time we do this pop statement. So I'll also print the list a each uh during each iteration and let's see what happens over here. As you can see what is happening out here is while a or while a has elements we are going to continue doing this while statement whatever is within it. So in the first iteration obviously we have all of it uh and it is iteration number one and as you can see the list contains everything. Then I pop it. Then iteration number two it again has elements but it has lost its most rightmost element. And this keeps on happening until the list finally has no elements left. And once element or once the list has no elements left we this expression while a evaluates to false and we do not execute this anymore and we leave the while loop. And again let's check what is in a. A should have no elements. As you can see it's an empty list. When I check what's in A, it's an empty list. And that is why while A returned near false and we did not print any further. So this is an example of using a while statement using lists. Uh and of course the next topic would be discussing nested while loops. And um this is a broad topic. What I mean by this is that when when I'm saying nested while loops, I'm actually talking about nested control structures. So we can have while loops within while loops. Not only that, we can have if and else statements within while loops. We can have while loops within if and else statements and we can have if and else statements within if and else statements. So if you think of all of these loops uh and we will even speak about for loops in the in the in in a in another video. For loops again are type of control structures. So all of these are control structures. While loops are control structures, for loops are control structures, if and else statements are control structures. All of these are control structures and nesting can happen within them and it does not matter which is the like the parent of the nest and then the child of the nest. You can have a while loop within an if and else statement. You can have for loop within a while loop. All of this is valid in Python and uh it's one of the one of the great features of Python that is very flexible in this way. So let's look at an example where I have a while loop within another while loop. So I'm copying this piece of code where I have this list 1 2 3 4 5 and another list first and second. Now my first while loop I print I print what is popped from a. So I take a then I pop the zeroth index. So basically from the left hand side and I print what is that. All right. And then I have another while loop where I have this list B and I'm doing the same thing. But this while loop is within the first while loop. And let's see what happens out here. So as you can see my first my first iteration of this parent while loop causes me to pop what is the first element which is one. Then I do these while loops and this is completely executed and only then do I go back to this first one and then I come back and do all of this again. Then I go back to this one and then I come back and do all of this again. And that's why you can see this repetition. So I have this first iteration and then I do uh the second while loop. Then I have the second iteration and then I do the second while loop and I keep doing this and this is an example of a nested while loop. Okay. So our next topic in uh this in this video would be break and continue statements. Now break and continue statements are ways to abruptly end iterations uh in in a particular loop. You can use break and continue statements not only in while loops but you can use it in also in for loops. So let's look at how we use it in while loops and let's look at what we mean by break and continue statements. First of all, well, the break statement terminates the loop entirely. So once the break statement is found the the complete while loop that we are using is completely terminated and the continue statement on the other hand terminates only the current loop iteration and it goes back to the beginning or the expression and it checks whether uh the expression or the the controlling expression is again reevaluated and it is checked whether uh the loop will execute again or terminate. This pictorial that you can see here actually emphasizes or it shows the difference between break and continue. So I have this while loop and I have an expression. All right, I have a bunch of statements and I have a break command over here and I have a continue command over here. So if I encounter the break command, it will not execute any of this. In fact, it will not execute this while statement at all and it will move on to the next piece of code that we have. But a continuous statement if a continuous statement is encountered in a while loop will not completely break away but it will not uh it will not execute the the rest of the statements in that iteration only these two but it will go back here it will check this expression again and again we will do this all over again. So this is just small uh difference between the break and continue statements but both are used to abruptly end an iteration in a loop. So let's look at an example of how we use the break statement. So I will use this example where I have again I have initialized some controlling variable n to zero. Uh my first loop will check whether n is less than equal to 5. If this is true then I will print this statement. Then I will what I'm doing over here is I am incrementing it by one. Now in my previous my earlier example using a similar uh using the similar example uh I had no like this this part was not there. So what you got was I was printing the value of n. So it was printing as like value of n is one I mean zero and then 1 and then two and then three four and five. But over here I've included a small I've nested an if statement and within that I put a break statement and this if statement is checking if if n is equal to equal to two. So once n is two I move on to this and I do the break statement. So let's see what happens now. As you can see once so when I've executed this I don't get the whole value of n is zero and then 1 and then 2 3 4 5 I I stop at one and that is because once n is equal to two I actually do not execute this while loop again. I do not go back and execute print value of n is two. Now in fact I break from this whole while loop and my while loop terminates right then and there. And that's why you only get these two iterations. Value of n is zero and then value of n is one. Okay. So now I'm going to show you a neat example of what happens or what is the difference between a break and a continue statement. Okay. So let's h go back to our initial example where we're printing this value of n is 0 1 2 3 4 and so on. But I have added one more statement where I print hello at each iteration. So let's see what happens when I do this. As you can see it will print hello after each of whatever I was doing already. Now let's introduce our break statement first of all. So when I do break well let's go back to this if statement. If is if we check this condition then we break. Let's see what happens now. As you can see something interesting has happened where we have this first iteration completely being executed but the second iteration kind of stops where there's no hello and that is because we have broken from this while loop before the second iteration's hello was printed. Let's see the difference between a break and continue now. So now I have this continue statement instead of a break. Let's see what happens here. As you can see, it seems familiar to our first example where we did not have any break or continue. But there is a small difference that I want you to catch and that is very important about this continue uh statement. So as you can see the first iteration has value of n is zero and then hello and value of n is one but there is no hello and this is because and this is because once this condition is actually met we do not move on to this statement but we go back to this expression and we evaluate this expression and then once we evaluate this expression again and it's found to be correct. We do everything the same again where we print hello but except for that one step where this if statement was met and we did continue and this following statement was not executed and that happens only for one iteration and that is when n is equal to equal to two and as you can see that is what happens over here in one of the iterations we miss a hello and that is the difference of the continue statement between uh that is the difference of the continue statement with the break statement. This video will be an extension into that topic where we will be looking at three examples where we are using while loops along with other stuff li
Original Description
🔥IIT Delhi - Data Analytics, Generative AI And Adaptive System - https://www.simplilearn.com/ihfc-iitd-data-analytics-genai-course?utm_campaign=n7_SsIVQRUs&utm_medium=DescriptionFirstFold&utm_source=Youtube
🔥Professional Certificate in Data Science and Generative AI - https://www.simplilearn.com/iitk-professional-certificate-course-data-analytics?utm_campaign=n7_SsIVQRUs&utm_medium=DescriptionFirstFold&utm_source=Youtube
🔥IIT Kanpur - Professional Certificate Course in Data Analytics and Generative AI - https://www.simplilearn.com/iitg-generative-ai-data-analytics-program?utm_campaign=n7_SsIVQRUs&utm_medium=DescriptionFirstFold&utm_source=Youtube
This Python Full Course 2026 by Simplilearn begins with an introduction to Python, setting the stage for why it's a powerful and popular language in today's tech industry. This Python Full Course 2026 is designed to take you from beginner to advanced level in a step-by-step manner. You’ll start by understanding what Python is, how to install it, and write your first program. The course then explores core concepts like variables, data types, strings, lists, dictionaries, and control statements. You'll also learn loops, functions, file handling, and Object-Oriented Programming. Advanced topics like polymorphism, list comprehensions, and REST APIs are covered. Hands-on projects in EDA and web scraping enhance practical skills. The course ends with top project ideas and interview questions to prepare you for real-world roles.
The Python For Data Analytics Full Course video covers the following topics.
00:02:10 - What is Python
00:51:37 - Linear Regression Analysis using python
01:26:46 - Python Basics
- First python program
- Variables and Expressions in Python
- Objects in Python
- Type conversion in Python
- Strings in Python
- Escape sequence in Python
- Lists in Python
- Tuples in python
- Dictionaries in Python
- Arithmetic operations in Python
- Math Function in Python
- Conditional Statements in Python
- While
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Simplilearn · Simplilearn · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Ethical Hacking Full Course 2026 | Ethical Hacking Course for Beginners | Simplilearn
Simplilearn
AWS Full Course 2026 | AWS Cloud Computing Tutorial for Beginners | AWS Training | Simplilearn
Simplilearn
Data Structures And Algorithms Full Course | Data Structures and Algorithms Tutorial | Simplilearn
Simplilearn
SQL Full Course 2026 | SQL Tutorial for Beginners | SQL Beginner to Advanced Training | Simplilearn
Simplilearn
Microsoft Azure Full Course 2026 | Azure Tutorial for Beginners | Azure Training | Simplilearn
Simplilearn
Shopify Tutorial For Beginners 2026 | Shopify Course | shopify dropshipping | Simplilearn
Simplilearn
Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Simplilearn
🔥Feeling Stuck? How Upskilling Can Boost Your Career! #shorts #simplilearn
Simplilearn
Growth Hacking In Marketing | Learn Growth Hacking Marketing Strategies | Simplilearn
Simplilearn
🔥Cracked 3 Job Offers with One AIML Course! | 20–30% Salary Hike #shorts #simplilearn
Simplilearn
Top 10 Must-Have Figma Plugins for UI/UX Designers in 2026 | Figma Plugins | Simplilearn
Simplilearn
Business Analytics Full Course 2026 | Business Analytics Tutorial For Beginners | Simplilearn
Simplilearn
Simplilearn Reviews | Getting future-ready with course in Artificial Intelligence | Roopam’s story
Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
Full Stack Developer Course 2026 | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Simplilearn
Simplilearn Reviews | How David Went From Seasoned Engineer to AI Innovator #GetCertifiedGetAhead
Simplilearn
Complete Social Media Marketing Strategy for 2026 | Social Media Marketing Strategy | Simplilearn
Simplilearn
🔥Top 4 Cybersecurity Certifications You Need! #simplilearn #shorts
Simplilearn
🔥Cloud Engineer Salary in India 2026 | City-Wise Breakdown #shorts #simplilearn
Simplilearn
Digital Marketing Full Course 2026 | Digital Marketing Tutorial For Beginners | Simplilearn
Simplilearn
Full Stack Java Developer Course | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Simplilearn
Social Media Marketing Full Course | Social Media Marketing Tutorial For Beginners | Simplilearn
Simplilearn
How To Create LLM Chatbot Demo 2026 | Build a LLM Chatbot From Scratch | Simplilearn
Simplilearn
Digital Supply Chain Management Certification | Supply Chain Management Course | Simplilearn
Simplilearn
AI Agents Full Course 2026 | AI Agents Tutorial for Beginners | How to Build AI Agents | Simplilearn
Simplilearn
ITIL Full Course 2026 | ITIL 4 Foundation Course | ITIL Tutorial For Beginners | Simplilearn
Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
ITIL Full Course 2026 | ITIL 4 Foundation Course | ITIL Tutorial For Beginners | Simplilearn
Simplilearn
Simplilearn Reviews | Integrating AI & Music | Diego's Story
Simplilearn
Digital Marketing Full Course 2026 | Digital Marketing Tutorial For Beginners | Simplilearn
Simplilearn
SEO Full Course 2026 | SEO Tutorial for Beginners | SEO Training | SEO Explained | Simplilearn
Simplilearn
PMP Vs CAPM: Which Certification Should You Choose? | PMP Vs CAPM | Simplilearn
Simplilearn
Complete Data Analyst Roadmap 2026 | How To Become A Data Analayst In 2026 | Simplilearn
Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
🔥5 Jobs That Are Most Likely Safe from Layoffs in Today’s Market #shorts #simplilearn
Simplilearn
🔥Git vs GitHub – What's the Difference?
Simplilearn
What Goes Behind Building the Likes of Uber and Netflix? | Product Management Tutorial | Simplilearn
Simplilearn
AI Agents Full Course 2026 | AI Agents Tutorial for Beginners | How to Build AI Agents | Simplilearn
Simplilearn
Full Stack Developer Course 2026 | Full Stack Java Developer Tutorial for Beginners | Simplilearn
Simplilearn
Product Life Cycle 2025 | Stages Of Product Life Cycle | Product Life Cycle Tutorial | Simplilearn
Simplilearn
Project Management Full Course 2026 | Project Management Tutorial | PMP Course | Simplilearn
Simplilearn
PCB Design Course 2025 | PCB Designing Explained | How To Make PCBs | Simplilearn
Simplilearn
Python Full Course 2026 | Python Data Analytics Tutorial For Beginners | Simplilearn
Simplilearn
🔥Top Product Management Skills You Need to Succeed in 2026 #shorts #simplilearn
Simplilearn
SQL For Data Analytics 2026 | Essential SQL Commands | SQL Tutorial For Beginners | Simplilearn
Simplilearn
Simplilearn Reviews | Paving Way To Success With AI & ML Course | Soumik’s Upskilling Journey
Simplilearn
Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Simplilearn
Learn Snowflake In 45 Mins | Snowflake Tutorial | What Is Snowflake | Snowflake Explained
Simplilearn
🔥ML Career Tip – How to Start Learning Machine Learning in 60 Seconds! #shorts#simplilearn
Simplilearn
🔥Agile vs Waterfall in 60 Seconds #shorts #simplilearn
Simplilearn
Excel Full Course 2026 | Excel Tutorial For Beginners | Microsoft Excel Course | Simplilearn
Simplilearn
What Are AI Agents? | Types Of AI Agents | AI Agents Explained | AI Agents Tutorial | Simplilearn
Simplilearn
How To Create a Product Roadmap In 2026 | Product Roadmap | What Is Product Roadmap | Simplilearn
Simplilearn
SQL Full Course 2026 | SQL Tutorial for Beginners | SQL Beginner to Advanced Training | Simplilearn
Simplilearn
🔥What Is Phishing? #shorts #simplilearn
Simplilearn
Cloud Computing Full Course 2026 | Cloud Computing Tutorial | Cloud Computing Course | Simplilearn
Simplilearn
Simplilearn Reviews | Overcoming Rejection & career plateau to finding a New Job : Bhaskar Banerji
Simplilearn
Six Sigma Full Course 2026 | Six Sigma Green Belt Training | Six Sigma Training | Simplilearn
Simplilearn
Generative AI Full Course 2026 | Gen AI Tutorial for Beginners | Gen AI Explained | Simplilearn
Simplilearn
VLSI Design Course 2026 | VLSI Tutorial For Beginners | VLSI Physical Design | Simplilearn
Simplilearn
Related Reads
📰
📰
📰
📰
I Built a Tool to Visualize DSA. Let’s Learn Together! (DSA View View 👀👀)
Dev.to · nyaomaru
Why More Organizations Are Embracing Conversational Analytics
Dev.to · Ravi Teja
I Pre-Registered a Hypothesis. 600 API Calls Later, the Data Killed It.
Dev.to · YuhaoLin2005
Data Science Course in Ameerpet: Complete Guide for Beginners (2026)
Medium · Machine Learning
Chapters (3)
2:10
What is Python
51:37
Linear Regression Analysis using python
1:26:46
Python Basics
🎓
Tutor Explanation
DeepCamp AI