Coming Up
- Functions and Methods
- Lists and Tuples
- Loops
Functions vs Methods
Discussion 1
D1: What is a “method”? How do methods differ from functions? How are they the same?
Exercise 1
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', '!')
:
Discussion 2: Lists and Tuples
D2: What is the difference between a “list” and a “tuple”?
Discussion 3: Adding and Removing Items in List
D3: How do we add and remove items from a list?
Exercise 2
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)
:
Discussion 4: Iteration
D4: What is “iteration” in programming? Why do we need it?
Discussion 5: Python Loops
D5: What are the two main types of loop in python? How do we write them?
Discussion 6: Loop Variables
D6: What do we mean by the “loop variable” in a for loop?
Discussion 7
D7: What are the differences between the two main types of loops? In which situations are they used?
Exercise 3
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
Discussion 8
D8: Is it always possible to convert a while loop into a for loop and vice versa? How do we do it?
Exercise 4
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!")
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