Numbers Python- Practical Explanation

Python supports majorly 3 types of numbers:

  • integers(0-infinity, and all of them in negative),
  • float(integers along with decimal/fraction numbers), and
  • complex numbers(3+6j).

Before we start remember this, you are a programmer👩‍💻, not a mathematician🧐🔢.

Hello Pythonistas🙋‍♀️, welcome back.

In the last post, we saw comments in Python.

Before that, here’s the answer to the challenge in the last post.

Previous Post’s Challenge’s Solution

Data type simply means the type of data we are using like a number, string, list, or dictionary.

Python has a type function that lets us know the data type of any object.

So, your answer code would be:-

print(type(10))

Output:-

<class 'int'>

We used the print function to display the result of the type function.

Let’s see what <class 'int'> means and be a number ❣ lover with python.

(Remember🤔 the string we used in the first program, is also a data type.)

Don’t worry😨 if you are not yet exactly clear about what is data type, you’ll get to know when we will study OOP concepts.

For now, just understand these are different types of data in python.

in-built or built-in means it comes with python installation.

Integers

Integers type in python are the same as integers in math no difference it includes numbers from 0 to infinite and all negatives too.

But, no fractions.

You can easily use them.

They are used without these-> ” ” or ‘ ‘. For example:-

print(1)
print(1,1)
print(type(1))
#And so on.

Output:-

1
1 1
<class 'int'>

Type is a function in python just as print is.

It tells🎙 you the data type of the object you give in it as an argument.

Integer also includes another datatype in it which is boolean.

Boolean Numbers

Boolean is a data type that includes 0 and 1 and, true and false.

Only these four.

Here, 0 is for False and 1 for True. As it includes 0 and 1 it is a part of integers.

Suppose that you want to make a tic-tac-toe game📺 using python.

To make this you need to know at the end of the game if a player won👍 or not👎. That is if the player won = true or false. Booleans help here. For example:

print(10 > 9)
print(10 == 9)
print(10 < 9)
player= "won" # this line creates a variable we will discuss them later. It prints nothing.
print(player=="won")

Output:-

True
False
False
True

True and False must have their first letter capitalized. Python is case-sensitive.

And that is all for boolean numbers now let’s move on to floating-point numbers.

Float

Float type in Python includes the numbers that contain some decimal value(or say fraction value).

You sometimes need to use decimals for calculations in your python program. Because integers are not always enough.

Example of a float-type number:

print(10.12)
print(type(10.12))

Output:-

10.12
<class 'float'>

Moving on to complex numbers.

Complex

These are the numbers that have:

  • a real part✅ and
  • an imaginary part.👼

They are expressed in the form of a+bi where "a" and "b" are real numbers and "i" is the imaginary unit.

Complex numbers have their uses in many applications related to mathematics.

You don’t need to get too confused😵 about them, in python they are easy to use. Example:-

print(3+1j)
a = (3+1j) # This is also a variable we will discuss them next to next post.
print(type(a))

Output:-

(3+1j)
<class 'complex'>

If you want to read more about complex numbers read this Wikipedia article.

That’s all you need to know about number data types in python.

If you want to read more visit the official website.

Note: This is just a basic level introduction to numbers you’ll understand them deeply as you will use them in solving questions and making projects.

conclusion

In a nutshell, python provides three data types for numbers integer(which includes boolean), float, and complex.

And it auto-detects what type of numbers you are providing.

challenge🧗‍♀️

Your challenge for this post is to add, divide, multiply, and find the remainder after dividing the two numbers provided to you.

Using python, not a calculator.

And your numbers are 8 and 9.

Go fast. I am waiting. Comment your answers below.

This was it for the post. Comment below suggestions if there and tell us whether you liked it or not. Do consider sharing this with your friends.

See you in the next post till then have a great time.😊

Leave a Reply