type(10.5)float
Lab 02 Solutions
Romi Hill
September 24, 2026
1. What happens when you multiply a `list` with an `int`?
2. What happens when you add a `list` with an `int`?
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 my_lst + my_int TypeError: can only concatenate list (not "int") to list
3. What happens when you multiply a `str` with an `int`?
4. What happens when you add a str with an int?
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[12], line 1 ----> 1 my_str + my_int TypeError: can only concatenate str (not "int") to str
5. What happens when you add a str with an list?
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[13], line 1 ----> 1 my_str + my_lst TypeError: can only concatenate str (not "list") to str
For example, what happens when you run the following code?
The take away message here: When you apply an operator to two different data types, the output might not be obvious! Some operations will work (though it’s not always clear what the final data type will be), while others will raise an error. If it is successful, then the one of the types will be converted to the other. This is called implicit type casting. You can read more about this here.
But if you cannot be bothered/not interested, you should still always check what happens when you combine two data types. Or better yet, you can type cast to make explicit what you’re trying to do!
You can also explicitly convert data types.
What do the following operations do?
We can index lists.
We can slice lists.
We can specify the steps
We can reverse lists
We can reverse and specify the number of steps
Recall functions:
What is a function? What are some of the components of a function?
Write a function that outputs the following
I’ll give you the start. Hint: you need to use an operator.
Solution:
Additional Exercise:
Modify the code so that the default value is “Sophie”. That is, your function should have the following output given these inputs. This should only take 9 characters to type!
Solution:
True when x and y are anagrams of each other, and false otherwise. Example input and expected output is provided below.Note: You may have noticed : str and -> bool in this sample code. It’s good style to yourself/anyone else using the code that the input parameters should be a str, and the output should be a bool. However, this is not necessary. In fact, Python will still run if the output is not a bool, for example.
Possible Solutions:
If there are no duplicates, we can use set() to check if the two words are anagrams of each other.
If there are duplicates, we can use sorted() to check if the two words are anagrams of each other.
def is_anagram(first_word: str, second_word: str) -> bool:
"""
Check whether two strings are anagrams of each other.
An anagram is formed by rearranging the letters of one word to produce another.
"""
first_word = list(first_word)
second_word = list(second_word)
if sorted(first_word) == sorted(second_word):
return True
else:
return FalseIf you want to be concise, you can also write the above code as:
Challenge question:
Modify your code so that it also ignores spaces and capitalization.
Solution:
True when x is a palindrome. Example input and expected output is provided below.Solution: