Function

Sections

  • What are Functions?
  • Why Use Functions?
  • Defining a Function
  • Call a function
  • Arguments
  • Function return
  • Define a function with unlimited inputs
  • Function Types

Section1- What are Functions?

Section 5- Arguments

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")

Section 6- Function return

To return the output from the function we need to write the “return” statement as given below

def a(x,y):
return x+y
a(2,3)

Section 7- Define a function with unlimited inputs

# define a function (take max 2 inputs)
def add(x,y):
#add two numbers
print(f'Reuslt:{result}')

#define a function (Take unlimited inputs

def add (*args):
# initialize result at 0
result =0
# iterate over args tuple
for arg in arg:
result +=arg
#print the result
print (f'Result:{result}')

Section 8- Function Types

If you are not familiar with "lambda functions," they are basically the same as "regular function but can be written more compactly as a one-liner.

def some_func(x):
    return 'Hello World ' + str(x)

some_func(123)

'Hello World 123'

f = lambda x: 'Hello World ' + str(x)
f(123)

'Hello World 123'

References

2-Important Python Functions used in Data Science

2-Python Functions

Last modified: Thursday, 15 May 2025, 10:59 AM