<jupyter_start><jupyter_text>Function  Why Functions?* Reuse code instead of rewrite * To make the code modular (to simplify the code)<jupyter_code>def f1():
    pass<jupyter_output><empty_output><jupyter_text>naming convension ==> use verb phrase<jupyter_code># best practice 
def increament(x, y): 
    return x + y 
increament(3, 1)
# bad practice 
def increament():
    x = int(input('x: '))
    y = int(input('y: '))
    print(x+y)
    
increament()
# best practice 
def increament(x, y): 
    return x + y 
print(increament(3, 1))
z = increament(3, 1) 
w = increament(3, 1) + 10<jupyter_output>4<jupyter_text>Write the following functions:1. a function that generate a random list 2. a fucntion that takes even number input from the user 3. a search function that search for the number in the list (search only in even numbers)<jupyter_code>from random import randint

def is_even(x):
    if x % 2 == 0:
        return True
    else:
        return False 
    
def is_even2(x):
    return x % 2 == 0


def gen_list(list_size, max_num):
    return [randint(0, max_num) for _ in range(list_size)]

def read_even_num():
    while True:
        num = int(input('plz enter even num: '))
        if is_even(num):
            break 
    return num 
def search(a_list, num):
    found = False 
    for n in a_list:
        if not is_even(n):
            continue 
        if n == num:
            found = True 
            break 
    return found 

def search2(a_list, num):
    found = False 
    indx = -1 
    for i in range(len(a_list)):
        if not is_even(a_list[i]):
            continue 
        if a_list[i] == num:
            found = True 
            indx = i 
    return found, indx 
           
            
l1 = gen_list(1000, 10)
number = read_even_num()
search(l1, number)
l1 = gen_list(list_size=1000, max_num=10)
number = read_even_num()
search2(num=number, a_list=l1)
l1 = gen_list(list_size=1000, max_num=10)
number = read_even_num()
search2(num=number, a_list=l1)
'hello'.find('a')
'hello'.find('e')
2 in [1, 3, 6]
def increment(number=1, by=1): # function with default parameters
    return number + by  
increment()
increment(number=3, by=1)
def increment(number=1, by=1): # function with default parameters
    return (number, number + by )
increment(number=4, by=10)
2 in (1, 3, 6)<jupyter_output><empty_output><jupyter_text>args, kwargs parameters<jupyter_code># ** converts parameters to a tupe
def f1(*args):
    return args 
f1('ahmed', 10)
f1('ahmed', 10, 20)
# ** converts parameters to a dictionary 
def f2(**kwargs):
    return kwargs
f2(name='ahdmed', i=10, j=20)
def f3(i=-1, **kwargs):
    return i, kwargs
    
f3(i=10, name='ahmed', j=30)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12038404726735598, 'fraction_numerical': 0.03249630723781388, 'mean_word_length': 3.1805555555555554, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '26980', 'license': 'Apache-2.0', 'n_tokens_mistral': 1033, 'n_tokens_neox': 965, 'n_words': 283, 'path': '05_functions.ipynb', 'repo_name': 'maramsofyan/Machine-Learning', 'size': '2708'}