5-Min Code Camp 0.1: Learning to code in Python. Data Types, Variables, Basic Math

Derek Loyer
5 min readFeb 4, 2021

If you read our last post “Write your first line of code in 5 minutes,” we brought you off the bench and got you from zero to coder in no time. We didn’t go into detail because we wanted to show you how to get started now, overcome an obstacle and write your first lines of Python code with Colaboratory. Interested? Check it out here.

To build on what you’ve learned, we’ll be tackling this in small bites (nanochomps, even. Get it?) to keep you coding. In this installment we’re introducing some core concepts and a little terminology that will make you just dangerous enough to say something stupid in your next scrum stand up meeting with the dev team.

Eventually, we’ll walk you through setting up your local computer with Anaconda, Homebrew (it’s not as delicious as it sounds), PIP, IDEs, virtual Envs, CLI tools, LMNOPs (OK, maybe I made up that last one), but for now we will keep using Colaboratory because it’s easy, free, and it works.

Alright, let’s get started!

Basic Data Types, Variables and Basic Math

Programming languages use interpreters and compilers to take what you type and magically convert that code into 1s and 0s. Those become instructions for a computer to crunch and give you a result.

Since computers are not exactly fantastic at reading our minds (yet…Wait, is this an ad on Instagram for something I was just thinking about?) and they’re not great at guessing, we need to tell them exactly what our intention is. To help organize our code/programs/apps we learn that languages have “data types.” Data types are building blocks that tell the computer what type of thing it is working with.

We don’t want to overload you (that’s a programming joke you’ll get at some point) so today we’ll introduce just a few data types to get the ball rolling: The integer, float, and string.

Stay with us; we’ll get you through the boring stuff and onto the fun stuff like making a bot that automatically waters your plants, but one step at a time.

It’s time to open up a Colaboratory notebook <here> as well as our little cheat sheet to follow along and see how this is all coming together.

Numbers are an important part of computer science and there are few different ways to handle them. In Python we can use:

Integers (int)

You will see these identified as “int.” Integers are whole numbers such as 1, 10, 100, 6. Just plain ol’ whole numbers with no decimal points at the end. In the Colab notebook you will see that “int” is the default data type for any number you input. Don’t believe us? We’re hurt. But fine, we’ll prove it.

In your Colab notebook:

  • Type (or copy paste we won’t judge):
x = 10
type(x)
  • Click run
  • The output should say ‘int’

Ok, we pulled a variable assignment here by setting x equal to 10, which we’ll touch on shortly. This code creates a variable we are calling “x” and sets it equal to an int 10. The next line tells us what “data type” x is. Since x is holding the value of integer 10 we should see type return “int.”

Now, give it a try.

Floats (float)

Floats are “floating point” numbers that are 64-bit double-precision values. “What does that mean?” you ask? It just means that a float is a number that can hold data beyond the decimal point.

Wanna test this out? In your Colab notebook:

  • Type:
x = 10.1
type(x)
  • Click run
  • The output should say ‘float’

String (str)

A string is a sequence of characters, any sequence of characters as long as they are between single or double quotes. This includes words, single letters, phrases and even numbers can be stored in strings (but not for math manipulation. If you’re curious what we mean, just ask) Here are a few examples of strings:

“Hello”
“nanochomp”
“Abracadabra”
“Star Wars is overrated”
“10 is a number”

Variables

Assigning variables you can save numbers, strings, equations, and other data types into variables. Before we jump in there are a few rules to note:

  1. Keep it short but meaningful. A good variable name should be easy to understand, but not super long.
  2. A variable name MUST start with a letter or and underscore. If you really wanted to start your variable names with numbers you are out of luck. _thisworks, this_works, This_Works2.
  3. You can use letters, numbers and underscores. Special ch@racte$ are out of bounds.
  4. No whitespace in a variable name
  5. Variable names are case sensitive. pyVar1 is seen as different from PyVar1
  6. Oh and one last thing. Reserved words are not usable for variable names. “for” “if” “while” and other reserved words in the language will not be usable as variable names. We will run into these as we go so don’t overthink it just yet.

For this example we will set ‘first_number’ to 10, ‘second_number’ to 5 and we will set a variable answer to be the result of ‘first_number’ + ‘second_number’. For good measure we will use the print function to print what gets saved into ‘answer’.

first_number = 10
second_number = 5
answer = x + y
print(answer)

“Fun” with Math

Now that you have the basics, let’s put it all together.

We learned that we can assign numbers to variables, add them together, store the result in another variable and print the result.

We can also subtract, multiply, divide, work with exponents, square roots, and even a weird little operator called the modulo (spoiler alert, it’s a fancy name for taking a remainder after division).

To have fun with some of these exercises, flip over to your Colab notebook and play around with someone of the basic math operations we added for you. Change the numbers and see what happens when you add two strings together.

Next time, we’ll play with data types and introduce you to Lists, Dictionaries, Tuples and Booleans. These will add some structure with ordered and unordered lists and will be really important later.

Then, we’ll explain what looping and control flow are.

The goal is to build you up to the point where you can create a game. It may not be Fortnite just yet, but tic tac toe or rock, paper, scissors are well within reach here.

--

--

Derek Loyer

co founder of nanochomp. 🚀 Startup builder 🤖 Machine learning/AI nerd ⚙️ Gearhead 📍 RDU/DTW