Use code for exercises 1.1-1.3
Use function print to print "Daniel"
. On a new line, print his occupation - "Lecturer"
. Finish by printing another new line with his favorite TAs - "Jessica"
, "Jess"
and "Jessica Lee"
.
# Your code here.
Given the code below, print x
, y
, and z
on three different lines.
x = 17
y = 19
z = 42
# Your code here.
Given the code below, print u
, v
, and w
on the same line.
u = [3, 5, 8]
v = True
w = 3.14
# Your code here.
Use code for exercises 2.1-2.3
Given the list list1
, print the first element.
list1 = ["coder", 2018, "addis"]
# Your code here.
Given the list list2
, print the last element in two ways.
list2 = ['apple', 'orange', 'banana', 'pear',
'dragonfruit', 'lychee', 'starfruit']
# Your code here.
list3 = [True, False, True, True, True, False, True, True, True, False]
# Your code here.
Given list4
print the last 5 elements in two ways.
list4 = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
# Your code here.
Given list5
, create the list [4,3,"hello world", "Jelani"]
using one line of code. How do you know you've succeeded?
Hint: print out the list to check
list5 = [4, 3, 7, 'Jelani']
# Your code here.
Given str1
, create the string cat
by using slicing.
str1 = "caterpillar"
print(str1) # modify this line
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)
What is different in the case below?
x = "42"
y = "111"
print(x < y)
# Your answer here
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.
What would be the result of the following operations?
'A' == 'a'
'Brunch'< 'banana'
'coffee' < 'Coffee'
'Dog' > 'cat'
'Daniel' != 'daniel'
'jessica' > 'jess'
'CORINA' < 'CORONA'
Check your answer in the box below. Can you figure out the rules for string comparisons?
# Explore here
# print('A' == 'a')
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 + 2
, 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.
Use the functions from 5.1 and 5.2, and add at least 3 functions similar to those above 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 operations.
# 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 prove it?
# 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.
# Add your own test cases to confirm your solution works properly.
Write a function fourPalindrome
that checks if a four-letter string is a palindrome.
# Define your function here.
# Test your function here.
# Add your own test cases to confirm your solution works properly.
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))
12