3.2. Python Lab - Questions#

Answer the questions or complete the tasks outlined in bold below, and use the specific method described if applicable.

3.2.1. Math#

What is 7 to the power of 4?

3.2.2. String#

Split the following string into a list:

“Hi there, Iron Man!”

  • Note: You may need to replace the characters first before split the string into a list.

### 1. Build the string and save it to a variable called "s"
### 2. Split the string into a list
['Hi', 'there', 'Iron', 'Man!']

3.2.3. String Format#

Given the variables:

planet = "Earth"

diameter = 12742

Use f-strings (formatted string literal) to print the following string:

The diameter of the Earth is 12,742 kilometers.
### assign variables
### print with f-string
The diameter of Earth is 12742 kilometers.

3.2.4. List Indexing#

Given this nested list, use list indexing to grab the word “hello”.

lst = [1, 2, [3, 4], [5, [100, 200, ['hello']], 23, 11], 1, 7]
### list indexing 
'hello'

3.2.5. Dictionary Indexing#

Given this nest dictionary below, grab the word “hello” using dictionary indexing:

d = {'k1': [1, 2, 3, {'tricky': ['oh', 'man', 'inception', {'target': [1, 2, 3, 'hello']
                                                           }
                                ]
                     }
           ]
    }
### dictionary indexing 
'hello'

3.2.6. Tuple#

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

3.2.7. Function + String#

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

### define the function here
### function call here

domainGet('user@domain.com')
'domain.com'

3.2.8. Function + String#

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.

  • string methods such as .lower() or .upper() are useful when case matters.

### define the function here 
### function call 

findDog('Is there a dog here?')
True

3.2.9. Function + String#

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

### function definition 
### function call 

countDog('This dog runs faster than the other dog dude!')
2

3.2.10. Lambda Expressions#

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']
### list definition 
### lambda and filter
['soup', 'salad']

3.2.11. Function + Conditional#

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.

### function definition
### function call

caught_speeding(81, True)
'Small Ticket'
### function call

caught_speeding(81, False)
'Big Ticket'