1.4.1 - Primitive data types and character sets

1.4.1 - Primitive data types and character sets

A value such as 007 might be an identifier, a number to calculate with, or three characters to display. This part of 1.4.1 explains how data types give values meaning and how ASCII and Unicode let computers store text as binary. You will choose types for a purpose, convert input deliberately, and interpret supplied character tables rather than memorise codes.

Values and their data types

Every stored value needs a meaning before a program can use it.

Data type

A data type describes the kind of value a variable can hold and the operations that make sense for it.

The same visible symbols can mean different things: adding the integers 12 and 3 gives 15, while concatenating the strings "12" and "3" gives "123". Stored bits need an interpretation; the bits alone do not announce whether they represent a number or text.

The primitive types named in this topic are:

TypeMeaningExample and useful operation
IntegerA whole number, including zero and negative values-4; add a whole-number change
Real / floating pointA numeric value that can have a fractional part18.75; calculate a measured average
CharacterOne character, such as a letter, digit or punctuation mark'?'; compare a single response character
StringA sequence of characters, possibly empty"Room 007"; concatenate a label
BooleanOne of two logical values, true or falsedoorClosed; use it in a decision

A real number is a mathematical idea; floating point is a finite computer representation for real values. Not every real value can be represented exactly. A Boolean is not the string "False": that string contains five characters, whereas a Boolean false value controls a logical decision. Type names and implementations vary between languages. Python has int, float, str and bool; it uses a length-one string for a character rather than a separate character type.

Choose types from the required use

Choose a type from what the program must do with a value, not from its appearance alone. A school stores a pupil's admission identifier "00418" as a string: the leading zeros matter and the identifier is not added to another identifier. The number of pupils in a class is an integer because pupils are counted in whole units. A mean journey time of 12.6 minutes is real because the calculated result may have a fractional part. A single grade letter can be a character, and a flag recording whether consent was given can be Boolean.

A useful justification connects the type to a requirement. “String because it is text” is less informative than “String because the identifier's leading zeros must be preserved and no arithmetic is required.” Conversely, storing a quantity as text makes numeric comparison or arithmetic require conversion. For example, string order need not agree with numeric order: "100" comes before "20" in a simple character-by-character ordering because 1 comes before 2.

Type choice is separate from validation. An integer can still be an invalid class size, such as -8. A string can still be an invalid identifier. The type gives the category of value; checks such as a permitted range or format decide whether that particular value is acceptable.

Convert input before using it

Casting converts a value to another type when a valid conversion exists. Python's input(prompt) returns a string, even if the user types digits. int converts suitable integer text to an integer, float converts suitable numeric text to a floating-point value, and str produces a string representation. print displays a value; displaying it does not change the stored variable's type.

This is a static example. The user types 7 at the prompt.

Python
text_count = input("Number of tickets: ")
count = int(text_count)
total = count * 2.5
message = "Cost: " + str(total)
print(message)

The states are text_count = "7", count = 7, total = 17.5, and message = "Cost: 17.5". Multiplication uses a number; concatenation uses strings. Without the conversion, "7" + "2" would join text rather than calculate nine.

Conversions can fail or lose information. int("7.5") fails because that text is not an integer literal; float("7.5") succeeds. Converting the already numeric value 7.5 with int(7.5) gives 7, truncating toward zero, not rounding to the nearest integer. Negative int(-7.5) gives -7. An invalid entry such as "seven" needs handling before the program can continue normally. Casting is therefore not a substitute for validating that input has the required format and range.

When checking a short fragment, follow both each value and its type. Convert a numeric count before calculating with it, then convert the calculated amount to text when constructing a message. An identifier can pass through the calculation unchanged as a string, preserving its leading zeros. Python and equivalent pseudocode can express the same conversion steps.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account