Python Intro
Python Intro
Python
! ! ! !
Highly popular language Easy to pick up and start Supported by large number of libraries/packages Highly popular among 'Information Retrieval' group who parse large amount of data
Running Python
!
! ! ! ! !
If you are using Mac or Linux>python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Hello, World!
! ! !
>python >>> print Hello, World! World's shortest Hello, World! program
Importing Packages
! ! !
These are software libraries written by someone else Similar to Java packages Remember following?
! !
!
! ! !
Declaring variables
! ! ! ! ! ! ! ! !
>>> x = 'This is a string' >>> print x This is a string >>> x = 0 >>> print (x+1) 1 >>> x = 0.0 >>> print (x+1) 1.0
Lists
! ! !
Similar to arrays in Java and C But much more powerful! Look at following Java code:
!
! !
Lists
! ! ! ! ! ! !
array = [1 ,2, '3', 'string 2', 4.5] print array[0] 1 print array[3] 'string 2' print array[5] IndexError: list index out of range
Or how to read each element in list Method I: li = [1 ,2, '3', 'string 2', 4.5] for item in li: print item
Method II:
Merging lists
! ! ! ! ! ! ! ! !
list1 = ['a' , 'b', 'c'] print list1 ['a', 'b', 'c'] list2 = ['d','e','f'] print list2 ['d','e','f'] list1.extend(list2) print list1 ['a', 'b', 'c', 'd', 'e', 'f']
list1 = ['a' , 'b', 'c'] print list1 ['a', 'b', 'c'] element = 'a' if element in list1: list1.remove(element) print list1 ['b','c']
Dictionaries
! ! ! !
Similar to HashTable in Java It's a list of <key,value> pairs. Similar to word,synonym pair in english dictionary. Example:
!
Dictionaries
! ! ! ! ! ! ! ! ! !
d = {"name":"Abhinav", "position":"TA", "course":445} print d[name] Abhinav print d[course] 445 d[course] = 645 print d[course] 645 print d[last name] KeyError: 'last name'
Add/Delete in Dictionary
Addition/Update: d[10] = xyz print d[10] xyz Deletion del d[10] print d {'position': 'TA', 'name': 'Abhinav', 'course': 645}
Merging dictionary
# Merging two dictionaries >>> d = {"name":"Abhinav", "position":"TA", "course":445} >>> d2 = {"semester":7} >>> d.update(d2) >>> d {'position': 'TA', 'semester': 7, 'name': 'Abhinav', 'course': 445}
Defining functions
# Print elements in list def printlist(mylist): for i in range(len(mylist)): print mylist[i] list1 = {'first', 'second'} printlist(list1)
Functions: Example 2
# Delete element with key 'name' in dictionary def deletename(mydict): if name in mydict: del mydict[name] return mydict
! !
! !
Note the difference from merging list! deepcopy(): makes a copy of your list
>>> import copy >>> list2 = copy.deepcopy(list1)
A Table is represented as list of dictionaries. ! Person = [ {'name':'Abhinav','age':26}, {'name':'Jane','age':21} ] ! In a list, each element is dictionary ! The column names in table are keys for dictionary ! You now know ! how to iterate and read list ! how to iterate and read dictionary ! how to add/delete elements in list and dictionary ! how to write functions ! You know all you need to do assignment!
Write all your code in a file with .py extension: <your-login-id>.py It should stick to the format provided in starter code on edlab machine. Execute it as follows:
!
python <your-login-id>.py