Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

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

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 difference between a constant and a variable

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.

Advantages of using variables

Variables let your program react and respond to data rather than always doing exactly the same thing every time they run.

Advantages of using constants

Constants make your programs easier to read and less prone to errors because the names are easier to understand than just the values.

Top tips

  • Make sure your choose sensible names for your variables that describe the data that they store.
    high_score is a much better name than myvariable1 because it makes your code easier to understand for you and other programmers.
  • Variable and constant names can't contain spaces.
    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)
  • Be consistent with the naming convention that you use (e.g. always use CamelCase or snake_case, don't mix and match)
    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.
Skill
Description
VB.NET
Python
C#
Integer variables
   

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

# create a new variable called score score = 100
// create a new variable called score int score = 100;
Real variables
   

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

# create a new variable called height height = 6.2
// create a new variable called height double height = 6.2;
Boolean variables
   

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

# create a new variable called hungry hungry = True
// create a new variable called hungry bool hungry = true;
Character variables
   

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.

# create a new variable called letter letter = '.'
// create a new variable called letter char letter= '.';
Integer constants
   

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

# Create a new constant called NUMBER_OF_TRIES NUMBER_OF_TRIES = 10
// Create a new constant called NUMBER_OF_TRIES const int NUMBER_OF_TRIES = 10;
Real constants
   

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

 

# Create a new constant called PI PI = 3.141
// Create a new constant called PI const double PI = 3.141;
Boolean constants
   

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

# Create a new constant called CHEAT_MODE CHEAT_MODE = True
// Create a new constant called CHEAT_MODE const bool CHEAT_MODE = true;
Character constants
   

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'

# Create a new constant called KEY_QUIT KEY_QUIT = 'Q'
// Create a new constant called KEY_QUIT const char KEY_QUIT = 'Q';
String variables
    

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"

# Create a new variable called name name = "Bob"
// Create a new variable called name string name = "Bob";
String constants
    

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!"

# Create a new constant called ERROR_MESSAGE ERROR_MESSAGE = "Don't panic!"
// Create a new constant called ERROR_MESSAGE const string ERROR_MESSAGE = "Don't panic!";
Casting (converting) to a string
     

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"

# create an integer variable int_number = 1000 # cast the integer to a string str_number = str(int_number)
// create an integer variable int intNumber = 1000; // cast the integer to a string string strNumber = intNumber.toString();
Casting (converting) to an integer
     

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

# create a string variable containing an integer str_number = "1000" # cast the string to an integer int_number = int(str_number)
// create a string variable containing an integer string strNumber = "1000"; // cast the string to an integer int intNumber = int.Parse(strNumber);
Casting (converting) to a real
     

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

# Create a string variable containing a real value str_number = "1.1234" # Cast the string to a real float_number = float(str_number)
// Create a string variable containing a real value strNumber = "1.1234"; // Cast the string to a real floatNumber = double.Parse(strNumber);

Loading...