Introduction
Setup
Operators
variable
String
Number
Dtype & Comment
Lists
Tuples
Sets
Dictionaries
if else statement
For Loop
While Loop
iterator & iterable
Pre-Built functions
Function
Decorators
generators
Types of errors
Exceptions Handling
OOP
Regular expression
Create module
OS module
Date module
Random module
Math
Statistics module
Exercise 1
Exercise 2
Exercise 3
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
Learn MySQL
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
The function is a block of code. It only runs when we called it. We will pass data through parameters into a function and when we call the function it will return or print the data as a result. The facility of function is that we write the code one time and can use it how many times we want. We can also pass different parameter values while calling the function.
First, write def then the function name then the first bracket, and then a colon. After this write the code inside it. At last call the function by the name. Call the function by going outside of the function. We can't give space between the function name words. But we can use a separator like a dash, hyphen, etc.
Example:1In example 1, we created a function named my_function. Inside the function, we wrote a simple code, that is to print "Hello world". After that, go outside of the function and call the function by its name and after that, the function runs and we got the output "Hello world".
Example:2In example 2, we have a parameter in function which is pra and we called the function three times and each time we gave a different value to the function while calling the function. So when we gave value while calling the function, it means that we are giving value to the parameter. So when we gave value Rafsun it means we gave value to the parameter pra. So when pra got value Rafsun it runs the code with that value and gives us output Rafsun. In the same way, when we gave Ahmad and RafsanAhmad it takes the value runs the code, and gave us the output. Because we called the function three times so we got three outputs.
Example:3In example 3 we used 2 parameters. We can use parameters how many we want. We can pass a number or string value to the parameter at a time. In the example, We can see that for a parameter we gave a string and for b we gave a number, and when we called we get two different values of two different parameters.
Example:4In example 5, we used 2 parameters but we pass one value so we will get an error. We have to give the same number of values according to the number of parameters.
Example:6In example 6, we used 2 parameters but we pass one value so we will get an error. We have to pass the same number of values according to the number of parameters. If we forget to pass value and if we don't want to get an error, then we have to pass a default value for that parameter as we gave to the b parameter. To do this give equal and then write the default value in the double or single quotation. But if we want to give a default value to the parameter then we have to start giving the default value from the end and have to come to the start. We have to give value serially. If we give a default value to the first and third parameters and don't give it to the second parameter then it will give an error. We have to give value serially and can't escape one in the middle.
Example:7If we write a parameter and give an asterisk(*) sign before it then we can pass unlimited values through that parameter. Normally we can pass one value for one parameter. But we use the asterisk(*) sign before the parameter then we can pass unlimited values through that parameter.
Example:1If we don't know how many keyword arguments we have to pass into the function, then we should add two asterisks (**) signs before the parameter in the function definition. In this way the function will receive arguments as a dictionary and also the function will be able to access the items accordingly.
Example:We can return the output from a function.
Example:Function definitions can't be empty. But if we have for some reason then put a pass statement to avoid getting an error.
Example:In example outer_func return inner_func.So it means now var variable has inner_func. So when we called var variable then inner_func was executed and what was written inside inner_func gets as the output.
Example:2Recursion means a function that can call itself. This means that we can loop through data to reach a result.
Example:1In the example, we used the variable as the data, which decrements -1 every time it recurs. The recursion will end when the condition is not greater than 0.