< Prev
Next >

# Python - Data Types

In Python, each value has a specific data type and each data type fall into a specific type category, such as -
• Numbers
• Sequences
• Mapping Sets
Each of these type categories represents the various built-in data types belonging to it. Let us take a close look at each of these type categories and their associated built-in data types.

• ## Type Category - Numbers

• There are four most used built-in data types that belong to the Numbers type category, such as -
• int - represents signed and unsigned integer values up to any length, defined by the int class.
• float- represents the floating point numbers with its accuracy up to 15 decimal places, defined by the float class.
• complex - represents the complex numbers, defined by the complex class.
• bool - represents the boolean values i.e. True or False, defined by the bool class.

Type Category Data Type Description
Numbers int Used to represent integer numbers
Numbers float Used to represent floating point numbers
Numbers complex Used to represent complex numbers
Numbers bool Used to represent boolean values - True or False

Note: Apparently, we can use the type() function, to know the class type of a specific value or isinstance() function, to know if an object belongs to a specific class.

An example of values under the Number type category
``````# Creating an integer object of type int
x = 9
print(x, 'is of type', type(x))

# Creating an floating-point object of type float
y = 19.99
print(y, 'is of type', type(y))

# Creating an complex number object of type complex
z = 9+2j
print(z, 'is of type', type(z))

# Creating an boolean object of type bool
b = True
print(b, 'is of type', type(b))``````

## Output

``````9 is of type
19.99 is of type
(9+2j) is of type
True is of type ``````

• ## Type Category - Sequences

• The three most used built-in data types that belong to the Sequences type category are -
• str - represents Unicode string i.e. a sequence of characters, defined by the class, str. In Python, in order to create a string object, we can enclose a string literal i.e. a string of characters, within delimiters, such as -
1. A pair of single quotes.
2. A pair of double quotes.
3. A pair of triple single quotes.
4. A pair of triple-double quotes.

Note: A string literal enclosed within single or double quotes can only span across a single line, whereas, a string literal enclosed within triple single or triple double quotes can span across multiple lines.

• list - represents a mutable ordered sequence of items, declared within a pair of square brackets [ ], defined by the class, list.

• tuple - represents an immutable ordered sequence of items, declared within a pair of parenthesis ( ), defined by the class, tuple.

Type Category Data Type Description
Sequences str Used to represent strings
Sequences list Used to represent a mutable ordered sequence of items
Sequences tuple Used to represent an immutable ordered sequence of items

An example of values under Sequences type category
``````# Creating an string object of type str
s = 'Hello World!'
print(s, 'is of type', type(s))

# Creating an list object of type list
misc =[19 ,-9, 9.99, 'Have a perfect day!']
print(misc, 'is of type', type(misc))

# Creating a tuple object of type tuple
customer =('Edward', 27, 178.5, 'M')
print(customer, 'is of type', type(customer))
``````

## Output

``````Hello World! is of type
[19, -9, 9.99, 'Have a perfect day!'] is of type
('Edward', 27, 178.5, 'M') is of type ``````

• ## Type Category - Mapping Sets

• The two most used built-in data types that belong to the Mapping Sets type category are -
• set - represents an unordered sequence of items, declared within a pair of curly braces { }, defined by the class, set.
• dict - represents a dictionary of an ordered sequence of items stored in the form of key:value pairs, where key and value are objects, declared within a pair of curly braces { }, It is defined by the class, dict.

• Type Category Data Type Description
Mapping Sets dict Used to represent sequence of items stored in the form of key:value pairs, where key and value are objects.
Mapping Sets set Used to represents an unordered sequence of items

An example of values under Mapping Set type category
``````# Creating an dict object with key-value pairs of type dict
dict1 = {
4:'Fourth',
3:'Third',
'two':'Second',
'one':'First',
5:'Fifth'
}
print(dict1, 'is of type', type(dict1))

# Creating an set object of type set
set_ex ={'Never give up', 19.2, -29, 'A'}
print(set_ex, 'is of type', type(set_ex))``````

## Output

``````{4: 'Fourth', 3: 'Third', 'two': 'Second', 'one': 'First', 5: 'Fifth'} is of type
{'A', 19.2, 'Never give up', -29} is of type ``````

< Prev
Next >
< The First Python Program in IDLE
Python keywords and identifiers >

Please subscribe to our social media channels for daily updates.