Declaring Variables and Translating Algorithms into Python and C

This is where it all comes together: you take an algorithm written in pseudocode and translate it into a real program. We will do every example in Python and C side by side, so you can see how each pseudocode statement becomes code.

Build on what you learned in Forms 1–3

These Lower School ICT posts cover the basics of this topic:

In this post:

  • Declaring variables and constants
  • A pseudocode-to-code translation table
  • Worked example 1: sequence (average of 3 scores)
  • Worked example 2: selection inside a WHILE loop (counting passes)

Declaring variables and constants

To declare a variable means to give it a name and (in many languages) a data type, so the computer sets aside memory for it.

  • In C you must declare each variable with its type before using it: int (integer), float (real), char (character), char name[20] (string), bool (Boolean, with #include <stdbool.h>). Constants can be made with #define PASS_MARK 50 or const int PASS_MARK = 50;.
  • In Python a variable is created when you first assign a value to it, and Python works out the type: age = 15 (int), price = 12.5 (float), name = "Kezia" (str), paid = True (bool). Constants are written in CAPITALS by convention: PASS_MARK = 50.

Python

# Declaring (creating) variables and a constant in Python
SCHOOL_NAME = "Island Secondary"   # constant (by convention, CAPITALS)
age = 15                            # integer
height = 1.62                       # real (float)
grade = 'A'                         # character (a string of length 1)
name = "Kezia"                      # string
is_prefect = True                   # Boolean
print(name, "is", age, "years old and got grade", grade)
print("Height:", height, "m  Prefect:", is_prefect, " School:", SCHOOL_NAME)

C

/* Declaring variables and a constant in C */
#include <stdio.h>
#include <stdbool.h>

#define SCHOOL_NAME "Island Secondary"   /* constant */

int main(void)
{
    int age = 15;              /* integer */
    float height = 1.62;       /* real */
    char grade = 'A';          /* character */
    char name[20] = "Kezia";   /* string (array of characters) */
    bool is_prefect = true;    /* Boolean (1 = true, 0 = false) */

    printf("%s is %d years old and got grade %c\n", name, age, grade);
    printf("Height: %.2f m  Prefect: %d  School: %s\n", height, is_prefect, SCHOOL_NAME);
    return 0;
}

Translation table

PseudocodePythonC
READ ageage = int(input())scanf(“%d”, &age);
READ priceprice = float(input())scanf(“%f”, &price);
PRINT “Total: “, totalprint(“Total:”, total)printf(“Total: %.2f\n”, total);
total ← 0total = 0total = 0;
IF x > 5 THEN … ELSE … ENDIFif x > 5:
    …
else:
    …
if (x > 5) { … } else { … }
FOR i ← 1 TO 10 DO … ENDFORfor i in range(1, 11):for (i = 1; i <= 10; i++) { … }
WHILE x <> -1 DO … ENDWHILEwhile x != -1:while (x != -1) { … }
a AND b, a OR b, NOT aa and b, a or b, not aa && b, a || b, !a
x MOD y, x DIV yx % y, x // yx % y, x / y (both integers)

Remember: Python uses indentation (spaces) to show which lines belong inside an IF or a loop; C uses curly braces { } and ends each statement with a semicolon.

Worked example 1: average of three scores (sequence)

Pseudocode

START
    READ score1, score2, score3
    average ← (score1 + score2 + score3) / 3
    PRINT "The average is ", average
STOP

Python

# Find the average of three test scores
score1 = float(input("Enter score 1: "))
score2 = float(input("Enter score 2: "))
score3 = float(input("Enter score 3: "))
average = (score1 + score2 + score3) / 3
print("The average is", format(average, ".2f"))

C

/* Find the average of three test scores */
#include <stdio.h>

int main(void)
{
    float score1, score2, score3, average;

    printf("Enter score 1: ");
    scanf("%f", &score1);
    printf("Enter score 2: ");
    scanf("%f", &score2);
    printf("Enter score 3: ");
    scanf("%f", &score3);

    average = (score1 + score2 + score3) / 3;
    printf("The average is %.2f\n", average);
    return 0;
}

Sample run: scores 60, 70 and 85 → The average is 71.67.

Worked example 2: counting passes (WHILE loop with selection)

Pseudocode

PASS_MARK ← 50
count ← 0
passed ← 0
READ mark
WHILE mark <> -1 DO
    count ← count + 1
    IF mark >= PASS_MARK THEN
        PRINT "Pass"
        passed ← passed + 1
    ELSE
        PRINT "Fail"
    ENDIF
    READ mark
ENDWHILE
PRINT passed, " out of ", count, " students passed"

Python

# Count how many students passed (mark of 50 or more).
# Enter -1 to stop.
PASS_MARK = 50
count = 0
passed = 0
mark = int(input("Enter mark (-1 to stop): "))
while mark != -1:
    count = count + 1
    if mark >= PASS_MARK:
        print("Pass")
        passed = passed + 1
    else:
        print("Fail")
    mark = int(input("Enter mark (-1 to stop): "))
print(passed, "out of", count, "students passed")

C

/* Count how many students passed (mark of 50 or more).
   Enter -1 to stop. */
#include <stdio.h>

#define PASS_MARK 50

int main(void)
{
    int mark, count = 0, passed = 0;

    printf("Enter mark (-1 to stop): ");
    scanf("%d", &mark);
    while (mark != -1)
    {
        count = count + 1;
        if (mark >= PASS_MARK)
        {
            printf("Pass\n");
            passed = passed + 1;
        }
        else
        {
            printf("Fail\n");
        }
        printf("Enter mark (-1 to stop): ");
        scanf("%d", &mark);
    }
    printf("%d out of %d students passed\n", passed, count);
    return 0;
}

Sample run with 72, 45, 50 then −1: Pass, Fail, Pass, then 2 out of 3 students passed. Notice that the mark of 50 (a boundary value) correctly gives “Pass”.

Try running these yourself. You can install Python (IDLE or Thonny) and a C compiler such as gcc or Code::Blocks, or use an online editor.

Try it: Pseudocode to code

Choose the correct Python or C statement for each pseudocode line.

READ age (Python)
PRINT total (C, total is an integer)
IF mark >= 50 THEN (Python)
WHILE n <> 0 DO (C)
FOR i ← 1 TO 5 DO (Python)
Declare a real variable called price (C)

Related

Related: Demo: Constants, Variables and Datatypes.; Python Example: Indefinite prompting to determine the corresponding grade for a given mark

Paper 1 practice (multiple choice)

Choose the best answer for each question. Try them all before you check.

1. Which C statement declares an integer variable called count?
(A) count int;
(B) int count;
(C) integer count
(D) declare count

2. In Python, which statement reads a whole number from the user?
(A) num = input(int)
(B) num = int(input())
(C) scanf(num)
(D) READ num

3. The pseudocode operator <> is written in Python and C as
(A) =
(B) ==
(C) !=
(D) <>

4. Which C data type is used for a real number such as 12.75?
(A) int
(B) char
(C) float
(D) bool

5. In Python, the lines inside an if statement are shown by
(A) curly braces
(B) semicolons
(C) indentation
(D) ENDIF

Show answers

1. B
2. B
3. C
4. C
5. C

Paper 2 practice (structured question)

The following pseudocode finds the total cost of tickets for a school concert:

READ num_tickets
cost ← num_tickets * 40
IF num_tickets >= 10 THEN
    cost ← cost - 50
ENDIF
PRINT cost

(a) Write a declaration, in a high-level language of your choice, for:

(i) the variable num_tickets (1 mark)

(ii) the variable cost (1 mark)

(b) Explain why the ticket price of $40 could be declared as a constant. (1 mark)

(c) Translate the pseudocode into a high-level language of your choice. (5 marks)

(d) State the output when num_tickets is 12. (2 marks)

Total: 10 marks

Show answers

(a)

(i) Int num_tickets; (in C) or num_tickets = 0 (in Python) (1 mark)

(ii) Int cost; or float cost; (in C) or cost = 0 (in Python) (1 mark)

(b) The price does not change while the program runs, so storing it as a constant makes the program easier to read and update (1 mark).

(c)
• Reading num_tickets correctly, e.g. num_tickets = int(input()) or scanf("%d", &num_tickets); (1 mark)
• cost = num_tickets * 40 (1 mark)
• if num_tickets >= 10 with correct syntax (1 mark)
• cost = cost - 50 inside the if (1 mark)
• print(cost) or printf("%d", cost); (1 mark)

(d) 12 × 40 = 480 (1 mark); 480 − 50 = 430, so 430 is printed (1 mark).

Previous: Program Errors, Testing and Debugging: Syntax, Logic and Runtime Errors
Next: Program Documentation: Internal and External Documentation
Back to CSEC Information Technology Notes

Syllabus link: This post is relevant to the CSEC® Information Technology Syllabus (CXC 30/G/SYLL 17), Section 8: Program Implementation, Specific Objectives 4 and 5 (“declare variables and constants using elementary data types” and “translate algorithmic statements into high-level language syntax”).

Attributions to media used in this post:

No third-party images are used in this post. Embedded videos are credited on YouTube to their channels.

Updates

2026/11/23

– Added Previous, Next and Back to course links at the end of the lesson.

2026/10/16

– Syntax highlighting added to code examples.

© 2026 Vedesh Kungebeharry. All rights reserved.

Variables, Constants and Data Types: Integer, Real, Character, String and Boolean

Every program stores data while it runs – a student’s name, a price, the number of goals scored. In this post you will learn the difference between a variable and a constant, and the five data types you need for CSEC: integer, real (floating point), character, string and Boolean.

Build on what you learned in Forms 1–3

These Lower School ICT posts cover the basics of this topic:

In this post:

  • What is a variable?
  • What is a constant?
  • Naming variables (identifiers)
  • Data types with examples

Variables

A variable is a named area of storage (in memory) whose value can change while the program is running. Think of it like a labelled box: the label is the name (for example score) and you can take out the old value and put in a new one at any time.

Example: in a cricket scoring program, runs starts at 0 and changes every time the batsman scores.

Constants

A constant is a named value that never changes while the program is running. Examples: the VAT rate (12.5% in Trinidad and Tobago), the number of days in a week (7), the pass mark (50), or π (3.14159).

Why use a constant instead of just typing the number? If the VAT rate changes, you only change it in one place, and a name like VAT_RATE is easier to understand than 0.125.

Naming variables (identifiers)

  • Use meaningful names: total_price is better than tp or x.
  • Start with a letter; no spaces (use an underscore instead, e.g. first_name).
  • Do not use words that the language already uses, such as if or while.
  • Many programmers write constants in CAPITALS, e.g. PASS_MARK.

Data types

The data type tells the computer what kind of value a variable will hold, and therefore what can be done with it.

Data typeWhat it storesExamples
IntegerWhole numbers (positive, negative or zero), no decimal point15 (age), −3, 0, 2026
Real (floating point)Numbers with a decimal (fractional) part12.50 (price), 1.62 (height), −0.5
CharacterA single letter, digit or symbol‘A’ (grade), ‘M’, ‘$’
StringA group (sequence) of characters, such as words or text“Kezia Ramdial”, “Port of Spain”, “868-555-1234”
BooleanOnly one of two values: TRUE or FALSEis_prefect = TRUE, has_paid = FALSE

Note: a telephone number such as 868-555-1234 is stored as a string, because you never do arithmetic with it and it contains dashes.

Shows how programs store values in variables and assign new values to them.

Video: Programming Basics: Statements & Functions: Crash Course Computer Science #12 – https://www.youtube.com/watch?v=l26oaHV7D40

A quick look in code

Here is how the five data types and a constant look in Python and C. (You will learn more about declaring variables in Section 8.)

Python

# Declaring (creating) variables and a constant in Python
SCHOOL_NAME = "Island Secondary"   # constant (by convention, CAPITALS)
age = 15                            # integer
height = 1.62                       # real (float)
grade = 'A'                         # character (a string of length 1)
name = "Kezia"                      # string
is_prefect = True                   # Boolean
print(name, "is", age, "years old and got grade", grade)
print("Height:", height, "m  Prefect:", is_prefect, " School:", SCHOOL_NAME)

C

/* Declaring variables and a constant in C */
#include <stdio.h>
#include <stdbool.h>

#define SCHOOL_NAME "Island Secondary"   /* constant */

int main(void)
{
    int age = 15;              /* integer */
    float height = 1.62;       /* real */
    char grade = 'A';          /* character */
    char name[20] = "Kezia";   /* string (array of characters) */
    bool is_prefect = true;    /* Boolean (1 = true, 0 = false) */

    printf("%s is %d years old and got grade %c\n", name, age, grade);
    printf("Height: %.2f m  Prefect: %d  School: %s\n", height, is_prefect, SCHOOL_NAME);
    return 0;
}

Sample output (Python): Kezia is 15 years old and got grade A / Height: 1.62 m Prefect: True School: Island Secondary. In C, the Boolean prints as 1 for true.

Try it: Choose the best data type

Choose the most suitable data type for each item.

The number of students in a class
The price of a bake and shark
A student’s grade, e.g. B
A customer’s name
Whether a fee has been paid (yes/no)
A WhatsApp phone number such as 1-876-555-0100
The temperature in °C, e.g. 31.5

Related

Related: Demo: Constants, Variables and Datatypes.

Paper 1 practice (multiple choice)

Choose the best answer for each question. Try them all before you check.

1. A storage location whose value can change while a program runs is called a
(A) constant
(B) variable
(C) data type
(D) compiler

2. Which data type is BEST for storing the price $45.99?
(A) Integer
(B) Character
(C) Real
(D) Boolean

3. Which value is a Boolean?
(A) “Yes please”
(B) TRUE
(C) 25
(D) ‘Y’

4. Which is the BEST example of a constant?
(A) A student’s age
(B) The number of days in a week
(C) The balance in a bank account
(D) The time of day

5. “Montego Bay” would be stored as a
(A) character
(B) integer
(C) string
(D) real

Show answers

1. B
2. C
3. B
4. B
5. C

Paper 2 practice (structured question)

A program is being written for a car rental company in Bridgetown. It stores the customer’s name, the number of days the car is rented, the daily rate of $95.50, whether the customer has insurance, and the car category (A, B or C).

(a) Distinguish between a variable and a constant. (2 marks)

(b) Identify the most suitable data type for EACH of the following:

(i) the customer’s name (1 mark)

(ii) the number of days rented (1 mark)

(iii) whether the customer has insurance (1 mark)

(iv) the car category (1 mark)

(c) The daily rate is the same for every customer. State whether it should be stored as a variable or a constant, and give ONE reason. (2 marks)

Total: 8 marks

Show answers

(a) A variable is a named storage location whose value can change during processing (1 mark); a constant’s value never changes during processing (1 mark).

(b)

(i) String (1 mark)

(ii) Integer (1 mark)

(iii) Boolean (1 mark)

(iv) Character (1 mark)

(c) Constant (1 mark); its value does not change while the program runs / it only needs to be changed in one place if the rate changes (1 mark).

Previous: Defining a Problem with an IPO Chart: Input, Processing and Output
Next: What Is an Algorithm? Definition and Characteristics of a Good Algorithm
Back to CSEC Information Technology Notes

Syllabus link: This post is relevant to the CSEC® Information Technology Syllabus (CXC 30/G/SYLL 17), Section 7: Problem-Solving and Program Design, Specific Objective 4 (“distinguish between variables and constants”).

Attributions to media used in this post:

No third-party images are used in this post. Embedded videos are credited on YouTube to their channels.

Updates

2026/11/23

– Added Previous, Next and Back to course links at the end of the lesson.

2026/10/16

– Syntax highlighting added to code examples.

© 2026 Vedesh Kungebeharry. All rights reserved.