Functions are reusable blocks of code that perform a specific task. They help in organizing code, making it more modular and easier to maintain. In this post, we will explore how to define and use functions in Python, as well as the differences between built-in and user-defined functions.
1. Defining a Function
To define a function in Python, you use the def
keyword followed by the function name and parentheses. Inside the parentheses, you can include parameters that the function accepts.
Syntax:
python
def function_name(parameters):
# block of code
Example:
python
def greet(name):
print(f"Hello, {name}!")
# Calling the function
greet("Alice")
Output:
Hello, Alice!
2. Function Parameters
Functions can accept parameters, which are values you pass to the function to customize its behavior. You can define functions with required parameters, default parameters, and variable-length parameters.
2.1. Required Parameters
These are parameters that must be provided when calling the function.
Example:
python
defadd(a, b):
return a + b
result = add(2, 3)
print(result)
Output:
5
2.2. Default Parameters
You can assign default values to parameters, making them optional.
Example:
python
defgreet(name="Guest"):
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
greet() # Output: Hello, Guest!
2.3. Variable-Length Parameters
You can use *args
for variable-length positional arguments and **kwargs
for variable-length keyword arguments.
Example:
python
def multiply(*args):
result = 1
for num in args:
result *= num
return result
print(multiply(2, 3, 4)) # Output: 24
def print_details(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_details(name="Alice", age=25, city="New York")
Output:
vbnet
24
name: Alice
age:25
city:New York
3. Return Statement
The return
statement is used to exit a function and return a value.
Example:
python
defsquare(number):
return number ** 2
result = square(4)
print(result)
Output:
16
4. Lambda Functions
Lambda functions are small anonymous functions defined using the lambda
keyword. They are often used for short, simple operations.
Syntax:
python
lambda arguments: expression
Example:
python
# Lambda function to add two numbers
add = lambda a, b: a + b
print(add(2, 3)) # Output: 5
# Lambda function to find the square of a number
square = lambda x: x ** 2
print(square(5)) # Output: 25
5. Built-in Functions
Python provides many built-in functions that you can use directly. Some commonly used built-in functions include len()
, type()
, print()
, input()
, sum()
, min()
, max()
, and many more.
Example:
python
numbers = [1, 2, 3, 4, 5]
# Finding the length of the list
print(len(numbers)) # Output: 5
# Finding the sum of the list
print(sum(numbers)) # Output: 15
# Finding the minimum and maximum values in the list
print(min(numbers)) # Output: 1
print(max(numbers)) # Output: 5
6. User-Defined Functions
While built-in functions are provided by Python, you can also create your own functions to perform specific tasks. These are known as user-defined functions.
Example:
python
def is_even(number):
if number % 2 == 0:
return True
else:
return False
print(is_even(4)) # Output: True
print(is_even(7)) # Output: False

Conclusion
In this post, we covered the basics of functions in Python, including how to define and use them, as well as the differences between built-in and user-defined functions. Understanding functions is crucial for writing efficient and reusable code. In the next post, we will dive into Python’s data structures, such as lists, tuples, sets, and dictionaries. Stay tuned!
Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :- https://play.google.com/store/apps/details?id=com.datavizon.courses&hl=en-IN
► KSR Datavizon Appstore :- https://apps.apple.com/in/app/ksr-datavizon/id1611034268
Most Commented