Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…
Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain.
Defining a Function:
In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses, and a colon. The function body is indented behind the definition line.
def capital(word):
print(f"{word.upper()}")
The camel function in this example takes one parameter and prints out a capital cased word.
Calling a Function:
To use a function, we call its name and provide required parameters if asked. This is how you would refer to the capital function:
capital("Use Python")
>>>USE PYTHON
Returning Values:
Functions can also return values using the return
statement. This is useful when you want the function to calculate something and provide a result back to the caller.
def mul(a, b):
return a * b
print(mul(7, 8))
>>>56
Default Arguments:
You can provide default values for function parameters, so they become optional when calling the function. If a value is not provided, the default value is used.
def listed(original_number, offset=2):
return original_number - offset
print(listed(9))
print(listed(9,5))
>>>7
>>>4
Variable Number of Arguments:
*args (for positional arguments) and **kwargs (for keyword arguments) enable functions to provide a variable number of arguments.
def avg_marks(*marks):
return sum(marks)/len(marks)
avg_marks(81,92,88,65,79,83,90)
>>>82.57
Lambda Functions:
Lambda functions are small, anonymous functions defined using the lambda
keyword. They are often used for simple operations.
marks=[81,92,88,65,79,83,90]
total = lambda x: sum(x)
print(total(marks))
>>>578
These are just the basics of Python functions. They play a crucial role in organizing and structuring your code for better readability and maintainability. Feel free to ask if you have more specific questions about functions or any other aspect of Python programming!
You can pass many arguments through a function in Python using a couple of techniques. Let’s explore two common ways: using positional arguments and using the *args
syntax.
Using Positional Arguments:
Positional arguments are passed to a function in the order they are defined. You can simply provide the values when calling the function, separated by commas.
def print_names(name1, name2, name3):
print(f"Name 1: {name1}")
print(f"Name 2: {name2}")
print(f"Name 3: {name3}")
print_names("Alice", "Bob", "Charlie")
In this example, the print_names
function accepts three arguments (name1
, name2
, and name3
), and you pass three values when calling it.
Using *args
for Variable Number of Arguments:
If you want to pass a variable number of arguments without having to define each argument explicitly, you can use the *args
syntax. This allows you to pass any number of positional arguments, and they will be collected into a tuple within the function.
def print_all(*args):
for arg in args:
print(arg)
print_all("Apple", "Banana", "Cherry", "Date")
In this example, the print_all
function accepts any number of arguments and prints each one.
Example with Both Techniques:
You can also mix both techniques if you want to pass a combination of explicitly defined arguments and a variable number of arguments.
def show_info(name, age, *hobbies):
print(f"Name: {name}")
print(f"Age: {age}")
print("Hobbies:", hobbies)
show_info("Alice", 30, "Reading", "Swimming", "Cooking")
In this example, the show_info
function takes the first two arguments (name
and age
) as positional arguments and then collects any additional arguments as hobbies
using *args
.
Remember that *args
should always be the last parameter in the function definition, as it collects all remaining positional arguments.