Lesson 22. Advanced operations with functions

 

After reading lesson 22, you’ll be able to

  • Pass functions (as an object) as a parameter to another function
  • Return a function (as an object) from another function
  • Understand which variables belong to which scope based on certain rules

Before formally learning about functions in lesson 21, you saw and used functions in simple code. Here are some of the functions you’ve been using already:

  • len()—For example, len("coffee")
  • range()—For example, range(4)
  • print()—For example, print("Witty message")
  • abs(),sum(),max(),min(),round(),pow()—For example, max(3,7,1)
  • str(),int(),float(),bool()—For example, int(4.5)
Consider this

For each of the following function calls, how many parameters does the function take in, and what’s the value of type returned?

  • len("How are you doing today?")
  • max(len("please"), len("pass"), len("the"), len("salt"))
  • str(525600)
  • sum((24, 7, 365))

Answer:

  • Takes in one parameter, returns 24
  • Takes in four parameters, returns 6
  • Takes in one parameter, returns "525600"
  • Takes in one parameter, returns 396

22.1. Thinking about functions with two hats

 
 
 

22.2. Function scope

 
 
 

22.3. Nesting functions

 
 
 

22.4. Passing functions as parameters

 

22.5. Returning a function

 
 
 

22.6. Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage