UTS BIG DATA_DESTI FITRIA
- Dapatkan link
- X
- Aplikasi Lainnya
s = "Hi there Sam!"
**into a list. **
s = "Hi there Sam!"word_list = s.split()print(word_list)['Hi', 'there', 'Sam!']
s = "Hi there dad!"word_list = s.split()print(word_list)['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.
planet = "Earth"diameter = 12742planet = "Earth"diameter = 12742formatted_string = "The diameter of {} is {} kilometers.".format(planet, diameter)print(formatted_string)The diameter of Earth is 12742 kilometers.
5. ** Given this nested list, use indexing to grab the word "hello" **
lst = [1,2,[3,4],[5,[100,200,['hello']],23,11],1,7]nested_list = [1, 2, [3, 4], [5, [100, 200, ['hello']], 23, 11], 1, 7]# To grab "hello," you would use multiple indices:word = nested_list[3][1][2][0]print(word)hello
6. ** 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']}]}]}nested_dict = { 'key1': 'value1', 'key2': { 'key3': 'value3', 'key4': { 'key5': 'value5', 'key6': { 'key7': 'hello' } } }}# To grab "hello," you need to access each nested level:word = nested_dict['key2']['key4']['key6']['key7']print(word)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.
# Tuple is immutableThe 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.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
def get_domain(email): # Split the email at the "@" symbol parts = email.split('@') # The domain is the second part of the split string domain = parts[1] return domain# Example usageemail = "user@domain.com"result = get_domain(email)print(result)domain.com
domainGet('user@domain.com')'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. **
def contains_dog(input_string): # Convert both the input string and the target word to lowercase input_string = input_string.lower() target_word = 'dog' # Check if the target word is in the lowercase input string return target_word in input_string# Example usageinput_string = "I have a Dog named Spot."result = contains_dog(input_string)print(result) # This will print TrueTrue
findDog('Is there a dog here?')True
9. Create a function that counts the number of times the word "dog" occurs in a string. Again ignore edge cases. **
def count_dog(input_string): # Convert both the input string and the target word to lowercase input_string = input_string.lower() target_word = 'dog' # Split the input string into words using whitespace as the delimiter words = input_string.split() # Count the occurrences of the target word in the list of words count = words.count(target_word) return count# Example usageinput_string = "I have a dog and my neighbor has a dog too. Our dogs play together."result = count_dog(input_string)print(result) # This will print 32
countDog('This dog runs faster than the other dog dude!')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']
seq = ['soup','dog','salad','cat','great']seq = ['soup', 'dog', 'salad', 'cat', 'great']filtered_words = list(filter(lambda word: word.startswith('s'), seq))print(filtered_words)['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. **
def caught_speeding(speed, is_birthday): # Adjust the speed limit if it's your birthday if is_birthday: speed -= 5 if speed <= 60: return "No Ticket" elif 61 <= speed <= 80: return "Small Ticket" else: return "Big Ticket"# Example usagespeed = 70 # Change this to your actual speedis_birthday = False # Change this to True if it's your birthdayresult = caught_speeding(speed, is_birthday)print(result)Small Ticket
caught_speeding(81,True)'Small Ticket'
caught_speeding(81,False)'Big Ticket'
Great job!
- Dapatkan link
- X
- Aplikasi Lainnya
Komentar
Posting Komentar