Data types Flashcards
Integer
A number without a fraction or decimal.
Can be positive or negative.
e.g. 0, -3, 107463
Real
Real numbers include all numbers that exist and their fractions and decimals
Integers are real number, but it is more efficient to declare them as integers so that less memory is needed
In OCR pseudocode, real numbers are declared as “float”
e.g.
float(“1.25”)
Real
Real numbers include all numbers that exist and their fractions and decimals
Integers are real number, but it is more efficient to declare them as integers so that less memory is needed
In OCR pseudocode, real numbers are declared as “float”
e.g.
float(“1.25”)
Boolean
A boolean value can only be true or false
Character
A character data type can store a single letter, number, or symbol
e.g. C, 3, !
String
A string can hold a lost of characters of any length.
When entered, they are enclosed in single or double quotation marks.
e.g. string = “Hello”
Importance of data type
Data used by a computer is only presented as 1s and 0s
The data type is needed so that the computer knows how to interpret the binary code so it can be used correctly
Declaring data type
In some programming languages the data type must be declared, e.g. int age declares that the variable age stores an integer value
In other languages, the type does not have to be declared, and is instead inferred from the first type of data stored
Casting
The conversion of one data type into another
e.g.
str(integer)
int(string)
float(string)
Identify a data type that would be suitable for the variable ‘response’ in the following situations and state why it is the most appropriate.
a) response = 10/7 [2]
b) response = input(“Please enter your name”) [2]
c) response = input(“Please enter your age in years”) [2]
a) real/float
It contains fractions or decimals
b) string
A person’s name consists of a sequence of characters
c) integer
It is a whole number without fractions or decimals
State what is returned by each of the following statements:
- str(9)
- int(999)
- float(90)
[3]
9
999
90.0