UNIVERSITAS SAM RATULANGI MANADO FAKULTAS TEKNIK, JURUSAN TEKNIK ELEKTRO Program Studi S-1 Teknik Informatika
List Mata Kuliah: Algoritma & Logika Informatika (IFC3504)
Alwin M. Sambul, S.T., M.Eng., Ph.D.
indexing. This is important to keep in mind to avoid We next look at some common operations p Apashall itusee.LIST? 2
Konsepnya mirip dengan LIST (daftar) dalam kehidupan sehari-hari. ¨ List adalah struktur data linier: ¨
¤ artinya,
elemen2 list terurut secara linier ¤ Ada elemen1, elemen2, elemen3… dst ¤ Ada index à penomoran elemen FIGURE 4-2
Operasi2 List yang umum 3
Retrieve à Mengambil nilai pada index tertentu ¨ Replace à Mengganti nilai pada index tertentu ¨ Insert à Menyisipkan nilai pada index tertentu ¨ Remove à Menghapus nilai pada index tertentu ¨ Append à Menambahkan nilai pada akhir List ¨
Operasi2 List yang umum 4
128 CHAPTER 4
Lists
Retrieve à Mengambil nilai pada index tertentu ¨ Replace à Mengganti nilai pada index tertentu ¨ Insert à Menyisipkan nilai pada index tertentu ¨ Remove à Menghapus nilai pada index tertentu ¨ Append à Menambahkan nilai pada akhir List ¨
Operasi2 List yang umum 5
Retrieve à Mengambil nilai pada index tertentu ¨ Replace à Mengganti nilai pada index tertentu ¨ Insert à Menyisipkan nilai pada index tertentu ¨ Remove à Menghapus nilai pada index tertentu ¨ Append à Menambahkan nilai pada akhir List ¨
Operasi2 List yang umum 6
Retrieve à Mengambil nilai pada index tertentu ¨ Replace à Mengganti nilai pada index tertentu ¨ Insert à Menyisipkan nilai pada index tertentu ¨ Remove à Menghapus nilai pada index tertentu ¨ Append à Menambahkan nilai pada akhir List ¨
Operasi2 List yang umum 7
Retrieve à Mengambil nilai pada index tertentu ¨ Replace à Mengganti nilai pada index tertentu ¨ Insert à Menyisipkan nilai pada index tertentu ¨ Remove à Menghapus nilai pada index tertentu ¨ Append à Menambahkan nilai pada akhir List ¨
FIGURE 4-3
Common List Operations
The operation depicted in (a) retrieves elements of a list by index val retrieved at index 4 (the fifth item in the list). The replace operation i
Operasi2 List yang umum 8
Retrieve à Mengambil nilai pada index tertentu ¨ Replace à Mengganti nilai pada index tertentu ¨ Insert à Menyisipkan nilai pada index tertentu ¨ Remove à Menghapus nilai pada index tertentu ¨ Append à Menambahkan nilai pada akhir List ¨
IGURE 4-3
Common List Operations
ge 129 16/10/12 10:55 AM user-019A
List Traversal
/Volumes/203/W
9
4.1 List Str List traversal = mengakses elemen2 list satu demi satu, elemen elemen d ending with the fimulai rst. To fidari nd a particular valuepertama in a list also hingga requires traversal. We depict the sks of summing and searching a list in Figure 4-4. terakhir. Contoh:
¨
¤ Menjumlahkan
semua elemen list ¤ Mencari niai tertentu dalam list
4.2
Lists (Sequences) in Python
Next, we look at lists (and other sequence types) in Python.
List dalam Python
10
4.2.1
Python List Type
A list in Python is a mutable, linear data structure of variable length, allowing mixed-typ elements. Mutable means that the contents of the list may be altered. Lists in Python use zero ¨ based indexing. Thus, all lists have index values 0 ... n-1, where n is the number of element Menggunakan square bracket (kurung siku)within square brackets a in the ¤ list. Lists are denoted by a comma-separated list of elements shown below, ¤ Elemen2 dipisahkan dengan koma
Notasi penulisan:
[1, 2, 3]
['one', 'two', 'three']
['apples', 50, True]
List dalam Python bersifat:
An¨ empty list is denoted by an empty pair of square brackets, []. (We shall later see the useful ness of the empty list.) Elements of a list are accessed by using an index value within squar ¤ Mutable = isi dari List bisa diubah2 brackets,
¤ Zero-based
= index-nya dimulai dari nol lst[0] ➝ 1 access of first element lst 5 [1, 2, 3] ¤ Mixed-tipe = elemenlst[1] bisa bertipe-data berbeda2 ➝ 2 access of second element lst[2] ➝ 3
access of third element
Thus, for example, the following prints the first element of list lst,
Operasi2 umum List dalam Python 11
???
???
... lst[0] ???
... lst.insert(1, 15) ... lst ???
Tuple
... lst[0] 5 5 12... lst ???
¨
... lst.append(40) ... lst ???
Tuple adalah struktur data linier yang immutable
¤ Immutable = sekali didefinisikan, isinya tidak bisa A list in Python is a mutable linear data structure, denoted by a comma-separated list of elements berubah2 within square brackets, allowing mixed-type elements. ¤ Selain
sifat immutable, Tuple sama dengan List.
4.2.2¨ Tuples Notasi: A tuple is an immutable linear data structure. Thus, in contrast to lists, once a tuple is defined, it ¤ Menggunakan tanda kurung: ( … ) cannot be altered. Otherwise, tuples and lists are essentially the same. To distinguish tuples from lists, tuples are denoted by parentheses instead of square brackets as given below, nums 5 (10, 20, 30) student 5 ('John Smith', 48, 'Computer Science', 3.42) www.it-ebooks.info
Operasi Tuple 13
Sequence 14
Struktur data dari elemen2 yang terurut linier dan bisa diakses dengan angka2 index.
sts.indd Page 133 16/10/12 10:55 AM user-019A
/Volumes/203/WB00
Operasi2 Sequence dalam Python
4.2 Lists (Sequences) in Pyth
15
FIGURE 4-6
Sequence Operations in Python
Nested Lists 16
List dan Tuple dapat berisi elemen2 bertipe data apapun, termasuk sequence lain.
10/12 10:55 AM user-019A
4
/Volumes/203/WB0
Iterasi dengan Sequence 17
Lists
Menggunakan sequence dengan statemen FOR:
FIGURE 4-9
The for Statement in Python
ariable k is referred to as a loop variable. Since there are six elements in the provided list, the op iterates exactly six times. To contrast the use of for loops and while loops for list iteration, me iteration is provided as a while loop below, k 5 0 while k , len(nums): print(nums[k]) k 5 k 1 1
Iterasi dengan Sequence 18
Menggunakan fungsi RANGE: