Number types in python

Python Numbers Types with Examples

Python Number Types with Examples

The Python Numbers data types store numeric values.

Since they are immutable, changing the value of a number data type results in a freshly allocated object.

Python Number objects are produced when a value is assigned to them.

Python supports three types of numbers: complex numbers, integers and floating point numbers (decimals).

Every numbers in Python is an object of the first class. It implies that each numeric data type is implemented via a class definition. Using the type() method, we may determine the data type of a number or the class to which a number belongs.

For instance, Python programming defines entire numbers as integers. The number’s data type can be confirmed as follows.

num1 = 1234 num2 = 12.34 print(type(num1)) print(type(num2))

The first number is an integer, thus the returned data type is class ‘int’ . The second number is a decimal, therefore the returned data type is class ‘float’ .

Now, we will examine each native Python number data types individually.

Number Data Types in Python

There are three number data types in Python:

1. Integer Data Type in Python

Python’s Integer Data Types represent whole numbers like 0 and negative integers like -1 . Defining an integer variable is as simple as assigning it a number.

var1 = 751 var2 = -8712 print (var1) print ("The var1 data type is <>".format(type(var1))) print (var2) print ("The var2 data type is <>".format(type(var2)))
751 The var1 data type is -8712 The var2 data type is

Integer data types in Python do not have a maximum value, unlike other computer languages. You can define a number of any size you choose, but the size is limited by the capabilities of your computer. For example, here we have built a number that contains 90 digits.

# 90 digit integer var1 = 963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714 print ("Here is the 99 digit integer with <> type".format(type(var1))) print (var1)
Here is the 99 digit integer with type 963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714

Additionally, Python offers integer data types for various number systems, such as binary, octal, and hexadecimal.

Читайте также:  Php if variable is an integer

We can represent binary numbers in Python by prefixing the number with 0b , as in 0b1110 , hexadecimal numbers by prefixing the number with 0x , as in 0x1117 , and octal numbers by prefixing the number with 0o , as in 0o1117 .

var1 = 1117 var2 = 0x1117 var3 = 0o1117 var4 = 0b1110 print("Data Type of 1117 is <>".format(type(var1))) print("Data Type of 0x1117 is <>".format(type(var2))) print("Data Type of 0o1117 is <>".format(type(var3))) print("Data Type of 0b1110 is <>".format(type(var4)))
Data Type of 1117 is Data Type of 0x1117 is Data Type of 0o1117 is Data Type of 0b1110 is

You may note that the data type of every number, regardless of the number system, is int. Each binary, octal, and hexadecimal number is an instance of the int class.

2. Float Data Type in Python

Float Data Types in Python is also known as floating-point number. Python employs double-precision floating-point numbers to represent decimal or real numbers.

A number with floating-point precision may be positive, negative, or zero.

Floating-point numbers contain one or more digits after the decimal point, much like decimal numbers.

var1 = 123.14 print (var1) print ("The var1 data type is <>".format(type(var1)))
123.14 The var1 data type is

Floats also representing real numbers, are represented with a decimal point separating the integer and fractional components.

Floats may alternatively be expressed in scientific notation, where E or e represents the power of 10 (2.5e2 = 2.5 x 102 = 250) .

var1 = 35e3 var2 = 12E4 var3 = -87.7e100 print(type(var1)) print(type(var2)) print(type(var3))

3. Complex Numbers in Python

The complex numbers data type is also supported by Python. In Python, a complex number is represented as a + i b, where a and b are numeric values representing the real and imaginary parts of the complex number, respectively.

Читайте также:  Java интервью вопросы ответы

Any English letter may be substituted for the letter i.

var1 = 6+5j var2 = 9j var3 = -7j print(type(var1)) print(type(var2)) print(type(var3))

As seen in the above example, the returned data type is “complex“.

Using the complex() function is another method for defining complex numbers. The complex() function receives the real and imaginary components of the needed complex number and returns a complex number.

The actual component will be passed as the first argument, while the imaginary part will be passed as the second parameter. Additionally, the real part is required while the imaginary part is optional.

var1= complex(3,2) print(var1,type(var1))

The «real» and «imag» characteristics of a complex number provide access to the real and imaginary components, respectively.

var1= complex(3,2) realnum = var1.real imagnum = var1.imag print(var1) print("Real number is ", realnum) print("Imaginary number is ", imagnum)
(3+2j) Real number is 3.0 Imaginary number is 2.0

The conjugate() method can also be used to locate the conjugate of a complex number. The conjugate() method returns the conjugate complex of a complex integer in the following format:

var1= complex(3,2) conjugateNum= var1.conjugate() print(var1) print("Conjugate is", conjugateNum)

Fraction Numbers in Python

Using the fraction numbers module, fractions have been explicitly implemented in Python. Import the fractions module using the import statement below to use the fraction data type in Python.

The Fraction() function of the fractions module can be used to define a fraction object using integers in Python. Two integers are required as input.

The first input is regarded as the numerator, while the second input is regarded as the denominator. After execution, the Fraction() function returns a numerator/denominator fraction object.

import fractions var1 = fractions.Fraction(1,2) print("var1 answer is",var1) print(type(var1))

Using the Fraction() method, you may also convert a floating-point number to a fraction.

import fractions var1 = fractions.Fraction(0.5) print("var1 answer is",var1) print(type(var1))

Mathematical Functions

If you require additional information, please refer to the Python Mathematical Functions Documentation.

Читайте также:  Pandoc html to pdf

Trigonometric Functions

Check the Python Documentation for additional details about Trigonometric Functions.

Mathematical Constants

The list below is the two Mathematical constants in Python.

Random Numbers Functions in Python

Python Documentation has additional information on Random Numbers.

Summary

In summary, you learned about Type conversion of Python Numbers, Mathematical Functions as well as Trigonometric Functions, and Random Numbers functions and you can do a few exercises to put what you learned into practice using our Python Compiler. I hope that this tutorial helped you understand how to use Numbers in Python.

You are one step closer to creating stronger, more effective codes now that you are aware of what Python Numbers are. In your code, start implementing all the functions you have studied today.

In the next post, “Python Strings,” you’ll learn about the different functions of Python strings and their uses.

Источник

Оцените статью