🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Chapter 2 Topic 01 List dan Array.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

STRUKTUR DATA Chapter 2 : Non Primitive Data Structure Topic 1 : List, Array Presented by : Willy Prihartono Kind of Non Primitive Data Structure Array Linked List Queque Tree Gra...

STRUKTUR DATA Chapter 2 : Non Primitive Data Structure Topic 1 : List, Array Presented by : Willy Prihartono Kind of Non Primitive Data Structure Array Linked List Queque Tree Graph Stack List Indeks 1 2 3 4 Sebuah List adalah kumpulan benda di mana setiap benda memiliki posisi. Setiap benda dalam List dapat diakses melalui indeks-nya. Contoh paling gampang: array! Contoh Interface list : void insert(int indeks,Benda x); void append(Benda x); void remove(int indeks); void remove(Benda x); Benda get(int indeks); List in Phyton #Empity List #Create Element List my_list=[] my_list = [1, 2, 3] print(my_list) print(my_list) #Create List #Create append the elements my_list = [1,2,3,4] passed to it as a single element my_list1=['apel, anggur, mangga'] my_list.append([555, 12]) print(my_list) print(my_list) print(my_list1) List in Phyton #Create extend the elements adds the elements one-by-one into the list my_list.extend([234, 'more_example']) print(my_list) #Create Insert adds the element passed to the index value and increase the size of the list too. my_list.insert(1, 'insert_example') print(my_list) List in Phyton #Deleting / Remove Element List #remove element with #delete element at index 5 my_list = [1, 2, 3, 'example', 3.132, 10, 30] value my_list.remove('example') del my_list print(my_list) print(my_list) #remove element with value #empty the list my_list.remove('example') my_list.clear() print(my_list) print(my_list) List in Phyton Exercise Return to : #Accesing Elements #access elements one by one #access all elements #access index 3 element #access elements from 0 to 1 and exclude 2 #access elements in reverse Array Array Array adalah tipe urutan dan berperilaku sangat mirip dengan daftar, kecuali bahwa tipe objek yang disimpan di dalamnya dibatasi. Tipe ditentukan pada waktu pembuatan objek dengan menggunakan kode tipe, yang merupakan karakter tunggal. Kode jenis berikut ditentukan: Array Array in Phyton #Import Array #Adds an element at the end of the list from array import * cars = ["Ford", "Volvo", "BMW"] array1 = array('i', [10,20,30,40,50]) cars.append("Honda") for x in array1: print(cars) print(x) a = ["apple", "banana", "cherry"] b = ["Ford", "BMW", "Volvo"] a.append(b) #Creates Arrays print(a) cars = ["Toyota", "Volvo", "BMW"] for x in cars: print(x) Array in Phyton #Removes the element at the #Removes the first item with the specified position specified value fruits = ['apple', 'banana', 'cherry'] fruits = ['apple', 'banana', 'cherry'] fruits.pop(1) fruits.remove("banana") print(fruits) print(fruits) fruits = ['apple', 'banana', 'cherry'] fruits = ['apple', 'banana', 'cherry'] fruits.pop(0) fruits.remove("banana") print(fruits) print(fruits) Array in Phyton #Sort ascending #Sorts the list a = ("h", "b", "a", "c", "f", "d", "e", "g") cars = ['Ford', 'BMW', 'Volvo'] x = sorted(a) cars.sort() print(x) print(cars) #Sorted the list #Sort descending: a = ("b", "g", "a", "d", "f", "c", "h", "e") a = ("h", "b", "a", "c", "f", "d", "e", "g") x = sorted(a) x = sorted(a, reverse=True) print(x) print(x)

Use Quizgecko on...
Browser
Browser