Example HTML page

Data Types in Python

Introduction to Data Types in Python

Any data must be stored in memory. For this we have many ways. For suppose if I want to store my address then I have to store it as alphanumeric characters. If I want to store my reg.no then I have to store it as numeric value. Likewise, in Python there are five standard data types. Those are:

  • Numbers
  • Strings
  • Lists
  • Tuples
  • Dictionaries

Numbers

Numbers are one of the data types in Python. We can store numeric values.

Ex

>>> m=13

>>> print(m)

13

>>> print(type(m))

<class ‘int’>

>>> 1+3.4

4.4

>>> int(7.4)

7

>>> float(5.2)

5.2

>>> 1.3+5.6

6.8999999999999995

Strings

In Python, strings are defined as a sequence of characters which are represented with single quotes or double quotes.

Ex

>>> s=”About strings”

>>> print(s)

About strings

>>> s+s

‘About stringsAbout strings’

>>> s*3                                                                              #Prints string three times

‘About stringsAbout stringsAbout strings’

>>> s+” in Python”                                                         #String concatenation

‘About strings in Python’

Lists

Another data type in Python is ‘List’. A list is a collection of items separated by commas and enclosed within square brackets [ ].

Ex

>>> L=[“VMS”,526,16,13,21,7.9]

>>> print(L)

[‘VMS’, 526, 16, 13, 21, 7.9]

>>> L*2

[‘VMS’, 526, 16, 13, 21, 7.9, ‘VMS’, 526, 16, 13, 21, 7.9]

>>> L+L

[‘VMS’, 526, 16, 13, 21, 7.9, ‘VMS’, 526, 16, 13, 21, 7.9]

>>> L+[‘xyz’]                                                                    #Appends an item into existing list ‘L’

[‘VMS’, 526, 16, 13, 21, 7.9, ‘xyz’]

Tuples

A tuple is another data type in Python. Tuples are similar to the list. A tuple contains number of items separated by commas.

One could notice that a tuple is enclosed with ( ) and the items in it cannot be changed. Whereas a list is enclosed with [ ] and the items in it can be changed.

Ex

>>> T=(“Sudha”,13.26,”abc”,”y”)

>>> print(T)

(‘Sudha’, 13.26, ‘abc’, ‘y’)

>>> T+T

(‘Sudha’, 13.26, ‘abc’, ‘y’, ‘Sudha’, 13.26, ‘abc’, ‘y’)

>>> T*2

(‘Sudha’, 13.26, ‘abc’, ‘y’, ‘Sudha’, 13.26, ‘abc’, ‘y’)

>>> T+(“z”)                                                                           #Appending an item into existing list

Traceback (most recent call last):

File “<pyshell#35>”, line 1, in <module>

T+(“z”)

TypeError: can only concatenate tuple (not “str”) to tuple

You can observe that in the above example I tried to append an item “z” into tuple “T”. But it is not possible. Because we cannot add an item into tuple or alter the size of a tuple . But it is possible in the case of lists and you may go through in the example of list.

Dictionaries

A dictionary has key-value pairs. A key in dictionary may be of any Python type but numbers or strings are used usually. Whereas the value can be any object in Python. Dictionaries are enclosed by { }. The values can be assigned and will be accessed by using [ ].

We can access the keys and values by using a dot operator.

Ex

>>> D={ }                                                                                        #Creates an empty Dictionary

>>> print(D)

{}

>>> D={“name”:”Sudhakar”,”Reg.no”:1316,”Dept.”:”CSE”}        #Appending key-values in to it

>>> print(D)

{‘Dept.’: ‘CSE’, ‘Reg.no’: 1316, ‘name’: ‘Sudhakar’}

>>> D.keys( )                                                                                #Accessing keys

dict_keys([‘Dept.’, ‘Reg.no’, ‘name’])

>>> D.values( )                                                                             #Accessing values.

dict_values([‘CSE’, 1316, ‘Sudhakar’])

Note: Dictionaries concept does not concentrate on the order of elements. They are unordered.

In the next section we will discuss each of the above five data types briefly.

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest