Ricky Martin

Ricky Martin

1587610262

Introduction Four Advanced Function Concepts in Python

Python functions are a block of code that executes particular operations when they’re requested to run, which we term calling a function. Most of the time, we declare and call a function using the following syntax:

>>> # define a function using the def keyword
>>> def add_up(num1, num2):
...     print("called the add_up function")
...     sum_n = num1+num2
...     return sum_n
... 
>>> sum0 = add_up(3, 5)
called the add_up function
>>> print(sum0)
8

In the code above, we declare a function called add_up using the def keyword. This function takes two numbers (num1 and num2) as the input arguments, calculates their sum as the defined operation, and returns this value as the function’s output. Pretty straightforward, right?

Besides these regular functions, there are other kinds of functions in Python that can benefit our development. Let’s review them in this article.

1. Lambda Functions

Lambda functions are small anonymous one-line functions. Sometimes, we can simply refer to them as lambdas. Lambdas have the following syntax:

lambda arguments: expression

We declare a lambda function using the lambda keyword. Following this keyword, we specify the list of arguments, the number of which can vary from zero to multiple. We then specify the operations that we want to perform, which is known as the expression for the lambda function.

As you can see, lambdas have very concise syntax and thus they are most suitable for scenarios where a brief, single-time-use function is needed. Let’s consider the following example. The sorted() function is to sort an iterable based on the key function specified by the key argument:

>>> # define a list of tuples
>>> records = [(1, 'John'), (2, 'Aaron'), (5, 'Ian')]
>>> # sort with a lambda
>>> sorted(records, key=lambda x: len(x[1]))
[(5, 'Ian'), (1, 'John'), (2, 'Aaron')]

In the code above, we first define a list to store the records of some students with their student IDs and names. We then sort them with a lambda function, which sorts the list by the length of the student’s name.

For information about sorting using lambda functions, you can refer to my previous article. It’s also important to know that lambdas can be misused.

2. Closures

Closures are nested functions that capture non-local variables of the outer functions. I don’t think that it’s easy to understand closures conceptually. A thorough understanding requires in-depth knowledge of scopes and functions being first-class objects in Python. For the purpose of this article, let’s have an overall idea of closures by going over a tangible example:

>>> # create a closure
>>> def make_multiplier(coefficient):
...     product = 1
...
...     def multiplier():
...         nonlocal product
...         product *= coefficient
...         return product
...
...     return multiplier
...

In the code above, we define a function called make_multiplier. Because it contains the other function multiplier, we can call make_multiplier an outer function and call multiplier a nested function. The outer function returns the nested function as its return value. Importantly, the nested function uses and modifies a non-local variable (i.e. product) defined within the scope of the outer function. To sum up, there are three key elements to creating closures in Python:

  • Declare a nested function within the scope of the outer function.
  • The binding of non-local variables outside the scope of the nested function.
  • Return the nested function to output the closure function.

How can we use closures? Let’s see the trivial example below:

>>> multipler3 = make_multiplier(3)
>>> multipler3()
3
>>> multipler3()
9
>>> multipler3()
27
>>> multipler3.__code__.co_freevars
('coefficient', 'product')
>>> multipler3.__closure__[1].cell_contents
27

We then declare a closure called multiplier3. Every time we call this closure, the product is multiplied by three. In other words, the closure “remembers” the state of the product after its last use. Related concepts include variable binding and value capturing. We can check the pertinent information by calling __code__.co_freevars and __closure__[1].cell_contents.

3. Decorators

Decorators are functions that extend the behavior of other functions without explicitly modifying them. In essence, decorators are a kind of higher-order function, which is defined as a function that either takes other functions as input or returns other functions as output. Let’s get a real feel for decorators through the example below:

>>> def clap():
...     print("Clap! Clap!")
...
>>> # define a higher order function
>>> def triple_repeat_wrapper(func):
...     def wrapper():
...         print(f"Before calling func {func.__name__}")
...         func()
...         func()
...         func()
...         print(f"After calling func {func.__name__}")
...     return wrapper
...

In the code above, we define two functions (clap and triple_repeat_wrapper), the latter of which is a higher-order function that calls the passed func three times within the nested wrapper function and returns the wrapper function as the higher-order function’s output.

How can we use these functions? As shown in the code below, we create a function called wrapped_clap that passes the clap function to the higher-order function triple_repeat_wrapper. As you can see, calling the wrapped_clap function will result in the code inside the nested wrapper function to be called. Consistent with the printed output, we get to know that the wrapped_clap function references the nested function wrapper.

>>> wrapped_clap = triple_repeat_wrapper(clap)
>>> wrapped_clap()
Before calling func clap
Clap! Clap!
Clap! Clap!
Clap! Clap!
After calling func clap
>>> wrapped_clap
<function triple_repeat_wrapper.<locals>.wrapper at 0x1038f0680>

But if you recall, when you’ve seen some example code about decorators, you must have seen the use of the @ symbol. How does this symbol come into play with the functions that we defined above? Let’s address this question by referring to the following code:

>>> @triple_repeat_wrapper
... def hooray():
...     print("Hooray! Hooray!")
... 
>>> hooray()
Before calling func hooray
Hooray! Hooray!
Hooray! Hooray!
Hooray! Hooray!
After calling func hooray
>>> hooray
<function triple_repeat_wrapper.<locals>.wrapper at 0x1038f0830>

