Certainly!
Here are the solutions to the assignment problems without using custom
functions:
### 1. Extend a List with a Sublist
**Code:**
```python
# Given List
list1 = ["a", "b", ["c", ["d", "e", ["f", "g"], "k"], "1"], "m", "n"]
# Sub list to add
sub_list = ["h", "i", "j"]
# Adding sub_list to the appropriate location
list1[2][1][2].extend(sub_list)
print("Extended List:", list1)
```
**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']
```
### 2. Replace Occurrences of the First Character
**Code:**
```python
# Sample String
string = 'restart'
# Replace occurrences of the first character
first_char = string[0]
result = first_char + string[1:].replace(first_char, '$')
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
```
### 3. Replace Substrings 'not'...'poor' with 'good'
**Code:**
```python
# Sample Strings
string1 = 'The lyrics is not that poor!'
string2 = 'The lyrics is poor!'
# Find positions of 'not' and 'poor'
not_pos1 = string1.find('not')
poor_pos1 = string1.find('poor')
# Replace if 'not' follows 'poor'
if not_pos1 != -1 and poor_pos1 != -1 and not_pos1 > poor_pos1:
result1 = string1[:poor_pos1] + 'good' + string1[not_pos1 + 3:]
else:
result1 = string1
print("Result 1:", result1)
# Repeat for second string
not_pos2 = string2.find('not')
poor_pos2 = string2.find('poor')
if not_pos2 != -1 and poor_pos2 != -1 and not_pos2 > poor_pos2:
result2 = string2[:poor_pos2] + 'good' + string2[not_pos2 + 3:]
else:
result2 = string2
print("Result 2:", result2)
```
**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!
```
### 4. Check Circularly Identical Lists
**Code:**
```python
# Input Lists
list1 = [10, 10, 0, 0, 10]
list2 = [10, 10, 10, 0, 0]
list3 = [1, 10, 10, 0, 0]
# Check circularly identical
double_list1 = list1 + list1
is_identical_list1_list2 = any(double_list1[i:i + len(list2)] == list2 for i in
range(len(list1)))
is_identical_list1_list3 = any(double_list1[i:i + len(list3)] == list3 for i in
range(len(list1)))
print("List1 and List2 are circularly identical:", is_identical_list1_list2)
print("List1 and List3 are circularly identical:", is_identical_list1_list3)
```
**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
```
### 5. Move All Zero Digits to the End
**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]
# Move zeros to the end
non_zero_elements = [x for x in original_list if x != 0]
zero_elements = [x for x in original_list if x == 0]
result = non_zero_elements + zero_elements
print("Moved List:", result)
```
**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]
```
### 6. Swap First Two Characters of Each String
**Code:**
```python
# Sample Strings
string1 = 'abc'
string2 = 'xyz'
# Swap first two characters and concatenate
result = string2[:2] + string1[2:] + ' ' + string1[:2] + string2[2:]
print("Result:", result)
```
**Explanation:**
1. Swap the first two characters of `string1` and `string2`.
2. Concatenate them with a space.
**Output:**
```
Result: xyc abz
```
### 7. Create a Dictionary of Squares
**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
print("Dictionary of Squares:", squares_dict)
```
**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}
```
### 8. Print Distinct Values in a Dictionary
**Code:**
```python
# Sample Data
data = [{"V": "S001"}, {"V": "S002"}, {"VI": "S001"}, {"VI": "S005"}, {"VII":
"S005"}, {"V": "S009"}, {"VIII": "S007"}]
# Collect all unique values
unique_values = set()
for d in data:
unique_values.update(d.values())
print("Unique Values:", unique_values)
```
**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'}
```
### 9. Explain Operators and Expressions in Python
**Explanation:**
**Operators** in Python are special symbols or keywords that perform operations on
operands.
**Types of Operators:**
- **Arithmetic Operators:** Used for basic math operations.
- `+` (Addition), `-` (Subtraction), `*` (Multiplication), `/` (Division), `//`
(Floor Division), `%` (Modulus), `**` (Exponentiation)
- Example:
```python
a = 5
b = 3
print(a + b) # 8
print(a ** b) # 125
```
- **Comparison Operators:** Used to compare values.
- `==` (Equal), `!=` (Not equal), `>` (Greater than), `<` (Less than), `>=`
(Greater than or equal), `<=` (Less than or equal)
- Example:
```python
a = 10
b = 20
print(a < b) # True
print(a == b) # False
```
- **Logical Operators:** Used to combine conditional statements.
- `and`, `or`, `not`
- Example:
```python
a = True
b = False
print(a and b) # False
print(not a) # False
```
- **Assignment Operators:** Used to assign values to variables.
- `=`, `+=`, `-=`, `*=`, `/=`
- Example:
```python
a = 10
a += 5
# a = a + 5
print(a) # 15
```
- **Bitwise Operators:** Operate on binary representations of integers.
- `&` (AND), `|` (OR), `^` (XOR), `~` (NOT), `<<` (Left Shift), `>>` (Right
Shift)
- Example:
```python
a = 5 # 0101 in binary
b = 3 # 0011 in binary
print(a & b) # 1 (0001 in binary)
```
### 10. Create a Dictionary Grouping Key-Value Pairs into Lists
**Code:**
```python
# Original list
pairs = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
# Create dictionary grouping values into lists
grouped_dict = {}
for key, value in pairs:
if key in grouped_dict:
grouped_dict[key].append(value)
else:
grouped_dict[key] = [value]
print("Grouped Dictionary:", grouped_dict)
```
**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]}
```
### 11. Remove Empty Tuples from a List of Tuples
**Code:**
```python
# Sample data
data = [(), (), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
# Remove empty tuples
result = [t for t in data if t]
print("Filtered List:", result)
```
**Explanation:**
1. Use a list comprehension to filter out empty tuples.
**Output:**
```
Filtered List: [('',), ('a', 'b'), ('a', 'b', 'c'), ('d',)]
```