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
** Split this string:**
s = "Hi there Sam!"
**into a list. **
s = "Hi there Sam!"
s.split()
** Given the variables:**
planet = "Earth"
diameter = 12742
** Use .format() to print the following string: **
The diameter of Earth is 12742 kilometers.
planet = "Earth"
diameter = 12742
print(f"The diameter of {planet} is {diameter} kilometers.")
** 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]
** Given this nested 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]
** 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@domain.comβ would return: domain.com
def domainGet(email):
return email.split('@')[1]
domainGet('user@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. **
On first pass forgot to include lower
def findDog(st):
return 'dog' in st.lower().split()
findDog('Is there a dog here?')
** Create a function that counts the number of times the word βdogβ occurs in a string. Again ignore edge cases. **
def countDog(string):
i = 0
y = string.split()
dogs = list(filter(lambda a:a=='dog', y))
return len(dogs)
ANSWER FROM INSTRUCTOR
def countDog(st): count = 0 for word in st.lower().split(): if word == βdogβ: count += 1 return count
countDog('This dog runs faster than the other dog dude!')
** 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 x: x[0]=='s', seq))
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. **
only difference in my answer and the instructor is no need for βis Trueβ for speed is not needed - also, used return βBig Ticketβ, etc as opposed to print
def caught_speeding(speed, is_birthday):
if is_birthday is True:
cspeed = speed-5
else:
cspeed = speed
if cspeed < 61:
print("No Ticket")
elif 60 < cspeed < 81:
print("Small Ticket")
elif cspeed > 80:
print("Big Ticket")
else:
print("Oops")
caught_speeding(81,True)
caught_speeding(81,False)