Python Tutorial For Data Domain [Advanced Guide - Beyond Basics]

Ansh Lamba www.youtube.com

Brief Summary

Alright fam, this is a comprehensive 7-hour course on Python beyond the fundamentals, tailored for data professionals. It covers essential topics like OOP, inheritance, polymorphism, decorators, multithreading, schema validation, async Python, API interactions, testing with PyTest, and the OS module. The course uses doodle graphics for easy understanding and is structured chapter-wise for step-by-step learning.

  • OOP concepts, classes, and objects
  • Inheritance, polymorphism, and decorators
  • Multithreading and asynchronous Python
  • API interactions and PyTest
  • OS module for data loading

Introduction

Python fundamentals are just the beginning. To land a job in the data domain, you need to know more than just loops and functions. This course covers advanced concepts like classes, objects, encapsulation, inheritance, polymorphism, decorators, multithreading, schema validation, asynchronous Python, API interactions, PyTest, and the OS module. The course is structured in chapters and uses doodle graphics for easy understanding.

Pre-Requisites

To continue with this course, you should have fundamental knowledge of Python, including how to write if-else conditions, for loops, and functions. The course agenda includes OOP concepts, classes, objects, decorators, inheritance, the OS module, polymorphism, multithreading, asynchronous Python, APIs, PyTest, JWT, query parameters, and gather core routines.

OOP Python Overview

OOP with Python is essential for building production-level projects. OOP promotes code reusability, scalability, and readability. While functions and variables are building blocks for reusability, OOP allows you to create bundles (classes) of related functions and variables, making code more manageable. Classes offer scalability and readability, allowing you to add functions without disturbing other parts of the code.

Classes & Objects

To create a class, use the class keyword followed by the class name. Inside the class, bundle related functions (methods) and variables (attributes). Methods always include self as a parameter. To use a class, create an object of that class. This allows you to reuse the code multiple times.

Constructors

Constructors are used when you don't want to use the default values provided by the class owner. To use constructors, add a special function __init__(self, ...). This function is automatically triggered when an object of the class is created. Inside the constructor, make the variables available throughout the class by creating self variables.

Encapsulation - Access Modifiers

Encapsulation is achieved in Python using access modifiers: public, private, and protected. Public variables can be accessed by anyone. Private variables (denoted by double underscores __) cannot be accessed outside the class. Protected variables (denoted by a single underscore _) are a warning to developers not to change them.

End-To-End Python Data Example

This section demonstrates creating a class for data extraction, allowing users to read any file format. The class includes methods for fetching CSV, JSON, and Parquet files. The fetch_text method uses a separator argument to handle different text-based file formats. This class promotes code reusability and scalability.

Class Methods vs Static Methods

There are different types of methods: instance methods, class methods, and static methods. Instance methods are the default methods that interact with the instance. Class methods interact with class attributes and are defined using the @classmethod decorator. Static methods are independent of the class and do not require self as a parameter; they are defined using the @staticmethod decorator.

Inheritance in Python

Inheritance allows you to reuse code from existing classes. When you inherit a class, you can extend it with your own methods and attributes. This avoids writing code from scratch every time. The Python community is very active, and you can inherit classes from public repositories.

Single Level, Multi-Level, Multiple Level

There are different types of inheritance: single-level, multi-level, and multiple. Single-level inheritance involves one parent class. Multi-level inheritance involves inheriting from a class that already inherits from another class. Multiple inheritance involves inheriting from multiple classes together.

Polymorphism in Python

Polymorphism means "many forms." It allows you to create objects with different behaviors based on their class. For example, you can have different classes for fetching data from APIs, databases, and S3 buckets, but all have a fetch method. This promotes scalability and code reusability.

Decorators

Decorators are a way to modify functions without changing their code. They are used to add extra functionality to functions. A decorator is a function that takes another function as an argument and returns a new function. Decorators are used in modern frameworks like Apache Spark and Apache Airflow.

Multi-Threading

Multithreading allows you to run multiple tasks concurrently within a single process. This is useful for IO-bound tasks, such as making API calls. Instead of waiting for each task to complete, you can use multithreading to utilize the thread fully. The ThreadPoolExecutor is a modern way to implement multithreading.

Pydantic For Schema Validation

Pydantic is used for data validation and parsing. It allows you to define a schema for your data and validate that the data conforms to that schema. Pydantic promotes type hints and strict mode for data validation. It also supports serialization and deserialization.

Async Python

Asynchronous Python allows you to write code that can perform multiple tasks concurrently without blocking the main thread. This is useful for IO-bound tasks, such as making API calls. Asynchronous Python uses coroutines and event loops to manage tasks.

Multiple Coroutines With Gather

To run multiple coroutines concurrently, you can use asyncio.gather. This allows you to fetch data from multiple APIs asynchronously. The event loop manages the threads and ensures that they are not sitting idle.

APIs Overview

APIs (Application Programming Interfaces) act as intermediaries between clients and servers. They allow you to access data from databases and other sources. APIs have endpoints, which are URLs that you can use to make requests. APIs use different methods, such as GET, POST, PUT, and DELETE.

Fetch Data From APIs

To fetch data from APIs, you can use the requests library in Python. You can use the get method to make a GET request and the post method to make a POST request. You can also pass query parameters and headers with your requests.

PyTest

PyTest is a testing framework for Python. It allows you to write test cases for your functions, classes, and utilities. PyTest uses assert statements to check if the code is working correctly. You can also use parameterized testing to write more efficient test cases.

OS Module

The OS module allows you to interact with the operating system. You can use it to create folders, create files, read the content of a folder, and perform incremental data loading. The OS module is essential for data engineers who need to automate data pipelines.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the App Store
Get it on Google Play

Transcript

YouTube Transcript Generator

0:00 Bro, Python fundamentals is just the starting point. But if you just want to land a job in the data domain, you need to learn much more than just the fundamentals because data professionals do not just write loops, functions, if conditions and ship the code to production. We build utilities. And do not worry that is why I have created this complete 7 hours course which will teach you everything that you need to know beyond fundamentals such as classes and objects encapsulation constructors multiple level and multi-level inheritance polymorphism decorators for declarative utilities parallel orchestration with multi-threading schema validation with identic modern asynchronous Python fetching data from APIs query parameters and tokens writing

0:48 test cases with pyest working with OS module for incremental data loading. Wow, that is exactly you need to know in Python if you are in the data domain. This is a complete package and do you know the best part about this course? The entire course is structured in the form of chapters so that you can just make progress step by step and you can actually track it and not just this you will learn everything using handmade amazing doodle graphics so that you can easily understand the complex concepts as well. And you know what? I have literally received so many requests to create such a Python course which discusses a lot about Python beyond fundamentals. So I decided to create a free course on YouTube so that everyone every single data family member can

1:34 learn from it easily. So please hit the like button and hit the subscribe button if you want to support my hard work. And now let's get started with this amazing Python course. So what's up? What's up? What's up fam? Happy Sunday and now finally now you will learn Python beyond fundamentals because I know that you have fundamental knowledge in Python because everyone has the fundamental knowledge in Python if you are in the data domain everyone knows that but now it's finally the time for you to learn the advanced concepts not like basically advanced because see if you will ask me I will not consider it as advanced I will still consider it as non-negotiable because when you are in the data domain you have

2:21 to know something in Python which is beyond fundamentals which is beyond loops functions and those if health conditions obviously okay so yes now finally this course is here and we will be just talking about a lot of things in this particular Python advanced course let's quickly see the agenda like what exactly we're going to cover before that let's cover the prerequisites so if you have fundamental knowledge in Python let's Say you know how to write if else conditions, if you know how to run a for loop, how to write a function, then yes, you can continue with this course. Okay, let's see the course agenda. So course agenda is wow is very detailed. So first of all, I'll be just looking at oop concepts which is the non-negotiable classes and objects. Then we'll be

3:06 looking at decorators. Then inheritance, multi-level versus multiple inheritance, OS module, polymorphism, multi- threading pyche wow asynchronous APIs, piest. By the way, we are not building APIs. We will be learning how data engineers or data professionals use APIs or you can say work with APIs. Then pyest, JWT and query parameters, gather core routines and and and much more. And you know what? I think I should just show you our whiteboard as well. So this is everything that we are just going to cover in this video course. So as you can see everything is detailed. Everything is like discussed in detail. Okay. And we'll be just looking at everything with codebase as well through

3:51 this visual graphics so that you can just understand everything. So everything is there for you. You just need to now sit and just decide that I will learn Python which is beyond fundamentals. Okay. And I can also quickly show you the code base as well for this particular course which is this one. So we have created chapters as you can see here chapter 1 till chapter 9. So everything is there for you so that you can just go through step by step chapter by chapter do not need to rush all at once simply grow step by step simple. So this is everything about this course and I hope that you will like the course content. So this is the agenda. So yes we are learning a lot of things. Why slam a lot of things? Because see you are in the 26 era. Okay. And it is

4:38 like almost almost um one quarter is already passed. You need to learn a lot of things. AI is here. Competition is here. Layoffs are here. Everything is here. So you need to learn a lot of things. You need to simply be competitive. And there's only one solution to it. Learn. That's it. I do not know about those smart things. I simply know if you just want to grow, simply learn. Simple. Okay, I do not know any kind of tips and tricks. I do not know any kind of hacks. I simply know if you want to grow, if you just want to grow in your domain, learn simple. That's it. So this is what we are going to master in this particular video. And now let's get started. And before starting, I want to just congratulate my laba family members. And yes, I feel really really happy in this layoff era when everyone is like laying

5:24 off thousands 30,000 people together. My dataf fam is standing strong and not just standing strong they are actually cracking offers maybe from big four companies um man companies top financial institutes banks MNC's whatever very well done so let's congratulate all let's feel happy let's spread positive vibes right so congratulations congratulations congratulations congratulations congratulations and and and and and congratul ratulations. Wow. So very congratulations to everyone. Okay. I'm really happy for you all who have just cracked the offers and these are the offers that we have received in the

6:10 past few months and the ones that you saw in the beginning are the latest ones. Okay. So I just keep on adding new and new um offer letters. And yes, your name will be here soon if you will just learn and if you will not focus on tips and tricks. If you if you will simply learn learn upscale upscale upscale and that's it. There is no end of learning. There is no concept of learn this much and that's it. No no no no no no no at all. Not at all. Okay. So simply learn as much as you can. You need to be competitive. Competition is growing rapidly rapidly. Okay. So now at the end um this is me and we are just going to learn about Python and let's get started. Okay, let's see. So now let's get started with

6:56 this Python advanced course and the very first topic that I want to talk about is oop with Python objectoriented programming because this is that programming that developers need to know and not just developers I would say even beginners who have just started with programming or anything because oop is like very basic thing. If you are building anything in the production level um project, you have to work with oop because you cannot work without it and okay okay why do we why do we need to use oop let's answer this question then only you will be able to feel its importance and then you will actually find uh it relevant for your learning okay so let's say you will say why do we need to use oop I will if I just want to answer this in just one word in just one

7:41 word I will say read use ability of code but you will say an Lamba do you know what you actually do not know anything um we do not need to use oop with Python we can actually use something called as functions okay okay let's say you will say we can actually use functions and this is a better approach so let's say if I want to or let's say if you want to write any code which can be reused by anyone right so you will simply say an I will simply create functions and I already know how to create functions so I am already good. Very good. Slow collapse. So now you are actually right to be honest. You are actually right. You can just create functions for reusability of code. That

8:29 is the building block of the reusability concept. You are absolutely right. But let me just ask you one question. You created this function. Yes. Okay. Can I use it? Let's say there are users. Yes, they can use it. Yes. Anyone can use it. Perfect. Okay. Um, do you just need to create one function in the organization? Um, obviously no. How many functions can you expect in your organization? At least 100. At least 100. Okay. For just one you can say feature. Okay. At least 100 functions or at least let's say 20 50 at least. Okay. So let's say hundreds of functions are there. Let's say 1 2 3 4 and so on. So many functions. That's a dot dot dot dot 100

9:17 of functions. Oh, okay. Now along with these functions, we'll also have some variables. Yes. Because variables are also building blocks for reusability. That's why we create variables. We just define variable once and we reuse it multiple times. Okay. Okay. So, let's create variable as well. So let's say we have one variable and variables will be at least 10 10 more times. It will be like up to thousand if we are just considering 100 functions. Makes sense. 1 2 3 and up to th00and makes sense. So you have this much of code and you are absolutely right that you create functions and variables for reusability. Actually the concept of

10:04 reusability start starts with the functions and variables. That is true. But now tell me one thing. Can you manage hundreds of functions? Can you manage thousands of variables? Let's say you you yourself forget about others. Let's say you are the sole developer in your organization. That will be like very rare case. But let's say you are the only developer in your organization. Now you want to use let's say 10 of functions which are similar which are um you can say grouped together to perform a specific task. Okay, it can be like anything. Let's say data extraction there are some 10 functions and those 10 functions you need to just use it. Can you just find those 10 functions among 100? It is possible but you would spend a lot of time and still you can make some mistakes. Um possible yes very

10:51 good. Um now just think about variables. You can create variables with the same name as well. Can you? Not at all. You cannot create variables with the same name. Okay. But let's say because of your use case you have to create the variables with the same name. You cannot create it. So for each variable you have to pick a different name and you have to remember thousands of variables. Which name did you pick? For which variable for which task? Oh, so now your reusability of code is actually becoming a messy network H okay this problem is solved by oop objectoriented programming so what do we

11:37 say what do we say hey you are a developer you just created all of these things it's very good but now I will give you a magical thing which is called objectoriented Pro programming and with the help of which what you can do you can literally create a bundle you can create what bundle so instead of creating independent functions now what you will do you will create bundles let's say bundle number one let's say bundle one bundle one and this bundle one is actually responsible for data extraction task make sense okay data extraction So what you will do you will simply put together all of the functions which are

12:24 related to data extraction. So let's say function one and function two and there are some variables as well which are related to only this task. Okay. Okay. Makes sense. Let's say some variables are also here. Let's say this variable and this variable this variable and let's say one more variable. Let's say this one. So these are the things that we just need to create a bundle. So now I will name this bundle let's say bundle extract. So now tell me one thing. Now if you want to do something with data extraction do you need to go to these functions? Not at all. Now you simply need to go to this bundle and you can

13:10 literally use the functions created inside these and variables created inside these. Wow. Similarly, you can have another bundle which is called let's say bundle transform. Maybe you are or let's say data writing. Okay, you are simply writing some data. So, it will like data writing. And now if you want to perform anything related to let's say data writing, you can literally go to this bundle. So now your functions are not floating in the air as an independent functions. Now they have a goal. Now they have a direction. Now they know why are we created. These variables are also isolated. That

13:55 means if you just create a variable in this bundle, you can also create the variable with the same name inside this class as well. Basically this bundle as well. Wow. And in the technical terms, we simply call this bundle as a class. This is the backbone of your oop class. In technical language, this bundle is what? Class. Make sense? Just remember this thing. Class. This is nothing but just a class. Okay. So within the class, we simply say, bro, you still need to create functions. You still need to create variables but with a different style so that you can just bundle together your code and you can just manage your

14:41 functions and and variables in a better way and with the direction that you need to just solve a problem or basically do a specific task. Wow. Make sense? This is the reason why you need to use oop. Does it just promote reusability? Does it just promote reusability? It is like the major goal of it. But it also uh you can say um offers something called as let me just write it here. Scalability. Scalability is the thing that you will realize when you will build something in the organization. Scalability. Let's say you want to add one more function in this

15:26 just bundle. Okay. Now you do not need to touch anything else. You simply need to add something here and you can scale this bundle alone without disturbing any other bundle without disturbing any other class. Scalability is very very easy. And what about readability which is the you can say heart of the coding because you will not be the only person using your own code. There is a dedicated team who will be using your code. So if your code is not readable, you are not actually a good developer. Yes, good developer doesn't mean that you can just write code. Good developer means you can write the code which is readable which others can also

16:11 understand because you are a contributor. Make sense? So readability and do you know what if you do not promote readability while writing the code trust me you yourself will not be able to read your own code after let's say a month or two. I'm so sure about it. Make sense? So these are all of the benefits that we get that we get with oop and this is the reason that why do we use oop in python at all. I hope it makes sense. Okay. So what is this class? If I if if anyone ask you like what is a class? What is a class? Simply say that this is a bundle or basically this is a way to create a bundle of related functions and variables. Simple. Very good. So now let me write it for you. It is a bundle of similar

17:02 functions and variables. Simple. Now what will happen? Now you will say an lamba um when we create functions we 99.99% of the time create arguments as well. Okay we create arguments as well. So what will happen to these arguments that will be taken care by the object of the class. So now what will happen? Let me just show you that as well. So let's say if someone else let's say this particular developer let's say there is a new developer and this developer wants to use this particular class. So do you know what this person what this person will do? This person will simply create a kind of object. This will simply say okay I want to just use your class that you have already

17:48 defined but I want to pass some different parameters obviously common uh you can scenario so this person will create a kind of object okay let's say object now this object can have different parameters and this person can simply do something else wow this is the backbone of it. If you just want to pass different parameters, do not worry. This is just like conceptual overview. You will just see the code as well. Do not worry. But you first need to understand why you are just building a class. And later on, you will also see the power and magic of inheritance. Okay? But for now, you can simply um assume that these features are

18:35 just the tip of the iceberg because we get so much so much so much of things with oop. Okay. So now, yes, we going to master OP. Do not worry at all. I will just teach you everything in OP that you need to know and it will obviously help you uh like almost almost everywhere whenever you're just writing code with Python. So do not worry at all. So now our goal is clear why do we need to learn OP at all. Okay. So now let's get started because your concept is strong. Now you just need to see the code. You need to build the things you need to break the things only then you will learn. Okay. So now what I will do? I will simply get started. So in order to get started, I know this is like common sense thing but still I want to just tell you that you would need um Python

19:20 in order to get started. Simple. So you would need Python and one more thing which is I would say you can add VS code editor as well or any code editor of your choice. It can be VS, it can be anti-gravity, it can be cursor, anything. Okay, but I will use VS. So it's your choice which VS code editor you want to use. Simple. So just quickly download these two things. And if you do not have these two things, by the way, you should have because this video is obviously you are watching because you already know a little bit about Python. Obviously you have some fundamental knowledge. But if you do not know how to just download these things, do not worry. I will just help you all. Do not worry at all. So

20:07 let me just quickly show you. Go to any browser and simply search Python download and you will land on this page. Click on downloads and do not click on this one because it will install 3.14 and it is in the bug fix you can say zone. So you can see some breaks. So simply go to downloads and go to Windows or Mac OS as per your choice. simple and you will you can just download any um installer but I will just simply recommend go with 3.12 or 3.11 because these two are in the green zone. So yeah simple okay download it and that's it. So now second thing is you can download VS code editor. Simply go on browser search VS code download and click on download VS code and pick your own machine. Simple. Why do we need

20:54 VS code editor? By the way, we can write code in um Notepad as well. But I don't think so if anyone writes code in Notepad nowadays. But yeah, if you want to, you can go ahead. All the best. Okay. So let me just open my VS code editor quickly. So this is my VS Code editor. It might look different than you or you can say than yours because I have installed some um extensions and all but code editor is just a code editor. Okay, do not need to worry at all. Now just go to this option file and click on open folder and you need to open one empty folder. If you do not have any empty folder, please create one. If you already have one, simply open it. But just make sure that folder is empty. So I have also created one folder called

21:41 Python advanced. Okay. And this is as you can see it is empty. Nothing is there because we need an empty folder. So you can also open it and make sure you have nothing here. Simple. Very well done. Now we will open the terminal. So simply click on these three dots. Click on terminal and new terminal or simply hit control shift and tick till day sign or command shift till day in your Mac OS. So this is my terminal. So first of all I will check my Python installation. If it is fine I will say Python minus minus version. So mine is 3.11.9. You can just use any of your choice. And I think in Mac you

22:26 just need to run the commands with Python 3 and that's it. Everything else is same. And here I will simply create one. Um should I create that? H yeah let's create it. So first of all I will create Python virtual environment. Why do we need to create Python virtual environment? Because see your Python is installed in the global environment and I might download some um you can say modules Python modules. So I do not want you to install any of the module in the global environment. You can download it. It's not a big deal at all. But whenever you work on any project you simply create the Python virtual environment. What is virtual environment? Basically it is a kind of isolated environment. So what will happen? Let's say this is your global environment. Okay.

23:12 And you have one global interpreter simple. So what will happen? We will create a virtual environment which is an isolated environment. Okay. And then inside this isolated environment I will also get one interpreter which is just pointing to this one. But it is a different one. This is a different this acts like a different one. So whatever package I install inside this let's say I'm installing pandas or whatever numpy whatever whatever. Okay. So this will be only installed in this particular package and not in the global one which is a good practice. So the moment you download um delete this folder the moment you delete the virtual environment all of the packages are also deleted. You do not need to worry about global environment. Simple very good. So how we can just create virtual environment in python. Simply say Python

24:00 and then say minus M which is module then when virtual environment then name of the virtual environment hit enter it will create virtual environment here if you are on Mac uh what is the thing keyboard interrupt I think I just press something uh run as module let me just delete it by the way it is created but still I just want to make sure it is fine by the way I just use UV nowadays But you don't need to worry about UV. Then you will say what is UV? No, nothing nothing. Python minus M when dot when okay. So now now if you are on Mac OS then what you can do? You can simply say Python 3 and then minus M when it will work fine.

24:48 Simple. Now we need to activate the environment. How you can just activate the environment? And let me just show you the Mac users as well. Do not worry at all. So create u python vn mac. So let me just tell you the exact command don't worry. So yeah see first of all check the version and then it is already installed obviously. See python 3 minus m vim and you can also add vnv which is a industry standard but go with vim if you just want to. Okay. Now we will be just activating the environment. So all the Mac users you can simply run this command source when bin activate it will activate your virtual environment and if you are on Windows you can simply run this command dot VNV then

25:33 scripts and then activate simple hit enter and you will see um colorful sign here even if if you do not see colorful sign you should see dot when this will be an indication that you are using virtual environment so now onwards if you just create or let's say add any package it will be downloaded on your virtual environment and not on your global environment. Simple very well done. These are some you can say boilerplate thing but you have to do it because this is very important for the code management. Right. So now what we need to do we need to simply get started with the oop. Okay. Simply close the terminal because our virtual norm is created. I will add one folder and I will say chapter one. Oop simple because I'll be going chapter by chapter so that you do not need to worry at all like

26:19 which folder to refer first. Plus you can also refer this particular repository or basically code base as your source of notes as well. You can just keep it as a reference. Simple. Good. Now let's create our very first Python file. So I will simply say um first basics py. Okay very good. So now I will just showing you how you can just create a class. Okay. Are you excited to create your first class? You should feel excited bro. What are you doing? So first of all you will write class keyword in the function. We simply write df. Okay here we write class and then we write class name. Let's say I want to create a class. Let's say my class.

27:05 Okay, because this is like very first class. So let's go with a very very simple one. And bro just snooze for 5 minutes. Let me just write my own code. So now class we will just use it when we have some fundamental knowledge because then you will be not confused when you will see the you can say recommendations by the code. But okay. So this is by the way if you are simply wondering like how to just own it because this is a kind of VS code feature you can just enable it by going to this particular button. This is a kind of GitHub copilot code suggestion that you need to enable it. So yeah if you are simply wondering how you are just getting the code suggestion. So class and then my class within this class now I just need to bundle all of the resources that I just

27:50 want to create. Simple very good. Now let's say forget about this class remove it. Let's say I have one variable variable one and its value is let's say an variable two value is let's say lamba simple and oops and let's say variable one variable two and I have one function which what do you want bro? Yeah. So let's say I have a function. Let's say function one. And this function one does nothing. Simply prints hello world. Okay. And function two. And let's say this is the thing hello

28:37 globe. So let's say these are the two function. These are the two variables. And similar to these we have hundreds of variables and hundreds of functions. But now we just need to create a bundle for just these two functions and these two variables. Right? This was the requirement. Now what I will do? I will simply create a class. And I will say class and I say my class. And now do you know what? I will simply put all of these things inside this class. Yes. Like this. Indentation is very important in Python because it interprets your code line by line. Python is not a kind of um you can say low-level language. Python is an interpreted language. Your code will be converted into a bite code and then bite code will be interpreted line by line. So just make sure indentation is there.

29:24 Okay. So this is your class. Yes, that's it. Yes, that's it. That's it. That's it. Now this class is converted. Um basically now this class is created. But one thing is left. What is that thing? So now whenever we create a function whenever we create a function in class we always you we always use something called as self as a parameter in all of the functions. Okay but why? Because this is not a normal function. This is called method. Oh inside a function we do not call a function a function. We just call a function as a method. So for method we

