Learn Functional Python in 10 Minutes

Learn Functional Python in 10 Minutes

In this article, you’ll learn what the functional paradigm is as well as how to use functional programming in Python. You’ll also learn about list comprehensions and other forms of comprehensions.

Functional paradigm

In an imperative paradigm, you get things done by giving the computer a sequence of tasks and then it executes them. While executing them, it can change states. For example, let’s say you originally set A to 5, then later on you change the value of A. You have variables in the sense that the value inside the variable varies.

In a functional paradigm, you don’t tell the computer what to do but rather you tell it what stuff is. What the greatest common divisor of a number is, what the product from 1 to n is and so on.

Because of this, variables cannot vary. Once you set a variable, it stays that way forever (note, in purely functional languages they are not called variables). Because of this, functions have no side effects in the functional paradigm. A side effect is where the function changes something outside of it. Let’s look at an example of some typical Python code:

a = 3
def some_func():
    global a
    a = 5

some_func()
print(a)

The output for this code is 5. In the functional paradigm, changing variables is a big no-no and having functions affect things outside of their scope is also a big no-no. The only thing a function can do is calculate something and return it as a result.

Now you might be thinking: “no variables, no side effects? Why is this good?”. Good question, gnarly stranger reading this.

If a function is called twice with the same parameters, it’s guaranteed to return the same result. If you’ve learnt about mathematical functions, you’ll know to appreciate this benefit. This is called referential transparency. Because functions have no side effects, if you are building a program which computes things, you can speed up the program. If the program knows that func(2) equates to 3, we can store this in a table. This prevents the program from repeatedly running the same function when we already know the answer.

Typically, in functional programming, we do not use loops. We use recursion. Recursion is a mathematical concept, usually, it means “feeding into itself”. With a recursive function, the function repeatedly calls itself as a sub-function. Here’s a nice example of a recursive function in Python:

def factorial_recursive(n):
    # Base case: 1! = 1
    if n == 1:
        return 1

    # Recursive case: n! = n * (n-1)!
    else:
return n * factorial_recursive(n-1)

Some programming languages are also lazy. This means that they don’t compute or do anything until the very last second. If you write some code to perform 2 + 2, a functional program will only calculate that when you actually need to use the resultant. We’ll explore laziness in Python soon.

Map

To understand map, let’s first look at what iterables are. An iterable is anything you can iterate over. Typically these are lists or arrays, but Python has many different types of iterables. You can even create your own objects which are iterable by implementing magic methods. A magic method is like an API that helps your objects become more Pythonic. You need to implement 2 magic methods to make an object an iterable:

class Counter:
    def __int__(self, low, high):
        # set class attributes inside the magic method __init__
        # for "inistalise"
        self.current = low
        self.high = high

    def __iter__(self):
        # first magic method to make this object iterable
        return self
    
    def __next__(self):
        # second magic method
        if self.current > self.high:
            raise StopIteration
        else:
            self.current += 1
return self.current - 1

The first magic method, “iter” or dunder iter (double underscore iter) returns the iterative object, this is often used at the start of a loop. Dunder next returns what the next object is.

Let’s go into a quick terminal session and check this out:

for c in Counter(3, 8):
print(c)

This will print

3
4
5
6
7
8

In Python, an iterator is an object which only has an iter magic method. This means that you can access positions in the object, but cannot iterate through the object. Some objects will have the magic method next and not the itermagic method, such as sets (talked about later in this article). For this article, we’ll assume everything we touch is an iterable object.

So now we know what an iterable object is, let’s go back to the map function. The map function lets us apply a function to every item in an iterable. Typically we want to apply a function to every item in a list, but know that it’s possible for most iterables. Map takes 2 inputs, the function to apply and the iterable object.

map(function, iterable)

Let’s say we have a list of numbers like so:

[1, 2, 3, 4, 5]

And we want to square every number, we can write code like this:

x = [1, 2, 3, 4, 5]
def square(num):
    return num*num

print(list(map(square, x)))

Functional functions in Python are lazy. If we didn’t include the “list()” the function would store the definition of the iterable, not the list itself. We need to explicitly tell Python “turn this into a list” for us to use this.

It’s a bit weird to go from non-lazy evaluation to lazy evaluation all of a sudden in Python. You’ll eventually get used to it if you think more in the functional mindset than an imperative mindset.

Now it’s nice to write a normal function like “square(num)” but it doesn’t look right. We have to define a whole function just to use it once in a map? Well, we can define a function in map using a lambda (anonymous) function.

Lambda expressions

A lambda expression is a one line function. Take, for instance, this lambda expression which squares a number given to it:

square = lambda x: x * x

Now let’s run this:

>>> square(3)

9

I hear you. “Brandon, where are the arguments? what the heck is this? that doesn’t look anything like a function?”

Well, it’s kind of confusing but can be explained. So we’re assigning something to the variable “square”. this part:

lambda x:

Tells Python that this is a lambda function, and the input is called x. Anything after the colon is what you do with the input, and it automatically returns whatever the resultant of that is.

To simplfy our square program into one line we can do:

x = [1, 2, 3, 4, 5]
print(list(map(lambda num: num * num, x)))

So in a lambda expression, all the arguments go on the left and the stuff you want to do with them go on the right. It gets a little messy, no one can deny that. The truth is that there’s a certain pleasure in writing code that only other functional programmers can read. Also, it’s super cool to take a function and turn it into a one-liner.

Reduce

Reduce is a function that turns an iterable into one thing. Typically you perform a computation on a list to reduce it down to one number. Reduce looks like this:

reduce(function, list)

We can (and often will) use lambda expressions as the function.

The product of a list is every single number multiplied together. To do this you would program:

product = 1
x = [1, 2, 3, 4]
for num in x:
product = product * num

But with reduce you can just write:

from functools import reduce

product = reduce((lambda x, y: x * y),[1, 2, 3, 4])

To get the same product. The code is shorter, and with knowledge of functional programming it is neater.

Filter

The filter function takes an iterable and filters out all the things you don’t want in that iterable.

Normally filter takes a function and a list. It applies the function to each item in the list and if that function returns True, it does nothing. If it returns False, it removes that item from the list.

The syntax looks like:

filter(function, list)

Let’s see a small example, without filter we’ll write:

x = range(-5, 5)
new_list = []

for num in x:
    if num < 0:
new_list.append(num)

With filter, this becomes:

x = range(-5, 5)
all_less_than_zero = list(filter(lambda num: num < 0, x))

Higher order functions

Higher order functions can take functions as parameters and return functions. A very simple example would look like:

def summation(nums):
    return sum(nums)

def action(func, numbers):
    return func(numbers)

print(action(summation, [1, 2, 3]))

# Output is 6

Or an even simpler example of the second definition, “return functions”, is:

def rtnBrandon():
    return "brandon"
def rtnJohn():
    return "john"

def rtnPerson():
    age = int(input("What's your age?"))

    if age == 21:
        return rtnBrandon()
    else:
return rtnJohn()

You know earlier how I said that pure functional programming languages didn’t have variables? Well, higher order functions are what makes this easier. You don’t need to store a variable anywhere if all you’re doing is passing data through a long tunnel of functions.

All functions in Python are first class objects. A first class object is defined as having one or more of these features:

  • Created at runtime
  • Assigned tro a variable or element in a data structure
  • Passed as an argument to a function
  • Returned as the result of a function

So all functions in Python are first class and can be used as a higher order function.

Partial application

Partial application is a bit weird, but are super cool. You can call a function without supplying all the arguments it requires. Let’s see this in an example. We want to create a function which takes 2 arguments, a base and an exponent, and returns base to the power of the exponent, like so:

def power(base, exponent):
return base ** exponent

Now we want to have a dedicated square function, to work out the square of a number using the power function:

def square(base):
return power(base, 2)

