Skip to content

Instantly share code, notes, and snippets.

@rmatei
Created July 17, 2010 19:55
Show Gist options
  • Save rmatei/479796 to your computer and use it in GitHub Desktop.
Save rmatei/479796 to your computer and use it in GitHub Desktop.
# MATH
>>> 5+5
10
>>> (5+5)/20
0
>>> (5+5)/20.0
0.5
# two thirds?
# VARIABLES
>>> x=5+5
>>> x
10
>>> y=x*40
>>> y
400
# y squared? (google it)
# STRINGS
>>> name = "Jerome"
>>> greeting = "Hello " + name
>>> greeting
'Hello Jerome'
>>> dir(greeting)
[..., 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> greeting.lower()
'hello jerome'
# replace 'Hello' with 'Goodbye'
# LISTS
>>> l=[3,4,1,2]
>>> dir(l)
[..., 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> l.append()
TypeError: append() takes exactly one argument (0 given)
>>> l
[3, 4, 1, 2]
>>> l.append(5)
>>> l
[3, 4, 1, 2, 5]
>>> l.reverse()
>>> l
[5, 2, 1, 4, 3]
# sort l?
# CHANGING LISTS
>>> double=[2*x for x in l]
>>> double
[2, 4, 6, 8, 10]
# cube each number and store in a new list? (google it)
# DICTIONARIES
>>> cities = {'CA':['San Francisco', 'Los Angeles'], 'NY':['New York City']}
>>> cities['CA']
['San Francisco', 'Los Angeles']
>>> cities['NY']
['New York City']
# add San Diego to California cities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment