This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.
Variables and constants let your program remember and use data (e.g. names or scores)
Both variables and constants have a name and a value.
The value is the actual data your program needs to remember (e.g. the number 3.141)
The name lets your program call that value something more helpful (e.g. PI)
The value of a variable can changed when the program runs.
The value of a constant is set once before the program runs, then can't be changed whilst the program runs.
Variables let your program react and respond to data rather than always doing exactly the same thing every time they run.
Constants make your programs easier to read and less prone to errors because the names are easier to understand than just the values.
high_score is a much better name than myvariable1 because it makes your code easier to understand for you and other programmers.
Calling a variable high score will cause an error message. Replace spaces with an underscore (snake_case) or make the next letter a capital letter (CamelCase)
snake_case is recommended for Python and CamelCase is recommended for VB.
These naming conventions (sets of rules for how to name your variables / constants) wont break your code if you don't follow them but they help you to be more consistent so your code is less likely to have errors.
Integers are whole numbers
Variables let you store data
Integer variables let you store whole numbers
In this example, a variable called score is declared then set to the integer value of 100
Reals are numbers that can contain a decimal place.
Reals are also known as floats or floating point numbers because they may have a decimal point floating somewhere inside them.
Variables let you store data.
A real variable lets you store a number that can contain a decimal place.
In this example, a variable called height is declared then set to the real value of 6.2
Boolean means one of two possible values: usually True or False.
Variables let you store data.
Boolean variables let you store the either value True or False, but nothing else
In this example, a variable called hungry is declared then set to True
Characters are single letters or digits such as '1', 'H' or '?'
Variables let you store data.
Character variables let you store a single letter, number or punctuation mark.
In this example, a variable called letter is declared then set to a full stop.
Integers are whole numbers (with no decimal places).
Constants store a value that is set once and then never changes.
Integer constants let you give a whole number a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.
In this example, a constant called NUMBER_OF_TRIES is set to the integer value of 10
Reals are numbers that can contain a decimal place.
Constants store a value that is set once and then never changes.
Real constants let you give a decimal number a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.
In this example, a constant called PI is set to the real value of 3.141
Boolean means one of two values (usually either True or False).
Constants store a value that is set once and then never changes.
Boolean constants let you give a boolean value a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.
In this example, a constant called CHEAT_MODE is set to the boolean value of True
Characters are single letters or digits such as '1', 'H' or '?'
Constants store a value that is set once and then never changes.
Character constants let you give a character value a name so you can use it to make your code easier to read or to set options for your code that the user wont be able to change.
In this example, a constant called KEY_QUIT is set to the character value of 'Q'
A string is some text such as "Hello" or "Area 51"
Variables let you store data.
String variables let you store text.
In this example a variable called name is set to the string value of "Bob"
A string is some text such as "Hello" or "Area 51"
Constants store a value that is set once and then never changes.
String constants let you give some text data a name so that you can use it to make your code easier to read or set options for your code that the user wont be able to change.
In this example, a constant called ERROR_MESSAGE is set to the string value of "Don't panic!"
Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.
This example converts the integer number 1000 to a string containing "1000"
Often it's useful to round a decimal number (real data type) to a whole number (integer data type).
It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer
This example converts the string "1000" to the integer 1000
Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)
This example converts the string "1.1234" to the real value 1.1234
Loading...