0.0
Whew! Yesterday we dived straight into Python
! We hope you are really like Python
so far.
Remember, raise your hand if you have any questions. We are here to learn together with you!
0.1
Write, in words in a comment (don't forget the #
):
[]
()
# Two uses of square brackets here!
# Two uses of round brackets here!
0.2
True
or False
: Mark each statement as True
or False
()
.=
is the same as the comparison ==
.a = b
means the same thing as the assignment b = a
a
,b
: a == b
is the same as b == a
==
, and
, or
, not
has type boolean
(remember, boolean
variables store True
or False
)a = b
means we are setting the value stored inside b
.a = b
means we are setting the value stored inside a
.==
alone does not change the values stored inside the variables.Write your answers here!
If you have any questions or are unsure, please raise your hand! We went over this pretty quickly yesterday, so we expect a lot of questions.
1.1
Use function print to print "Boaz"
. On a new line, print his occupation - "Lecturer"
. Finish by printing the list
of his favorite TAs - "Orr"
, "Jabari"
, "Natnael"
, and "Bryan"
.
# Your code here.
1.2
Given the code below, print x
, y
, and z
on three different lines.
x = 17
y = 19
z = 42
# Your code here.
1.3
Given the code below, print u
, v
, and w
on the same line. There are many different ways to do this!
Challenge: Do this two ways if you remember how!
u = [3, 5, 8]
v = True
w = 3.14
# Your code here.
2.1
Given the list list1
, print the first element.
Hint: what index is the first element?
list1 = ["coder", 2022, "jam"]
# Your code here.
2.2
Given the list list2
, print the last element.
Hint: what index is the last element?
list2 = ['apple', 'orange', 'banana', 'pear',
'dragonfruit', 'lychee', 'starfruit']
# Your code here.
3.1
Try executing the code below. Can you change the output by changing only 1 character (there may be multiple solutions)?
x = 42
y = 111
# Modify the output of this code by changing only 1 character:
print(x < y)
3.2
What is different in the case below? What will be printed?
x = "42"
y = "111"
print(x < y)
# Your answer here
3.3
Construct an 4 digit number and a 5 digit number such that the 4 digit number is greater than the 5 digit number when they are of type string.
# Your code here.
# Make sure to test your code here.
3.4
What would be the result of the following operations?
'A' == 'a'
'Brunch'< 'banana'
'coffee' < 'Coffee'
'Dog' > 'cat'
'Bryan' != 'bryan'
'nadia' > 'nadi'
'CORINA VIRUS' < 'CORONA VIRUS'
3.5
Check your answer in the box below. Can you figure out the rules for string comparisons?
# Explore here
# print('A' == 'a')
Note: sometimes each cell can also be hidden
Write a function that takes x
as an argument, and returns 5 * x
.
def multFive(x):
# Your code here.
# Test your code here.
multFive(2)
Write a function that takes x
as an argument and returns x + 2
.
def addTwo(x):
# Your code here.
# Test your code here.
addTwo(2)
Write a function that returns 5 * x + 10
, without using any arithmetic operations.
Hint: use multFive and addTwo. Please run the cells for exercises 4.1 and 4.2 before running this cell.
# Define your function here.
# Test your code here.
Write a function called addXtoY
that takes two arguments x
and y
, and returns their sum.
# Define addXtoY(x, y) function here.
# Test your code here.
Write a function called multiplyXandY
that takes two arguments x
and y
, and returns their product.
# Define multiplyXandY(x, y) function here.
# Test your code here.
Similar to the functions from 5.1 and 5.2, add a few more functions that simulate a calculator's functions.
# Define your functions here.
# Test your functions here.
Use your calculator functions to write a function that represents $ f(x) = 5x + 4 $.
# Define your function here.
# Test your function here.
Write a function called concatXandY
that takes two strings x
and y
, and concatenates them together.
# Define your function here.
# Test your function here.
Write a function concatXYZ
that takes three strings x
, y
, and z
, and concatenates them together without using any arithmethic (including +
) operations.
Hint: you can call the function you wrote in 6.1
# Define your function here.
# Test your function here.
Write a function cToF
that converts Celsius to Fahrenheit using the equation $F = C \times \frac{9}{5} + 32$.
# Define your function here.
# Test your function here.
Write a function fToC
that converts Fahrenheit to Celsius using the equation $C = (F - 32) \times \frac{5}{9}$.
# Define your function here.
# Test your function here.
What happens when you call cToF(fToC(x))
? Try for a few values of x
. Can you show that it is true using math?
# Define your function here.
# Test your function here.
Write a function called pythagoreanTriplet
that takes in 3 integers which will represent triangle side lengths and returns a bool, to determine if it forms a right-angled triangle. The arguments are guaranteed to be sorted in increasing order.
Hint: A triangle is a right-angled when side lengths a, b, and c satisfy $ a^{2} + b^{2} = c^{2}$.
# Define your function here.
# Test your function here.
# Add your own test cases to confirm your solution works properly.
print(pythagoreanTriplet(3, 4, 5))
A palindrome is a string that is the same when read forwards and backwards. For example, "madam", "alabala", and "12321" are palindromes but "daniel", "hello", and "1234" are not. Write a function threePalindrome
that takes in a THREE-LETTER string, and returns a boolean that is True
when the string is palindrome, and False
when it is not.
# Define your function here.
# Test your function here. Try different three letter palindromes here!
Look at the function below. Without calling it, can you predict the outcomes of calling funkyFunc(100)
, funkyFunc(123456)
, and funkyFunc(420)
?
# Run this cell first.
def funkyFunc(x):
z = x * 10
x = x % 10
z = z + x
return z
# FILL IN YOUR ANSWERS FOR ??
# for x = 100, funkyFunc(x) = ??
# for x = 123456, funkyFunc(x) = ??
# for x = 420, funkyFunc(x) = ??
# Check your answers below with code:
Can you write a function funkierFunc
which takes one argument and for which the following equation is true for any $x>0$:
funkierFunc ( funkyFunc(x) ) = x
def funkierFunc(x):
# Your code here.
# Check if your code is correct here:
funkierFunc(funkyFunc(12))
11.1
Add 'leg'
to the octopus, human, and table using append()
# 1. Octopus
octopus = ['leg', 'leg', 'leg', 'leg', 'leg', 'leg', 'leg']
# Add one more leg here!
#
print(len(octopus)) # Should be eight!
# 2. Human
human = ['arm', 'arm', 'leg']
# Add one more leg here!
#
print(len(human)) # Should be four!
# 3. Table
table = ['top', 'leg', 'leg', 'leg']
# Add one more leg here!
#
print(len(table)) # Should be five!
11.2
Add 'end'
to the sentence using append()
sentence = ['put', 'the', 'word', 'end', 'at', 'the']
# Add the word end to sentence here!
print(sentence)
11.3
Add the number 3
to the end of the second list so that each list is the same length!
list_of_lists = [[1, 2, 3], [1, 2], [1, 2, 3]]
# Add 3 to the end of the second list!
print(list_of_lists)
Use insert()
to fill in what might be missing in the correct place in the list
12.1
my_list_1 = [1, 2, 3, 5, 6] # insert 4
my_list_2 = ['a', 'b', 'd', 'e', 'f'] # insert c
my_list_3 = ['is', 'the', 'country', 'we', 'are', 'in'] # insert jamaica
my_list_4 = ['my', 'name', 'is'] # insert your name
my_list_5 = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6]] # insert [1, 2, 3, 4, 5]
Code below!
my_list_1 = [1, 2, 3, 5, 6]
# Insert here!
print(my_list_1)
my_list_2 = ['a', 'b', 'd', 'e', 'f']
# Insert here!
print(my_list_2)
my_list_3 = ['is', 'the', 'country', 'we', 'are', 'in']
# Insert here!
print(my_list_3)
my_list_4 = ['my', 'name', 'is']
# Insert here!
print(my_list_4)
my_list_5 = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6]]
# Insert here!
print(my_list_5)