3

Words, Numbers, & Variables, Oh My

 2 years ago
source link: https://dev.to/vickilanger/words-numbers-variables-oh-my-mmi
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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"
Enter fullscreen modeExit fullscreen mode

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
"""
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Note: unlike real life, we don’t include commas in our numbers. If you have 1,000,000 it would just be 1000000.

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
Enter fullscreen modeExit fullscreen mode

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.

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
Enter fullscreen modeExit fullscreen mode

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:

  1. Must start with a letter or _
  2. Not allowed to use keywords (words set aside for different processes)
  3. Use lowercase letters and numbers
  4. 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
Enter fullscreen modeExit fullscreen mode

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”
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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.

Screenshot of Python tutor: code:<br> kiwis = 10<br> apples = 7<br> total_fruits = kiwis + apples<br> people = 6<br> fruits_per_person = total_fruits / people  <br> fruits_left_over = total_fruits % people<br> print(fruits_left_over)<br>  then it shows a chart to the right Frames<br> Global frame<br> kiwis   10<br> apples  7<br> total_fruits    17<br> people  6<br> fruits_per_person   2.8333<br> fruits_left_over    5<br>

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.

On the left, there are lego building blocks with different data type examples on them. On the left, there are lego building blocks with the data types on on them. The left has 3.14<br> True<br> 92802834<br> “2 Kitties”<br> False<br> “It’s True”<br> 185<br> 4591.28701<br> The right has: Boolean, Float, String, Integer

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” cat_summoning_spell = “Here, ” + (“Kitty ” * 3)

people = 7 treasure_chest_loot = 1000000 gold_coins_leftover = treasure_chest_loot % people


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK