Coder Social home page Coder Social logo

dsc-instance-methods-lab-onl01-dtsc-pt-030220's Introduction

Instance Methods - Lab

Introduction

In the last lesson, you learned about instance methods -- what they are and how to define them. In this lab, you are going to flesh out the Driver and Passenger classes by writing your own instance methods for these classes.

Objectives

In this lab you will:

  • Create an instance of a class
  • Define and call an instance method

Define classes and instance methods

You will now define classes and associated instance methods in the cell below:

Remember: as we learned in the previous lesson, we need to define our instance methods with at least one argument (self) in order to call them on an instance object.

Define a class Driver with two instance methods:

  • greeting: this should return the string "Hey, how are you?"
  • ask_for_destination: this should return the string "Where would you like to go today?"
# Define Driver class here

Define a class Passenger with two instance methods:

  • reply_greeting: this should return the string "I am doing well!"
  • in_a_hurry: this should return the string "Punch it! They're on our tail!"
# Define Passenger class here 

Instantiate classes and methods

Great! You've now defined classes and the associated instance methods. You will now actually use them:

Start by instantiating a driver and a passenger. Assign the driver to the variable daniel and assign the passenger to niky.

daniel = None # driver
niky = None # passenger

Alright, you have the passengers and drivers! Now you need to put those instance methods to use. Try them out and assign the return values to the variables below.

  • Have daniel greet his passenger, who is going to be niky. Assign the greeting to the variable polite_greeting
  • Have niky respond by calling in_a_hurry(), and assign the return value to the variable, no_time_to_talk
polite_greeting = None
print(polite_greeting)
no_time_to_talk = None
print(no_time_to_talk)

Feel like doing more?

In the cells below, you'll create three different classes that represent animals in a zoo -- lions, tigers, and elephants. Each animal should have a method, speak(), which returns a string containing the sound they make (feel free to have some fun with this -- we don't know how to spell the sound an elephant makes any better than you do!).

# Create Lion class
# Create Tiger class
# Create Elephant class

Now, in the cell below, create an instance of each animal:

simba = None
tony = None
dumbo = None

Now, add each of them into the list zoo in the cell below:

zoo = None

Now, loop through the zoo list and call out the .speak() method for every animal in the zoo. Make sure you print this in order to see the output!

Summary

In this lab, you practiced defining classes and instance methods. You then instantiated instances of your classes and used them to practice calling your instance methods.

dsc-instance-methods-lab-onl01-dtsc-pt-030220's People

Contributors

alexgriff avatar loredirick avatar mas16 avatar mathymitchell avatar mike-kane avatar peterbell avatar sanpietro avatar sumedh10 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.