Functions:

  • Functions are blocks of reusable code that perform a specific task
  • It runs when it is called
  • Data known as parameters, can be passed into a function to get the result data
  • Data known as arguments, can be passed into a function when it is called

Types of Functions:

  • User-defined functions: These are functions that we create our own based on our requirements.
  • Built-in library function: These are Standard functions in Python that are available to use.

User-Defined Functions:

How to Define & Call Functions:

  • Use the “def” keyword to start the function definition.
  • Follow it with the function name and parentheses ().
  • Add a colon ” : ” to indicate the start of the function body.
  • Indent the body of the function.
  • To call a function, use the function name followed by parentheses ().
  • If the function requires arguments, you pass them inside the parentheses.

Syntax: 

             def function_name(parameters):

                     Body of Statement

                     return expression

            function_name()

Let’s see three examples:

# Define the function
def greet():
    print("Hello, Rana!")

# Call the function
greet()
Output:
Hello, Rana!
# Define the function
def calculate_square(number):
    """
    This function takes a number and returns its square.
    """
    square = number * number
    return square

# Call the function
result = calculate_square(4)
print(result)  

Output: 
16
# Define the function
def add_numbers(a, b):
    return a + b

# Call the function
result = add_numbers(3, 5)
print(result)  

Output: 8

Definition & Difference Between Parameter & Argument:

  • Definition of Parameter:  A parameter is a variable specified within the parentheses in the function definition.

  • Definition of Argument: An argument is a value provided to the function when it is called.

  • Key Difference:

    • Parameters appear in function definitions while Arguments appear in function calls.
    • Parameters act as placeholders for the values that will be passed whereas Arguments are the actual values supplied to the function.
  • Code Example:
def greeting(name):  # 'name' is a parameter
  print(f"Hi {name}")

greeting("Rana")  # 'Rana' is an argument

Output:
Hi Rana

The Default Arguments:

  • By default, a function must be called with the exact number of arguments it expects.
  • If the function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less. Otherwise, it will give an error
  • Example: In the below example, the function expects two arguments but I have provided three arguments to add, so it will give an error
# Define the function
def add_numbers(a, b):
    return a + b

# Call the function
add_numbers(3,4,5)
Output:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-086fe64a5dfc> in <cell line: 6>()
      4 
      5 # Call the function
----> 6 add_numbers(3,4,5)

TypeError: add_numbers() takes 2 positional arguments but 3 were given

Keyword Arguments:

  • Keyword arguments allow you to specify argument values by the parameter name, which can make your function calls more readable and flexible.
  • We can send arguments with the key = value syntax.

  • It does not matter in which order the arguments are

  • Examples
def describe_pet(animal_type, pet_name):
    """
    Display information about a pet.
    """
    print(f"\nI have a {animal_type}.")
    print(f"My {animal_type}'s name is {pet_name}.")

# Using keyword arguments to call the function
describe_pet(animal_type='cat', pet_name='Mini')
describe_pet(pet_name='Tommy', animal_type='dog')

Output:
I have a cat.
My cat's name is Mini.

I have a dog.
My dog's name is Tommy.

Positional Arguments:

  • Positional arguments in functions are arguments that need to be passed to the function in the same order in which the function’s parameters are defined.
  • The values of positional arguments are assigned to the corresponding parameters based on their position.
def info(name, age):
    print("Hi, I am", name)
    print("My age is ", age)


print("When argument is given in order")
info("Rana", 35)

print("\nWhen argument is not in order")
info(35, "Rana")
Output:
When argument is given in order
Hi, I am Rana
My age is  35

When argument is not in order
Hi, I am 35
My age is  Rana

Arbitrary Arguments:

  • Arbitrary arguments in Python allow a function to accept an indefinite number of arguments.
  • There are two types of Arbitrary Arguments:
    • *args in Python (Non-Keyword Arguments)
    • **kwargs in Python (Keyword Arguments)
  • You can use *args to handle positional arguments and **kwargs to handle keyword arguments.
  • Example with *args: In this example, the print_numbers function can take any number of positional arguments and print each one.
def print_numbers(*num):
    for number in num:
        print(number)

print_numbers(1, 2, 3, 4, 5,6)
Output:
1
2
3
4
5
6
  • Example with **kwargs: In this example, the print_person_info function can take any number of keyword arguments and print each key-value pair.
def print_person_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_person_info(name="Rana", age=35, city="Dhaka")
Ouput:
name: Rana
age: 35
city: Dhaka
  • We can also combine both *args and **kwargs in the same function where mixed_function can accept both positional and keyword arguments and print them separately.
def mixed_function(*args, **kwargs):
    print("Positional arguments:", args)
    print("Keyword arguments:", kwargs)

mixed_function(1, 2, 3, name="Rana", age=35)
Output:
Positional arguments: (1, 2, 3)
Keyword arguments: {'name': 'Rana', 'age': 35}

Parameter Default Value:

  • We can put default value for parameter
  • When we call function without argument, it will give default value
def my_country(country = 'Bangladesh'):
  print("I am from " + country)

my_country('India')   #Call function with argument
my_country()  #Call function without argument
Output:
I am from India
I am from Bangladesh

Any data type as an Argument:

We can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.

E.g. if you send a tuple as an argument, it will still be a tuple when it reaches the function:

def my_function(y):
  for x in y:
    print(x)

fruits = ("apple", "banana", "cherry")
country = ["Bangladesh","India","Ireland"]
numbers = {1,2,3,4,5}
my_function(fruits)
print('-------')
my_function(country)
print('-------')
my_function(numbers)
Output:
apple
banana
cherry
-------
Bangladesh
India
Ireland
-------
1
2
3
4
5

Return Values:

To let a function return a value, use the return statement:

def my_func(a,b):
  return a*b

my_func(2,3)
Output:
6

Positional-Only Arguments:

  • Positional-only arguments in Python are specified such that they can only be provided positionally and not as keyword arguments.
  • This feature helps in preventing the user from passing certain arguments using keywords, thereby enforcing a specific usage pattern.
  • To define positional-only arguments, you use a ‘/’ in the function definition. Parameters before the ‘/’ are positional-only.
def greet(name, /, greeting="Hello"):
        print(f"{greeting}, {name}!")
  • The ‘/’ indicates that ‘name’ is a positional-only argument.
  • ‘Greeting’ can still be used as a positional or keyword argument.
# Correct usage with positional arguments
greet("Rana")                  
greet("Rana", "Good morning")  #
Output:
Hello, Rana!
Good morning, Rana!
  • ‘name’ is passed positionally.
  • ‘greeting’ can be passed either positionally or as a keyword argument.
# Incorrect usage with keyword arguments for positional-only parameters
greet(name="Rana")  
Output:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-bbe7f3821c9f> in <cell line: 1>()
----> 1 greet(name="Rana")

TypeError: greet() got some positional-only arguments passed as keyword arguments: 'name'

Trying to pass ‘name’ as a keyword argument results in a TypeError.

Why Use Positional-Only Arguments?

  1. Clarity: It makes the function’s intended usage more clear and avoids confusion between parameters that should strictly be passed positionally.
  2. Consistency: Helps maintain a consistent function signature, especially in public APIs where the function’s usage pattern should be enforced.
  3. Performance: In some cases, it can lead to performance improvements because the function does not need to handle keyword arguments parsing for those parameters.

Keyword-Only Arguments:

  • Keyword-only arguments in Python are specified such that they can only be provided using their names, not as positional arguments.
  • This feature helps in making the function calls more explicit and readable, especially when dealing with optional arguments that have default values.
  • To define keyword-only arguments, you use a ‘*’ in the function definition. Parameters after the ‘*’ are keyword-only.
def greet(*, name, greeting="Hello"):
    print(f"{greeting}, {name}!")
  • The ‘*’ indicates that all arguments following it (‘name’ and ‘greeting’) are keyword-only arguments.
greet(name="Rana")                
greet(name="Rana", greeting="Good morning")  
Output:
Hello, Rana!
Good morning, Rana!
  • ‘name’ and ‘greeting’ are passed as keyword arguments.
greet("Rana") 
Ouput:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-b1c54412daef> in <cell line: 1>()
----> 1 greet("Rana")

TypeError: greet() takes 0 positional arguments but 1 was given
  • Trying to pass ‘name’ as a positional argument results in a TypeError.

Why Use Keyword-Only Arguments?

  1. Clarity: It makes the function’s intended usage more clear and avoids confusion between parameters that should strictly be passed as keywords.
  2. Explicitness: Ensures that certain arguments are explicitly named, improving code readability and reducing errors.
  3. Flexibility: Provides a way to add new parameters to functions in a backward-compatible way, as new keyword-only parameters won’t interfere with existing positional arguments.

Combine Positional-Only and Keyword-Only:

  • We can combine the two argument types in the same function.
  • Any argument before the / , are positional-only, and any argument after the *, are keyword-only.
def custom_function(a, b, /, *, c, d="default"):
    print(f"a: {a}, b: {b}, c: {c}, d: {d}")
  • a and b are positional-only arguments, indicated by the /.
  • c and d are keyword-only arguments, indicated by the *.
  • d has a default value of “default”.
custom_function(1, 2, c=3, d=4)         
custom_function(5, 6, c=7)              
Output:
a: 1, b: 2, c: 3, d: 4
a: 5, b: 6, c: 7, d: default
  • a and b are passed positionally.
  • c and d are passed as keyword arguments.
custom_function(1, 2, 3, 4)
Output:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-58-9a7f1451816a> in <cell line: 1>()
----> 1 custom_function(1, 2, 3, 4)

TypeError: custom_function() takes 2 positional arguments but 4 were given
  • Attempting to pass c and d as positional arguments results in a TypeError.
custom_function(a=1, b=2, c=3, d=4) 
Output:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-4ac822930636> in <cell line: 1>()
----> 1 custom_function(a=1, b=2, c=3, d=4)

TypeError: custom_function() got some positional-only arguments passed as keyword arguments: 'a, b'
  • Attempting to pass a and b as keyword arguments results in a TypeError.

Recursive Functions:

  • Python supports function recursion, allowing a function to call itself.
  • Recursion is a useful concept in programming and mathematics for looping through data to achieve a result.
  • However, developers must be cautious, as poorly implemented recursion can lead to infinite loops or excessive resource usage.
  • For example, the recursion() function calls itself, decrementing the x variable each time until it reaches 0.
def recursion(x):
  if(x > 0):
    result = x + recursion(x - 1)
    print(result)
  else:
    result = 0
  return result

print("\n\nRecursion Example Results")
recursion(5)
Output:

Recursion Example Results
1
3
6
10
15
15

Built-in library function:

Python includes a wide array of built-in functions that are readily available without the need to import additional libraries.

print(): Outputs a message to the console.

print("Hello, Rana!")
Output:
Hello, Rana!

type(): Returns the type of an object.

type(10)
Output:
int

len(): Returns the length of an object.

len("Hello")
Output:
5

input(): Reads a string from user input.

name = input("Enter your name: ")
print(name)
Output:
Enter your name: Rana
Rana

abs(): Returns the absolute value of a number.

abs(-5)
Output:
5

round(): Rounds a number to a specified number of decimals.

round(3.14159, 2)
Output:
3.14

max() and min(): Returns the maximum or minimum value in an iterable.

max([1, 2, 3])  
Output:
3
min([1, 2, 3]) 
Output:
1

sum(): Sums the items of an iterable.

sum([1, 2, 3])
Output:
6

list(): Converts an iterable to a list.

list("hello")
Output:
['h', 'e', 'l', 'l', 'o']

tuple(): Converts an iterable to a tuple.

tuple("hello")
Output:
('h', 'e', 'l', 'l', 'o')

set(): Converts an iterable to a set.

set("hello")
Output:
{'e', 'h', 'l', 'o'}

dict(): Creates a dictionary.

dict(a=1, b=2)
Output:
{'a': 1, 'b': 2}

int(): Converts a value to an integer.

int("10")
Output:
10

float(): Converts a value to a float.

float("10")
Output:
10.0

str(): Converts a value to a string.

str(10)
Output:
'10'

Map ():

‘map()’ is a built-in Python function that applies a given function to all items in an input iterable (like a list, tuple, etc.) and returns an iterator (or list in Python 2) that produces the results.

Syntax:

map(function, iterable, ...)
  • function: The function to apply to each item of the iterable.
  • iterable: The iterable(s) (e.g., list, tuple) to process.
  • map() can take multiple iterables as arguments if the function provided can take multiple arguments.

How map() Works:

  • map() applies the function to each item of the iterable and returns a map object (which is an iterator).
  • The map object can be converted to a list, tuple, or another collection by passing it to the appropriate constructor (e.g., list(map_object)).

When to Use map():

  • When you have a function that you want to apply to every element in an iterable.
  • When you need to apply a function across multiple iterables simultaneously.

Example:

# Applying a function to square each number in a list
numbers = [1, 2, 3, 4, 5]
def square(x):
    return x ** 2

squares = map(square, numbers)
print(list(squares))
Output:
[1, 4, 9, 16, 25]
# Adding corresponding elements of two lists

numbers1 = [1, 2, 3]
numbers2 = [4, 5, 6]
result = map(lambda x, y: x + y, numbers1, numbers2)
list(result)  
Output: 
[5, 7, 9]
# Converting a list of numbers to a list of strings
numbers = [1, 2, 3, 4]
result = map(str, numbers)
list(result) 
Output:
['1', '2', '3', '4']

Filter():

  • filter() is a built-in Python function that constructs an iterator from elements of an iterable (like a list, tuple, etc.) for which a specified function returns True.

Syntax:

filter(function, iterable)
  • function: A function that tests each element of the iterable. It should return True if the element should be included in the output and False otherwise.If function is None, it removes all elements in the iterable that are false (e.g., 0, None, False, ”).
  • iterable: The iterable whose elements are to be filtered.

How filter() Works:

  • filter() applies the function to each item of the iterable and only includes those items for which the function returns True.
  • It returns a filter object (which is an iterator) that can be converted to a list, tuple, or another collection by passing it to the appropriate constructor (e.g., list(filter_object)).

