Important Python Concepts for Beginners | Learn Python with Examples | Python Tutorial | Intellipaat
Key Takeaways
This video tutorial covers important Python concepts for beginners, including data types, variables, control structures, functions, and data analysis, with practical examples and explanations to help learners understand and apply Python programming skills for data analytics.
Full Transcript
Do you know what separates someone who knows Python's index from someone who actually understands Python? It's not knowing more keywords. It's understanding the concepts behind the language. Concepts like dynamic typing, mutability, decorators, object-oriented programming, and some of Python's unique behaviors can either save you hours of work or leave you scratching your head wondering what just happened. So, in this video, we're going to break down the most important Python concepts you should know and understand how they work with simple examples and explanations. Let's dive in. All right, so let's get started. So, I have my VS Code open and I'm simply going to go ahead and create a file called variables because that is our first topic. Okay, so whenever we want Python to remember any piece of information for us, instead of manually typing the value each and every time in our code, we're going to store it in something known as variables. Okay, so say for example, we're building a student management system. So, in that student management system, we'll have to store the student's name, the student's age, their marks, or the subjects they have scored their marks in, and so on, right? So, instead of manually typing those values again and again, we're going to create a variable called student name. Okay, and let's say the student's name is John, and then I'm going to create another variable called student age, right? And then I'm going to create another variable called student marks. And say the marks are around 65.5. And say I have another variable called whether the student passed that particular semester or not, so I'm going to say true. So, whenever the student has passed the exam, it's going to be true, otherwise it's going to be false. Now, in order to check the type of uh my variable, like what uh data type this particular variable is storing, what I'm going to do is I'm simply going to print the type of my variables. Okay, and then finally I have is passed or not. Okay, so let me just run this particular file. Okay? So, you can see the student name is of type string. Then my age is of type integer. Then I have marks as type float. And then I have true, which is type boolean. Okay, so say in case I had false over here. Again, it would be a boolean value. Right? So, true and false come under boolean values. And whenever we have a decimal, it is stored as float. And whenever we have a whole number, it is stored as an integer. Okay, now what if we had a single character? What do you think is going to be the data type of that particular variable? Quickly go ahead and comment your answer. Okay, so let us check. Okay. So, you can see again a single character is also of type string. Okay? So, I hope this particular concept is clear. Moving on to the next topic, that is data structures. Okay, so whenever we need to work with multiple values, Python has something known as data structures for us. So, whenever we are talking about data structures, we're talking about tuples, we're talking about lists, dictionaries, and sets. Right? So, these are our main data structures. Okay, so here you guys notice we're storing a single value John in a variable student name. Okay, same goes for age, marks, is passed, and the student grade. Correct? Now, what if I want to store multiple values of say multiple students? Okay. I'm just going to go ahead and say student names. And what I can do is I can pass all my student names in this particular list. Okay? Then I can go ahead and create another list of student age. Similarly, I can do it for the marks and so on. So, what I can do here is using these data structures, I can store multiple values. Okay? So, I don't have to go ahead and create separate variables for each particular value of John, Alice, and Bob. I can simply create one variable stored in names, and then create a list for that particular variable. Okay? So, now, uh lists are mutable. Okay? So, what do we mean by mutable? Once I create this particular list, I can go ahead and add another name, okay, using append. All right? Now, if I go ahead and print this, I will get my output. So, as you can see over here, my output is John, Alice, Bob, and Charlie. So, basically, list allows modification. Similarly, I can go ahead and also delete or like remove a particular element. Okay? So, say I want to remove Alice. Now, if I go ahead and print my student names, okay, so you can see over here, now my list contains John, Bob, and Charlie. Alice has been removed. Now, if we talk about tuples, so tuple also allow us to store multiple values, okay, just like a list, but in case of a tuple, we are not allowed to modify it. Okay? So, say this is my sample tuple over here. So, I have a student's name, I have their age, their marks, whether they passed the exam, and their grade. Okay? So, since you're saying the student has failed the exam, it is It makes more sense to have some grade like E or F. Right? So, now, if I go ahead and print my tuple, okay, so let me just go ahead and run this. Okay, so you can see this is my tuple over here. So, whenever we create a tuple, we use parentheses, but when we create a list, we use square brackets. Right? So, over here, if I try to modify my tuple, so let's run this. Okay, so you can see over here, I got an error. Tuple object does not support item assignment. Okay, so I cannot assign a particular item to my tuple. Now, if you guys are wondering, I did not use this particular method to do it for my list. Maybe that is that is the reason I'm getting an error. So, let me show you guys the append method which does not exist for tuples in the first place, but let me just go ahead and run it so that you guys get more clarity. So, again, double does not support item assignment. Okay? So, instead of appending, so append usually works when we want to insert an element in the last. Okay, that is for list. But if you want to say insert an element in the start of the list or at a particular index, we can simply do it using this method. Okay, so let me just show it to you guys. So, say I want to insert the name David at the index one. Okay, so index one would be the second position, right? Okay, first let me comment this so that I don't get an error again and again in my code. All right. So, once I save it, I'm going to run it. Okay. So, over here you can see I did not get any error, but it did not give me the modified list because I forgot to print it. Okay, so let me just go ahead and print my list. And now you can see over here John, David, and Charlie. Since we had already removed Alice over here. So, at index one we have David. Okay, so I hope you guys know how indexing works. Indexing starts from zero. We have John at zero, David at one, and Charlie at two. And similarly, we have something known as reverse indexing where Charlie is going to get minus one, David minus two, and then John minus three. All right. So, let us also quickly go ahead and see indexing before we move to the other data structures. Okay, so all right, let me take this same student names list. Okay? So, we have this particular list and say I want to access Charlie. Okay, so I can access Charlie in student names with the index minus one. Okay, I'm simply going to print it. So, you can see over here my my output is Charlie. Okay, so to make things more clear, what I can do is I can say last student. Okay? So, when we're looking at our outputs, it is going to be more clear for us. Okay? So, that is what we call indexing. Now, similarly, we have something known as slicing. So, slicing is when we want to print a particular section of our list. Okay? So, say I want to print the first two students, that is John and David. So, what I'm going to do is I'm going to give my index is zero. So, that is my first index. So, whenever we're passing our first index over here, it's inclusive, and our ending index is always excluded. Okay? So, at the index two, we have Charlie. So, Charlie will not be included in our output, and our result is just going to be the names John and David. Okay? So, let us run this. So, you can see over here first two students, we have John and David. So, this is how indexing and slicing work. Moving on to our next data structures, that is sets. So, sets are nothing but a collection of unique elements. So, whenever we want to store elements that should not have any duplicates, this is the most useful data structure. So, say for example, I have a set of numbers, 1 2 3 4 5. Okay, so let me give another duplicate value, say four. Okay? And then I'm going to give another one, three. Now, what I'm going to do is I'm going to print numbers. Let me just say this is a set. Okay? So, when I run it, you can see I have only unique values, that is 1 2 3 4 and 5. So, my duplicate values, that is four and three, have been removed. Okay? So, this is how you can store your values in a set, and then finally, we have dictionaries. Okay? So, say I have a student information dictionary, where I have my name. This is my key, and the value is John for this particular key, name. Okay? then I have my key called age and the value for that particular key is 20. Then I have my marks. And the value then again my key and a value and so on. Okay, so dictionary is nothing but your key value pairs. So it is similar to a dictionary that you use in real life. So whenever you look at a real life dictionary, you have the words and their meanings, right? So this is similar to how a real dictionary works. So say now I want to access this particular value John. Okay, how I'm going to do that? I am going to say print student info and I am going to pass my key value over here, that is name. Okay, so my dictionary understands that I am trying to access this particular value because this is the key I am calling over here. Right, so let me just run this and you can see my output is John. Similarly, I can go ahead and access the marks. Okay. So if I run it over here, I have my value of this particular key. Okay, now if you guys are already familiar with some other programming language, all this must seem pretty simple and pretty basic to you guys. But there is another functionality called dynamic typing in Python. Now what do we mean by that? In Python, a variable is not forced to stay of one type forever. Now what do we mean by that? So say I have a variable called value and I have assigned 100 to it. Okay, if I go ahead and print my value, you can see over here. Or say I go ahead and print the type of my value. So over here you can see it is a class integer. Okay, now what if I go ahead and assign this particular variable a string value? Do you think it is going to give me an error or is it going to uh run normally? Right, so it runs without any error because of the concept that we just discussed called dynamic typing in Python. So, it is mainly because of this specific feature in Python that it has become so popular and uh you can actually code without having to worry about strict declarations of your data types. But again, because of this particular feature, there is a trade-off. So, since Python is so flexible, you can make mistakes, right? So, sometimes uh you can actually uh say now I have assigned the same variable a string value. Now, if I try to uh perform any sort of mathematical operation on this particular variable, I'm going to end up getting a error, right? So, let me just show it to you guys. So, say now I want to add two in my value, okay? So, because earlier I remember assigning 100 to this particular variable, so I just want to add the number two to this particular variable. But now what I have done is I have also assigned a string value to this particular variable called value. And now when I go and try to perform any sort of mathematical operation, I am going to get an error, okay? So, I can only concatenate string, not an integer, since my value is now of type string. All right? So, this is again a major trade-off in Python because we don't have strict declarations of data types in our variables. So, let's move to the next concept that is functions in Python, okay? So, a function is nothing but a reusable block of code. Uh so, basically whenever you want to perform any specific task, instead of writing the code for that task again and again, you can just write it once and then call that task whenever you want to get it performed, okay? So, say you're building a very simple application, say a calculator, and you want to add two numbers, okay? So, you might have to perform that addition multiple times throughout your uh application process. Right? So, instead of saying print a plus b again and again or performing that logic again and again, what we are going to do is we are going to create a function. And whenever we create a function, it starts with d e f. Okay? So, I'm going to say add numbers and I'm going to pass two variables a and b that I want to add. Okay? And then I'm simply going to say return a plus b. Okay? So, this is the operation that I'm performing. Now, this is a very simple logic and typing this a plus b does not take very long, but again, when we perform or when we build actual applications, our logics are not limited to just one-line syntax. So, this is why we require functions. And then we can just call our function add numbers and pass our values, that is five and 10. Okay? So, if I run this, I should get the output as 15. Right? So, another way that I can perform this is I can create two variables, say x = 5 and y = 10. And then over here, I can simply pass variables x and y. Okay? If I run this, again, my output should be 15. Okay? Now, this is a very simple demonstration of a function. Let us try and perform something more practical. So, let's take another example where we need to calculate the grades for hundreds of students. Okay? So, how do we calculate the grades? So, say if the student has scored the marks greater than, say, 90, let's give him grade a. If the marks are, say, between 80 and 90, let's give him grade b, and so on. Right? So, now performing this particular logic for each and every student manually would take, again, a lot of time. Right? So, what we are going to do is we are going to create a function called calculate grade. Okay? And we are going to pass our marks in this particular function. Now, we have given the logic if marks are greater than or equal to 90, let's give the grade as B. But if the marks are greater than 80, the grade is going to be B, and so on, okay? And finally, if the marks are say below 60, the student has failed, okay? So, this is our logic. Okay? And now, what we can do is we are going to create a variable student marks, okay? And let's call the function calculate grade, okay? And pass the student marks. Let's go ahead and print it. So, if I run my code, you can see the output is the marks are 85 and the grade is B, okay? Now, I can go ahead and just pass the score over here in my variable and get the score instantly, okay? So, I do not have to write this particular block of code again and again, right? I can just go ahead and call it whenever I want. Okay? Now, let's go back to the dynamic functionality in Python. Now, say I had this particular function add numbers, okay? Where I'm returning the addition of my variables A and B. Now, I had performed something like X equal to 10 and say Y equal to 20. So, in this case, my function should run perfectly fine, okay? Let me just comment this. Okay, I'm supposed to print my result as well, okay? So, you can see I'm getting the output that is 30, which makes perfect sense. Now, say throughout the code, somewhere I tend to assign hello to X, okay? And then, I go ahead and call my result, that is X and Y, okay? Now, what do you think is going to happen? It is going to raise an error, okay? Now, in order to solve this particular error that was arising due to the dynamic functionality of Python, we were introduced to something known as type annotations. Now, what exactly is type annotation? So, it is nothing but a way to specify the type of data that a variable or a function is going to work with. So, it's just basically a label for your code, right? So, you're basically telling the developer and the code that this particular variable is an integer or this particular variable is a string. Okay, so let us look at how it is specified. So, suppose we have a variable age, I'm going to specify that this is an integer and it has a value 25. Okay? Similarly, I can do for string as well. Okay, so say I have a variable name and I want to assign it the value Alice, I'm going to specify that this particular variable is a string. Okay? Now, one very important thing that you guys should note over here is just specifying or annotating your variables is not going to change how your Python program executes. What I mean by this is, now say if I assign my variable age the value hello, now this is a string, but we have already specified over here that our age variable is an integer. Do you think it is going to give me an error or is it going to execute perfectly fine? I want you guys to quickly comment your answers in the chat box. Okay, so if I execute over here, you can see I got the value hello printed. So, now what exactly is happening? Okay, so when we said that type annotations act as label for your code, we are not saying that they are going to change the way our code executes. So, what we mean by this is, type annotations basically act like warning signs or like a reminder that your age variable is supposed to be an integer, okay? Since we have already declared it a value of an integer type, but it is not going to stop us from assigning a different value. So, let's consider an example where you're say organizing your house and you have different label boxes. Okay, so say one box says books only and another box says clothes only and say you have a third box for kitchen items only. Now, these labels are not going to physically stop you from say putting a spoon in your clothes only box. Correct? So, that is exactly how type annotations work in Python as well. Okay, so they're just a label. They're just a warning that okay, your age variable is supposed to be an integer and your name variable is a string. But again, if you want to go ahead and use a different kind of data type for that particular variable, Python is not going to raise any error. Okay, so I hope type annotations are clear. All right, so moving on to the next topic. Okay, so we just done talking about variables and data. Okay, so another important thing that you guys should know is in Python everything is considered as an object. Okay, so what do we mean by object? So, object is nothing but a package that contains both the data and the operations that can be performed on the data. Okay? And when we say that everything in Python is considered as an object, we actually mean everything. Okay, so let's see how everything is an object in Python. So, say we have a variable that contains the string hello world. Okay? Now, I can perform different kind of operations on this particular variable. And when I say operations, I mean operations like I can convert it into upper case. Okay? I can again go ahead and print it in lower case. Okay, so in in order to show you lower case, let me just go ahead and randomly capitalize some letters. Okay? So, first let's print the normal text that we have. Okay? Then let's go ahead and print the upper case, then the lower case. Okay? So, you can see this was our original string. Then we capitalized the string and then we converted into lower case. Okay? Similarly goes for your list. Okay, so say I have a list containing fruits. Okay, so say I have apple and then I have banana and then I have cherry. Okay. Now, we have already seen that you can go ahead and append or you can even remove items from your list because it is mutable. Okay, so say I want to append grapes. Okay. If I run it, okay, I did not print my list. So, let me just print it. And there. Apple, banana, cherry and then grapes. Okay, so we have already discussed that append works at the end of the list. So, whenever we want to add an element at the end of the list, that's when we use the append method. Okay. Similarly, we can go ahead and remove a particular item from our list. Okay? Similarly, we have functions that work as objects, too. Okay? So, say I have a function called greet. Okay? I'm not going to pass anything in my function. I'm simply going to print hello world. Okay? Now, what I'm going to do is I am going to create a variable, say hello, and I'm going to store my function in this particular variable. Now, I can simply call my variable over here. Let's see if it runs. And there. So, if I call my variable say hello, I'm simply calling the function greet over here. So, basically I have stored this function in my variable and I can simply go ahead and call my variable directly. Okay? And when we mean functions are objects, too, we mean functions can also be passed as an argument. Okay, so let me take you back to our functions file. So, over here you have seen that we passed a variable in our functions. Right? Okay, so you can see over here, currently our argument in our function is marks. Okay, so this is a variable. Now, when we say that everything is an object in Python, we are saying that we can pass functions. Let me just write it down. Functions can be passed as arguments to other functions. Okay? Let us take the example of this function greet itself. Okay, so we have already created the function greet over here, right? Now, what I'm going to do is I'm going to create another function, execute function, and I'm going to pass a variable found. Okay? And I'm going to print the function after calling it. Okay? Now, I can simply say Okay, let me just comment this because I don't want to run that. And now, what I'm going to do is I'm going to say execute function, and I'm going to pass the function greet. Okay? So, I'm passing this particular function as a variable or as an argument in a different function. Okay? So, let me just run this. All right, so you can see I got the output hello world. Moving on the next very important concept, that is function arguments. Okay? So, when we talk about function arguments, we are referring to the concepts star args and double star keyword arguments. Okay? So, till now, whatever function we have looked at Okay, so let me just write the addition function again. Okay, so I'm going to say add numbers. Okay? And we had simply returned the addition of two numbers, that is a plus b. And when we pass the correct parameters, we get the result. Okay? So, let me just run this over here, 30. Now, what if I want to add three numbers? I have a third variable now. What do you think is going to be the output? If I run it, I'm getting an error because add numbers takes only two positional arguments, but in our case, we gave three arguments. Now, if you're building an application like a calculator, do you think it allows only addition of two numbers or subtraction of only two numbers? No, you can go ahead and perform n number of calculations. Now, one possible solution that you guys might have in mind is that why don't we just go ahead and add another argument over here called C. Do you think it is going to work now? Definitely, it will work. But, now what if I go ahead and add another number, say 40. What do you think is going to happen now? I get an error again because I do not have a fourth argument over here, neither I have included it in my result. So, I have to go ahead and change the code every time the number of arguments or the number of parameters being passed are changed. So, one very good solution to this particular problem is that we use the argument called star args. Now, this basically helps the function by taking n number of arguments without having to specify. Okay? So, we can simply go ahead and complete this function. I'm going to create a variable where my total is zero. And then I am going to simply iterate over these particular arguments and I'm going to add the total. And then finally, I'm going to return my total output. Okay? Now, I can go ahead and pass just one parameter. Okay? If I do that, my output is 10. If I go ahead and pass say two over here, my output is 30. If I go ahead and say give five parameters, over here my output is 150. So, currently, I'm not getting any sort of error. Does not matter how many parameters I pass in my function. All right? So, this is how star args helps us in making our function more powerful. Right? Now, let's look at double star keyword arguments. Okay? Now, what does this particular function help us do? So, it basically gives us again a variable number of keyword arguments. Okay? So, say for example, I have a function where I have student information. Okay? Now, if I pass double star KW arguments in this particular function as an argument, I can simply go ahead and print the key value in my keyword arguments. Okay? So, what I mean by this is I can pass something like name is John, age is 20, marks is so and so. Okay? Now, if I run this here, I get the complete information printed. So, I do not have to go ahead and guess what variable is what, right? So, it is like a dictionary that I'm passing into my function. All right. So, moving on to the next very important topic, that is decorators. Okay. So, you can basically think of a decorator as an additional wrapping function around the existing function. Okay. So, let's take an example. So, we have a function that does nothing but like displays a user profile. Okay. So, say for example, any social media application that you're using, you have a function to show profile. Okay? So, now what this function is going to do is it is simply going to print displaying profile. Okay? Simple enough? Now, when we call this function, so say I'm going to call show profile, what is it supposed to do? It is supposed to print this particular message, displaying profile. Okay? Now, what I'm going to do is in this particular function, I want to also print welcome to my profile, and then I want my function to display the profile. Okay? So, now one easy way to do it is just add one line of statement, that is welcome to the profile page, and then we're going to show the profile. Right? So, this works perfectly fine. Correct? Now, this is simple because we have one particular function over here. Now, what if we have 50 different functions and I want to add the same code everywhere? That is again going to be a very tiresome task, right? So, this is where decorators come in. Now, let me go ahead and create a decorator. So, I'm going to say it welcome decorator because it is a decorator function and I'm going to pass my function. Okay, so I'm just going to write funk over here. Now, I'm going to create a wrapper function. Okay, which is not going to take any inputs and then I'm going to say print welcome to the website. Okay? And now, I'm simply going to call this particular function that I have passed as an argument in my decorator function. Okay? And then, I have to return my wrapper. Okay, so let's look at it line by line. We have created a decorator function which is taking a function as an input. This is nothing but our show profile function. Inside this function, we're creating another function called wrapper which does nothing but prints the message welcome to the website. And then, after that, we execute our function that has been passed as an argument. And then, we return our wrapper function. Okay? So, now in order to execute this, in order to apply this, what we are going to do is, okay, let me just copy this over here. Now, I do not need this particular message since I have created a decorator function for this, right? And now, in order to call the function, what I'm going to do is I'm going to say at the rate welcome decorator. Okay? So, what this line is basically doing, okay, so let me just write it in the comments for you guys. Let me comment this out as well. Okay? Now, let me just explain the meaning of this particular line to you guys. Okay, so say I want to call my show profile using the decorator function that we have created. So, what I'm doing is I'm basically creating a variable show profile and then I'm calling the decorator function, right? And I'm passing my show profile function inside this. That is what this particular line means. Okay? So let me just go ahead and finally call my function over here. Let me run this. Okay, so I'm getting an error none type object is not callable show profile. Okay, so what I've done here is I have not indented it properly. Let me run it again. Okay. So you can see welcome to the website and then displaying the profile. Okay? I hope decorator is clear to you guys. What we're doing here is we are simply passing our show profile function inside our welcome decorator and we are storing it inside show profile variable. So I hope you guys are able to relate this particular syntax with the fact that we discussed everything in Python is an object, right? So we're basically passing our function inside a function and storing it in a variable called show profile using this particular line. All right? So whenever we use the syntax show profile, we are able to run the entire code, okay, along with our decorator function. All right? Let's move on to the next concept that is classes. Okay, so so far we have looked at the built-in data types in Python like list, strings, dictionaries, sets and so on, right? But Python also allows us to create our own objects using something known as classes. So you guys must have heard the statement that a class is nothing but a blueprint. Okay? So whenever we create an object or an actual instance, it is created from that particular blueprint. Okay, so let us go ahead and implement a class, okay? Say I want to implement a class student. Okay? So let me create a simple class for you guys where I am going to create an initializer function called init. Okay, so this is something that we are going to look at uh the next section. And I have name and I have age. So, I am going to initialize my variables. I'm going to say self.name equal to name and self.age equal to age. Okay, so this is basically uh how you initialize your function before running them. Okay, now I'm going to create uh two students. Okay, so my student one is going to be say Alice and age 22. Okay, so this is going to be student one. And then I'm going to have student two and let's say it's Bob and he's 24. Now, what I'm going to do is I'm simply going to print student one.name. Okay, so right now when I have given the input Alice and 22, I do not know that Alice is the name, right? But my class over here and my initializer function over here has defined that my first input or my first parameter is going to be name and my second input or my second parameter is going to be the age. Okay, so let me just run it and see if it's working. All right. So, over here I can see student one.name is giving me the output Alice. Okay, similarly I can go ahead and print student two. Say age. Okay. So, let me run it again and uh 24, right? So, this is how you can say that the entire student class is nothing but a blueprint and student one over here is the object. So, what do you think is happening over here as soon as we create this particular object? Okay, so as soon as we create our object, Python automatically runs this particular code. Okay, so it runs the init method, okay, and it specifies that the object is student one, the name is Alice and the age is 22. Okay, so this is what is called as soon as we run this particular line of code. Okay? Similarly, for this code, it specifies that the object is student two and the name is Bob and the age is 24. Okay? So, this is all about class and again, the initializer method is again a very important concept. Okay? So, let us also discuss the self keyword in brief. So, we already discussed that whenever we run this particular line of code, we are running nothing but this line of code, right? We are calling the init method and we are saying that our object is student one, our name is Alice and our age is 22. So, whenever we're referring to the self keyword, we're referring to the object. All right? So, over here, the object is student one in this case and the second time when we create this particular object, the self refers to the object student two. Okay? I hope this is clear to you guys. So, now that classes are clear, let's also look at the initializers and the dunder methods. So, dunder is nothing but double underscore. Okay? So, again, your init method is again a dunder method, but it is also known as an initializer. Okay? So, whenever an object is created, Python automatically runs some special methods, which are nothing but these dunder methods. Okay? So, as soon as we created this particular object, this particular dunder method was executed automatically, okay? Behind the scenes. So, we have already seen one particular dunder method that is init. It is also known as an initializer. Okay? Another dunder method that is again very popular. Okay, let me just comment it. Double underscore str. Okay? Again, we have a class student and we have the parameters as name and age. Okay? So, let us look at the str method. Okay? Double underscore str. So, this method basically displays how the object is represented or it basically controls what gets displayed whenever we create or whenever we print an object. Okay, so in order to see how it works, let me create the class student again. So, let me create the class student. Let me just comment this out. Okay, I'm going to define my init function first and then I'm going to just pass name as the parameter. Okay, I'm going to say self. name equal to name. Okay. And now I'm going to create my string method. So, I'm going to say str and I'm going to only pass the object because it does not require anything else, right? And then I'm going to simply return self.name. Okay. So, student name and self.name. So, let me create an object over here student and I'm going to call my class student and pass the name Alice. And now I'm going to print student. Okay, so I am going to print my object and let's see what's the output now. Okay. So, now you can see student name is Alice. Basically, this particular method is getting printed whenever I print my object. Okay. Was this the case over here as well? Okay, let me just comment this out. Let me show you guys the difference if we do not create or if we do not define a dunder string method in our class, let's see what the output looks like. So, let me just print the object over here. Okay, let's see what happens. Okay, so over here it is simply giving me information about my object, right? So, since we do not have a defined string dunder method unlike in the second example that we saw where we were being returned a particular string, that is the student name and then the value of the student name. Correct? But over here we're getting some object information. Correct? So, this is basically the use of a dunder method. Moving on to the next concept that is duck typing. So again, duck typing is one of the most unique concepts in Python. So let us understand this with the help of an example. So say you want to hire somebody to drive a car for you. Does it matter whether that person is a professor, whether that person is a doctor, or would you look into the fact that if the person knows to drive or not? And Python follows a very similar philosophy. Instead of asking what kind of object you are, Python only considers the fact whether an object can do a particular task or not. Okay? And this particular idea is known as duck typing and the name basically comes from the saying, "If it walks like a duck, treat it like a duck." Okay? In other words, we can say that Python basically focuses on the behavior of an object rather than its type. All right, so let us look at a quick example. Okay, let me go ahead and create a class dog. Okay, if you guys have noticed whenever we are creating a class, the first letter of the name of the class is going to be capital. Okay, that's not the case for a method. Okay, so this is a function or a method, and this can be in lower case. Okay, now I'm going to define a function called speak. And if it's a dog, I'm going to return woof. All right? Let me create another class called cat. And again, I'm going to return meow. Okay? Now, what I'm going to do is I'm going to create a function make sound. And I'm simply going to pass an animal. Now, I'm going to print animal.speak. Now, what I'm going to do is I'm going to call make sound, and I'm going to pass dog. Okay? Let's see what it does. Let me just run it. Okay. So you can see the output is woof. All right, so what's happening over here is in this particular function that we have defined, we're not specifying any sort of situation that if animal is dog, then I want to print woof. Right? We're not going for any kind of if-else condition over here. Correct? What we are doing is we are simply trying to make the animal speak. Okay, so if a particular class has the function speak, it is going to work for it. Okay, so since we passed dog, it is going to check whether this particular class has a function speak. If it does, it is going to return it. Okay, similarly we can do it for cat as well. Okay, so let me just run it. And now you can see we have the output as meow. Okay. So this particular functionality in Python is known as duck typing. Okay? If you want to go ahead, you can try it with the other animals as well. You can create more classes and you can try it out yourself. All right? Moving on to another important concept that is list comprehension. Okay. So say I want to create a list where I have the squares of all the numbers from 1 to 10. Okay, how can I do that? So there's something known as loops. Okay, so I'm going to use the for loop. Okay, first let me create an empty list. Okay. Now I can go ahead and say for I in range 1 to 11, I'm going to append my list and I'm going to multiply it with the square. Let's me print it. Let me see if it's working. Okay, that. So I have my squares from 1 to 10. All right? So this is one way of creating a list. Now what is list comprehension? So let me create a similar list using list comprehension. Okay. So as you can see over here, what I have done is I have defined what kind of value I want to be stored in my list, that is the square of my value, and then I have defined for range 1 to 11. I hope you guys are aware that whenever we are specifying a value, say in four range or even while indexing, the end value is never considered. Okay, so this is never going to be included. It's always excluded. So that means my loop is going to run from 1 to 10. Okay? So this is how you can create your list using list comprehension. So let's go ahead and try to create another list using list comprehension itself. So this time I want a list containing of all even numbers. So this time my input is going to be just I, okay? And I'm going to say for I in range, let's say I want it from 1 to 20. So I'm going to say 1 to 21, okay? But now I have a condition. I want only even numbers. So that means my input or my element should be completely divisible by two. Okay? So let me run this. I'm going to say even numbers list. Okay? So let me run this. And there you can see. My even numbers list contains all my elements from 2 to 20, right? 21 is not included. And anyway, 21 is an odd number. All right? Now what if I wanted an odd number list? Okay, I want you guys to try it out and comment your answers in the chat box. Okay, it's very simple. Let me do it for you guys. So I'm just going to copy this and I'm going to change a few lines of code that is just a little bit of logic that needs to be changed. Okay, so if it is not equal to zero, then I am going to print odd numbers list. Okay? So let me run this. And there you have all the numbers from 1 to 20 that are odd. Okay? So list comprehension is basically a compact way of creating list, okay? You can do it in fewer number of lines. Okay, so moving on to the next concept that is unpacking and multiple assignment. Okay? So first let's look at multiple assignment. Okay, so say I have three variables and three values. I can just assign them using commas. Okay, so let me just go ahead and print it. And I'm going to comment all of this out so that I don't get this new output. Okay, so I printed only A. If I want, I can print A, B, and C. And in order to make sure what is getting printed, I can simply say Okay, let me just print this. And you have A equal to 10, B equal to 20, and C equal to 30. Okay, so in whatever order you assign your values using comma separated values, it is going to be assigned in that particular order. Okay, so moving on to unpacking. Okay, so say I have a variable student and it has the following values. Okay, it has a student's name, age, marks, uh whether the student passed or failed, and the grades. Okay, now all these values are stored in a tuple and they're stored in this particular variable. Now, what if I want to separate the name, age, marks, whether the student passed, and the grade separately? I can do that using unpacking. Okay, now let me just print this. There, so we have our name as John. So over here, as you can see, we have assigned multiple values to multiple variables. We have done the same thing over here. Right? So we have comma separated variables and the student variable basically represents a tuple, which is again comma separated values. Right? So we are again assigning different variables different values based on the order. Correct? So the order matters in both these concepts a lot. If your order changes, the value that is being assigned to your variable is also going to change. Moving on, we have something known as F-strings. Okay, so say I have a variable name Okay, Alice, and I have a variable age as 25. Okay, now I want to print my name is I can just use a comma, then I can pass the variable name, and then I can say, "And I am my age, and then years old." All right? So, this is one way of printing some information and your variables together. Now, another way is something known as F-string, okay? So, if I want to do the same thing using F-string, I would have to do something like print F, and then use quotes. "My name is", and then whenever I'm passing a variable, I am going to use curly brackets. Okay? So, this is how this particular function gets to know that this is a variable and not just another sentence. Right? So, let me print this and then So, we have the same outputs. So, whenever you're involved in a more professional coding environment, F-strings are a more preferred method compared to the comma-separated uh printing, or say even the format, the percentage format method, because these are more readable and give you much more control over formatting. Okay? What I mean by when I say much more control, uh it's that you can also perform calculations inside your F-string. Okay? So, let us quickly see how that is done. Okay? So, say I have a variable called price, and then I have a quantity variable, uh which is five. So, I can simply say, print, and use the F-string method. The total cost is and then simply calculate both my variables, or multiply both my variables inside the print function itself. Okay? And you get the output as total cost is 500. Okay? So, again, it is a very good practice to use the F-string method whenever you are trying to print some information and your variables together. Okay? All right. So, the next and the last concept for this particular video that we will be looking at is if name equal to equal to main. Okay. So, this is also one of the most common lines that you will come across once you start coding in Python. Okay. So, in order to understand this, first you need to understand that every Python file automatically gets the variable name. And Python assigns a value to this particular variable name depending on how the file is being used. Okay. So, if you run the file directly, this variable is going to get assigned the value main. Okay. But, if the file is imported into another Python file, this particular variable name gets assigned the name of that particular file. Okay. So, how this is done? Let us quickly go ahead and see. So, let's make it more realistic. Say I have built a function or say a method to calculate. Okay. So, let me name it calculator.py. Okay. So, this is my main file. Okay. So, let me just go ahead and print Okay. is the name of name variable. Okay. So, I'm going to run this as the first line of code. Okay. So, let me just run this. Okay. So, over here you can see main is the name of the name variable. Okay. So, because I have not imported anything in this file. Okay. So, let us not go into the definition. Let us just see how it is implemented. Okay. So, let me go ahead and create a function add A and B. And let me just return this. And I'm going to say if name equal to equal to main, print this file this code is running directly. Okay. Now, let me save this. Let me run this. And you can see name is the name of my name variable. And since this condition is true, I'm going to get this as the output. This code is running directly. Okay. Now, let me go ahead and create another file, say app.py. Okay. Now, in this particular app.py file I am going to import calculator. Okay, and now I am going to print that I am inside app.py. Okay, so let me run this. Now you can see calculator is the name of the name variable. This time it is not main and I am inside app.py. Okay, so I hope this was clear. So since we imported a particular file in another file, this time our name variable gets assigned the value of that particular file that is calculator. Okay, so this time it is not main. So since the value of our name variable is not main, this particular line is not getting executed because our condition is false. Okay, so say I had named it calculator. Okay, now I run this. Now over here this condition is not true because in this file my name variable is getting assigned the value of main. Okay, now if I execute this particular file that is app.py, now I can see that I'm getting the output as this code is running directly because my condition is being met. Okay, over here my name variable gets assigned the value calculator. That is why this particular line of code is being run. All right, so that was all for the most important Python concepts before you actually start coding, especially if you're a beginner. I hope you guys found it helpful. Thank you and see you in the next one.
Original Description
🔥Enroll for Python Course: https://intellipaat.com/python-certification-training-online/
🔥𝐁𝐨𝐨𝐤 𝐲𝐨𝐮𝐫 𝐅𝐫𝐞𝐞 𝐌𝐚𝐬𝐭𝐞𝐫𝐜𝐥𝐚𝐬𝐬: https://forms.gle/g5tExa7e54xpYZW97
#python #importantpythonconcepts #pythonforbeginners #pythontutorial #trending #pythonfordataanalyst #learnpython #intellipaat
➡️ About the Course
This Python course provides a thorough introduction to Python, a high-level, general-purpose dynamic programming language. The course covers data analysis, scientific computing, data visualization, web scraping, database interface, and essential programming principles such as object-oriented design and multithreading.
📌 Do subscribe to Intellipaat channel & come across more relevant Tech content: https://goo.gl/hhsGWb
▶️ Intellipaat Achievers Channel: https://www.youtube.com/@intellipaatachievers
📚For more information, please write back to us at sales@intellipaat.com or call us at IND: +91-7022374614 / US : 1-800-216-8930
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
Stop Watching Python Tutorials. Automate These 7 Boring Analytics Chores Instead.
Medium · Data Science
A Fairer Champions League Draw — Without Losing the Magic
Medium · Data Science
Data Science Didn't Make Us Smarter—It Made Us Stop Trusting Our Intuition
Medium · Data Science
German-Japanese researchers invent electricity-free tech that could cool datacenters
The Register
🎓
Tutor Explanation
DeepCamp AI