If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.
Jump To:
Programmingâs Building Blocks - Data Types
Think about the world around you. There are lots of different types of things, right? We can take all of those things and break them down into different types. For everything we see, weâll call them different pieces of data or information. Each piece of data can be broken down into different types. These different types are the building blocks of programming. Weâll go into more detail on all of these, but for the most part, we have words, numbers, and true or false things.
Look around you and take in your surroundings. Weâre going to use them to make these data types. Iâll give examples with my surroundings, but please do use your own.
For me, my surroundings have:
- a bookshelf with 23 books on it
- a half-full (or half-empty?) water bottle
- a dog bed with 2 cats sleeping on it
- a keyboard with 68 keys on it
Strings
The first type of data weâll use is called a string. A string is a bunch of characters (eg. letters, numbers, symbols) surrounded by quotes. Strings can be a single letter, a word, a sentence, or even a whole book.
Strings can use ""
but they can also use single quotes, an apostrophe, ''
. They must be in pairs though. If the quotes donât match on both ends of a string, like this âpuppiesâ
, it wonât work.
From my surroundings, I could have:
"Felix Ever After"
'They Both Die At The End'
"The Dangerous Art of Blending In"
"keyboard"
"Remmy and Beans are the 1 cats on the dogâs bed"
If you want your string to show on multiple lines, you can surround it with three pairs of quotes. Hereâs an example of a multi-line string with a dog haiku.
"""How do I love thee?
The ways are numberless as
My hairs on the rug.
-Author unknown
"""
Useful Built-in Functions for Strings
Functions are actions you can use with your data. Python has a bunch that are already pre-built and ready for use. In order to use them, we first need to know the syntax, functionname(âyour stringâ)
. Some helpful built-in functions for strings are len()
, min()
, and max()
.
-
len()
- returns the length -
min()
- returns the smallest value; âAâ being the smallest -
max()
- returns the biggest value; âzâ being the biggest; capitals being smaller than lowercase -
chr()
- returns the character associated with a given Unicode number, âchrâ is short for character -
ord()
- returns the Unicode number associated with a given character
Here are some examples. Try printing them.
len(âKittiesâ)
min(âKittiesâ)
max(âKittiesâ)
chr(86)
ord("V")
You may not use them often, but with chr()
and ord()
is it helpful to know that each character is assigned and associated with a unicode code number. You can use ord()
to find out what that number is or you can look it up at https://www.lookuptables.com/text/ascii-table. Youâll find that lowercase letters range from 97 to 122 and uppercase numbers range from 65 to 90. With that known, 97 a
is the lowercase version of 65 A
.
Note, we are about to talk about the .upper()
method. Later on, weâll learn how to code that using chr()
and ord()
.
Some Methods You Can Use with Strings
Methods are special functions available based on an objectâs type. We just learned that strings are a type. Letâs go over some methods we can use on strings. First, we should talk about how to use them or the syntax. String methods will look like âyour stringâ.methodname()
. Sometimes there will be something in the parentheses, but not always.
-
âyour stringâ.upper()
- returns string all uppercase -
âyour stringâ.title()
- returns string with first letter of each word uppercase -
âyour stringâ.split()
- returns a your string cut up at itâs spaces and put into a list Here are some examples. Try printing them.
"Chickens slipping on a fondue fountain".upper()
"Llamas melting on a math recipe".title()
"Snakes jumping on a potato pie".split()
Numbers
The next type of data weâll use is not called numbers. Instead, numbers are broken down into two different types. We have whole numbers and decimal numbers.
Numbers do not need and should not have any quotes around them unless they are part of a string. Being part of a string would make them a string and not a type of number.
Integers
Just like in math class, integers are whole numbers. Integers are never decimals or fractions. They are whole things. For example, you wouldnât have 1.7 airplanes or 2.3 dogs. For things like this, you want to use the integer data type.
From my surroundings, I could have:
23 # books
2 # cats
68 # keys on the keyboard
Note: unlike real life, we donât include commas in our numbers. If you have 1,000,000
it would just be 1000000
.
Useful Built-in Functions for Integers
We have built-in functions for whole numbers too!
-
pow()
- returns 1st number to the power of the second number, exponents -
abs()
- returns the absolute value or positive of a negative number Here are some examples. Try printing them.
pow(2, 3) # 2 * 2 * 2
abs(-3) # |3|
ââ
Floats
Similar to integers, but not the same, we have a type of data called floats. Floats are also called floating-point numbers. Floats are numbers with decimals. Floats can also be whole numbers, but typically only when you may need to count the portion of a thing.
From my surroundings, I could have:
0.5
In this example, I only have the contents of my water bottle. From my surroundings, unless theyâre broken, I canât very well have a decimal number of books, cats, or keyboards.
Useful Built-in Functions for Floats
Iâm sure it comes as no surprise that floats have some built-in functions.
Here are some examples. Try printing them.
-
pow()
- returns 1st number to the power of the second number, exponents - `abs()â - returns the absolute value or positive of a negative number
-
round()
- returns a rounded decimal Here are some examples. Try printing them.python pow(4.1, 2) abs(-3.234) round(3.65) round(1.29)
Youâre yes then youâre no - Booleans
The last type of data we are going to cover is boolean. Like numbers, booleans do not need and should not have any quotes. They must start with a capital letter. Your only two options are the keywords True
and False
.
Booleans use True
and False
but they can be used as stand-ins for yes/no and on/off. If you choose to think of booleans as, yes/no or on/off, use True
for âyesâ & âonâ then use False
for ânoâ & âoffâ.
From my surroundings, I could have:
`
True # Remmy is sleeping
False # Beans is sleeping
False # Water bottle is full
`
Keep Your Data Dry with Variables
Now that we know the different types of data, letâs find a way to use each piece of data without having to type it over and over again. In programming, this concept of not retyping things is called Donât Repeat Yourself(DRY). Variables help us keep code dry by holding data for us.
Writing variables is incredibly similar to math class where you may have had x = 5
to mean there are 5 watermelons. The biggest and best difference is that we donât typically use x
. Instead, we give descriptive names that help us understand what data we are working with. Instead of a letter variable, we can actually say what the variable stands for. Instead of x = 5
we could have watermelon_count = 5
.
Picking out a Name
There are some rules with naming that have to be followed or your code wonât work as expected. Then there are some suggestions that you may or may not find helpful.
Here are the requirements:
- Must start with a letter or
_
- Not allowed to use keywords (words set aside for different processes)
- Use lowercase letters and numbers
- No spaces, use
_
instead
Some suggestions to make your life easier:
- Make the names descriptive
- Include the data type or structure
From my surroundings, I could have:
`
books_count_int = 23
cat_one_name = âRemmyâ
cat_two_name = âBeansâ
water_level = 0.5
keys_count_int = 68
cat_one_sleeping = True
cat_two_sleeping = False
`
Did you notice that with the variable names, I didnât have to include comments and you understood what each of those numbers meant? Thatâs why descriptive names are important. Had I called something x
, you wouldnât have known which thing I was referring to.
Assignment
Did you notice above where we used =
for our variables? We call the =
an assignment operator. I know itâs an equals sign, but I find it helpful to read it as âisâ. In cat_one_name = âRemmyâ
, I would read this out loud as âcat one name is Remmyâ.
Whatever is on the left of the =
is the variable name. The data on the right side is the value being assigned to the variable.
A Teensy Tiny Bit of Math
I know, I know. I said there wouldnât be a lot of math and it feels like everything has been about math. Whether we like it or not, a lot of the cool things we can do are based on some math concepts. For example, you can add and multiply words. Adding words is called concatenation but if you prefer, you can call it âsmashing multiple strings togetherâ.
`
short_greeting = âheyâ
long_greeting = short_greeting * 3
different_greeting = short_greeting + âyouâ
`
If you put this code into Python Tutor, youâll get the below. On the right, it will show you your variables and what values they hold.
Notice that you can mix variables and data when using these math operators.
By the way, we also have addition +
, subtraction -
, multiplication *
, and division /
. These all work exactly as you would expect. They follow the same order of operations (eg PEMDAS or BODMAS) that you learned in math class. You may also choose to put parentheses ()
around things you would like done first.
There is another math operator %
, called modulus. It looks like and is a percent sign. However, it does something very different. %
is used like /
but it gives the remainder instead.
Letâs use some variables and do a little math. We have some fruits and we want to share them with a group of people. If you divide them, youâll find you would have to cut fruits in order to split them evenly. Instead, we can use %
or modulus to find out how many fruits are remaining. You can choose what to do with the remaining fruits.
`
kiwis = 10
apples = 7
total_fruits = kiwis + apples
people = 6
fruits_per_person = total_fruits / people
fruits_left_over = total_fruits % people
print(fruits_left_over)
`
If you run this code in Python Tutor, it will look like this. On the right, it has an output box where all of your print()
statements show up. Under that it says âFramesâ, thatâs where it will show you your variables and what values they hold.
Do you remember? - the basics
Here's some practice challenges. Letâs practice what weâve learned so far. Go ahead and comment on this post with your answers. Do you remember? If not, you can always go back to read sections again.
Match data with their data types
Match each block on the left with its data type on the right. Each data type block should be used only twice.
Give an example or three of each Type of Data
Example 1 | Example 2 | Example 3 | |
---|---|---|---|
String | |||
Integer | |||
Float | |||
Boolean |
Can You Fix Whatâs Wrong with These?
Some of these have more than one right answer. Believe in yourself and give it a try.
â234 Puppies in 3 bathtubsâ |
45,345.012 |
true |
291â345â710.5 |
27,345,192 |
90.0000° N, 135.0000° W |
â4,823 bunnies frolicking in a fieldâ |
Off |
Make some Variables with each Type of Data
Example 1 | Example 2 | Example 3 | |
---|---|---|---|
String | |||
Integer | |||
Float | |||
Boolean |
How would you read these out loud?
Examples |
---|
book_name = âThe Meet Cute Diaryâ print(book_name)
|
cat_summoning_spell = âHere, â + (âKitty â * 3) |
people = 7 treasure_chest_loot = 1000000 gold_coins_leftover = treasure_chest_loot % people
|
Top comments (1)
Converting to symbols slope and codes caused me many difficulties, but thanks to your help I was able to solve many problems.