When to Use filter():

  • When you want to select only certain elements from an iterable based on a condition.
  • When you need a clean, functional approach to filtering data.
  • When you want to avoid writing loops for simple filtering tasks.

Example:

# Filtering even numbers from a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def is_even(x):
    return x % 2 == 0

evens = filter(is_even, numbers)
list(evens) 
Output: 
[2, 4, 6, 8, 10]

When working on data science projects in Python, several built-in libraries and functions are commonly used to manipulate, analyze, and visualize data. Here are some of the most frequently used built-in libraries and their key functions, details will be discussed in other specific content

NumPy

Library for numerical computations and array manipulation.

  • np.array(): Creates a NumPy array.
  • np.arange(): Generates an array of evenly spaced values.
  • np.linspace(): Generates an array of evenly spaced values over a specified range.
  • np.mean(): Computes the mean of an array.
  • np.median(): Computes the median of an array.
  • np.std(): Computes the standard deviation of an array.
  • np.sum(): Sums all elements of an array.
  • np.dot(): Computes the dot product of two arrays.
  • np.linalg.inv(): Computes the inverse of a matrix.
  • np.random.rand(): Generates random numbers in a given shape.

Pandas

Library for data manipulation and analysis, especially with tabular data.

  • pd.DataFrame(): Creates a DataFrame.
  • pd.Series(): Creates a Series.
  • df.head(): Returns the first few rows of the DataFrame.
  • df.tail(): Returns the last few rows of the DataFrame.
  • df.describe(): Provides summary statistics of the DataFrame.
  • df.info(): Provides information about the DataFrame.
  • df.groupby(): Groups data by a particular column.
  • df.merge(): Merges two DataFrames.
  • df.fillna(): Fills missing values in the DataFrame.
  • df.dropna(): Drops rows with missing values.

Matplotlib

Library for creating static, animated, and interactive visualizations.

  • plt.plot(): Plots a line chart.
  • plt.bar(): Creates a bar chart.
  • plt.hist(): Creates a histogram.
  • plt.scatter(): Creates a scatter plot.
  • plt.show(): Displays the plot.
  • plt.title(): Sets the title of the plot.
  • plt.xlabel(): Sets the label for the x-axis.
  • plt.ylabel(): Sets the label for the y-axis.

Seaborn

Library for statistical data visualization, built on top of Matplotlib.

  • sns.lineplot(): Plots a line chart.
  • sns.barplot(): Creates a bar chart with confidence intervals.
  • sns.heatmap(): Creates a heatmap.
  • sns.boxplot(): Creates a boxplot.
  • sns.pairplot(): Creates pairwise relationships in a dataset.
  • sns.distplot(): Creates a histogram with a kernel density estimate.

SciPy

Library for scientific and technical computing.

  • scipy.stats(): Contains functions for statistical analysis.
  • scipy.optimize(): Contains functions for optimization.
  • scipy.integrate(): Contains functions for integration.
  • scipy.signal(): Contains functions for signal processing.
  • scipy.linalg(): Contains linear algebra functions.

Scikit-learn

Library for machine learning, including tools for model building, evaluation, and preprocessing.

  • train_test_split(): Splits the dataset into training and testing sets.
  • StandardScaler(): Standardizes features by removing the mean and scaling to unit variance.
  • LinearRegression(): Creates a linear regression model.
  • LogisticRegression(): Creates a logistic regression model.
  • RandomForestClassifier(): Creates a random forest classifier.
  • accuracy_score(): Computes the accuracy of a model.
  • confusion_matrix(): Computes the confusion matrix.
  • cross_val_score(): Evaluates a model using cross-validation.

TensorFlow & PyTorch

Libraries for deep learning.

  • TensorFlow:
    • tf.constant(): Creates a constant tensor.
    • tf.Variable(): Creates a variable tensor.
    • tf.keras.Model(): Defines a Keras model.
    • tf.data.Dataset(): Creates a dataset object for feeding data to models.
  • PyTorch:
    • torch.tensor(): Creates a tensor.
    • torch.nn.Module(): Defines a neural network module.
    • torch.optim.Adam(): Defines an optimizer.
    • torch.utils.data.DataLoader(): Loads data for training/testing.

Above some common functions used for data science project but I didn’t explain anything here as it will be too long , there will specific post for each library where you will find details with example 

Register

Login here

Forgot your password?

ads

ads

I am an enthusiastic advocate for the transformative power of data in the fashion realm. Armed with a strong background in data science, I am committed to revolutionizing the industry by unlocking valuable insights, optimizing processes, and fostering a data-centric culture that propels fashion businesses into a successful and forward-thinking future. - Masud Rana, Certified Data Scientist, IABAC

© Data4Fashion 2023-2024

Developed by: Behostweb.com

Please accept cookies
Accept All Cookies