Data types¶
What is it data type?¶
Data type
is a description of kind, structure and range of values that can be assigned to variable, constant, argument, function result or value.
Data types in Python¶
Every data in Python have some type:
- int - integer number type.
- float - floating point number (real) type.
- complex - complex number type.
- str i bytes - text sequence type.
- bool - boolean data type: true/false.
- NoneType - specific, undefined type, null value.
Integer numbers¶
Usually called integers or ints. They are positive or negative numbers without a decimal part (including 0) with infinity range.
Unlike static programming languages, Python on an ongoing basis, allocates a free space in the memory and assigns a given value to its area to contain a given number with an excess. That's why we can analyse large numbers in Python without worries about memory overflow.
In Python we can store number in decimal or binary system (0b
prefix), octal (0o
prefix), hexadecimal (0x
prefix).
Number 10 in several numerical systems:
>>> 10
10
>>> 0b1010
10
>> 0o12
10
>> 0xA
10
Floating point numbers (real)¶
These are numbers with a decimal value: 1.5, 1.333, -10.0.
Floating point number data type is float. Python uses 8 bytes (64 bits) to store number value, so we can store with accuracy up to 15 digits after comma.
Attention: Integer part is separated by a dot, not a comma. Also you should not use floating point numbers for financial calculation, where accuracy is important. It's because in binary system we cannot set exact value of all numbers, but there's approximate value. You can avoid this using specific libraries i.e. decimal
.
Complex numbers¶
These numbers contain two parts: real and imaginary. They are stored with j
(like in electronic), instead of mathematic i
.
In real these are two number types combination int and float: 2+4j, 5.5-0.4j etc.
Text (string)¶
Values str type are used to store text data, Unicode string. Values of bytes type are text data, that allows you to choose coding format as needed. Strings should be limited by quotes, or apostrophes (you should not use both in the same code, good practice is to use one)
Every single character uses 1 byte.
>>> "string in quotes"
"string in quotes"
>>> 'string in apostrophes'
'string in apostrophes'
>>> """multi lines
... string
... in triple quotes
... or triple apostrophes"""
'multiline\n string\n with triple quotes\n or triple apostrophes'
As you can see, there are several ways to store text. Anyway Python displays strings with apostrophes. Command \n
causing text goes to a new line.
In case we need to use an apostrophe or a quote as a value we need additional characters describing string. Otherwise Python will cut off values. Eventually we can use an escape command \
:
>>> "Block \"Python basics\" is OK!"
'Block "Python basics" is OK!'
Logic type (boolean)¶
This have only one of two values: True
or False
.
Boolean data are often used in condition instructions and loops - we can use them to check if our program should or should not execute.
None type¶
None value is python's way to represent something empty. In other languages we can meet with types like null
or nil
.
Data types in Python: examples¶
This is collection of examples of values using several types of basis data available in Python:
Data type | Value example |
---|---|
int | 0, 1, -3, 128, 4567654324567, 0b111, 0x18C |
float | 0.123, 256.2, -3.14, 1e10 |
complex | (1-2j), (30+15j), 3j |
str | "string", "", "also!@#$%^string", 'single apostrophe' |
bytes | b"byte sequence", b'123456' |
bool | True, False |
NoneType | None |
Several data types display on screen¶
The same as string example from Hello world program, other data types we can use as print
parameter:
print("String to display.")
print(-17)
print(123.4)
print(False)
print(None)
Data type check¶
In Python we can verify, what kind of data are considered using type
function:
>>> type("What type of data it is?")
<class 'str'>
>>> type(10)
<class 'int'>
>>> type(True)
<class 'bool'>
>>> type(100.0)
<class 'float'>
- Function
type
uses parameter in the same way asprint
function. type
returns data type as a value.- We can display data type by joining two known functions:
print(type("What type of data it is?"))
print(type(10.2))
print(type(True))
Type conversion¶
We can convert one data type to another, if that make sense.
int(<value>)
- convert data type <value> to int type, in case when <value> is float type, it cut of fractional partfloat(<value>)
- convert data type <value> to float typestr(<value>)
- convert data type <value> to str typebool(<value>)
- convert data type <value> to bool type - conversion works in this way that following values: empty string (''
), 0, 0.0, (0+0j) are changed toFalse
, all others are changed toTrue
complex(<value>)
- convert data type <value> to complex type
>>> int("5")
5
>>> int(123.456)
123
>>> float(2)
2.0
>>> str(10.0)
'10.0'
>>> bool(0)
False
>>> bool(3.0)
True