Lesson Four Computer
Lesson Four Computer
Learning Lists
Mrs. Jenkins is using a Python program to help her keep a record of the students in her class. She has created a
list called ‘myClass’ as shown here:
myClass.append(‘Bobby’)
What will her list look like now? Write your answer here:
myClass.sort()
What will her list look like now? Write your answer here:
4. Finally, Mrs. Jenkins then adds these lines of code to her program.
myClass.insert(2, ‘Billie’)
print(myClass)
What output will she see on screen? Write your answer here:
Basically, this how program going to looks like (If needed. If not, just delete it):
print(myClass)
Page 1 of
5. What is the proper way to insert '5' into the 5th position of the MyList that contains '0','1','2','3','4','6','7','8','9','10'
a. MyList.insert(5, '5')
(This inserts the value '5' at index position 5 in the list MyList. The syntax for the insert() method is list.
Insert(index, element) where index is the position where the element will be inserted and element is the
value to be inserted.)
b. MyList.insert(5) = '5'
c. MyList.insert[5] = '5'
d. MyList[5] = '5
Answer: [1, 3, 2, 1, 3, 2]
( When you multiply a list by a number, it duplicates the elements in the list the specified number of times. In this
case, List1 * 2 duplicates the elements in List1 once, so you get [1, 3, 2] followed by [1, 3, 2], resulting in [1, 3, 2,
1, 3, 2] )
Yellow is tips for you. You can delete it and it’s still will work.
Answer:
# Define the list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Iterate through each number in the list
for num in numbers:
# Print the square of the number
print(num ** 2)
Answer:
Page 2 of
# Define the list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Initialize a variable to store the sum
total_sum = 0
# Iterate through each number in the list
for num in numbers:
# Add the current number to the total sum
total_sum += num
# Print the sum of all elements
print("The sum of all elements in the list is:", total_sum)
Answer:
10. Write a Python program to convert temperatures to and from Celsius and Fahrenheit.
[ Formula : c/5 = f-32/9 [ where c = temperature in celsius and f = temperature in fahrenheit ]
Answer:
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
Page 3 of
print(f"{temp} Celsius is equal to {converted_temp} Fahrenheit")
elif unit == 'F':
converted_temp = fahrenheit_to_celsius(temp)
print(f"{temp} Fahrenheit is equal to {converted_temp} Celsius")
else:
print("Invalid unit. Please enter 'C' for Celsius or 'F' for Fahrenheit.")
11. Write a Python program that accepts a word from the user and reverses it.
Answer:
12. Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.
Answer:
13. Write a Python program that iterates the integers from 1 to 25.
14. Write a python program that list all the courses you have taken since grade 9
a. print
b. Add 2 courses you wish you had taken.
c. print
d. Sort the courses
e. print
f. print the number of courses in your list
g. reverse the list of courses
h. print
i. remove the least favourite courses.
j. iterate the list
k. print
Answer:
Page 4 of
# Courses taken since grade 9
courses = ['Mathematics', 'English', 'Science', 'History', 'Geography', 'Computer Science']
Page 5 of
Page 6 of