Skip to content

Instantly share code, notes, and snippets.

@stenof
stenof / gist:30c71b99810fb8ca234b
Last active August 29, 2015 14:05
Learning python:
'''
Create three dictionaries: lloyd, alice, and tyler.
Give each dictionary the keys "name", "homework", "quizzes", and "tests".
Have the "name" key be the name of the student (that is, lloyd's name should be "Lloyd")
and the other keys should be an empty list.
'''
lloyd = {
"name":"Lloyd",
@stenof
stenof / gist:42959f390fbcc7bf95bf
Created August 27, 2014 15:18
Learning python: a day at the supermarket 3
# Now you need your compute_bill function to take the stock/inventory of a particular item into account when computing the cost.
Ultimately, if an item isn't in stock, then it shouldn't be included in the total. You can't buy or sell what you don't have!
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 6,
"apple": 0,
@stenof
stenof / gist:674879d63be61fc11868
Last active September 13, 2015 18:32
Learning python: a day at the supermarket
# Create a new dictionary called prices using {} format like the example above.
# Put these values in your prices dictionary, in between the {}:
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
@stenof
stenof / gist:05aa558b2a4ac2689ed8
Created August 27, 2014 13:36
Learning python: String Looping
# Strings are like lists with characters as elements. You can loop through strings the same way
for letter in "Codecademy":
print letter
# Empty lines to make the output pretty
print
print
word = "Programming is fun!"
@stenof
stenof / gist:a8d776099abf1f4ae856
Created August 27, 2014 13:23
Learning python: Lists + Functions
# Functions can also take lists as inputs and perform various operations on those lists.
def count_small(numbers):
total = 0
for n in numbers:
if n < 10:
total = total + 1
return total
lost = [4, 8, 15, 16, 23, 42]
@stenof
stenof / gist:eff3df83af66c55d1fe0
Created August 27, 2014 13:11
Learning python: Control Flow and Looping
# The blocks of code in a for loop can be as big or as small as they need to be.
# While looping, you may want to perform different actions depending on the particular item in the list.
# example
numbers = [1, 3, 4, 7]
for number in numbers:
if number > 6:
print number
@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.
@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: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: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']