• Foundation
  • About 12 min
  • 5 practice questions

Variables, data types and operators

Explain variables, common data types and the arithmetic, comparison and logical operators.

If something looks wrong, please tell us.

A program needs to remember values while it runs and work on them. Variables hold the values, data types say what kind they are and operators act on them.

Variables and constants

A variable is a named place in memory that holds a value which can change, such as marks or total. A constant has a value that does not change, such as the number of days in a week. Assigning a value uses =, as in marks = 72.

Data types

Common data types
TypeHoldsExample
IntegerWhole numbers25, -3
Float (real)Numbers with a decimal part3.14
CharacterA single letter or symbol'A'
StringText"Delhi"
BooleanTrue or falseTrue

Operators

  • Arithmetic: + - * / and % (remainder, called modulus).
  • Comparison (relational): == (equal), != (not equal), >, <, >=, <=.
  • Logical: AND, OR, NOT, which combine true/false conditions.

Worked examples

Example 1. What is 17 % 5? 17 divided by 5 gives 3 with a remainder of 2, so 17 % 5 = 2.

Example 2. A = 10 and B = 4. What is A % B? 10 = 2 x 4 + 2, so the remainder is 2.

Example 3. Evaluate (7 > 3) AND (2 > 5). The first is True, the second is False, so True AND False is False.

Example 4. Which data type should hold a student's name? A string.

A variable's value can change; a constant's cannot. % gives the remainder.

— Remember

Key takeaways

  • Variables hold changing values; know the five data types.
  • Learn arithmetic, comparison and logical operators.
  • % is remainder.

Check your understanding

  1. 1. What is 23 % 4?

    1. 5
    2. 3
    3. 5.75
    4. 4
    Show answer

    Answer: B. 23 divided by 4 gives 5 with remainder 3 (4 x 5 = 20, 23 - 20 = 3). The % operator gives the remainder, so 23 % 4 = 3.

  2. 2. Evaluate (8 > 2) AND (3 > 6).

    1. False
    2. True
    3. 8
    4. 3
    Show answer

    Answer: A. 8 > 2 is True and 3 > 6 is False. True AND False is False, because AND needs both conditions true.

  3. 3. Which data type should hold a student's name?

    1. Integer
    2. Boolean
    3. Float
    4. String
    Show answer

    Answer: D. A name is text, so it is held in a string.

  4. 4. Which value is best stored as a constant?

    1. The number of days in a week
    2. A student's marks in a test
    3. The balance in a bank account
    4. The number of people in a queue
    Show answer

    Answer: A. A constant has a value that does not change, such as the number of days in a week. The others change, so they are variables.

  5. 5. Which data type holds a single letter such as 'A'?

    1. Integer
    2. Boolean
    3. Float
    4. Character
    Show answer

    Answer: D. A character holds a single letter or symbol, such as 'A'.

Sign in to keep track of the lessons you finish.

Next lesson → Making decisions: if and else

Privacy · Terms · Refunds · Delivery