It has become appallingly obvious that our technology has exceeded our humanity

The era of Computer Buisness!!!

Monday, 21 October 2013

SCOPE

There are mainly two types of scopes for python variables:

  • Local scope
  • Global scope
Global variables are accessible from all functions whereas local variables are accessible only from the function where it is declared.
Consider the following example.
----------------------------------------------------------------------------------------------------------------------------------
x = 1

def fun():
    x = 2
    print x

fun()
print x

Here the output is
2
1
-----------------------------------------------------------------
First the function fun() is invoked, when the print x statement is encountered, it looks for local variables named x if found it prints the value of local x if not it goes for global x.Since in this problem local x is available the value of global x is overwritten by local x within the function.
-----------------------------------------------------------------

x = 1

def fun():
    p = 2
    print p

fun()
print x
print p #this gives an error

Here the output is
2
1
Traceback (most recent call last):
  File "nested_scope.py", line 7, in 
    print local_var  # this gives an error
NameError: name 'local_var' is not defined
-----------------------------------------------------------------
If at all you invoke global variable within a function, if you declare another variable with the same name, then the global value will be over written(only within the function).
For eg:
-----------------------------------------------------------------
x = 1

def fun():
    global x
    x = 10
    print x

fun()

print x
 
The output will be:
10
10
-----------------------------------------------------------------

./ method to execute a python file

A file can be executed from the terminal using
./filename.py
by typing a special command in the beginning of the filename.
The very first byte of the file must start with a # symbol followed by a !,followed by the path.
For eg,
#!/usr/bin/python

Tuesday, 1 October 2013

REGULAR EXPRESSIONS

Regular expressions are used for pattern matching.The Python "re" module provides regular expression support.

import re

s1 = 'this is a nice line!'

pat1 = 'nice'

print re.search(pat1, s1)

If pattern matching is successful a match object is returned else the function returns None.
pat1 = r'^is'

print re.search(pat1, s1)

'r' before the string gives a special meaning to it. By using that we say the string is raw or the special characters used withing the string doesn't have special meaning.
 
Ordinary characters just match themselves exactly.
The characters which do not match themselves because they have special meanings are:
 . ^ $ * + ? { [ ] \ | ( ) 
  • . (a period) -- matches any single character except newline '\n'
  • \w -- (lowercase w) matches a "word" character: a letter or digit or underscore. Note that although "word" is the mnemonic for this, it only matches a single word char, not a whole word. \W (upper case W) matches any non-word character.
  • \b -- boundary between word and non-word
  • \s -- (lowercase s) matches a single whitespace character -- space, newline, return, tab, form. \S (upper case S) matches any non-whitespace character.
  • \t, \n, \r -- tab, newline, return
  • \d -- decimal digit [0-9]
  • ^ = start, $ = end -- match the start or end of the string
  • \ -- inhibit the "specialness" of a character. So, for example, use \. to match a period or \\ to match a slash. If you are unsure if a character has special meaning, such as '@', you can put a slash in front of it, \@, to make sure it is treated just as a character.