Git Product home page Git Product logo

dsc-1-02-10-creating-functions-lab-online-ds-pt-041519's Introduction

Creating Functions - Lab

Introduction

As you know, we can use functions to name snippets of our code, thus making our code more expressive. We can also use functions to allow us to reuse our code. In this lab we will practice using functions for both of those purposes.

Objectives

You will be able to:

  • Create and use your own custom functions

Instructions:

Writing our first functions

Imagine we are creating a list of travel destinations -- which can really turn out to be a full time job if we like to travel. We have our list of travel_destinations which we assign below. Write a function called number_of_destinations that returns the number of destinations we have on our list.

travel_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
# define function here

Below, remove the first # to uncomment the following line(s) of code and then press shift + enter to run the cell

# number_of_destinations() # 6
6

Now write another function called next_up that returns our first destination (the destination with the lowest index), in the list_of_destinations list.

# define function here

Below, remove the first # to uncomment the following line(s) of code and then press shift + enter to run the cell

list_of_destinations = ['argentina', 'canada', 'croatia']
# next_up() # 'argentina'
'argentina'

Ok, now write a function called favorite_destination that returns the string 'madagascar'.

# define function here

Below, remove the first # to uncomment the following line(s) of code and then press shift + enter to run the cell

# favorite_destination() # 'madagascar'
'madagascar'

Again, let's declare a list called favorite_destinations. Write a new function called add_favorite_destination that also returns the string 'madagascar', but adds the string 'madagascar' to the end of the list, favorite_destinations, as well.

# define function here

Below, remove the first # to uncomment the following line(s) of code and then press shift + enter to run the cell

favorite_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
# add_favorite_destination()
favorite_destinations[-1] # 'madagascar'
['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia', 'madagascar']





'madagascar'

Now let's write another function called capitalize_countries which iterates through the list of capitalized_destinations and capitalizes the first letter of each word. It should return a list of capitalized destinations.

capitalized_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
# define function here

Below, remove the first # to uncomment the following line(s) of code and then press shift + enter to run the cell

# capitalize_countries() # ['Argentina', 'Mexico', 'Italy', 'Finland', 'Canada', 'Croatia']
['Argentina', 'Mexico', 'Italy', 'Finland', 'Canada', 'Croatia']

Great! Now if someone adds a country that is lowercased to our list of destinations, we can simply call our function again to capitalize each of the destinations in the list.

Summary

Great job! In this lab we were able to get practice both creating and returning values from functions. We also practiced accessing variables not local to the function but in the global scope.

dsc-1-02-10-creating-functions-lab-online-ds-pt-041519's People

Contributors

tkoar avatar peterbell avatar

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar  avatar Joe Cardarelli avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Scott Ungchusri avatar Nicole Kroese  avatar Lore Dirick avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton 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.