Document (10) python
Document (10) python
My_list = [1, 2, 3, 4, 5]
First_element = my_list[0]
Last_element = my_list[-1]
My_list[2] = 10
My_list.append(6)
My_list.remove(4)
Index_of_7 = my_list.index(7)
My_list.sort()
My_list.reverse()
Length_of_list = len(my_list)
My_list.clear()
Output:
First element: 1
Last element: 5
List after modifying the 3rd element (index 2): [1, 2, 10, 4, 5]
Popped element: 10
Index of element 7: 2
# Tuple creation
# 2. Slicing a tuple
Print(element)
If 30 in my_tuple:
Print(“Concatenated tuple:”, new_tuple) # Output: (10, 20, 30, 40, 50, 60,
70)
# 7. Repeating a tuple
Repeated_tuple = my_tuple * 2
Index_of_30 = my_tuple.index(30)
Count_of_40 = my_tuple.count(40)
A, b, c, d, e = my_tuple
Length: The len() function returns the number of elements in the tuple.
Repetition: You can repeat a tuple multiple times using the * operator.
Finding index: The index() method returns the index of the first occurrence
of an element.
Here’s a sample Python program that demonstrates all the key operations
of a set:
```python
# Set creation
My_set.add(60)
Print(“After adding 60:”, my_set) # Output: {10, 20, 30, 40, 50, 60}
Print(“After removing 30:”, my_set) # Output: {10, 20, 40, 50, 60}
# 3. Checking membership
# 4. Set union
Union_set = my_set.union(another_set)
# 5. Set intersection
Intersection_set = my_set.intersection(another_set)
# 6. Set difference
Difference_set = my_set.difference(another_set)
Sym_diff_set = my_set.symmetric_difference(another_set)
# 9. Set length
My_set.clear()
```
4. **Set union**: The `union()` method returns a new set that contains all
elements from both sets (without duplicates).
10. **Clearing the set**: The `clear()` method removes all elements from
the set, leaving it empty.
Note: Since sets are unordered collections, the elements’ order during
iteration or when printed may vary.
Here’s a sample Python program that demonstrates all the key operations
of a dictionary:
```python
# Dictionary creation
My_dict = {
“name”: “Alice”,
“age”: 25,
Removed_value = my_dict.pop(“city”)
If “name” in my_dict:
Print(key)
Print(value)
Print(f”{key} = {value}”)
# 10. Merging two dictionaries
My_dict.update(another_dict)
Keys = my_dict.keys()
Values = my_dict.values()
Items = my_dict.items()
My_dict.clear()
```
1. **Accessing values by key**: You can access the value of a specific key
using the `my_dict[key]` syntax.
5. **Getting values safely**: The `get()` method retrieves the value for a
given key, and you can specify a default value if the key doesn’t exist.
6. **Iterating**: You can iterate over keys, values, or both key-value pairs
using `.keys()`, `.values()`, and `.items()`, respectively.
8. **Getting all keys, values, or items**: You can retrieve all the keys,
values, or key-value pairs using `.keys()`, `.values()`, and `.items()`.
This program illustrates the key functionalities and operations that you
can perform on dictionaries in Python.
Here’s a sample Python program that demonstrates all the key operations
for strings:
```python
# String creation
# 2. Slicing a string
# 4. String concatenation
Greeting = “Hello”
Name = “Alice”
Print(char)
# 6. Checking membership
# 8. Replacing substrings
# 9. Splitting a string
Words = my_string.split(“, “)
Joined_string = “ “.join(words_to_join)
Stripped_string = extra_spaces.strip()
Position = my_string.find(“World”)
Count = my_string.count(“l”)
```
2. **Slicing**: You can extract a portion of the string using slicing, which
works similarly to lists and tuples.
9. **Splitting**: The `split()` method splits the string into a list based on a
delimiter.
10. **Joining**: The `join()` method combines a list of strings into a single
string with a specified separator.
12. **Finding a substring**: The `find()` method returns the index of the
first occurrence of a substring.