- AQA
Global and local variables
I can explain the difference between a local and global variable.
- AQA
Global and local variables
I can explain the difference between a local and global variable.
Lesson details
Key learning points
- The scope of a variable determines where it can be accessed.
- A local variable is a variable that is only accessible within a specific part of a program.
- A global variable can be accessed anywhere in a program for the duration of the program's execution.
Keywords
Scope - the section of the program where the variable can be accessed and modified
Local variable - a variable that is defined and visible for use only in specific parts of a program, e.g. within a subroutine
Global variable - a variable that is accessible from all parts of a program
Common misconception
Assigning a variable inside a function automatically makes it global.
A variable is local, unless it is specifically set as a global variable.
Equipment
Pupils will need access to a device that can edit and run Python programs. Examples in this lesson use the RPF Code Editor https://oak.link/python-editor
Licence
Lesson video
Loading...
Prior knowledge starter quiz
6 Questions
Q1.A procedure executes a list of commands when called. A function will execute a list of commands and also a value.
Q2.What code would correctly call the function calculate?
123456def calculate(a, b):
total = a + b
print(f"{a} + {b} = {total}")
return total
num1 = 10
num2 = 15Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q3.What are the parameters in this program?
1234567def calculate(a, b):
total = a + b
print(f"{a} + {b} = {total}")
return total
num1 = 10
num2 = 15
calculate(num1, num2)Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q4.What Python keyword is used to share a value from a function to the rest of the program?
Q5.A can have one, several or no parameters.
Q6.What is the definition of values held in the brackets of a subroutine call that are passed into a subroutine via the parameters?
Assessment exit quiz
4 Questions
Q1.What value will be output when this program is executed?
123456def example():
number = 10
print(number)
number = 5
example()Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q2.What will be the output of this program?
12345def multiply(a, b):
answer = a * b
return answer
print(multiply(2, 4))Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q3.What will be the output of this program?
123456def divide(a, b):
answer = a / b
return answer
divide(8, 2)
print(answer)Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q4.What will be the output of this program?
123456def add(a, b):
answer = a + b
return answer
solution = add(8, 4)
print(solution)Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords