-
Notifications
You must be signed in to change notification settings - Fork 0
/
List_tutorial.py
78 lines (64 loc) · 1.54 KB
/
List_tutorial.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
71
72
73
74
75
76
77
78
#LIST
#indexing list - mencari nilai list
cart=["milk","tea","coffee"]
print(cart[0])
#ADD LIST VALUE
cart=["milk","tea","coffee"]
cart.insert(1, "milktea")
print(cart)
#nilai milktea sudah masuk dilist dengan posisi 1
#SORT & REVERSE VALUE
cart=["milk","tea","coffee"]
cart.sort()
print(cart)
#mengurutkan value cart sesuai abjad
cart.reverse()
print(cart)
#membalikkan value cart sesuai abjad
#COPY LIST
cart2=cart.copy()
print(cart2)
#CHANGE LIST VALUE
cart=["milk","tea","coffee"]
cart[2]="water"
print(cart)
#INDEX FROM VARIABLE
name="ukon"
age=23
adress="solok selatan"
info=[name,age,adress]
print(info)
#SLICING LIST - [:]
animals=["cat","dog","bird","cow"]
print(animals[:3])
print(animals[1:3])
pesawat="airplane"
print(pesawat[3:7])
#mencetak "plan"
#SLICING NEGATIF LIST
animals=["cat","dog","bird","cow"]
print(animals[-1])
print(animals[-3:])
print(animals[-3:-1])
#slicing nol dimulai dari kanan
#membuat list
cube=[i**3 for i in range(5)]
print(cube)
#mencetak list pangkat 3 dari range 5
num=[i**2 for i in range(10)]
print(num)
#mencetak pangkat 2 dari range 10
#membuat list ditambah kondisi
even=[i**2 for i in range(10) if i**2 % 2==0]
print(even)
#membuat list pangkat dua bilangan genap dari range 10
num=[i for i in range(10) if i % 3==0]
print(num)
#membuat list kelipatan 3 dari range 10
#ARRANGE LIST
a=[6,3,2,5,4,1]
a.sort() #mengurutkan list
print(a)
a=range(11)
print(a[::2]) #menyusun list bil.genap
print(a[::-1]) #menyusun list secara terbalik/reverse