Python Functions
Definition:
A function is a block of code that performs a specific task.
A function is a block of code which only runs when it is called.
Different types of Python functions:
1. Built-in Functions: These functions are built into the Python language .Some examples of
built-in functions are print(), len(), sum(), min(), max(), etc.
2. User-defined Functions in Python
User-defined functions are functions that the programmer creates to perform a specific task or set of
tasks. Defining a function allows you to reuse code and makes your code more modular and easier to
read. In Python, you can define a function using the def keyword, followed by the function name
3. Built in Methods
Method is also called by its name, but it is associated to an object (dependent).
Method is called using object name.
Examples: str is object
str=”hello”
str.upper(), str.lower(),str.isupper() etc.
Key Characteristics:
• Encapsulation: Grouping related code together.
• Reusability: Use the same code multiple times.
• Modularity: Breaking down complex problems into simpler pieces.
Function Definition:
A function definition includes the function name, parameters, and the function body.
A function is defined by def keyword followed by function name.
Components:
1. def Keyword: Used to indicate the start of a function definition.
2. Function Name: A unique identifier for the function, following naming conventions (e.g., no
spaces, not starting with a number).
3. Parameters: Variables listed inside parentheses that accept input values (optional).
4. Colon (:): Indicates the beginning of the function body.
5. Function Body: code inside a function
6. Return Statement: (Optional) Specifies the value to return to the caller. If omitted, the function
returns None by default.
Example:
Function Calling:
Function code is run only when we call a function. To call a function, use the function name followed by
parenthesis:
Syntax:
function_name(arguments)
Components:
1. Function Name: The name of the function to call.
2. Arguments(Optional): Values provided to the function's parameters, enclosed in parentheses.
Important Points:
1. Function definition should be done before the function calling otherwise error will occur.
2. Indentation: Indentation is crucial in Python. The function body must be indented consistently
to indicate that the statements belong to the function.
3. Parameters and Arguments: Parameters are variables in the function definition. Arguments
are the values passed to these variables when the function is called.
4. Return Statement: The return statement ends the function execution and optionally passes
back a value to the caller. If no return statement is present, the function returns None by
default.
Example 1: Function with No Parameters and No Return Value
Example 2: Function with Parameters and No Return Value
Example 2:
3: Function with no Parameters and Return Value
Example 4: Function with Parameters and Return Value
Parameters or Formal Parameters or formal arguments:
They are the variables that are used in the function definition. They act as a placeholder for the actual
values. In the following example a,b are parameters.
Arguments or Actual Parameters or actual arguments:
These the real values passed to the function when it is called. These values are assigned to the
corresponding parameters.
ARGUMENT TYPES:
1.) Positional Arguments:
• These are the simplest and most common form of arguments. They are passed to the function
in the order they are defined.
• No of arguments must be equal to no of parameters otherwise code will give error .
Keyword Arguments:
• You can also send arguments with the key = value syntax.
• This way the order of the arguments does not matter.
• This can be achieved by keyword arguments.
• But name of keyword arguments should match the name of parameters in the function
definition.
• Keyword arguments should come after positional arguments only not before.
All the keyword arguments should match the parameters in the function definition.
Default Arguments:
• Default arguments are values that are provided while defining functions.
• The assignment operator = is used to assign a default value to the argument.
• Default arguments become optional during the function calls.
• If we provide a value to the default arguments during function calls, it overrides the default
value.
• The function can have any number of default arguments.
• Default arguments should follow non-default arguments i.e. they should come after positional &
keyword arguments.
Example 2: If all arguments have default values, we can call that function without passing any values.
SAMPLE QUESTIONs :
Q1 Assertion (A):- If the arguments in a function call statement match the number and order of
arguments as defined in the function definition, such arguments are called positional arguments.
Reasoning (R):- During a function call, the argument list first contains default argument(s) followed by
positional argument(s).
Ans: A is True but R is False
Q2: Assertion (A):- key word arguments are related to the function calls.
Reasoning (R):- when you use keyword arguments in a function call, the caller identifies the arguments
by parameter name.
Ans: Both A and R are true and R is the correct explanation for A
Q3 : .Assertion: The default value of an argument will be used inside a function if we do not pass a
value to that argument at the time of the function call.
Reason: the default arguments are optional during the function call. It overrides the default value if we
provide a value to the default arguments during function calls.
Ans: Both A and R are true and R is the correct explanation for A
Q4 Assertion (A):- The default arguments can be skipped in the function call.
Reasoning (R):- The function argument will take the default values even if the values are supplied in the
function call
Ans: Both A and R are true and R is the correct explanation for A
Q5 17 Assertion (A): The function definition calculate(a, b, c=1,d) will give error.
Reason (R): All the non-default arguments must precede the default arguments.
Ans: Both A and R are true and R is the correct explanation for A
Ques:The code given below accepts N as an integer argument and returns the sum of all integers from
1 to N. Observe the following code carefully and rewrite it after removing all syntax and logical errors.
Underline all the corrections made.
Ques : Vivek has written a code to input a number and check whether it is even or odd number. His
code is having errors. Rewrite the correct code and underline the corrections made.
WRITE THE OUTPUT
250 # 150
250 # 100
130 # 100
Output : g0n2Ge
Write the output:
[1, 2, 3]
['ONE', 'TWO', 'THREE']
{1: 'ONE', 2: 'TWO', 3: 'THREE'}
cbse#OO#eXAM
300 @ 200 5#8#5#4#
300 @ 100
120 @ 100
300 @ 120
Ques: Write a Python function that displays all the words starting and ending with a vowel from a
line.The consecutive words should be separated by a space in the output.
Ques: Write a function that takes a string as argument , checks each word of the string and returns a
list containing length of each word of a string. For example, if the string is "Come let us have some fun",
the tuple will have [4, 3, 2, 4, 4, 3]
Also Make this argument as default and write a function call for each with and without default value.
Ques: Write a Python function that takes a dictionary as an argument and displays all the names
containing ‘ow’ from the dictionary D1 = {1: 'parrot', 2: 'tiger', 3: 'cow', 4: 'owl', 5: 'elephant'}
Ques: Write a Python function that takes a dictionary as an argument and displays all the names longer
than 5 characters from the dictionary D1 = {1: 'parrot', 2: 'tiger', 3: 'cow', 4: 'owl', 5: 'elephant'}
Ques: Write a function EVEN_LIST(L), where L is the list of elements passed as argument to the
function. The function returns another list named ‘evenList’ that stores the indices of all even numbers
of L.
Ques : Write a function in Python Convert() to replaces elements having even values with its half
and elements having odd values with twice its value in a list. eg:if the list contains 3,4,5,16,9 then
rearranged list as 6,2,10,8,18
Ques: Write a function SQUARE_LIST(L), where L is the list of elements passed as argument to the
function. The function returns another list named ‘SList’ that stores the Squares of all Non-Zero
Elements of L. For example: If L contains [9,4,0,11,0,6,0] The SList will have - [81,16,121,36]