Python has various built-in data or object types.
The following is the Python's built in data types and their respective literals.
Literals are the expressions that generates the objects.
The Python data types are divided into 2 categories i.e. mutable and immutable types.
In this section we will only discuss few of them (listed below)
Data types | Literals or Expression to create objects |
---|---|
Number | 1234, 3.1415, 5+7j, 0b101 |
String | "pythoneasy", 'pythoneasy', "python's easy" , 'python"s easy', b'c\x02d', u'e\xc4sy' |
Lists | [2], [5, "pythoneasy", 4.5] , list('easy') |
Tuple | (5, "pythoneasy", 4.5), tuple('easy') |
Dictionary | {"name": 'pythoneasy', 'year': 2004}, dict(name="pythoneasy") |
Set | set('pythoneasy'), {'e', 'a', 's','y'} |
In Python number data type objects store numeric data.
Python supports the following numeric data types
Numeric types are created by assigning numeric values to the variables
Examples
x = 10 # int
y = 7.5 # float
z = 2j # complex
Python has a set of built-in functions.
The type() built in function helps to determine the type of the object.
print(type(x))
print(type(y))
print(type(z))
<class 'int'>
<class 'float'>
<class 'complex'>
A string literal is simply a list of characters (textual data) in sequence surrounded by quotes.
A character can be anything like numbers, backslash or letters and can also have space.
For example, "easy"
is a string.
It is a four character long with each characters in sequence "e", "a", "s", "y".
We can assign the same string to a variable as follows.
mystring = "easy" # create a 4 character string objects and assign it to name "mystring"
Example:
string1 = 'It is a single quoted string'
string2 = "It is a double quoted string"
string3 = """It is a triple double quoted string"""
string4 = '''It is a triple single quoted string'''
multiline_string1 = """
This is a
sting in multiple lines
"""
multiline_string2 = '''
This is a
sting in multiple lines
'''
unicode_string = u'e\xc4sy'
The slicing operator [ ] can be user to indexing and slicing a string
Example:
>>> mystring = "easy"
>>> mystring[0] # Indexing - gives 0th item i,e first item
'e'
>>> mystring[1] # Indexing - gives item at 1st index
'a'
>>> mystring[2]
's'
>>> mystring[1:3] # Slicing with index . provides sub string from 1st index element to 3rd-1 th index element
'as'
>>> mystring[-1] # Negative Indexing from right to left
'y'
>>> mystring[-2] # Negative Indexing - 2nd element from right
's'
>>> mystring[-3:-1] # Slicing with Negative Indexing
'as'
Python lists are ordered collections of arbitrarily typed objects, can be written as a list of comma-separated values (items) between square brackets. Lists are mutable type that means they can be changed after assigned.
Example :
>>> mylist = ["pythoneasy", 2016, "english"]
>>> type(mylist)
<class 'list'>
Like strings the slicing operator [ ] can be user to indexing and slicing a list
>>> mylist = ["pythoneasy", 2016, "english"]
>>> mylist[0]
'pythoneasy'
>>> mylist[2]
'english'
# Slicing a list
>>> mylist = ["pythoneasy", 2016, "english", 10.9, 90.23 ]
>>> mylist[2:5]
['english', 10.9, 90.23]
>>>
# Negative indexing
>>> mylist = ["pythoneasy", 2016, "english", 10.9, 90.23 ]
>>> mylist[-1]
90.23
>>> mylist[-3]
'english'
>>> mylist[-4:-1]
[2016, 'english', 10.9]
>>>
Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content using index
>>> cubes = [1, 8, 27, 65, 125]
>>> cubes[3] = 64 # replace the value at index
>>> cubes
[1, 8, 27, 64, 125]
>>>
Tuples in python are similar as Lists. But tuples are immutable where as lists are mutable. That means you can not change the values of tuple once assigned
Tuples can contain any type of data like lists
A tuple consists of a number of values separated by commas or can be created by using parentheses ()
>>> t = 12345, 54321, 'hello!'
>>> t
(12345, 54321, 'hello!')
>>> t = (12345, 54321, 'hello!')
>>> t
(12345, 54321, 'hello!')
>>>
Tuples are immutable
>>> t = 12345, 54321, 'hello!'
>>> t[0] = 88888
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>
A dictionary in Python is a collection of unordered series of “key: value” pairs.
Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys.
Dictionaries are coded in curly braces using key value pairs. The keys should be unique and hashable (immutable) types.
>>> d = {"website": "pythoneasy", "year": 2016} # website, year are the keys and pythoneasy, 2016 are the values
>>> d
{'website': 'pythoneasy', 'year': 2016}
>>> d["website"]
'pythoneasy'
>>> d["year"]
2016
>>>
Using assignment operator and the dictionary name we can update an existing value or add new value with a key
>>> d = {"website": "pythoneasy", "year": 2016}
>>> d["website"] = "python.org" # Change the value of key "website"
>>> d
{'website': 'python.org', 'year': 2016} # The value of website is updated
>>>
A set is an unordered collection with no duplicate elements.A set is mutable data type
Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference
To create a set object we need to use curly braces {}
>>> myset = {"pythoneasy", 2016, "english"}
>>> myset
{'pythoneasy', 2016, 'english'}
>>> type(myset)
<class 'set'>
>>>