30:10 have to use self parameter self parameter otherwise it will not work. This is the non-negotiable. Simple. Why do we use self parameter? Don't need to worry. We will be just discussing that as well. But for now just remember that you need to use self parameter. And inside the class your function is called as method. Okay. And your variable is called as attribute methods and attributes. Just the renaming but the concept is exactly same. Make sense? But usually there are so many things that are also considered as attributes. So if you want to call it as a variable, class variable, it's still fine. But you should call a function as method for sure. Simple. So

30:55 I can write class variables. Simple. And it will be like class uh methods simple. Okay. Very good. By the way, class method is like a different thing that we are just going to talk about very very soon. Class space methods. Okay. It is not attached class method. Class method is like something else. So let me just remove it because you'll be confused. Simply say class variables and these are the class methods. Okay. So this is my class object and now what I will do I will simply first of all snooze it and then I will just create an object out of it because if I just run this Python file it will work fine. See and obviously nothing is there because I didn't print anything. Now let's say an Lamba you said that we can just reuse this code.

31:41 Yes you can. So let's say we want to call this function then how I will just call this function. So in order to use any um object of um class you first need to create the object itself right so what you will do let's say create python object simple so now what I will do I'll simply say obj equals my class and that's it now if I just call any function obj dot function one then it should return that particular function. Simple. So let's say I just call this function and I run this, I will get hello world. Wow. So what did I do? I created an object of my class and

32:27 then I just called this particular function. Okay. If I just create a new object, I can create as many objects as I can. That is a power of it. That means this class can be used by infinite number of objects. I can also say let's say OBJ2 equals my class and then I will say OBJ2 dot function two let's say simple and if I just run this you will see hello world hello globe that's how you just create objects from your Python class this was like a very very very simple example but still you grow step by step right even if you know this particular simple example it's fine but there are some people who do not know right so we just need to help every single one and it will be like good

33:13 revision for you as well. I hope it would be. So this is like very basic class. Now this class is not something that we just create normally because it is not actually very dynamic. It is very static because see I am using the variables that are created by the class creator that is like me or you. I am simply using the functions which are created by the class owner like you. But I do not have any control. I cannot make any changes. I have to rely on your logic. Which is good. Which is good. Which is still good for static um you can say use cases. But what about dynamic use cases? H what about dynamic use cases? So that means you need to pass some arguments for in your classes.

34:02 You need to pass some arguments in your objects. Are you getting a point? So let's say you are creating this object. Okay. So you are creating this object and this object should be able to make some changes inside this class. Oh and these functions should also rely on those particular variables so that it can also behave differently for each particular you can say object. For that for that we can use something called as constructors. Okay. So if anyone asks you why do you use constructors in Python? Simple. If we do not want to use default values provided by the class owner, we want to

34:48 assign our own values at the object creation time. We use constructor. Simple. This is the best way to remember it. Okay. So let me write it here as well. So constructors. So with Python. So this is like heading and within that we have a special section called which is called let's say yeah so let's call it as constructors simple remember. So we use constructors when we do not want to use default values. Simple.

35:33 Good. Now how do you use it? How to use constructor? It is very simple. We always create constructors like this. So let's say this is your constructor and you want to create a constructor for your class so that other people other developers can make the changes as well. Right? So you just need to add one special function and it is called as constructor function but you can still remember like special function which looks like this in itself self and like this. Wow. What is this? This is a function that you create and obviously with df simple this is the function that you create when you write this code this df1. Okay. When you write this code then

36:18 your class will be able to perform sp some you can say um modifications at a runtime at the time of object creation. Okay. So let me just show you that as well. So now what what we will do we will simply create a new class. Okay. And let's add one more notebook constructors. Perfect. So here I will simply create a class and let's copy and paste the previous example. So here what I will do I will use these class variables obviously but here like these are the let me just snooze it. So these are basically the variables which

37:04 will be constant for all of the functions but there are some some dynamic variables. So I will simply say dynamic one dynamic two like this. Right? So what I will do I will create a kind of constructor like this in it and self. Now within that what I need to do whatever dynamic variables I need to um allow to developers like what variables they can just pass at the time of object creation they can use the variables from here. So I will simply name it as like the let's say dynamic 1, dynamic 2 or let's say dynamic three. Simple. So these are the variables which

37:50 will be required every time now whenever someone will be trying to create an object of this class. So in the previous example you created the class without any parameter. Right? This time you have to pass these three parameters. Oh, okay. That makes sense. So then you also need to do one thing. So you created these you can say parameters here. Yes. And you ask the user to pass these variables. Yes. How how this function and this function and all the other functions will be able to use these arguments because let's say requirement is like this hello world and then you just need

38:35 to pass it like this. Let's say um fringing and this is the variable which is let's say dynamic one. Okay. And this is like again Python fundamentals f strings and all when you just combine variables with string you use f and then you add curly braces around the variable dynamic two or let's say there's one more function which is dynamic 3. Simple yes. So now how we can just use these variables the answer is in the constructor only. So what you need to do now you will make you will make these

39:21 variables available throughout your class. Do you know how you will create self variable. So you will write self dot dynamic 1 equals to dynamic one. That means the value you are getting from the user. Similarly, self dot dynamic 2 equals to dynamic 2. Then self dot dynamic 3 equals dynamic 3. Okay. And I can just use these values like this. Simple self dot and self dot. What did I just do? I simply made these variables available in the form form of self because these functions do not understand this

40:07 language dynamic one, dynamic 2, dynamic 3. No, they simply can use anything throughout this class using this self variable. Oh, that is why we pass this self object. See, so that we can use anything which is available inside the class. If you do not use self, you cannot refer to any of the object. And by the way, it will throw the error as well. But still, that is a primary use case. So that you can just use a shared resources, shared um you can say variables. Okay, make sense? And these variables are also called as instant variable. Instance variables. Why? Because whenever you create an object, what is object? an instance.

40:54 Whenever you create an instance, you define these variables. These are called class variables because you define it at the time of class creation. Wow. Wow. Wow. Wow. Wow. Let's try to create an object for this. So, you will simply say obj and let's say my class. And this time you have to pass the three variables. What if I do not pass anything? What if? Okay, let's see. Let's see. Error. It is saying missing three required position arguments. Dynamic 1, 2 and three. It is saying bro where are your three parameters dynamic 1 2 and three. I need these three variables. So I will now pass it. Let's say I want to pass a b c d e f let's say xyz. Simple. Now if I

41:42 just call let's say any function obj dot um function two. And by the way, this should be also renamed function three. And I just want to call function two. Then if I just call it, it will work. See hello globe def. Wow. This way you create the functions with different values coming from the self variable. Make sense? I hope so. So this is the use of constructors. H by the way you will say an Lamba I have one very um curious question let's see if you have this question I created the object like this my class yes I

42:28 didn't even call this function in it why then it is asking me for these variables do you have this question because I had so now let me tell Whenever we call this class, okay, whenever we create the object of this class, this init function will automatically be triggered, automatically be triggered at the time of object creation of this class. So let's say if you are writing obj or anything equals to this my class, this function will be automatically triggered. Wow, that is why it is asking you to provide these values here. That is the you can say property that is the characteristic of um constructor. That

43:14 is why it is called constructor because it is it constructs your class. Make sense? Make sense? A little bit. This this much h this much. So this is the concept of you can say constructor. It is very useful and you will hardly see any class okay in the code that is written for production without a constructor. Without a constructor, you will never see any class because um people hardly create static classes. They always create dynamic classes. Make sense? So that user can just or developer can just let's say tweak their own parameters and they can just build um their own objects. Simple. Very good. Very good. Now you understood self as

43:59 well. So if someone asks you, hey just why just tell me why do you just add self? Now you simply have the you can say answer but let me just tell you you can even create functions without self. Yes you can even just create the self but we simply do it most of the time we simply do it. But now when we just create functions without self they they are just called special functions. We will just talk about it and we will also talk about like why do we need it. And by the way, just a pro tip. We hardly create those static functions without self. But still you should be aware of those functions. Simple. Now just me one thing, one more question if you have an Lamba. Yes, I called this function. Yes,

44:48 I called OBJ.Function 2. H. Okay, I got this. Yes, I can see one parameter self. Yes. Why I didn't pass anything here? Why? Just tell me all just just guess anything. Write write anything any any of your thoughts in the comments right now. So when we created the object of the class, we didn't pass anything, right? But we actually passed it but you didn't see it. What what does it mean? What does it mean? So basically whenever we create an object whenever we create an object there are two ways. Okay. So let's say if I'm just writing obj equals to my class and this this. So this time this obj

45:35 this one will be also passed as a kind of object like a self object. But really yes it will be like automatically passed as like a self object. It is like a behind the scene work. Okay. So even if you just want to pass it like this let's say let me just show you otherwise you will say bro you didn't show us. So let's say you want to call this particular function right now. Do you know what you can even call this function like this? Let me just show you. Let's comment it out. Let me just show you. So this is like one way of doing it where it automatically passes the self object into the um you can say um you can say argument automatically. But you can also call this thing like this. Let's say my class dot function two or whatever function

46:23 one. And you can pass let's say obj. What? Yes, you can also pass it like this. Wow. Wow. And then obviously you can just pass something. Let's say whatever parameter you just want to pass and you can define obj first of all obj equals to my class or let's comment out uncomment this one. So you can also pass it like this. Wow. This is like second way of writing it. But we usually do not write it like this. But I'm simply telling you where is the self keyword. Where is the self keyword? The self keyword is actually passed it like this. Okay. So it is actually like behind the scenes my class which is your class then

47:08 your function name and then the object itself. Wow. Okay. So let's try to run this. Let's see what do we get. See hello globe df. And let me just clear it out so that you will see the latest um output. See second way of writing it another way to call the function. And let's use this as well so that you will not be confused. Simple. Let's see two times. Yeah. Hello glob df.f.f.f.f.f.f.f.f.f.f Hello Globe DF. Perfect. So this is the reason why you do not pass self because Python is automatically passing it for you. So you just need to use it like this. It is like much better and more readable. This is like less readable. Make sense? Very very good. Now let me just tell you one

47:54 more very small thing but still it is very important for you so that you would know this concept. Now what do you think if let's say someone creates the class and with these variables can't you just change these variables? Can't you change these variables? you can because whenever you create the object you can modify the values whatever are listed inside the um variable. So let's say I just want to do variable one. Okay. So now I just want to let's say create an object and I want to simply say let's say object new equals my class. These are the values and I want to do I want to create

48:40 this class and you know what I will simply say obj dot my value name let's say variable 2 equals let's say lambda 2 changed okay and now if I just print the value obj um two not two I think obj new dot v variable two and if I simply say dot where two if Just print this variable like this. Then you will see that I will get the value like this. Let me start print here. I will get see changed. Wow. So this way you can even change the values as well. It's like very small thing but still I

49:25 just thought of telling it to you. Simple. So now let's talk about the encapsulation in Python. Basically encapsulation is just a fancy name in Python because encapsulation is not a kind of you can say concept in Python. Encapsulation is just a concept like just a generic concept. It's not something that we have inside Python. But there is a way we achieve this encapsulation thing in Python using access modifiers. Simple. So I can simply say encapsulation and then I can write access modifiers simple so this is the

50:12 real stuff encapsulation just like a concept simple let's use yellow here perfect so now what is the concept and why do we need to use encapsulation or whatever thing is this so now whenever you just want to work with let's say variables inside a class security is very important right yes so let's say let me just show you my VS code so here we have created these variables and we have just seen that we can change the variables right let me just create a new notebook or let's say file let's say um encapsulation perfect and let me just copy this code only let's say this one

50:57 make sense very good Now let's say um we want to or let's say we do not even need to ask for these variables but we can still like pass these variables or let's say we just want to define something let's say none none none or whatever or let's say let's let's define it let's it's fine let's say let's keep it as it is now this is our class these are our variables that we are using throughout our class through any function or through any kind of method anything. Users can also edit it because they are passing these variables in the runtime, right? And they can also see everything. Makes sense. But sometimes you need to keep some variables private that cannot

