Repository navigation
Expand file tree
/
Copy path060-Change-List-Element.py
More file actions
80 lines (64 loc) · 2.81 KB
/
Copy path060-Change-List-Element.py
File metadata and controls
80 lines (64 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
===============================================================================
File Name : 60-Change-List-Element.py
Description : Change a List Element in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
List elements can be changed by assigning a new value to an existing index.
Since lists are mutable, their elements can be modified after creation.
Syntax:
list_name[index] = new_value
Example:
numbers[2] = 30
"""
# -----------------------------------------------------------------------------
# Creating a list of integers.
# -----------------------------------------------------------------------------
numbers = [10, 20, 30, 40, 50]
# -----------------------------------------------------------------------------
# Displaying the original list.
# -----------------------------------------------------------------------------
print("Original List :", numbers)
# Output:
# Original List : [10, 20, 30, 40, 50]
print()
# -----------------------------------------------------------------------------
# Changing the first element.
# -----------------------------------------------------------------------------
numbers[0] = 100
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("After Changing First Element :", numbers)
# Output:
# After Changing First Element : [100, 20, 30, 40, 50]
print()
# -----------------------------------------------------------------------------
# Changing the third element.
# -----------------------------------------------------------------------------
numbers[2] = 300
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("After Changing Third Element :", numbers)
# Output:
# After Changing Third Element : [100, 20, 300, 40, 50]
print()
# -----------------------------------------------------------------------------
# Changing the last element.
# -----------------------------------------------------------------------------
numbers[4] = 500
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("After Changing Last Element :", numbers)
# Output:
# After Changing Last Element : [100, 20, 300, 40, 500]
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the list.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 5