python assignment
python assignment
python assignment
Here are the solutions to the assignment problems without using custom
functions:
**Code:**
```python
# Given List
list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "1"], "m", "n"]
**Explanation:**
1. `list1[2][1][2]` accesses the list `["f", "g"]`.
2. `extend()` method adds the elements of `sub_list` to this list.
**Output:**
```
Extended List: ['a', 'b', ['c', ['d', 'e', ['f', 'g', 'h', 'i', 'j'], 'k'], '1'],
'm', 'n']
```
**Code:**
```python
# Sample String
string = 'restart'
print("Result:", result)
```
**Explanation:**
1. `first_char` stores the first character of the string.
2. `replace()` method replaces all occurrences of `first_char` with '$' in the
substring from the second character onward.
**Output:**
```
Result: resta$t
```
**Code:**
```python
# Sample Strings
string1 = 'The lyrics is not that poor!'
string2 = 'The lyrics is poor!'
**Explanation:**
1. `find()` method is used to locate the positions of 'not' and 'poor'.
2. If 'not' follows 'poor', replace the substring with 'good'.
**Output:**
```
Result 1: The lyrics is good!
Result 2: The lyrics is poor!
```
**Code:**
```python
# Input Lists
list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
list3 = [1, 10, 10, 0, 0]
**Explanation:**
1. Concatenate `list1` with itself to handle circular shifts.
2. Check if `list2` and `list3` appear in this concatenated list.
**Output:**
```
List1 and List2 are circularly identical: True
List1 and List3 are circularly identical: False
```
**Code:**
```python
# Original List
original_list = [3, 4, 0, 0, 0, 6, 2, 0, 6, 7, 6, 0, 0, 0, 9, 10, 7, 4, 4, 5, 3, 0,
0, 2, 9, 7, 1]
**Explanation:**
1. Separate non-zero and zero elements.
2. Concatenate these lists.
**Output:**
```
Moved List: [3, 4, 6, 2, 6, 7, 6, 9, 10, 7, 4, 4, 5, 3, 2, 9, 7, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0]
```
**Code:**
```python
# Sample Strings
string1 = 'abc'
string2 = 'xyz'
print("Result:", result)
```
**Explanation:**
1. Swap the first two characters of `string1` and `string2`.
2. Concatenate them with a space.
**Output:**
```
Result: xyc abz
```
**Code:**
```python
# Create dictionary with keys as numbers 1 to 15 and values as their squares
squares_dict = {}
for x in range(1, 16):
squares_dict[x] = x**2
**Explanation:**
1. Use a loop to create key-value pairs where each key is a number and its value is
the square of the number.
**Output:**
```
Dictionary of Squares: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81,
10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
```
**Code:**
```python
# Sample Data
data = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":
"S005"}, {"V": "S009"}, {"VIII": "S007"}]
**Explanation:**
1. Use a loop to collect all values from the list of dictionaries into a set to
ensure uniqueness.
**Output:**
```
Unique Values: {'S005', 'S002', 'S007', 'S001', 'S009'}
```
**Explanation:**
**Types of Operators:**
# a = a + 5
print(a) # 15
```
**Code:**
```python
# Original list
pairs = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
**Explanation:**
1. Use a loop to iterate through key-value pairs.
2. Add values to lists in the dictionary, grouping by the key.
**Output:**
```
Grouped Dictionary: {'yellow': [1, 3], 'blue': [2, 4], 'red': [1]}
```
**Code:**
```python
# Sample data
data = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
**Explanation:**
1. Use a list comprehension to filter out empty tuples.
**Output:**
```
Filtered List: [('',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
```