Python Data Types
Table Of Contents
- What is a Data Type?
- Python Data Types.
- Numeric.
- String.
- Boolean.
- List.
- Tupple.
- Set.
- Dictionary
(1) What Is A Data Type?
- A data type, in programming, is defined as the type of value that a variable is going to store.
- This type can be of anything like numbers, texts which are of string type, True or False values which are of Boolean types, etc.
- Based on the type of values stored inside the variable, we are able to perform some specific tasks upon it.
- Like we can add, subtract, divide and multiply integer values, but we can’t do these operations on string data types.
(2) Python Data Types.
- Every value in Python has a datatype.
- Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it.
The interpreter implicitly binds the value with its type.
Example
num = 35
message = "Hello World"
percent = 65.89
subjects = ['Physics', 'Chemistry', 'Math']
Note:
- Here the variable holds different values and we did not define its type.
- Python interpreter will automatically interpret variables and their types.
- This is the beauty of Python.
Different Data Types:
- Numbers
- String
- Boolean
- List
- Tuple
- Set
- Dictionary
(3) Numbers Data Type
- In Numbers, there are mainly 3 types which include Integer, Float, and Complex.
- They are defined as int, float, and complex classes in Python.
- In order to find to which class the variable belongs you can use the type () function.
- The isinstance() function is used to check if an object belongs to a particular class.
(a) Integer Data Type
- Integer data types are used to store a whole number without decimals, like 35, -99, or 1345000.
- The integer data type has two categories:
- Signed integers – can store both positive and negative values.
- Unsigned integers – can only store non-negative values
Example
a = 56
print('Type Of a : ',type(a))
b = -99
print('Type Of b : ',type(b))
c = 152365874566666466464
print('Type Of c : ',type(c))
Output:
Type Of a : <class 'int'>
Type Of b : <class 'int'>
Type Of c : <class 'int'>
(b) Float Data Type
- The float data types are used to store decimal point values, like 35.3, -2.34, or 3597.34987.
- It can store positive and negative decimal point values.
Example
a = 89.575
print('Type Of a : ',type(a))
b = -99.45
print('Type Of b : ',type(b))
c = 152365874566666466464.5464
print('Type Of c : ',type(c))
Output:
Type Of a : <class 'float'>
Type Of b : <class 'float'>
Type Of c : <class 'float'>
(c) Complex Data Type
- Complex numbers are written in the form, ( x + yj), where x is the real part and y is the imaginary part.
- We can use complex numbers in scientific calculations.
Example
a = 3 + 7j
print('Type Of a : ',type(a))
b = -(5 + 9j)
print('Type Of b : ',type(b))
c = -10j
print('Type Of c : ',type(c))
d = 0j
print('Type Of d : ',type(d))
Output:
Type Of a : <class 'complex'>
Type Of b : <class 'complex'>
Type Of c : <class 'complex'>
Type Of d : <class 'complex'>
(d) String Data Type
- The string data type is used to store a sequence of characters (text).
- We can use single quotes or double quotes to represent strings.
- Multi-line strings can be denoted using triple quotes, ”’ or “””.
Example
str1 = "string using double quotes"
print(str1,'\n', type(str1))
str2 = '''A multiline
string '''
print(str2,'\n',type(str1))
str3 = 'string using single quotes'
print(str3,'\n',type(str1))
str4 = '123456789'
print(str4,'\n',type(str1))
Output:
string using double quotes
<class 'str'>
A multiline
string
<class 'str'>
string using single quotes
<class 'str'>
123456789
<class 'str'>
(e) Boolean Data Type
- Boolean type provides two built-in values, True and False.
- These values are used to determine the given statement true or false.
- It denotes by the class bool. triple quotes, ”’ or “””.
Example
#Example1
bool1 = True
print(bool1)
#Example2
bool2 = False
print(bool2)
#Example3
print(5 > 3)
#Example4
print(10 < 5)
#Example5
print(type(bool1))
#Example6
print(type(bool2))
Output:
True
False
True
False
<class 'bool'>
<class 'bool'>
(f) List Data Type
- Lists are used to store multiple items in a single variable. A list can contain a series of values.
- The items stored in the list are separated with a comma (,) and enclosed within square brackets [].
- The List can contain data of different types.
A list is mutable, which means we can modify the list.
Example
#Example1
lst1 = [1,2,3,4,5,6]
print(lst1)
#Example2
lst2 = ['a','b','c','d','e','f']
print(lst2)
#Example3
lst3 = ['a','b','c',1,2,3]
print(lst3)
#Example4
lst4 = [45.56,'Hello',(4+5j),True,False]
print(lst4)
print(type(lst1))
print(type(lst2))
print(type(lst3))
print(type(lst4))
Output:
[1, 2, 3, 4, 5, 6]
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 1, 2, 3]
[45.56, 'Hello', (4+5j), True, False]
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
(g) Tuple Data Type
- Tuples are used to store multiple items in a single variable.
- The items stored in the tuple are separated with a comma (,) and enclosed within parenthesis ().
- A tuple is Immutable, which means we can not modify the Tuple.
Example
#Example1
tpl1 = (1,2,3,4,5,6)
print(tpl1)
#Example2
tpl2 = ('a','b','c','d','e','f')
print(tpl2)
#Example3
tpl3 = ('a','b','c',1,2,3)
print(tpl3)
#Example4
tpl4 = (45.56,'Hello',(4+5j),True,False)
print(tpl4)
print(type(tpl1))
print(type(tpl2))
print(type(tpl3))
print(type(tpl4))
Output:
(1, 2, 3, 4, 5, 6)
('a', 'b', 'c', 'd', 'e', 'f')
('a', 'b', 'c', 1, 2, 3)
(45.56, 'Hello', (4+5j), True, False)
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>
(h) Set Data Type
- Python Set is the unordered collection of unique items.
- That means the items in the set are not arranged in a particular order.
The items stored in the set are separated with a comma (,) and enclosed within curly braces { }.
- Set is a mutable data type. That means we add or remove elements from the set.
Example
#Example-1
set1 = {1,1,2,2,3,3,4,4,5,5,6,6}
print(set1)
#Example-2
set2 = {'a','a','b','c','c','d','e','f','f'}
print(set2)
#Example-3
set3 = {45.56,'Hello',(4+5j),True,False}
print(set3)
print(type(set1))
print(type(set2))
print(type(set3))
Output:
{1, 2, 3, 4, 5, 6}
{'e', 'b', 'd', 'c', 'a', 'f'}
{False, True, 45.56, (4+5j), 'Hello'}
<class 'set'>
<class 'set'>
<class 'set'>
(i) Dictionary Data Type
- Dictionaries are used to store data values in key: value pairs.
- A dictionary is a collection that is ordered, changeable, and does not allow duplicates.
- 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.
Example
#Example-1
dict1 = {
1:'India',
2:'America',
3:'Netherland',
4:'France',
5:'Finland'
}
print(dict1)
#Example-2
dict2 = {
'Name':'Subrat',
'Class':'10th',
'Percentage':98.99,
'Subjects':['Physics','Chemistry','Math']
}
print(dict2)
print(type(dict1))
print(type(dict2))
Output:
{1: 'India', 2: 'America', 3: 'Netherland', 4: 'France', 5: 'Finland'}
{'Name': 'Subrat', 'Class': '10th', 'Percentage': 98.99, 'Subjects': ['Physics', 'Chemistry', 'Math']}
<class 'dict'>
<class 'dict'>