- 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
| Type | Holds | Example |
|---|---|---|
| Integer | Whole numbers | 25, -3 |
| Float (real) | Numbers with a decimal part | 3.14 |
| Character | A single letter or symbol | 'A' |
| String | Text | "Delhi" |
| Boolean | True or false | True |
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.
Key takeaways
- Variables hold changing values; know the five data types.
- Learn arithmetic, comparison and logical operators.
- % is remainder.
Check your understanding
-
1. What is 23 % 4?
- 5
- 3
- 5.75
- 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. Evaluate (8 > 2) AND (3 > 6).
- False
- True
- 8
- 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. Which data type should hold a student's name?
- Integer
- Boolean
- Float
- String
Show answer
Answer: D. A name is text, so it is held in a string.
-
4. Which value is best stored as a constant?
- The number of days in a week
- A student's marks in a test
- The balance in a bank account
- 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. Which data type holds a single letter such as 'A'?
- Integer
- Boolean
- Float
- 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.