-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch_3_practice.txt
72 lines (28 loc) · 1.95 KB
/
ch_3_practice.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
## 1. Why are functions advantageous to have in your programs?
Functions compartmentalize code, reduce duplication, and allow variables to be defined in separate scopes. This makes code easier to read and maintain.
## 2. When does the code in a function execute: when the function is defined or when the function is called?
When the function is called.
## 3. What statement creates a function?
def
## 4. What is the difference between a function and a function call?
A function defines the code that eventually gets called. A function call actually executes the function code and returns the return value.
## 5. How many global scopes are there in a Python program? How many local scopes?
There is one global scope in a Python program. There is a separate local scope for each function call.
## 6. What happens to variables in a local scope when the function call returns?
The local variables are destroyed.
## 7. What is a return value? Can a return value be part of an expression?
A return value is the value that gets returned from a function call. Yes, a return value can be part of an expression.
## 8. If a function does not have a return statement, what is the return value of a call to that function?
None
## 9. How can you force a variable in a function to refer to the global variable?
You use the global statement at the beginnning of the function.
## 10. What is the data type of None?
NoneType
## 11. What does the import areallyourpetsnamederic statement do?
Loads the module called areallyourpetsnamederic.
## 12. If you had a function named bacon() in a module named spam, how would you call it after importing spam?
spam.bacon()
## 13. How can you prevent a program from crashing when it gets an error?
By using a try,except statement.
## 14. What goes in the try clause? What goes in the except clause?
The code that may generate an error goes in the try clause. The code that deals with the error(s) should go in the except clause.