4.1.2. Basic Data Types#
First, we’ll look at a few basic data storage types. We’ll also be including some code examples you can look at, though don’t worry yet if you don’t understand the code, since we’ll be covering these in more detail throughout the rest of the book.
Booleans (True / False)#
Binary consisting of 0s and 1s make it easy to represent true and false values, where 1 often represents true and 0 represents false. Most programming languages have built-in ways of representing True and False values.
Booleans are often created when doing sort of comparison or test, like:
Do I have enough money in my wallet to pay for the item?
Does this tweet start with “hello” (meaning it is a greeting)?
Click to see example Python code
# Save a boolean value in a variable called does_user_have_blue_checkmark
does_user_have_blue_checkmark = True
# Save a boolean value in a variable based on a comparison.
# The code checks if a wallet has more in it than the cost of the item
# which will be True or False, and be saved in has_enough_money
has_enough_money = money_in_wallet > cost_of_item
# Save a boolean value in a variable based on a function call.
# The code checks if the text of a tweet (stored in tweet_text) starts
# with "Hello", which will be True or False, and be saved in is_greeting
is_greeting = tweet_text.starts_with("Hello")
Numbers#
Numbers are normally stored in two different ways:
Integer: whole numbers like 5, 37, -10, and 0
Floating point numbers: these can represent decimals like: 0.75, -1.333, and 6.626 × 10 ^ −34 (see more about the complications of floating point numbers in binary [d2])
Click to see example Python code
# Save an integer value in a variable called num_tweet_likes
num_tweet_likes = 197800
# Save an integer value in a variable called max_tweet_length
max_tweet_length = 280
# Save a floating point number in a variable called average_tweet_length
average_tweet_length = 133.5
When computers store numbers, there are limits to how much space is can be used to save each number. This limits how big (or small) the numbers can be, and causes rounding with floating-point numbers.
Additionally, programming languages might include other ways of storing numbers, such as fractions, complex numbers [d3], or limited number sets (like only positive integers).
Strings (Text)#
Computers typically store text by dividing the text into characters (the individual letters, spaces, numerals, punctuation marks, emojis, and other symbols). These characters are then stored in order and called strings (that is a bunch of characters strung together, like in Fig. 4.6 below).
In our example tweet, we can see some different pieces of information that might be represented with strings:
Click to see example Python code
# Save a string in a variable called user_name
user_name = "WeRateDogs®"
# Save a string in a variable called twitter_handle
twitter_handle = "@dog_rates"
# Save a string in a variable called tweet_text
twitter_handle = "This is Woods. He’s here to help with the dishes. Specifically the pre-rinse, where he licks every item he can. 12/10"
Text can be stored with extra formatting information, such as fonts and colors, in different document file formats [d4] like Word Documents, PDF files, html website files [d5], etc.
Note: We’ll demonstrate strings later in this chapter, and in more detail in Chapter 7: Trolling