Intro python

719 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
719
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

Intro python

  1. 1. I'm Still Learning Programming, Motherfucker! Or Beginning Python from a Beginner's Perspective Kami Lott
  2. 2. Python is an open-source, object-oriented, functional language that was created by Guido van Rossom in 1991. In Python, everything is an object. Data types: Strings, Lists, Dictionaries, and Tuples Whitespace... Code blocks are marked by indenting and new lines. See PEP8 for more details. What is Python?
  3. 3. Operators <ul>+ Addition - Subtraction * Multiplication / Division % Modulus ** Exponent == Are values equal != Are values not equal > Greater than < Less than </ul><ul>>= Greater than or equal <li><= Less than or equal </li></ul><ul><li>= Assignment
  4. 4. += Add and assignment
  5. 5. -= Subtract and assignment
  6. 6. *= Multiply and assignment
  7. 7. /= Divide and assignment </li></ul>
  8. 8. Strings Strings are immutable objects. They are marked by quotes... single or double whatever you use just be consistent. Use triple quotes for blocks of lines. Strings use operators. >>> x = 'spam' >>> y = “eggs” >>> x + y 'spameggs' >>> x + ' ' + y 'spam eggs' >>>print ' '.join([x,y]) spam eggs
  9. 9. String Methods s = 'a string to see methods' len(s) = 23 s[0] = 'a', s[19] = 'h', s[-1] = 's' s.find('g') -> 7 # if not found, will return -1 s.index('s') -> 2 # gives position of first occurrence s.count('s') -> 3 s.upper() -> 'A STRING TO SEE METHODS' s.title() -> 'A String To See Methods' s.strip('a') -> ' string to see methods' s.isdigit() -> False # Boolean test
  10. 10. Strings Assigning 2 variables to same object: >>> i = 'chunky bacon' >>> j = I >>>j 'chunky bacon' >>>id(i) == id(j) True String formatting and embedded variables: >>>name = “Kami” >>>height = 64 # inches >>>print “My name is %s and I am %d inches tall.” % (name, height) My name is Kami and I am 64 inches tall.
  11. 11. Lists Lists are mutable. They are marked by [ ]. Lists can contain strings, integers, tuples, and even lists. Built in methods of lists include: append(), extend(), pop(), reverse(), index() sort() vs sorted(): list.sort() -> sorts list in place new_list = sorted(list) -> makes a copy of list that is now sorted
  12. 12. Lists range() ouputs a list: >>>k = range(6) >>>k [0, 1, 2, 3, 4, 5] >>>m = range(3, 7, 2) >>>m [3, 5] Iterators loop over object until StopIteration error >>> p = [1, 2] >>> it = reversed(p) >>>it.next() 2 >>>it.next() 1 >>>it.next() Traceback (most recent call last): File &quot;<stdin>&quot;, line 1, in <module> StopIteration
  13. 13. Lists Splicing: Used to pull out a chunk of the list [ start : stop : step ] If the step is -1, then list is reversed >>>mylist = [1, 2, 3, 4, 5, 6] >>>print mylist[2::2] [2, 4, 6] >>>print mylist[::-1] [6, 5, 4, 3, 2, 1] Positions of items in list: >>> mylist[0] 1 >>>mylist[-1] 6
  14. 14. List Comprehensions >>>print newlist = [x * 2 for x in range(5)] [0, 2, 4, 6, 8] Flatten a nested list: >>>nested = [[1, 2], [3], [4, 5]] # [1, 2, 3, 4, 5] is what you want >>>result = [item for sublist in nested for item in sublist] >>>result [1, 2, 3, 4, 5] result = [ ] for sublist in nested: for item in sublist: result.append(item)
  15. 15. Dictionaries {key1: value1, key2: value2} Order is not preserved in dictionaries. Keys tend to be strings, integers, and tuples. Need ability to change your keys, use lists or dictionaries. Operations for dictionary D: D[k] = x -> Assigning key k to value x del D[k] -> Deletes key k len(D) -> returns length of keys k in D -> checks to see if key k in dictionary D
  16. 16. Dictionaries >>>favthings = dict(food='sushi', color='pink', drink=('wine', 'beer')) >>>favthings {'color': 'pink', 'food': 'sushi', 'drink': ('wine', 'beer')} >>>favthings.keys() ['color', 'food', 'drink'] >>>favthings.items() [('color', 'pink'), ('food', 'sushi'), ('drink', ('wine', 'beer'))] >>>favthings.popitem() ('color', 'pink') >>>favthings {'food': 'sushi', 'drink': ('wine', 'beer')}
  17. 17. Tuples Like a list but immutable, marked by ( ) or ( ,) Use as keys in dictionaries, unless it contains a mutable object Functions: len(), min() and max() Methods: index(), count() and slicing The original tuple will remain unchanged
  18. 18. Simple Function song = ''' %d bottles of beer on the wall, %d bottles of beer, take one down, pass it around, %d bottles of beer on the wall! ''' bottles_of_beer = 99 while bottles_of_beer > 1: print song % (bottles_of_beer, bottles_of_beer, bottles_of_beer – 1) bottles_of_beer -= 1
  19. 19. Where To Go From Here... http://docs.python.org/ http://code.google.com/edu/languages/google-python-class/ http://diveintopython.org/ http://www.python.org/dev/peps/ (8, 20) http://greenteapress.com/thinkpython/html/index.html http://us.pycon.org/2010/tutorials/ http://pycon.blip.tv/

×