Python programming offers you in-built functions but you can create your own functions. In this python tutorial, you will learn about python function and how to define and call a function with examples.
Python Function explained
Function is a block of code which is reusable as it performs a single or related action. Instead of using python in-built functions like print() etc we can create our own functions.
A function is a block of code that runs only when it is called. We pass data, known as parameters, into a function and it will return data as a result.
How to create a python function?
We can define a function in python by using the “def” statement followed by the function name and parentheses()
def my_function(): print("hello world")
How to call a function in python?
For a function call use function name followed by parentheses:
def my_function(): print("hello world") my_function()
How to pass an arguments in python?
Information can be passed into a function as an argument.These can be specified after a function name inside the parentheses. We can add as many arguments we want to pass, just separate them by commas.
def my_function(first_name,last_name): print(first_name+" " +last_name) my_function("Abdul","Kalam")
def my_function(first_name,last_name): print(first_name+" " +last_name) my_function("Abdul")
This function will give an error because the function expects 2 arguments but it gets only 1. So it will give an error that one positional argument is missing.
So, when we don’t know how many arguments to pass in our function add a * before parameter name in the function definition.This is known as arbitrary argument *args.
def my_function(*fruits): print("My favorite fruit is " + fruits) my_function("mango", "litchi", "apple")
Keyword Arguments in python function explained
We can send argument with the key =value syntax
In this order of argument doesn’t matter.
def my_function(boy3, boy2, boy1): print("The tallest boy is " + boy3) my_function(boy1 = "Jack", boy2 = "Leo", boy3 = "Tony")
When we don’t know that how many keyword arguments to be passed into function, then add two asterisk: ** before the parameter name in the function definition. This is known as Arbitrary keyword argument **kwargs.
def my_function(**boy): print("His last name is " + boy["last_name"]) my_function(first_name = "Tony", last_name = "Stark")
Lambda Function in python
Lambda function in python is a single line function which is declared without name and can have any number of arguments, but it can only have one expression.
Let us understand it better by code examples:
x = lambda a : a + 5 print(x(5)) x = lambda a, b : a * b print(x(3, 4))
Why to use lambda function?
The lambda keyword in Python provides a shortcut for declaring small anonymous functions.Lambda function is used as an argument to a higher-order function means a function that takes in other functions as arguments.
def my_function(b): return lambda a : a * b double = myfunc(2) print(double(5))
What is the difference between lambda and regular function?
Let us understand this with an example:
def square(a): return a*a x = lambda b: b*b print(x(3)) print(square(6))
Here, we see that both of them return the square of a given number. But, when we are using def, we need to define a function with a name square and need to pass a value to it. After execution, we are using the return keyword also, to return the result from where the function was called.
Now when the Lambda function is used then the“return” statement is not included, it always contains an expression which is returned. We can also use a lambda definition anywhere when a function is expected, and assignment of variables is not required at all. This is the simplicity of lambda functions.
What is the significance of indentation in python?
Indentation refers to the spaces at the beginning of a code line. In programming languages like c, c++, java etc the indentation in code is only for readability purposes, whereas in Python the indentation is very important as it is used to indicate a block of code .
if 3>2: print('3 is greater than 2')
Introduction to Python Queue
Queue is like a container which is used to store data or elements and it follows the FIFO semantics as the data which is inserted first will be removed first(First in First Out). We can understand this better by seeing real world examples like a line of people standing in a queue waiting for a ticket, so the person who stands first will get the ticket first , second person next and so on.
We can perform operations like insertion and deletion in a queue which is also known as enqueue and dequeue respectively.
There are basically two sides, one is rear and other is front . The rear side represents that data is inserted from that point and the front side represents the point from where the queue is removed. In this way the queue works.
What are the types of queue in python?
1. First in First out Queue: In this, the element which is inserted first will be removed first. To work with FIFO, you have to call the Queue() class from the queue module.
2. Last in First out Queue: In this, the element which is inserted at last will be removed first. So to work with LIFO, you have to call the LifoQueue() class from the queue module.
How to install a queue in python?
Step 1 – First import queue module
Step 2 – To work with FIFO queue,we need to call the Queue class by using the imported queue module.
q = queue.Queue()
Step 3 – To work with the LIFO queue, we need to call the LifoQueue class by using the imported queue module.
q = queue.LifoQueue()
We have the following methods in queue:
- q. size() – This will return the length of the queue.
- q. empty() – This will return True if empty, False otherwise.
- q. put(item) – This will put item inside the queue.
- q. get() – This will return an item from the queue.
How to reverse a string in python?
In python we don’t have any built-in function to reverse a string.
The easiest way is to use a backward slicing.
Let us understand it with example:
a = "Tech Learn"[::-1] print(a)
def my_function(a): return a[::-1] text = my_function("Let us study about strings in python") print(text)
How to print newline in python?
We can print newlines in python by using \n character.
By using print statements we can print newline in python.
You can read our complete guide to learn python programming-