Python 101  Alan Tree 08/24/2011
What is Python?● Python is a powerful scripting language developed by  Guido van Rossum in the late 80s.● Python is a full...
Why Python?● Readability● Easy to learn● Easy to write● Cross platform● Powerful● Extensive uses in many areas of developm...
Who uses Python?● Google● Yahoo● YouTube● Industrial Light and Magic● Disney● EVE Online● RackSpace● AstraZeneca● HoneyWell
Popular Applications using Python● Google ● Yahoo● Blender● GIMP● BitTorrent● Civ 4 ● BattleField 2 ● Eve Online● Ubuntu P...
Other uses for Python1. IT - Various scripting tasks (nix, OSX, Windows)2. Web Development (Django, pylons, Grok, TurboGea...
Ease of use
IDLE● Able to modify code and run code directly from editor.● Code is stored in simple text files with the .py extension.●...
Basics
Variables and DataTypes
Stringss = alan s = "alan" s = al + an>>> alan s = "%s%s" % (al, an) >>> alan
Integersx=2 y=4 z=x+y z=z/2>>> 3   
Floatsz = 0.1 y = 1.2 m = z+y>>> 1.3  
Dictt = {} t = {name:alan, age:30}
Listl = [] l  = [alan, 30] l = [alan, 30, apples, {lname:tree}]
Tuple t = [alan, 30] t[1] = 5
Loopswhile (condition):    do this for i in range (100):     do this a bunch peeps_list = [alan, joe, frank]for i in peeps...
Conditionalsif condition:    do something if condition:    do thiselse:    do that  
Functionsdef my_function():    print "this is my function"    return  my_function()>>> this is my function
Codez time
Hello Worldprint "Hello World"
Hello {Your name here}your_name = "Alan" print "Hello %s" % (your_name)
Hello {What is your name?}your_name = raw_input ("What is your name? : ") print "Hello %s" % (your_name)
Hello Conditionalyour_age = int(raw_input("How old are you? :")) if your_age >= 30:    print "hmm, getting up there arnt w...
Hello {While Loop}your_name = "" while your_name != "Alan":    your_name = raw_input ("What is your name? : ")print "Acces...
Questions?
Workshop Easy     Hi - Low
Write a program that will:Guess a random number and have the user try to guess thatrandom number by providing the clues go...
Write a program that will:Guess a random number and have the user try to guess thatrandom number by providing the clues go...
Workshop Easy Solution:import randomguess = 0number = random.randint(1,10)print "I have chosen a number between 1 and 10. ...
Workshop Advanced   Fetch Search Results
Write a program that will:Fetch the sports located in active search and display a menuthat will allow the user to choose a...
Things to note:You will need these libraries:import urllibimport jsonYou will need this base url:base_url = http://api.amp...
Workshop Advanced HELP 1import urllibimport jsonbase_url = http://api.amp.active.com/search?f=activities&v=json&api_key=wu...
Workshop Advanced HELP 2for i in range(1, len(responses)):   print "%s - %s" % (i, responses[i][title].split(|)[0])more_de...
the end
questions?
Upcoming SlideShare
Loading in...5
×

Python 101

7,744

Published on

Alan Tree, Software Engineer at The Active Network, walks us through the fundamentals of Python.

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,744
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python 101

  1. 1. Python 101 Alan Tree 08/24/2011
  2. 2. What is Python?● Python is a powerful scripting language developed by Guido van Rossum in the late 80s.● Python is a fully functional language while maintaining readability and understandability.● Python is a common language that ships with most nix distros.● Python is very extendable ● Python is both dynamically typed and strongly typed at the same time.
  3. 3. Why Python?● Readability● Easy to learn● Easy to write● Cross platform● Powerful● Extensive uses in many areas of development
  4. 4. Who uses Python?● Google● Yahoo● YouTube● Industrial Light and Magic● Disney● EVE Online● RackSpace● AstraZeneca● HoneyWell
  5. 5. Popular Applications using Python● Google ● Yahoo● Blender● GIMP● BitTorrent● Civ 4 ● BattleField 2 ● Eve Online● Ubuntu Package Manager
  6. 6. Other uses for Python1. IT - Various scripting tasks (nix, OSX, Windows)2. Web Development (Django, pylons, Grok, TurboGears, CherryPy, Flask, Google App Engine)3. Game Development (Panda3d, pyGame, Pyglet, PIL, Python-Ogre, Soya3d)4. Desktop Development (GUI development)
  7. 7. Ease of use
  8. 8. IDLE● Able to modify code and run code directly from editor.● Code is stored in simple text files with the .py extension.● No open and close statements or curly braces. Indention determines blocks of code. ● Comments are achieved with # 
  9. 9. Basics
  10. 10. Variables and DataTypes
  11. 11. Stringss = alan s = "alan" s = al + an>>> alan s = "%s%s" % (al, an) >>> alan
  12. 12. Integersx=2 y=4 z=x+y z=z/2>>> 3   
  13. 13. Floatsz = 0.1 y = 1.2 m = z+y>>> 1.3  
  14. 14. Dictt = {} t = {name:alan, age:30}
  15. 15. Listl = [] l  = [alan, 30] l = [alan, 30, apples, {lname:tree}]
  16. 16. Tuple t = [alan, 30] t[1] = 5
  17. 17. Loopswhile (condition):    do this for i in range (100):     do this a bunch peeps_list = [alan, joe, frank]for i in peeps_list:    print i 
  18. 18. Conditionalsif condition:    do something if condition:    do thiselse:    do that  
  19. 19. Functionsdef my_function():    print "this is my function"    return  my_function()>>> this is my function
  20. 20. Codez time
  21. 21. Hello Worldprint "Hello World"
  22. 22. Hello {Your name here}your_name = "Alan" print "Hello %s" % (your_name)
  23. 23. Hello {What is your name?}your_name = raw_input ("What is your name? : ") print "Hello %s" % (your_name)
  24. 24. Hello Conditionalyour_age = int(raw_input("How old are you? :")) if your_age >= 30:    print "hmm, getting up there arnt we?"else:    print "young grass hopper!" 
  25. 25. Hello {While Loop}your_name = "" while your_name != "Alan":    your_name = raw_input ("What is your name? : ")print "Access Granted!!!"     
  26. 26. Questions?
  27. 27. Workshop Easy Hi - Low
  28. 28. Write a program that will:Guess a random number and have the user try to guess thatrandom number by providing the clues go higher or go lower depending on the users guess. You will need this at the top of your program:import random You will need to get your random number like this:random_number = random.randint(1,10)  
  29. 29. Write a program that will:Guess a random number and have the user try to guess thatrandom number by providing the clues go higher or go lower depending on the users guess.  Hint:import randomyour_guess = 0random_number = random.randint(1,10)while {something doesnt match...}:
  30. 30. Workshop Easy Solution:import randomguess = 0number = random.randint(1,10)print "I have chosen a number between 1 and 10. Try to guess it!"while guess != number : guess = int(raw_input("Your guess? :")) if guess < number: print "go higher" if guess > number: print "go lower"print "You guessed it!"
  31. 31. Workshop Advanced Fetch Search Results
  32. 32. Write a program that will:Fetch the sports located in active search and display a menuthat will allow the user to choose a sport. Once a sport isselected, have the user provide skills they can use to refinetheir search. Once the search is conducted and an item isfound, present a menu so they may see more details aboutthat event. 
  33. 33. Things to note:You will need these libraries:import urllibimport jsonYou will need this base url:base_url = http://api.amp.active.com/search?f=activities&v=json&api_key=wuhmn9ye94xn3xnteudxsavwYou will need to url encode the keywords:url_with_keywords = urllib.quote_plus(key_words)You will need to parse json:json.loads(<json_string>) You will need to use some of this:"my_string".split("|")"my_string".strip() 
  34. 34. Workshop Advanced HELP 1import urllibimport jsonbase_url = http://api.amp.active.com/search?f=activities&v=json&api_key=wuhmn9ye94xn3xnteudxsavwsports = [Baseball, Basketball, Football, Golf, Outdoors, Running, Walking]for sport in sports: print "%s - %s" % (sports.index(sport) + int(1), str(sport))print "n"the_sport = int(raw_input("What sport would you like to search for?"))the_keywords = raw_input("Enter any keywords (or press ENTER for now): ")print "n"new_url = "%s&m=meta:channel%%3D%s" % (base_url, sports[the_sport])if the_keywords.strip() != : new_url = "%s&k=%s" % (new_url, urllib.quote_plus(the_keywords))responses = urllib.urlopen(new_url).read()responses = json.loads(responses)[_results]
  35. 35. Workshop Advanced HELP 2for i in range(1, len(responses)): print "%s - %s" % (i, responses[i][title].split(|)[0])more_details = int(raw_input("Choose an event to see more details: "))result = responses[more_details]print "n"print "Title: %s" % (result[title].split(|)[0])print "Location: %s, %s" % (result[meta][city], result[meta][state])print "Start Date: %s" % (result[meta][startDate])print "Assed ID: %s" % (result[meta][assetTypeId])
  36. 36. the end
  37. 37. questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×