forked from singhsanket143/Unacademy_Pec_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlists.py
71 lines (52 loc) · 1.26 KB
/
lists.py
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
# How to initialise a list ??
a = []
b = [1,2,3]
c = [1, 'a', "string", 4.5, 2+3j, False, -89, True]
print(a)
print(b)
print(c)
# How to add elements at the last in the list ??
b.append(33)
c.append(29)
# How is indexing done in lists of python ??
# -> same like flowcharts i.e. 0 based indexing
print(c[0], c[1], c[2])
# how can we take input from user and add in list ???
# n = 10
# arr = []
# while n > 0:
# x = input()
# arr.append(x)
# n = n-1
# print(arr)
# Are lists mutable ?? i.e can u update any element ?? Yes!!!
c[0] = "Sanket"
print(c)
# how can we manually print list ??
# i = 0
# while i < len(c):
# print(c[i], end = " ")
# i = i + 1
# -> Can we remove the elements from last just like how we added at last ?? yes
c.pop()
print(c)
#-----------------------
# Can we append multiple elements at once ? yes
c.extend([23, 'true', False, 45, 45, 45, 45, -9, 45])
print(c)
# How to remove from any specific index ????
c.pop(3)
print(c)
# Can we remove any specific element ????
c.remove(45)
print(c)
# Can we add any element at a specific index ?? Yes
c.insert(1, "Singh")
print(c)
# can we remove all the elements at once ??? yes
# c.clear()
# print(c)
# can we reverse the list ?? yes
c.reverse()
print(c)
### use help(list) to get all the functions