Python Tutorial - 17. Class and Objects
Key Takeaways
Builds a Python class and objects
Full Transcript
hello and welcome to core basics coding tutorial today's topic is classes in Python and here is the list of items we are covering in this video ok let's begin with what is class when we are talking about class it's not specific to a Python programming language it applies to move to the other programming languages as well and it falls under a common paradigm called object-oriented programming okay so let's see what is class when you talk about human beings it has every human being has a common set of properties such as name gender occupation etc also every human will be doing some kind of common activities such as picking doing work or sleeping so the first set of properties that we cover are called properties in object-oriented programming world and the activities that human performs are called methods in object-oriented programming thus properties and methods forms a central entity called a class so here human is a class which has two central component which is properties and methods so class is nothing but an abstraction of some entity which contains common set of properties and matters ok what is object then so object is nothing but a specific instance of a class for example for human being these two are specific instances of the human class Tom Cruise and Mayas are poor the both are human beings they both have names gender occupation they both do common set of activities such as speaking doing work right but when you talk about specific instance the behavior so that the behavior varies that properties are common for example Tom Cruise has a name - our opera have has a name but when you look at the actual value which is the real name they are different for example Tom Cruise no name is Tom Cruise in the second case it is - are poor so these names are different but the the properties are common okay as we know the concepts now let's write our first class in Python as usual I have P by charm editor open and I have created class dot py file so the way you write your class is first you'll use a class keyword followed by the class name which in our case is human column enter the first property the first method not the property is in it method so you say depth underscore underscore in it and it is Auto typing this first argument which is self self means this class itself and this self is useful to define the properties of this class and the way you define the properties is self dot name equal to something and you say self dot occupation is equal to something by doing this I effectively define two properties in this human class and those two properties are named and occupation okay now this init method will be called whenever you will create an instance of this class later on so then you're not as you know from the concept that an instance has a specific value of those properties so we need to pass those here in this function so I'm going to pass name and occupation as an argument of this function so I will call it N and O and assign the value of those arguments to these two properties of the class ok let's slide our first method now which is do work okay so if you recall from our concepts based on the occupation of the person that human being will be doing different kind of work so if the human being is a tennis player what kind of work does he do well he will play tennis right so I will just say plays tennis as if the occupation of the person is an actor then what does an actor usually do by the way does he cook in the kitchen no he shows a film usually you might coconut kitchen who knows okay so this is our first matter less side our second method which is speaks so human being speaks what does he speak well I will say just for simplicity how are you okay so we effectively defined two methods of the class and two properties of the class so our class definition is complete so this entire thing is our class definition now let's create some instance of it the first systems I'm going to create a storm and the way you do it is you create a variable named term equal to the class name and in the bracket you will pass down those arguments as you seen in the tooltip it is same self and onn is highlighted which means when you're creating an instance of a class it is going to first call this method in it okay so by doing this you are calling this method and this method takes three arguments self and and oh but if you look in door tool tip here it was saying that the first argument is just implicitly taken so you need to start from the second argument okay so in the case of Raoul's it will the name is Tom Cruise and the occupation is an actor I'll say Tom do work and Tom dot speak okay so I create an instance of human class and I just call in these two methyl s execute the program and see what happens okay so when I execute it what happens is it says Tom Cruise suits film Tom who says how are you excellent so here it recognized that if this guy is a co occupation is an actor that's why it printed shoots them okay let's create another instance of this class which is Marius our boss so I'll see what I'm going to do is just okay I'm not going to do copy paste because that won't be efficient here so name of the person and the occupation is tennis player and I do work and my - speaks okay so this for the second instance the name and occupation is different okay so although the class is same the instance is different so let's execute this okay excellent so here you see for Tom it printed tom cool suits film and he says how are you vs. 4 - our poet says she plays tennis and she also says how are you ok so this was a very quick demonstration of how to create a class and how to create the instance or an object of that class thanks for watching
Original Description
Video without background music: https://youtu.be/6XWeeEg6d3s
This video python tutorial will explain about “class” and “objects”. It has covered what is a class, how to create a class in python, how to create a method in the class and how to create an instance of a class.
Topics that are covered in this Python Video:
0:00 Introduction
0:11 what is a class
2:45 create a class in python
4:36 create method in class
6:21 create an instance of a class
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Code used in this tutorial: https://github.com/codebasics/py/blob/master/Basics/17_class.py
Next Video:
Python Tutorial - 18. Inheritance: https://www.youtube.com/watch?v=KdIyRbixrYE&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=20
Website: https://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 23 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
▶
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
Related AI Lessons
Chapters (5)
Introduction
0:11
what is a class
2:45
create a class in python
4:36
create method in class
6:21
create an instance of a class
🎓
Tutor Explanation
DeepCamp AI