The following table lists the predefined data types in Python.
| Data Type |
| Numeric String Sequence Binary Mapping Boolean Set None |
| Example |
int, float, complex |
To get the type of a variable, we use the type() function.
Python has four predefined numeric types:int (signed integer)bool (boolean, subtype of int)float (real number)complex (complex number)
Example
var1 = 1 # int: integer variable
var2 = True # bool: boolean variable
var3 = 1.2 # float: real variable
var4 = 1 + 2j # complex: complex variable
Sequence is a collection data type.
It is an ordered collection of elements.
The elements of the sequence have a position index starting with the value 0.
The sequence type is conceptually similar to the array type.
A Python sequence is bounded and iterable.
Python has three sequence data types:
strlisttuplestr TypeA variable of type str (string) is a string of characters delimited by single, double or triple quotes.
Access
The elements of a variable of type str are accessed using the [ ] and [:] operators.
Operations
The concatenation operator is the symbol + .
The repetition operator is the symbol * .
Example
greet = 'Hello'
print (greet)
print (greet[0])
print (greet[2:4])
print (greet[2:])
print (greet * 2)
print (greet + " World!")
list TypeA list type variable consists of elements separated by commas and delimited by square brackets [ ].
The elements of a list can be of different types.
Nested list
A list can have one element of type list.
Access
The elements of a variable of type list are accessed using the [ ] and [:] operators.
Operations
The concatenation operator is the symbol + .
The repetition operator is the symbol * .
Example
data = ['Primes', 2, 3, 5, 7, 11]
print (data)
print (data[0])
print (data[1:3])
print (data[1:])
tuple TypeA tuple variable consists of elements separated by commas and delimited by parentheses ( ).
The elements of a tuple can be of different types.
Nested tuple
A tuple can have an element of type tuple or list.
Unlike a variable of type list, a variable of type tuple cannot be modified.
A variable of type tuple can be considered as a variable of type list that is read-only.
Example
data = ('Primes', 2, 3, 5, 7, 11)
print (data)
print (data[0])
print (data[1:3])
print (data[1:])
dict TypeA dict (dictionary) variable is a collection of key:value pairs.
These pairs are separated by commas and the sequence is delimited by curly braces { }.
To establish the correspondence between the elements constituting the key and value pair, we use the symbol : (colon.)
To access the values of a dict variable, we use the brackets [ ].
Example
data = {
'com' : 'Commercial',
'org' : 'Organization',
'net' : 'Network'
}
print (data)
print (data['com'])
print (data.keys())
print (data.values())
set TypeThe set type is an implementation of the mathematical concept of a set.
A variable of type set consists of a collection of elements delimited by curly braces { }, the elements being separated by commas.
The collection is unindexed and unordered.
The elements can be of different types.
Each element of the collection is enumerated once and only once.
An element must be a non-mutable object such as a numeric object (int, bool, float, complex), str or tuple.