03-Python Crash Course Exercises - Solutions PDF
03-Python Crash Course Exercises - Solutions PDF
Exercises
Answer the questions or complete the tasks outlined in bold below, use the specific method
described if applicable.
** What is 7 to the power of 4?**
7**4
2401
into a list.
s = 'Hi there Sam!'
s.split()
planet = "Earth"
diameter = 12742
** Given this nested list, use indexing to grab the word "hello" **
lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]
lst[3][1][2][0]
'hello'
** Given this nest dictionary grab the word "hello". Be prepared, this will be
annoying/tricky **
d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':
[1,2,3,'hello']}]}]}
d['k1'][3]['tricky'][3]['target'][3]
'hello'
** Create a function that grabs the email website domain from a string in the form: **
[email protected]
domainGet('[email protected]')
'domain.com'
** Create a basic function that returns True if the word 'dog' is contained in the input
string. Don't worry about edge cases like a punctuation being attached to the word dog, but
do account for capitalization. **
def findDog(st):
return 'dog' in st.lower().split()
True
** Create a function that counts the number of times the word "dog" occurs in a string.
Again ignore edge cases. **
def countDog(st):
count = 0
for word in st.lower().split():
if word == 'dog':
count += 1
return count
** Use lambda expressions and the filter() function to filter out words from a list that don't
start with the letter 's'. For example:**
seq = ['soup','dog','salad','cat','great']
seq = ['soup','dog','salad','cat','great']
['soup', 'salad']
Final Problem
You are driving a little too fast, and a police officer stops you. Write a function to
return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket". If your
speed is 60 or less, the result is "No Ticket". If speed is between 61 and 80 inclusive,
the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it
is your birthday (encoded as a boolean value in the parameters of the function) -- on
your birthday, your speed can be 5 higher in all cases.
def caught_speeding(speed, is_birthday):
if is_birthday:
speeding = speed - 5
else:
speeding = speed
caught_speeding(81,True)
'Small Ticket'
caught_speeding(81,False)
'Big Ticket'
Great job!