Functions Flashcards

(20 cards)

1
Q

What is a function ?

A

Functions are named blocks of code that are designed to do one specific job.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a simple function and explain each part .

A

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!”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is calling a function ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a parameter ?
What is an argument ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different ways to pass an argument ?

A

Positional arguments : Passing the arguments in same order the parameters were written.
Keyword arguments : each arguments consists of a variable name and a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a default value ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are arguments errors ?

A

Unmatched arguments occur when you provide fewer or more arguments than a function needs to do its work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What isn the return statement ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to make an argument optional ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is it’s possible to use functions with other python structures ?

A

Yes. With if statements , for, while loops etc…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give example of how to pass a list to a function.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to pass an arbitrary number of arguments ?

A

(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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to use arbitrary keyword arguments.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an import statement ?

A

An import statement tells Python to make the code in a module available in the currently running program file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is a module ?

A

A module is a file ending in .py that contains the code you want to import into your program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to import a module ?

A

import module_name

module_name.function_name(arguments)

import pizza

pizza.make_pizza(16, ‘pepperoni’)
pizza.make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)

17
Q

How to import specific functions.

A

from module_name import function_name

Example :

from pizza import make_pizza
make_pizza(16, ‘pepperoni’)
make_pizza(12, ‘mushrooms’, ‘green peppers’, ‘extra cheese’)

18
Q

What can i do if the function i am importing might conflict with nan existing name in my program ?

A

from module_name import function_name as fn

19
Q

Is is possible to do that for a module too ?

A

Yes.

import module_name as mn

20
Q

Is it possible to import all functions in a module ?

A

Yes.
from module_name import *