Python is one of the most preferred programming languages these days. It allows developers to focus all their efforts on implementation instead of complex programs and data types in python are used for that purpose only to make our work easy.
The classification or categorization of data items is known as Data types. A kind of value is signified by data type which determines what operations need to be performed on that particular data.
Now the thing to remember is by default the answer we will get will be in decimal forms, so we can change that as well.
a=10
b=20
c=a>b
print(c)
print(type(c))
The process of converting from one type to another type. 5 inbuilt functions are there-
int(“10.5”) , again error as it is not integral value it is a float value.
Int to float: float (15) answer will be 15.0
Or float(0B1111) will give 15.0
Or Float(0XFace) will give 64206.0
Complex to float is not possible :
Bool to float is possible
If we convert float like
float(True), then the answer would be 1.0
float(False), then the answer would be 0.0
String to float type is also possible, only in the case when the string contains an integral value or float value but they should be specified in base 10.
Float (10) will give 10.0
Float (0XFACE) error
Float (“Aashish”) error
Complex (x)
If only one argument then it will become a real value
Complex (10)
The answer would be 10 + 0j
Complex (0B1111) would be 15+0j
Complex (10.5) would be 10.5 + 0j
Complex (True) would be 1+0j
Complex (False) would be 0j
Complex (“String”) it should have either int or float value in base 10
Complex (x,y)
When two arguments are there one will go to real and one to the imaginary complex(10, 20) would be 10+20j.
Complex (10.5, 20.6) would be 10.5 + 20.6j
Complex (“10”, “20”)
It will give an error if we are having the string argument, we can’t have the second value as a string as well
Complex (10, “20”)
It will also give an error, the second argument can’t be a string in complex
Int argument
bool(10)
If the argument is 0 , then it will be false whether it is int, float, complex, etc.
If it is non zero then it will be True
Int
bool(10) will give True
bool(0) will give false
Float
bool(0.0) false
bool(0.1) true
Complex
bool(0+0j) False
bool(1+0j) True
String
bool(“True”) True
bool(“False”) True
bool(“Yes”) True
bool (“No”) True
bool( “ ”) False
If the string is empty then only it is false, otherwise if it not empty then it will be True
str(10) “10”
str(0B1111) “15”
str(10.5) “10.5”
str(10+20j) “10+20j”
str(True) “True”
str(False) “False”
Once we create an object, we cannot perform any changes in that object. If we try to change, then the new object would be created. This non-changeable behavior is immutability.
These are the collection related data type.
l=[07, “Aashish”, 20,30, “Nimish”] Print type(l)
It will give a list. The list is always in square brackets. If we print (l), the order will be in the same in which we have given. So the order of perseverance is there. Duplicates objects are allowed.
Heterogenous objects are allowed, like int, string, etc. Indexing and slicing are applicable. Now,
print (l[0])
So will give the first element. And similarly -1 for the last. Now if let say we have an empty list.
l=[ ]
So we can add elements like-
l.append(10)
We can remove elements like-
l.remove(10)
Now let say, we have-
l=[10, 20, 30, 40]
Now the list is mutable so at l[0] we can add value. Like-
l[0]= 100
So now 100 will be added to the starting. List will become l[100, 10, 20, 30, 40].
Now we will learn the data type Tuple. It is the same as the list except it is immutable, if we create one object so we cannot add or remove any object or value. It is like a read-only version of a list. We represent tuple by (10, 20,”aashish”).
Performance is better than the list. And it also uses less memory than a list.
Now we will learn about the new data type i.e Set. It is used for the group of objects or entities but duplicates are not allowed and I don’t care about the order. So if we have such a type of requirement then we go for the Set.
So-
s1 = {1,2,3} s2 = {3,1,2}
So basically s1 and s2 are equal. So in the set, there is no first element no last element kind of concept. Curly brackets are used inset.
s= {10, 10, 20, “Aashish”, 30}
But output will come s= {10, 20, “Aashish”, 30}
But order can be anything. So indexing cannot be there or slice operator-
Print s[0]
Will give an error.
Heterogeneous objects are allowed.
s.add(50)
In the list, it was appended. In set, it is added.
Append was used in the list to add items in the last. So add is used in the set, as we don’t know where it is going to get added.
s= { }
So it is not known as an empty set it is known as an empty dictionary. As the dictionary gets privilege because of the frequent use of it more than the set.
If we want to create the empty set then we can create it like
s= set()
It is exactly like a set except it is immutable. Because inset we can make changes, but not in the frozen set. So we create the frozen set like-
s={ 10, 20, 30, 40} fr= frozenset(s) Print type(fr)
It will be frozen set s.
So now we cannot add or remove the values from the frozen set and will get the attribute error if we try to do that.
Now we will learn about the Range.
R = range(10)
So we will get 10 values starting from the 0. So basically it is an inbuilt function used in python. If we print r-
It will give 0,1,2,3,4,5,6,7,8,9
We can use it like
For x in r : print(x)
So we will get all the values printed. How we can make the range objects.
Form: range (n)
So will give values to 0 to n-1
Form : range(begin : end)
So will give values from the beginning to the n-1
r= range(1, 10) For x in r : Print (x)
So we will get 1,2,3 …. 9
Form:
Range (begin, end, increment/Decrement)
R = range(1,21,1) So 1 to 20
1,2,3,….. 20
R = Range(1, 21,2 )
1,3,5… 19
Also, we can do like for decrement
Range (20, 1, -2)
So 20, 18 ….
So now we have known things go in order in range, so wherever order is there, we can do the indexing or slicing. But the range is immutable, as the values are in sequence and if we try to add the values, an error will occur.
Now we will learn about the dictionary. Dictionary is different from the above data types in the manner that it is used to represent the key values. Represent by Dict
d={ k1: v1, k2 : V2}
So let say we have an empty dictionary
d= { }
So we can add value like
d[100]=”Aashish” d[200]= “Ayush”
So it will look like-
{ 100 : Aashish , 200 : Ayush}
Now there would be one concern about the duplicity of the values. So duplicate keys are not allowed but values can be duplicated. S-
d[100] = “Abhishek”
So the old value i.e Aashish will be replaced by the Abhishek, so the duplicate key is not allowed, anyhow there would be no error, but the value will get replaced by the new value.
No order there, similarly like a set. Heterogeneous values can be there. It is mutable, we can make changes. Slicing, indexing, etc are not there, due to no order.
l= [10, 20,30,40]
Till now it is a list. Now to convert it into the bytes we need to
Do-
b=bytes(l)
print(b)
Now It is used for the binary creation of the data. Bytes can only have values from 0 to 256. So basically if we add the value 256, 257 …
We will get the value error. Now in this as well indexing, slicing is there. But it is immutable, we cannot change its content. We will get an error. But if we want mutable bytes then it is known as Bytearray
l= [ 10, 20, 30, 40 ] b=bytearray(l)
b[0]= 77 print(b[0])
None data type is nothing i.e no value is associated with it. To make the value available for the garbage collection-
a=None
None is also stored as object internally-
Print (type(a))
These are –
\ n : for the next line
\ t : for the tab space
\ r : carriage return, means starting from the starting
\ b : backspace
\ f : form feed
\ ‘ : for single quote
\ “ : for double quote
\\ : for backslash symbol
Now to type the comments in python :
// This is a single line comment
# This is a single line comment
So to type multi-line comments
We need to put # in every line.
Constants :
The things of which we cannot change the values
So as such no constant concept is there in python, but if we want the value to be constantly put it in the upper case.
MAX_VALUE=10
For the convention purpose only.
So we learned that Datatypes are an important concept because statistical methods and certain other things can only be used with a certain python data type. You have to analyze data differently and then categorize data otherwise it would result in a wrong analysis.
Each and every part you have explained is quite clear... very well written
Every python lover must read this.. Aashish sir you are doing a great job ...thankyou for enhancing our knowledge on Python.
Very nice...