0% found this document useful (0 votes)
62 views5 pages

Python Unit4 Answers

The document provides answers to various Python programming questions, covering topics such as list cloning, tuple assignment, file operations, and dictionary properties. It includes code examples to illustrate concepts like mutable vs. immutable objects, accessing list values, and using list comprehension. Additionally, it explains how to handle command-line arguments and count words in a sentence.

Uploaded by

sabitha
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)
62 views5 pages

Python Unit4 Answers

The document provides answers to various Python programming questions, covering topics such as list cloning, tuple assignment, file operations, and dictionary properties. It includes code examples to illustrate concepts like mutable vs. immutable objects, accessing list values, and using list comprehension. Additionally, it explains how to handle command-line arguments and count words in a sentence.

Uploaded by

sabitha
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
You are on page 1/ 5

### Answers to Python Questions (Unit 4)

1. **Define cloning in a list.**

- Cloning a list involves creating a copy of the list to ensure the original remains unchanged during

modifications. Example:

```python

original = [1, 2, 3]

clone = original[:]

clone.append(4)

print(original) # Output: [1, 2, 3]

print(clone) # Output: [1, 2, 3, 4]

```

2. **What is the benefit of using tuple assignment in Python?**

- Tuple assignment allows unpacking multiple values in a single step, enhancing code readability

and reducing redundancy. Example:

```python

a, b = 5, 10

print(a, b) # Output: 5 10

```

3. **How do you delete a file in Python?**

- Use the `os.remove()` function from the `os` module. Example:

```python

import os

os.remove("example.txt")
```

This deletes the file `example.txt` if it exists.

4. **How do you use command-line arguments to give input to the program?**

- Use the `sys.argv` list to access command-line arguments. Example:

```python

import sys

print("Arguments:", sys.argv)

```

Run as: `python script.py arg1 arg2`.

5. **Relate String and List.**

- Strings and lists share similar operations like slicing and indexing, but strings are immutable,

while lists are mutable. Example:

```python

s = "hello"

lst = ['h', 'e', 'l', 'l', 'o']

print(s[1:4]) # Output: "ell"

print(lst[1:4]) # Output: ['e', 'l', 'l']

```

6. **Let List = ['a', 'b', 'c', 'd', 'e', 'f']. Find the following:**

- a. `List[1:3]`: Extracts elements from index 1 to 2 (inclusive of start, exclusive of end). Output:

`['b', 'c']`

- b. `List[:4]`: Extracts elements from the beginning up to index 3. Output: `['a', 'b', 'c', 'd']`

- c. `List[3:]`: Extracts elements from index 3 to the end. Output: `['d', 'e', 'f']`
7. **Give examples for mutable and immutable objects.**

- Mutable objects can be changed after creation (e.g., lists, dictionaries). Immutable objects cannot

be changed (e.g., strings, tuples). Example:

```python

# Mutable

lst = [1, 2, 3]

lst[0] = 5

print(lst) # Output: [5, 2, 3]

# Immutable

s = "hello"

s[0] = 'H' # Raises TypeError

```

8. **What is the purpose of a dictionary in Python?**

- A dictionary stores key-value pairs, enabling quick lookups based on keys. Example:

```python

d = {"name": "Alice", "age": 25}

print(d["name"]) # Output: "Alice"

```

9. **List any four file operations in Python.**

- Opening a file (`open()`)

- Reading a file (`read()`)

- Writing to a file (`write()`)

- Closing a file (`close()`)


10. **Write a Python program to count words in a sentence using the `split()` function.**

```python

sentence = "This is a sample sentence."

words = sentence.split()

print("Word count:", len(words)) # Output: Word count: 5

```

11. **In Python, how are the values stored in a list accessed? Should the elements of a list be of the

same data type?**

- Values in a list are accessed via indexing. Lists can hold elements of different data types.

Example:

```python

lst = [1, "hello", 3.14]

print(lst[1]) # Output: "hello"

```

12. **How does Python dictionary store data? Give an example.**

- Python dictionaries store data as key-value pairs in a hash table for efficient lookups. Example:

```python

d = {"key": "value"}

print(d["key"]) # Output: "value"

```

13. **Write syntax for opening a file to write in binary mode.**

```python

file = open("example.bin", "wb")

```
14. **Define list comprehension.**

- List comprehension provides a concise way to create lists using an expression and an optional

condition. Example:

```python

squares = [x ** 2 for x in range(5)]

print(squares) # Output: [0, 1, 4, 9, 16]

```

15. **What are the properties of dictionary keys?**

- Dictionary keys must be immutable (e.g., strings, tuples).

- Keys must be unique within a dictionary. Example:

```python

d = {"a": 1, "b": 2}

print(d.keys())

```

You might also like