All programs manipulate data in some way. A program might manipulate text to find relevant web pages, sound waves to cancel background noise, or photos to detect faces.
How does a computer represent all of those complex forms of data? You might be surprised to discover that everything boils down to just numbers and letters!
Primitive data types
In programming, we call a single piece of data a value.
A data type is a category for a value. It describes what the value represents and how it can be used.
Just a few basic types form the building blocks of all data. We call these primitive data types because we can create all other types of data by composing them. In Python, the primitive data types are integers, floats, booleans, and strings.
Integers
An integer is any number without a decimal point. It includes negative numbers, positive numbers, and zero.
The values 1 , 2024 , 12345678 , 0 , -47 , and -1000000 are all integers.
We don't add commas between digits. The computer doesn't recognize the comma-separated -1,000,000 as an integer.
Floats
A float is any number with a decimal point. The name float comes from the term floating point number because the decimal point 'floats' between digits.
The values 1.5 , 3.14159 , 56.01 , -91.7, and -33.33333 are all floats.
The values 22.000 , 0.0, and -1.0 are also floats. The computer recognizes any number written with a decimal point as a float, even if its decimal part is zero.
Booleans
A boolean is a truth value. It can only be one of two possible values: True or False.
We can think of a boolean value as indicating the answer to a question with only two options. Yes or no? On or off? Logged in or logged out?
In Python, the capitalization of True and False matters. The computer does not recognize the lowercase true or false as booleans.
Strings
A string is text, shortened from: a string of characters. A character is any letter, number, symbol, or space that we can type on a keyboard. A string must be surrounded by quotation marks.
The values "z" , "python@example.com" , "Try it out!" , "25% off", and ":)" are all strings.
The values "-12" , "98.5" , "2 + 2" , and "True" are also strings. The computer recognizes anything surrounded by quotation marks as a string, even if the text inside the quotation marks looks like a number or a boolean.
The value "" is a string, often called the empty string because it contains no characters. This is different from the value " ", which contains a single space character.
๐ Try it out!
[[โ graded-group 1]]
[[โ explanation 1]]
Complex data
How can just integers, floats, booleans, and strings possibly make up something as complex as a webpage, song, or photo?
Think about a webpage you use often. What data is on that page? Maybe it has a username (string!), a view count (integer!), or a price (float!). Maybe it shows your last message as read or unread (boolean!). This whole article you're reading right now is represented as one giant string.
Audio and images
Computers represent all audio and image data as huge sequences of integers. For audio, we sample the sound wave at regular time intervals and convert the amplitude at each sample point to an integer. That sequence of integers describes the sound wave, just like points on a graph.
[[โ image 4]]
Computers break images down into thousands of tiny pixels like a mosaic. They represent each pixel as an integer between 0 and 255 that describes its brightness. In black and white images, for example, the computer represents each pixel as either the integer value 255 (white) or the integer value 0 (black). In color images, we need three integers in the range 0-255 to describe each pixel's color: one for the amount of red light, one for green, and one for blue.
Binary data
Okay, but why make this all so complicated? It turns out that computers can only truly understand 1s and 0s, or binary. At the end of the day, every piece of data that the computer processes needs to be translated to 1s and 0s. [[โ explanation 2]]
Computers can easily convert integers, floats, booleans, and strings to binary, so it's convenient to use them as the basis for data in most programming languages. We can imagine how a boolean True or False maps to a binary 1 or 0, but integers, floats, and strings map to 1s and 0s, as well. We can represent the integer 47 as 101111, and we can represent the string "k" as 01101011. [[โ explanation 1]]
Computers automatically handle the data conversion behind the scenes, saving programmers like us from needing to directly manipulate 1s and 0s. Instead, we get to enjoy the convenience of primitive data types!
Summary
[[โ video 1]]