Built-in Types
Booleans
bool: A boolean value of either True or False.
Logical operations like and, or, not can be performed on booleans.
x or y # if x is False then y otherwise x
x and y # if x is False then x otherwise y
not x # if x is True then False, otherwise True
In Python 2.x and in Python 3.x, a boolean is also an int. The bool type is a subclass of the int type and True and False are its only instances:
issubclass(bool, int) # True
isinstance(True, bool) # True
isinstance(False, bool) # True
If boolean values are used in arithmetic operations, their integer values (1 and 0 for True and False) will be used to return an integer result:
True + False == 1 # 1 + 0 == 1
True * True == 1 # 1 * 1 == 1
Numbers int: Integer number
a = 2
b = 100
c = 123456789
d = 38563846326424324
Integers in Python are of arbitrary sizes.
Note: in older versions of Python, a long type was available and this was distinct from int. The two have been unified.
float: Floating point number; precision depends on the implementation and system architecture, for CPython the float datatype corresponds to a C double.
a = 2.0
b = 100.e0
c = 123456789.e1
complex: Complex numbers
a = 2 + 1j
b = 100 + 10j
The <, <=, > and >= operators will raise a TypeError exception when any operand is a complex number.
Strings Python 3.x Version ≥ 3.0
Python differentiates between ordered sequences and unordered collections (such as set and dict).
b = ('a', 1, 'python', (1, 2))
b[2] = 'something else' # returns a TypeError
Supports indexing; immutable; hashable if all its members are hashable
b = ['a', 1, 'python', (1, 2), [1, 2]]
b[2] = 'something else' # allowed
Not hashable; mutable.
Booleans
bool: A boolean value of either True or False.
Logical operations like and, or, not can be performed on booleans.
x or y # if x is False then y otherwise x
x and y # if x is False then x otherwise y
not x # if x is True then False, otherwise True
In Python 2.x and in Python 3.x, a boolean is also an int. The bool type is a subclass of the int type and True and False are its only instances:
issubclass(bool, int) # True
isinstance(True, bool) # True
isinstance(False, bool) # True
If boolean values are used in arithmetic operations, their integer values (1 and 0 for True and False) will be used to return an integer result:
True + False == 1 # 1 + 0 == 1
True * True == 1 # 1 * 1 == 1
Numbers int: Integer number
a = 2
b = 100
c = 123456789
d = 38563846326424324
Integers in Python are of arbitrary sizes.
Note: in older versions of Python, a long type was available and this was distinct from int. The two have been unified.
float: Floating point number; precision depends on the implementation and system architecture, for CPython the float datatype corresponds to a C double.
a = 2.0
b = 100.e0
c = 123456789.e1
complex: Complex numbers
a = 2 + 1j
b = 100 + 10j
The <, <=, > and >= operators will raise a TypeError exception when any operand is a complex number.
Strings Python 3.x Version ≥ 3.0
Python differentiates between ordered sequences and unordered collections (such as set and dict).
- strings (str, bytes, unicode) are sequences
- reversed: A reversed order of str with reversed function
- tuple: An ordered collection of n values of any type (n >= 0).
b = ('a', 1, 'python', (1, 2))
b[2] = 'something else' # returns a TypeError
Supports indexing; immutable; hashable if all its members are hashable
- list: An ordered collection of n values (n >= 0)
b = ['a', 1, 'python', (1, 2), [1, 2]]
b[2] = 'something else' # allowed
Not hashable; mutable.
- set: An unordered collection of unique values.
- dict: An unordered collection of unique key-value pairs;
keys must be hashable.
a = {1: 'one',
2: 'two'}
b = {'a': [1, 2, 3],
'b': 'a string'}
An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__()
method), and can be compared to other objects (it needs an __eq__() method). Hashable objects which
compare equality must have the same hash value.
Converting between datatypes
You can perform explicit datatype conversion.
For example, '123' is of str type and it can be converted to integer using int function.
a = '123'
b = int(a)
Converting from a float string such as '123.456' can be done using float function.
a = '123.456'
b = float(a)
c = int(a) # ValueError: invalid literal for int() with base 10: '123.456'
d = int(b) # 123
You can also convert sequence or collection types
a = 'hello'
list(a) # ['h', 'e', 'l', 'l', 'o']
set(a) # {'o', 'e', 'l', 'h'}
tuple(a) # ('h', 'e', 'l', 'l', 'o')
Data types whose instances are mutable are called mutable data types, and similarly for immutable objects and
datatypes.
Examples of immutable Data Types:
- int,
- long,
- float,
- complex
- str
- bytes
- tuple
- frozenset
Examples of mutable Data Types:
- bytearray
- list
- set
- dict
Comments :
Post a Comment