Skip to content

Instantly share code, notes, and snippets.

@stenof
stenof / gist:7046c7383251e089580a
Created August 26, 2014 14:22
Learning python: Introduction to Lists
'''
Lists are a datatype you can use to store a collection of different pieces of information as a sequence under
a single variable name. (Datatypes you've already learned about include strings, numbers, and booleans.)
'''
# You can assign items to a list with an expression of the form
list_name = [item_1, item_2]
# with the items in between brackets. A list can also be empty:
@stenof
stenof / gist:d28708eb0e8eaaf6bf56
Last active August 29, 2015 14:05
Learning Python: Access by Index
'''
You can access an individual item on the list by its index. An index is like an address that identifies the
item's place in the list. The index appears directly after the list name, in between brackets, like this:
'''
list_name[index]
'''
List indices begin with 0, not 1! You access the first item in a list like this: list_name[0].
The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero.
@stenof
stenof / gist:92a92d306fe846a8a611
Created August 26, 2014 14:55
Learning pythons: Late Arrivals & List Length & List Slicing
# A list doesn't have to have a fixed length. You can add items to the end of a list any time you like!
letters = ['a', 'b', 'c']
letters.append('d')
print len(letters)
print letters
# example
suitcase = []
suitcase.append("sunglasses")
@stenof
stenof / gist:e9ef9598df5b6eec9280
Created August 26, 2014 16:13
Learning python: Maintaining Order
# Sometimes you need to search for an item in a list.
animals = ["ant", "bat", "cat"]
print animals.index("bat")
# First, we create a list called animals with three strings..
# Then, we print the first index that contains the string "bat", which will print 1.
# We can also insert items into a list.
animals.insert(1, "dog")
@stenof
stenof / gist:8532b5c9845cfde5c8df
Last active August 29, 2015 14:05
Learning python: For One and All & More with 'for'
# If you want to do something with every item in the list, you can use a for loop
for variable in list_name:
# Do stuff!
# A variable name follows the for keyword; it will be assigned the value of each list item in turn.
# Then in list_name designates list_name as the list the loop will work on. The line ends with a colon (:)
# and the indented code that follows it will be executed once per item in the list.
@stenof
stenof / gist:e9cd4801b7e83f4242bc
Last active August 29, 2015 14:05
Learning python: Dictionary
##############
# dictionary #
##############
# A dictionary is similar to a list, but you access values by looking up a key instead of an index.
# A key can be any string or number. Dictionaries are enclosed in curly braces, like so:
d = {'key1' : 1, 'key2' : 2, 'key3' : 3}
@stenof
stenof / gist:23af8903cfeb77aae5da
Created August 27, 2014 10:09
Learning Python: remove something from a list
# Sometimes you need to remove something from a list.
# example
beatles = ["john","paul","george","ringo","stuart"]
beatles.remove("stuart")
print beatles
backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
@stenof
stenof / gist:6657f8a1fe8a848ddd24
Created August 27, 2014 12:46
Learning python: dictionaries 2
# Let's go over a few last notes about dictionaries
my_dict = {
"fish": ["c", "a", "r", "p"],
"cash": -4483,
"luck": "good"
}
print my_dict["fish"][0]
# 1. In the example above, we created a dictionary that holds many types of values.
@stenof
stenof / gist:e130ce6aacfa3ee80253
Created August 27, 2014 12:56
Learning Python: FOR loop in a LIST
# for loops allow us to iterate through all of the elements in a list from the left-most (or zeroth element)
# to the right-most element. A sample loop would be structured as follows:
a = ["List of some sort”]
for x in a:
# Do something for every x
# This loop will run all of the code in the indented block under the for x in a: statement.
# The item in the list that is currently being evaluated will be x. So running the following:
@stenof
stenof / gist:8be3ff4b992e18b27feb
Created August 27, 2014 13:02
Learning python: FOR loop on a DICTIONARY
# You can also use a for loop on a dictionary to loop through its keys with the following:
# A simple dictionary
d = {"foo" : "bar"}
for key in d:
print d[key] # prints "bar"
# Note that dictionaries are unordered, meaning that any time you loop through a dictionary,
# you will go through every key, but you are not guaranteed to get them in any particular order.