In the code above, we declare a function called hooray. When we call this function, the output has the same format as the previous wrapped_clap function’s output. Similarly, the hooray function references the nested wrapper function.

Why does this happen? As you may have noticed, right above the declaration of this hooray function, we use the @ symbol prefixing the triple_repeat_wrapper function name. It’s just syntax sugar for the decorators. In essence, we’re telling the Python interpreter that the function that we’re going to define will be wrapped by the decorator function.

4. Currying

Named after the mathematician Haskell Curry, currying refers to creating new functions from existing functions by applying partial arguments. Thus, this concept is sometimes also termed partial functions.

Compared to the concepts above, this one is slightly easier to understand. Let’s consider the simplified example below, using the same function add_up that we defined above:

>>> # define a function returns a value
>>> def add_up(num1, num2):
...     sum_n = num1+num2
...     return sum_n
... 
>>> # define a partial function that adds seven
>>> add_seven = lambda x: add_up(7, x)
>>> add_seven(10)
17
>>> add_seven(72)
79
>>> # use a regular def keyword
>>> def add_eight(x):
...     return add_up(8, x)
... 
>>> add_eight(10)
18

In the code above, we use the lambda function to set the number 7 as the first argument for the add_up function. In other words, the created add_seven function is a partial function of the original add_up with the first argument constantly set to be 7. Besides using the lambda function, it’s certainly acceptable to use the regular way to define a function with the def keyword.

Another handy tool to create partial functions is available in the functools module. Consider the following example. We use the partial function to create the add_ten function that has a default argument of 10 that’s calling the add_up function:

>>> # import the module
>>> from functools import partial
>>> # using the partial function
>>> add_ten = partial(add_up, 10)
>>> add_ten(100)
110

In this article, we reviewed four advanced concepts beyond the basics of Python functions. Here is a quick recap of these concepts:

  • Lambdas are anonymous functions that are handy in scenarios where a simple one-time inline function is needed.
  • Closures are nested functions that bind non-local variables of the surrounding scope.
  • Decorators are higher-order functions that modify the behaviors of other functions.
  • Currying is how we create partial functions from existing functions by setting some default arguments.

Thank you for reading!

#python #artificial intelligence #technology

What is GEEK

Buddha Community

Introduction Four Advanced Function Concepts in Python
Ray  Patel

Ray Patel

1619518440

top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel

1619510796

Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Biju Augustian

Biju Augustian

1574339995

Learn Python Tutorial from Basic to Advance

Description
Become a Python Programmer and learn one of employer’s most requested skills of 21st century!

This is the most comprehensive, yet straight-forward, course for the Python programming language on Simpliv! Whether you have never programmed before, already know basic syntax, or want to learn about the advanced features of Python, this course is for you! In this course we will teach you Python 3. (Note, we also provide older Python 2 notes in case you need them)

With over 40 lectures and more than 3 hours of video this comprehensive course leaves no stone unturned! This course includes tests, and homework assignments as well as 3 major projects to create a Python project portfolio!

This course will teach you Python in a practical manner, with every lecture comes a full coding screencast and a corresponding code notebook! Learn in whatever manner is best for you!

We will start by helping you get Python installed on your computer, regardless of your operating system, whether its Linux, MacOS, or Windows, we’ve got you covered!

We cover a wide variety of topics, including:

Command Line Basics
Installing Python
Running Python Code
Strings
Lists
Dictionaries
Tuples
Sets
Number Data Types
Print Formatting
Functions
Scope
Built-in Functions
Debugging and Error Handling
Modules
External Modules
Object Oriented Programming
Inheritance
Polymorphism
File I/O
Web scrapping
Database Connection
Email sending
and much more!
Project that we will complete:

Guess the number
Guess the word using speech recognition
Love Calculator
google search in python
Image download from a link
Click and save image using openCV
Ludo game dice simulator
open wikipedia on command prompt
Password generator
QR code reader and generator
You will get lifetime access to over 40 lectures.

So what are you waiting for? Learn Python in a way that will advance your career and increase your knowledge, all in a fun and practical way!

Basic knowledge
Basic programming concept in any language will help but not require to attend this tutorial
What will you learn
Learn to use Python professionally, learning both Python 2 and Python 3!
Create games with Python, like Tic Tac Toe and Blackjack!
Learn advanced Python features, like the collections module and how to work with timestamps!
Learn to use Object Oriented Programming with classes!
Understand complex topics, like decorators.
Understand how to use both the pycharm and create .py files
Get an understanding of how to create GUIs in the pycharm!
Build a complete understanding of Python from the ground up!

#Learn Python #Learn Python from Basic #Python from Basic to Advance #Python from Basic to Advance with Projects #Learn Python from Basic to Advance with Projects in a day

Art  Lind

Art Lind

1602968400

Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName 
>>> print(FirstName, LastName)
('Jordan', 'kalebu')

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development

Art  Lind

Art Lind

1602666000

How to Remove all Duplicate Files on your Drive via Python

Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.

Intro

In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.

Heres a solution

Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.

But How do we do it?

If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?

The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.

There’s a variety of hashing algorithms out there such as

  • md5
  • sha1
  • sha224, sha256, sha384 and sha512

#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips