0% found this document useful (0 votes)
6 views

Python Material

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python Material

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1st file

my_list = [1, 2, 3]
print(my_list)
# Modify the list by changing an element
my_list[0] = 10

# Add an element to the list


my_list.append(4)

# Remove an element from the list


my_list.remove(2)

print(my_list) # Output: [10, 3, 4]

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


# Modify the value associated with a key
my_dict["age"] = 26

# Add a new key-value pair


my_dict["city"] = "New York"

# Remove a key-value pair


del my_dict["name"]

print(my_dict) # Output: {'age': 26, 'city': 'New York'}

2nd file

my_string = "Hello"
# Trying to modify a string directly will create a new string
new_string = my_string.replace("H", "h")

print(my_string) # Output: "Hello" (unchanged)


print(new_string) # Output: "hello" (new string)

#Tuple Example
my_tuple = (1, 2, 3) # Attempting to modify a tuple will raise an error
#my_tuple[0] = 10 # TypeError: 'tuple' object does not support item assignment

# But you can create a new tuple by combining elements


new_tuple = my_tuple + (4, 5)

print(my_tuple) # Output: (1, 2, 3)


print(new_tuple) # Output: (1, 2, 3, 4, 5)

num = 10
# You can't change the value of the original integer directly.
num += 5 # This creates a new integer with the value 15
print(num) # Output: 15 (a new integer object is created)

You might also like