Git Product home page Git Product logo

introduction-to-functions-lab-data-science-alpha's Introduction

Functions lab

As we know, we can use functions to give a name to sequences of our code, to make it more expressive. We can also use functions to allow us to reuse code. In this lab we will get practice in using functions for these purposes.

Objectives

  • Practice declaring and returning values from function
  • Practice accessing variables outside of a function's scope, from inside of a function

Writing our first functions

Imagine we work in a diner. We have a list of orders which we assign below. Write a function called number_of_orders that returns the current number of orders.

orders = ['turkey sandwich', 'eggs']
def number_of_orders():
    pass
number_of_orders() # 2

Now write another function called next_up that returns the first order (the order with the lowest index), in the orders list.

def next_up():
    pass
orders = ['turkey sandwich', 'eggs']
next_up() # 'turkey sandwich'

orders = ['beet salad', 'fennel']
next_up() # 'beet salad'

Ok, now write a function called healthy_order that returns the string 'spinach salad'.

def healthy_order():
    pass
healthy_order() # 'spinach salad'

Now let's declare an array called orders. Change the function healthy_order so that it continues to return the string 'spinach salad', but also adds the string 'spinach salad' to the end of the list of orders.

orders = ['turkey sandwich', 'eggs']
healthy_order()
orders[-1] # 'spinach salad'
'eggs'

Now let's write another function iterates through the list of orders and capitalizes the first letter of each word in the order. It should return a list of capitalized orders.

orders = ['spinach salad', 'turkey sandwich']
def capitalize_orders():
    pass
capitalize_orders() # ['Spinach Salad', 'Turkey Sandwich']

Now if someone adds, an upcapitalized order, we can simply call our function again to return a list of capitalized orders.

orders = ['spinach salad', 'turkey sandwich']
capitalize_orders() # ['Spinach Salad', 'Turkey Sandwich']
orders.append('eggs')
capitalize_orders() # ['Spinach Salad', 'Turkey Sandwich', 'Eggs']

Summary

Great job. Through this lab you were able to get practice both writing and returning values from functions. You also practiced accessing variables not local to the function, but in the global scope.

introduction-to-functions-lab-data-science-alpha's People

Contributors

jeffkatzy avatar

Watchers

James Cloos avatar Chris D'Ascoli 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.