Python Data Types
In this topics we discuss about different data types we can use in python. Data types are the basic building blocks when constructing large pieces of code. It basically represents the type of data a variable can strore/hold.
Python Number
Like integers, complex number floating point numbers comes under numbers category which are defined as int, float, complex in python
To know the value or a variable belong to which class we can use type() function. And, also isinstance()function can be used to check the value or variable belong to particular class.
There is no predefined limit of integer, it can be limited by memory available.
A floating-point number is accurate up to 15 decimal places. Integer and floating points are separated by decimals points.
Complex number are written is the form, x = yj, where x is the real part and y is the imaginary part. Here are some examples.
a = 5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Output
5 is of type <class 'int'> 2.0 is of type <class 'float'> (1+2j) is complex number? True
Python List
List is an ordered sequence of items. Mostly used datatypes in python and is very flexible. All the item in a list doesn't need to be of same data type.
a = [1, 2.2, 'python']
Declaration of list is simple and items are separated by commas which enclosed within a bracket [ ].
List are mutable, which means the value of elements of a list can be altered.
a = [1, 2, 3]
a[2] = 4
print(a)
Output
[1, 2, 4]
Python Tuple
Tuple is an ordered sequence of items same as a list. The only difference is
that tuples are immutable. Tuples once created cannot be modified.
They are basically used to write protected data and are usually faster than lists as they cannot change dynamically.
It can be defined within parentheses ( ) where are items are separated by commas.
t = (5,'program', 1+3j)
Python Strings
String is sequence of Unicode characters. We can use single quotes or double
quotes to represent strings. Multi-line strings can be denoted using
triple quotes,
'''
or """
.s = "This is a string"
print(s)
s = '''A multiline
string'''
print(s)
Output
This is a string A multiline string
Python Set
Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces
{ }
. Items in a set are not ordered.a = {5,2,3,1,4}
# printing set variable
print("a = ", a)
# data type of variable a
print(type(a))
Output
a = {1, 2, 3, 4, 5} <class 'set'>
We can perform set operations like union, intersection on two sets. Sets have unique values. They eliminate duplicates.
a = {1,2,2,3,3,3}
print(a)
Output
{1, 2, 3}
Since, set are unordered collection, indexing has no meaning. Hence, the slicing operator
[]
does not work.Python Dictionary
Dictionary is an unordered collection of key-value pairs.
It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.
In Python, dictionaries are defined within braces {}
with each item being a pair in the form key:value
. Key and value can be of any type.
d = {1:'value','key':2}
print(type(d))
print("d[1] = ", d[1])
print("d['key'] = ", d['key'])
# Generates error
print("d[2] = ", d[2])
Output
<class 'dict'> d[1] = value d['key'] = 2 Traceback (most recent call last): File "<string>", line 9, in <module> KeyError: 2
0 Comments