Skip to content

Commit d6637fb

Browse files
committed
update the scope lecture
1 parent 39c6789 commit d6637fb

1 file changed

Lines changed: 109 additions & 82 deletions

File tree

Lines changed: 109 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,149 @@
11
# Scope & Statements
22

3-
What happens in the function, stays in the function. What happens in the loops stays in the loop. What happens in the if statement stays in the if statement
3+
The **scope** of a variable or function **name** defines *where that can variable/function be accessed* in the code.
44

5-
## Functions and scopes
5+
The point of this concept is to deal with cases where there are name conflicts between
6+
function and variable names.
67

7-
The scope of a variable defines where it can be accessed in the code.
8+
Name conflicts between variable names and function names can happen all the time.
89

9-
A function's parameter, for example, is only available within that function and can't be used outside of it. This concept is illustrated in the code provided.
10+
For example, consider the code below:
1011

11-
```python
12-
def my_function():
13-
print("Do I know that variable?", var)
12+
```py
13+
x = 1 # a variable name x
14+
x = 0 # update the value of x
1415

16+
def function_1(x): # function parameter name x
17+
print(x) # what is the value of x?
18+
x = x**2
19+
return x
1520

16-
var = 1
17-
my_function()
18-
print(var)
19-
```
21+
def function_2(x): # x again!
22+
print(x) # what is the value of x?
23+
x = x**3
24+
return x
2025

21-
**output:**
26+
def function_3(y): # y, not x
27+
print(x) # is x defined?
28+
y = y**3
29+
return y
2230

23-
```
24-
Do I know that variable? 1
25-
1
31+
32+
print(x)
33+
print(function_1(2))
34+
print(x) # does x change after calling function_1?
35+
x = 1
36+
print(function_2(3))
37+
print(x) # what did x change to after the previous two lines of code?
38+
print(function_3(3))
39+
print(y) # is y defined?
2640
```
2741

28-
A variable existing outside a function has scope inside the function's body.
42+
What gets printed to the console? That is, what is the value stored by the variable called
43+
`x` at the moment the `print` statement is called? What about the variable `y`?
2944

30-
```python
31-
def my_function():
32-
var = 2
33-
print("Do I know that variable?", var)
45+
To know for certain, we have to know the **scope** where the variables `x` and `y` are defined.
3446

47+
## Concepts
3548

36-
var = 1
37-
my_function()
38-
print(var)
39-
```
49+
There are two main concepts to know:
4050

41-
**output:**
51+
1. **Global scope**: Variable and function names in *global scope* are available to *all* your code.
52+
1. **Local scope**: Variable and function names in the *local scope* are only available or visible to the code *within that scope*.
4253

43-
```python
44-
Do I know that variable? 2
45-
1
46-
```
54+
## Rules
4755

48-
✍️**Challenge: What is the output?**
56+
How do we know when a variable or function is accessible in global or local scope? Here
57+
are the rules:
4958

50-
```python
51-
value = 50
52-
def function1():
53-
value = 25
54-
print(value)
59+
1. By default, all variables and functions are created in *global scope*
60+
2. However, when a variable or function is created *inside a function*, that
61+
variable/function is *only accessible inside that function*
62+
3. That is, *all functions* create a *local scope*, where all variables or functions
63+
defined within the function body only exist within that local scope.
5564

56-
function1()
57-
print(value)
58-
```
65+
Function parameters are examples of variables created inside a function, therefore
66+
*function parameters are only accessible inside that function* and cannot be used outside
67+
of it.
5968

60-
## **Using Loops Inside Functions**
6169

62-
A `for` loop is often used inside functions to automate repetitive tasks. Here's a breakdown of how loops work with functions in Python.
70+
## Examples
6371

64-
**For Loop Syntax**:
72+
1. Can a variable created in global scope be accessed inside a function?
6573

66-
```python
67-
for variable in sequence:
68-
# code to execute in each iteration
69-
```
74+
```python
75+
def my_function():
76+
print("Do I know that variable?", var)
7077

71-
The loop iterates over a sequence, performing the same action for each item.
78+
var = 1
79+
my_function()
80+
print(var)
81+
```
7282

73-
#### **Example 1: Simple Function with a For Loop**
83+
**output:**
7484

75-
This function prints the numbers from 1 to 5 using a `for` loop:
85+
```
86+
Do I know that variable? 1
87+
1
88+
```
7689

77-
```python
78-
def print_numbers():
79-
for i in range(1, 6):
80-
print(i)
90+
A variable existing outside a function is in **global scope**, so it can be accessed inside the function's body.
91+
92+
Careful though: the variable has to be *defined already* in order for it to be in
93+
**global scope**:
8194

82-
# Call the function
83-
print_numbers()
84-
```
95+
```python
96+
def my_function():
97+
print("Do I know that variable?", var)
8598

86-
**Explanation**:
87-
The `for i in range(1, 6)` loop iterates over the numbers from 1 to 5, printing each value.
99+
my_function()
100+
var = 1
88101

89-
#### **Example 2: Sum of All Integers from 1 to `n`**
102+
print(var)
103+
```
90104

91-
This function calculates the sum of all integers from 1 to a given number `n`.
105+
**output:**
92106

93-
```python
94-
def sum_numbers(n):
95-
total = 0
96-
for i in range(1, n+1):
97-
total += i
98-
return total
107+
```
108+
error (var not defined)
109+
```
99110

100-
# Example usage:
101-
print(sum_numbers(5)) # Output: 15 (1+2+3+4+5)
102-
```
111+
2. Can a variable created in a local scope be accessed outside the function it was defined
112+
in?
103113

104-
**Explanation**:
105-
The function uses a loop to sum up all the integers from 1 to `n`. When called with `n = 5`, it returns 15.
114+
```python
115+
def my_function():
116+
var = 2
117+
print("Do I know that variable?", var)
106118

107-
#### Example 3: Sum of Squares
119+
var = 1
120+
my_function()
121+
print(var)
122+
```
108123

109-
```python
124+
**output:**
110125

111-
def sum_of_squares(n):
112-
total = 0
113-
for i in range(1, n + 1):
114-
total += i ** 2
115-
return total
126+
```python
127+
Do I know that variable? 2
128+
1
129+
```
116130

117-
# Test the function
118-
print(sum_of_squares(4)) # Output: 30 (1^2 + 2^2 + 3^2 + 4^2)
119-
```
131+
Note that `var` is defined in **global scope** AND in the **local scope** of
132+
`my_function` -- however, they do not store the same value since the scope is
133+
different.
134+
135+
136+
3. ✍️**Challenge: What is the output?**
137+
138+
```python
139+
value = 50
140+
def function1():
141+
value = 25
142+
print(value)
143+
144+
function1()
145+
print(value)
146+
```
120147

121-
**Explanation**:
122-
The function loops through numbers from 1 to `n` and adds the square of each number to the total. For `n = 4`, the result is 30.
148+
Once you have done all three of these problems, return to the problem at the beginning of
149+
these notes -- can you get the output for all of the print statements?

0 commit comments

Comments
 (0)