0% found this document useful (0 votes)
17 views6 pages

Lesson Four Computer

The document discusses Python list methods and operations like append, sort, insert, find length, sum and product of elements. It also contains examples of programs to reverse a string, print numbers within a range excluding some, iterate through numbers and courses list, convert temperatures between Celsius and Fahrenheit scales.

Uploaded by

mi.reina.heydy5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Lesson Four Computer

The document discusses Python list methods and operations like append, sort, insert, find length, sum and product of elements. It also contains examples of programs to reverse a string, print numbers within a range excluding some, iterate through numbers and courses list, convert temperatures between Celsius and Fahrenheit scales.

Uploaded by

mi.reina.heydy5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Class Activity

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 = ['Sandra', 'Kelli', 'James', 'Zaina', 'Ethan']

1. How many students does Mrs. Jenkins have in her list?

Mrs. Jenkins has 5 students in her list.

2. Mrs. Jenkins adds this second line of code to her program:

myClass.append(‘Bobby’)

What will her list look like now? Write your answer here:

myClass = ['Sandra', 'Kelli', 'James', 'Zaina', 'Ethan', 'Bobby']

3. Mrs. Jenkins then adds this line of code to her program

myClass.sort()

What will her list look like now? Write your answer here:

myClass = ['Bobby', 'Ethan', 'James', 'Kelli', 'Sandra', 'Zaina']

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:

['Bobby', 'Ethan', 'Billie', 'James', 'Kelli', 'Sandra', 'Zaina']

Basically, this how program going to looks like (If needed. If not, just delete it):

myClass = ['Sandra', 'Kelli', 'James', 'Zaina', 'Ethan']


myClass.append('Bobby')
myClass.sort()
myClass.insert(2, 'Billie')

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

6. What will be the output of the following code?


List1=[1,3,2]
List1*2

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] )

7. Given the following list of numbers


1, 2, 3, 4, 5, 6. 7. 8. 9, 10
Write a python program that will print out each number squared so your output would look like…
1
4
….
100

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)

8. Write a program to find the sum of all elements of a list.

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)

9. Write a program to find the product of all elements of a list.

Answer:

# Define the list of numbers


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Initialize a variable to store the product
total_product = 1
# Iterate through each number in the list
for num in numbers:
# Multiply the current number with the total product
total_product *= num
# Print the product of all elements
print("The product of all elements in the list is:", total_product)

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

# Take user input for temperature and unit


temp = float(input("Enter the temperature: "))
unit = input("Enter the unit (C for Celsius, F for Fahrenheit): ").upper()

# Perform conversion based on the unit


if unit == 'C':
converted_temp = celsius_to_fahrenheit(temp)

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:

# Accept a word from the user


word = input("Enter a word: ")
# Reverse the word using slicing
reversed_word = word[::-1]
# Print the reversed word
print("Reversed word:", reversed_word)

12. Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.

Answer:

# Iterate through numbers from 0 to 6


for num in range(7):
# Check if the number is not 3 and not 6
if num != 3 and num != 6:
# Print the number
print(num)

13. Write a Python program that iterates the integers from 1 to 25.

# Iterate through integers from 1 to 25


for i in range(1, 26):
print(i)

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']

# Print the courses


print("Courses taken since grade 9:")
for course in courses:
print(course)

# Add 2 courses you wish you had taken


courses.extend(['Art', 'Music'])

# Print the updated list of courses


print("\nCourses after adding:")
for course in courses:
print(course)

# Sort the courses


courses.sort()

# Print the sorted list of courses


print("\nCourses after sorting:")
for course in courses:
print(course)

# Print the number of courses


print("\nNumber of courses:", len(courses))

# Reverse the list of courses


courses.reverse()

# Print the reversed list of courses


print("\nCourses after reversing:")
for course in courses:
print(course)

# Remove the least favourite courses


least_favourite = ['Art', 'Music']
for course in least_favourite:
courses.remove(course)

# Print the list after removing the least favourite courses


print("\nCourses after removing least favourite:")
for course in courses:
print(course)

# Iterate through the list of courses


print("\nIterating through the list:")
for idx, course in enumerate(courses, 1):
print(f"Course {idx}: {course}")

Page 5 of
Page 6 of

You might also like