Level Three: Numbers in Python

Level three will teach you about numbers in Python. But hey! No boring math stuff! Let’s get started!

In Python, you can work with numbers just like you do in math class! There are two main types: 

The whole numbers and the decimal numbers.

Whole numbers: These are like counting numbers, like 1, 3, 10, or 50. In Python, we call these integers.

Decimal numbers: These have a dot and include parts after the dot, like 2.5, 3.14, or 10.5. In Python, we call these decimals (or sometimes floating-point numbers or simply floats).

Python can do cool things with numbers, like adding, subtracting, multiplying, and dividing them! 

To do that it uses mathematical operators. Let’s check them out!

Math Operators

Python works well with math operators like:

  • Addition: +
  • Subtraction: –
  • Multiplication: *
  • Division: /

Let’s try them out. Let’s start by creating a variable named result, and assign it the 2+2 value. Then let’s print the result to the screen. Here’s how it will look:

result = 2+2
print(result)

Write this code into the compiler and run it.

Code
Run
#write code here

What did you get? That’s right, you got four.

Now, using the same code, subtract 5 from 10 using the subtraction operator.

Run the code.

Do you have a five? Great.

Now let’s multiply 5 by 5 using the multiplication operator.

Did you get twenty-five? Great.

Finally, let’s divide 10 by 2 using the division operator.

Got five? That’s right. You just performed a division with a remainder. Therefore, the result was a decimal number.

But how do we get an integer after division? Let us see it next!

Division of integer numbers in Python

So, in the previous example, we got a decimal number (float).

But, what if we need to get an integer?

It’s quite simple. To get an integer when dividing, all we need to do is use the double division operator – //

Go ahead and replace the division operator with the double division operator in our previous example, and run the code:

result = 10//2
print(result)
Code
Run
#write code here

Got five as an integer? Great job!

Calculation Order

Python performs calculations in exactly the same order as we, the people do it.
Calculate the following example in your head:

(5+5)3

Don’t use your phone. Keep the answer in your head. Then enter it into the compiler and run the code:

result = (5+5)3
print(result)
Code
Run
#write code here

Here is how Python will solve it:

First, Python will calculate everything inside the parentheses. It will do this in the following order: multiplication, then division, then addition, then subtraction.

Then, Python will calculate everything outside the parentheses. It will do this in the same order as mentioned earlier (multiplication, division, addition, subtraction).

Therefore, Python will add 5 and 5, which will give 10. And it will multiply 10 by 3, which will give 30.

So, did your results match?

Good! Let’s move on.

Numbers and Variables in Python

As you have already noticed, while working with numbers and math operators in Python, we also used variables and the print function.

So, let’s recap how we did that:

First, we declared a variable, giving it the name “result” and the value “(5+5)3″;

Then we went down one line, wrote the print function, and passed it the name of our variable;

When we ran the code, Python calculated (5+5)3, got 30, and assigned this value to the variable “result”;

Finally, Python saw the print function with the argument (result) and understood that it had to output the value of the variable result to the screen, which as we already understood is equal to 30.

Oh, and by the way, we can easily print the result of calculation using only the print function, like so:

print(10*2//5)

But, as you can see, Python perfectly combines numbers, math operators, variables, and functions at the same time!

Now let’s practice a little more and create our own examples with numbers and variables, similar to the ones we just used.

Here are a few templates for you:

result = 2+2
print(result)

result = 10-5
print(result)

result = 5*5
print(result)

result = 10/2
print(result)
Code
Run
#write code here

Level Three Quiz

You have to pass the quiz to complete Level Three!

Level Three Quiz

1 / 3

What is a string?

2 / 3

How do we create a string?

3 / 3

We ran the code and got: Hello, John Doe! Here is the first line of that code: message = "Hello, John Doe!". What was the second line of the code?

Your score is

The average score is 66%

0%

Wrapping Up Level Three

At the Level Three, you accomplished the following:

  1. Learned about integers and decimals in Python;
  2. Applied math operators in calculations using Python;
  3. Learned how to divide a number without a remainder;
  4. Learned the order of math operations;
  5. Learned how to combine numbers, variables, and functions in Python.

Great job! Let’s move on to Level Four: Strings in Python.


Python Online is an online compiler where you can write, run, and check your code without installing any software.