• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Python in 90mins
 

Python in 90mins

on

  • 1,700 views

learn python with exercise in 90 minutes

learn python with exercise in 90 minutes

Statistics

Views

Total Views
1,700
Views on SlideShare
1,700
Embed Views
0

Actions

Likes
4
Downloads
73
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Python in 90mins Python in 90mins Presentation Transcript

    • Larry cai <larry.caiyu@gmail.com>
    •  What is python ? Why Python ? Exercise 1: Hello World Exercise 2: String, List via command line Exercise 3: For loop with if..else.. Exercise 4: Learn how to use module json to dump data Exercise 5: Generate online NASDAQ data using json !!! Lots of materials are copied from http://www.cs.columbia.edu/~hgs/teaching/ap/slides/python.ppt and which is based on official tutorial by Guido van Rossum Exercises are created by Larry Cai 2 Python in 90 minutes
    • What is Python ? And Why Python 2.7 Python is an easy to learn, powerful script programming language Python is mature and better community compare to Perl Python is high level over shell script Python is well supported in OS as default compare to Ruby (which is also nice) Python 3.x is quite new, not well accepted Welcome to Python world 3 Python in 90 minutes
    •  Python 2.7.x In Windows with Git Bash  http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi  Add into Path How to run ?  /usr/local/bin/python  #!/usr/bin/env python  interactive use Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>  $ python script.py 4 Python in 90 minutes
    •  Hello World hello.py#!/usr/bin/env pythonfor name in [“larry”,”cai”]: print "hello" , name 5 Python in 90 minutes
    • Basic operations & program Assignment: a,b = 0, 1  size = 40 # non-zero = true  a = b = c = 3 while b < 10: # formatted output, Numbers without n  integer, float print b,  complex numbers: 1j+3, # multiple assignment abs(z) a,b = b, a+b Strings  hello world, its hot  "bye world"  continuation via or use """ long text """" Python forces to use a certain indentation style instead of “{,}” 6 Python in 90 minutes
    • String & List operations concatenate with + or  lists can be heterogeneous neighbors  a = [spam, eggs,  word = Help + x 100, 1234, 2*2]  word = Help a  Lists can be indexed and subscripting of strings sliced:  Hello[2]  l  a[0]  spam  slice: Hello[1:2]  el  a[:2]  [spam, eggs]  word[-1]  last character  Lists can be manipulated  len(word)  5  a[2] = a[2] + 23  immutable: cannot assign to subscript  a[0:2] = [1,12]  a[0:0] = []  len(a)  5 7 Python in 90 minutes
    • Learn the standard script sampleHelp docImport libraryFunctionMain funcStart here ! https://gist.github.com/4308811 8 Python in 90 minutes
    • Exercise 2: remove characters Practices basic operation in Interactive Shell Add features to remove two characters each from input  ./hello2.py -m larry,cai hello rry hello iHints: names = message.split(“,”) 9 Python in 90 minutes
    • Control flow: if & forx = a = [cat, window, int(raw_input("Please defenestrate] enter #:")) for x in a:if x < 0: print x, len(x) x = 0 print Negative  no arithmetic changed to zero progression, butelif x == 0:  range(10)  [0, 1, 2, print Zero 3, 4, 5, 6, 7, 8, 9]elif x == 1:  for i in range(len(a)): print Single print i, a[i]else: print More  do not modify the no case statement sequence being iterated over 10 Python in 90 minutes
    • Loops: break, continue, else, o nothing break and continue  pass does nothing like C  syntactic filler else after loop exhaustion while 1:for n in range(2,10): pass for x in range(2,n): if n % x == 0: print n, equals, x, *, n/x break else: # loop fell through without finding a factor print n, is prime 11 Python in 90 minutes
    • Exercise 3: for loop with if..else.. ./hello2.py -m larry,cai,in,github 1. hello Larry 2. hello Cai 3. @ 4. hello GithubHints: think about loop in clean way 12 Python in 90 minutes
    • Defining functionsdef fib(n): def ask_ok(prompt, """Print a Fibonacci retries=4, complaint=Yes series up to n.""" or no, please!): a, b = 0, 1 while 1: while b < n: ok = raw_input(prompt) print b, if ok in (y, ye, a, b = b, a+b yes): return 1 if ok in (n, no):>>> fib(2000) return 0 retries = retries - 1 if retries < 0: raise First line is docstring IOError, refusenik first look for variables in error local, then global print complaint need global to assign global variables >>> ask_ok(Really?) 13 Python in 90 minutes
    • Modules import module:  function definition + executable statements import fibo  executed only when module Use modules via "name is imported space":  modules have private symbol tables >>> fibo.fib(1000)  avoids name clash for global >>> fibo.__name__ variables fibo  accessible as module.globalname can give it a local name:  can import into name space: >>> fib = fibo.fib >>> from fibo import fib, fib2 >>> fib(500) >>> fib(500)  can import all names defined by module: >>> from fibo import * 14 Python in 90 minutes
    • Exercise 4: write into json config file  JSON (JavaScript Object Notation) dump list of the messages into external config files  ./hello2.py -m larry,cai,in,github > config.json  $ cat config.json [ “larry”, “cai”, “in”, “github” ]Hints: json.dumps(data, indent=2) 15 Python in 90 minutes
    • Tuples and sequences lists, strings, tuples: examples  Empty tuples: () of sequence type >>> empty = () tuple = values separated by >>> len(empty) commas 0>>> t = 123, 543, bar  sequence unpacking  distribute>>> t[0] elements across variables123 >>> t = 123, 543, bar>>> t >>> x, y, z = t(123, 543, bar) >>> x Tuples may be nested 123>>> u = t, (1,2)  packing always creates tuple>>> u  unpacking works for any sequence((123, 542, bar), (1,2)) 16 Python in 90 minutes
    • Dictionaries like Tcl or awk associative  no particular order  delete elements with del arrays >>> del tel[foo] indexed by keys  keys() method  unsorted list of keys keys are any immutable type: >>> tel.keys() e.g., tuples [cs, lennox, hgs] but not lists (mutable!)  use has_key() to check for existence >>> tel.has_key(foo) uses key: value notation>>> tel = {hgs : 7042, lennox: 0 7018}>>> tel[cs] = 7000>>> tel 17 Python in 90 minutes
    • Exercise 5: Generate report from internet  Print MSFT, GOOG stock via Nasdaq live JSON with sort  ./hello2.py –s MSFT,GOOG “Getting live data from NASDAQ @ <current time> [ GOOG: xx { MSFT: xx "id": "694653" ,"t" : "GOOG" ,"e" : "NASDAQ" ,"l" : "701.96" ,"l_cur" : "701.96" ,"s": "0" ,"ltt":"4:00PM EST" ,"lt" : "Dec 14, 4:00PM EST" ,"c" : "-0.74" ,"cp" : "-0.10"Hints: urlopen -> json.load -> data .. ,"ccol" : "chr" } ]$ curl -x <proxy> "http://finance.google.com/finance/info?client=ig&q=NASDAQ:MSFT,NASDAQ:GOOG “fl = urlopen (“link”,"r")data = json.load(fl) 18 Python in 90 minutes
    •  Automate your daily work in python scripts Keep coding in python !!! Reading books and sample codes !! 19 Python in 90 minutes
    •  Slides:  http://www.slideshare.net/doughellmann/an-introduction-to- the-zen-of-python - Doug Hellmann  http://www.cs.columbia.edu/~hgs/teaching/ap/slides/python.ppt List books, articles, and electronic sources  http://docs.python.org/2.7/  Python cheatsheet: http://www.addedbytes.com/cheat- sheets/python-cheat-sheet/ 20 Python in 90 minutes