-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment_22.py
More file actions
126 lines (102 loc) · 3.69 KB
/
Copy pathAssignment_22.py
File metadata and controls
126 lines (102 loc) · 3.69 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
1. What is the result of the code, and explain?
```python
>>> X = 'iNeuron'
>>> def func():
print(X)
>>> func()
```
Output: `iNeuron`
Explanation:
- The variable `X` is defined in the global scope and has the value `'iNeuron'`.
- The function `func()` simply prints the value of `X`, which is the global variable.
- When `func()` is called, it prints the value of the global variable `X`, which is `'iNeuron'`.
2. What is the result of the code, and explain?
```python
>>> X = 'iNeuron'
>>> def func():
X = 'NI!'
>>> func()
>>> print(X)
```
Output: `iNeuron`
Explanation:
- The variable `X` is defined in the global scope and has the value `'iNeuron'`.
- The function `func()` defines a local variable `X` and assigns it the value `'NI!'`.
- When `func()` is called, it modifies the local `X` variable, but it does not affect the global `X` variable.
- After `func()` is called, the `print(X)` statement still prints the global `X` variable, which is `'iNeuron'`.
3. What does this code print, and why?
```python
>>> X = 'iNeuron'
>>> def func():
X = 'NI'
print(X)
>>> func()
>>> print(X)
```
Output:
```
NI
iNeuron
```
Explanation:
- The variable `X` is defined in the global scope and has the value `'iNeuron'`.
- The function `func()` defines a local variable `X` and assigns it the value `'NI'`. It then prints the value of this local `X`.
- When `func()` is called, it prints the value of the local `X`, which is `'NI'`.
- After `func()` is called, the `print(X)` statement prints the value of the global `X`, which is `'iNeuron'`.
4. What output does this code produce? Why?
```python
>>> X = 'iNeuron'
>>> def func():
global X
X = 'NI'
>>> func()
>>> print(X)
```
Output: `NI`
Explanation:
- The variable `X` is defined in the global scope and has the value `'iNeuron'`.
- The function `func()` uses the `global` keyword to access the global `X` variable. It then assigns the value `'NI'` to the global `X`.
- When `func()` is called, it modifies the global `X` variable.
- After `func()` is called, the `print(X)` statement prints the new value of the global `X`, which is `'NI'`.
5. What about this code---what's the output, and why?
```python
>>> X = 'iNeuron'
>>> def func():
X = 'NI'
def nested():
print(X)
nested()
>>> func()
>>> X
```
Output:
```
NI
'iNeuron'
```
Explanation:
- The variable `X` is defined in the global scope and has the value `'iNeuron'`.
- The function `func()` defines a local variable `X` and assigns it the value `'NI'`. It then defines a nested function `nested()`, which prints the value of `X`.
- When `func()` is called, it executes the `nested()` function, which prints the value of the local `X`, which is `'NI'`.
- After `func()` is called, the expression `X` evaluates to the global `X`, which is still `'iNeuron'`.
6. How about this code: what is its output in Python 3, and explain?
```python
>>> def func():
X = 'NI'
def nested():
nonlocal X
X = 'Spam'
nested()
print(X)
>>> func()
```
Output:
```
Spam
```
Explanation:
- The function `func()` defines a local variable `X` and assigns it the value `'NI'`. It then defines a nested function `nested()`.
- Inside `nested()`, the `nonlocal` keyword is used to access the local `X` variable from the enclosing `func()` scope.
- The nested function `nested()` then assigns the value `'Spam'` to the local `X` variable from the `func()` scope.
- When `func()` is called, it executes the `nested()` function, which modifies the local `X` variable from the `func()` scope.
- Finally, `print(X)` inside `func()` prints the updated value of `X`, which is `'Spam'`.