|
1 | 1 | # Python program to demonstrate Creation of List Creating a List
|
2 | 2 |
|
3 | 3 | a = [2,8]
|
4 |
| -print("List is : ",a) |
| 4 | +print("List is : ",a) # [2,8] |
5 | 5 |
|
6 | 6 | # Creating a List with the use of a String
|
7 | 7 |
|
8 | 8 | List = ['Mr Rajeev Bhardwaj']
|
9 |
| -print("\nLList with String: ",List) |
| 9 | +print("\nLList with String: ",List) # ['Mr Rajeev Bhardwaj'] |
10 | 10 |
|
11 | 11 | # Creating a List with the use of multiple values
|
12 | 12 |
|
13 | 13 | List = ["Aman", "Anshika", "Veena"]
|
14 |
| -print("\nList containing multiple values: ",List) |
15 |
| -print(List[0]) |
16 |
| -print(List[1]) |
17 |
| -print(List[2]) |
| 14 | +print("\nList containing multiple values: ") |
| 15 | +print(List[0]) # Aman |
| 16 | +print(List[1]) # Anshika |
| 17 | +print(List[2]) # Veena |
18 | 18 |
|
19 | 19 | # Creating a Multi-Dimensional List (By Nesting a list inside a List)
|
20 | 20 |
|
21 | 21 | List = [['IOT'], ['For'] , ['Smart'],['Cities']]
|
22 |
| -print("\nMulti-Dimensional List: ",List) |
| 22 | +print("\nMulti-Dimensional List: ",List) # [['IOT'], ['For'] , ['Smart'],['Cities']] |
23 | 23 |
|
24 | 24 | # Creating a List with the use of Numbers (Having duplicate values)
|
25 | 25 | List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
|
26 |
| -print("\nList with the use of Numbers: ",List) |
| 26 | +print("\nList with the use of Numbers: ",List) # [1, 2, 4, 4, 3, 3, 3, 6, 5] |
27 | 27 |
|
28 | 28 | # Creating a List with Alpha_nnumeric value
|
29 | 29 |
|
30 | 30 | List = [89, 2, 'Aman', 4.567, 'Branch is', .12, 'IOT']
|
31 |
| -print("\nList with the use of Mixed Values: ",List) |
| 31 | +print("\nList with the use of Mixed Values: ",List) # [89, 2, 'Aman', 4.567, 'Branch is', .12, 'IOT'] |
32 | 32 |
|
33 | 33 | #---------------------------------------------------------------------------------
|
34 | 34 | # Addition of Elements in the List
|
35 | 35 | List.append(1)
|
36 |
| -print("\nList after Addition of Three elements: ",List) |
| 36 | +print("\nList after Addition of Three elements: ",List) # [89, 2, 'Aman', 4.567, 'Branch is', .12, 'IOT',1] |
37 | 37 |
|
38 | 38 | # Adding elements to the List using Iterator
|
39 | 39 | for i in range(1, 4):
|
40 |
| - List.append(i) |
| 40 | + List.append(i) # [89, 2, 'Aman', 4.567, 'Branch is', .12, 'IOT',1,1,2,3] |
41 | 41 | print("\nList after Addition of elements from 1-3: ",List)
|
42 | 42 |
|
43 | 43 | # Adding Tuples to the List
|
44 | 44 | List.append((5, 6))
|
45 |
| -print("\nList after Addition of a Tuple: ",List) |
| 45 | +print("\nList after Addition of a Tuple: ",List) # [89, 2, 'Aman', 4.567, 'Branch is', .12, 'IOT',1,1,2,3,(5, 6)] |
46 | 46 |
|
47 | 47 | # Addition of List to a List
|
48 |
| -List2 = ['Aman', 'Bhardwaj'] |
| 48 | +List2 = ['Aman', 'Bhardwaj'] |
49 | 49 | List.append(List2)
|
50 |
| -print("\nList after Addition of a List: ",List) |
| 50 | +print("\nList after Addition of a List: ",List) # [89, 2, 'Aman', 4.567, 'Branch is', .12, 'IOT',1,1,2,3,(5, 6) ['Aman', 'Bhardwaj']] |
51 | 51 |
|
52 | 52 | #---------------------------------------------------------------------------------
|
53 | 53 | # Python program to demonstrate Addition of elements in a List ( Creating a List)
|
54 | 54 | # Important
|
55 | 55 |
|
56 |
| -List = [1,2,3,4] |
57 |
| -print("Initial List: ",List) |
| 56 | +List3 = [1,2,3,4] |
| 57 | +print("Initial List: ",List3) # [1,2,3,4] |
58 | 58 |
|
59 | 59 | # Addition of Element at specific Position (using Insert Method)
|
60 | 60 |
|
61 |
| -List.insert(0, 'AmanDeep') |
62 |
| -List.insert(3, 12) |
63 |
| -print("\nList after Insert Operation: ",List) |
| 61 | +List3.insert(0, 'Aman') |
| 62 | +List3.insert(3, 12) |
| 63 | +print("\nList after Insert Operation: ",List3) # ['Aman',1,2,3,4] |
| 64 | + |
64 | 65 |
|
65 | 66 | #----------------------------------------------------------------------------
|
66 | 67 |
|
67 | 68 | # Python program to demonstrate Addition of elements in a List Creating a List
|
68 |
| -List = [1,2,3,4] |
69 |
| -print("Initial List: ",List) |
70 | 69 |
|
71 | 70 | # Addition of multiple elements to the List at the end (using Extend Method)
|
72 | 71 |
|
73 |
| -List.extend([8, 'Be happy', 'Always']) |
74 |
| -print("\nList after Extend Operation: ",List) |
| 72 | +List3.extend([8, 'Be happy', 'Always']) |
| 73 | +print("\nList after Extend Operation: ",List3) # ['Aman',1,2,3,4,8, 'Be happy', 'Always'] |
75 | 74 |
|
76 | 75 | # Creating a Multi-Dimensional List (By Nesting a list inside a List)
|
77 |
| -List = [['Happy', 'For'] , ['Aman','harish']] |
78 |
| -print(List) |
| 76 | +List3 = [['Happy', 'For'] , ['Aman','harish']] |
| 77 | +print(List3) |
79 | 78 | print("Acessing a element from a Multi-Dimensional list")
|
80 |
| -print(List[0][0],List[0][1],List[1][0],List[1][1]) |
| 79 | +print(List3[0][0],List3[0][1],List3[1][0],List3[1][1]) |
81 | 80 |
|
82 | 81 | #------------------------------------------------------------------------
|
83 | 82 | # Python program to demonstrate Removal of elements in a List
|
84 | 83 |
|
85 |
| -List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
| 84 | +List5 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
86 | 85 |
|
87 |
| -List.remove(5) |
88 |
| -List.remove(6) |
89 |
| -print("\nList after Removal : ",List) |
| 86 | +List5.remove(5) |
| 87 | +List5.remove(6) |
| 88 | +print("\nList after Removal : ",List5) #[1, 2, 3, 4, 7, 8, 9, 10, 11, 12] |
90 | 89 |
|
91 | 90 |
|
92 | 91 | # Removing elements from List using iterator method
|
93 | 92 | for i in range(1, 5):
|
94 |
| - List.remove(i) |
95 |
| -print("\n updated list : ",List) |
| 93 | + List5.remove(i) |
| 94 | +print("\n updated list : ",List5) # [7, 8, 9, 10, 11, 12] |
| 95 | + |
96 | 96 | #----------------------------------------------------------------------------
|
97 | 97 | List = [1,2,3,4,5]
|
98 | 98 |
|
|
0 commit comments