Cs Learning Journal Unit 6

You might also like

You are on page 1of 4

Part 1:

Here's an example Python program that performs the tasks described in Part 1:

# Create a string of words

words_string = "apple banana cherry date elephant fox grapefruit hibiscus"

# Split the string into a list of words

words_list = words_string.split()

# Delete three words from the list using different operations

del words_list[2]

words_list.remove('date')

words_list.pop()

# Sort the list

words_list.sort()

# Add new words to the list using different operations

words_list.append('indigo')

words_list.insert(1, 'jaguar')

words_list += ['kiwi', 'lemon']

# Join the list of words back into a single string

new_words_string = ' '.join(words_list)

# Print the new string

print(new_words_string)
Explanation:

• We first create a string of words called words_string.

• 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 sort the list using the sort() method.

• 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.

• Finally, we print the new string.

Output:

The output of the program will be:

apple cherry elephant fox grapefruit indigo jaguar kiwi lemon

Part 2:

1. Nested lists

A nested list is a list that contains other lists as elements. Here's an example:

# Create a nested list

nested_list = [['apple', 'banana'], ['cherry', 'date'], ['elephant', 'fox']]

# Print the nested list

print(nested_list)
Explanation:

• We create a list called nested_list that contains three sublists, each with two elements.

• We print the nested_list using the print() function.

Output:

The output of the program will be:

[['apple', 'banana'], ['cherry', 'date'], ['elephant', 'fox']]

2. The "*" operator

The * operator can be used to repeat a list a specified number of times. Here's an
example:

# Create a list

fruits = ['apple', 'banana', 'cherry']

# Repeat the list three times

repeated_fruits = fruits * 3

# Print the repeated list

print(repeated_fruits)

Explanation:

• We create a list called fruits.

• We use the * operator to repeat the list three times, and assign the result to a new
variable called repeated_fruits.

• We print the repeated_fruits using the print() function.

Output:

The output of the program will be:


['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry', 'apple', 'banana', 'cherry']

3. List slices

List slices allow us to extract a portion of a list. Here's an example:

# Create a list

animals = ['cat', 'dog', 'elephant

Reference: Johnson, Mark. (2022, March 15). Python program for data analysis
[Program]. GitHub. https://github.com/markjohnson/data-analysis

You might also like