You will be working on a series of exercises for the material covered in lecture 2b.
We covered:
We will go through a series of exercises to reinfornce the concepts covered today
Use len
function to find the lengths of a string s = "hello"
and a list lst = [2 ,3, 45, "t5", "ew", 4]
s = "hello"
lst = [2 ,3, 45, "t5", "ew", 4]
Which of the following options are the correct way to swap two elements m
and n
's values.
For example, m = 3
and n = 2
initially, and after certain operations, m = 2
and n = 3
A. $\newline m = 3 \newline n = 2 \newline m = n \newline n = m$
B. $\newline m = 3 \newline n = 2 \newline m = n \newline temp = m \newline n = temp$
C. $\newline m = 3 \newline n = 2 \newline temp = n \newline n = m \newline m = temp$
D. $\newline m = 3 \newline n = 2 \newline temp = m \newline n = temp \newline m = n$
Type your answer here:
Create one list containing the following:
The number 5731
, the string "bye"
, and the boolean True
First define two lists:
[87, 109, 38]
["string1", "string2", "string3"]
Assign them to two variables list1
, list2
Add the two lists together to create a new list list3
. Use print(list3)
to print out list3
. What happens when you switch the order of addition?
Suppose list1
is [1, 2, 3]. Use list multiplication to create a new list list2
, which is [1, 2, 3, 1, 2, 3]. Use print(list2)
to print out list2
list1 = [1, 2, 3]
Suppose that lst = [2, 5, 2, 5, 9, 7]
, what is lst[2]
?
lst = [2, 5, 2, 5, 9, 7]
Type your answer here:
Suppose that you are given a sorted list lst = [0 ,2, 3, 5, 7, 21, 837, 3284, 23842934]
, what is the median of this list? Use len(lst)
to find the length of this list.
lst = [0 ,2, 3, 5, 7, 21, 837, 3284, 23842934]
Given a string st = "addis coder"
, find the second to the last char.
st = "addis coder"
Change the second to the last char of the above st
to 'i'
, and see if it is possible.
Given a string st = "ddkang"
, remove the first two letters of st
to create a new string str1
. Create a second string str2
from str1
so that str2
is "daniel kang"
st = "ddkang"
Given a list lst = [2 ,3 ,4 ,5, 6]
of odd length, print out the middle three numbers in a new list.
lst = [2 ,3 ,4 ,5, 6]
Suppose that you have a dictionary dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }
. Find "Jelani"
's city.
dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }
Suppose that you have a dictionary dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }
. Change "Jelani"
's city to "Berkeley"
.
dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }
Print the length of dic
.
Print the keys of dic
as a list
Suppose you have a list of 3 numbers lst = [1, 3, -4]
, and you want to negate the elements of the list to get [-1, -3, 4]
. Create a new list containing the negated elements of lst
(in such a way that your code works even if the values of the entries are different)!
lst = [1, 3, -4]
Suppose you have a nested list lst = [["addis", 1, 3, ["coder"]], [237894, "ajhfoiah"]]
.
lst[0]
?lst[0][0]
?"coder"
from this list.Type your answer here:
Type your answer here:
lst = [["addis", 1, 3, ["coder"]], [237894, "ajhfoiah"]]
You can put lists inside of lists to create a matrix, like:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
to represent
$$\begin{bmatrix} 1&2&3\\ 4&5&6\\ 7&8&9 \end{bmatrix}$$How would you access the entry in the first row, first column (whose value is 1)?
Write your answer here:
How would you access the entry whose value is 8?
Write your answer here:
You are given two lists of strings called names
and cities
. The first person in names
lives in the last city in cities
, the second person lives in the second to last city, the third lives in the third to last, and so on. Write code to create a third list called inOrder
such that the even indexes (0, 2, 4, etc.) are the names of the people (in order) and that the odd indexes (1, 3, 5, etc.) are the cities. For a person in index $i$, their corresponding city should be in index $i+1$.
For example:
names = ["Daniel", "Nati", "Jelani"]
cities = ["Cambridge","Addis Ababa", "San Fransisco"]
In this case,
- Daniel lives in San Francisco
- Nati lives in Addis Ababa
- Jelani lives Cambridge
You should create the final list:
inOrder = ["Daniel","San Fransisco", "Nati", "Addis Ababa", "Jelani", "Cambridge"]
names = ["Timnit", "Binam", "Basi"]
cities = ["Harar", "Addis Ababa", "Gondar"]
Note: If your code does not stop running and has an [*]
on the left, it is in an infinite loop. To get out of this loop, in the top menu, select Kernel -> Interrupt, or press the black square button in the top menu bar.
What happens with the code inside the loop if you write a while True:
loop? Explain below!
You answer here.
Try running the code below and see what happens (hint: you might need to use the stop button at the top of the page).
i = 0
while True:
i += 1
print(i)
What happens with the code inside the loop if you write a while False:
loop?
You answer here.
Use a while loop to print the numbers between 0
to 10
(inclusive on both ends).
Use a while loop to print the even numbers between 0
to 10
(inclusive on both ends).
When computing 1+2+3+4+..., how many numbers do you need to sum up so that the sum exceeds 1000?
Hint: Use a while loop.
In each of the following code snippets, there is a mistake. Find it and fix it!
Suggestion: First run the code and look at the error message. Fix it and run it again.
# I want to sum up the first 10 square numbers,
# but the loop just runs infinitely.
i = 1
summ = 0
while i <= 10:
summ += i**2
print(summ)
# I want to add (and print) the first few numbers until their sum exceeds 100.
# but nothing is being printed
total = 0
i = 1
while total >= 100:
total += i
i += 1
print(i)
# I want to print the first number in my list that's bigger than 10.
# But the answer I get, 5, is certainly not bigger than 10.
myList = [1,3,9,5,-1,13,5,11,2]
number = 0
while myList[number] <= 10:
number += 1
print(number)
Let n = 345323
. Write a code that checks if n
is prime. Print "n is prime"
if it is, or "n is not prime"
otherwise.
n = 345323
The Collatz sequence of a number $n$ is defined as follows:
Example: for the number 12, the sequence is: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
The Collatz conjecture says that whichever number you start with, you'll always reach 1 at some point.
Define an integer variable n
and then use a while
loop to print out its Collatz sequence!
Suppose that you have lst = [1, 7, 4, 5, 10, 2, 3, 6, 9, 12]
. Use a while loop to find the first number that is at least 10.
lst = [1, 7, 4, 5, 10, 2, 3, 6, 9, 12]
Suppose that you have lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
, reverse the list using a while loop.
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The Fibonnaci sequence is defined as follows: The first two elements in the sequence f(0)
and f(1)
are equal to 1
. For every n>1
, the n
-th element in the sequence is $$f(n)=f(n-1)+f(n-2).$$
Use a while loop to write a code that prints out all the Fibonnaci numbers less than 10000.