List, Tuples & Strings

Lists

1. Describe list.
A list is a built-in data type used to store a collection of items.
2. How to create a list? Provide an example.

You can create a list by enclosing a comma-separated sequence of values within square brackets [ ].

Example: list = [1,2,3,4]

3. What can a list store?
A list can store multiple data items even if they are different types (string, int, bool, etc.) in a single variable.
4. Are lists mutable?
Lists are mutable, after creating a list, you can modify their contents by adding, removing, or modifying elements.
5. Explain the method lst.append(x).
Appends element x to the the end of the list lst
6. Explain the method lst.pop().
Removes and returns the final element of the list lst.
7. Explain the method lst.index(x).
Returns the position (index) of the first occurrence of the value x in the list lst.
8. Explain the method lst.insert(i,x).
Insert the element x at position (index) i in the list lst.
9. Explain the method lst.sort().
Sorts the element in the list in ascending order.
10. Explain the method lst.extend(iterable).
Appends the elements in the iterable to the end of the list lst.
11. Explain the method lst.remove(x).
Removes the first occurrence of x in list lst.
12. Explain the method lst.copy().
Create a copy of the list lst.

Tuples

1. Describe tuple.
A tuple is a built-in data type used to store an ordered sequence of elements.
2. How to create a tuple? Provide an example.

You can create a tuple by enclosing a comma-separated sequence of values within a parenthesis ().

Example: tuple = (1,2,3,4)

3. What can a tuple store?
A tuple can store multiple data items even if they are different types (string, int, bool, etc.) in a single variable.
4. Are tuples mutable?
No, tuples are immutable. Once a tuple is created, its elements cannot be modified, added, or removed.
5. Explain the method tpl.count(x).
Count the number of occurrences of a given element x in the tuple tpl.
6. Explain the method tpl.index(x).
Returns the position (index) of the first occurrence of the value x in the tuple tpl.

Strings

1. Describe string.
The string type is a built-in data type that represents textual data.
2. How to create a string? Provide an example.

You can create a string by enclosing using single '' or double apostrophes "".

Example: string = “1234”

3. What can a string store?
A string can store all types of characters and will be stored as a single object.
4. Are strings mutable?
No, strings are immutable. Once a string is created, its elements cannot be modified, added, or removed.
5. Explain the methods s.lower() and s.upper().
Returns the lowercase or uppercase version of the string s.
6. Explain the method s.strip().
Returns the string s with whitespaces removed from the start and end.
7. Explain the method s.replace('old', 'new').
Returns the string s where all occurrences of ‘old’ have been replaced by ‘new’
8. Explain the function str().
Returns a string representation of an object.
9. Explain the function len().
Returns the length of a string.
Last updated on 19 Nov 2023