Tutorials
Hands-On Python
Hands-On Python
  • Hands-On Python Tutorial For Real-World Business Analytics Problems
  • Preface
    • Section I. A Note From The Author
    • Section II. Tutorial Overview
    • Section III. What Is The Preflight Checklist?
    • Section IV. Supplimentery Material
  • Preflight Checklist
    • Section V. Select Your Difficulty Setting
    • Section VI. Download Anaconda
    • Section VII. Download PyCharm (Optional)
    • Section VIII. Download SQL Server Developer Edition
    • Section IX. Configure Database Environment
    • Section X. Download The Source Code
    • Section XI. Starting JupyterLab
    • Section XII. How To Get Help With This Tutorial
  • Language Basics
    • Lesson 1. Obligatory Hello World
    • Lesson 2. Code Comments
    • Lesson 3. Data Types
    • Lesson 4. Variables
    • Lesson 5. String Concatenation
    • Lesson 6. Arithmetic Operators
    • Lesson 7. Making Decisions
    • Lesson 8. Control Flow With if-elif-else
    • Lesson 9. Control Flow With while
    • Lesson 10. Data Structures Part I: List
    • Lesson 11. Data Structures Part II: Tuples
    • Lesson 12. Data Structures Part III: Dictionaries
    • Lesson 13. Looping With for
    • Lesson 14. Functions
    • Lesson 15. Importing Modules
    • Lesson 16. Python Programming Standards
  • Advanced Topics
    • Lesson 17. Functional Programing With map
    • Lesson 18. Generators
    • Lesson 19. Comprehensions
    • Lesson 20. Basic File Operations
    • Lesson 21. Working With Data In Numpy
    • Lesson 22. Working With Data In Pandas
    • Lesson 23. Working With JSON
    • Lesson 24. Making File Request Over HTTP And SFTP
    • Lesson 25. Interacting With Databases
    • Lesson 26. Saving Objects With Pickle
    • Lesson 27. Error Handling
    • Lesson 28. Bringing It All Together
  • Solutions To Real World Problems
    • Lesson 29. Download A Zip File Over HTTP
    • Lesson 30. Looping Over Files In A Directory
    • Lesson 31. Convert Comma Delmited Files To Pipe Delimited
    • Lesson 32. Combining Multiple CSVs Into One File
    • Lesson 33. Load Large CSVs Into Data Warehouse Staging Tables
    • Lesson 34. Efficiently Write Large Database Query Results To Disk
    • Lesson 35. Working With SFTP In The Real World
    • Lesson 36. Executing Python From SQL Server Agent
Powered by GitBook
On this page
  • Examples
  • Now you try it!
  1. Advanced Topics

Lesson 17. Functional Programing With map

Functional Programming

Functional programming gives me migraines. I made a legit run at it and I could not wrap my brain around it. OO 4 lyfe yo!

What am I talking about? Programming paradigms.

In this tutorial, I am teaching you to write “scripts”. The word scripts usually refers to a set of instructions written in a non-compiled programming language that execute one after another. Scripts are usually relatively small programs that automate relatively simple and repetitive task. A good example is bash scripts or PowerShell scripts that are created to do things like move files around a network on a regular schedule. These scripts are usually kept in a single file.

I will not be teaching you how to write full blow Python programs in this tutorial. Being able to write full blown programs requires more knowledge than I can drop in a medium this simple. Full blow programs are huge and require many files to work together properly. How you code these files is called a programming paradigm.

There are a handful of paradigms. My OO comment earlier was a reference to object-oriented programming which is the dominant paradigm in modern software development. In recent years, functional programming has started to become more popular.

Functional programming consists of writing software in such a manner that functions are first class citizens. Everything is a function. If you spend your life in OO, moving to functions is a real mind bender. However, it does come in handy sometimes.

Sometimes you need to run the same operation over numerous data points. There are various ways to do that, but the most efficient way is to borrow some concepts from functional programing.

Functional programming is supported in Python. If you move into data science or learn to use Apache Spark, you are going to need a function called map.

Lambda Functions

The functions that we wrote in lesson 14 all had names. There is another way to write functions and that is anonymously.

Anonymous functions do not have names. The come in handy because they really reduce the code you have to write when the task is simple. You can fit an anonymous function on a single line.

Anonymous functions are implanted by using the lambda construct instead of def like with named functions.

map

Now that I have given you all that exposition, I can finally talk about map.

The map function lets you apply a function over an iterable object like a list. The map function takes a function as an argument and the iterable that the function is to be applied to.

Examples

Example #1 This Example Has No Name

Below is an example of an anonymous function. First, we review example 3 of lesson 14. Then we re-engineer it as an anonymous function. As you can see, the same thing can be achieved with much less code.

#original function
def divide(numerator, denominator):
    quotient = numerator / denominator
    return int(quotient)

print(divide(10,5))
#SHAZAM!
divide = lambda x,y: int(x / y)

print(divide(10,5))

Example #2 A Different Kind Of Loop

This example does not show the real power of map. We will not see that until we start using Python for data analysis. For now, here is a different way to iterate over a list of values.

The map function simply returns an object so before we can print it to the screen, we have to turn it into a list.

count = [1,2,3,4,5]

newcount = list(map(lambda x: x + 1, count))

print(newcount)

Now you try it!

Don't copy and past. Type the code yourself!

PreviousAdvanced TopicsNextLesson 18. Generators

Last updated 3 years ago