Classes and Objects with Python - Part 2 (Python Tutorial #10)
Skills:
Python for Data80%
Key Takeaways
Builds object-oriented programming concepts using Python classes and objects
Full Transcript
hey guys this is part two of my introduction to classes and objects with Python and in this video I'm going to talk about how multiple classes and objects can interact with each other in Python now if you're new to these concepts I'd recommend my introduction to classes and objects videos first and in this video I'm gonna use the same example as part two of those two videos so if you already watch that one you can just skip over to this time in this video to check out my Python code okay first of all here are the class on objects we created in the last video we have the robot class with three attributes named color and weight and one function introduce self and then out of this class we created these two objects or 1 and r2 containing these attributes for example r1 had Tom as the name red as the color and 30 as the weight and on top of that we're gonna create this class called person representing a person of course and it's gonna have these attributes name which is going to be a string personality which is also going to be a string and is sitting which is going to be either true or false and then if this is true that means that this particular object this person is sitting and if it's false that means that this person is not sitting and then out of this class we're gonna create these two objects the first object will have the name Ali's personality aggressive and is sitting will be false and the second object will be Becky for the name and her personality will be talkative and is sitting will be true so this person will be sitting now just a quick side note in case you're not familiar with these values false and true they belong to a special data type called boolean so if you have numbers like these there are 4 1 - 13 and so on there are integers and they refer to as int in Python and if you have collections of characters like these enclosed in double quotes or single quotes there are strings of course worst STR in Python and if you have true or false they are boolean or the spool in Python okay back to my main point we're gonna put this person object in a variable called p1 and we're gonna put this other person object emp2 and the person class will have two methods as well the first one is gonna be called sit down when you run sit down for example on p1 the is sitting attribute will be turn true because this person will be sitting after running this function and then the standup method is exactly the opposite so for example if you run p2 that stand up the is sitting value of p2 will be false because this person will not be sitting after standing up and here how can we show the relationships between these two sets of objects for example what if we wanted to show that p1 this person owns this robot r2 we can do that by adding an extra attribute to the person class let's call it robot owned and then if you want to show that p1 owns r2 we can set the robot own attribute of p1 2 or 2 and then if you want to show that p2 owns or 1 this robot named Tom you can set the robot own attribute of p2 to r1 now let's see how we can do that in Python here we have the class that we defined in the last video called robot he had a constructor in it plus a function called introduce self and then we also learned how we can create objects out of that class just like that for example R 1 equals robot Tom red 30 and then the same thing for our two and now let's create a new class here this is gonna be called person of course and then it's gonna have a constructor for that you can just write def to underscores in it two underscores again self and then let's have it take three additional arguments on top of self and pi/4 name personality and is sitting and then you can set this new object's name to n by writing self dot name equals n and then you can do the same thing for personality and is sitting and then this class will have two additional methods on top of the constructor like we saw earlier the first one is sit down this is not going to take any arguments except for self and to implement this you can just write self dot is sitting equals true and note here that true here is capitalized and then you can do the opposite for stand up stand up self and then itself that is sitting equals false again the word false is capitalized here and that's just how it works in Python let's run this cell to load this class once this class is loaded or once this block of the code is run you'll be able to create a new object out of this class you can do that by writing P 1 equals person note here that the word person is capitalized double quotes Alice comma double quotes aggressive comma false so this new object will have Alice as a name aggressive as the personality and false as is sitting that's the same thing for p2 by writing p2 equals person Becky comma talkative comma true let's execute this block of code and these two objects have been created now like I said earlier let's say we want to show that p1 owns the first person owns the second robot r2 to show that all you need to do is like I said earlier set the robot owned attribute of P 1 to R 2 you could do it in a couple different ways one way to do that would be to change the constructor and then have it take maybe an additional argument let's call it R and then here you can just say self dot robot own equals R that's one way to do it but you could just write here P 1 dot robot own equals R 2 as well and that defines a new attribute in P 1 called robot own and sets it to R 2 let's just do that let's just do the same thing for P 2 as well let's say P 2 owns R 1 so we're gonna write P 2 the robot own equals R 1 let's execute this piece of code and you'll be able to access this robot owned attribute and say P 1 just like you can use any other attributes so here if you write P 1 dot robot owned that will give us R 2 and then further if you right dot introduce self this is the same thing as saying R to the introduce self so this should print out my name is jeri because that's the name of our two let's see if that works by running this block of code and it does we see my name is Jerry right here ok you'll be able to find a link to my sample code in the description below both as a plain Python file and as a jupiter notebook file and actually i'm not sure how many Python videos I'll be able to make in the future because right now I'm really focused on my data structure series if you want to learn more Python though I personally recommend two of my online courses which you should be able to take for free the first one is called get ready for your coding interview and its own lynda.com this course is basically for practicing your problem-solving skills with Python and the second course I recommend is called introduction to data visualization with Python and this one is on this website called flora site and this one should be good if you're interested in things like their analysis data science or machine learning both of these courses are taught by me and you should be able to take them for free by signing up for their free trials for seven days or 30 days I'm gonna put links to those courses in the description below in case you're interested or you can just go to CS dojo dot IO to find those courses okay as usual thanks so much for watching my videos and I will see you in the next one
Original Description
Object-oriented programming (OOP) in Python Part 2 - let's go!
Introduction to Classes and Objects Part 1: https://youtu.be/8yjkWGRlUmY
Introduction to Classes and Objects Part 2: https://youtu.be/4dqlSk_RPmI
Find the sample files here: https://www.csdojo.io/class2
You can find this entire Python series in a playlist here: https://goo.gl/eVauVX
For more Python, I would recommend two of my online video courses.
- Get Ready for Your Coding Interview: https://goo.gl/RMCaxW
- Introduction to Data Visualization with Python: https://goo.gl/fZ5oVX
Also, 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 · 45 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
43
44
▶
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: Python for Data
View skill →Related Reads
📰
📰
📰
📰
Top 10 AI APIs & Scrapers in 2026 — Ranked by Active Users
Dev.to · Nick Davies
AI Isn’t Coming for Your Job — It’s Coming for Your Excuses: The Complete Guide to Making Money…
Medium · ChatGPT
The 30 Cheapest AI APIs in 2026: My Honest Open Source Take
Dev.to · rarenode
What Due Diligence Automation Actually Catches (And What It Can't)
Dev.to · DEVALAND
🎓
Tutor Explanation
DeepCamp AI