contact@eeehub.in +919470012104

Data Types in Python

Data Types: A data type refers to the format of data that can hold a distinct type or range of values. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.


Python has five data types:


  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Python Numbers:


Number data type in python store the numeric value. It could be integer, float or complex. We can also perform mathematical operation like addition, subtraction, multiplication and division.

We can do data conversion. But there are some exceptions. Complex data could not convert into float or integer.

Python 3.8.0 (tags/v3.8.0:fa3219fd, Oct 14 2019, 19:21:23) [MSC v.1316 32 bit (Intel)] on win64
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 12 # Stores integer value
>>> b = 12.5 # Stores float value
>>> c = 3 + 5j # Stores complex value
>>> a = b + a # Addition of 12 + 12.5
>>> print(a)
24.5
>>> a = a - 2 # Subtracting 2 from 24.5
>>> print(a)
22.5
>>> a = a * 2 # Multiply a with 2
>>> print(a)
45.0
>>> a = a / 15 # Divide a by 15
>>> print(a)
3.0
>>> # Conversion of data
...
>>> d = int(a) # Converting float 3.0 to integer and store it in d variable
>>> print(d)
3
>>> d = float(23) # Converting integer data into float
>>> print(d)
23.0
>>> d = complex(2) # Converting integer data to complex
>>> print(d)
(2+0j)
>>> d = complex(2.3) # Converting float data to complex
>>> print(d)
(2.3+0j)
>>> d = int(2 + 4j) # Converting complex data to int (It will throw error)
Traceback (most recent call last):
File "", line 1, in
TypeError: can't convert complex to int
>>> d = float(2 + 3j) # Converting complex data to float
Traceback (most recent call last):
File "", line 1, in
TypeError: can't convert complex to float


Python String:


String: It is define as the series of characters. It could be series of alpha-bates , numbers, special characters and space under single (' ') or double (" ") quotes.

Indexes starting at 0 in the beginning of the string. Subsets of strings can be taken using the slice operator ([:] ). The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator.

All the numbers data type can be converted into string data type. But string only have integer, float or complex can be convert into numbers data type.

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'Hello' # A variable store string under single quotes
>>> b = "EEEHUBIans" # A variable store string under double quotes
>>> type(a)
Class 'str'
>>> type(b)
Class 'str'
>>> a[0] # A is storing a string "Hello". 0 is index of first character of string.
'H'
>>> a[1] # a is storing a string "Hello". 1 is index of second character of string.
'e'
>>> a[5] # a is storing a string "Hello" it indexes is 0, 1, 2, 3, 4 but here as an argument we are passing 5
Traceback (most recent call last):
File "", line 1, in
IndexError: string index out of range
>>> a[:] # Here we are using slicing operator
'Hello'
>>> a[0:3] # variable_name[first_index
'Hel'
>>> a[0:3] # variable_name[first_index:second_index] First index will be inclusive and second index will be exclusive
'Hel'
>>> a + b
'HelloEEEHUBIans'
>>> a + ' ' + b
'Hello EEEHUBIans'
>>> a * 2
'HelloHello'
>>> a * 5
'HelloHelloHelloHelloHello'



>>> a = "3"
>>> type(a)
class 'str'
>>> a = int(a)
>>> type(a)
class 'int'
>>> a = "3i"
>>> type(a)
class 'str'
>>> a = int(a)
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '3i'


Python Lists:


A list contains items separated by commas and enclosed within square brackets ([]).

It is similar as array in C but it can hold different data type in it like float, integer, string, complex, tuples and so on. Indexes starting at 0 in the beginning of the list. Subsets of list can be taken using the slice operator ([:]).

The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> book_list = ['Science','Maths','Hindi','English']
>>> type(book_list)
class 'list'
>>> print(book_list)
['Science', 'Maths', 'Hindi', 'English']
>>> print(book_list[0])
Science
>>> print(book_list[1])
Maths
>>> print(book_list[1:3]
... )
['Maths', 'Hindi']
>>> print(book_list + book_list)
['Science', 'Maths', 'Hindi', 'English', 'Science', 'Maths', 'Hindi', 'English']
>>> user_detail = ['Sam', 'sam@eeehub.in', 21]
>>> type(user_detail)
class 'list'
>>> user_detail[1]
'sam@eeehub.in'


Python Tuples:


A tuple consists of a number of values separated by commas enclosed under parenthesis(). It is similar to list but tuples can't updated.Tuples can be thought of as read-only lists.


Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> book_tuple = ('Englis','Hindi')
>>> type(book_tuple)
class 'tuple'
>>> print(book_tuple)
('Englis', 'Hindi')
>>> print(book_tuple[0])
Englis
>>> print(book_tuple[0:1])
('Englis',)
>>> print( book_tuple + book_tuple)
('Englis', 'Hindi', 'Englis', 'Hindi')

Now, we will try to update the tuple and list let see what we get in the output.


Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> book_list = ['English','Hindi']
>>> book_tuple = ('Science','Maths')
>>> type(book_list)
class 'list'
>>> type(book_tuple)
class 'tuple'
>>> book_list[0] = 'SST'
>>> book_tuple[0] = 'SST'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment
>>> print(book_list)
['SST', 'Hindi']
>>> print(book_tuple)
('Science', 'Maths')


Python Dictionary:


Python dictionary is key:value pair. A dictionary key can be almost any Python type, but are usually numbers or strings.Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> book_dic = {1:'English',2:'Hindi',3:'SST'}
>>> type(book_dic)
class 'dict'
>>> book_dic[1]
'English'
>>> book_dic[0]
Traceback (most recent call last):
File "", line 1, in
KeyError: 0
>>> book_dic.keys()
dict_keys([1, 2, 3])
>>> book_dic.values()
dict_values(['English', 'Hindi', 'SST'])