Python bootcamp - C4Dlab, University of Nairobi

965 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
965
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python bootcamp - C4Dlab, University of Nairobi

  1. 1. Python Bootcamp - C4Dlab SCI labs, University of Nairobi Nov 24th 2013 Kenny Rachuonyo
  2. 2. Introduction to Python Features of Python● Simplicity - pseudocode● Free and Open Source - community● High-level – no low-level mngt● Interpreted – run from source● Object-Oriented – simple to use● Extensible – C/C++
  3. 3. Features (cont.)● Embeddable – games, graphics,● Extensive Libraries (batteries included) – data compression, OS, Networking, Internet, Multimedia, Graphics
  4. 4. Python in the Industry Web● Google – Youtube, backend tasks..● Reddit – news aggregation site● Disqus – commenting service● Numerous web frameworks – django, Zope, webapp2, web.py, pyramid, flask
  5. 5. Python in the Industry Desktop● Games – Counterstrike, Civilization IV● Cinema 4D – Graphics● Dropbox● GUI frameworks – PyGTK, PyQT,
  6. 6. Python in the Industry Scientific Computing● NASA● Packages: Scipy, Numpy, Matplotlib,
  7. 7. Python in the Industry Mobile● Nokia Symbian Series 60● Android – Scripting Layer for Android● Blackberry● Kivy – cross-platform: iOS, Android, Linux, Windows, Mac
  8. 8. Python Basics● The interpreter – Installation – Windows (set path)● Datatypes: int, str, float, lists, tuples, dictionaries● Basic I/O
  9. 9. Python Basics● Variables – Dynamically-typed vs statically-typed >>> x = 1 >>>y = “hello” – Strongly-typed >>> x + y● Type function >>> type(x)● Integer vs float >>> z = 1.0
  10. 10. Python BasicsOperator Operation+ Addition- Subtraction/ Division* Multiplication** Power% Modulus
  11. 11. Python Basics● How will this be evaluated? >>>X = 1 + 2 – 3 ** 4 * ( 5+6)
  12. 12. Python Basics● Operator Precedence rules Parenthesis Power Multiplication Addition Left-to-right
  13. 13. Python Basics● Integer division >>> 4/2 >>> 5/2● Mixing integer and floats >>> 5/2.0● Casting between integer and floats >>> float(5) >>>int(5.0)
  14. 14. Python Basics Strings – sequence of characters >>> s = “hello world”● Looking inside >>> s[0]● Concatenation >>> s = ”hello ” + “world”● Finding length >>> len(s)● Slicing >>> s = s[0:5]
  15. 15. Python Basics Handy String functions● find >>> s.find(e)● Replace >>> n = s.replace(e, a )● Make upper, lower >>> u = s.upper()
  16. 16. Python Basics● Get the second word world by slicing >>> “hello, world”[x:y]
  17. 17. Python Basics Lists – collection of values● Declaring >>> l = list() >>> l = []● Can hold different types >>> l = [1, a, [2, 3], 4] >>> l[2]● Appending >>> l.append(an item) >>>del(l[2])
  18. 18. Python Basics Lists – collection of values● Getting length >>> len(l)● Slicing >>> l[1:4]● Converting between strings and lists >>> strlist = “this is a string”.split(s) >>> “z”.join(strlist)
  19. 19. Python Basics● Append an item to the list within the list >>> l = [1, a, [2, 3], 4] >>> l = [1, a, [2, 3, 5], 4]
  20. 20. Python Basics● Handy functions Sum >>> sum([2, 3, 4]) Max >>> max([2, 3, 4]) Min >>> min([2, 3, 4])
  21. 21. Python Basics Dictionaries – key, value pairs Associative array, hash table● Declaring >>> d = dict() >>> d = {}● Setting a value >>> d[“event”] = “bootcamp” >>> d = {“event” : “bootcamp” }● Getting a value >>> d[“event”]
  22. 22. Python Basics Mutability● Mutable – can change – Lists, dictionary● Immutable – cannot change – Strings, tuples● Try set, del..
  23. 23. Python Basics Casting – numbers and strings● Strings and numbers >>> int(“234”) >>> str(234)
  24. 24. Python Basics● Importing modules >>> import math >>> math.sqrt(4) >>> from math import sqrt >>> sqrt(4)● dir() function >>> dir(math)
  25. 25. Python Basics● Basic I/O >>> name = raw_input() >>> name = raw_input(“Name: “) Input numbers: >>>age = raw_input(“Age: “) >>>age = int(raw_input(“Age: “))
  26. 26. Modules● Interactive mode vs modules● Indentation
  27. 27. Boolean Values● True >>> 1 < 2● False >>> 1 > 2● Also evaluate to False: “”, [], {}, 0
  28. 28. Loops● While loop – while condition is true x=0 while x < 10: print x x=x+1● For loop – loops over items words = [this , is, a, list] for w in words: print w● Loop over strings, dictionaries..● Range() function >>> range(3) >>> range(0, 10, 2)
  29. 29. Functions● Defining functions def say_hello(): print “hello”● Calling functions say_hello()● Parameters def sub(a, b): s=a-b return s sub(b=3, a=2)
  30. 30. Functions● Commenting in Python def sub(a, b): d = a – b #subtracts b from a return d● Doc strings def sub(a, b): “””this functions takes in 2 integers and returns their difference””” d=a–b return d
  31. 31. File I/O● Writing to a file f = open(text.txt, wb) f.write(This is a line.n) f.close()● Reading a file f = open(text.txt, rb) stream = f.read() f.close()
  32. 32. Accessing the Web● Establishing a connection – sockets● Requests and Responses – GET, retrieve a webpage – POST, save data● Download a webpage fopen = urllib.urlopen(“http://www.google.com”) data = fopen.read()
  33. 33. Demo● Web demo● Scientific computing
  34. 34. Next Steps● Intermediate topics: – Classes and objects in Python – Regular Expressions – Exceptions etc● Python on Appengine● Python user group
  35. 35. Resources● Official Python Docs tutorial http://docs.python.org/2/tutorial/● A byte of Python http://www.swaroopch.com/notes/python/● Think like a Computer Scientist http://www.openbookproject.net/thinkcs/python/english2e/

×