Numbers are the numeric data types used in Python. In this lesson, we will focuc on Python Numbers. We will learn this data type with various Python Coding Examples.
There are three different numeric data types used in python. These numberic data types are given below:
int is the abbreviation of integer. Integer numbers can be positive, negative numbers and zero. Integer Examples: 1, 9, 235, -5, -23, 0 etc.
You can check also Python Operators
float is the abreviation of fractional numbers or rational numbers. Again they can be positive, negative or zero. float type can be written with a dot(.). Examples: 0.5, 7.1, 235.67, -66.0, -23.4, 0 etc.
complex is the abbreviation of complex numbers. They can be positive, negative or zero like the other numeric data types. complex numbers can be written with a “j” as the imaginary part. Examples: 2+7j, 8j.
When we assign a numeric value to a variable, this numeric data type is created.
You can check also Python Math Functions
Let’s do an example in which we will define three different numbers and each of them will be a different numeric type. Here, we will print the class of each variable with type method.
The output of this example will be:
Here, be careful about creating floating number. Sometimes we can use “comma(,)” instead of “dot(.)”. If we use comma, then the variable becomes tuple not float. So, we create a different adta type if we use comma instead of dot.
You can also watch the video of this lesson!
Integer is any whole number. It can be a positive whole number, a negative one or 0. This numbers can start from 0 and can go endless through negative numbers and positive numbers.
Let’s do some example to learn integer better. Below, we will create two integer numbers.
Let’s do a simple math operation and learn a + b.
You can also test yourself with Python Questions
Float is any fractional or rational number. It can be a positive rational number, a negative one or 0. This numbers can start from 0 and can go endless through negative numbers and positive numbers.
Let’s do some example to learn float better. Below, we will create two float numbers.
Below, we will add these two float numbers and print both the result and the type of this result.
We can also use “e” with float numbers and create scientific numbers. Here, “e” means that the power of 10.
complex is another type of Python Numbers. They are expressed in the form a + bi , but in pyhon we use a+bj form. It can be a positive complex number, a negative complex number or 0. This numbers can start from 0 and can go endless through negative and positive numbers. “j” is used for the imaginary part of this numbers.
Let’s do some example to learn complex numbers better. Below, we will create two complex numbers.
In the below example, we will use addition math operation with complex numbers.
In this lesson, we have learned Python Numbers with different Python Number types. We have learned integer, float, complex numbers. You can do more practice on these Python Numbers and learn this lesson better.