Coder Social home page Coder Social logo

ds-skills-1-1-functions-london-ds-091018's Introduction

Defining Functions

We've briefly seen a little bit of defining functions but will investigate more details and nuances here. Recall that you can define a function using the built in keyword def followed by a function name and any required parameters. Within an indendented block you then define what happens during the function call. Finally, any useful function should then return some output. All together we have:

def function_name(parameter1, parameter2):
    #What the function does
    return output

Functions, Variables, Namespaces and Scope

We've already seen many examples of functions and variables. After importing some packages, our very first line of code was roughly:
df = pd.read_csv('filename.csv'). In this, we called a built in function (also called a method) from within the pandas package. This demonstrates the concept of a namespace; read_csv was only defined under the pandas (pd) namespace, and was not globally accessible. Similarly, we will start to see that what a variable refers to depends on context and what namespace is currently active.

# This is a global variable
a = 0

if a == 0:
    # This is still a global variable
    b = 1
c = 5 #Globally defined
c #Calling the global variable
5

Undefined Variables Produce Errors

d
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-7-e983f374794d> in <module>()
----> 1 d


NameError: name 'd' is not defined

Variables within Functions

def my_function(c):
    # this is a local variable
    d = 3
    print('C is:', c) #Look for c within the function....if not then it will look in the global sense
    print('D is:', d)
# Now we call the function, passing the value 7 as the first and only parameter
my_function(c=7)
C is: 7
D is: 3
#Notice that globally, c is still 5
c
5
#Notice that globally, d is still not defined!
d
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-13-86fe4be54280> in <module>()
      1 #Notice that globally, d is still not defined!
----> 2 d


NameError: name 'd' is not defined
#Notice this function doesn't return anything. It only prints.
output = my_function(4)
C is: 4
D is: 3
output
type(output)
NoneType
print(output)
None

Describe the scope of a, b and c below.

def my_function(a):
    b = a - 2
#     print(b)
    return b
    #Technically inside the function.
    #This would never execute because
    #as soon as we hit a return statement
    #the function exits (and returns what we asked)
    print('Stuff that never happens.')
#Your description here

Exercise 2

A.

Write a Python program to convert a temperature given in degrees Fahrenheit to its equivalent in degrees Celsius. You can assume that T_c = (5/9) x (T_f - 32), where T_c is the temperature in °C and T_f is the temperature in °F. Your program should ask the user for an input value, and print the output. The input and output values should be floating-point numbers.

B.

What could make this program crash? What would we need to do to handle this situation more gracefully?

#Your code here

Lambda Functions

As a final note on functions, we've also preview writing quick throwaway functions using python's lambda method. Lambda functions cannot be reused, but provide a quick and easy way to both define and implement a function simultaneously. Our previous outline of functions,

def function_name(parameter1, parameter2):
    #What the function does
    return output

becomes:

lambda x: output

Notice that everything in a lambda function must happen in a single line. For more complex functions, you will still need to define a seperate function. However, you could still pass your function through a lambda pattern like so:

lambda x: your_func(x)

Practice Translating Functions

Practice your programming syntax by rewriting you farenheight to celsius function above as a lambda function.

f_temps = [32, 212, 0, 40, 50, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105]
c_temps = f_temps.map(lambda x: #Rewrite your temperature function in a single line here)

ds-skills-1-1-functions-london-ds-091018's People

Contributors

mathymitchell 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

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.