lis = ["The Poet X", "Electric Arches", "America Is Not The Heart", "Reliquaria"]
# If we wanted to print the element "Electric Arches":
print(lis[1])
lis = ["The Poet X", "Electric Arches", "America Is Not The Heart", "Reliquaria"]
sub_lis = lis[0:2]
# What do you think this will print?
print(sub_lis)
lis = ["The Poet X", "Electric Arches", "America Is Not The Heart", "Reliquaria"]
# Finish the following line so that your slice has "America Is Not the Heart" and "Reliquaria
sub_lis = lis[2:4]
print(sub_lis)
lis = ["The Poet X", "Electric Arches", "America Is Not The Heart", "Reliquaria"]
sub_lis = lis[:2]
print(sub_lis)
lis = ["The Poet X", "Electric Arches", "America Is Not The Heart", "Reliquaria"]
sub_lis = lis[2:]
print(sub_lis)
string = "This is a string!"
# Can you guess what this will print?
sub_string = string[10:]
print(sub_string)
string = "This is a string!"
# If I wanted the first character of this string, 'T', I would do:
first_char = string[0]
print("The first character of the string is", first_char)
# This is an empty list
lis = []
lis.append("My Hero Academia")
lis.append("Your Name")
print(lis)
lis = ["My Hero Academia", "Your Name"]
lis.insert(1, "Totoro")
print(lis)
lis = ["My Hero Academia", "Totoro", "Your Name", "Avatar: The Last Airbender"]
# Looks for the element "Totoro" and removes it
lis.remove("Totoro")
print(lis)
# Remove an element with this specific index
lis.pop(1)
print(lis)
# Removes the last element when no parameter is given
lis.pop()
print(lis)
lis = ["My Hero Academia", "Totoro", "Your Name"]
lis[1] = "Spirited Away"
print(lis)
tup = ("My Hero Academia", "Totoro")
print(tup)
print(tup[0])
tup = ("My Hero Academia", "Totoro")
tup[1] = "Spirited Away"
print(tup)
tup = ("My Hero Academia", "Totoro")
tup.append("Your Name")
print(tup)
some_list = ["Item 0", "Item 1"]
some_set = {"Some Item", "Another Item"}
print(some_list[1])
# The following line will cause an error:
print(some_set[1])
some_list = ["Item", "Item"]
some_set = {"Item", "Item"}
print(some_list)
print(some_set)
some_set = {"Item"}
print(some_set)
some_set.add("Other Item")
print(some_set)
some_set.remove("Item")
print(some_set)
fav_flavors = {"strawberry", "vanilla", "chocolate", "green tea", "green tea", "vanilla", "green tea"}
# Remember that once you add those items into the set, the duplicates go away
print(fav_flavors)
# Print the length of the set
print(len(fav_flavors))
# What if we tried this with a list?
fav_flavors = ["strawberry", "vanilla", "chocolate", "green tea", "green tea", "vanilla", "gr"]
print(fav_flavors)
# Print the length of the list
print(len(fav_flavors))
favAnimals = set()
for i in range(5):
animal = input("What is your favorite animal? ")
favAnimals.add(animal)
print(favAnimals)
# This is how you define a dictionary
definition = {
"flammable": "easily set on fire",
"inflammable": "easily set on fire",
"nonflammable": "not catching fire easily"
}
# In order to access an element in the dictionary, we refer to its "key"
print(definition["flammable"])
# Another example of a dictionary, this time with a mix of integer and string values
album_1 = {
"name": "Escape from New York",
"artist": "Beast Coast",
"year": 2019
}
# How would we print out the artist of album_1?
print(album_1["artist"])
album_2 = {
"name": "Sgt. Pepper's Lonely Hearts Club Band"
}
# Inserting a new pair, with "artist" as the key and "The Beatles" as its value
album_2["artist"] = "The Beatles"
print(album_2)
album_2["year"] = 1967
print(len(album_2))
print("artist" in album_2)
# Notice what the following will print:
print("The Beatles" in album_2)
# This is because we can only search a dictionary by key, not by value
# Deleting a key-value pair from the dictionary
del album_2["artist"]
print(album_2)
# If you just wanted the keys in a dictionary:
print(album_2.keys())
# Finally, if you want to clear out the entire dictionary:
album_2.clear()
print(album_2)
empty_dict = dict()
empty_set = set()
empty_list = []
empty_tuple = ()
dictionary = {
"Issa":"1",
"Daniel":"3",
"Molly":"2",
"Kelli":"3",
"Lawrence":"2"
}
print(dictionary)
# Some code to get you started:
pineapple_votes = {
"pineapple": 0,
"no pineapple": 0
}
for i in range(5):
num = int(input("Input 1 if you like pineapple on pizza. Input 0 if you don't like pineapple."))
if num == 1:
pineapple_votes["pineapple"] += 1
if num == 0:
pineapple_votes["no pineapple"] += 1
print(pineapple_votes)
fav_ff_restaurant = dict()
for i in range(10):
restaurant = input("What is your favorite fast food restaurant? ").lower()
if restaurant in fav_ff_restaurant:
fav_ff_restaurant[restaurant] += 1
else:
fav_ff_restaurant[restaurant] = 1
print(fav_ff_restaurant)
# A list of tuples
points_on_a_graph = [(3, 9), (1,1), (5, 25), (2, 4), (4, 16)]
# We can print this list, but... is this the most useful?
print(points_on_a_graph)
%matplotlib inline
import matplotlib
x = [3, 1, 5, 2, 4]
y = [9, 1, 25, 4, 16]
matplotlib.pyplot.scatter(x, y)
matplotlib.pyplot.show()
%matplotlib inline
import matplotlib.pyplot as plt
x = [3, 1, 5, 2, 4]
y = [9, 1, 25, 4, 16]
plt.scatter(x, y)
plt.show()
%matplotlib inline
import matplotlib.pyplot as plt
x = [3, 1, 5, 2, 4]
y = [9, 1, 25, 4, 16]
plt.plot(x, y)
plt.xlabel("Some numbers")
plt.ylabel("Some other numbers")
plt.show()
%matplotlib inline
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.xlabel("Some numbers")
plt.ylabel("Some other numbers")
plt.show()
%matplotlib inline
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.xlabel("Some numbers")
plt.ylabel("Some other numbers")
# What might these following lines do?
plt.gray()
plt.axis([0, 10, 0, 100])
plt.show()