Functions of python
Functions of python
Functions of python
In Python, a function is a block of reusable code that performs a specific task or set of
tasks. Functions help organize code, make it more readable, and facilitate code reuse.
They allow you to break down a program into smaller, modular pieces, making it
easier to understand and maintain
1. Built-in Functions:
Built-in functions are functions that are already available in Python. They provide a wide
range of functionality and can be used directly without the need for additional definitions.
a. Common Built-in Functions:
print(): Used to display output.
len(): Returns the length of an object (e.g., string, list, tuple).
type(): Returns the type of an object.
input(): Reads input from the user.
int(), float(), str(): Convert values to integer, float, or string, respectively.
b. Mathematical Functions:
abs(): Returns the absolute value.
max(), min(): Returns the maximum or minimum value among arguments.
c. List and String Functions:
list(), tuple(), set(): Convert to list, tuple, or set.
str(), join(), split(): Convert to string, concatenate strings, and split strings.
Print(‘helloworld’)
Output: helloworld
2. Custom-Defined Functions:
Custom-defined functions are created by the programmer to perform specific tasks. They
enhance code modularity and reusability.
a. Function Declaration:
Use the def keyword to declare a function.
Specify a meaningful name for the function.
b. Parameters and Arguments:
Explain parameters as variables that the function receives.
Arguments are the actual values passed to the function when calling it.
c. Function Body:
Describe the block of code inside the function where tasks are performed.
Discuss the use of variables, control structures (if, for, while), and other Python constructs.
d. Return Statement:
Discuss the optional return statement.
Explain that it sends a value back to the calling code.
e. Example:
Provide a simple example of a custom function.
Call the function with arguments to demonstrate its usage.
Example Explanation:
# Custom-defined function
def add_numbers(a, b):
result = a + b
return result
All the words in the sentence gets splitted into the separate words.
The split() function is used to break a string into a list of words based on whitespace
or a specified delimiter.
Example:
pythonCopy code
sentence = 'There are more trees on Earth than stars in the Milky Way galaxy'
words = sentence.split()
Example:
pythonCopy code
word = 'galaxy'
length_of_word = len(word)
print(length of word)
Example:
pythonCopy code
word_to_find = 'galaxy'
In Python, functions play a crucial role in organizing and executing code. Two fundamental concepts
associated with functions are parameters and arguments.
1. Parameters:
Purpose: Parameters enable functions to accept and process different inputs, adding
flexibility and reusability to the code.
Syntax: Parameters are listed within the parentheses following the function name.
Example:
pythonCopy code
def greet(name):
print(f"Hello, {name}!")
Explanation: In this example, name is a parameter, and the function greet can be called with
different values for name.
2. Arguments:
Purpose: Arguments provide the concrete values that replace the parameters in the function
during execution.
Syntax: Arguments are supplied within the parentheses during the function call.
Example:
pythonCopy code
greet("Alice")
Explanation: Here, "Alice" is an argument for the name parameter in the greet function.
3. Importance:
Parameters and arguments enhance code modularity by allowing functions to handle various
inputs without modifying the function itself.
4. Example Usage:
pythonCopy code
result = a + b
return result
In this example, a and b are parameters, and 3 and 5 are the arguments supplied during the
function call.