Last active
August 29, 2015 13:58
-
-
Save houseofjeff/9970511 to your computer and use it in GitHub Desktop.
Python Intro - #1 - Lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Welcome to Jeff's first 5 minute intro to Python. | |
# More illustrative of a language than just the syntax, I think | |
# how a language handles data says a lot. So, I'm going to talk | |
# mostly about that, and let's start with lists. | |
# Since we all know how to program, I'm going to just leave a lot to | |
# be picked up by example rather than by exposition. | |
# --- save as 'listsarecool.py' | |
import string | |
def main(): | |
# lists are first class entities in Python | |
colors = [ 'red', 'yellow', 'black', 'blue', 'brown', 'white' ] | |
# they follow standard array semantics with some nice enhancements: | |
firstcolor = colors[0] | |
lastcolor = colors[-1] | |
blacknblue = colors[2:4] | |
everysecondcolor = colors[::2] | |
colorsreversed = colors[::-1] | |
print "Lists can be joined together as strings like this: {0}".format( ", ".join(colors) ) | |
# oops, I forgot one, but lists are mutable. | |
colors.append('orange') | |
# a linear search for existence of an element in a list is simple: | |
if 'milk' in colors: | |
print "Wow, it turns out milk is a color" | |
else: | |
print "Everyone knows that 'milk' is actually a number" | |
# 'in' is also used during iteration, this is the prefered form | |
for color in colors: | |
print color | |
# You can transform lists with list comprehensions, which wrap the iteration | |
reversedcolors = [ color[::-1] for color in colorsreversed ] | |
print "In reverse: {0}".format( ", ".join(reversedcolors) ) | |
# they can also do filtering: | |
fiveletters = [ color for color in colors if len(color) == 5 ] | |
print "These colors have 5 letters: ", ", ".join(fiveletters) | |
# essentially, a list comprehension is syntactic sugar for map() & filter() | |
# the following is the same as: | |
# print [color[::-1] for color in colors if len(color) == 5] | |
is_len_five = lambda s: len(s) == 5 | |
reverse_string = lambda s: s[::-1] | |
fivereversed = map( reverse_string, filter(is_len_five, colors) ) | |
print fivereversed | |
# btw: strings can be treated as lists of characters | |
phone = '(260) 555-1212' | |
justdigits = [c for c in phone if c in string.digits] | |
normalized_phone = "".join(justdigits) | |
print "normalized {0} -> {1}".format(phone, normalized_phone) | |
# There are some custom classes for supporting more complex data | |
# structures, but the .pop(n) method lets you treat a list as | |
# a stack or a queue easily enough. | |
stack = [] | |
stack.append( 1 ) # it would be nice if it were named 'push' but oh well | |
stack.append( 2 ) | |
top = stack.pop(-1) | |
assert top == 2 | |
queue = [] | |
queue.append( 1 ) | |
queue.append( 2 ) | |
head = queue.pop(0) | |
assert head == 1 | |
if __name__ == "__main__": | |
main() | |
# --- end 'listsarecool.py' | |
# You can execute it thusly: python listsarecool.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment