
Python Global Variables
Presentation
•
Computers
•
8th Grade
•
Practice Problem
•
Medium
Mark Christensen
Used 5+ times
FREE Resource
8 Slides • 13 Questions
1
Global Variables
2
Global Variables
Variables that are created outside of a function are known as global variables.
Global variables can be used by everyone, both inside of functions and outside.
Subject | Subject
3
Create a variable outside of a function, and use it inside the function.
Notice how the variable is created outside of the function, but it is being used inside of the function.
Example
4
Fill in the Blanks
Type answer...
5
Fill in the Blanks
Type answer...
6
Global vs Local
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
Subject | Subject
7
Create a variable inside a function, with the same name as the global variable
Notice how the variable inside of the function has a different value than the Global variable.
Example
8
Multiple Choice
What is the value GLOBAL variable?
awesome
fantastic
Python is
myfunc( )
9
Multiple Choice
What is the value LOCAL variable?
awesome
fantastic
Python is
myfunc( )
10
The global Keyword
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.
To create a global variable inside a function, you can use the global keyword.
Subject | Subject
11
If you use the global keyword, the variable belongs to the global scope.
If the variable is inside of the function and if the variable isn't declared as being global, it is considered local.
Example
12
Fill in the Blanks
Type answer...
13
Fill in the Blanks
Type answer...
14
Fill in the Blanks
Type answer...
15
When you try to modify the global variable value inside a function, it will throw UnboundLocalError, because while modifying Python treats points as a local variable, but points is also not defined inside the function myfunc( ). It will need to be declared as a global variable to work properly.
Example
16
Fill in the Blanks
Type answer...
17
Multiple Choice
Which of the following is...
#defining a local variable
def superfunc( )
x = fantastic
print("Python is" + x)
superfunc()
18
Multiple Choice
Which of the following is...
#defining a function
def superfunc( )
x = fantastic
print("Python is" + x)
superfunc()
19
Multiple Choice
Which of the following is...
#accessing a local variable
def superfunc( )
x = fantastic
print("Python is" + x)
superfunc()
20
Multiple Choice
Which of the following is...
#calling the function
def superfunc( )
x = fantastic
print("Python is" + x)
superfunc()
21
Fill in the Blanks
Type answer...
Global Variables
Show answer
Auto Play
Slide 1 / 21
SLIDE
Similar Resources on Wayground
16 questions
Metric Conversions Notes
Presentation
•
8th Grade
16 questions
10/26 Causes of American Revolution
Presentation
•
8th Grade
16 questions
The Industrial Age & Immigration
Presentation
•
8th Grade
16 questions
Types of Slope
Presentation
•
8th Grade
16 questions
Biomes
Presentation
•
8th Grade
16 questions
Linear Relationships
Presentation
•
8th Grade
16 questions
Dramatic Irony Mini Lesson
Presentation
•
8th Grade
16 questions
Graphs
Presentation
•
8th Grade
Popular Resources on Wayground
20 questions
Math Review
Quiz
•
3rd Grade
15 questions
Fast food
Quiz
•
7th Grade
20 questions
Context Clues
Quiz
•
6th Grade
20 questions
Inferences
Quiz
•
4th Grade
19 questions
Classifying Quadrilaterals
Quiz
•
3rd Grade
20 questions
Figurative Language Review
Quiz
•
6th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
10 questions
Identify Fractions, Mixed Numbers & Improper Fractions
Quiz
•
3rd - 4th Grade
Discover more resources for Computers
17 questions
guess the logo
Quiz
•
8th Grade
10 questions
Candy
Quiz
•
4th - 8th Grade
10 questions
Fact Check Ice Breaker: Two truths and a lie
Quiz
•
5th - 12th Grade
10 questions
Memorial Day History and Traditions
Interactive video
•
5th - 8th Grade
20 questions
summer trivia
Quiz
•
8th Grade
15 questions
Middle School Fun Trivia Questions
Quiz
•
7th - 8th Grade
7 questions
8th U8 T2 Quiz Review
Quiz
•
8th Grade
10 questions
Video Games
Quiz
•
6th - 12th Grade