Git Product home page Git Product logo

cssi-prework-python-multiple-and-default-arguments's Introduction

Multiple and Default Parameters in Python Functions

Objectives

  • Define and use functions that have multiple parameters
  • Define and use functions that have default arguments
  • Describe the purpose of default arguments in Python

Overview

We will often want to create and use functions that take in multiple pieces of infomation. For this we add additional parameters during function definition. In other cases, we want to add an argument in only certain situations - we'll create default arguments to solve for these cases.

Multiple Arguments

Let's say we want to create a function that takes in a user's name, favorite food, and favorite programming language. We can do this by creating our function and separating our parameters using commas within the parentheses:

def long_greeting(name, favorite_food, language):
    return "Hello " + name + ", we have some " + favorite_food + " for you, but first you'll have to write FizzBuzz in " + language + "!"

>>> print long_greeting("Danny", "Lasagna", "Ruby")
"Hello Danny, we have some Lasagna for you, but first you'll have to write FizzBuzz in Ruby!""

Pretty simple - separate multiple parameters using comments. You can use as many parameters as you need.

Default Arguments

Sometimes it is useful to have a "default" parameter in cases where the user doesn't give us one. For example, I'm building a airfare search function. I'd like the class of seats to default to economy unless otherwise specified:

def airfare_search(start_point, destination, airline_class = "economy"):
    return "searching for " + airline_class + " class tickets from " + start_point + " to " + destination

>>> print airfare_search("New York", "Mumbai", "first")
"searching for first class tickets from New York to Mumbai"

>>> print airfare_search("Aruba", "Lima")
"searching for economy class tickets from Aruba to Lima"

Notice that we were able to completely omit the airline class in the second invocation of the method, and it defaulted to "economy". This is because we set the default argument in the parameter as airline_class = "economy".

Can you think of situations where you'd need to use default arguments in a function?

cssi-prework-python-multiple-and-default-arguments's People

Contributors

annjohn avatar dfenjves avatar

Watchers

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

cssi-prework-python-multiple-and-default-arguments's Issues

Questions

This jumps right into string formatting and interpolation, without going in depth into the why. Could you add some more context into that first section. i.e. after this sentence "String interpolation lets us pass different data into a string, which is faster and more convenient than keeping track of different pieces of string and concatenating them ourselves" showing and example of how to do something without formatting and interpolation and showing the pains with that, so that it sets you up for the rest of the content.

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.