Functions in python
Table of contents
- Hey techies, I hope and wish u have a good day today, welcome to this week's article. Today, I am going to make you gain knowledge on "Functions in python". Hope you are loving my articles and I appreciate it if you show a response by commenting, liking or even sharing on your socials. I am focusing more on Python so I also suggest you to read my previous articles on Python.
- 1) What is function?
- 2) Types of functions
- Quote of the day
Hey techies, I hope and wish u have a good day today, welcome to this week's article. Today, I am going to make you gain knowledge on "Functions in python". Hope you are loving my articles and I appreciate it if you show a response by commenting, liking or even sharing on your socials. I am focusing more on Python so I also suggest you to read my previous articles on Python.
1) What is function?
Let's start as a (Grandma tale), A function in Python is like a recipe. Just like how you follow a recipe to make a cake, a function is a set of instructions that tell the computer what to do. It's like a mini-program within a bigger program.
We give a name to the function and then tell it what to do step by step. And when we want the computer to do those steps, we just call the function by its name. Just like how you can make different cakes by following different recipes, you can use functions to perform different tasks in your program.
Think of it like a toolbox, where each function is a different tool you can use to build your program.
2) Types of functions
Built-in functions: These are functions that are already built into Python and can be used without importing any additional modules. Examples include the
print()
function and thelen()
function.print()
- This function is used to output text or other data to the console. For example,print("Hello World!")
will print the string "Hello World!" to the console.len()
- This function returns the number of items in a list, tuple, or other data structure. For example,len([1, 2, 3, 4])
will return the value 4.type()
- This function returns the type of a variable. For example,type(5)
will return the value<class 'int'>
int()
- This function converts a value to an integer. For example,int("5")
will return the value 5.str()
- This function converts a value to a string. For example,str(5)
will return the value "5".min()
andmax()
- These functions return the minimum and maximum values of a list or tuple. For example,min([1, 2, 3, 4])
will return the value 1 andmax([1, 2, 3, 4])
will return the value 4.
User-defined functions: These are functions that are defined by the programmer. They can be created using the
def
keyword.def say_hello(variables): print("Hello, " + variables + "!") say_hello("John")
1.) Here "def" is where we were defining that it is a function
2.)"say_hello" is the name of the function like how we have our names.
3.)in the last line we are calling the function "say_hello"
4.)NOTE: if we call the function then only the program will get executed with no errors.
def ironman(a,b,c):
# iron man is the function name
print("Hey " + a,b,c)
# here ironman is we are calling the function.
ironman("i","love u ", 3000)
Output :
[Running] python -u
Hey i love u 3000
[Done] exited with code=0 in 0.143 seconds
Click here to see some examples on User-defined functions
3.) Anonymous functions: These are also called lambda functions. They are defined without a name and are used only once. Anonymous functions are defined using the lambda
keyword.
also called as nameless(no- name) functions .
these are shorter line codes than the user-defined functions.
but we need to assign it to a variable first.
we can give as many as arguments but only one expression.
Remember that according to syntax, lambda functions are limited to a single statement.
Syntax:
lambda arguments: expression .
lambda_reciprocal = lambda num: 1 / num # we use the key word lambda here important # using the function defined by lambda keyword print( "Lambda keyword: ", lambda_reciprocal(3000) ) #here we need to call lambda_reciprocal because we have assigned to it. Output: Lambda keyword: 0.00033333333
That's it for today hope you gained some knowledge today, let's meet in next blog post.
Quote of the day
“Don't settle for average. Bring your best to the moment. Then, whether it fails or succeeds, at least you know you gave all you had.” —Angela Bassett
Subscribe to my newsletter
Read articles from Smd Sohail directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Smd Sohail
Smd Sohail
Hello there, I am a Cloud intern | Ex- SDE Odoo Developer | a passionated techie from India, i like to work with machines by training and testing them. In my free time, I often blog to share my experiences as well as read more blog posts to learn and enrich my knowledge. #Developer📔#Tech enthusiast💡 #Machine learning🤖 #blogger #writer