Integers are numbers without a decimal point. Integers in Python including positive, 0 and negative integers. Python has only one type of integer and Python integers have an infinite range of values then Python can handle them easily, no matter the size of value.
<#Assign 63 to the variable n (use anaconda) n = 63 print (n) print (type (n)) #Assign a large integer to x x = 9999999999999999999999 print (x) print (type (x)) #Assign a small integer to y y = -9999999999999999999999 print (y) print (type (y))
The output is:
63
[class 'int']
9999999999999999999999
[class 'int']
-9999999999999999999999
[class 'int']
x is a very large number while y is a very small number. Python can output it correctly which means that Python’s ability to handle integers is very powerful. No matter how big or small integers, Python uses only one type for storage, which is integer.
In Python, integers can be represented in multiple formats:
No. | Formats | Details |
---|---|---|
1 | Decimal format | Normally, usual integer is the decimal form, which is composed of a total of ten numbers from 0 to 9. Note that integers in decimal form cannot start with 0 unless the value itself is 0. |
2 | Binary format | It consists of only two numbers, 0 and 1. Normally it is written as 0b or 0B. |
3 | Octal format | An octal form consists of eight digits, from 0 to7. Normally it is written as 0o or 0O. Note that integers in decimal form cannot start with 0 unless the value itself is 0.. |
4 | Hexadecimal format | It consists of ten numbers from 0 to 9 and six letters from A to F (or a to f). When writing, it starts with 0x or 0X. |
Examples of different integers format in Python:
#Binary binary_1 = 0b101 print ('binary_value1:', binary_1) binary_2 = 0B110 print ('binary_value2:', binary_2)
The output is:
binary_value1: 6
binary_value2: 2
#Hexadecimal hexa_1 = 0x45 hexa_2 = 0x4Af print ("hexa_value1:", hexa_1) print ("hexa_value2:", hexa_2)
The output is:
hexa_value1: 50
hexa_value2: 1469
#Octal octal_1 = 0o26 print ('octal_value1:', octal_1) oct2 = 0O41 print ('octal_value2:', octal_2)
The output is:
octal_value1: 51
octal_value2: 21
To easier user to read the number, Python 3.x allows the underscore (_) as a separator for numbers (including integer and decimals). The values of the number itself do not affect by the separator – underscores (_). It is like commas in English numbers. The underscore replaces commas in Python 3.x.
Examples:
click = 11_311_547 distance = 283_000_000 print ("Python Tutorial Reading:", click) print ("The distance between the A and B:", distance)
The output is:
Python Tutorial Reading: 11311547
The distance between the A and B: 283000000
In programming languages, decimals point number are usually stored as floating-point numbers. Floating-point and fixed-point numbers are opposite: if the decimal point moves during storage, it is called a floating-point number; if the decimal point does not move, it is called a fixed-point number.
No. | Formats | Details |
---|---|---|
1 | Decimal format | To include a decimal point when writing decimals, otherwise Python will treat them as integer. The example of decimal format: 35.6, 356.0, and 0.356. |
1 | Exponential format | It means a numeric form involving exponents. For example: 2.3E6, 3.8E-3, etc. If it is written in exponential format, it is considered as a decimal type even if its final value looks like an integer. |
C language has two types of decimals, they are float and double type. Float type can store a small range of decimals while double can store a large range of decimals.
But Python has only one decimal type, which is float type. Let’s refer to the following example of decimals type in Python:
f_1 = 12.5 print("f1Value: ", f_1) print("f1Type: ", type(f_1)) f_2 = 0.34557808421257003 print("f2Value: ", f_2) print("f2Type: ", type(f_2)) f_3 = 0.0000000000000000000000000847 print("f3Value: ", f_3) print("f3Type: ", type(f_3)) f_4 = 345679745132456787324523453.45006 print("f4Value: ", f_4) print("f4Type: ", type(f_4)) f_5 = 12e4 print("f5Value: ", f_5) print("f5Type: ", type(f_5)) f_6 = 12.3 * 0.1 print("f6Value: ", f_6) print("f6Type: ", type(f_6))
The output is:
f1Value: 12.5
f1Type: [class 'float']
f2Value: 0.34557808421257003
f2Type: [class 'float']
f3Value: 8.47e-26
f3Type: [class 'float']
f4Value: 3.456797451324568e+26
f4Type: [class 'float']
f5Value: 120000.0
f5Type: [class 'float']
f6Value: 1.2300000000000002
f5Type: [class 'float']
Conclusion: From the result, Python can accommodate very small and very large floating-point numbers. When printing out the floating-point numbers, some numbers are rounded up/ down based on the length or size of the floating-point numbers. The value of f_5 is 120 000, but it is still a decimal type but not an integer type
The result of f_6 supposed to be 12.3 * 0.1 = 1.23. But the output of print result is not accurate. This is because decimals are stored in binary form in memory. The numbers after the decimal point is likely to be a series of infinite loop numbers when converted to binary form. Thus, it cannot calculate the number accurately.
Complex type is a built-in type in Python, you do not have to import any Python packages. In other words, Python programming language supports complex type without relying on any third party libraries.
A complex number consists of a real part and an imaginary part. In Python, the imaginary part of complex number is suffixed by j or J. The format is:
a + bj
a is the real part and b is the imaginary part.
Examples:
c_1 = 22 + 0.3j print ("c1Value:", c_1) print ("c1Type", type (c_1)) c_2 = 5-1.3j print ("c2Value:", c_2) #Simple calculation of complex numbers print ("c_1 + c_2:", c_1 + c_2) print ("c_1 * c_2:", c_1 * c_2)
The output is:
c1Value: (22+0.3j)
c1Type: [class 'complex']
c2Value: (5-1.3j)
c_1 + c_2: (27-1j)
c_1 + c_2: (110.39-27.1j)
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises