Sunday, March 4, 2012

Python programming Day 01

Glossary 

#Extract URL in a web page.
page=contents of some web page as a string
start_link=page.find('<a href=')
start_quote=page.find('"',start_link)
end_quote=page.find('"',start_quote + 1)
url=page[start_quote+1:end_quote]
print url
page=page[end_quote:] #all the remaining text of page started from the position end_quote.

#Find a target string t started from the location i in string s.
s.find(t,i)

#Define a function
def summation(a,b):
    return a+b
print summation(4,5)

#Waiting for user's input
a_str=raw_input('What is your name? ')
print 'Hello' + a_str

#Using the 'import' statement to import a library
import math
print math.sqrt(9)
print math.pow(5, 7)

Find the position of the second occurrence of string 't' in string 's'.

def find_second(s,t):
    first=s.find(t)
    return s.find(t,first+1)
E.g. print find_second('Hello David, Hello Dave'); #The result will be 13.

#A function uses IF statement to find out the bigger number between the two numbers

def bigger(n1,n2):
    if n1<n2:
        return n2
    else:
        return n1     
print bigger(3,8)

#A function uses IF statement to check if the first letter of a string is started with 'D'.

def is_friend(s):
    if s[0]=='D': #indexing the string using [ ]
        return True
    else:
        return False     
print is_friend('Dave')

#Another procedure uses IF and ELSE statement with 'or' logical operator

def is_friend(name):
    if (name[0] == 'D' or name[0] == 'N'):
        return True
    else:
        return False     
print is_friend('David')
print is_friend('Neville')
print is_friend('Tom')

#Another procedure uses to find the biggest number among the three numbers
def biggest(x,y,z):
    if (x >= y and x >=z):
        return x
    else:
        if (y >= x and y>=z):
            return y
        else:
            return z
print biggest(9,7,8)

#A better solution than the previous
def biggest(x,y,z):
  return bigger(bigger(x,y),z)
def bigger(x,y):
if x>y:
return x
else:
return y

# A procedure uses a 'while' loop to print all numbers up to n (n is included)
def print_numbers(n):
i=1;
while i <= n:
                print i
                i=i+1
 print_numbers(10)

# A procedure use 'while' loop to compute the factorial of number n

def factorial(n):
    if n==1:
        return 1
    while(n>0):
        return n*factorial(n-1)
print factorial(4)

# Another procedure uses 'while' loop to compute the factorial of number n
def factorial(n):
      result=1
      while(n >=1):
              result=result*n
              n=n-1
      return result
print factorial(4)

#The procedure 'get_next_target' is used to extract URL and the position of the end_quote
def get_next_target(page):
    start_link = page.find('<a href=')
    if start_link == -1:
        return None, 0
    else:
        start_quote = page.find('"', start_link)
        end_quote = page.find('"', start_quote + 1)
        url = page[start_quote + 1:end_quote]
        return url, end_quote
               
page='This is the udacity link <a href="http://udacity.com">'
url,post=get_next_target(page)
print url
print post

#Print all links in the page 'http://xkcd.com/353'

def print_all_links(page):
while True:
url, endpos = get_next_target(page)
if url:
print url
page = page[endpos:]
else:
break
      print_all_links(get_page('http://xkcd.com/353'))

Every possible computer program can be written using the following concepts.
  1. arithmetic
  2. comparisons
  3. if
  4. procedures
References
Udacity, CS1 Lecture Notes, February-April 2012

No comments:

Post a Comment

Mounting USB drives in Windows Subsystem for Linux

Windows Subsystem for Linux can use (mount): SD card USB drives CD drives (CDFS) Network drives UNC paths Local storage / drives Drives form...