<jupyter_start><jupyter_text>This notebook is created by  <NAME>. For any feedback or suggestion, please contact me via my email, (<EMAIL>).   🐍 Python 🐍    Modular programming in PythonThis notebook will cover the following topics:- Introduction -  1. Functions -  2. Lambda functions -  3. Built-in functions     -  map function     -  filter function     -  enumerate function     -  zip function -  4. Classes   Introduction  ***Modular programming*** in Python is a very important tool for modern developers. To create robust systems that last, you need to know how to organize your programs so that they can grow over time. The techniques of modular programming and the specific use of Python modules and packages give us tools that we need to succeed as expertise in the fast-changing programming landscape.  1. Functions  A function is a block of code that only runs when it is called. Functions are used to reduce the repetition in code.```Pythondef function_name(inputs):    """    a summary    Parameters    ----------    inputs : type        description    Returns    -------    type        description    """    block of code        return result```<jupyter_code># Example 1

def add (a , b):
    """
    add is used to calculate the summation of two variables


    Parameters
    ----------
    a : Numeric
        a numeric variable
    b : Numeric
        a numeric variable

    Returns
    -------
    c : Numeric
        a + b
    """
    c = a + b
    
    return c<jupyter_output><empty_output><jupyter_text>add(2,3)<jupyter_code># Calling a function
 
add(2, 3)
# Example 2

def subtract (a , b):
    """
    subtract is used to calculate the subtraction of two variables


    Parameters
    ----------
    a : Numeric
        a numeric variable
    b : Numeric
        a numeric variable

    Returns
    -------
    c : Numeric
        a - b
    """
    c = a - b
    
    return c
subtract(2, 3)
# How to get the documentation of a function

print(add.__doc__)<jupyter_output>add is used to calculate the summation of two variables


    Parameters
    ----------
    a : Numeric
        a numeric variable
    b : Numeric
        a numeric variable

    Returns
    -------
    c : Numeric
        a + b<jupyter_text>2. Lambda functions  We can make a function in one line using `lambda` as```Pythonfunction_name = lambda input1, input2: statement```<jupyter_code>add_lambda = lambda a, b: a + b
add_lambda(2, 3)<jupyter_output><empty_output><jupyter_text>3. Built in functions  Python has a set of built-in functions.You can find a list of them here.<jupyter_code># Function for absolute value

abs(-3)
a = '2'

print(type(a), type(float(a)))<jupyter_output><class 'str'> <class 'float'><jupyter_text>Let's learn some more useful and complicated built-in functions.  3.1 `map` function  The `map()` function applies a specific function for each item in an iterable structure.```Pythonmap(function_name, iterable)```<jupyter_code>add_lambda = lambda a, b: a + b

add_lambda(1, 2)
# By entering 2 lists, addition becomes concatenation.

add_lambda([1, 3], [3, 2])
# To add up each two items, we need to use the map()

x = map(add_lambda, [1, 3], [3, 2])<jupyter_output><empty_output><jupyter_text>The output of `map()` has a type of map! 🤯<jupyter_code># Type is another built-in function

type(x)<jupyter_output><empty_output><jupyter_text>So, we use another built-in function like `list()` to get our data in list type.<jupyter_code>list(x)<jupyter_output><empty_output><jupyter_text>3.2 `filter` function  The `filter()` function returns an iterator where the items are filtered through a function to test if the item is accepted or not.<jupyter_code>def day_check(day):
    workday_list = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
    
    return day in  workday_list


        
my_list = ['Python', 'May', 1, 'Mon', 'Fri', 'Sun', 'Sunday']

x = filter(day_check, my_list)

type(x)<jupyter_output><empty_output><jupyter_text>As is shown, the output of `filter()` has a type of filter. We use another build-in function like `tuple()` to get the data.<jupyter_code>tuple(x)<jupyter_output><empty_output><jupyter_text>3.3 `enumerate` function The `enumerate()` function takes a collection and returns it as an enumerate object.<jupyter_code>x = ['France', 'Japan', 'USA']

y = enumerate(x)
y
list(y)
for i, country in enumerate(x):
    print(country)
def even_items(iterable):
    return [v for i, v in enumerate(iterable, start=1) if not i % 2]
seq = list(range(1, 11))

print(seq)
even_items(seq)<jupyter_output><empty_output><jupyter_text>Reference   3.4 `zip` function Python’s `zip()` function creates an iterator of tuples that will aggregate elements from two or more iterables.<jupyter_code>a = [1, 2, 5, 1.3]
b = ['Nike', ' Adidas', 'Soccer', 'Volleyball']

c = zip(a, b)
c
list(c)<jupyter_output><empty_output><jupyter_text>Reference    4. Classes Python is an ***object-oriented programming language*** and almost everything is an object in Python with its own attributes and methods TO create a class we use the keyword `class`<jupyter_code>class Calculator:
    
    intro = 'Hi, this is my calculator! '
    
    def add(self, a, b):
        return a + b
    
    def multiply(self, a, b):
        return a * b
MyClass = Calculator()

# Methods
MyClass.add(1, 2)
# Attribute 

print (MyClass.intro)<jupyter_output>Hi, this is my calculator!
StarcoderdataJupyterStructuredCleanDedup