20. PYTHON (Struktur Kontrol) – Operasi List

Item pada indeks tertentu di dalam List dapat di-set ulang nilainya.

nomor = [5,5,5,5,5]
nomor[4] = 10

print(nomor)
=====>
[5, 5, 5, 5, 10]
=====>

List dapat ditambahkan dan dikalikan dengan cara yang sama seperti string.

nomor = [1,2,3]
print(nomor + [4,5,6])
print(nomor * 5)
=====>
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
=====>

Untuk memeriksa apakah item terdapat dalam list, maka digunakan in. Jika terdapat item, maka akan mengembalikan nilai TRUE.

binatang = ["sapi","kambing","bebek","ayam"]

print("ayam" in binatang)

if "kambing" in binatang:
	print("ada kambing")
=====>
True
ada kambing
=====>
nomor = [1,2,3,4,5]

print(6 not in nomor)
print(7 not in nomor)
print(1 not in nomor)
=====>
True
True
False
=====>

LANJUTKAN BACA MATERI LENGKAP –> Seri: Belajar Bahasa Python Lengkap

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.