0% found this document useful (0 votes)
18 views10 pages

04b. Array

This document discusses arrays and lists in Python. It defines an array as a special variable that can hold more than one value. Lists are defined as ordered and changeable collections that allow duplicate members. The document provides code samples to demonstrate creating and manipulating arrays and lists through various methods like append, extend, remove, reverse, etc. It also discusses common list methods such as append, insert, pop, sort, and reverse.

Uploaded by

Fiona Tjia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

04b. Array

This document discusses arrays and lists in Python. It defines an array as a special variable that can hold more than one value. Lists are defined as ordered and changeable collections that allow duplicate members. The document provides code samples to demonstrate creating and manipulating arrays and lists through various methods like append, extend, remove, reverse, etc. It also discusses common list methods such as append, insert, pop, sort, and reverse.

Uploaded by

Fiona Tjia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Algoritma dan

Pemrograman

Array
List

Adi
[email protected]
Array
• What is array ?
– Special variable, which can hold more than one
value at a time.
• Module: array
Array
Array (sample)
import array

# create variable array, bertipe int, dan lakukan inisialisasi


arr = array.array('i', [1, 2, 3, 4, 5, 6, 7, 8])

# liat isi variable array


for x in arr:
print(x, end=' ')
print()

# liat isi index pertama (0)


print(f"first item: {arr[0]}")

# list isi index terakhir


print(f"last item: {arr[len(arr)-1]}")
Array (sample)
# tambah elemen baru ke variable array
arr.append(9)

# extend elemen baru ke variable array


arr.extend([10, 11, 12])

print("after append and extend: ", end='')


for x in arr:
print(x, end=' ')
print()
Array (sample)
arr.remove(11)
arr.remove(12)

print("after remove 11 dan 12: ", end='')


for x in arr:
print(x, end=' ')
print()

arr2 = arr.reverse()

print("after reverse: ", end='')


for x in arr:
print(x, end=' ')
print()
List
• a collection which is ordered and changeable
• Allows duplicate members
• In Python lists are written with square
brackets
• Sample:
thislist = ["apple", "banana", "cherry"]
print(thislist)
List
• Cara membuat variable bertipe list
– var_list = []
– Var_list = list(iterable)
– Contoh
list = [] # list kosong
list.append(1)
list.append(“satu”)
list.append(False)
list.append(2.5)
List
• Method-method umum pada list:
• List.append(obj)
• List.extend(iterable)
• List.insert(i, obj)
• List.remove(obj)
• List.pop(i)
• List.clear()
• List.index(obj[,start[,end]])
• List.count(obj)
• List.sort(key=None, reverse=False)
• List.reverse()
• List.copy()

You might also like