Data types, Data types, Data types
what is data types in python, what are data types in python, how many data types in python, what is list data type in python, what is string data type in python?
In previous article we read about basic of python, that what is Python? Why should we learn python, How to execute program first time etc many questions were discussed about python.
So today I am discussing about what is data types in Python?
Data types a important part of programming which is consist in integer values, float, string, list etc.
Data types in Python -
In this article we first learn about basic data types eg. integer, float & string. After that we learn about advanced data types eg. list, tuple & dictionary. So let's get start.....
1. Integers -
Integers are those numbers which is doesn't consist with decimal number. Examples - -2, -1, 0, 1, 2 etc
To declare an integer value in Python,
simply we write variableName = initial value
Example - UserPincode = 123456
2. Float Value -
Float number consist with decimal part such as - 1.20, 1.36, 77.36 etc.
To declare an integer value in Python,
simply we write variableName = initial value
Example - UserHeight - 1.93
3. String -
String belongs to text. To write a text in python is called string, which is written in between double inverted comma(" ") or single inverted commas (' ').
To declare an integer value in Python, simply we write variableName = "initial value" or
variableName = 'initial value'
Example - UserName = "John"
UserAge = "27"
Note - Here we wrote UserAge = "27". Here UserAge is string because it written in double inverted comma. If you wrote userAge = 27 (without quotes), then userAge is an integer value.
Read also - What is Python? Learn basics of Python?
What is type casting in Python?
Sometimes in our program, it is necessary for us to convert from one data type to another data type , such as from an integer value to a string value. This is known as type casting.
Three main type casting function that is
(i) int()
(ii) float()
(iii) str()
Example - Suppose to change a float number to an integer number, we can type int(5.712987). We’ll get 5 as the result (anything after this decimal point is removed).
Advanced data types -
There are advanced data types is list, tuple & dictionary.
1. List - List refers to collection of data which we write in sequence, we can store them as a list.
To declare a list in Python, simply we write
ListName = [initial value].
Note - To write a list in python square bracket [] are necessary & every value should be separated by commas.
ListName = [1, 2, 3, 4]
Individual values in the list are accessible by their indexes, and indexes always start from ZERO, not 1. This is a common practice in almost all programming languages, such as C and Java. Hence the first value has an index of 0, the next has an index of 1 and so forth. For instance,
ListName[0] = 1, ListName[1] = 2, ListName[2] = 3, ListName[3] = 4.
Alternatively we can access the value from end by negative mark(-)
Example -
ListName[-1] = 4, ListName[-2] = 3
2. Tuple in Python -
Tuples are just as like list, but there values can't modified.
To declare a tuple we write tupleName = (initial values).
Note - Tuple would be written in round bracket () & there values are separated by comma.
DayName = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat").
We can access the individual values as like -
DayName[0] = "Sun"
DayName[-1] = "Sat"
3. Dictionary in Python -
Dictionary is a collection of data in pairs. For example, if we want to store the username and age of 5 users, we can store them in a dictionary.
Syntax - dictionaryName = {dictionary key : data}
Example -
OurDictionary = {“Deo”:30, “Andrew”:50, “Mac”:35}
Note - dictionary keys must be unique.
To find the dictionary index or value, write
OurDictionary ["Andrew"] & the output came out
50.
You can also declare a dictionary using the dict( ) method. To declare the OurDictionary, we can write
OurDictionary = dict(Deo= 30, Andrew= 50, Mac =35)
In this method we use round bracket () without double inverted comma.
Conclusion -
I hope you will clearly understood that what are data types in python? And how it work? If you have any question you can ask me in comment. Thank you for your appreciation.
0 Comments