Diantara fungsi variable adalah untuk "storing" dan "manage" data dalam program bahasa Python.
Numeric Variables in Python
x= 1 ----> intger
y= 1.34 ----> float
z= True ----Bolean
>>> x= 1
>>> y= 1.34
>>> z= True
>>> print(x)
1
>>> print(y)
1.34
>>> print(z)
True
>>>
Beberapa operasi arithmatic dasar: tambah(+), kali(*), bagi (/) dan kurang (-):
x= 4
y= 8
tambah= x+y
kali= x*y
bagi= x/y
kurang= x-y
print(tambah)
print(kali)
print(bagi)
print(kurang)
>>> x=4
>>> y= 8
>>> tambah= x+y
>>> kali= x*y
>>> bagi= x/y
>>> kurang= x-y
>>> print(tambah)
12
>>> print(kali)
32
>>> print(bagi)
0.5
>>> print(kurang)
-4
>>>
List memakai tanda kurung segi empat (square bracket): [ ].
I= ["Ana", "Bud", "Cad", "Dom"]
print(I)
output: ['Ana', 'Bud', 'Cad', 'Dom']
Note: print all elements
print(l[0])
output: Ana
Note: print the first element
print(l[1])
output: Bud
Note: print the second element
Manipulating Lists: Adding and Removing Elements
I= ["Ana", "Bud", "Cad", "Dom"]
I.append("victoria")
I.append("victoria")
print(I)
output: ['Ana', 'Bud', 'Cad', 'Dom', 'victoria']
Note: Now, 'victoria' to be fifth elements.
I= ["Ana", "Bud", "Cad", "Dom"]
I.remove("Ana")
print(I)
Output: ['Bud', 'Cad', 'Dom']
Note: No "Ana" anymore.
Sorting Lists: Ascending and Descending Order
Kita bisa menyusun (mengorganisasi) items yang ada di dalam LIST secara Ascending atau Descending.
Fungsi yang dipakai adalah: sort() by default.
I= ["Ana", "Bud", "Cad", "Dom"]
print(I)
output: ['Ana', 'Bud', 'Cad', 'Dom']
I.sort()
print(I)
output: ['Ana', 'Bud', 'Cad', 'Dom']
Untuk descending order, kita memakai fungsi: reverse()
I= ["Ana", "Bud", "Cad", "Dom"]
I.reverse()
print(I)
output: ['Dom', 'Cad', 'Bud', 'Ana']
# Posting sebelumnya:
No comments:
Post a Comment