Interview Questions
1) What is List?
Ans- A list is a built-in data structure that allows you to store an ordered
collection of items. Lists are very flexible and can contain items of different
data types, including integers, floats, strings, and even other lists.
2) Difference between mutable and immutable?
Ans- In Python, **mutable** and **immutable** refer to the behavior of objects with
respect to their ability to change after creation:
1. **Mutable Objects**:
- Mutable objects can be modified after they are created.
- Examples of mutable objects include **lists**, **dictionaries**, and **sets**.
- When you modify a mutable object, it doesn't create a new object; instead, it
changes the existing one.
- Mutable objects are useful when you need to alter their content or size
dynamically.
2. **Immutable Objects**:
- Immutable objects cannot be changed after creation.
- Examples of immutable objects include **strings**, **integers**, and
**tuples**.
- When you modify an immutable object, it creates a new object with the updated
value, leaving the original object unchanged.
- Immutable objects are often used for data that should remain constant.
In summary:
- **Mutable**: Can be modified after creation (e.g., lists, dictionaries).
- **Immutable**: Cannot be changed after creation (e.g., strings, integers,
tuples).
3) What is slicing and indexing explain with any 5 example?
Ans- In Python, slicing and indexing are techniques used to access parts of
sequences such as strings, lists, and tuples. Here’s an explanation of each along
with five examples:
### Indexing
Indexing refers to accessing a single element of a sequence. Each element in a
sequence has a specific position or index, starting from 0 for the first element.
#### Examples of Indexing:
1. **String Indexing:**
```python
text = "Hello"
print(text[1]) # Output: 'e'
```
Here, `text[1]` accesses the second character of the string "Hello".
2. **List Indexing:**
```python
numbers = [10, 20, 30, 40, 50]
print(numbers[3]) # Output: 40
```
Here, `numbers[3]` accesses the fourth element of the list.
3. **Negative Indexing:**
```python
fruits = ["apple", "banana", "cherry"]
print(fruits[-1]) # Output: 'cherry'
```
Negative indexing starts from the end of the sequence. `fruits[-1]` accesses the
last element.
# Slicing
Slicing refers to accessing a range of elements in a sequence. A slice is specified
by a start index, a stop index, and an optional step.
# Examples of Slicing:
4. **String Slicing:**
```python
text = "Hello, World!"
print(text[0:5]) # Output: 'Hello'
```
Here, `text[0:5]` extracts characters from index 0 to index 4 (stop index 5 is
not included).
5. **List Slicing:**
```python
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]
```
Here, `numbers[1:4]` extracts elements from index 1 to index 3.
6. **Slicing with Step:**
```python
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(letters[0:7:2]) # Output: ['a', 'c', 'e', 'g']
```
Here, `letters[0:7:2]` extracts every second element from index 0 to index 6.
### Combined Examples:
7. **Negative Indexing with Slicing:**
```python
text = "Hello, World!"
print(text[-6:-1]) # Output: 'World'
```
Here, `text[-6:-1]` extracts characters from the 6th last to the 2nd last.
8. **Reversing a Sequence:**
```python
text = "Hello, World!"
print(text[::-1]) # Output: '!dlroW ,olleH'
```
Here, `text[::-1]` reverses the entire string using slicing with a step of -1.
### Summary:
- **Indexing** accesses a single element in a sequence using its position.
- **Slicing** accesses a range of elements using a start index, stop index, and an
optional step.