Python Crash Course Exercises - 1
Python Crash Course Exercises - 1
2401
Out[1]:
In [1]:
2401
Out[1]:
into a list.
In [2]: s = "Hi there Sam!"
In [3]:
In [5]:
Given this nested list, use indexing to grab the word "hello"
In [1]: lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]
In [17]: lst[3][1][2][0]
'hello'
Out[17]:
In [14]:
'hello'
Out[14]:
Given this nested dictionary grab the word "hello". Be prepared, this will be
annoying/tricky
In [18]: d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']
In [31]:
'hello'
Out[31]:
In [22]:
'hello'
Out[22]:
['hotmail', 'com']
Out[25]:
Create a function that grabs the email website domain from a string in the form:
user@domain.com
return
In [27]: print(domainGet('user@hotmail.com'))
hotmail
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.
In [31]: def findDog(st):
False
Out[33]:
Create a function that counts the number of times the word "dog" occurs in a
string. Again ignore edge cases.
In [34]: def countDog(st):
return st.count("dog")
In [35]: countDog('This dog runs faster than the other dog dude!, dog dog dog')
5
Out[35]:
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']
['soup', 'salad']
Out[39]:
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.
In [58]: def caught_speeding(speed, is_birthday):
# code start from here
return
In [62]:
no ticket
In [60]:
Big Ticket
Great job!
localhost:8888/nbconvert/html/Python Crash Course Exercises -1.ipynb?download=false 3/4
01/02/2023, 11:01 Python Crash Course Exercises -1
In [ ]: