Free Online Tutorials ==>> Machine Learning || Python Programming || DBMS || OOPs using C++ || DSA || Java Programming || Linux/Unix || C Programming
Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions
0 like 0 dislike
258 views
in Tutorial & Interview questions by Goeduhub's Expert (7.8k points)
Describe Functions in Python
by (103 points)
learn about functions used in python

5 Answers

0 like 0 dislike
by Goeduhub's Expert (2.8k points)
edited by
 
Best answer

Python Function

  • Function is a group of statement that perform specific task.
  • Function help reduce line of code and break into small code section.
  •  basically function use  code in more organised and manageable.

In python there are three types of functions 

1.User defined function

2.Built in function 

3.Lambda Function

User defined function

User defined function made by user as their requirement. 

Defining a function

  • To define a function we use  def keyword with parenthesis.
  • We pass parameters/ arguments in parenthesis.
  • To close the function we use return(expression).

Syntax of a function

def function_Name( parameters ):
   #Statements
   return (expression)

#Calling a function 

Calling a function 

After defining a function we need to call a function to perform function task, In function calling we need to passed arguments in function.

Lets understand it with an example

Example

#Defining a function 

def ADD (a,b):  

    return a+b;  

  #taking values from the user  

a = int(input("Enter a= "))  

b = int(input("Enter b= "))  

  #Calling a function

print("Sum = ",ADD(a,b))  

Output

Enter a= 5 

Enter b=5 

Sum = 10

Example

#defining a function

def Odd_Even( x ):

    if (x % 2 == 0): 

        print ("even")

    else: 

        print ("odd")

#Calling a function

Odd_Even(8)

Odd_Even(9)

Output

even 

odd 

Parameters:-A variable listed inside a function when function is defined 

Arguments:-A variable is passed in a function during calling of function.

Different types of arguments in python function

Required arguments

Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.

Example

def add(a,b):

    c=a+b

    print(c)

add(7,8)

Output 

15 

Note:-Here "a" and "b" are required argument without that you cannot perform or call a function , if you try without passing a and b it will give a syntax error.

Note:- In this type of argument variables arguments  taken by function are in correct positional order.

Keyword arguments

In this type of argument order of arguments does not matter,when we call function we define arguments with their respected values. It works like Key=value.

Example

def Name_Age (Name,Age):

    print(Name)

    print(Age)

    return;

#Function calling

Name_Age(Name='XYZ',Age='21')

Output

XYZ 

21 

Default arguments

In Default argument it works ,when a argument value is not provided in function calling than it take a default value that is already defined in define function.

Note:-If a argument value is provided in function calling than  default value is avoided.

Example

def Name_Age (Name,Age,salary=20000):

    print(Name)

    print(Age)

    print(salary)

    return;

#Function calling

Name_Age(Name='XYZ',Age='21')


def Name_Age (Name,Age,salary=20000):

    print(Name)

    print(Age)

    print(salary)

    return;

#Function calling

Name_Age(Name='XYZ',Age='21',salary=50000)

Output

XYZ 21 

20000 


XYZ 21

 50000

Variable Length arguments

Sometimes we need more variables than already listed in function defining , in such cases we use variable length arguments.

An asterisk (*) is placed before the variable name that holds the values of all  variable arguments.

Example

def Sum(*b):

    c=0

print(b)

    for i in b:

        c=c+i

    print(c)

Sum(1,5,6,8)

Output

(1, 5, 6, 8)

20 

Built-In Functions

  • A function that is built into an application and can be accessed by users is called built-in function. Python has a number of built-in functions.
  • For example-print() is a built-in function in python

 Some built-In python functions and their description

Function NameDescription
print()gives object on the screen or other standard output devices.
list()create a list
set()create a set 
len()shows the length of list, string etc
tuple()create a tuple 
type()shows type of a object  e,g-string, integer etc
max()shows maximum value from the given values 

Example

#Sorting in ascending order 

L=[462,45,546,6485,65,7,65,54,524,874,54,5]

Sort_L=sorted(L)

print(Sort_L)

#Descending order

Sort_L_Reverse =sorted(L, reverse=True)

print(Sort_L_Reverse)

Output

[5, 7, 45, 54, 54, 65, 65, 462, 524, 546, 874, 6485] 

[6485, 874, 546, 524, 462, 65, 65, 54, 54, 45, 7, 5] 

Note:- If we want to sort a list without using built-in sorted function,for that we have to write a long code.Built-in functions make things easy to us.

Lambda Function

  • Lambda function is a anonymous function means that a function is without a name.
  • To create a small anonymous function lambda keyword is used.

Syntax of a lambda function

lambda arguments:expression

Example 

x = lambda a:a*n

n=int(input("A number="))   

print("sum = ",x(20)) 

Output

A number=4 

sum = 80

by (101 points)
i read it completely
by (422 points)
Function is important topic and in this module function is explain briefly.
by (304 points)
Python function is also same as the other language like c,c,++,java
But in this lambda is the new term is introduced
by (100 points)
I Learned how to create function to perform a task. It makes simple and quicker code. We can use inbuilt functions or can create it by our own logics.
by (100 points)
In this article i learned about funtions. Functions are used for performing a specific task on it's calling that makes code readable and more efficient. Python have 3 types of funtions:-
1. Built-in functions
2. User defined functions
3. Lembda functions

User defined functions have argument that must be passed at the time of calling.
0 like 0 dislike
by Goeduhub's Expert (7.8k points)

Functions in Python

# Defining a function


def student_scores(English, German, Spanish, French):
   print ('My score in English is %d:' % English)
   print ('My score in German  is %d:' % German)
   print ('My score in Spanish is %d:' % Spanish)
   print ('My score in French  is %d:' % French)

# Scores given to function directly
 

print ('Scores given directly to student_scores function:')
student_scores(70, 83, 92, 73)

# Scores given to function using variables

print ('Scores given using variables:')
score_1 = 70
score_2 = 83
score_3 = 92
score_4 = 73
student_scores(score_1, score_2, score_3, score_4)

# Scores added and given to function
 

print ('Scores given by adding assignment scores:')
student_scores(65 + 5, 80 + 3, 90 + 2, 70 + 3)

# Scores added to varilable values and given to function 


print ('Scores added to values in variables')
student_scores(score_1 + 5, score_2 + 5, score_3 + 5, score_4 + 5)

0 like 0 dislike
by (421 points)
Date: 23/04/2020

Topic: Python Function

I Learned how to create function to perform a task. It makes simple and quicker code. We can use inbuilt functions or can create it by our own logics.
0 like 0 dislike
by (232 points)

                                                                                                 Topic-Python function

I learned about python function and their use in python. It makes simple and quicker code. We can use inbuilt functions or can create it by our own logics.

0 like 0 dislike
by (304 points)
In this module I learned the different type of functions used in python which is help in solve the complex problem

Related questions

1 like 0 dislike
1 answer 38 views
1 like 0 dislike
6 answers 353 views
0 like 0 dislike
1 answer 25 views
0 like 0 dislike
1 answer 30 views
0 like 0 dislike
1 answer 30 views
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...