Classes and Objects with Python - Part 1 (Python Tutorial #9)
Skills:
Algorithm Basics80%
Key Takeaways
Introduces object-oriented programming concepts using Python classes and objects
Full Transcript
hey guys in this video I'm gonna give you a quick introduction on how to use classes and objects in Python I'm going to assume that you're already familiar with these concepts so if you're new to you know the concepts of classes and objects I'd recommend my introduction to classes and objects video first okay so here's the class we want to create it's gonna be called robot and it will have three attributes or three instance variables name color and weight those are the name of the robot the color of the robot and weight of the robot and then it's gonna have one function into yourself where when you run this function it'll just print out my name is the name whatever the name is and from this class we want to create these objects the first object will look like this it's gonna have the name Tom the color is gonna be read in a string and the weight will be 30 to show that it's 30 pounds it's gonna be an integer and we're gonna put this object in a variable called r1 and the second object we're gonna create will look like this this robot will have the name Jerry the color will be blue and weight will be 40 and then we're gonna put this object in a variable called r2 so let's now see how we can create this class called robot and these two objects out of that class in Python the first thing we need to do here is of course we need to create a class for that you can just write class robot : and notice here that our here is capitalized and then for spaces after that and we're gonna implement the function we saw earlier in to do itself for that you can just write def introduce underscore self parentheses : and four more spaces after that and then print double quotes my name is plus self dot name and this keyword here self is sort of like this in Java and it will refer to whatever object we're running this function on so for example if we run this function engine yourself on an object whose name is Tom this is gonna print out my name is Tom because self will refer to that object now this function you know if you see it looks like a regular Python function we're defining a function called introduce self and then below that we have some implementation but actually when this function is within a class when this function is a method of a class you need to add an additional argument and that's going to be call itself so this self is exactly the same as this self right here so in Python basically you need to add this additional argument called self to every method you want to add to this class and once you run this block of code this class is gonna be created and you might say what about the attributes like the name weight and color we're gonna worry about that later so let's run this block of code for now now that we have defined this class we'll be able to create an object out of this class to do that you can just write our 1 equals robot parentheses again note that the word robot is capitalized here and this simply says create a new object with the class robot and here you're using the default Python constructor for this class robot and to set the attributes you can just write or one dot name equals Tom I want the color equals red and r1 dot weight equals 3 and when you run this so a new object has been created it's assigned to r1 and then its attributes have been set and then if you want to run this function into yourself on this object you can just right or one dot introduce self parentheses and you don't need to pass in any arguments here and when you run this cell what's gonna happen is it's gonna go into this part of the code and then self will refer to r1 and so this part of the code will look like print my name is r1 dot name and r1 dot name is of course Tom here so it should print my name is Tom let's see if it works and I did we see my name is Tom right here and just like that you can create another object - let's go back into this cell let's write our two eCos robots this will create a new robot object and then let's set its attributes to so our two names echoes Jerry our two the color equals blue and our 2.8 equals 30 let's actually set the weight of our two to 40 like we saw earlier and once you run this cell you'll have an object in r1 and another object in r2 with different sets of attributes of course and then at this point you should be able to run or what introduce self and our to the introduced self so the first line should print my name is Tom just like we saw earlier and then the second line should print my name is Jerry instead so let's see if we can get that and that's exactly what we see okay going back a little bit when you see this block of code that we use to create these two objects you might say well that's not great because here we're writing our want name equals Tom but if we may spell the word name for example if we wrote our one dot name e equals Tom it's gonna stop working because if you look at this function in particular in this class we're writing here print my name is self dot name and this piece of code basically depends on us writing the attributes name name correctly for it to work what I mean by that is for example you know if you misspelled the word name by writing R 1 dot name equals Tom the attribute called name does not exist in this object anymore so we can actually see what's gonna happen if we try that so let's run this cell again and this is not gonna give us any error because when you write R 1 dot name equals Tom the attribute called name E is just defined and then the value Tom is set for that but when you run this block of code or Wanda introduce self let's comment out the second line and let's focus on the first line when you run this part you'll say attribute error and it says Robert as an object has no attribute name and that's because it has the attribute name e but it doesn't have the attribute name so this is probably not the best way to deal with different attributes we have let's see how we might be able to fix that and the way you can fix it is basically by using a constructor and in Python you can create a custom constructor with this keyword underscore underscore in it and ask underscore underscore so there's the word in it and it's surrounded by pairs two pairs of underscores and then parentheses and let's have this function take three arguments give a name given color and given weight so these are the name color and weight we want to set for this particular object that we are trying to create and then we can write here self dot name equals give a name self dot color equals given color and self dot weight equals given weight and this block of code of course says set this object that we are just creating set that object's name to give a name and set that object's color to given color and set the object's weight to given weight and earlier I said you need to add this self keyword this additional argument to every function we define in this class and the constructor is no exception so you actually need to write self as the first argument and then the arguments that you want to add in and actually just like many other languages a common practice here is to name these arguments exactly the same as the attributes that we want to set you don't have to do it but that's just a style thing so let's just do that let's change give a name to name and then given color to color and given weight to wait and once you run this block of code this class is going to be refreshed with a new constructor once we have a custom constructor in our class the default constructor that we used will stop working so let's just comment out this block of code and then let's rewrite it using our new constructor for that you can just write R 1 equals robots parentheses Tom comma red comma 30 let's capitalize Tom here and then R 2 equals robot Jerry comma blue comma 40 and these two pieces of code do exactly pretty much the same thing as what we saw earlier but as you can see this is much cleaner because you don't need to specify the attribute names manually every time and just to reiterate just to sort of clarify this line R 1 equals robot Tom red 30 will create a new robot object and then it'll set Tom as its name because Tom will go in here as the name argument and then LLL go in here self dot name and self again will refer to the object that we're creating so it's sort of like saying R 1 dot name equals name or one dot color equals color just like we did earlier here and of course it's the same thing with our two okay and to make sure we don't have anything left from the previous code we were using let's go to kernel and click restart and clear output this is gonna clear out every variable we define earlier and then let's load this class again and let's run this cell and at this point we should have r1 and r2 objects defined so we should be able to run our one dot engine yourself and our tada introduce self and that should print exactly what we saw earlier my name is Tom and my name is Jerry so let's see if that works and it does all right you'll be able to find links to the sample code I used in this video in the description below and if anything was unclear in this video please let me know in a comment below as well because you know I might be able to adjust that in the next video and in my next Python tutorial video I'm planning to cover how multiple classes and objects can interact with each other in Python so look out for that as well and I'll see you in the next video
Original Description
Object oriented programming (OOP) in Python - let's go!
Introduction to Classes and Objects: https://youtu.be/8yjkWGRlUmY
Download the sample file here: https://www.csdojo.io/class
You can find this entire series in a playlist here: https://goo.gl/eVauVX
And keep in touch on Facebook: https://www.facebook.com/entercsdojo
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 43 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
▶
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Builder Story: Saymon and Core Care
Dev.to AI
AI-Powered Variance Narratives: Prompt Engineering for Solo Fractional CFOs
Dev.to AI
A Gen X entrepreneur closing their laptop at the end of a productive, shortened workday, ready to…
Medium · AI
Earning Technical Certifications in 5 Steps with AI Tools
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI