PYTHON FUNCTIONS

  • By
  • November 1, 2022
  • Python
Python Functions

PYTHON FUNCTIONS

Normally a function is a code of blocks that runs only when we call them. We can pass data parameters variables through function. After that function will give return data as result.

How to create a function. ?

In python a function can be defined with help of def keyword –

Example :

def func():

print(“hello function”)

how we call a function.?

To call the function we need to use the function name followed by parenthesis –

def func():

print(“hello function”)

func()

what is the role of arguments.?

If we want to pass any information to the functions then through arguments it is possible.

Arguments are written after function name where it locates in side to parentheses.

If you want you can add more than one arguments with in on function arguments.

For adding more than one arguments we can separate them by using comma.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Here I am providing you one example based on arguments where function is called , in that we are passing first name, which is used inside function to print full name :

Example :

def  func(fnm):

print(fnm+”mentor” )

func(“seven”)

func(“eight”)

func(“nine”)

note : arguments are used as args also mentioned in python documentation

 

what is the difference between arguments or parameters.?

Actually the terms parameter , argument can be used for same purpose that purpose is called to pass the information through function.

If we see from the functions point of view…

A parameter is work like variable which stores inside parentheses when we define a function.

Where argument is a value which is send to function when we call during operation.

 

How we can pass number of arguments.?

Normally when a function can be called that calling will call correct number of arguments. It means that if you are expecting 2 args then you have to call function with 2 args not more that that not in less way.

For example –

def fun(fm,lm):

print(fm+””+lm)

fun(“email”,”address”)

if u try to call 1 or 3 args while calling function then that program throws error- 

def fun(fm,lm):

print(fm+””+lm)

fun(“email”)

what means arbitrary arguments  …???? *args—

in program if u don’t know how many args are passed in your function then we need to add * before parameter name while defining function.

With help of this we can take tuple of args and can easily access items according to that-

Example –

def  funct(*cars):

print(“the most wanted car is”+cars[3])

funct(“ferrari”,”bmw”,”nisan”,”climber”,”suzuki”)

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

then what means keyword arguments..??

to send data in key = values format we use keyword arguments. In this type there is no matter of sequence.

Example-

def  fnc(fruit1, fruit2, fruit3):

print(“the favourite fruit is”+fruit2)

fnc(fruit1=”mango”, fruit2=”banana”, fruit3=”apple”)

**kwargs—–

If we don’t know how many arguments keywords are added in program that will pass in function then you need to add ** before parameter while defining function.

So in this platform the arguments will show in dict form. So that on accordingly we can check the items.

Sevenmentor’s Python training in Pune covers the fundamentals of Python, data operations, conditional expressions, shell scripting, and Django.

Example-

def fnc1(**fruit):

print(“which fruit you eat most”+fruit[“category”])

fnc1(category=”sweet”, category1=”sour”)

 

default parameter value-

one example shows how to use them.

def fun2(country=”india”):

print(“I am from”+country)

fun2(“america”)

fun2(“dubai”)

fun2()

fun2(“norway”)

 

pass a list through args-

as the heading says we can send data in types through arguments to a function(like string, number, list, tuple, dictionary, etc) and it will be treated like same data which inside put in function.

Example :

def fn(foody):

for x in foody:

print (x)

foodys=[“pani puri”,”bhel”,”chat”]

fn(foodys) 

how to return values..??

example-

def fct(y):

return 4*y

print(fct(4))

print(fct(6))

print(fct(8))

print(fct(2))

pass statement in function.?

When you are writing a program while defining a function that should be not empty but still due to some reason if it is empty then you need to pass statement in that function to avoid program termination.

Like this

Def myfn():

Pass

Then what is recursion..??

Python accepts the concept of recursion in function which it means that define a function and call many times as we need per program.

It gives a benefit to us like you can loop your data to search and get the result.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

But you need to take care while applying recursion concept that it is easy to slip function write but it never terminates the program so to access more amounts of memory then we can apply recursion concept.

 

Example :

Def tri(k):

If (k>0):

Result=k+tri(k-1)

Print(result)

Else:

Result=0

Return result

Print(“recursion example result”)

Tri(7)

 

Author:-

Sneha Ranbhidkar
Call the Trainer and Book your free demo Class 

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published.

*
*