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

Python_Functions_Guide

This document provides an overview of functions in Python, describing their purpose, syntax, and types including built-in, user-defined, lambda, and recursive functions. It explains function parameters and arguments, showcasing examples for user-defined functions, return statements, lambda functions, and recursive functions. Overall, it emphasizes the modularity and readability that functions bring to Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python_Functions_Guide

This document provides an overview of functions in Python, describing their purpose, syntax, and types including built-in, user-defined, lambda, and recursive functions. It explains function parameters and arguments, showcasing examples for user-defined functions, return statements, lambda functions, and recursive functions. Overall, it emphasizes the modularity and readability that functions bring to Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Functions in Python

In Python, a function is a block of reusable code that is used to perform a specific action.
Functions help make programs modular, readable, and easier to manage.

Python provides both built-in functions (like print(), len(), etc.) and user-defined functions.

Function Syntax

def function_name(parameters):
"""docstring"""
statement(s)
return value

Types of Functions
1. 1. Built-in Functions (e.g., print(), len(), type(), etc.)
2. 2. User-defined Functions
3. 3. Lambda Functions (Anonymous functions)
4. 4. Recursive Functions

Function Parameters and Arguments

- Required arguments
- Default arguments
- Keyword arguments
- Variable-length arguments (*args, **kwargs)

Example: User-defined Function

def greet(name):
"""This function greets to the person passed in as a parameter."""
print("Hello, " + name + "!")

greet("Alice")
Return Statement
Functions can return a value using the return statement. Example:

def add(a, b):


return a + b

result = add(3, 4)
print(result) # Output: 7

Lambda Functions
Lambda functions are small anonymous functions defined using the lambda keyword.

Example:
square = lambda x: x * x
print(square(5)) # Output: 25

Recursive Function

A function that calls itself.

Example:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

print(factorial(5)) # Output: 120

You might also like