Category Archives: Python Data Types

Datatypes in Python

Datatypes in Python

Data type is a set of values, and the allowable operations on those values. Every value has a datatype. Fortunately, we aren’t forced to declare the datatype of our variables because Python is able to keeps tracks of that internally.

Built-in objects in Python

Numbers(Integer, Float, Decimal, Fraction)
Strings
Lists
Dictionaries
Tuples
Files
Sets
Booleans
Types
None
Functions
Modules
Classes
etc.

There are following native types:

integer

An immutable integer of unlimited magnitude.

4

-42

float

An immutable floating point number (system-defined precision).

1.2

1.3333

fraction

An immutable fraction number.

1/2

1/3

complex

An immutable complex number with real and imaginary parts.

3+1j

4+ 2j

string

An immutable sequence of Unicode codepoints.

‘Mirek’

“Mirek”

“””Mirek

jest z

nami.”””

tuple

Immutable, can contain mixed types.

(1,2,3,4,5)

list

Mutable list, can contain mixed types.

[1,2,3,4,5]

dictionary

A mutable associative array of key and value pairs.

{1: "Grzesiek", 'a': "Tania"}

set

Unordered mutable set, contains no duplicates.

set([‘e’, 45, 0.5, “Darek”])

frozenset

Unordered immutable frozenset, contains no duplicates.

frozenset([4.0, 'string', True])

byte

An immutable sequence of bytes.

b'Some ASCII'
b"Some ASCII"
bytes([119, 105, 107, 105])

byte array

A mutable sequence of bytes.

bytearray(b'Some ASCII')
bytearray(b"Some ASCII")
bytearray([119, 105, 107, 105])

boolean

An immutable type with two values: True or False

True

False

None

A type means non existent, not known or empty.

There are other datatypes like module, function, class, method, file, and even compiled code, but they are connected with OOP.

Floats in Python

Floats in Python

Now, let’s see the float methods:

>>> dir(float)
[‘__abs__’, ‘__add__’, ‘__bool__’, ‘__class__’, ‘__delattr__’, ‘__dir__’, ‘__div
mod__’, ‘__doc__’, ‘__eq__’, ‘__float__’, ‘__floordiv__’, ‘__format__’, ‘__ge__’
, ‘__getattribute__’, ‘__getformat__’, ‘__getnewargs__’, ‘__gt__’, ‘__hash__’, ‘
__init__’, ‘__int__’, ‘__le__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__neg
__’, ‘__new__’, ‘__pos__’, ‘__pow__’, ‘__radd__’, ‘__rdivmod__’, ‘__reduce__’, ‘
__reduce_ex__’, ‘__repr__’, ‘__rfloordiv__’, ‘__rmod__’, ‘__rmul__’, ‘__round__’
, ‘__rpow__’, ‘__rsub__’, ‘__rtruediv__’, ‘__setattr__’, ‘__setformat__’, ‘__siz
eof__’, ‘__str__’, ‘__sub__’, ‘__subclasshook__’, ‘__truediv__’, ‘__trunc__’, ‘a
s_integer_ratio’, ‘conjugate’, ‘fromhex’, ‘hex’, ‘imag’, ‘is_integer’, ‘real’]
>>>

They are similar to the integer methods.

float.as_integer_ration

>>> h = 2.5
>>> h
2.5

>>> h.as_integer_ratio()
(5, 2)
>>>

>>> g.as_integer_ratio()
(5998794703657501, 18014398509481984)
>>>

We can know the exact value of a float. This method expresses the value of a float as a fraction. We can recreate the original value:

>>> 5/2
2.5
>>>

>>> 5998794703657501/18014398509481984
0.333
>>>

float.hex()

>>> h.hex()
‘0x1.4000000000000p+1’
>>> g.hex()
‘0x1.54fdf3b645a1dp-2’
>>>

We can express a float in hexadecimal (base 16).

float.fromhex()

>>> h.fromhex(‘0x1.4000000000000p+1’)
2.5

>>> g.fromhex(‘0x1.54fdf3b645a1dp-2’)
0.333

We can restore them.

float.conjugate() -> int

>>> w.conjugate()
4
>>> v.conjugate()
2345
>>>

We can identity on real numbers in the complex numbers.

float.imag

>>> w.imag
0
>>>

>>> v.imag
0
>>>

We can know if our float has an imaginary unit. If not, we’ll get 0.

float.real

>>> w.real
4
>>> v.real
234
>>>

We can know if our float is a real number.

float.is_integer()

>>> h.is_integer()
False
>>> g.is_integer()
False
>>>

>>> f = 10.0

>>> f.is_integer()
True

We can know if our float is integer or not.