Exploring Data Types of python
As we practiced the python installation and get to started to used python in development and we understood the variable declaration in python in my last article. If you have not checked till now then I would be recommended to check my last articles, Here are the link Installation and uses of python and Variable declaration of python.
Now in this tutorial, we will learn what are the data types are available in python? Do not be worry about the uses of those data types right now. We will explain in detail in the upcoming tutorial. For this time being just try to understand these are the data types that we can use in the python development. So let’s get started to learn data types of python.
Python Data Types are used to define the type of a variable. In python there are five data types are available.
1. Numbers
Numbers is the data type in python. A Number means it having numeric values for example int, float, long and complex. Let’s see the uses of number with the help of example for better understanding.
Type | Format | Description | ||
---|---|---|---|---|
int | a = 10 | Signed Integer | ||
long | a = 345L | (L) Long integers, they can also be represented in octal and hexadecimal | ||
float | a = 45.67 | (.) Floating point real values | ||
complex | a = 3.14J | (J) Contains integer in the range 0 to 255. |
var1 = 1 var2 = 10.3
2. String
A string is a sequence of characters. Python supports Unicode characters. Generally, strings are represented by either single or double quote.
a = "This is first name" b= 'This is last name' print(a) print(b) # using ',' to concate the two or several strings print(a,"concated with",b) #using '+' to concate the two or several strings print(a+" concated with "+b)
3. List
Lists are a very useful variable type in Python. A list can contain a series of values. List variables are declared by using brackets [ ]
following the variable name. As we know in Java or Kotlin that List can hold anyone data types at one declaration. But here in python, it is the good thing that List can hold any type in one declaration. It could be a list, string, tuple or dictionary etc.
#list of having only integers a= [1,2,3,4,5,6] print(a) #list of having only strings b=["hello","john","reese"] print(b) #list of having both integers and strings c= ["hey","you",1,2,3,"go"] print(c)
4. Tuple
A tuple is another data type which is a sequence of data similar to the list. Tuples are fixed in size once they are assigned. In Python, the fixed size is considered immutable as compared to a list that is dynamic and mutable. Tuples are defined by parenthesis ().
#tuple having only integer type of data. a=(1,2,3,4) print(a) #prints the whole tuple #tuple having multiple type of data. b=("hello", 1,2,3,"go") print(b) #prints the whole tuple
Some of the good things to notice about the tuple.
- Tuples have no append or extend method. So the elements cannot be removed or add in a tuple.
- Tuples are faster than lists. It is good and safer to define product which is fixed.
5. Dictionary
Python Dictionary is an unordered sequence of data of key-value pair form. It is similar to the hash table type in java. Dictionaries are written within curly braces in the form key:value
. This is a very powerful datatype to hold a lot of related information that can be associated through keys
. It is very useful to retrieve data in an optimized way among a large amount of data. In Dictionaries the Key
must be unique.
Dictionaries are created by using braces { } with pairs separated by a comma (,) and the key values associated with a colon(:). Let’s see an example.
friends_age = {'john': 42, 'tom': 21} friends_age['john'] = 64 # set the value associated with the 'john' key to 64 print (friends_age['tom']) # print the value of the 'tom' key. friends_age['isaac'] = 34 # Add a new key 'isaac' with the associated value print (friends_age.keys()) # print out a list of keys in the dictionary print ('isaac' in friends_age ) # test to see if 'issac' is in the dictionary. This returns true.
So these are the data types are available in python. We have understood that how we can use this data types in python. In my next tutorial, we will focus on basic conditional feature and their uses in python.
If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.
Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on below section.
You can learn all articles of MobologicPlus from the android app, which is available to download below.
[appbox googleplay mobi.androapp.mobologicplus.c7929]
Happy coding 🙂
I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com
thanks