51:45 be seen by the other users. That will be only managed and seen by the owners of the class. You need to have that kind of encapsulation for your class. Now unlike C++ or Java, Python doesn't have you can say a built-in method or built-in kind of way so that you can just keep your variables private. You can just keep your variables protected. Nothing. Here we simply play with the naming convention because Python is built in such a way that is like assuming that okay whoever is working with Python is a kind of senior person. Yeah. So we just use naming convention for it. So basically there are three types of access modifiers that we use. What are those three? So first of all public

52:33 that means it is accessed by everyone. Okay public variable and after public public variables are those variables that we have just seen like all of the public um variables it can be accessed by public. Then we have one more which is called private which should not be used by anyone anyone at all. Then there's one more protected. Okay. Simple. Protected. Now these three are the variable types that we have. Simple. Good. Now how to achieve this thing? Okay, we got the concept. We got

53:19 the requirement like why do we need to learn this? Because obviously security is important. We need to make sure like nothing would be broken because anyone can just change some values which are used by the downstream solutions downstream methods. So in Python we do not have any kind of um built-in structure to manage this thing. We simply play with the you can say your um naming convention. It is also called as name mangling. I will just tell you don't worry about that. So this is everything but now what what kind of naming convention we need to use it is very simple. So let's say this is your dynamic one dynamic two dynamic three variable right. So now let's say I want to keep this variable as public so that anyone can just use it. Anyone can just change it whoever is just let's say creating the instance or anything.

54:06 Okay. Because once the instance is created once the object is created no one can just change anything using that object. So let's say I create one object let's say object my class is this one right now once this object is created now whoever is using this object maybe this object is being used by let's say 10 developers same object is being used by 10 developers and let's say one developer change the value of this one to let's say something let's say 100 and all of the downstream functions are broken and other nine developers be shouting bro What just happened? Everything was working fine. What just happened? So in order to get rid of these kind of problems, what you can do? You can uh

54:52 first of all, let's say this one is fine. Like anyone can just change this value because this is not very sensitive. This is like fine. This should be like public variable. Simple. But dynamic two variable let's say is very sensitive. You cannot you cannot afford to change this value by anyone. So now what you can do? You can simply make this. First of all, this is public um public variable. Perfect. I will just make it as private. How I can just make it as private? Simply say underscore and that's it. When you add double underscore, it will become private variable. Private variable. Now, no one can actually change it. And let me just change the value here as well because I

55:37 just need to refer it as underscore. Simple. Very good. Now, do you know what? If I just print OBJ um dot um let's say variable name is um self dot self not self. I can simply call this function which is your dynamic one. Let's say I just want to print it. Let's say because this is like my um variable. Obviously I have created this but let's say I just want to print it. Simple. So I will simply say dynamic one. But but now if let's let's run this first of all. Let's say you just want to run this. Simple. And you should see the value ABC. Yes, we got it. So let's say print

56:26 ABC. Perfect. But now let's say now you just want to see the value, okay, of your private variable as well. Because this variable is private, you cannot access it at all. Nothing. No, it can only be accessed within the class but not outside the class. So I'll simply say object dot dynamic 2. You will see the error. Let me just show you. See my class object has no attribute dynamic 2. Wow. So it is saying there's no object like dynamic 2. I cannot access it. Wow. That means you cannot even change it as well. So let's say I want to change the value of dynamic one equals to obviously let's say pqr and let's say print of dynamic one. You

57:12 will see pqr. Yeah. But you cannot basically change the value of dynamic 2 because it is not accessible. You will see the error. Stu an lamba. How how did it happen? How did it happen? How did it happen? So on llama what just happened? What just happened? So nothing has happened. Let me just tell you what happened. Do you know what? Now dynamic one, this dynamic one is actually your this variable. Okay. But do you know what? Do you know what? This dynamic two, this dynamic two, this dynamic two is not your this variable. Not at all. Not at all. Why? Because now let me just add a

58:00 comment as well. So now why you will just see this thing read it. This will not give error but it will create a new variable dynamic to in the object and assign the value as stu. So it is a new variable. It is not this dynamic to and you need proof of it. Let me just show you the proof because if I print this function function two this will print only and only the private value not the one that you have just set. Let me just show you. So let's say I want to print object 2.12 not print because I'm using print command in the function itself. So let's say and let's click on print and clear the output. Now let's see. See I still got

58:46 def. I didn't get uh you can say stu. I didn't get this thing. Why? Because stu is a new variable. It is not changing this variable because this is not accessible at all. Ooh. Okay. But in this particular case, it changed this value. If I just say print one um let's say obj. One, it will print hello world. PQR change value because it changed the value. This is the difference between public and private variable or basically or you can say access modifiers. See you got PQR the change value but here you didn't get SDU. Wow. Wow. Make sense? Make sense? So this is your

59:33 public and private. There is one more type called protected. In protected you simply add only one underscore instead of two. So here you will simply say protected variable. What is the use of protected? See protected variable is not a kind of you can say private variable at all but it is just for the protection. Developers should be aware that this is a protected variable and we should not change it at all. This is just a kind of warning. But private variables are like you should not you cannot change its value. But here with only one underscore it is a kind of sign that hey you should not change it. It is just a protection and that's it. only one underscore make sense and yes you can access it outside the class as well. Let me just show you.

1:00:20 Um if I just try to access this particular function um not function like object dot dynamic 3 and if I just print it I will be able to see it easily. Okay let's see this. So perfect. See, I'm getting XYZ. So, I'm able to use this. But obviously, I cannot use your protected variable obj dynamic 2 because that is a private variable. See? Wow. So, protected variables are accessible because it is a protected variable and it can be accessed outside the class. But it is not recommended. It is a warning. It is just a naming convention

1:01:07 to use underscore one before the variable name to indicate that it is a protected variable and it should not be accessed outside the class. This is just a kind of warning sign to the other other developers. Hey, this is a protected one. So do not change it. But if developers do not see any kind of underscore that means they can change it as well. And if you have double underscore then you cannot you cannot still there's a hack you can just use it but you should not okay you should not play around with that thing because there are some serious solutions built using that particular concept like your organization might have just created um so many functions downstream functions you should not play around with that simple very good so now let's try to see because now your fundamentals are strong now let's try to see a very

1:01:52 simple class that is like more aligned towards the data domain. So now what we will do we will simply create um one class and we will create let's say one class for let's say data class not data class because let me just change the name because data class is a kind of different area let's say um fetch something like that let's say data extraction class okay py so now I will create one class where I will simply show you a very simple example very simple example and here we'll be just working with some data frames like how you can just create the class to make your data frames how you can just write and just create the

1:02:38 methods for your data abstraction and here you can just use any data frame library and you can use pandas pispark anyone because here we are not learning pandas pispark we are simply focusing on python the real python but if you have like more interest in pispark you can just go with pispark if you just want to go with pandas, you can just go with pandas and obviously um pispark local um um you can say setup will require some dedicated effort to just create first of all docker compose and just run the apache spark notebook in the docker and then just create a connection then just create a you can say client connection with the cluster that is why we will be simply using pandas but if you want to use pispark you are open to do that simply go and create pispark data frames no one can stop

1:03:24 you can just write the pispark code the same code that we are writing here you can just write in datab bricks if you are um installing pispark locally you can do that but because pandas do not require any kind of installation any kind of setup time that is why I'm simply going with that because it will save some time and it will try it will help us to be focused more on the python makes sense but here you can just use any python data frame pandas py or whatever very good so in order to work with that thing first of all we need to install pandas so let's install all pandas like this. Simply go to your terminal. Okay. And then simply make sure that your virtual envirment is turned on because you need to work within virtual environment. And then you need to add pip install pandas. Simple.

1:04:10 This is just a warning sign. Simply ignore it. So this is installed. This has installed pandas in your virtual environment. And you can also confirm it by going here virtual environment. Here inside libraries you should see something called as pandas because it has downloaded the package. So now we can simply use our um panda. So it is saying python.exe upgrade pip. Let's do it because it is upgraded. Requirement already satisfied. Blah blah blah. Successfully installed. Very good. Good work. So our pandas is also installed. So now what I want to do I have let's say a CSV file. So I will just put the CSV file. But for now we can just imagine that we have CSV file here in this folder. So now what I need to do I need to create a class which

1:04:56 will be which will be let's say data class okay not like let me just change the name let's say data extraction data analysis whatever I want to create this class and inside this class and now um if some suggestions are coming simply um ignore it because you'll be writing some code. So data extraction. So in this class what we need to achieve we want to create such a class which can be used by anyone. What do I mean by that? So you know that if you have create if you have worked with some let's say data frames we often um fetch the data from multiple file formats and for each file format we just work with you can say so many um file formats and every file format has its own configuration and

1:05:41 all. So here what we will do we will simply uh let's say create one function okay and within that function um or you can say one class first of all and then we will be just creating one method which is just a function and within that method I will make sure that you or any user okay can use that function to read any file format you do not need to write a different code every time because whenever you just need to read let's say CSV file you will be using some different properties if you are using TSV file tab separated values you will be using different properties if you read let's say park file so you'll be just using something else make sense so you will be just doing all of these things so let's try to create a class for data extraction and let's try to

1:06:28 create a function for each kind of class or basically each kind of file format and then you can just write some um custom logics so here you can or anyone can ask me that hey we already have like let's say um file format methods written inside pandas let's say read csv read xlsx and read par we already have those things why do we need to um create our own class see this class is just a kind of simulation of what you can do what you can do make sense here if your file formats are coming in a specific format because not every time you will receive receive the file formats in you can say standard format maybe your CSVs has some special characters. Um if you have just worked with the scenario where you have

1:07:14 let's say so many special characters or basically um characters um escaping as well. So for that particular thing you have to write some custom code. So for that particular thing you can create a class for your specific project and you can just use it instead of writing the code again and again and again. I hope it makes sense. Okay. So let's try to create that class and let's see how it just behaves. So I have created this files folder. So I thought of like let's create it. Why do you imagine? So we have orders CSV file, orders.json file, orders.park file, orders.tsv file. TSV is also a kind of file format that we just use similar to CSV. And instead of comma, we have tab separated values as you can see like this. And park you would already know that it is a kind of

1:07:59 compressed file format used for big data. JSON is already there. And then we have orders dot CSV file. Simple. So we want to create a class called data extraction where we can just write any logic of our choice and each logic will be aligned for each file format. So let's say I want to create first of all in it because that is a non-negotiable self and then I want to pick the um variable variable name will be let's say file format simple because that will just tell us like which um we can just take file format or basically we can also take path like where is our u file is saved simple so let's take the path it will be like better because it can be saved anywhere to be honest okay And let

1:08:45 me just say df and this is my file format. And I can also use type ends. If you do not know in python we always encourage using type ends. So whenever you use any variable or um argument simply go with simply go with this thing. Let's say colon and what type of file format are you expecting for this thing? I I'm I'm expecting let's say string. So all simply a string. Simple. That's it. It is a kind of typeing because whenever you simply pass anything else you will see some um errors before just passing a wrong value. So it is a good practice. So now within that I will simply say self dot file path equals to file path. Simple. So this is my constructor. My

1:09:30 constructor is ready. And after that I want to create one. Let's say fetch CSV. Okay. And the first thing that we use is self. That's it. And now I need to import the pandas as well. So let's import pandas spd. Perfect. Very good. So now within that what I will do now I will create or basically write my logic to fetch the data from the CSV file only. Okay. I will simply say um or let's say file name as well file path or let's put the file name in the file path. Simple. So I'll simply say df equals and let's hit enter or let's not hit enter. Let's not do

1:10:15 enough indentation. So df equals to pd dot read csv which is a kind of function we have builtin available in pandas. And now obviously I need to pass the file path and file path will be let's say file path variable itself which will be self dot of file path. Simple. Very good. So this is my data frame. I can also say um df head if I just want to display it or if I can just do anything here. Now you can write any business logic here depending upon your CSV file. Here we are using orders CSV file which is this one which has like clean data. But if you have let's say um characters special characters if you just want to let's say remove any kind

1:11:00 of spaces because whenever you just receive some data in the CSC for file especially you get some um characters which are also encoded in the double quotes. So in that particular scenario you have to first of all remove the characters. If you are getting some extra spaces you can just remove that before processing it. So it totally depends upon what kind of logic that you are just trying to use. Make sense? So that every time you do not need to u perform that logic again and again you can just write any logic depending upon your file here but now obviously this is a tutorial so I'm simply expecting that we have simply written everything here let me just add one comment write your custom logic simple very good now let's say I want to

1:11:46 create a function let's say fetch um

…

Share

YouTube Video Summaries