chapter3 Flashcards
1
Q
- Why are functions advantageous to have in your programs?
A
Functions reduce the need for duplicate code. This makes programs shorter,
easier to read, and easier to update.
2
Q
- When does the code in a function execute: when the function is defined or when the function is called?
A
The code in a function executes when the function is called, not when the function is defined.
3
Q
- What statement creates a function?
A
The def statement defines (that is, creates) a function.
4
Q
- What is the difference between a function and a function call?
A
A function consists of the def statement and the code in its def clause. A function call is what moves the program execution into the function, and the function call evaluates to the function’s return value.
5
Q
- How many global scopes are there in a Python program? How many local scopes?
A
There is one global scope, and a local scope is created whenever a function is
called.
6
Q
- What happens to variables in a local scope when the function call returns?
A
When a function returns, the local scope is destroyed, and all the variables in it
are forgotten.
7
Q
- What is a return value? Can a return value be part of an expression?
A
- A return value is the value that a function call — evaluates to—. Like any value, a
return value can be used as part of an expression.
8
Q
- If a function does not have a return statement, what is the return value of a call to that function?
A
- If there is no return statement for a function, its return value is None.
9
Q
- How can you force a variable in a function to refer to the global variable?
o co w tym chodzi ????
A
- A global statement will force a variable in a function to refer to the global
variable.
10
Q
- What is the data type of None?
A
- The data type of None is NoneType.
11
Q
- What does the import areallyourpetsnamederic statement do?
A
- That import statement imports a module named areallyourpetsnamederic.
CHAPTER 4
(This isn’t a real Python module, by the way.)
12
Q
- If you had a function named bacon() in a module named spam, how would you call it after importing spam?
A
- This function can be called with spam.bacon().
13
Q
- How can you prevent a program from crashing when it gets an error?
A
- Place the line of code that might cause an error in a try clause.
14
Q
- What goes in the try clause? What goes in the except clause?
A
- The code that could potentially cause an error goes in the try clause.
The code that executes if an error happens goes in the except clause.