What is a function ?
Functions are named blocks of code that are designed to do one specific job.
Write a simple function and explain each part .
def greet_user():
“"”Display a simple greeting.”””
print(“Hello!”)
greet_user()
The first line uses the keyword def to inform Python that you’re defining a function. This
is the function definition, which tells Python the name of the function and, if
applicable, what kind of information the function needs to do its job. The parentheses hold that information. Finally, the definition ends in a colon.
Any indented lines that follow def greet_user(): make up the body of the function. The text at line 2 is a comment called a docstring, which describes what the function does. Docstrings are enclosed in triple quotes which Python looks for when it generates documentation for the functions in your programs.
The line print(“Hello!”) is the only line of actual code in the body of this function, so greet_user() has just one job: print(“Hello!”).
What is calling a function ?
When you want to use a function, you call it. A function call tells Python to execute the code in the function. To call a function, you write the name of the function, followed by any necessary information in parentheses.
What is a parameter ?
What is an argument ?
A parameter is a piece of information the function need to do its job.
An argument is a piece of information that is passed from a function call to a function.
What are the different ways to pass an argument ?
Positional arguments : Passing the arguments in same order the parameters were written.
Keyword arguments : each arguments consists of a variable name and a value.
What is a default value ?
When writing a function, you can define a default value for each parameter. If an argument for a parameter is provided in the function call, Python uses the argument value. If not, it uses the parameter’s default value. So when you define a default value for a parameter, you can exclude the corresponding argument you’d usually write in the function call.
What are arguments errors ?
Unmatched arguments occur when you provide fewer or more arguments than a function needs to do its work.
What isn the return statement ?
The return statement takes a value from inside a function and sends it back to the line that called the function.
Since it’s taking a value, that value has to be stored in a variable before being displayed .
How to make an argument optional ?
To make an argument optional, we can give the argument an empty default value and ignore the argument unless the user provides a value. Which means that you will need to use an if-else statement.
Is it’s possible to use functions with other python structures ?
Yes. With if statements , for, while loops etc…
Give example of how to pass a list to a function.
def greet_users(names):
“"”Print a simple greeting to each user in the list.”””
for name in names:
msg = “Hello, “ + name.title() + “!”
print(msg)
usernames = [‘hannah’, ‘ty’, ‘margot’]
greet_users(usernames)
How to pass an arbitrary number of arguments ?
(name) the * tell python to make a tuple of that name.
def make_pizza(toppings):
“"”Print the list of toppings that have been requested.”””
print(toppings)
make_pizza(‘pepperoni’)
make_pizza(‘mushrooms’, ‘green peppers’, ‘extra cheese’)
How to use arbitrary keyword arguments.
def build_profile(first, last, **user_info):
“"”Build a dictionary containing everything we know about a user.”””
profile = {}
profile[‘first_name’] = first
profile[‘last_name’] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile(‘albert’, ‘einstein’, location=’princeton’,field=’physics’)
print(user_profile)
What is an import statement ?
An import statement tells Python to make the code in a module available in the currently running program file.
what is a module ?
A module is a file ending in .py that contains the code you want to import into your program.
How to import a module ?
import module_name
module_name.function_name(arguments)
import pizza
pizza.make_pizza(16, ‘pepperoni’)
pizza.make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)
How to import specific functions.
from module_name import function_name
Example :
from pizza import make_pizza
make_pizza(16, ‘pepperoni’)
make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)
What can i do if the function i am importing might conflict with nan existing name in my program ?
from module_name import function_name as fn
Is is possible to do that for a module too ?
Yes.
import module_name as mn
Is it possible to import all functions in a module ?
Yes.
from module_name import *