0% found this document useful (0 votes)
3 views2 pages

Python List Tuple Homework

This document is a homework assignment focused on Python lists and tuples, covering topics such as list operations, methods, and the differences between lists and tuples. It includes questions on creating, indexing, slicing, and modifying lists and tuples, as well as using various list methods. The assignment encourages students to write code neatly and add comments where necessary.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python List Tuple Homework

This document is a homework assignment focused on Python lists and tuples, covering topics such as list operations, methods, and the differences between lists and tuples. It includes questions on creating, indexing, slicing, and modifying lists and tuples, as well as using various list methods. The assignment encourages students to write code neatly and add comments where necessary.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lists & Tuples — Homework

Topics: List Operations · List Methods · Tuples · Tuple Methods · List vs Tuple
Class Homework | Write your name on top before submitting

Q1 — Creating Lists and Indexing


a) Create a list called fruits with 6 items: "apple", "banana", "cherry", "mango", "grape",
"orange". Print the full list.

b) Using positive indexing, print the 1st, 3rd, and last item from the list.

c) Using negative indexing, print the last item, 2nd from last, and 4th from last.

d) Change the item at index 2 to "watermelon". Print the updated list.


■ Note: Lists use square brackets [ ]. Items are separated by commas. Index starts at 0.
■ Hint: fruits[0] gives 'apple'. fruits[-1] gives the last item. To change: fruits[2] = 'watermelon'

Q2 — List Slicing
a) Create a list: numbers = [10, 20, 30, 40, 50, 60, 70, 80]. Use slicing to print the first 4 numbers.

b) From the same list, use slicing to print the numbers from index 3 to index 6 (not including 6).

c) Create a list of 7 subject names. Use slicing to print only the first 3 subjects and only the last 3
subjects separately.

d) Given marks = [55, 72, 88, 91, 60, 77, 83], slice and print marks from index 2 to index 5.
■ Note: List slicing works just like string slicing — list[start:end]. The end index is NOT included.
■ Hint: numbers[0:4] gives [10, 20, 30, 40]. numbers[3:6] gives [40, 50, 60].

Q3 — List Methods — append, insert, remove, pop, sort, reverse, len


a) Create a list colors = ["red", "blue", "green"]. Use append() to add "yellow" at the end. Print the
list.

b) Use insert() to add "white" at index 1 in the same list. Print the updated list.

c) Use remove() to delete "blue" from the list. Then use pop() to remove the last item. Print the list
after each step.

d) Create a list nums = [5, 2, 9, 1, 7, 3]. Use sort() to sort it in ascending order. Then use reverse()
on it. Print both results.

e) Use len() to print the number of items in your final colors list and the nums list.
■ Note: append() adds to the end. insert(index, item) adds at a specific position. remove() deletes by value.
pop() removes the last item by default.
■ Hint: [Link]('yellow') adds to end. [Link](1, 'white') adds at index 1. [Link]() sorts lowest to
highest.

Q4 — Tuples — Creating, Indexing, Slicing, count( ), index( )


a) Create a tuple called days with 7 days of the week. Print the full tuple, the first day, and the last
day using indexing.

b) Create a tuple scores = (88, 72, 95, 72, 60, 88, 72). Use count() to find how many times 72
appears. Print the result.

c) Using the same scores tuple, use index() to find the position of the first 95. Print the index.

d) Use slicing on the days tuple to print only the weekdays (Monday to Friday — first 5 items).

e) Try to change the first item of the days tuple. Write what error you get in a comment. Then explain
in a comment why tuples give this error.
■ Note: Tuples use round brackets ( ). They are IMMUTABLE — you cannot change, add, or remove items after
creating them.
■ Hint: [Link](72) counts how many times 72 appears. [Link](95) gives the position of 95.

Q5 — List vs Tuple — Mixed Practice


a) You are given student_info = ("Ravi", 15, "10th Grade") as a tuple. Unpack it into 3 variables:
name, age, grade. Print each variable separately.

b) Create a list of 5 numbers. Find the sum, minimum, and maximum value using sum(), min(), and
max(). Print all three.

c) Create a list shopping = ["milk", "eggs", "bread", "butter", "sugar"]. Check if "eggs" is in the
list and if "rice" is in the list using the in keyword. Print True or False for both.

d) Convert the list ["Mon", "Tue", "Wed", "Thu", "Fri"] into a tuple using tuple(). Then convert the
tuple (1, 2, 3, 4, 5) into a list using list(). Print both results.

e) Create a list with some repeated items, for example: ["cat", "dog", "cat", "bird", "dog", "cat"].
Count how many times "cat" appears using the count() method. Then check what the total number of
items in the list is using len().
■ Note: Use a list when data needs to change. Use a tuple when data should stay fixed. Both support indexing,
slicing, len(), min(), max(), sum(), and the 'in' keyword.
■ Hint: 'eggs' in shopping gives True. tuple([1,2,3]) converts list to tuple. list((1,2,3)) converts tuple to list.

■ Key Difference: Lists [ ] can be changed (mutable). Tuples ( ) cannot be changed (immutable). This is the
most important thing to remember!

Write your code neatly. Add comments where needed. Good luck! ■

You might also like