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

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

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

FavoriteLoadingAdd to favorites

Object-oriented programming (OOP) in Python Part 2 – let’s go!

Introduction to Classes and Objects Part 1:
Introduction to Classes and Objects Part 2:

Find the sample files here:

You can find this entire Python series in a playlist here:

For more Python, I would recommend two of my online video courses.
– Get Ready for Your Coding Interview:
– Introduction to Data Visualization with Python:

Also, keep in touch on Facebook:

source

By |2019-11-26T00:11:34+00:00November 26th, 2019|Python Video Tutorials|26 Comments

26 Comments

  1. Владислав Горобчук November 26, 2019 at 12:11 am - Reply

    It's just awesome! So easy to understand. Thanks a lot for the Tutorial

  2. DarkPlatinum November 26, 2019 at 12:11 am - Reply

    When I do
    p1.robot_owner = r1
    It gives me an error = 'tuple' object has no attribute 'robot_owned'
    Please, help someone.

  3. Ave Stuart November 26, 2019 at 12:11 am - Reply

    Hey @CS Dojo – i'm really not understanding the robot_owned attribute, and looking at the comments here i'm not the only one who is confused. Can you please explain this a bit more?

  4. Cliff Williams November 26, 2019 at 12:11 am - Reply

    THANK YOU YK I have come so far so quickly with you!

  5. Jaroslav Marhivka November 26, 2019 at 12:11 am - Reply

    You are amazing at explaining things! Keep it up!

  6. Gourab Paul November 26, 2019 at 12:11 am - Reply

    hy, i need to pay for your course at lynda.com can you please tell me that from where i can get the course free?

  7. Shibin Raja November 26, 2019 at 12:11 am - Reply

    Every time I see your video I will be like thats easy , i can do that. Thank you so much Dojo

  8. Ben Chung November 26, 2019 at 12:11 am - Reply

    why could not you include the is_setting as one of the arguments in the _init_ constructor?

  9. This is Taiwan November 26, 2019 at 12:11 am - Reply

    Wow! Thank you for such a clear and concise tutorial. All your videos have been incredibly helpful so far. Cheers!

  10. Chanchal Gupta November 26, 2019 at 12:11 am - Reply

    In python don't we need to define the type of variable…..like string name, string color, int weight?

  11. chr ber November 26, 2019 at 12:11 am - Reply

    Dojogang

  12. Juma John HM November 26, 2019 at 12:11 am - Reply

    Can use add use case for the person class

  13. Matheus Mascarenhas November 26, 2019 at 12:11 am - Reply

    My notes on L10

    # Classes and Objects – Part 2/2

    # https://www.youtube.com/watch?v=wfcWRAxRVBA

    #

    # Exercise

    # Defining the Robot class

    class Robot:

    def __init__(self, name, color, weight):

    self.name = name

    self.color = color

    self.weight = weight

    def introduce_self(self):

    return "Hi my name is {}.".format(self.name)

    # Defining the Person class

    class Person:

    def __init__(self, name, personality, is_sitting):

    self.name = name

    self.personality = personality

    self.is_sitting = is_sitting

    def sit_down(self):

    self.is_sitting = True

    def sit_up(self):

    self.is_sitting = False

    # Creating the robot objects

    r1 = Robot("Tom", "red", "30")

    r2 = Robot("Jerry", "blue", "40")

    # Testing

    print(Robot.introduce_self(r1))

    print()

    # Creating the person objects

    p1 = Person("Alice", "aggressive", False)

    p2 = Person("Becky", "talkative", True)

    # To add a ner object attribute related to the robot a person owns, there are 2 options.

    #

    # (1) Edit the attributes of the constructor _init_ within the Robot class

    # (2) Manually create the attributes for each person

    # As (1) is more intuitive, let's use the other example (2)

    # p1 owns r2

    p1.robot_owned = r2

    p2.robot_owned = r1

    # Testing

    print("Hi, my name is {} and I'm very {}. I also have a robot called {}.".format(p1.name, p1.personality, p1.robot_owned.name))

  14. Greyshades 1798 November 26, 2019 at 12:11 am - Reply
  15. Roshan Chapagain November 26, 2019 at 12:11 am - Reply

    what are the keyboard shortcuts to add new shell , run shells and so on ?

  16. Raghukrishnan J November 26, 2019 at 12:11 am - Reply

    Please clear my doubt… can we have a default constructor in python where values are automatically initialised?
    Thank you 🙂

  17. DIRTY Dooney November 26, 2019 at 12:11 am - Reply

    how do you memorize every single bit of code you do? I mean obviously you don't just want to memorize the code you want to understand each and everything you do and why you do it but do you ever need to review things or go back and study code?

  18. Martin Kaspar November 26, 2019 at 12:11 am - Reply

    hello dear CS Dojo

    wow – a really overwhelming tutorial – very helpful and in all things Python. I like the sidenotes that you
    give to the idea and concepts that are used in JAVA. This is very interesting. I really like this ideas and your "CS-Dojo approach". Well your stuff is among of the very best python-ressources on the whole net. One question though can you put togehter the most important infos into a cheatsheet – a Summarizing Cheat Sheet to download: that would be fantastic.
    Above all: dear CS Doyo – you did a fantastic job explaining these concepts of Python programming!
    I am learning to code and your introduction helps me to fully grasp the ideas. keep up the great work – it rocks!! best regards martin

  19. RoyalTuxedo November 26, 2019 at 12:11 am - Reply

    your videos are really good and make OOP easier to understand

  20. vano christian November 26, 2019 at 12:11 am - Reply

    Because of you, now I know to leave four spaces in def and other stuff before I got an error and did not know what to do THX

  21. True911 November 26, 2019 at 12:11 am - Reply

    I've enjoyed your tutorials so far. They're to the point and share some nice extra tips without being over people's heads. Practically speaking, though, I'd like to know about handling IO in Python, maybe in a desktop app, command-line app and a web app, if they're (probably) handled differently. I can look it up, but it seems like a pretty basic need in any app. 🙂

    Keep up the great work and thanks for considering my request.

  22. Obilor Jim November 26, 2019 at 12:11 am - Reply

    Good job. Thank you.

  23. habib shirzad November 26, 2019 at 12:11 am - Reply

    what is robot.owned ? i understood what it does but you didn't define it anywhere ? what if we just write p1 = r1 ?? btw great explanation.

  24. Benedek Dankó November 26, 2019 at 12:11 am - Reply

    thanks, great video and perfect explanation

  25. Abhisek Shaw November 26, 2019 at 12:11 am - Reply

    Hey I have some changes in your code see here👇👇

    class Robot:
    def __init__(self, n, c, w):
    self.name = n
    self.color = c
    self.weight = w
    def introduce_self(self):
    print("His Name Is " + self.name)
    print("His Color Is " + self.color)
    print("His Weight Is " + self.weight)
    r1 = Robot ("Tom", "Red", "20")
    r2 = Robot ("Jerry", "Blue", "20")
    class Person:
    def __init__(self, n, p, i):
    self.name = n
    self.personality = p
    self.is_sitting = i
    def introduce_self(self):
    print("My Name Is " + self.name)
    print("My Personality " + self.personality)
    if self.is_sitting == True:
    print("I am Sitting")
    else:
    print("I am Not Sitting")
    print("My Robot :")
    def sit_down(self):
    self.is_sitting = True
    def stand_up(self):
    self.is_sitting = False
    p1 = Person("Alice", "Aggressive", False)
    p2 = Person("Abhi", "Talkative", True)
    p1.robot_owned = r2
    p2.robot_owned = r1

    #asking questions

    p1.introduce_self()
    p1.robot_owned.introduce_self()
    p2.introduce_self()
    p2.robot_owned.introduce_self()

  26. dz3000 sky November 26, 2019 at 12:11 am - Reply

    Alice And Becky, both women,both talkative and aggressive.

    print("I see what you did there.")

Leave A Comment

*