## Functions continued:

• Annotating Functions with Types
``````def is_prime(number: int) -> bool:
pass

``````
• Documenting the functions with docstrings
``````def is_prime(number: int) -> bool:
'''This function will find if the number is prime or not

Args:
number (int): The number to be checked

Returns:
True if prime False otherwise
'''
pass
``````
• Default arguments

• Python allows us to write default arguments for functions
• *args and **kwargs in python:

• In addition to normal arguments in python we can pass two special symbols for arguments
• Non Keyword arguments (*args)
• Keyword arguments (**kwargs)
• Non Keyword arguments
``````  def sum(*args):
print(type(args))
result = 0
for arg in args:
result += arg
return result

result = sum(1,2)
print(result)
result = sum(1,2,3,4,5,6,7,8,9)
print(result)
result = sum()
print(result)
``````
• Keyword arguments
``````def explain_kwargs(**kwargs):
print(type(kwargs))
for key, value in kwargs.items():
print(f"{key} = {value}")

explain_kwargs(name='IHub', course='Python', duration='3')
``````
• Combination of keyword and non keyword arguments
``````def fun_function(*args, **kwargs):
print(f"args are {args}")
print(f"keyword args are {kwargs}")

``````
• When we have arguments of different types in one python function the they have to follow the following order

• arguments
• default arguments
• Non key word arguments
• Keyword arguments
``````def rule_function(number, debug=True, *args, **kwargs):
pass
``````

## Exceptions

• If an error occurs in your program an exception is raised and traceback message will be shown
``````def is_even(number):
return number % 2 == 0

is_even('Hello')
``````

• Now lets Try to learn an approach to handle errors
``````def is_even(number):
return number % 2 == 0

try:
is_even('Hello')
except Exception as e:
print("Enter the arguments correctly")
finally:
print('Executed the program successfully')

``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.