# 11.4. Function Arguments¶

• Argument - value/variable/reference being passed to the function

• Positional argument - value passed to function - order is important

• Keyword argument - value passed to function resolved by name - order is not important

Function definition with parameters:

myfunction(<arguments>)

>>> add(1, 2)
>>> add(a=1, b=2)
>>> add(1, b=2)

argument

Value/variable/reference being passed to the function

positional argument

Value passed to function - order is important

keyword argument

Value passed to function resolved by name - order is not important

## 11.4.1. Positional Arguments¶

• Order of positional arguments has significance

Let's define a function:

>>> def echo(a, b):
...     return f'{a=}, {b=}'


Positional arguments are resolved by order. This mean, that the first argument will be assigned to the first parameter, and the second argument to the second parameter and so on:

>>> echo(1, 2)
'a=1, b=2'


The order of positional parameters is important:

>>> echo(2, 1)
'a=2, b=1'


## 11.4.2. Keyword Arguments¶

• Order of keyword arguments has no significance

Let's define a function:

>>> def echo(a, b):
...     return f'{a=}, {b=}'


Keyword arguments are resolved by name instead of the position. This mean, that the argument with particular name will be assigned to the corresponding parameter with the same name in function signature.

>>> echo(a=1, b=2)
'a=1, b=2'


The order of keyword parameters is not important, because values are assigned by name, not a position:

>>> echo(b=2, a=1)
'a=1, b=2'


## 11.4.3. Positional and Keyword Arguments¶

• Positional arguments must be at the left side

• Keyword arguments must be at the right side

>>> def echo(a, b):
...     return f'{a=}, {b=}'


All positional arguments must be on the left side, and all the required arguments must be on the right side:

>>> echo(1, b=2)
'a=1, b=2'


Passing positional argument which follows keyword argument will yield a SyntaxError:

>>> echo(a=1, 2)
Traceback (most recent call last):
SyntaxError: positional argument follows keyword argument


Positional argument are resolved first. Defining keyword argument which follows positional argument with the same name will yield a TypeError:

>>> echo(1, a=2)
Traceback (most recent call last):
TypeError: echo() got multiple values for argument 'a'


## 11.4.4. Errors¶

>>> def echo(a, b, c, d, e):
...     return f'{a=}, {b=}, {c=}, {d=}, {e=}'
>>>
>>>
>>> echo(1, 2, 3)
Traceback (most recent call last):
TypeError: echo() missing 2 required positional arguments: 'd' and 'e'
>>>
>>> echo(1, 2, 3, d=4)
Traceback (most recent call last):
TypeError: echo() missing 1 required positional argument: 'e'
>>>
>>> echo(1, 2, 3, d=4, 5)
Traceback (most recent call last):
SyntaxError: positional argument follows keyword argument
>>>
>>> echo(1, 2, 4, 5, c=3)
Traceback (most recent call last):
TypeError: echo() got multiple values for argument 'c'
>>>
>>> echo(1, 2, 3, d=4, e=5)
'a=1, b=2, c=3, d=4, e=5'


## 11.4.5. Use Case - 0x01¶

>>> def say_hello(text='say what?'):
...      return text
>>>
>>>
>>> say_hello('hello')
'hello'
>>>
>>> say_hello(text='hello world')
'hello world'
>>>
>>> say_hello()
'say what?'


## 11.4.6. Use Case - 0x02¶

>>> def connect(*args, **kwargs):
...     ...

>>> connect('myusername', 'mypassword')

>>> connect('myusername', 'mypassword', 'example.com', 443, False, 1, True)

>>> connect(host='example.com', username='myusername', password='mypassword')

>>> connect(
...     host='example.com',
...     username='myusername',
...     password='mypassword',
...     port=443,
...     ssl=True,
...     persistent=True)


## 11.4.7. Use Case - 0x03¶

>>> def read_csv(*args, **kwargs):
...     pass

>>> read_csv('iris.csv')

>>> read_csv('iris.csv', encoding='utf-8')

>>> read_csv('iris.csv', encoding='utf-8', parse_dates=['born'])

>>> read_csv('iris.csv', skiprows=3, delimiter=';')

>>> read_csv('iris.csv',
...     encoding='utf-8',
...     skiprows=3,
...     delimiter=';',
...     usecols=['sepal_length', 'species'],
...     parse_dates=['born'])


## 11.4.8. Assignments¶

"""
* Assignment: Function Arguments Sequence
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
1. Define function which takes sequence of integers as an argument
2. Sum only even numbers, use even()
3. Run doctests - all must succeed

Polish:
1. Zdefiniuj funkcję biorącą sekwencję liczb całkowitych jako argument
2. Zsumuj tylko parzyste liczby, użyj even()
3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert total is not Ellipsis, \
'Write solution inside total function'
>>> assert isfunction(total), \
'Object total must be a function'

>>> total([1,2,3,4])
6
>>> total([2,-1,0,2])
4
>>> total(range(0,101))
2550
"""

def even(x):
return x % 2 == 0


"""
* Assignment: Function Arguments Divide
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
1. Define function divide
2. Function takes two arguments
3. Function returns result of the division of both arguments
4. If division cannot be made, stop function and print:
"Argument b cannot be zero"
5. Run doctests - all must succeed

Polish:
1. Zdefiniuj funkcję divide
2. Funkcja przyjmuje dwa argumenty
3. Funkcja zwraca wynik dzielenia dwóch argumentów
4. Jeżeli nie można podzielić zakończ funkcję i wypisz:
"Argument b cannot be zero"
5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert divide is not Ellipsis, \
'Write solution inside divide function'
>>> assert isfunction(divide), \
'Object divide must be a function'

>>> divide(4, 0)
Argument b cannot be zero

>>> divide(4, 2)
2.0
"""


"""
* Assignment: Function Arguments Power
* Type: class assignment
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
1. Define function power
2. Function takes two arguments
3. Second argument is optional
4. Function returns power of the first argument to the second
5. If only one argument was passed, consider second equal to the first one
6. Run doctests - all must succeed

Polish:
1. Zdefiniuj funkcję power
2. Funkcja przyjmuje dwa argumenty
3. Drugi argument jest opcjonalny
4. Funkcja zwraca wynik pierwszego argumentu do potęgi drugiego
5. Jeżeli tylko jeden argument był podany, przyjmij drugi równy pierwszemu
6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert power is not Ellipsis, \
'Write solution inside power function'
>>> assert isfunction(power), \
'Object power must be a function'

>>> power(4, 3)
64
>>> power(3)
27
"""


"""
* Assignment: Function Arguments Translate
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 5 min

English:
1. Define function translate with parameter text
2. Use str.join() with generator expression to iterate over text
3. If letter is in PL then use conversion value as letter,
otherwise take letter
4. Return from function translated text
5. Run doctests - all must succeed

Polish:
1. Zdefiniuj funkcję translate przyjmującą parametr text
2. Użyj str.join() z wyrażeniem generatorowym do iteracji po text
3. Jeżeli litera jest w PL to użyj skonwertowanej wartości jako litera,
w przeciwnym przypadku to weź literę
4. Zwróć z funkcji przetłumaczony text
5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert translate is not Ellipsis, \
'Write solution inside translate function'
>>> assert isfunction(translate), \
'Object translate must be a function'

>>> translate('zażółć')
'zazolc'
>>> translate('gęślą')
'gesla'
>>> translate('jaźń')
'jazn'
>>> translate('zażółć gęślą jaźń')
'zazolc gesla jazn'
"""

PL = {'ą': 'a', 'ć': 'c', 'ę': 'e',
'ł': 'l', 'ń': 'n', 'ó': 'o',
'ś': 's', 'ż': 'z', 'ź': 'z'}


