I'm Still Learning Programming,  Motherfucker! Or Beginning Python from a Beginner's Perspective Kami Lott
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?
Operators + Addition - Subtraction * Multiplication / Division % Modulus ** Exponent == Are values equal != Are values not equal > Greater than < Less than >= Greater than or equal <= Less than or equal = Assignment
+= Add and assignment
-= Subtract and assignment
*= Multiply and assignment
/= Divide and assignment
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
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
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.
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
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
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
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)
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

More Related Content

PDF
Advanced regular expressions
PDF
Introducing Regular Expressions
ODP
Presentation 2
PPTX
Operator precedance parsing
PPTX
Python advanced 2. regular expression in python
PPT
Erlang Concurrency
PDF
Operator precedence
PPTX
Python Workshop
Advanced regular expressions
Introducing Regular Expressions
Presentation 2
Operator precedance parsing
Python advanced 2. regular expression in python
Erlang Concurrency
Operator precedence
Python Workshop

Viewers also liked (7)

PDF
Pg92 HA, LCA 2012, Ballarat
PDF
Job Hunting Under Duress
PDF
Leading Without Being In Charge
PPT
Techno Parenting
PPT
7th grade presentation cite1
PPT
Flat Class Workshop
PPTX
7th grade presentation cite2
Pg92 HA, LCA 2012, Ballarat
Job Hunting Under Duress
Leading Without Being In Charge
Techno Parenting
7th grade presentation cite1
Flat Class Workshop
7th grade presentation cite2
Ad

Similar to Intro python (20)

ODP
Why Python by Marilyn Davis, Marakana
PPT
Perl Presentation
ODP
Python quickstart for programmers: Python Kung Fu
PDF
Python Workshop Part 2. LUG Maniapl
ODP
Terms of endearment - the ElasticSearch Query DSL explained
PPT
Python Programming - Chapter 3
PPT
Groovy every day
ODP
Design Patterns in Ruby
PDF
Introduction to Erlang
PPTX
F# Presentation
PPT
Prototype js
PPT
03 Php Array String Functions
PPT
Php Using Arrays
PPT
Python And GIS - Beyond Modelbuilder And Pythonwin
ODP
Haml & Sass presentation
PDF
My First Rails Plugin - Usertext
PPT
Antlr V3
PPTX
C to perl binding
PPT
Ods Markup And Tagsets: A Tutorial
PPT
Python scripting kick off
Why Python by Marilyn Davis, Marakana
Perl Presentation
Python quickstart for programmers: Python Kung Fu
Python Workshop Part 2. LUG Maniapl
Terms of endearment - the ElasticSearch Query DSL explained
Python Programming - Chapter 3
Groovy every day
Design Patterns in Ruby
Introduction to Erlang
F# Presentation
Prototype js
03 Php Array String Functions
Php Using Arrays
Python And GIS - Beyond Modelbuilder And Pythonwin
Haml & Sass presentation
My First Rails Plugin - Usertext
Antlr V3
C to perl binding
Ods Markup And Tagsets: A Tutorial
Python scripting kick off
Ad

Intro python

  • 1. I'm Still Learning Programming, Motherfucker! Or Beginning Python from a Beginner's Perspective Kami Lott
  • 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. Operators + Addition - Subtraction * Multiplication / Division % Modulus ** Exponent == Are values equal != Are values not equal > Greater than < Less than >= Greater than or equal <= Less than or equal = Assignment
  • 4. += Add and assignment
  • 5. -= Subtract and assignment
  • 6. *= Multiply and assignment
  • 7. /= Divide and assignment
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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/