Search Header Logo
Python Global Variables

Python Global Variables

Assessment

Presentation

Computers

8th Grade

Practice Problem

Medium

Created by

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.

media

​Example

4

Fill in the Blanks

media image

Type answer...

5

Fill in the Blanks

media image

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.

media

​Example

8

Multiple Choice

Question image

What is the value GLOBAL variable?

1

awesome

2

fantastic

3

Python is

4

myfunc( )

9

Multiple Choice

Question image

What is the value LOCAL variable?

1

awesome

2

fantastic

3

Python is

4

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

media

12

Fill in the Blanks

media image

Type answer...

13

Fill in the Blanks

media image

Type answer...

14

Fill in the Blanks

media image

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

media

16

Fill in the Blanks

media image

Type answer...

17

Multiple Choice

Which of the following is...

#defining a local variable

1

def superfunc( )

2

x = fantastic

3

print("Python is" + x)

4

superfunc()

18

Multiple Choice

Which of the following is...

#defining a function

1

def superfunc( )

2

x = fantastic

3

print("Python is" + x)

4

superfunc()

19

Multiple Choice

Which of the following is...

#accessing a local variable

1

def superfunc( )

2

x = fantastic

3

print("Python is" + x)

4

superfunc()

20

Multiple Choice

Which of the following is...

#calling the function

1

def superfunc( )

2

x = fantastic

3

print("Python is" + x)

4

superfunc()

21

Fill in the Blanks

media image

Type answer...

Global Variables

Show answer

Auto Play

Slide 1 / 21

SLIDE