Python Numbers

pyhton-numbers-ipcisco-1

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
  • float
  • complex

 

int is the abbreviation of integer. Integer numbers can be positive, negative numbers and zero. Integer Examples: 1, 9, 235, -5, -23, 0 etc.

 

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.

 

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.

 

a = 5                    # integer
b = 5.3                 # floating number
c = 5j                    # complex number
print(type(a))
print(type(b))
print(type(c))

 

The output of this example will be:

 

<class 'int'>
<class 'float'>
<class 'complex'>

pyhton-numbers-ipcisco-1

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!

 


 

Python Numbers : Int Data Type

 

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.

 

a = 5
b = -235
print(type(a))
print(type(b))
<class 'int'>
<class 'int'>

pyhton-numbers-int-ipcisco-2

Let’s do a simple math operation and learn a + b.

 

a = 5
b = -235
c = a + b
print(c)
print(type(c))
-230
<class 'int'>

 


 

Float Data Type

 

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.

 

a = -3.3
b = 123.3
print(type(a))
print(type(b))
<class 'float'>
<class 'float'>

pyhton-numbers-float-ipcisco-3

Below, we will add these two float numbers and print both the result and the type of this result.

 

a = -3.3
b = 123.3
c = a + b
print(c)
print(type(c))

 

120.0
<class 'float'>

pyhton-data-types-numbers-ipcisco-4

We can also use “e” with float numbers and create scientific numbers. Here, “e” means that the power of 10.

 

a = -3.3e10
b = 123.3E10
print(type(a))
print(type(b))
<class 'float'>
<class 'float'>

 


 

Complex Data Type

 

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.

 

a = 3+5j
b = -5j
print(type(a))
print(type(b))

 

<class 'complex'>
<class 'complex'>

pyhton-data-types-numbers-complex-ipcisco-5

In the below example, we will use addition math operation with complex numbers.

 

a = 3+5j
b = -5j
c = a + b
print(c)
print(type(c))

 

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

 

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.

Back to: Python Programming Course > Python Math

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k