This article will help the reader understand the basic Python functions along with some basic applications in the real world. We will be using Visual Studio Code as our code editor. If you have not installed Visual Studio Code, the instructions are given in the first blog.
Python functions – table of contents:
Python functions
Python functions are objects that means the functions can be used as return value for other functions, can be stored in a variable, can be stored in data structures, or can be used as an argument in other functions.
Python functions are defined using “def” keyword following the function name. Then inside these brackets “()” , the arguments are defined. The basic syntax of Python functions is illustrated below.
For Example:
# Create a function # def keyword def functioname():
Note:
Function name is also having the same norms as the variable declaration.
Let’s write our first function
# first function def sum(a,b): return a+b
In the above code block, we have written a function that gives us the sum of two numbers. As you can see, we have used “def” keyword, a and b are the arguments which in our case would be the numbers we want the sum for. Now, we have used a keyword here called “return” which is used to return the desired value or string from the function after performing the desired task. The values which are returned by using returned keywords can be further assigned to other variables or can be used in functions as an argument.
Let’s now see, how to use this function on our desired numbers.
# first function def sum(a,b): return a+b sum(6,7) x=sum(6,7) print(x)
As you can see if we just use the function, the function will not show any value, but when we store the functions return value in another variable and print it, it gives the desired result.
Let’s run the program and see the output
# Output 13
We have got the output as 13, which is the sum of 6 and 7. Let’s write another function which gives us fullname given firstname and lastname.
# second function def fullname(fn,ln): return fn+ln x=fullname(“python”,”language”) print(x)
As you can see, we have just defined the function fullname and gave it parameters firstname and lastname. We are returning fullname using “+” which is a concatenation operator in string which we learnt in the variables blog.
Let’s explore the output
#Output pythonlanguage
Python functions as objects
Most of the data in Python is represented in the form of objects. In Python strings, modules, functions are all represented in the form of objects. Let’s see how we can use functions as objects.
Assigning functions to a variable
As function is an object, it can be assigned to a variable. Example is illustrated below.
# first function def sum(a,b): return a+b sumab=sum
In the above example, we can see that assigning it to new variable doesn’t call the function instead it just assigns the function to the variable “sumab”. The actual meaning of the above example is that the variable “sumab” takes the sum function object as a reference and the “sumab” now points to that object. Hence the sumab can also be used as a function now. Example is illustrated below.
# New function def sum(a,b): return a+b sumab=sum s=sumab(7,8) print(s)
Output:
#output 15
Note:
The function name which we give in the declaration and the function objects work very differently. Even if we delete the original function name, if there is another name pointing to that reference function object, still the function will work. Example is illustrated below.
# New function def sum(a,b): return a+b sumab=sum del sum sum(8,7)
Output:
#Output NameError: “name ‘sum’ is not defined”
But when we use the sumab function, then the result is illustrated below.
# New function def sum(a,b): return a+b sumab=sum del sum sumab(8,7)
Output:
15
Storing Python functions in Data Structures
As the functions are objects in Python, we can store them in data structures in the same way we store our variables and constants. The syntax changes a little bit but it’s like how we stored elements in the datatypes.
#function storing in datastructures Storedfunctionslist=[len,str.upper(),str.strip(),str.lower()] Storedfunctionslist
Iterating through functions is just like iterating objects. Example illustrated below.
#function storing in datastructures Storedfunctionslist=[len,str.upper(),str.strip(),str.lower()] for fun in Storedfunctionslist: print(fun, fun('Hello'))
In this blog, we have covered some basics Python functions, the further detailed topics on functions will be covered in the next blog post.
You may also like our JavaScript Course from Beginner to Advanced.
Python Course From Beginner to Advanced in 11 blog posts:
- Introduction to Python Course. Part 1 Python Course from Beginner to Advanced in 11 blog posts
- Variables and Data Types in Python. Part 2 Python Course from Beginner to Advanced in 11 blog posts
- Python tuples, lists, sets and dictionaries. Part 3 Python Course from Beginner to Advanced in 11 blog posts
- Python sets and dictionaries. Part 4 Python Course from Beginner to Advanced in 11 blog posts
- Conditional statements in Python. Part 5 Python Course from Beginner to Advanced in 11 blog posts
- Loops in Python. Part 6 Python Course from Beginner to Advanced in 11 blog posts
- Python functions. Part 7 Python Course from Beginner to Advanced in 11 blog posts
- Advanced functions in Python. Part 8 Python Course from Beginner to Advanced in 11 blog posts
- Python classes and objects. Part 9 Python Course from Beginner to Advanced in 11 blog posts
- Files in Python. Part 10 Python Course from Beginner to Advanced in 11 blog posts
- Python applications in practice. Part 11 Python Course from Beginner to Advanced in 11 blog posts