You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10 KiB

<html> <head> </head>

___

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. I would suggest you take another course more geared towards complete beginners, such as Complete Python Bootcamp

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?**

In [1]:
7**4
Out[1]:
2401

** Split this string:**

s = "Hi there Sam!"

into a list.

In [4]:
s = 'Hi there Sam!'
In [3]:
s.split()
Out[3]:
['Hi', 'there', 'dad!']

** Given the variables:**

planet = "Earth"
diameter = 12742

** Use .format() to print the following string: **

The diameter of Earth is 12742 kilometers.
In [5]:
planet = "Earth"
diameter = 12742
In [6]:
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" **

In [7]:
lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]
In [14]:
lst[3][1][2][0]
Out[14]:
'hello'

** Given this nest dictionary grab the word "hello". Be prepared, this will be annoying/tricky **

In [16]:
d = {'k1':[1,2,3,{'tricky':['oh','man','inception',{'target':[1,2,3,'hello']}]}]}
In [22]:
d['k1'][3]['tricky'][3]['target'][3]
Out[22]:
'hello'

** What is the main difference between a tuple and a list? **

In [23]:
# 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

In [24]:
def domainGet(email):
    return email.split('@')[-1]
In [26]:
domainGet('user@domain.com')
Out[26]:
'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. **

In [27]:
def findDog(st):
    return 'dog' in st.lower().split()
In [28]:
findDog('Is there a dog here?')
Out[28]:
True

** Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. **

In [30]:
def countDog(st):
    count = 0
    for word in st.lower().split():
        if word == 'dog':
            count += 1
    return count
In [31]:
countDog('This dog runs faster than the other dog dude!')
Out[31]:
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']
In [34]:
seq = ['soup','dog','salad','cat','great']
In [35]:
list(filter(lambda word: word[0]=='s',seq))
Out[35]:
['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.

In [4]:
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'
In [5]:
caught_speeding(81,True)
Out[5]:
'Small Ticket'
In [6]:
caught_speeding(81,False)
Out[6]:
'Big Ticket'

Great job!

</html>