Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
63 views

Functions of python

python funtions

Uploaded by

yourdatabar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Functions of python

python funtions

Uploaded by

yourdatabar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

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.

Example for builtin function:

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

# Using the function sum_result = add_numbers(3, 5) print("Sum:", sum_result)

2. Word and Sentence Functions in Python:

Word and Sentence

Word and Sentence functions include the following steps,

Split the string into words, using split() function.

All the words in the sentence gets splitted into the separate words.

Calculate length of word using len() function.

Length of the word gets printed as output.

Finding a Particular word in the sentence.

1. Splitting the String into 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()

2. Calculating the Length of a Word:

 The len() function calculates the number of characters in a word.

 Example:

pythonCopy code

word = 'galaxy'
length_of_word = len(word)

print(length of word)

3. Finding a Particular Word in the Sentence:

 The in operator is used to check if a specific word exists in a sentence.

 Example:

pythonCopy code

word_to_find = 'galaxy'

if word_to_find in sentence: print('Word found')

3.Parameters and Arguments in Python:

In Python, functions play a crucial role in organizing and executing code. Two fundamental concepts
associated with functions are parameters and arguments.

1. Parameters:

 Definition: A parameter is a variable declared in the function definition, serving as a


placeholder for values that the function will receive when called.

 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:

 Definition: An argument is the actual value or expression passed to a function when it is


called.

 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.

 This separation of concerns promotes code readability, maintenance, and reuse.

4. Example Usage:

pythonCopy code

def calculate_sum(a, b):

result = a + b

return result

# Calling the function with arguments total = calculate_sum(3, 5) print(f"Sum: {total}")

 In this example, a and b are parameters, and 3 and 5 are the arguments supplied during the
function call.

In conclusion, understanding the distinction between parameters and arguments is fundamental to


effective function usage in Python. Parameters define the function's input structure, while
arguments provide the actual values that the function operates on. This clear separation enhances
the flexibility and maintainability of Python code

You might also like