Beginner Python Cheat Sheet - Functions (Python Crash Course) Flashcards

1
Q

How do you make an argument in an argument optional?

A

Use None.

E.g.
def describe_pet(animal, name=None):
 """Display information about a pet."""
 print("\nI have a " + animal + ".")
 if name:
 print("Its name is " + name + ".")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you pass a list to a function without having to “unpack” the list?

A

Yes

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

What’s an easy way to pass a list as a copy to a function?

A

Use brackets with a colon after the list to select all items
E.g.
print_models(original[:], printed)

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

How do you create a module for use in your code?

A

Create the function and store it in a script. Store that script in the same directory and then call it like a normal module. Use the same name as the name of the .py script file

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