Variable and Data Types in Python

As we discussed in last tutorial, the variables are the reserved memory space to store some data in them so that we can use same data again. Data type is the type of data which can be placed in variable or reserved memory. Python is a weak type language and we do not have to mention explicitly that which data type we are defining. Python manages itself. Python has five standard data types:

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictionary

Every data type has its own properties. In this tutorial, we will discuss about what are these data types, how to access them and some of their properties. In some future tutorials we will also discuss their advance properties.

1) Numbers:

Number store numeric values like intgers (2, 4, 5, 32), float (2.3, 2.34, 87.0), long (51847302L,-0x98345L, 0xAE987CECBDAEFDB876l) and complex ( 1+2j, -4+5j). Note that in python 3 do not place L or l after a long number. In python 2, it is optional. Try these examples:

  1. >>> a = 2
  2. >>> b = 34
  3. >>> c= 34.5
  4. >>> d = 0xDEF098ECBDAE987987
  5. >>> e = 1+4j
  6. >>> f = -2+3j
  7. >>> g = e+f
  8. >>> print(g)
  9. (-1+7j)

2) String:

String is simple text covered by single or double quotation marks. We can access any character of the string using slice operator [], and [:] with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. Lets see this example:

  1. >>> name = "harish Kumar"
  2. >>> print(name)
  3. harish Kumar
  4. >>> print(name[0])
  5. h
  6. >>> print(name[2:5])
  7. ris
  8. >>> print(name[2:])
  9. rish Kumar
  10. >>> print(name*2)
  11. harish Kumarharish Kumar
  12. >>> print(name + " this is my name")
  13. harish Kumar this is my name
  • Line 4 is printing first character
  • Line 6 is printing characters starting from index 2 and before index 5
  • Line 8 is printing characters starting form index 2
  • Line 10 is printing name twice
  • Line 12 is printing an another string with name.

For understanding strings much better play with it. Try to change indexes and printing other strings also.

3) List:

List is something similar to array in C, C++ and Java. It contains a set of data. You can consider it as a group of variables with a lot of more features. Look at this example:

  1. >>> a = 'variable'
  2. >>> b = 3
  3. >>> list = ['string', 2, 5.6, a, b]
  4. >>> print(list[0])
  5. string
  6. >>> print(list[-1])
  7. 3
  8. >>> print(list[4])
  9. 3
  10. >>> print(list[:2])
  11. ['string', 2]
  12. >>> print(list[1:3])
  13. [2, 5.6]
  14. >>> print(list[1:])
  15. [2, 5.6, 'variable', 3]
  16. >>> print(list[2])
  17. 5.6
  18. >>> list[2] = 56.7
  19. >>> print(list[2])
  20. 56.7
  21. >>> print(list)
  22. ['string', 2, 56.7, 'variable', 3]
  23. >>> anotherList = ['name','harish']
  24. >>> list = list + anotherList
  25. >>> print(list)
  26. ['string', 2, 56.7, 'variable', 3, 'name', 'harish']
  • In line 3, we are defining a list. We can also put variables in the list.
  • Line 4 is printing first element of the list.
  • Line 6 is printing the last element of the list.
  • Line 10 is printing elements before index 2, i.e., first 2 elements
  • Line 12 is printing elements starting with index 1 and and before index 3.
  • Line 18 is updating the list element
  • Line 21 is printing whole list
  • Line 23 is defining a new list
  • Line 24 is adding new list into old list

4) Tuple:

Tuple appears similar to list but it is written within () instead of []. All of the properties of the tuple are not like list. Tuple is completely different data type. We will see more properties of list and tuple in new tutorials. The major difference is that the data of the tuple can not be updated and you can add new tuple to the existing tuple. Look at this example:

  1. >>> a = 2
  2. >>> b = 2.3
  3. >>> tuple = ('harish', 5, a, b)
  4. >>> print(tuple[0])
  5. harish
  6. >>> print(tuple[:2])
  7. ('harish', 5)
  8. >>> print(tuple[1:3])
  9. (5, 2)
  10. >>> anotherTuple = ('string', 'name')
  11. >>> tuple = tuple + anotherTuple
  12. >>> print(tuple)
  13. ('harish', 5, 2, 2.3, 'string', 'name')
  14. >>> print(tuple*2)
  15. ('harish', 5, 2, 2.3, 'string', 'name', 'harish', 5, 2, 2.3, 'string', 'name')
  16. >>> tuple[2] = 3
  17. Traceback (most recent call last):
  18. File "<stdin>", line 1, in <module>
  19. TypeError: 'tuple' object does not support item assignment

We got error in line 16 because we can not update tuple. Tuple is important when you don't want to give permission to other people for updating some data like usename.

5) Dictionary:

Dictionary in python are similar to the associative array in PHP. Till now, you were assigning only values and indexes were mapped automatically but in dictionaries you have to give keys (indexes) yourself. So it is obvious that you can not use slice operators [] and [:] with dictionaries. Try this example:

  1. >>> dict1 = {'name':'harish','age':20, 2:'index'}
  2. >>> print(dict1['name'])
  3. harish
  4. >>> dict1['addMore'] = "new string"
  5. >>> print(dict1.keys())
  6. dict_keys([2, 'addMore', 'age', 'name'])
  7. >>> print(dict1.values())
  8. dict_values(['index', 'new string', 20, 'harish'])
  9. >>> anotherDict = {}
  10. >>> anotherDict['key0'] = 47
  11. >>> a = 'a string'
  12. >>> anotherDict['key1'] = a
  13. >>> dict1 = dict1 + anotherDict
  14. Traceback (most recent call last):
  15. File "<stdin>", line 1, in <module>
  16. TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

We got error in line 13 because we can not add two dictionaries directly.

Final words:

Beyond the core data types there are some more data types : sets, files, Boolean, types and None. We are not discussing them here but will cover in future tutorials.

To understand data types better and to differentiate between them give some more time on exercise and try to play with operations like assignment and printing.



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Help Dad unlock new skills this summer in just $10 on Udemy

Development Category (English)300x250

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Best Video Courses