UTS BIG DATA_DESTI FITRIA

Exercises

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

1. ** What is 7 to the power of 4?**

2.** Split this string:**
s = "Hi there Sam!"

**into a list. **

In [3]:
['Hi', 'there', 'Sam!']
In [4]:
['Hi', 'there', 'dad!']

3. ** Given the variables:**

planet = "Earth"
diameter = 12742

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

The diameter of Earth is 12742 kilometers.
In [5]:
In [9]:
The diameter of Earth is 12742 kilometers.

5. ** Given this nested list, use indexing to grab the word "hello" **

In [7]:
In [10]:
hello

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

In [16]:
In [11]:
hello

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

# Tuple is immutable

The main difference between a tuple and a list in Python is their mutability:


1. Mutability:

   - Lists are mutable, which means you can change, add, or remove elements after the list is created. You can use methods like `append`, `extend`, `insert`, `remove`, and `pop` to modify a list in place.

   - Tuples, on the other hand, are immutable. Once you create a tuple, you cannot change its elements. This means that you cannot add, remove, or modify elements in a tuple after it is defined.


2. Syntax:

   - Lists are defined using square brackets, e.g., `my_list = [1, 2, 3]`.

   - Tuples are defined using parentheses, e.g., `my_tuple = (1, 2, 3)`.


Because of their immutability, tuples are typically used for situations where you want to ensure that the data remains constant, while lists are used when you need a collection that can change dynamically.

In [23]:

7. ** 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 [12]:
domain.com
In [26]:
Out[26]:
'domain.com'

8.** 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 [13]:
True
In [28]:
Out[28]:
True
9.  Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. **
In [14]:
2
In [31]:
Out[31]:
2

10. ** 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]:
In [15]:
['soup', 'salad']

Final Problem

10. **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 [16]:
Small Ticket
In [42]:
Out[42]:
'Small Ticket'
In [43]:
Out[43]:
'Big Ticket'

Great job!

Komentar

Postingan populer dari blog ini

Demo Bypass Login dengan SQL Injection

TCP/IP

Getting Started with NumPy