1. Numeric Data Types in Python
Python supports numeric data types: integers, floats, and complex numbers. Python defines classes int, float, and complex to store and process numeric data types.
– The integer is represented by the int
class, which contains positive or negative integers (without decimal places). In Python, integers have no length limit.
– The float is represented by the float
class and represents real numbers with floating points (with an accuracy of 15 decimal places). Real numbers can be represented with the sign e or E, which is a scientific notation of a10b. For example, 1.5e2 corresponds to 1.5 x 102.
– The complex number is represented by the complex
class. Complex numbers are written in the form x + yj. Where x is the real part and y is the imaginary part.
In Python, we can represent numbers in binary, hexadecimal (base 16), and octal. We’ll use a prefix before a number to indicate which number system is being represented.
Number System | Prefix |
---|---|
Binary | ‘0b’ or ‘0B’ |
Octal | ‘0o’ or ‘0O’ |
Hexadecimal | ‘0x’ or ‘0X’ |
a = 5
print(type(a))
print("a is an integer number?", isinstance(a, int))
print(type(5.0))
c = 5 + 3j
print(c + 3)
print(type(c))
print("c is a complex number?", isinstance(c, complex))
# Output: 107
print(0b1101011)
# Output: 253 (251 + 2)
print(0xFB + 0b10)
# Output: 13
print(0o15)
Result
<class 'int'>
a is an integer number? True
<class 'float'>
(8+3j)
<class 'complex'>
c is a complex number? True
107
253
13
Converting between Numeric Data Types in Python
When adding, subtracting, multiplying, or dividing an integer with a float, the integer is automatically coerced into a float to perform the calculation. Therefore, the result of the calculation will be a float.
print(1 + 2.0)
print(1 - 2.0)
print(1 * 2.0)
print(1 / 2.0)
Result
3.0
-1.0
2.0
0.5
We can also use built-in Python functions such as int()
, float()
and complex()
to convert between data types.
a = int(2.3)
b = int(-2.8)
c = float(5)
d = complex('3+5j')
print(a)
print(b)
print(c)
print(d)
Result
2
-2
5.0
(3+5j)
Some notes when using Numeric Data Types in Python
When performing calculations between real numbers, we need to pay attention to the accuracy of the result. For example:
a = 1.1 + 2.2
print(a)
print("a == 3.3?", (a==3.3))
Result
3.3000000000000003
a == 3.3? False
The result is not 3.3 but 3.3000000000000003. This is due to the mechanism of storing decimal places in computers using binary digits 0 and 1 (because computers only understand bits 0 and 1).
For example, the decimal number 0.1 is represented in binary as an infinite binary sequence 0.000110011001100110011…. However, our computer only stores a limited number of binary digits to represent the decimal number 0.1. Therefore, any decimal number is relatively accurate.
Using the decimal module in Python
By default, float in Python only has an accuracy of up to 15 decimal places. To determine the number of decimal places of accuracy in Python, we can use the decimal module that has been pre-installed.
import decimal
#return 0.05000000000000000277555756156289135105907917022705078125
print(decimal.Decimal(0.05))
#return 0.05
print(decimal.Decimal('0.05'))
#return
a = 1.1 + 2.2
print(a)
#return
b = decimal.Decimal('1.1') + decimal.Decimal('2.2')
print(b)
Result
0.05000000000000000277555756156289135105907917022705078125
0.05
3.3000000000000003
3.3
When you want to add 1.1 and 2.2 to get an accurate result of 3.3, use the decimal
module with decimal.Decimal('1.1')
and decimal.Decimal('2.2')
.
To control the precision of decimal places, and how many digits should be used, we can use the getcontext()
function with the prec
attribute. For example:
import decimal
# returns 0.07692307692307692307692307692
print(decimal.Decimal(1) / decimal.Decimal(13))
decimal.getcontext().prec = 10
#return 0.07692307692
print(decimal.Decimal(1) / decimal.Decimal(13))
Result
0.07692307692307692307692307692
0.07692307692
2. Boolean Data Type in Python
The Boolean data type in Python has only 2 values: True and False. In Python, True is represented by the value 1 and False is represented by the value 0. For example:
x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10
print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)
Result
x is True
y is False
a: 5
b: 10
3. String Data Type in Python
A string is a sequence of characters enclosed in double-quotes. We can use single quotes, double quotes, or triple quotes for a string. The string data type in Python is represented by the str
class.
In Python, there is no character data type, but a character is a string with a length of 1 character.
strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Result
This is Python
C
This is a multiline string with more than one line code.
Ünicöde
raw \n string
Note: The string u”\u00dcnic\u00f6de” contains Unicode characters. Where \u00dc represents Ü and \u00f6 represents ö. While r”raw \n string” is a raw string.