Classes and Objects with Python - Part 2 (Python Tutorial #10)

CS Dojo · Beginner ·🛠️ AI Tools & Apps ·8y ago

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 4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
2 Dynamic Programming Tutorial with Fibonacci Sequence
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
3 Kadane's Algorithm to Maximum Sum Subarray Problem
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
4 Longest Common Subsequence (Dynamic Programming)
Longest Common Subsequence (Dynamic Programming)
CS Dojo
5 0-1 Knapsack Problem (Dynamic Programming)
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
6 Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
7 Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
8 Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
9 Radix Sort Algorithm Introduction in 5 Minutes
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
10 Coding Interview Question and Answer: Longest Consecutive Characters
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
11 Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
12 Coding Interview Question: Tower Hopper Problem
Coding Interview Question: Tower Hopper Problem
CS Dojo
13 Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
14 Google Coding Interview Question and Answer #1: First Recurring Character
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
15 Facebook Coding Interview Question and Answer #1: All Subsets of a Set
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
16 Think you're not smart enough to work at Google? Well, think again.
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
17 How to Crack a Google Coding Interview - An Ex-Googler’s Guide
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
18 Amazon Coding Interview Question - K Closest Points to the Origin
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
19 How I Got an Internship at Microsoft
How I Got an Internship at Microsoft
CS Dojo
20 How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
21 Why I Left My $100,000+ Job at Google
Why I Left My $100,000+ Job at Google
CS Dojo
22 Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
23 How I Learned to Code - and Got a Job at Google!
How I Learned to Code - and Got a Job at Google!
CS Dojo
24 Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
25 What Is Dynamic Programming and How To Use It
What Is Dynamic Programming and How To Use It
CS Dojo
26 Python Tutorial for Absolute Beginners #1 - What Are Variables?
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
27 What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
28 How to Use If Else Statements in Python (Python Tutorial #2)
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
29 Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
30 How To Use Functions In Python (Python Tutorial #3)
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
31 What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
32 Introduction To Lists In Python (Python Tutorial #4)
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
33 Introduction to For Loops in Python (Python Tutorial #5)
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
34 What Programming Language Should I Learn First?
What Programming Language Should I Learn First?
CS Dojo
35 What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
36 While Loops and The Break Statement in Python (Python Tutorial #6)
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
37 More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
38 How to Learn to Code - Best Resources, How to Choose a Project, and more!
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
39 How To Use Dictionaries In Python (Python Tutorial #8)
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
40 Data Structures & Algorithms #1 - What Are Data Structures?
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
41 An Overview of Arrays and Memory (Data Structures & Algorithms #2)
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
42 Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
43 Classes and Objects with Python - Part 1 (Python Tutorial #9)
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
44 Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
46 Introduction to Linked Lists (Data Structures & Algorithms #5)
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
47 Introduction to Recursion (Data Structures & Algorithms #6)
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
48 Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
49 Amazon Coding Interview Question - Recursive Staircase Problem
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
50 Using Boolean in Python (Python Tutorial #11)
Using Boolean in Python (Python Tutorial #11)
CS Dojo
51 Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
52 What Can You Do with Python? - The 3 Main Applications
What Can You Do with Python? - The 3 Main Applications
CS Dojo
53 Facebook Coding Interview Question - How Many Ways to Decode This Message?
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
54 List Comprehension Basics with Python (Python Tutorial #12)
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
55 How To Use Sets in Python (Python Tutorial #13)
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
56 Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
57 Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
58 6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
59 Google Coding Interview - Universal Value Tree Problem
Google Coding Interview - Universal Value Tree Problem
CS Dojo
60 Best laptops for programming? How to get a job at Google? - And other FAQ’s!
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo

Related Reads

📰
Top 10 AI APIs & Scrapers in 2026 — Ranked by Active Users
Discover the top 10 AI APIs and scrapers in 2026, ranked by active users, to boost your development projects
Dev.to · Nick Davies
📰
AI Isn’t Coming for Your Job — It’s Coming for Your Excuses: The Complete Guide to Making Money…
Learn how to leverage AI to earn money and grow your business with the latest strategies and opportunities
Medium · ChatGPT
📰
The 30 Cheapest AI APIs in 2026: My Honest Open Source Take
Discover the 30 cheapest AI APIs in 2026 and learn how to integrate them into your projects, saving you time and money
Dev.to · rarenode
📰
What Due Diligence Automation Actually Catches (And What It Can't)
Learn what due diligence automation can and can't catch, and the standard that separates useful from dangerous automation
Dev.to · DEVALAND
Up next
I Built a Live Dashboard With Claude - Zero Coding, Zero IT Skills
Nicolas Boucher
Watch →