SEI-Example
  • Introduction
  • About These Notes
  • Syllabus
  • Development Workflow
    • Installfest
      • Mac OSX
      • Linux
      • Git Configuration
      • Sublime Packages
    • Command Line
      • The Terminal
      • Filesystem Navigation
      • File Manipulation
      • Additional Topics
    • Intro to Git
      • Version Control
      • Local Git
      • Remote Git
      • Git Recipes
    • Group Collaboration
      • Git Workflows
      • Project Roles and Tools
    • VS Code Tips & Tricks
  • HTML/CSS
    • HTML
    • CSS Selectors
    • CSS Box Model and Positioning
      • Box Model
      • Display and Positioning
      • Flexbox
      • Grid
      • Flexbox & Grid Games
      • Floats and Clears
      • Additional Topics
    • Advanced CSS
      • Responsive Design
      • Pseudo-Classes/Elements
      • Vendor Prefixes
      • Custom Properties
      • Additional Topics
    • Bootstrap
    • CSS Frameworks
    • Accessibility
  • JavaScript
    • Primitives
    • Arrays
    • Objects
    • Control Flow
      • Boolean Expressions
      • Conditionals
      • Loops
      • Promises
    • Functions
      • Callbacks
      • Timing Functions
      • Iterators
    • DOM and Events
    • DOM Manipulation
    • HTML5 Canvas
    • How To Reduce Redundancy
    • (2019) JavaScript OOP
    • (2016) OOP with Classes
    • (1995) OOP with Prototypes
      • Constructors
      • Prototypes
    • Intro to TDD
    • Scoping
    • Inheritance
      • Prototypal Inheritance
      • Call, Apply, and other Functions
      • ES6 Inheritance
      • Resources
    • Custom Node Modules
    • Additional Topics
      • AJAX, Fetch, and Async/Await
      • AJAX w/JSON and Localstorage
        • AJAX w/JSON
        • Local Storage
      • Async module
      • Data Scraping
  • jQuery
    • Intro
      • DOM Manipulation
      • Reddit Practice
      • Styling
      • Events
    • Plugins
    • AJAX
  • APIs
    • Fetch
    • AJAX w/jQuery
    • AJAX w/Fetch
  • Databases
    • Intro to SQL
    • Advanced SQL
    • MongoDB
      • Intro to NoSQL
      • CRUD in MongoDB
      • Data Modeling
      • Intermediate Mongo
  • Node/Express
    • Node
      • Intro to Node
      • Node Modules
      • Node Package Manager (NPM)
    • Express
      • Intro to Express
        • Routes
        • Views
        • Templates
        • Layouts and Controllers
        • CRUD & REST
          • Get and Post
          • Put and Delete
      • APIs with Express (request)
      • APIs with Express (axios)
    • Sequelize
      • Terminology
      • Setup
      • Using Models
      • Seeding Data
      • Validations and Migrations
      • Resources
      • 1:M Relationships
      • N:M Relationships
    • Express Authentication
      • Research Components
      • Code Components
      • Auth in Theory
        • Sessions
        • Passwords
        • Middleware
        • Hooks
      • Auth in Practice
        • Create the User
        • User Signup
        • Sessions
        • User Login
        • Authorization and Flash messages
    • Testing with Mocha and Chai
    • Mongoose
      • Mongoose Associations
    • JSON Web Tokens
      • Codealong
    • Additional Topics
      • oAuth
      • Geocoding with Mapbox
      • Geocoding and Google Maps
      • Cloudinary
      • Websockets with Socket.io
      • SASS
  • Ruby
    • Intro to Ruby
    • Ruby Exercises
    • Ruby Classes
    • Ruby Testing with Rspec
    • Ruby Inheritance
    • Ruby Data Scraping
  • Ruby on Rails
    • Intro to Rails
    • APIs with Rails
    • Asset Pipeline
    • Rails Auth and 1-M
      • Auth Components
    • Rails N:M
    • ActiveRecord Polymorphism
    • Additional Topics
      • oAuth
      • SASS
      • Rails Mailers
      • Cloudinary
      • Jekyll
  • React (Updated 2019)
    • ES6+/ESNext
      • Const and Let
      • Arrow Functions
      • Object Literals and String Interpolation
      • ES6 Recap
      • ES6 Activity
    • Intro to React
      • Create React App
      • Components and JSX
      • Virtual DOM
      • Props
      • Dino Blog Activity
      • Nested Components
      • Lab: LotR
    • React State
      • Code-Along: Mood Points
      • Code-Along: Edit Dino Blog
      • Lab: Simple Calc
      • Lifting State
    • React Router
      • Browser History/SPAs
      • React Router (lesson and full codealong)
      • Router Lab
    • Fetch and APIs
      • APIs with Fetch and Axios
      • Fetch the Weather
    • React Hooks
    • React LifeCycle
      • Lab: Component LifeCycle
    • React Deployment
    • Additional Topics
      • React Frameworks
        • Material UI Theming
      • Typescript
        • More Types and Syntax
        • Tsconfig and Declaration Files
        • Generics with Linked List
      • Redux
      • TypeScript
      • Context API
      • React Native
  • Meteor
  • Deployment and Config
    • Deploy - Github Pages
    • Deploy - Node/Sequelize
    • Deploy - Node/MongoDB
    • Deploy React
    • Deploy - Rails
      • Foreman (Environment Variables)
    • Deploy - AWS Elastic Beanstalk
    • Deploy - S3 Static Sites
    • Deploy - Django
    • Deploy - Flask
  • Data Structures and Algorithms
    • Recursion
    • Problem Solving - Array Flatten
    • Binary Search
    • Algorithm Complexity
    • Stacks and Queues
    • Bracket Matching
    • Ruby Linked Lists
      • Sample Code
      • Beginner Exercises
      • Advanced Exercises
    • JS Linked Lists
      • Sample Code
      • Beginner Exercises
      • Beginner Solutions
    • Hash Tables
    • Intro to Sorting
    • Insertion Sort
    • Bucket Sort
    • Bubble Sort
    • Merge Sort
    • Quick Sort
    • Heap Sort
    • Sorting Wrapup
    • Hashmaps
    • Trees and Other Topics
  • Python
    • Python Installation
    • Intro to Python
    • Python Lists
    • Python Loops
    • Python Dictionaries
    • Python Sets and Tuples
    • Python Cheatsheet
    • Python Functions
    • Python Classes
    • Python Class Inheritance
    • Intro to Flask
    • Intro to SQLAlchemy
      • Flask and SQLAlchemy
    • Using PyMongo
    • Intro to Django
    • CatCollector CodeAlong
      • URLs, Views, Templates
      • Models, Migrations
      • Model Form CRUD
      • One-to-Many Relations
      • Many-to-Many Relations
      • Django Auth
    • Django Cheatsheet
    • Django Auth
    • Django Polls App Tutorial
    • Django School Tool Tutorial
    • Django 1:M Relationships
    • Custom Admin Views
    • Data Structures and Algorithms
      • Recursion
      • Binary Search
      • Stacks and Queues
      • Linked Lists
      • Binary Trees
      • Bubble Sort
      • TensorFlow & Neural Networks
    • Adjacent Topics
      • Raspberry Pi
      • Scripting
  • Assorted Topics
    • History of Computer Science
    • Regular Expressions
    • Intro to WDI (Course Info)
    • Being Successful in WDI
    • Internet Fundamentals
      • Internet Lab
    • User Stories and Wireframing
      • Wireframing Exercise: Build an Idea
    • Post WDI
      • Learning Resources
      • Deliverables -> Portfolio
      • FAQ
  • Projects
    • Project 1
    • Project 2
    • Project 3
      • Project 3 Pitch Guidelines
    • Project 4
    • Past Projects
      • Project 1
      • Project 2
      • Project 3
      • Project 4
      • Portfolios
    • Post Project 2
    • MEAN Hackathon
      • Part 1: APIs
      • Part 2: Angular
    • Portfolio
  • Web Development Trends
  • Resources
    • APIs and Data
    • Tech Websites
    • PostgreSQL Cheat Sheet
    • Sequelize Cheat Sheet
    • Database Administration
  • Archived Section
    • (Archived) ReactJS
      • Intro to React
        • Todo List Codealong
        • Additional Topics
      • Deploy React
      • React with Gulp and Browserify
        • Setting up Gulp
        • Additional Gulp Tasks
      • React Router
        • OMDB Router
        • OMDB Search
        • Additional Resources
      • React Animations
        • CSS Animations
    • AngularJS
      • Intro to AngularJS
        • Components and SPA
        • Create an Angular App
      • Angular Directives and Filters
      • Angular Animation
      • Angular Bootstrap Directives
        • Bootstrap Modals
      • Angular $http
      • Angular Services
        • Service Recipes
        • ngResource
        • Star Wars Codealong
      • Angular Routing
      • Angular + Express
      • Angular Authentication
        • Additional Topics
      • Angular Components
      • Angular Custom Filters
      • Angular Custom Directives
