W5: Functions and Methods, Lists and Tuples, Loops
Coming Up
- Functions and Methods
- Lists and Tuples
- Loops
Functions vs Methods
D1: What is a “method”? How do methods differ from functions? How are they the same?
You can type your answer to D1 here...
E1: Evaluate the following method calls given the assignment s = "Computing is FUN!" Think about the input and output of each method. You’re not expected to know all methods for all types: if you haven’t seen some of these before, your best guess based on the name will probably be right!
s.isupper():s.upper():s.endswith("FUN!"):s.count('i'):s.strip('!'):s.replace('i', '!'):
Lists and Tuples
D2: What is the difference between a “list” and a “tuple”?
You can type your answer to D2 here...
D3: How do we add and remove items from a list?
You can type your answer to D3 here...
E2: Evaluate the following given the assignment lst = [2, (“green”, “eggs”, “ham”), False]
lst[2]:lst[1][-2]:lst[1][-2][:3]:lst.append(5); print(lst):lst.pop(2); print(lst):
Loops
D4: What is “iteration” in programming? Why do we need it?
You can type your answer to D4 here...
D5: What are the two main types of loop in python? How do we write them?
You can type your answer to D5 here...
D6: What do we mean by the “loop variable” in a for loop?
You can type your answer to D6 here...
D7: What are the differences between the two main types of loops? In which situations are they used?
You can type your answer to D7 here...
E3: What is the output of the following snippets of code containing loops?
i = 2
while i < 8:
print(f"The square of {i} is {i * i}")
i = i + 2
for ingredient in ("corn", "pear", "chilli", "fish"):
if ingredient.startswith('c'):
print(ingredient, "is delicious!")
else:
print(ingredient, "is not!")
i = 0
colours = ("pink", "red", "blue", "gold", "red")
while i < len(colours):
if colours[i] == "red":
print("Found red at index", i)
i += 1
MIN_WORD_LEN = 5
long_words = 0
text = "There once lived a princess"
for word in text.split():
if len(word) >= MIN_WORD_LEN:
print(word, "is too long!")
long_words += 1
print(long_words, "words were too long")
# you can use this code cell to check your answers
D8: Is it always possible to convert a while loop into a for loop and vice versa? How do we do it?
You can type your answer to D8 here...
E4: Rewrite the loops in question 3a and 3b converting for loops to while loops and vice versa. (We’ll include answers for c and d for good measure)
i = 2
while i < 8:
print(f"The square of {i} is {i * i}")
i = i + 2
for ingredient in ("corn", "pear", "chilli", "fish"):
if ingredient.startswith('c'):
print(ingredient, "is delicious!")
else:
print(ingredient, "is not!")
Problems
Problem 1
P1: Write a function which takes a positive integer input n and prints the thirteen times tables from 1 _ 13 until n _ 13.
def thirteen_table(n):
pass
Problem 2
P2: Write a function which converts a temperature between degrees Celsius and Fahrenheit. It should take a float, the temperature to convert, and a string, either ‘c’ or ‘f’ indicating a conversion from degrees Celsius and Fahrenheit respectively. The formulae for conversion are below.
def conver_temp(degrees, unit):
pass
Problem 3
P3: Write a function which takes a tuple of strings and returns a list containing only the strings which contain at least one exclamation mark or asterisk symbol. words*with*symbols((‘hi’, ‘there!’, ’***’)) should return [‘there!’, ’__*’].
def words_with_symbols(words):
# write your code here