list
This commit is contained in:
49
src/basic/list.py
Normal file
49
src/basic/list.py
Normal file
@@ -0,0 +1,49 @@
|
||||
a = [1, 2, 3, 4, 5]
|
||||
|
||||
# List of strings
|
||||
b = ['apple', 'banana',]
|
||||
|
||||
# Mixed data types
|
||||
c = [1, 'hello', 3.14, True]
|
||||
|
||||
print(a)
|
||||
print(b)
|
||||
print(c)
|
||||
|
||||
# Using list() Constructor
|
||||
a = list([1, 2, 3, 4]) # [1, 2, 3, 4]
|
||||
b = list("3, 4, 8") # ['3', ',', ' ', '4', ',', ' ', '8']
|
||||
c = list((1, 2, 3, 'apple', 4.5))
|
||||
print(a)
|
||||
print(c)
|
||||
|
||||
b = [0] * 7
|
||||
|
||||
print(b) # [0, 0, 0, 0, 0, 0, 0]
|
||||
|
||||
|
||||
# Initialize an empty list
|
||||
a = []
|
||||
|
||||
# Adding 10 to end of list
|
||||
a.append(10)
|
||||
print("After append(10):", a)
|
||||
|
||||
# Inserting 5 at index 0
|
||||
a.insert(0, 5)
|
||||
print("After insert(0, 5):", a)
|
||||
|
||||
# Adding multiple elements [15, 20, 25] at the end
|
||||
a.extend([15, 20, 25])
|
||||
print("After extend([15, 20, 25]):", a)
|
||||
|
||||
a = [10, 20, 30, 40, 50]
|
||||
|
||||
# Removes the first occurrence of 30
|
||||
a.remove(30)
|
||||
print("After remove(30):", a)
|
||||
|
||||
a.pop(1) # Removes the element at index 1 (20)
|
||||
|
||||
# Deletes the first element (10)
|
||||
del a[0]
|
||||
Reference in New Issue
Block a user