In [1]:
out(1]
In [4]:
In [3]:
out[3]
PIERIAN @ DATA
(bitptiwww,pieriandata,com)
Python Crash Course Exercises - Solutions
This is an optional exercise to test your understanding of Python Basics. If you find this extremely
challenging, then you probably are not ready for the rest of this course yet and don't have enough
programming experience to continue. | would suggest you take another course more geared
towards complete beginners, such as Complete Python Bootcamp
*Y20)
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 47°*
788
2401
** Split this string:**
"Hi there Sam!”
“into alist. *
s = Hi there Sam!"
s.split()
['Hi', ‘there’, ‘dad!*]
** Given the variables:**In [5]:
In [6]:
In [7]:
In (14):
out (14)
In [16]:
In [22]:
out [22]
In [23]:
In [24]:
In [26]:
out [26]:
planet = “Earth”
diameter = 12742
** Use .formatt) to print the following string: **
The diameter of Earth is 12742 kilometers.
planet = “Earth”
diameter = 12742
print("The diameter of {} is {} kilometers.” .format (planet, diameter)
The diameter of Earth is 12742 kilometers.
** Given this nested list, use indexing to grab the word "hello" **
Ast = [1,2,[3,4],[5,[100, 280, [ "hello" ]],23,11],1,7]
Ast(3][1][2][2]
hello’
“* Given this nest dictionary grab the wor
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*
** What is the main difference between a tuple and a list? “*
# Tuple is immutable
** Create a function that grabs the email website domain from a string in the form: *
user@domain. com
So for example, passing "user@domai
domain.com
com (mailto:[email protected])" would return:
def domainget (email):
return email.split('@')[-1]
domainGet (‘user@domain. com’)
"domain.com"
** Create a basic function that returns True if the word 'dog' is contained in the input string. Don'tIn [27]:
In [28]:
out(28]
In [30]:
In [32]:
out [31]
In [34]:
In [35]:
out [35]
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()
findDog('Is there a dog here?)
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 = @
for word in st.lower().split():
if word == ‘dog
count +21
return count
countDog('This dog runs faster than the other dog dude!)
2
** 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']
should be filtered down to:
['soup", ‘salad"]
seq = ['soup', ‘dog’, ‘salad’, ‘cat’, great")
List (Filter(Lambda word: word[@]
2seq))
['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”. f speed is between 61 and 80 inclusive, the result is “Small Ticket’. If speed is 81 or
more, the result is "Big Ticket”. Uniess itis your birthday (encoded as a boolean value in the
parameters of the function) -- on your birthday, your speed can be § higher in all cases. *In [4]:
In [5]:
out [5]
In [6]:
out [6]
def caught_speeding(speed, is_birthday):
if is_birthday:
speeding = speed - 5
else:
speeding = speed
if speeding > 80:
return ‘Big Ticket’
elif speeding > 60:
return ‘Small Ticket’
else:
return ‘No Ticket’
caught_speeding(81, True)
"small Ticket"
caught_speeding(81, False)
"Big Ticket’
Great job!