'''
List is a collection which is ordered and changeable. Allows duplicate members.
In Python lists are written with square brackets '''
''' List Functions '''
numbers = [1, 23, 22, 3, 34, 5, 1]
numbers.sort() # sort() used to print the List in ascending order.
numbers.reverse() # It simply Reverse the List.
print(numbers)
numbers.append(23) # It is used to add values in the End of List
numbers.insert(2, 21) # It is used to add values at fixed index of List
num = numbers.pop(2) # pop() used to remove/pop an element from List
print(num)
print(numbers.sum()) # Sum all the integers in the List
print(numbers.index(23)) # It will tell the index of number 23
print(numbers.count(1)) # It will count that how many times 1 are present in List
numbers.remove(1) # It will remove specific value from List
print(numbers)
''' List Slicing '''
numb = [1, 2, 23, 74, 3, 4, 12, 32, 11, 8, 6]
print(numb[:5:2])
print(numb[5:0:-2])
print(numb[0:5:-2]) # It'll give empty list bec it makes no sense
list1 = ["akash", [17, 15], "class", 12]
print(list1[1][1])
''' More in List '''
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
for i in range(len(num)) :
print(num[0:i])
for j in range(len(num)-2):
print(num[j:j+3])
siz = 3
for k in range(len(num)-(siz-1)):
print(num[k:k+siz])
''' List Comprehension '''
lst = ['akash', 'dipu', 'tony', 'thor', 'hulk', 'alan']
l = []
for names in lst:
l.append(names)
print(l)
# Short-Cut for this ->
newlist = [names for names in lst]
print(newlist)
l = []
for person in lst:
l.append(person + " :) ")
print(l)
# OR
print([human + " :) " for human in lst])
PythonBasics