24 total
By the end of this topic, you should be able to:
A variable is a named storage location in a computer's memory that holds a value. Think of it like a labeled box where you can store information. The important thing about variables is that the value inside them can change while your program is running.
Rules for naming variables:
MyAge or PlayerScore)How to declare a variable:
In pseudocode:
DECLARE <identifier> : <datatype>
Examples:
DECLARE Age : INTEGER
DECLARE Price : REAL
DECLARE GameOver : BOOLEAN
In Python:
score = int() # whole number
cost = float() # decimal number
light = bool() # True or False value
When you declare a variable, the computer sets aside memory space for it based on the data type you specify.
A constant is like a variable, but its value is set once and never changes during the program. Constants are useful for values that should stay the same throughout your program, like the value of pi (3.142) or a password.
Rules for naming constants:
PI or PASSWORD)Why use constants?
How to declare a constant:
In pseudocode:
CONSTANT <identifier> ← <value>
Examples:
CONSTANT PI ← 3.142
CONSTANT PASSWORD ← "letmein"
CONSTANT HIGHSCORE ← 9999
In Python:
PI = 3.142
VAT = 0.20
PASSWORD = "letmein"
Note: Python doesn't have a special way to declare constants, but programmers use uppercase names to show that a value shouldn't be changed.
Sign in to view full notes