|
1 | 1 | # Scope & Statements |
2 | 2 |
|
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. |
4 | 4 |
|
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. |
6 | 7 |
|
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. |
8 | 9 |
|
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: |
10 | 11 |
|
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 |
14 | 15 |
|
| 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 |
15 | 20 |
|
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 |
20 | 25 |
|
21 | | -**output:** |
| 26 | +def function_3(y): # y, not x |
| 27 | + print(x) # is x defined? |
| 28 | + y = y**3 |
| 29 | + return y |
22 | 30 |
|
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? |
26 | 40 | ``` |
27 | 41 |
|
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`? |
29 | 44 |
|
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. |
34 | 46 |
|
| 47 | +## Concepts |
35 | 48 |
|
36 | | -var = 1 |
37 | | -my_function() |
38 | | -print(var) |
39 | | -``` |
| 49 | +There are two main concepts to know: |
40 | 50 |
|
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*. |
42 | 53 |
|
43 | | -```python |
44 | | -Do I know that variable? 2 |
45 | | -1 |
46 | | -``` |
| 54 | +## Rules |
47 | 55 |
|
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: |
49 | 58 |
|
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. |
55 | 64 |
|
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. |
59 | 68 |
|
60 | | -## **Using Loops Inside Functions** |
61 | 69 |
|
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 |
63 | 71 |
|
64 | | -**For Loop Syntax**: |
| 72 | +1. Can a variable created in global scope be accessed inside a function? |
65 | 73 |
|
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) |
70 | 77 |
|
71 | | -The loop iterates over a sequence, performing the same action for each item. |
| 78 | + var = 1 |
| 79 | + my_function() |
| 80 | + print(var) |
| 81 | + ``` |
72 | 82 |
|
73 | | -#### **Example 1: Simple Function with a For Loop** |
| 83 | + **output:** |
74 | 84 |
|
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 | + ``` |
76 | 89 |
|
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**: |
81 | 94 |
|
82 | | -# Call the function |
83 | | -print_numbers() |
84 | | -``` |
| 95 | + ```python |
| 96 | + def my_function(): |
| 97 | + print("Do I know that variable?", var) |
85 | 98 |
|
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 |
88 | 101 |
|
89 | | -#### **Example 2: Sum of All Integers from 1 to `n`** |
| 102 | + print(var) |
| 103 | + ``` |
90 | 104 |
|
91 | | -This function calculates the sum of all integers from 1 to a given number `n`. |
| 105 | + **output:** |
92 | 106 |
|
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 | + ``` |
99 | 110 |
|
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? |
103 | 113 |
|
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) |
106 | 118 |
|
107 | | -#### Example 3: Sum of Squares |
| 119 | + var = 1 |
| 120 | + my_function() |
| 121 | + print(var) |
| 122 | + ``` |
108 | 123 |
|
109 | | -```python |
| 124 | + **output:** |
110 | 125 |
|
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 | + ``` |
116 | 130 |
|
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 | + ``` |
120 | 147 |
|
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