"Scope is defined as when and where a variable is accessible in a program. There are different types of scope - different ways a variable can be accessed in a program. This document will only focus on two types of scope, Global scope and Local scope."
]
},
{
"cell_type": "markdown",
"id": "58ec20e8",
"metadata": {},
"source": [
"## Self Assessment\n",
"\n",
"Questions that test for the learning goals and allows students to evaluate if they truly understand the topics."
]
},
{
"cell_type": "markdown",
"id": "4af35ce7",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 1:</span>** Which of the scope types is accessible to all of the program?\n",
"\n",
"1. Local Scope\n",
"2. Global Scope"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "15f43828",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"2\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "3921c156",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 2:</span>** Where would you not see a local scope defined in a program?\n",
"\n",
"1. In a function\n",
"2. In a class\n",
"3. In a loop\n",
"4. Beginning of a program"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c3dfd97d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"4\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "77bd1740",
"metadata": {},
"source": [
"**Use the code below to answer questions 3-5.** <br/>\n",
"**Hint:** The print statement is written as the following:\n",
"``` bash \n",
"std::cout<< YourTextHere <<std::endl; \n",
"```\n",
""
]
},
{
"cell_type": "markdown",
"id": "12d2601a",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 3:</span>** What is printed at line 8?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "65583bcc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"10\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "93d6fe2f",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 4:</span>** What is printed at line 15?"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e5e975d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"4\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "87a7ef55",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 5:</span>** If we were to print out **c** after the last yellow curly bracket, what would be its value?"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "53254c28",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"10\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "5024c8b7",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 6:</span>** What will be printed below?"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1d37b9f3",
"metadata": {},
"outputs": [],
"source": [
"x = 300\n",
"def func():\n",
" x = 200\n",
" print(x)\n",
"\n",
"\n",
"# print(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6c5c196b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"300"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"300\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "0456a3c4",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 7:</span>** What will be printed below?"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f0e44800",
"metadata": {},
"outputs": [],
"source": [
"x = 300\n",
"def func():\n",
" x = 200\n",
" print(x)\n",
"\n",
"# func()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "51029b03",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"200"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"##ANSWER##\n",
"200\n",
"##ANSWER##"
]
},
{
"cell_type": "markdown",
"id": "6b48b0d0",
"metadata": {},
"source": [
"✅ **<span style=\"color:red\">Question 8:</span>** How do you identify the scope of a variable in Python (As opposed to C++ syntax)?\n",
"✅ **<span style=\"color:red\">Read the following website:</span>** https://www.educative.io/answers/what-is-scope <br>\n",
"**Note:** The website talks about where the scope is but not how to necessarily identify it in the code. When there are \"curly brackets\" or { }, the variables defined within this are considered **local** - in the case seen on the website, the following variable is local:\n",
"``` bash \n",
" string name = Bob\n",
"```\n",
"<br> \n",
"The video below will go more in depth about this, but this link will help first introduce you to Global and Local Scope."
]
},
{
"cell_type": "markdown",
"id": "b44650dd",
"metadata": {},
"source": [
"#### Introduction to Global and Local Scope in C++"
"✅ **<span style=\"color:red\">Read the following website:</span>** https://www.w3schools.com/python/python_scope.asp <br>"
]
},
{
"cell_type": "markdown",
"id": "44b461a0",
"metadata": {},
"source": [
"---\n",
"\n",
"Written by Jessica Parks, Michigan State University \n",
"As part of the Data Science Bridge Project \n",
" \n",
"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by-nc/4.0/88x31.png\" /></a><br />This work is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\">Creative Commons Attribution-NonCommercial 4.0 International License</a>."
Scope is defined as when and where a variable is accessible in a program. There are different types of scope - different ways a variable can be accessed in a program. This document will only focus on two types of scope, Global scope and Local scope.
%% Cell type:markdown id:58ec20e8 tags:
## Self Assessment
Questions that test for the learning goals and allows students to evaluate if they truly understand the topics.
%% Cell type:markdown id:4af35ce7 tags:
✅**<span style="color:red">Question 1:</span>** Which of the scope types is accessible to all of the program?
1. Local Scope
2. Global Scope
%% Cell type:code id:15f43828 tags:
``` python
##ANSWER##
2
##ANSWER##
```
%% Output
2
%% Cell type:markdown id:3921c156 tags:
✅**<span style="color:red">Question 2:</span>** Where would you not see a local scope defined in a program?
1. In a function
2. In a class
3. In a loop
4. Beginning of a program
%% Cell type:code id:c3dfd97d tags:
``` python
##ANSWER##
4
##ANSWER##
```
%% Output
4
%% Cell type:markdown id:77bd1740 tags:
**Use the code below to answer questions 3-5.**<br/>
**Hint:** The print statement is written as the following:
``` bash
std::cout<<YourTextHere <<std::endl;
```

%% Cell type:markdown id:12d2601a tags:
✅**<span style="color:red">Question 3:</span>** What is printed at line 8?
%% Cell type:code id:65583bcc tags:
``` python
##ANSWER##
10
##ANSWER##
```
%% Output
10
%% Cell type:markdown id:93d6fe2f tags:
✅**<span style="color:red">Question 4:</span>** What is printed at line 15?
%% Cell type:code id:e5e975d1 tags:
``` python
##ANSWER##
4
##ANSWER##
```
%% Output
4
%% Cell type:markdown id:87a7ef55 tags:
✅**<span style="color:red">Question 5:</span>** If we were to print out **c** after the last yellow curly bracket, what would be its value?
%% Cell type:code id:53254c28 tags:
``` python
##ANSWER##
10
##ANSWER##
```
%% Output
10
%% Cell type:markdown id:5024c8b7 tags:
✅**<span style="color:red">Question 6:</span>** What will be printed below?
%% Cell type:code id:1d37b9f3 tags:
``` python
x=300
deffunc():
x=200
print(x)
# print(x)
```
%% Cell type:code id:6c5c196b tags:
``` python
##ANSWER##
300
##ANSWER##
```
%% Output
300
%% Cell type:markdown id:0456a3c4 tags:
✅**<span style="color:red">Question 7:</span>** What will be printed below?
%% Cell type:code id:f0e44800 tags:
``` python
x=300
deffunc():
x=200
print(x)
# func()
```
%% Cell type:code id:51029b03 tags:
``` python
##ANSWER##
200
##ANSWER##
```
%% Output
200
%% Cell type:markdown id:6b48b0d0 tags:
✅**<span style="color:red">Question 8:</span>** How do you identify the scope of a variable in Python (As opposed to C++ syntax)?
✅**<span style="color:red">Read the following website:</span>** https://www.educative.io/answers/what-is-scope <br>
**Note:** The website talks about where the scope is but not how to necessarily identify it in the code. When there are "curly brackets" or { }, the variables defined within this are considered **local** - in the case seen on the website, the following variable is local:
``` bash
string name = Bob
```
<br>
The video below will go more in depth about this, but this link will help first introduce you to Global and Local Scope.
%% Cell type:markdown id:b44650dd tags:
#### Introduction to Global and Local Scope in C++
<IPython.lib.display.YouTubeVideo at 0x7fd334290d60>
%% Cell type:markdown id:e8ae5f06 tags:
#### Scope in Python
%% Cell type:markdown id:7e9a0e12 tags:
✅**<span style="color:red">Read the following website:</span>** https://www.w3schools.com/python/python_scope.asp <br>
%% Cell type:markdown id:44b461a0 tags:
---
Written by Jessica Parks, Michigan State University
As part of the Data Science Bridge Project
<arel="license"href="http://creativecommons.org/licenses/by-nc/4.0/"><imgalt="Creative Commons License"style="border-width:0"src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png"/></a><br/>This work is licensed under a <arel="license"href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a>.