Computer Science
Grade 9
20 min
Creating Variables
Creating Variables
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Differentiate between local and global variable scope.
Correctly apply type casting to convert variables between strings, integers, and floats.
Define and use constants according to programming conventions.
Utilize multiple assignment to initialize several variables in a single line of code.
Apply standard naming conventions (like snake_case) for variable clarity and readability.
Explain how variable scope can prevent or cause bugs in a program.
Ever wonder how a game remembers your high score even after you finish a level, but forgets your temporary power-up? 🤔 It's all about where and how variables are created!
In this lesson, we'll move beyond basic variable creation. You'll learn advanced techniques like controlling a variable's...
2
Key Concepts & Vocabulary
TermDefinitionExample
Variable ScopeThe region of a program where a variable can be accessed. A variable created inside a function is 'local' to that function, while a variable created outside all functions is 'global'.In `def game_level(): score = 100`, the `score` variable is local and only exists inside the `game_level` function.
Global VariableA variable declared in the main body of a program, outside of all functions. It can be accessed from anywhere in the code, including inside functions.`HIGH_SCORE = 9500` declared at the top of a file is a global variable.
Local VariableA variable declared inside a function. It is only accessible from within that function and is destroyed when the function finishes executing.In `def calculate_tax(price): tax_amount = price * 0...
3
Core Syntax & Patterns
Type Casting Syntax
`new_variable = new_type(old_variable)`
To change a variable's type, use the target type's function (e.g., `int()`, `str()`, `float()`) and pass the variable you want to convert as an argument.
Constant Naming Convention
`CONSTANT_NAME = value`
In Python, constants are not enforced by the language. We use all uppercase letters with underscores to signal to other developers that this variable's value is intended to remain fixed.
Accessing Global Variables
To modify a global variable from inside a function, you must use the `global` keyword: `global variable_name`.
If you just read a global variable inside a function, you don't need the keyword. But if you want to change its value, you must first declare your intent with `global`...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A program has a bug where a global `config_mode` variable is set to 'expert', but a function `start_level(config_mode)` is called, which then sets its parameter `config_mode` to 'easy'. This is causing the game to run in easy mode. What is the most likely cause of this bug?
A.Variable shadowing: The function parameter `config_mode` is a local variable that hides the global one.
B.The `global` keyword was used incorrectly, causing the global variable to be overwritten.
C.Type mismatch: The string 'expert' cannot be changed to 'easy'.
D.Multiple assignment error: The function can only accept one parameter.
Challenging
What is the final output of this code?
`value = 1
def func_one():
global value
value = value + 3
def func_two():
value = 10
print(f"Inside func_two: {value}")
func_one()
func_two()
print(f"Global: {value}")`
A.Inside func_two: 10
Global: 10
B.Inside func_two: 10
Global: 4
C.Inside func_two: 4
Global: 4
D.The code will produce an error.
Challenging
You are designing a game. You need variables for:
1. The gravitational force of the planet (e.g., 9.8), which never changes.
2. The player's total score, which needs to be saved across all levels.
3. The number of enemies in the current room, which is reset for each room.
What is the best way to define these variables?
A.1: Global, 2: Local, 3: Constant
B.1: Local, 2: Constant, 3: Global
C.1: Global, 2: Global, 3: Local
D.1: Constant, 2: Global, 3: Local
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free