Skip to content

Instantly share code, notes, and snippets.

@stenof
stenof / gist:a97b741f33488dfdbfa6
Created September 17, 2014 17:04
Learning python: Using strings in lists in functions
# Define a function called join_strings accepts an argument called words. It will be a list.
# Inside the function, create a variable called result and set it to "", an empty string.
# Iterate through the words list and append each word to result.
# Finally, return the result.
# Don't add spaces between the joined strings!
n = ["Michael", "Lieberman"]
def join_strings(words):
result = ""
@stenof
stenof / gist:47d2e007371a657e93b0
Created September 17, 2014 16:00
Learning python: Passing a range into a function
# The Python range() function is just a shortcut for generating a list, so you can use ranges in all
# the same places you can use lists.
range(6) # => [0,1,2,3,4,5]
range(1,6) # => [1,2,3,4,5]
range(1,6,3) # => [1,4]
# The range function has three different versions:
range(stop)
@stenof
stenof / gist:5e46d58a94106525dcc2
Created September 17, 2014 15:00
Learning python: Printing out a list item by item in a function
# Define a function called print_list that has one argument called x.
# Inside that function, print out each element one by one.
# Then call your function with the argument n.
n = [3, 5, 7]
def print_list(x):
for i in range(0, len(x)):
print x[i]
@stenof
stenof / gist:0a249ec4f489838b43e9
Last active August 29, 2015 14:06
Learning python: Modifying an element of a list in a function and List manipulation in functions
# Add 3 to the item at index one of the list.
# Store the result back into index one.
# Return the list.
def list_function(x):
x[1] = x[1] + 3
return x
@stenof
stenof / gist:25d188195f22a64ffa30
Created September 17, 2014 14:31
Learning python: Passing a list to a function and Using an element from a list in a function
# You pass a list to a function the same way you pass any other argument to a function.
def list_function(x):
return x
n = [3, 5, 7]
print list_function(n)
# list_function returns only the item stored in index one of x, rather than the entire x list.
@stenof
stenof / gist:f76b7a5a3269caf9d307
Created September 17, 2014 14:22
Learning python: Strings in functions and String concatenation
# Write a function called string_function that takes in a string argument (s) and then
# returns that argument concatenated with the word 'world'. Don't add a space before word!
n = "Hello"
def string_function(s):
return s + "world"
print string_function(n)
@stenof
stenof / gist:a9708fb44d2c2694b9b0
Last active August 29, 2015 14:06
learning python: Appending to a list, Removing elements from lists
#######################
# Appending to a list #
#######################
n = [1, 3, 5]
# Append the number 4 to the end of the list n.
n.append(4)
print n
@stenof
stenof / gist:bb52890527b62f0522b1
Last active August 29, 2015 14:05
Learning Python: List accessing, List element modification
#####################
# List accessing ####
#####################
# Please add the code to print out the second element in the list.
n = [1, 3, 5]
print n[1]
#############################
@stenof
stenof / gist:acd20c181e880dd9727b
Last active August 12, 2017 18:35
Learning Python:
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
@stenof
stenof / gist:0985e3044767f67469fc
Created August 27, 2014 17:31
learning python:
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],