Python For Kids (Part 22: Float Primitive Data Type)

Python For Kids (Part 22: Float Primitive Data Type)

For a complete table of contents of all the lessons please click below as it will give you a brief of each lesson in addition to the topics it will cover. https://github.com/mytechnotalent/Python-For-Kids

Today we will discuss the float primitive data type and work with a simple example or two as a float is a fractional number with a decimal point in contrast to an integer.

STEP 1: Open Mu IDE

STEP 2: Type Code

x = 10.1
y = -9.5

print(x + y)

STEP 3: Click Save

STEP 4: Save File - main.py - Desktop

STEP 5: Click Run

STEP 6: Review Output

We see that our REPL has opened and the number 0.5999999999999996 has printed nicely. We can see that if we take decimal 10 and subtract -9.5 we do in fact get 0.5999999999999996 which may take a bit to understand.

We see many layers of precision as we call it. What is going on behind the scenes is 0.1001100110011001101 is what that number looks like in binary. Let us break it down a bit.

 0        .  1            0            0            1         ...
(0 * 2^0) + (1 * 2^-1) + (0 * 2^-2) + (0 * 2^-3) + (1 * 2^-4) ...

This is not something you have to memorize this is just what is going on behind the scenes.

STEP 7: More Examples

Let's try another.

x = 10.1
y = -9.5


print(x + y)

Here we see 1.25 which means in binary it is 1.01 and we can break it down like the following.

 1        .  0            1
(1 * 2^0) + (0 * 2^-1) + (1 * 2^-2)

Here we check to see again what type our number is:

type(1.25)
<class 'float'>

HOMEWORK:

I would like to you try a few more examples and re-run them so you get a better idea of how this works. Remember that floating-point numbers are represented in computer hardware as base 2 (binary) fractions.

This is the FUN part of learning when we apply our new knowledge and experiment on our own it REALLY helps us learn these concepts so please take your time and try a number of different numbers :)

In the next lesson we discuss the string primitive data type.


要查看或添加评论,请登录

Kevin Thomas的更多文章

社区洞察

其他会员也浏览了