SlideShare a Scribd company logo
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
Ad

Recommended

Advanced regular expressions
Advanced regular expressions
Neha Jain
 
Introducing Regular Expressions
Introducing Regular Expressions
Neha Jain
 
Presentation 2
Presentation 2
Matthew Haveard
 
Operator precedance parsing
Operator precedance parsing
sanchi29
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Erlang Concurrency
Erlang Concurrency
Barry Ezell
 
Operator precedence
Operator precedence
Akshaya Arunan
 
Python Workshop
Python Workshop
Assem CHELLI
 
Pg92 HA, LCA 2012, Ballarat
Pg92 HA, LCA 2012, Ballarat
Selena Deckelmann
 
Job Hunting Under Duress
Job Hunting Under Duress
Matthew McCullough
 
Leading Without Being In Charge
Leading Without Being In Charge
Selena Deckelmann
 
Techno Parenting
Techno Parenting
Patrick Woessner
 
7th grade presentation cite1
7th grade presentation cite1
Patrick Woessner
 
Flat Class Workshop
Flat Class Workshop
Patrick Woessner
 
7th grade presentation cite2
7th grade presentation cite2
Patrick Woessner
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
Marko Gargenta
 
Perl Presentation
Perl Presentation
Sopan Shewale
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 
Terms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explained
clintongormley
 
Python Programming - Chapter 3
Python Programming - Chapter 3
Maria Joslin
 
Groovy every day
Groovy every day
Paul Woods
 
Design Patterns in Ruby
Design Patterns in Ruby
Aleksander Dąbrowski
 
Introduction to Erlang
Introduction to Erlang
Gabriele Lana
 
F# Presentation
F# Presentation
mrkurt
 
Prototype js
Prototype js
mussawir20
 
03 Php Array String Functions
03 Php Array String Functions
Geshan Manandhar
 
Php Using Arrays
Php Using Arrays
mussawir20
 
Python And GIS - Beyond Modelbuilder And Pythonwin
Python And GIS - Beyond Modelbuilder And Pythonwin
Chad Cooper
 
Haml & Sass presentation
Haml & Sass presentation
bryanbibat
 

More Related Content

Viewers also liked (7)

Pg92 HA, LCA 2012, Ballarat
Pg92 HA, LCA 2012, Ballarat
Selena Deckelmann
 
Job Hunting Under Duress
Job Hunting Under Duress
Matthew McCullough
 
Leading Without Being In Charge
Leading Without Being In Charge
Selena Deckelmann
 
Techno Parenting
Techno Parenting
Patrick Woessner
 
7th grade presentation cite1
7th grade presentation cite1
Patrick Woessner
 
Flat Class Workshop
Flat Class Workshop
Patrick Woessner
 
7th grade presentation cite2
7th grade presentation cite2
Patrick Woessner
 

Similar to Intro python (20)

Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
Marko Gargenta
 
Perl Presentation
Perl Presentation
Sopan Shewale
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 
Terms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explained
clintongormley
 
Python Programming - Chapter 3
Python Programming - Chapter 3
Maria Joslin
 
Groovy every day
Groovy every day
Paul Woods
 
Design Patterns in Ruby
Design Patterns in Ruby
Aleksander Dąbrowski
 
Introduction to Erlang
Introduction to Erlang
Gabriele Lana
 
F# Presentation
F# Presentation
mrkurt
 
Prototype js
Prototype js
mussawir20
 
03 Php Array String Functions
03 Php Array String Functions
Geshan Manandhar
 
Php Using Arrays
Php Using Arrays
mussawir20
 
Python And GIS - Beyond Modelbuilder And Pythonwin
Python And GIS - Beyond Modelbuilder And Pythonwin
Chad Cooper
 
Haml & Sass presentation
Haml & Sass presentation
bryanbibat
 
My First Rails Plugin - Usertext
My First Rails Plugin - Usertext
frankieroberto
 
Antlr V3
Antlr V3
guest5024494
 
C to perl binding
C to perl binding
Shmuel Fomberg
 
Ods Markup And Tagsets: A Tutorial
Ods Markup And Tagsets: A Tutorial
simienc
 
Python scripting kick off
Python scripting kick off
Andrea Gangemi
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
Marko Gargenta
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
climatewarrior
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
Ankur Shrivastava
 
Terms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explained
clintongormley
 
Python Programming - Chapter 3
Python Programming - Chapter 3
Maria Joslin
 
Groovy every day
Groovy every day
Paul Woods
 
Introduction to Erlang
Introduction to Erlang
Gabriele Lana
 
F# Presentation
F# Presentation
mrkurt
 
03 Php Array String Functions
03 Php Array String Functions
Geshan Manandhar
 
Php Using Arrays
Php Using Arrays
mussawir20
 
Python And GIS - Beyond Modelbuilder And Pythonwin
Python And GIS - Beyond Modelbuilder And Pythonwin
Chad Cooper
 
Haml & Sass presentation
Haml & Sass presentation
bryanbibat
 
My First Rails Plugin - Usertext
My First Rails Plugin - Usertext
frankieroberto
 
Ods Markup And Tagsets: A Tutorial
Ods Markup And Tagsets: A Tutorial
simienc
 
Python scripting kick off
Python scripting kick off
Andrea Gangemi
 
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/