Skip to content

Instantly share code, notes, and snippets.

@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: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: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: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: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: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:206c56d331dad744c013
Last active August 29, 2015 14:05
Learning python: Function example 2
# What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money?
def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city == "Charlotte":
return (183)
@stenof
stenof / gist:0a98f30f68753b56a5c2
Last active August 29, 2015 14:03
Learning Python: Functions example
## Don't forget the colon at the end of the function definition!
def shut_down(s):
if s=="yes":
return("Shutting down")
elif s=="no":
return("Shutdown aborted")
@stenof
stenof / gist:ee2760c6493c0ccd06f7
Last active August 29, 2015 14:03
Learning Python: what is available in the math module
import math # Imports the math module
everything = dir(math) # Sets everything to a list of things from math
print everything # Prints 'em all!
@stenof
stenof / gist:d4a08365e7d711fa56b7
Last active August 29, 2015 14:03
Learning Python: Generic import, Function Imports and Universal Imports
"""
Here is a Python module named math that includes a number of useful variables and functions,
and sqrt() is one of those functions. In order to access math, all you need is the import keyword.
"""
#Generic import: Ask Python to print sqrt(25)
import math
print math.sqrt(25)