Conditionals
Lists
Loops
Dictionaries
Jupyter Notebooks, Pandas, & Matplotlib
100

Write a boolean expression to check if a list bleh contains at least 1 item

len(bleh) >= 1

100

In 1 line, create a list called elements that has the strings “fire”, “water”, “earth”, and “air”.

elements = ["fire", "water", "earth", "air"]

100

Write a while loop that continuously asks the user for input until the user enters an input of only digits

ans = input("enter an input")
while not ans.isdigit():
    ans = input("enter an input")
100

Given a dictionary stuff, write an expression to get the value at the key thing (you may assume the key exists)

stuff["thing"]

100

Import pandas and matplotlib for plotting, using conventional aliases for both

import pandas as pd
import matplotlib.pyplot as plt
200

Assume you have two strings, a and b. Write a code snippet that prints whichever of the two strings is longer. (If they are the same length, do nothing.)

if len(a) > len(b):
    print(a)
elif len(b) > len(a):
    print(b)
200

Given a list of strings bob, join the strings together with a + between each

'+'.join(bob)

200

Write a function that takes an integer parameter x. The function prints the string “:)” x times.

def smile(x):
    for i in range(x):
        print(":)")
200

You have a dictionary party_details. Add to that dictionary the key time with the value “6 PM”

party_details["time"] = "6 PM"

200

Create a DataFrame from data.csv and display the first 10 rows

df = pd.read_csv('data.csv')
print(df.head(10))
300

Write a function is_valid_pw that takes a string parameter. The function returns true if the string is at least 8 characters long and contains at least 1 character that isn't a letter or number.

def is_valid_pw(s):
    if not s.isalnum() and len(s) >= 8:
        return True
    else:
        return False
300

You have a list of integers lotto_draws where none of the numbers repeat. Print the index of the number 7, if it exists. If it doesn’t, print -1.

if 7 in lotto_draws:
    print(lotto_draws.index(7))
else:
    print(-1)
300

You have a list of strings sandwich. While there are one or more of the string “tomato” in sandwich, remove the first instance of “tomato”

while "tomato" in sandwich:
    sandwich.remove("tomato")
300

Write a function checkout_book that takes a dictionary parameter shelf and a string parameter book . If book is a key in shelf, remove that key/value pair and return the value. Otherwise, print “Book not found”.

def checkout_book(shelf, book):
    if book in shelf:
        return shelf.pop(book)
    else:
        print("Book not found")
300

You have a Jupyter Notebook with the following code cells. Rearrange the cells so that the code will run successfully when you press “Run All”:

  1. lucky_number = len(player_name) ** 2 + 7
  2. greeting = f"Hello {player_name}, your number is {lucky_number}!"
  3. player_name = input("Enter your name")

3, 1, 2

400

Write a function met_word_count that takes a string as its parameter. If the string has between 100 to 300 words (inclusive), return True. Otherwise, print whether it was longer or shorter than the word count and return False. (You can assume all words are separated by a space.)

def met_word_count(s):
    word_count = s.split(" ")
    # or
    # word_count = s.count(" ") + 1
    if word_count < 100:
        print("Too few words")
        return False
    elif word_count > 300:
        print("Too many words")
        return False
    else:
        return True
400

Suppose you have a list of strings called my_list. Print whether any of the strings in the list contain the letters Q or q.

has_q = False

for s in my_list:
    if 'q' in s or 'Q' in s:
        has_q = True

if has_q:
    print("Found a q")
else:
    print("Did not find a q")
400

You have a string called hide_me. Create a copy of hide_me where every other character is replaced by an asterisk *

hidden = ''
for i in range(len(hide_me)):
    if i % 2 != 0:
        hidden = hidden + '*'
    else:
        hidden = hidden + hide_me[i]
400

Write a function reset_scores that takes a dictionary as its parameter. Replace the value of every key in that dictionary to 0.

def reset_scores(scores):
    for score in scores:
        scores[score] = 0
400

Plot a bar graph based on the DataFrame df where the names of the bars come from the column Municipality and the height from the column Population. Include labels and a title.

plt.figure()
plt.bar(df["Municipality"], df["Population"])
plt.xlabel("Municipalities")
plt.ylabel("Population")
plt.title("Populations of Different Municipalities")
plt.show()
500

Assume you have 3 integer variables: a, b, and c. Determine whether a triangle with these three lengths would be a right triangle.

if a ** 2 + b ** 2 == c ** 2:
    print("Yes")
elif a ** 2 + c ** 2 == b ** 2:
    print("Yes")
elif b ** 2 + c ** 2 == a ** 2:
    print("Yes")
else:
    print("No")
500

Write a function get_factors that takes an integer x and returns a list of all factors of x (including 1 and itself)

def get_factors(x):
    result = []
    for n in range(1, x + 1):
        if x % n == 0:
            result.append(n)
    return result
500

You have a dictionary inventory where the keys are strings and the values are numbers. Print the sum of all the values in the dictionary.

sum = 0
for n in inventory.values():
    sum = sum + n

print(sum)
500

Given a DataFrame df, filter the DataFrame for just the entries where the column Shape has the value square. For this filtered DataFrame, calculate the mean of the column Size.

square_df = df[df["Shape"] == "square"]
square_df["Size"].mean()
M
e
n
u