My QA Projects

QA Projects I was involded.

View on GitHub

Function Arguments


Positional Arguments

def name_joiner(first, last, title):
    return title + ' ' + first + ' ' + last
print(name_joiner('Stephen', 'Strange','Dr.')) 
print(name_joiner('Dr.'. 'Stephen', 'Strange')) 
def my_function(arg1, arg2, arg3):
    """
    This function takes three positional arguments.
    """
    print("Argument 1:", arg1)
    print("Argument 2:", arg2)
    print("Argument 3:", arg3)

# Calling the function with positional arguments
my_function("Hello", 42, True)

Keyword Arguments

def function(param1, param2):
    function body

function(param2=arg2, param1=arg1)
def greet(name, age):
    """
    This function greets the person with the given name and age.
    """
    print("Hello,", name + "!", "You are", age, "years old.")

# Calling the function with keyword arguments
greet(name="Alice", age=30)

Keyword Argument Examples

def name_joiner(first, last, title):
    return title + ' ' + first + ' ' + last
print(name_joiner(title='Dr.', first='Stephen', last='Strange'))
# Output
Dr. Stephen Strange

Mix positional and keyword arguments

def name_joiner(first, last, title):
    return title + ' ' + first + ' ' + last
print(name_joiner('Stephen', title='Dr.',last='Strange'))
# Output
Dr. Stephen Strange

Default Arguments

def function(param1, param2=value):
    function body

function(arg1)

Default Argument Example 1

def name_joiner(first, last, title=''):
    # title is now optional and an empty string
    return title + ' ' + first + ' ' + last
print(name_joiner('Stephen', 'Strange'))
# Output
Stephen Strange

Default Argument Example 2

def name_joiner(first, last, title=''):
    # title is now optional and an empty string
    return title + ' ' + first + ' ' + last
print(name_joiner('Stephen', 'Strange', 'Mr.'))
# Output
Mr. Stephen Strange

Variable Number of Arguments

def function (*params):
    function body
function(1,2,3)

Variable Number of Arguments Example1

def mean(*numbers, prec=2):
    # round our mean by default precision of 2
    m=sum(numbers)/len(numbers)
    # sum and len accept sequences
    return round(m,prec)

print(mean(3,3,4,1,2,6))
# Output
# 3.17

print(mean(16, 11, 431, pres=1))
# Output
# 152.7

Variable Number of Arguments Example2

def sum_all(*sequences):
    # all arg have to be #s of sequences
    total = 0
    for seq in sequences:
        for n in seq
        total +=n
    return total

print(sum_all())
print(sum_all([1,2,3]))
# sequence is a tuple ([1,2,3])
print(sum_all([1,2],[3], [4,5,6]))
# passing 3 lists

Call a function with parenthesis

def make_a_sound():
    print('quack')
def agree():
    True
def agree():
    if agree():
        print('Splendid')
    else:
        print('That was to be expected.')

Arguments and Parameters

Argument = value you pass to the function Parameter = witin the function Function should only be responsible for one thing

def echo(anything):
    return anything + ' / ' + anything

echo('Rumplestiltskin')

None is usefull

def whatis(thing):
    if thing is None:
        print(thing, "is None")
    elif thing:
        print(thing, "is True")
    else: 
        print(thing, "is False")
whatis(None)
whatis(True)
whatis(False)
# try with real values
whatis(0)
whatis(0.0)
whatis('')
whatis(())
whatis([])
whatis({})
whatis([0])
whatis(set())
whatis([''])

Positional Arguments

def menu(wine, entree, dessert):
    return{'wine':wine, 'entree': entree, 'dessert': dessert}

menu('Chardonnay', 'chicken', 'cake')

Another example

def f(qty, item, price):
    print(f'{qty} {item} cost ${price:.2f}')

f('bananas', 1.74, 6)

Keyword Arguments

menu(entree='beef', dessert= 'Streuselkuchen', wine='Bordeaux')
#Output
{'wine':'Bordeaux', 'entree': 'beef', 'dessert': 'Streuselkuchen' }  

functions is expected to run each time with a fresh result list

specify Default Paramter Values

def buggy():

def works(arg):

def nobuggy(arg, result=None):

Explode/Gather Positional Arguments with *

grouping a variable number of postional arguments into a single tuple or parameter values. (tuples are non mutable)

def print_args(*args):
    print('Positional tuple: ', args)

def print_args(*args):
    print('Positional tuple: ', args)
    
print_args(3,2,1, 'wait!'. 'uh..')
# Output
Positional tuple: (3,2,1, 'wait!'. 'uh..')
def print_more(required1, required2, *args):
    print('this is required 1: ', required1)
    print('this is required 2: ', required2)
    print('all the rest: ', args)

print_more('cap', 'gloves', 'monocole', 'mustach wax')
# Output

puff-args (values are exploded)

inhale-args (values are gathered)

Explode/Gather Positional Arguments with **

def print_kwargs(**kwargs):
    print('keyword arguments: ' , kwargs)
print_kwarks()
# Output
keyword arguments: {}

print_kwargs(wine='Bordeaux', entree='mutton', dessert= 'macaroon' )
# Output
keyword arguments: {dessert= 'macaroon', wine='Bordeaux', entree='mutton'}

Summarizing

Keyword-Only Arguments

def print_data(data, *, start=0, end=100):
    for value in (data[start:end]):
        print(value)

data = ['a','b','c','d','e','f',]
print_data(data)

# Output

a
b
c
d
e
f

Mutable and Immutable Arguments

page 151 passing arguments to functions:

outside = ['one','fine','day']
def mangle(arg):
    arg[1] = 'terrible!'

>>> outside
# Output
['one','fine','day']
>>> mangle(outside)
outside
['one','terrible!','day']