Last active
August 29, 2015 13:58
-
-
Save houseofjeff/9982519 to your computer and use it in GitHub Desktop.
Python Intro - #2 - Tuples
This file contains 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 back for Jeff's second 5 minute intro to Python. | |
# This time we'll talk about the list's immutable cousin, the tuple. | |
from collections import namedtuple | |
def main(): | |
# tuples can be considered as immutable lists. A lot of what | |
# I'm going to show you here can also be done with lists, but | |
# it seems to fit better here. | |
name = ("John", "Q", "Public") | |
# A common misconception is that parenthesis are what specify | |
# a tuple, but it's actually the comma. That's why a 1-tuple | |
# looks like this: | |
firstonly = ("John", ) | |
assert len(firstonly) == 1 # even though it looks like it should be two | |
# You can assign a tuple to multiple variables | |
first, middle, last = name | |
print "{2}, {0} {1}.".format(first, middle, last) | |
# You can use the tuple in place of all three variables as well, | |
# though you have to put a * in there. | |
print "{2}, {0} {1}.".format( *name ) | |
# because you can assign a tuple to multiple variables, and you don't | |
# need the parens to define a tuple, you end up with this well worn | |
# chestnut for swapping two variables | |
a, b = "first", "second" | |
a, b = b, a | |
print a, b | |
# or more interesting | |
a, b = 0, 1 | |
for _ in xrange(20): | |
print a | |
a, b = b, a+b | |
print "Fibiotastic!" | |
# xrange(20) returns an iterator that runs 0..19. range(20) would | |
# actually return the list [0, 1, 2, ... , 18, 19], which is suboptimal | |
# for large numbers, so just get used to xrange(). Also, the _ here | |
# is just a dummy value, since we don't plan to use the iterated value. | |
#----------- | |
# Python's object model is ok, but a bit weak. You can do most of the | |
# typical things but, in particular if the object is just to store data, | |
# the language and libraries give you other options. Lists are one, | |
# and you'll often see people just storing data in a list and working | |
# with it as an array. But there's a nice solution with 'namedtuple'. | |
FullName = namedtuple( 'FullName', 'first, middle, last' ) | |
name = FullName( "John", "Q", "Public" ) | |
print name.first | |
print name.middle | |
print name.last | |
if __name__ == "__main__": | |
# this is a common idiom. By default python will just start | |
# at the top and run, but coming from a C background, you | |
# might be expecting a main() function. This checks to see | |
# if the program is being run stand-alone (__name__ == "__main__") | |
# and executes main(). If it were imported as a module in another | |
# application, then main() wouldn't run. This is useful for | |
# setting up unit tests, etc. | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment