Sunday, December 24, 2023

Python Basics - Data Structures - Module 2

 In this module 2, we divide in three parts: Parts 1 (List and Tuples), part 2 (Dictionaries) and part 3 (Sets)


Image 1 - Pad Thai, fried noodle.

Parts 1 (List and Tuples)
Tuples is data type that built in Python to store multiple items in a single variable.
Tuples are similar to List, tey are immutable or their values can't be changed.

Tuples are an ordered sequence:
Ratings =(10,9,6,4,2)

Tuples:
String: 'disco'
int: 10
float: 1.2
tuple1 =('disco',10,1.2)
type(tuple1)=tuple

tuple1 =('disco',10,1.2)
index:       0        1    2
hence, tuple1[0]:'disco'
tupl1[1]:10

Index can be negative too:
tuple1 =('disco',10,1.2)
index:       -3      -2  -1
hence, tuple1[-3]:'disco'
tupl1[-1]:1.2

We can add tuples:
tuple1 =('disco',10,1.2)
tuple2 = tuple1 +("hard".10)
tuple2=('disco',10,1.2,"hard",10) 
index =     0       1   2       3      4

tuple2=('disco',10,1.2,"hard",10) 
index =     0       1   2      3       4
tuple2[0:3]: ('disco',10,1.2)
tuple2[3]: ('hard', 10)

len command:
len(('disco',10,1.2,"hard",10))= 5

Ratings =(10,9,6,5,10,8,9,6,2)
Ratings1=Ratings

Tuple: Nesting
nested_tuple= ((1, 20, (3, 4), (5, 6))
This nested_tuple contains 3 tuples, each with 2 elements.

We can access the elements of a nested_tuple by using indexing:
print(nested_tuple[1][0])
output: 3 ----> first element of second tuple

List is data type, can contain any data types include integers, strings and objects. Mutable, can be changed.
L=["Michael", 10.1, 1982]
index: 0            1        2
Hence, L[0]: "Michael"
L[1]: 10.1

Negative index:
L=["Michael", 10.1, 1982]
Index:  -3           -2    -1
L[-1]: 1982
L[-3]: "Michael"

List: slicing
L=["Michael", 10.1, 1982, "MJ", 1]
L[3:5]: ["MJ', 1]


Image 2 - Jambu bol, ilustrasi

Practice Quiz: Lists and Tuples

1) Consider the following tuple:
say_what=('say',' what', 'you', 'will')

What is the result of the following say_what[-1] ?
Answer: 'will'

2) Consider the following tuple A=(1,2,3,4,5), 
what is the result of the following:A[1:4]:
answer: (2, 3, 4)
Note: A[1:4] is a slice of the list A that contains elements from index 1 to index 3. The first index is inclusive, while the second index is exclusive.

3) Consider the following tuple A=(1,2,3,4,5). 
What is the result of the following: len(A)
answer: 5

4) Consider the following list B=[1,2,[3,'a'],[4,'b']]. 
What is the result of the following: B[3][1]
answer: b
Note:
B[3] returns the fourth element of the list B, which is [4, 'b'].
B[3][1] returns the second element of the list [4, 'b'], which is 'b'.
Therefore, the result of the expression B[3][1] is 'b'.

5)What is the result of the following operation?
[1,2,3]+[1,1,1]
Answer: [1, 2, 3, 1, 1, 1]
Note: The addition operator corresponds to concatenating a list.

6) What is the length of the list A = [1] after the following operation: A.append([2,3,4,5])
answer: 2
Note: The append() method in Python adds an element to the end of a list. In this case, the list A is initially [1]. After the operation A.append([2,3,4,5]), the new list A will be [1, [2, 3, 4, 5]]. Therefore, the length of the list A after the operation is 2.

7)What is the result of the following: "Hello Mike".split()
Answer: "Hello"," Mike"


Image 3 - Karya seni, Singapura

Part 2 : Dictionaries

List has index and elements

The Dictionary has key and value. 
- keys don't have to be integers. Key usually characters.
- key is immutable and unique
- Values similar to the elements in the List, contains information.

To create Dictionary, we use curly brackets:
{"key1":1, "key2":"2", "key3":[3,3,3], ('key4'):4}


Image 4 - Kemping untuk wisata

Practice Quiz: Dictionary

1) What are the keys of the following dictionary: {"a":1,"b":2}
answer: "a", "b"

2) Consider the following Python Dictionary: Dict={"A":1,"B":"2","C":[3,3,3],"D":(4,4,4),'E':5,'F':6} What is the result of the following operation: Dict["D"]
answer: (4, 4, 4)


 
Image 5 - Semak tidak terurus, ilustrasi

Part 3: Sets

Sets are type of collection means you can input different python types. You place the elements of a set within te curly brackets. 

You notice there are duplicates items.

example:
set1 = {"pop", "rock", "soul", "hard rock", "rock", "R&B", "rock", "disco"}

Convert List to Set:
album_list = [ "Michael Jackson", "Thriller", 1982, "00:42:19", \
              "Pop, Rock, R&B", 46.0, 65, "30-Nov-82", None, 10.0]

album_set = set(album_list) 

The set() function in Python is used to create a set object, which is an unordered collection of unique elements. In the given code snippet, album_list is a list containing 10 elements. When set(album_list) is called, it creates a new set object album_set containing only the unique elements of album_list. The resulting set object will contain the following elements:

{None, 'Pop, Rock, R&B', 1982, 10.0, 46.0, '30-Nov-82', 'Michael Jackson', '00:42:19', 'Thriller', 65}


Image 6 - Buah manggis, masih hijau

Practice Quiz Sets:
1) Consider the following set: {"A","A"}. What will the result be when the set is created?
answer: {"A"}
Note: A set can't have duplicate elements. Therefore the set will only one element which is "A".

2) What is the result of the following: type(set([1,2,3]))
answer: set
note: the result of type(set([1,2,3])) is <class 'set'>

3) What method do you use to add an element to a set?
answer: add
Note: my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)
print(my_set) # Output: {1, 2, 3}

4) What is the result of the following operation: {'a','b'} & {'a'}
answer: {'a'}
Note: The operation {‘a’,‘b’} & {‘a’} is the intersection of two sets. The intersection of two sets is the set of elements that are common to both sets. In this case, the intersection of {‘a’,‘b’} and {‘a’} is the set {‘a’}. Therefore, the result of the operation is {‘a’}.

JJ

Saturday, December 16, 2023

Python Basics - Operasi Strings - Module 1 - Part 3

Strings deretan dari karakter, setiap string ada tanda petiknya: ' ' atau " ". Misalnya 'Michael Jackson' atau "Michael ackson"




Image 1 - Cherry, just illustration

String can be space or digits: '1 2 3 4'
Strings can also be special characters: '#$@%'

Name= 'M i c h a e l        J  a  c  k   s   o   n'
index:    0 1 2 3 4 5 6  7  8 9 10 11 12 13 14
Name[0]: M
Name[6]: l
Name[13]: o

We can also use negative indexes.



Image 2 - Bunga liar, hanya ilustrasi

Name= 'M   i   c   h  a  e   l'
index:    -7 -6 -5 -4 -3 -2 -1

Name[-6]: i

Strings: slicing
Name[-7:-5]: Mic

A tuple is a built-in data type in Python, used to store collections of data. It is one of the four built-in data types in Python used to store collections of data, the other three being:
- List
- Set
- and Dictionary. 



Image 3 - Jambu bol, siap untuk dikonsumsi

A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.


String: escape sequences: backslash (\)

\n represents new line
print('Michael \n is the best')
output: Michael 
is the best



Image 4 - Pantai di pulau Batam.

\t represents tab
print('Michael \t is the best')
output: Michael is the best

\\ to place single backlash \
print('Michael \\ is the best')
output: Michael \ is the best



Image 5 - Jalan menuju ke Barelang

String: Stride
Name ="M  i  c  h a e l"
              0  1 2  3 4 5 6
Name.find('el'):5 

If not in the substring Michael, thus the result: -1
Name.find('&*D'):-1

Quiz:
1) Name= "Michael Jackson"
What is the result of the following: Name[0]
answer: M

2) Name= "Michael Jackson"
Name[-1]
answer: n

3) What is the output of the following: print("AB\nC\nDE")
answer: 
AB
C
DE

4)What is the result of following?
"hello Mike".find("Mike")
answer: 6


JJ 
What is the result of following?
"hello Mike".find("Mike")

What is the output of the following: print("AB\nC\nDE")
JJ

JJJjjj

Wednesday, December 13, 2023

Python Basics - Expressions dan Variables - Module 1 - Part 2

 Kita sudah bicara Type Data pada posting lalu, silahkan baca kembali:


Image 1 - Suasana di airport, Singapura

Expression adalah operasi (prosedur atau langkah langkah) dalam Python, misalnya:

# Operasi matematika: tambah, kurang, bagi dsb
- Contoh: 10 + 5 = 15.
Angka 10 dan 5 kita sebut operands.
adding: + kita sebut operators

- Kita bisa melakukan operasi perkalian dengan menggunakan asterisk (*): 6*6 = 36

- Operasi pembagian dengan menggunakan forward slash (/): 6/3 = 2.0.
Jikka pakai double slash (//): 6/3 = 2

- Perkalian dan penambahan
contoh 1: 2*60+30 = 150
contoh dua: 30+2*60= 150
contoh tiga: (8+2)*60 = 600


Image 2 - Pakaian saat hari Halloween.

Sekarang, kita bicara tentang variabel.
my_variable=1
my_variable:1

x=43 + 60 + 16 + 41
X:160
X=160, disimpan (store)

Saat ini, kita bisa juga melakukan operasi pada X:
Y=X/60
Y:2.666


Image 3 - Fly in the sky, illustration

Kemudian, kita bicara tentang variable: 
total_min=43 + 42 + 57 = 142

total_hr=total_min/60: 2.367


Image 4 - Cup cake, illustration only

Practice Quiz: Expressions and Variables

1)What is the result of the following operation: 11//2
answer: 5

2) What is the value of x after the following is run:
x=4
x=x/2
answer: 2.0

Monday, December 11, 2023

Python Basics - Module 1 - Part 1

 Ada dua jenis "type" dalam program Python, yaitu Type Data dan Type Function. Type data merujuk pada category data dalam Python.


Image 01 - Bunga Zinnia, hanya ilustrasi.

Diantara 3 type data:
- int: 11
- float: 11.23
- str "Apa Kabar"

11.23 juga disebut real number
-2, -1, 0, 1, 2 juga disebut int (integer)

int, float dan str juga disebut expression.

Kita bisa menggunakan type command, misalnya:
- type(11)
- type(11.23)
- type("Apa Kabar")

Kita bisa convert atau cast (mengubah) int ke float.
- convert atau cast int(2) ke float(2.0), caranya: float(2):2.20
- cast float(11.23) ke int(11), caranya: int(11.23): 11 atau round(11.23): 11
- ubah (cast) int(1) atau float(1.1) ke string (str), caranya str(1): '1' atau str(1.1): '1.1'

Boolean values: True or False

type(True); <class 'bool'>

int(True): 1
int(False): 0

bool(1); True
bool(0): False

Quiz: Type
1) What is the type of the following: 0
answer: int

2) What is the type of the following number: 5.12323
answer: float

What is the result of the following: int(5.99)
answer: 5

Saturday, December 9, 2023

10 Mata Kuliah Untuk Menjadi Seorang Data Scientist

 Ada 10 mata kuliah untuk menjadi seorang "data scientist," menurut EDX, sebuah badan nirlaba yang bekerjasama dengan berbagai universitas dan lembaga.


Image - Hiasan saat Thanksgiving

Ya, paling tidak, dengan mengikuti 10 mata kuliah ini akan mengantarkan anda untuk menjadi "calon" Data Scientist. 

