Python - Things to know before proceed - Datatype Tutorial
Every variable in python has a datatype. Python interpreter will automatically detect the data type of variable.
In python, we can also use type() to check the data type of a variable i.e which class a variable belongs to.
Similarly, isinstance() is used to check whether an object belongs to a particular class or not.
a=20
b="Hello Fresherbell"
c=20.339
d=True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
Output
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>
Type Of Datatype in python
- Number
Python numbers are categorized in int, float, and complex
a=20
b=20.339
c=7+8j
print("a is type of ",type(a))
print("b is type of ",type(b))
print("c is type of ",type(c))
print("c is a complex type : ",isinstance(c,complex))
- List
A list is a set of ordered sequences. It is a data type, which is used to store different datatype. The item in the list are separated with a comma “,” and square brackets “[]”.
a = [1, 4.022, "Hello Fresherbell", True]
print(type(a))
print(a)
We can also slice the list using the ' : ' operator. Slicing is used to get a particular value from the list. And its indexing starts from 0.
For example, if we want a value starting from the 2nd index
a = [1, 4.022, "Hello Fresherbell", True]
print(type(a))
print (a[2:])
Output:
<type 'list'>
['Hello Fresherbell', True]
The list can also be concatenated using the + operator
a = [1, 4.022, "Hello Fresherbell", True]
print(type(a))
print (a + a)
Output
<type 'list'>
[1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True]
The list can also be repeated using the * operator
a = [1, 4.022, "Hello Fresherbell", True]
print(type(a))
print (a * 3)
Output:
<type 'list'>
[1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True]
- Tuple
A tuple is similar to a list. It is a set of ordered sequences. It is a data type, which is used to store different datatype. The item in the tuple are separated with a comma “,” and parenthesis“()”.
But in a tuple, we cannot modify the value and data after it is been declared.
a = (1, 4.022, "Hello Fresherbell", True)
print(type(a))
print (a)
Output
<type 'tuple'>
(1, 4.022, 'Hello Fresherbell', True)
Use Of Slicing in the tuple
a = (1, 4.022, "Hello Fresherbell", True)
print(type(a))
print (a[3:])
Output:
<type 'tuple'>
(True,)
Tuple cannot be modify
a = (1, 4.022, "Hello Fresherbell", True)
print(type(a))
print (a)
a[2] = False
It will throw an error
TypeError: 'tuple' object does not support item assignment
Tuple can also be concatenated using + operator
a = (1, 4.022, "Hello Fresherbell", True)
print(type(a))
print (a + a)
Output
<type 'tuple'> (1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True)
Tuple can also be repeated using * operator
a = (1, 4.022, "Hello Fresherbell", True)
print(type(a))
print (a * 2)
Output:
<type 'tuple'>
(1, 4.022, 'Hello Fresherbell', True, 1, 4.022, 'Hello Fresherbell', True)
- Dictionary
Dictionary is a set of unordered key-value pairs of items. It is a data type, which is used to store a specific value with its key. The item in the dictionary are separated with a comma “,” and curly brackets “{}”.
For example
a = {1:"Hello",2:2,3:4.02,4:"fresherbell",5:True}
print(type(a))
print (a)
print (a[3])
Output
<type 'dict'>
{1: 'Hello', 2: 2, 3: 4.02, 4: 'fresherbell', 5: True}
4.02
Dictionary has a pre-defined function for getting keys and values separately
a = {1:"Hello",2:2,3:4.02,4:"fresherbell",5:True}
print(type(a))
print (a.keys())
print (a.values())
Output
<type 'dict'>
[1, 2, 3, 4, 5]
['Hello', 2, 4.02, 'fresherbell', True]
- Boolean
Boolean has two built-in values i.e True and False. It denotes by class bool. It should start with a capital letter
a = True
b = False
print(type(a))
print(type(b))
Output
<type 'bool'>
<type 'bool'>
- String
The string is formed using multiple Unicode characters. It can be used within a single quote or double quote.
a = "Welcome to Python" #for single line we can use '' or ""
print(a)
a = '''Welcome to
Fresherbell''' #for multi line we can use ''' or """
print(a)
Output:
Welcome to Python
Welcome to
Fresherbell
The string is immutable. Therefore we cannot change the value of the string once it's declared.
a = "Welcome to Fresherbell"
a[0]="D"
print(a)
Output:
a[0]="D"
TypeError: 'str' object does not support item assignment
We can also use the slicing operator [] in the string for example
a = "Welcome to Fresherbell"
print(a[0])
print(a[11:])
Output:
W
Fresherbell
- Set
Set is an unordered collection of unique items. It is iterable and mutable. The item in the set are separated with a comma “,” and curly brackets “{}”.
a = {3,"Hello",2,7,"Fresherbell",9}
print(a)
print(type(a))
Output:
{2, 3, 7, 9, 'Fresherbell', 'Hello'}
<class 'set'>
Set eliminate duplicate items in it.
a = {3,3,3,2,2,7,8,8,8,9}
print(a)
print(type(a))
Output:
{2, 3, 7, 8, 9}
<class 'set'>