"""
* Assignment: Function Arguments Clean
* Type: homework
* Complexity: medium
* Lines of code: 15 lines
* Time: 13 min

English:
1. Write function cleaning up data
2. Function takes one argument of type str
3. Function returns cleaned text
4. Run doctests - all must succeed

Polish:
1. Napisz funkcję czyszczącą dane
2. Funkcja przyjmuje jeden argument typu str
3. Funkcja zwraca oczyszczony tekst
4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert clean is not Ellipsis, \
'Write solution inside clean function'
>>> assert isfunction(clean), \
'Object clean must be a function'

>>> clean('ul.Mieszka II')
'Mieszka II'
>>> clean('UL. Zygmunta III WaZY')
'Zygmunta III Wazy'
>>> clean('  bolesława chrobrego ')
'Bolesława Chrobrego'
>>> clean('ul Jana III SobIESkiego')
'Jana III Sobieskiego'
>>> clean('\tul. Jana trzeciego Sobieskiego')
'Jana III Sobieskiego'
>>> clean('ulicaJana III Sobieskiego')
'Jana III Sobieskiego'
>>> clean('UL. JA    NA 3 SOBIES  KIEGO')
'Jana III Sobieskiego'
>>> clean('ULICA JANA III SOBIESKIEGO  ')
'Jana III Sobieskiego'
>>> clean('ULICA. JANA III SOBIeskieGO')
'Jana III Sobieskiego'
>>> clean(' Jana 3 Sobieskiego  ')
'Jana III Sobieskiego'
>>> clean('Jana III Sobi  eskiego ')
'Jana III Sobieskiego'

TODO: Translate input data to English
"""


"""
* Assignment: Function Arguments Num2Str
* Type: homework
* Complexity: medium
* Lines of code: 5 lines
* Time: 8 min

English:
1. Given is pilot's alphabet for numbers
2. Convert DATA: dict[int, str] to data: dict[str, str] (keys as str)
3. Define function pilot_say converting int or float
to text form in Pilot's Speak
4. You cannot change DATA, but you can modify data
5. Run doctests - all must succeed

Polish:
1. Dany jest alfabet pilotów dla numerów
2. Przekonwertuj DATA: dict[int, str] na data: dict[str, str]
(klucze jako str)
3. Zdefiniuj funkcję pilot_say konwertującą int lub float
na formę tekstową w mowie pilotów
4. Nie możesz zmieniać DATA, ale możesz modyfikować data
5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert pilot_say is not Ellipsis, \
'Write solution inside pilot_say function'
>>> assert isfunction(pilot_say), \
'Object pilot_say must be a function'

>>> pilot_say(1)
'one'
>>> pilot_say(+1)
'one'
>>> pilot_say(-1)
'minus one'
>>> pilot_say(1+1)
'two'
>>> pilot_say(1-1)
'zero'
>>> pilot_say(1969)
'one niner six niner'
>>> pilot_say(31337)
'tree one tree tree seven'
>>> pilot_say(13.37)
'one tree and tree seven'
>>> pilot_say(31.337)
'tree one and tree tree seven'
>>> pilot_say(-1969)
'minus one niner six niner'
>>> pilot_say(-31.337)
'minus tree one and tree tree seven'
>>> pilot_say(-49.35)
'minus fower niner and tree fife'
>>> pilot_say(1.0)
'one and zero'
>>> pilot_say(1.)
'one and zero'
>>> pilot_say(123.)
'one two tree and zero'
>>> pilot_say(123.0)
'one two tree and zero'
>>> pilot_say(.44)
'zero and fower fower'
>>> pilot_say(1-)
Traceback (most recent call last):
SyntaxError: invalid syntax
"""

DATA = {
0: 'zero',
1: 'one',
2: 'two',
3: 'tree',
4: 'fower',
5: 'fife',
6: 'six',
7: 'seven',
8: 'ait',
9: 'niner',
}


"""
* Assignment: Function Arguments Range
* Type: homework
* Complexity: medium
* Lines of code: 7 lines
* Time: 13 min

English:
1. Define function myrange with parameters: start, stop, step
2. Write own implementation of a built-in function
myrange(start, stop, step)
3. Do not use built-in range() function
4. Run doctests - all must succeed

Polish:
1. Zdefiniuj funkcję myrange z parametrami: start, stop, step
2. Zaimplementuj własne rozwiązanie wbudowanej funkcji
myrange(start, stop, step)
3. Nie używaj wbudowanej funkcji range()
4. Uruchom doctesty - wszystkie muszą się powieść

Hints:
* while

Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from inspect import isfunction

>>> assert myrange is not Ellipsis, \
'Write solution inside myrange function'
>>> assert isfunction(myrange), \
'Object myrange must be a function'

>>> myrange(0, 10, 2)
[0, 2, 4, 6, 8]

>>> myrange(0, 5)
[0, 1, 2, 3, 4]
"""