Pages

Wednesday, July 15, 2015

Python – Data Types – 2

As we know that most programs are not simple and when complex data is involved we need to use better data Types then the Simple Types. These are done by using the Container Types in Python

Here are the List of Container types available in Python

  • tuple
  • string
  • unicode
  • list
  • set
  • frozenset
  • dictionary
The Container Types provides 2 Capabilities. There are
Sequence Types and Non-Sequence Types: The tuple, String,Unicode , list , set and Frozen set are all Sequence Types and the last one dictionary is a map. A Sequence type is just a sequence of Objects. The elements in these are accessed by Sequnce.

The Map Object on the other hand holds objects for which the order is not important; a value is extracted from the container by providing a key that locates the value of interest

The other important feature of Container Types is
Mutable and non-mutable:  The tuple,string,Unicode and frozenset are Mutable Container means data being stored can't be changed. If you need to change the data for some reason, you need to create a new container to hold the new data.

The last three container types (list, set, and dictionary) are all mutable containers, so any data they hold can be changed as needed.

Lets start with the Container Types one by one

Strings - Strings are the basic type in Python. We can create them simply by enclosing them in Quotes. Python treats both single and double Quotes as same. A String can be created in Python as,

var = “hello World”

Accessing – A String can be accessed in multiple ways. In Python there are no character type unlike in other languages. A Single character is also treated as a single String with length one.

>>> var = "hello World"
>>> print var [1]
e
>>> print var[1:5]
ello

Updating – A String can be updated by re-assigning a variable to another String.

>>> var = "hello World"
>>> print "Updating String :- ",var + " Python"
Updating String :-  hello World Python

Escape Characters – An escape character is the one that gets interpreted; in a single quoted as well as double quoted strings. So when ever we use a special escape character like “\n” in a string it tell the underlying environment with a new definition

Some of the escape Characters are

\n       0x0a   Newline
\r       0x0d   Carriage return
\s       0x20   Space
\t       0x09   Tab

String Operators – String Operators are nothing new, they are similar to the Operators available but show different behavior with String. The Operators include,

+ (Concatenation) – Adds values on either side of the Operator ( a+b = ab)
* (Repetition) – Creates new String, concatenating multiple copies of the same string (a *2 = aa)
[] (Slice) – Gives Character from the given index ( a[1] – 1 character in  String a)
[:](Range Slice ) - Gives Character from the given index (a[1:4] – from 1 to 4 in String a)
In – Member Ship Operator - Returns true if a character exists in the given string ( I in India – True)
Not in – Returns true if the character does not exist in the given String ( K not in India – True)
r/R (Raw String) – Suppresses the actual meaning of the Escape Characters.

Formatting – String Formatting allows one to format the way the Output is printed. It use the Operator (%).

>>> print "My Name is %s and Age is %d" % ("Java",30)
My Name is Java and Age is 30

We can see that we have used %s and %d in formatting the output for displaying the values. Some other formatting values includes

%c     character
%s     string conversion via str() prior to formatting
%i      signed decimal integer
%d     signed decimal integer
%u     unsigned decimal integer
%o     octal integer

Triple Quotes – Triple Quotes helps in allowing string to span multiple lines including the New Lines in Python. This also include any Special Characters too.

The syntax for triple quotes consists of three consecutive single or doublequotes.

>>> para_string = """ this is Pyhton and very
... language for starting such as Tab ( \t ) nad breaks too
... include the details please
... which comes handy"""

>>> print para_string
 this is Pyhton and very
language for starting such as Tab (      ) nad breaks too
include the details please
which comes handy

We can see that special characters like Tab are also processed in this.

Raw String – There are certain cases where we don’t want the interpreter to process special characters like “\” etc.

Consider the below example, 
>>> print "c:\\python"
c:\python

See the second “\” is ignored. But if we want the special characters not to be processed we can use the raw String like

>>> print r"c:\\python"
c:\\python

Some more examples include,
>>> s3 ="this \n and \n too Much"
>>> print s3
this
 and
 too Much
>>> s4 = r"this \n and \n too much"
>>> print s4
this \n and \n too much

String Methods – String supports many Methods that helps in processing of the String like,

max(str)
min(str)
isdigit()
islower()
isnumeric()
isspace()
isupper()


Most of these methods are self explanatory

No comments :

Post a Comment