Powered by GitBook
On this page
  • Objectives
  • The Basics
  • def keyword
  • Parameters and Arguments
  • A function with one parameter
  • Multiple Parameters
  • Named Arguments
  • Default Values
  • Global vs Local Variables
  • Activity
  1. Python

Python Functions

Objectives

After this lesson students will be able to

  • Write a Python function using the def keyword

  • Include parameters and return statements in a Python function

  • Describe the use of the global keyword

  • Use named arguments and default values for arguments to a function

The Basics

A function in Python has the same purpose as a function in other languages: To write reusable code that can be run at any time after the function as been written. If you're hopping over to Python after using JavaScript, you'll be relieved to hear that there is only really one way in Python to write functions!

def keyword

The def keyword is used much like the function keyword in JavaScript. It's used to declare or define the function. A function that prints "Hello world" in Python looks like this:

def greeting():
  print('Hello world')

Notice the parts of the function:

  • def: Keyword that indicates this is a function definition

  • greeting: The name of the function (can be alphanumeric + underscores - see details below)

  • (): An empty set of parentheses indicates that no data is expected to be passed into this function. Any parameters we want to define would go here

  • :: The colon here is important. It indicated the end of the function signature and the start of the function's code

  • print('Hello world'): This is the actual code that the function contains.

Aside: Naming a Python function

Naming conventions for functions in Python follow many of the same guidelines as naming variables. Your function in Python should:

  • Start with a letter or underscore

  • Contain alphanumeric characters and underscores

  • Be lowercase (use snake case: like_this instead of camel case or title case: notLikeThis)

  • Not be a reserved keyword in Python (for example, no function can be named def or if)

  • Be concise. Python functions can be any length within reason, but try to keep the names short and to the point. Try to make the function name informative about what it does.

Aside: Stubbing a function

In JavaScript or another language, you may have done a process called "stubbing". This is a practice where you'd define functions, classes, routes, etc., with the intention of writing the actual code inside them later. It can be a useful practice when you want to plan out the overall structure of your program before getting into the line-by-line details of your code.

function greeting() {
  // TODO: Write this code later
}

However, if you try to run this code in Python, you will get an error:

def greeting():
  # TODO: This is a job for future me!

The error you get looks like this:

def greeting():
  # TODO: This is a job for future me!
  pass # This let's the function have one line of code that just does nothing

Parameters and Arguments

Parameters and arguments both describe data that goes into a function. Generally the subtle difference is that arguments are the bits of data you pass in, and parameters are the names that the function uses to understand the data that was passed in to it.

A function with one parameter

Here's a function with one parameter - let's modify our greeting function from above to allow a person's name to be passed in.

def greeting(name):
  print('Hello', name)

greeting('Erin')
greeting('Paolo')
greeting('Tanya')

We've called (executed) the function 3 times with different names. Expected Output:

Hello Erin
Hello Paolo
Hello Tanya

In the above example, name is the parameter. It's the data as defined within the function. Arguments on the other hand are the data that is passed in when the function is called. In this case, 'Erin', 'Paolo', and 'Tanya' are all arguments being passed into the greeting function.

Multiple Parameters

Creating a function with multiple parameters in Python is a lot like creating a function with only one parameter. It's important to note that unless otherwise specified (see named arguments) the arguments are understood to be in order. Consider the following code:

def about_me(fave_food, fave_animal, fave_place):
  print('I love to eat', fave_food, 'while petting my', fave_animal, 'at', fave_place)

about_me('sushi', 'cat', 'the beach')

The expected output is "I love to eat sushi while petting my cat at the beach". This is because "sushi", "cat", and "the beach" are provided in order corresponding to the parameters fave_food, fave_animal, and fave_place. Therefore, the values are assigned like this:

  • fave_food = "sushi"

  • fave_animal = "cat"

  • fave_place = "the beach"

What happens if we mess up the order? For example, what is the output when the function is called like this:

about_me("the beach", "sushi", "cat")

The output is a goofy non-sensical sentence because the parameters have been assigned like this:

  • fave_food = "the beach"

  • fave_animal = "sushi"

  • fave_place = "cat"

What if we want or need to provide them out of order? What we need is named arguments!

Named Arguments

Consider the above example code. When we tried to call the function with the arguments out of order, we got a mess:

about_me("the beach", "sushi", "cat")

However, Python allows us to specify the names of the parameters if we wish. Try running this code:

about_me(fave_place="the beach", fave_food="sushi", fave_animal="cat")

Default Values

Typically in Python, if we pass a wrong number of arguments in, we'll get an error - unlike JavaScript which just allows undefined arguments! However sometimes having a default value or an optional argument can be useful. For example, perhaps if a user does not provide a piece of information, we can make an assumption about what it is. If we have a function that stores user information and they don't provide whether their phone number is a work, home, or cell number, we can infer that it's a cell number.

def accept_phone(number, phone_type):
  print('The phone number', number, 'is a', phone_type, 'phone')

We can pass arguments as normal:

accept_phone('555-1234', 'home')
accept_phone('555-5678', 'cell')
accept_phone('555-8765', 'work')

But what if we'd like to pass arguments like this and just assume that the type of phone is a cell?

accept_phone('555-1122')

As the function is currently written we'll get an error telling us we've passed the wrong number of arguments. We can modify the function to accept a default of "cell" for the second argument.

def accept_phone(number, phone_type="cell"):
  print('The phone number', number, 'is a', phone_type, 'phone')

With this modification, test out the following functions. What do they print? Do they behave as expected?

accept_phone('555-1234', 'home')
accept_phone('555-5678', 'cell')
accept_phone('555-8765', 'work')
accept_phone('555-1122')

Global vs Local Variables

Python functions have their own scope, which can house separate variabled from the parent scope. If you want to use a global variable within your function, it will typically work as expected, however it is a good practice to indicate which values are global and in certain cases you will need to use it to disambiguate when there is a global and local variable with the same name.

Activity

Run each of the following code snippets. What prints out? Was it what you expected? Why or why not?

Scenario 1: Only a global variable

def greeting():
  print('Hello', name)

name = 'Marco'
greeting()

Scenario 2: A global variable and a local variable

def greeting():
  name = 'Maria'
  print('Hello', name)

name = 'Marco'
greeting()

Scenario 3: Using both variables

def greeting():
  print('Hello', name)
  name = 'Maria'
  print('Hello', name)

name = 'Marco'
greeting()

Scenario 4: Is this allowed?

def greeting():
  global name
  print('Hello', name)
  name = 'Maria'
  print('Hello', name)

name = 'Marco'
greeting()
PreviousPython CheatsheetNextPython Classes

Last updated 3 years ago

You can look into for more info about naming conventions in Python. Also, need a keyword list? .

Syntax Error

In order to make a stub in Python what we want to use is the . As you might have expected given the name, the pass keyword does nothing. It's a placeholder, exactly what we want! Let's modify our Python stub above with the pass keyword to make the code functional again.

You can .

more details here
Check this out
pass keyword
read more on it here