This works, but what if we want a cube function? or a function to the power of 4? Can we keep on writing them forever? Well, you could. But programmers are lazy. If you repeat the same thing over and over again, it’s a sign that there is a much quicker way to speed things up and that will allow you to not repeat things. We can use partial applications here. Let’s see an example of the square function using a partial application:

from functools import partial

square = partial(power, exponent=2)
print(square(2))

# output is 4

Isn’t that cool! We can call functions which require 2 arguments, using only 1 argument by telling Python what the second argument is.

We can also use a loop, to generate a power function that works from cubed all the way up to powers of 1000.

from functools import partial

powers = []
for x in range(2, 1001):
  powers.append(partial(power, exponent = x))
  
print(powers[0](3))
# output is 9

Functional programming isn’t Pythonic

You might have noticed, but a lot of the things we want to do in functional programming revolve around lists. Other than the reduce function & partial application, all the functions you have seen generate lists. Guido (the inventor of Python) dislikes functional stuff in Python because Python already has its own way to generate lists.

If you write “import this” into a Python IDLE session, you’ll get:

>>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one — and preferably only one — obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

This is the Zen of Python. It’s a poem about what something being Pythonic means. The part we want to relate to here is:

In Python, map & filter can do the same things as a list comprehension (discussed next) can do. This breaks one of the rules of the Zen of Python, so these parts of functional programming aren’t seen as ‘pythonic’.

Another talking point is Lambda. In Python, a lambda function is a normal function. Lambda is syntactic sugar. Both of these are equivalent:

foo = lamda a: 2
  
def foo(a):
return 2

A regular function can do everything a lambda function can, but it doesn’t work the other way around. A lambda function cannot do everything that a regular function can do.

This was a short argument about why functional programming doesn’t fit into the whole Python ecosystem very well. You may have noticed I mentioned list comprehensions earlier, we’ll discuss them now.

List comprehensions

Earlier, I mentioned that anything you could do with map or filter, you could do with a list comprehension. This is the part where we’ll learn about them.

A list comprehension is a way to generate lists in Python. The syntax is:

[function for item in iterable]

So let’s square every number in a list, as an example:

print([x * x for x in [1, 2, 3, 4]])

Okay, so we can see how we can apply a function to every item in a list. How do we go around applying a filter? Well, look at this code from earlier:

x = range(-5, 5)

all_less_than_zero = list(filter(lambda num: num < 0, x))
print(all_less_than_zero)

We can convert this into a list comprehension like so:

x = range(-5, 5)

all_less_than_zero = [num for num in x if num < 0]

List comprehensions support if statements like this. You no longer need to apply a million functions to something to get what you want. In fact, if you’re trying to make some kind of list chances are that it’ll look cleaner and easier using a list comprehension.

What if we want to square every number below 0 in a list? Well, with lambda, map and filter you’ll write:

x = range(-5, 5)

all_less_than_zero = list(map(lambda num: num * num, list(filter(lambda num: num < 0, x))))

So that’s seems really long and slightly complicated. With a list comprehension it’s just:

x = range(-5, 5)

all_less_than_zero = [num * num for num in x if num < 0]

A list comprehension is only good for, well, lists. Map and filter work on any iterable, so what’s up with that? Well, you can use any comprehension for any iterable object you encounter.

Other comprehensions

You can create a comprehension of any iterable

Any iterable can be generated using a comprehension. Since Python 2.7, you can even generate a dictionary (hashmap).

# Taken from page 70 chapter 3 of Fluent Python by Luciano Ramalho

DIAL_CODES = [
    (86, 'China'),
    (91, 'India'),
    (1, 'United States'),
    (62, 'Indonesia'),
    (55, 'Brazil'),
    (92, 'Pakistan'),
    (880, 'Bangladesh'),
    (234, 'Nigeria'),
    (7, 'Russia'),
    (81, 'Japan'),
    ]

>>> country_code = {country: code for code, country in DIAL_CODES}
>>> country_code
{'Brazil': 55, 'Indonesia': 62, 'Pakistan': 92, 'Russia': 7, 'China': 86, 'United States': 1, 'Japan': 81, 'India': 91, 'Nigeria': 234, 'Bangladesh': 880}
>>> {code: country.upper() for country, code in country_code.items() if code < 66}
{1: 'UNITED STATES', 7: 'RUSSIA', 62: 'INDONESIA', 55: 'BRAZIL'}

If it’s an iterable, it can be generated. Let’s look at one last example of sets. If you don’t know what a set is, check out this other article I wrote. The TLDR is:

  • Sets are lists of elements, no element is repeated twice in that list
  • The order in sets do not matter.
# taken from page 87, chapter 3 of Fluent Python by Luciano Ramalho

>>> from unicodedata import name
>>> {chr(i) for i in range(32, 256) if 'SIGN' in name(chr(i), '')}
{'×', '¥', '°', '£', '©', '#', '¬', '%', 'µ', '>', '¤', '±', '¶', '§', '<', '=', '®', '
You may notice that sets have the same curly braces as dictionaries. Python is really smart. It’ll know whether you’re writing a dictionary comprehension or a set comprehension based on whether you provide the extra value for the dictionary or not. If you want to learn more about comprehensions, check out [this ](http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ "this ")visual guide. If you want to learn more about comprehensions & generators, check out this [article](https://medium.freecodecamp.org/python-list-comprehensions-vs-generator-expressions-cef70ccb49db "article").
# Conclusion

Functional programming is beautiful and pure. Functional code can be clean, but it can also be messy. Some hardcore Python programmers dislike the functional paradigm in Python. You should use what you want to use, use the best tool for the job.




**Originally published by **[Brandon Skerritt](https://hackernoon.com/@brandonskerritt "Brandon Skerritt")** ***at *[hackernoon.com](https://hackernoon.com/learn-functional-python-in-10-minutes-to-2d1651dece6f "hackernoon.com")

===============================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on [**Facebook**](https://www.facebook.com/Pythontutorial4u "**Facebook**") | [**Twitter**](https://twitter.com/codek_tv "**Twitter**")
# Learn More

☞ [Complete Python Bootcamp: Go from zero to hero in Python 3](http://deal.codetrick.net/p/S15_M7e-l "Complete Python Bootcamp: Go from zero to hero in Python 3")

☞ [Complete Python Masterclass](http://deal.codetrick.net/p/SkCfL7xbe "Complete Python Masterclass")

☞ [Learn Python by Building a Blockchain & Cryptocurrency](https://deal.codetrick.net/p/Hkf1gIuaf "Learn Python by Building a Blockchain & Cryptocurrency")

☞ [Python and Django Full Stack Web Developer Bootcamp](http://deal.codetrick.net/p/r1-quFMgce "Python and Django Full Stack Web Developer Bootcamp")

☞ [The Python Bible™ | Everything You Need to Program in Python](http://deal.codetrick.net/p/Skc18mgbl "The Python Bible™ | Everything You Need to Program in Python")

☞ [Learning Python for Data Analysis and Visualization](http://deal.codetrick.net/p/HywrG7e-l "Learning Python for Data Analysis and Visualization")

☞ [Python for Financial Analysis and Algorithmic Trading](http://deal.codetrick.net/p/BkBWKHZtb "Python for Financial Analysis and Algorithmic Trading")

☞ [The Modern Python 3 Bootcamp](http://edupioneer.net/p/HkOjAJUcM "The Modern Python 3 Bootcamp")
, '÷', '¢', '+'}

You may notice that sets have the same curly braces as dictionaries. Python is really smart. It’ll know whether you’re writing a dictionary comprehension or a set comprehension based on whether you provide the extra value for the dictionary or not. If you want to learn more about comprehensions, check out this visual guide. If you want to learn more about comprehensions & generators, check out this article.

Conclusion

Functional programming is beautiful and pure. Functional code can be clean, but it can also be messy. Some hardcore Python programmers dislike the functional paradigm in Python. You should use what you want to use, use the best tool for the job.

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Description
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Quizzes
Assignments
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: