Python Data Types
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
~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.
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 = {"
>>> print DictVar["
~ 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
No comments:
Post a Comment