<jupyter_start><jupyter_text>Types Of Functions 1. Built-in Functions2. User-defined Functions  Built-in Functions  1. abs()<jupyter_code># find the absolute value

num = -100

print(abs(num))<jupyter_output>100<jupyter_text>2. all() return value of all() functionTrue: if all elements in an iterable are trueFalse: if any element in an iterable is false<jupyter_code>lst = [1, 2, 3, 4]
print(all(lst)) 

lst = (0, 2, 3, 4)    # 0 present in list 
print(all(lst))

lst = []              #empty list always true
print(all(lst))

lst = [False, 1, 2]   #False present in a list so all(lst) is False
print(all(lst))<jupyter_output>False<jupyter_text>dir() The dir() tries to return a list of valid attributes of the object.If the object has __dir__() method, the method will be called and must return the list of attributes.If the object doesn't have __dir()__ method, this method tries to find information from the __dict__ attribute (if defined), and from type object. In this case, the list returned from dir() may not be complete.<jupyter_code>numbers = [1, 2, 3]

print(dir(numbers))<jupyter_output>['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']<jupyter_text>divmod() The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.<jupyter_code>print(divmod(9, 2)) #print quotient and remainder as a tuple

#try with other number<jupyter_output>(4, 1)<jupyter_text>enumerate() The enumerate() method adds counter to an iterable and returns it  syntax: enumerate(iterable, start=0)<jupyter_code>numbers = [10, 20, 30, 40]

for index, num in enumerate(numbers,1):
    print("index {0} has value {1}".format(index, num))<jupyter_output>index 1 has value 10
index 2 has value 20
index 3 has value 30
index 4 has value 40<jupyter_text>filter() The filter() method constructs an iterator from elements of an iterable for which a function returns true. syntax: filter(function, iterable)<jupyter_code>def find_positive_number(num):
    """
    This function returns the positive number if num is positive
    """
    if num > 0:
        return num
    
number_list = range(-10, 10) #create a list with numbers from -10 to 10
print(list(number_list))

positive_num_lst = list(filter(find_positive_number, number_list))

print(positive_num_lst)<jupyter_output>[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]<jupyter_text>isinstance() The isinstance() function checks if the object (first argument) is an instance or subclass of classinfo class (second argument). syntax: isinstance(object, classinfo)<jupyter_code>lst = [1, 2, 3, 4]
print(isinstance(lst, list))

#try with other datatypes tuple, set
t = (1,2,3,4)
print(isinstance(t, list))<jupyter_output>True
False<jupyter_text>map() Map applies a function to all the items in an input_list. syntax: map(function_to_apply, list_of_inputs)<jupyter_code>numbers = [1, 2, 3, 4]

#normal method of computing num^2 for each element in the list.
squared = []
for num in numbers:
    squared.append(num ** 2)

print(squared)

numbers = [1, 2, 3, 4]

def powerOfTwo(num):
    return num ** 2

#using map() function
squared = list(map(powerOfTwo, numbers))
print(squared)<jupyter_output>[1, 4, 9, 16]<jupyter_text>reduce() reduce() function is for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list.<jupyter_code>#product of elemnts in a list
product = 1
lst = [1, 2, 3, 4]

# traditional program without reduce()
for num in lst:
    product *= num
print(product)

#with reduce()
from functools import reduce # in Python 3.

def multiply(x,y):
    return x*y;

product = reduce(multiply, lst)
print(product)<jupyter_output>24<jupyter_text>2. User-defined Functions Functions that we define ourselves to do certain specific task are referred as user-defined functions If we use functions written by others in the form of library, it can be termed as library functions.  Advantages 1. User-defined functions help to decompose a large program into small segments which makes program easy to understand, maintain and debug.2. If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.3. Programmars working on large project can divide the workload by making different functions.  Example:<jupyter_code>def product_numbers(a, b):
    """
    this function returns the product of two numbers
    """
    product = a * b
    return product

num1 = 10
num2 = 20
print ("product of {0} and {1} is {2} ".format(num1, num2, product_numbers(num1, num2)))<jupyter_output>product of 10 and 20 is 200<jupyter_text>Python program to make a simple calculator that can add, subtract, multiply and division<jupyter_code>def add(a, b):
    """
    This function adds two numbers
    """
    return a + b

def multiply(a, b):
    """
    This function multiply two numbers
    """
    return a * b

def subtract(a, b):
    """
    This function subtract two numbers
    """
    return a - b

def division(a, b):
    """
    This function divides two numbers
    """
    return a / b

print("Select Option")
print("1. Addition")
print ("2. Subtraction")
print ("3. Multiplication")
print ("4. Division")

#take input from user
choice = int(input("Enter choice 1/2/3/4"))

num1 = float(input("Enter first number:"))
num2 = float(input("Enter second number:"))
if choice == 1:
    print("Addition of {0} and {1} is {2}".format(num1, num2, add(num1, num2)))
elif choice == 2:
    print("Subtraction of {0} and {1} is {2}".format(num1, num2, subtract(num1, num2)))
elif choice == 3:
    print("Multiplication of {0} and {1} is {2}".format(num1, num2, multiply(num1, num2)))
elif choice == 4:
    print("Division of {0} and {1} is {2}".format(num1, num2, division(num1, num2)))
else:
    print("Invalid Choice")<jupyter_output>Select Option
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter choice 1/2/3/41
Enter first number:6
Enter second number:8
Addition of 6.0 and 8.0 is 14.0
{'fraction_non_alphanumeric': 0.12205860027326552, 'fraction_numerical': 0.031425535144982544, 'mean_word_length': 4.778947368421052, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '100113', 'license': 'MIT', 'n_tokens_mistral': 2324, 'n_tokens_neox': 2152, 'n_words': 811, 'path': 'Python Functions, Packages, Input_Output, Exception Handling and Debugging/2_function_types.ipynb', 'repo_name': 'sauravraghuvanshi/AI-and-ML-Complete-Learning', 'size': '6587'}