Python Data Types
Now we will see what data types does Python supports, the list is here. The 2nd part of the post talks about how to declare and use the variables. Please read till the end if you want to do any experiments with different data types.
1. Numbers
This data type is used to work with numbers. I don;t call this as Integer data type, as many books say. The number data type supports both whole numbers (Integers) and also the real numbers. There are no different data types for integers and real number. It is easy to define a number variable, just assign the number as shown below
>>> i = 10
>>> r = 1.2
>>> print i*r
2. Strings
This data type is used to work with text. There is no special data type in python to declare characters. Strings are enclosed with in single or double quotes both works in python. But do not start with single quote and end with double or vice versa. Both these quotes can be clubbed and used to if a string contains a quote, for example "What's you name?" .
~The same thing can be done by using escape character also. \ (back slash) works as a escape character in Python. For example "What\'s your name?", In the same way below escape sequences can be used inside strings
\n -- new line
\t -- tab
\' -- Single Quote
\" -- Double Quote
\\ -- Back Slash
~Multiple lines also can be specified using triple quotes like below (it is easier if you don;t want to use \n)
"""1st line
2nd line
3rd line"""
~If you are working with very big string, you can type the string in multiple lines with out breaking the string in different line like shown below by using \ at the end of the line.
>>> bigstring = "hello world!!! \
Python is a good language"
~Raw Strings are used if you do not want to process a string for any escape characters. A raw string holds the characters that you typed. Raw strings are defined as below, an r plus quote.
>>> rawstring = r"\n is used for new line characters"
~Unicode Strings are used if the string contains any special character sets like French, Hindi, Arabic, etc. Unicode strings are defined with u plus quotes.
>>> unistring = u"This is unicode string."
We will talk about the string functions in coming posts.
3. Lists
This is a new data type for C/C++ programmers, but It works like a dynamic array. Dynamic means we can add/remove elements whenever required using it's built-in functions and you can add any type of values in the same list. Find the below examples to see how to use lists. Individual list elements can be accessed as same as array elements using the index value like ListVar[0].
Ex: >>> ListVar = ['abc', 'pqr', 'xyz', 123]
>>> print ListVar
4. Dictionaries
Dictionary type variable contains set of keys and their values. It is like a phone book, name-number (of course one can have more numbers :) ) Find the below example how dictionaries can be defined. We can retrieve values by using by keys, but not in the other way.
Ex: >>> DictVar = {"India":"NewDelhi", "Sweden":"Stockholm", "Denmark":"Copenhagen"}
>>> print DictVar["India"]
~ Can not have duplicate keys.
~ Can add new key-values at any time.
~ keys are case sensitive.
~ these can be sorted on keys.
5. Tuples
Tuple is an immutable list. This data type is almost same as List, but it does not allow any addition/deletion/modification of tuple elements once a tuple is defined. It is like const keyword in C/C++ language. Tuple is defined also in the same way as List, but instead of square braces normal braces should be used like below. Tuples are faster than lists.
Ex: >>> TupVar = ('xyz','pqr')
>>> print TupVar