logo

AQA A-Level Computer Science practice

Explain the difference between local and global variables. Use the following Python code in your answer and state what happens when the final line is reached. def calculate(): result = 42 print(result) calculate() print(result)

Try the question first, then check the answer and practise the same topic again.

At a glance

Question

Type

practice

Style

Topic

Programming

Exam-style question

Try this first

Explain the difference between local and global variables. Use the following Python code in your answer and state what happens when the final line is reached. def calculate(): result = 42 print(result) calculate() print(result).

Answer and explanation

Concise answer

result is a local variable because it is defined inside calculate.

Why this is correct

This example demonstrates the restricted scope of a local variable. Defining result inside calculate does not make it available to statements outside calculate. A global variable has a wider scope because it is defined outside the local function area.

Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions