Cs Learning Journal Unit 6
Cs Learning Journal Unit 6
Cs Learning Journal Unit 6
Here's an example Python program that performs the tasks described in Part 1:
words_list = words_string.split()
del words_list[2]
words_list.remove('date')
words_list.pop()
words_list.sort()
words_list.append('indigo')
words_list.insert(1, 'jaguar')
print(new_words_string)
Explanation:
• We then use the split() method to split the string into a list of words called words_list.
• We delete three words from the list using three different operations: del to remove an
item by index, remove() to remove an item by value, and pop() to remove the last item
in the list.
• We add new words to the list using three different operations: append() to add a word
to the end of the list, insert() to add a word at a specific index, and += to concatenate
another list to the end of the existing list.
• We then join the list of words back into a single string using the join() method, and
assign it to a new variable called new_words_string.
Output:
Part 2:
1. Nested lists
A nested list is a list that contains other lists as elements. Here's an example:
print(nested_list)
Explanation:
• We create a list called nested_list that contains three sublists, each with two elements.
Output:
The * operator can be used to repeat a list a specified number of times. Here's an
example:
# Create a list
repeated_fruits = fruits * 3
print(repeated_fruits)
Explanation:
• We use the * operator to repeat the list three times, and assign the result to a new
variable called repeated_fruits.
Output:
3. List slices
# Create a list
Reference: Johnson, Mark. (2022, March 15). Python program for data analysis
[Program]. GitHub. https://github.com/markjohnson/data-analysis