EdX bekerjasama dengan IBM, menawarditawarkan 10 mata kuliah tersebut. Anda harus LULUS ujian setiap mata kuliah untuk kemudian meng"claim" sebagai seorang Data Scientist.

Adapun 10 mata kuliah tersebut adalah sbb:
5) SQL for Data Science
7 Analyzing Data with Python
8 Visualizing Data with Python

Kita akan lanjutkan posting berdasarkan susunan mata kliah di atas!

Terakhir, untuk mengikuti ujian setiap mata kuliah, tentu saja anda harus masuk ke site, di web ini juga anda akan tahu biaya ujian tiap mata kuliah: Program Details | edX

JJ

Friday, January 4, 2019

Ini Dia Negara Paling “Baik, Bersahabat dan Menyenangkan” di Dunia

Amsterdam, credit to Royal Caribbean

HAAS institute, sebuah lembaga studi dibawah Universitas Berkeley, California membuat index yang disebut dengan inclusive index untuk menggambarkan apakah sebuah negara “baik dan menyenangkan” atau “friendly” 

Apa yang dimaksud dengan negara yang “baik dan menyenangkan?” Yaitu negara yang benar benar baik secara keseluruhan, terutama terhadap minoritas dan orang asing atau immigran. 

Indikator yang diukur diantaranya adalah tingkat kebencian, repsentasi politik, UU anti diskriminasi, penghasilan yang setara, tingkat penahanan (masuk penjara) dan kebijaksanaan yang pro immigrant.

Nilai tertinggi 100, sedangkan nilai terendah adalah nol (0). Semakin tinggi nilai, maka negara tersebut semakin “friendly.” Ini daftar negara teratas tahun 2017:

1.Belanda, skor 100
2. Swedia, skor 86.26
3. Norwegia, skor 78.14
4. United Kingdom, skor 68.88
5. Irlandia, skor 68.83
6. Denmark, skor 66.54
7. Canada, skor 66.37
8. Finlandia, skor 66.30
9. Portugal, skor 65.59
10. Germany, skor 63.19

Wow...ternyata saya mengenal baik kesemua negara ini. Alhamdulillah …. Thanks God !!

Apakah Indonesia termasuk negara yang “friendly” atau negara yang “baik, bersahabat dan menyenangkan?” Mari kita cek inclusive index:

Indonesia, skornya: 25.06. Bandingkan dengan negara Timor Timur yang skornya 27.95 dan salah satu negara di benua Afrika, Nigeria yang skornya 28.83. Artinya apa?

Ternyata, Timor Timur dan Nigeria lebih “baik, bersahabat dan menyenangkan.”

PANTASAN !!…. saya selalu dibully di mana mana: KOMPASIANA, FACEBOOK dan BLOGGER.

Begitulah kondisi TANAH AIR BETA…. TANAH TUMPAH DARAH BETA !!

Wednesday, January 2, 2019

Di Amerika, Menunggu Rumah Kosong adalah Pekerjaan

Pekerjaan nunggu rumah kosong, credit to towson service

Ada 12 juta orang Amerika punya rumah lebih dari satu, bahkan ada jutaan yang punya lebih dari dua rumah. Kemudian, untuk apa rumah rumah itu?

Pertama ada yang menjadikannya sebagai rumah untuk “vacation.” Rumah liburan ini kebanyakan terletak di pinggi pantai, biasanya di negara bagian seperti Florida dan Alabama. Kalau musim panas mereka akan bermain di pantai sebagai destinasi liburan.

Untuk yang suka main salju atau ski atau berselancar di atas salju, maka “vacation house” mereka di daerah pegunungan. Biasanya di Colorado yang dinginnya sangat panjang, karena dikepung oleh pegunungan. Selain salju, pemandangan pegunungan juga sangat indah, ditambah dengan udara yang segar.

Tujuan lain pemilikan rumah lebih dari satu adalah untuk menghindari ekstrem-nya temperature, di mana kalau musim dingin di utara seperti di Maine, Vermont, Massachusetts atau New York bisa mencapai dibawah nol, tak jarang minus 20 derjat celcius. Sedangkan musim panas bisa mencapai 40 derjat celcius di selatan Amerika seperti Florida dan Alabama.

Selama musim semi dan panas udara sangat nyaman di utara amerika, sekitar 18 sampai 20 derjat celcius, maka orang orang kaya akan menghuni rumahnya di utara. Sedangkan saat musim gugur dan salju, di selatan udaranya sangat nyaman, sama dengan musim semi dan panas di utara (sekitar 15 dan 20 derjat celcius).

Mereka mereka yang kaya ini hidupnya terus menerus “nyaman” sepanjang tahun, karena bisa berpindah pindah sesuai dengan suhu udara. Saat mereka berada di utara, rumah mereka di selatan menjadi kosong, begitu juga sebaliknya. Rumah kosong ini harus dijaga tentunya.

Menjaga rumah kosong ini adalah pekerjaan, namanya “house sitter” atau “home sitter.” Gajinya cukup lumayan US$ 12.29 perjam, sebulannya sekitar US$2500 (Rp 35 juta). Gaji itu tergolong bersih, karena anda tak perlu mengeluarkan biaya sewa rumah, listrik gratis, bebas menggunakan air dan tak bayar uang sampah. Kalau pemilik rumahnya super kaya dan super baik, akan disediakan transportasi (mobil) untuk anda belanja.

Apakah ada libur atau cuti kerja? Tentu saja ada, asalkan anda mencari pengganti anda selama liburan!


Tuesday, January 1, 2019

Dog Walker, Jenis Pekerjaan Apa Pula?

Dog walker in action, credit to Ali Vingiano

Di negara maju, ada ribuan jenis pekerjaan, dari yang biasa biasa saja sampai yang luar biasa. Dari yang bergaji pas pasan, sampai ke angka yang tidak terpikirkan. Banyak hal yang tidak terduga, dan tidak bisa diduga duga.

Ada pekerjaan namanya “dog walker.” biasanya orang yang sudah pensiun melakoni kerjaan ini, tujuannya: badan bergerak (olahraga ringan), menghirup udara segar dan uang tambahan. Gajinya lumayan, sebesar US$13,27 per-jam.

Dog walker ini banyak ditemui di taman taman publik milik pemerintah pusat maupun pemda. Kadang kadang juga ditemui di sekitar warung kopi, dog walker-nya istirahat sambil minum kopi. Juga bisa ditemui di sekitar “pet shop” (toko yang menjual makanan hewan peliharaan). 

Selain pensiunan, ini kerjaan untuk anak anak remaja yang sedang liburan musim panas. Kerjaan part-time (paruh waktu) istilahnya. Seminggu kerjanya cuma 20 jam, sebulan 88 jam. Kalikan saja: 88 jam x US$ 13,27 = US$ 1.168, kalau dirupiahkan sekitar Rp 18 juta sebulan. Lumayan untuk seorang pensiunan atau uang jajan untuk anak SMP, SMA atau mahasiswa.

Di mana mendapatkan pekerjaan ini? Ya, banyak sekali. Bisa melalui iklan di koran koran local atau iklan yang ditempel di “mini market.” Bisa juga dari mulut ke mulut. 

Banyak sekali yayasan yayasan (NGO) yang bertujuan untuk memberi perlindungan kepada anjing maupun kucing yang ditinggalkan oleh tuannya di jalan. Mereka “mengadopsi” hewan hewan ini, kemudian ditempatkan di “shelter” (rumah perlindungan). 

Nah, shelter ini juga menawarkan pekerjaan: sebagai pelatih, perawat atau tukang bawa jalan jalan anjing dan kucing (pet walker). 

Untuk anjing dan kucing yang sudah “terlatih” (mengerti perintah perintah) dan terawat dengan baik, akan ditawarkan ke “tuan tuan” baru. Begitulah siklusnya. Menarik bukan?