Thursday, July 8, 2010

Basic points about Python

Python is a high-level, interpreted, interactive and object oriented-scripting language.
  • Python is Interpreted
  • Python is Interactive
  • Python is Object-Oriented
  • Python is Beginner's Language
Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands.


Python's Feature highlights include:
  • Easy-to-learn
  • Easy-to-read
  • Easy-to-maintain
  • A broad standard library
  • Interactive Mode
  • Portable
  • Extendable
  • Databases
  • GUI Programming
  • Scalable



    Reserved Words:


    The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names.
    Keywords contain lowercase letters only.



    andexecnot
    assertfinallyor
    breakforpass
    classfromprint
    continueglobalraise
    defifreturn
    delimporttry
    elifinwhile
    elseiswith
    exceptlambdayield



    Standard Data Types:



    Python has five standard data types:

    • Numbers
    • String
    • List
    • Tuple
    • Dictionary



    The search Function


    This function search for first occurrence of RE pattern within string with optional flags.
    Here is the syntax for this function:

    re.string(pattern, string, flags=0)
    
    Here is the description of the parameters:



    ParameterDescription
    patternThis is the regular expression to be matched.
    stringThis is the string which would be searched to match the pattern
    flagsYou can specifiy different flags using exclusive OR (|). These are modifiers which are listed in the table below.
    The re.search function returns a match object on success, None on failure. We would use group(num) or groups() function of match object to get matched expression.



    Match Object MethodsDescription
    group(num=0)This methods returns entire match (or specific subgroup num)
    groups()This method return all matching subgroups in a tuple (empty if there weren't any)

    Matching vs Searching:

    Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).



    Search and Replace:


    Some of the most important re methods that use regular expressions is sub.


    Syntax:

    sub(pattern, repl, string, max=0)
    
    This method replace all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method would return modified string.

    Built-In Class Attributes:

    Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute:



    • __dict__ : Dictionary containing the class's namespace.
    • __doc__ : Class documentation string, or None if undefined.
    • __name__: Class name.
    • __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.
    • __bases__ : A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

No comments:

Post a Comment