Data Types

                            Data Types

As the name suggests, data type means the type of data you are using like integer, string, float, array etc. Variable is used to hold any type of data. In Python, you do not need to define the type of variable, just assign the value to it and it will automatically be declared as that type of data. In python to check the data type of variable, type() function is used.

For e.g. :                   x = 10
               type(x)           # int

               y = "Hello"
               type(y)           # str

In python there are many data types like any other programming language. But there are 5 standard data type:
  • Numbers : Number stores numeric values. Python supports 4 types of numeric data i.e. int, long, float, complex. int represents signed integers like 10, 2 etc. long reperesents higher range of values like 7384037834L, -0x1929292L etc. float represents floating point numbers like 10.2, 0.45 etc. complex represents complex numbers like 1+2j, 2.4+1.2j etc.
  • String : String is the sequence of characters. In python there is no charater data type. Even a single alphabet is string. To represent string we use single, or double quotes. E.g. name = "John"
  • List : List is similar to array but not array. Unlike array list can contain different type of data in a single list. It is represents by square brackets [ ] and elements are separated by comma (,). Indexing in list is started from 0 just like array. E.g. l = [12, "World", 34.5]
  • Tuple : A tuple is similar to list upto maximum extent. The difference between list and tuple is that tuple can not be modified unlike list and we use paranthesis to represent tuple ( ). Tuple is read only data structure. E.g. t = ("Hello", 20, 100.34)
  • Dictionary : Dictionary is ordered set of key-value pair. It is similar to hash table. In dictionary we have a key and corresponding value. We use curly braces { } to represent dictionary. E.g. d = {"Jon":1, "Harry":2, "Tony":3}
There are other data types also like set, boolean:
  • Set : Set is unordered collection of items. It is similar to what we know about set in Maths. We can do intersection, union and difference of sets. There is no indexing of items in Set, elements appear in random order. We use curly braces { } to represents the set. In set there is no duplication of item. Every item appears only once. E.g. S = {1, 2, 3, 4}
  • Boolean : Boolean data type have two possible values only either True or False. It is same in Python just like any other programming language. E.g. valid = True

Comments

Popular posts from this blog

Loops in Python

Python OOPs

Regular Expressions