
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
Multiplying Integers
Presentation
•
8th Grade
16 questions
Intro to the Solar System
Presentation
•
8th Grade
16 questions
Rational and Irrational Number Review
Presentation
•
8th Grade
16 questions
Mood and Tone
Presentation
•
8th Grade
16 questions
Pixar Animation
Presentation
•
7th - 8th Grade
19 questions
computational thinking
Presentation
•
8th Grade
16 questions
Washington Leads a New Nation
Presentation
•
8th Grade
16 questions
Newton's 3rd Law
Presentation
•
8th Grade
Popular Resources on Wayground
24 questions
PBIS-HGMS Day 10
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Review 3
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
15 questions
HCS SCI 05 Summer School Assessment 3 Review
Quiz
•
5th Grade
35 questions
Lufkin Road Middle School Student Handbook & Policies Assessment
Quiz
•
7th Grade
18 questions
Geo 11.3 Area of Circles and Sectors
Quiz
•
9th - 11th Grade