The use of variables, constants, operators, inputs, outputs and assignments
2.2.1 Programming Fundamentals
Practise exam questionsProgramming Fundamentals — Note 1 of 6
Practical use of the techniques
Variables and constants are fundamental building blocks of any program. A variable is a named storage location whose value can change during the program. A constant is a named value that does not change. Programs use inputs to receive data, outputs to display results, and assignment statements to store values in variables.
score = 0, radius = 0, area = 0.0.PI = 3.142 or const VAT = 0.2.= to store a value in a variable, e.g. name = "Alice". Do NOT use == for assignment.name = input("Enter your name"). Note: input cannot be used as a variable name.print("Hello World").input() ALWAYS returns a string. Wrap it in int() or float() before doing any maths with the value.print() with commas puts a space between each item: print("Hello", name) outputs Hello Sam.The act of creating a named location in memory to store a value. The value can change during program execution.
The act of creating a named memory location whose value cannot change during program execution.
radius, area, score, staffID, etc. Give the exact identifier from the code.word1.age = input("Age? ") followed by age + 1 causes an error, because age is a string, not a number.# Constant (does not change)
VAT_RATE = 0.2
# Input
price = float(input("Enter the price: "))
# Assignment and calculation
total = price + (price * VAT_RATE)
# Output
print("Total with VAT:", total)name = input("Enter your name: ")
print("Hello", name)
# input() returns a string, so cast it before doing maths
age = int(input("Enter your age: ")) # whole number
height = float(input("Enter your height in m: ")) # decimal
print("Next year you will be", age + 1)Ready to test yourself?
Practise 2.2.1 questions