GithubHelp home page GithubHelp logo

cpease00 / python-instance-methods-lab-nyc-career-ds-062518 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/python-instance-methods-lab-nyc-career-ds-062518

0.0 1.0 0.0 7 KB

Jupyter Notebook 75.55% Python 24.45%

python-instance-methods-lab-nyc-career-ds-062518's Introduction

Practice with Instance Methods

Introduction

In the last lesson, we talked about instance methods -- what are they and how to define them. In this lab, we are going to put our new skills to the test and start writing our own instance methods on our classes.

Objectives

  • Practice defining classes and instantiating instances of those classes
  • Practice defining instance methods and calling them

Defining Classes and Instance Methods

In this section, define two classes, Driver, Passenger.

In the Driver class, define the instance method greeting that returns the string "Hey, how are you?". Then define the method ask_for_destination, which returns the string "Where would you like to go today?".

In the Passenger class, define the instance method reply_greeting that returns the string "I am doing well! Thanks for picking me up today!". Then define the method in_a_hurry, which returns the string "Punch it! They're on our tail!".

Again, we have both a driver.py and passenger.py file included in our directory. Use these files to define your classes and instance methods.

Remember: as we learned in the previous lesson, we need to define our instance methods with at least one argument in order to call them on instance objects. Don't worry, we will explain this more in a later lesson.

Note: the next cell is loading an extension, autoreload, from IPython. This tool simply reimports our code right before we execute any python (i.e. run a cell with Python code). This ensures that any update we make in our other files, passenger.py and driver.py, will be reflected in the jupyter notebook. Don't worry about autoreload for now, just run the cell once and forget about it.

%load_ext autoreload
%autoreload 2
# import Passenger here
# import Driver here

Instantiate Instances and Practice Using Instance Methods

Great! We've defined our classes and our instance methods. Now, in this section we are going to actually use them!

Start by instantiating two drivers and two passengers. Assign the drivers to the variables daniel and meryl and assign the passengers to niky and terrance.

daniel = None # driver
meryl = None # driver
niky = None # passenger
terrance = None # passenger

Alright, we have our passengers and drivers! Now we need to put those instance methods to use. Try them out and assign the return values to variables below. Let's 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)

Summary

In this lab, we practiced defining classes and instance methods. We then instantiated instances of our classes and used them to practice calling our instance methods.

python-instance-methods-lab-nyc-career-ds-062518's People

Contributors

tkoar avatar cpease00 avatar cutterbuck avatar

Watchers

James Cloos 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.