diff --git a/README.md b/README.md index 0955397..e2df4ec 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# basic-python-course +# Basic-python-course ## Week 1 @@ -7,16 +7,18 @@ ```python print('what to print') ``` +- output :
+what to print ### How to write comments -#### Single Line +### Single Line ```python # this is a single line comment. ``` -#### Multi Line +### Multi Line ```python ''' @@ -28,7 +30,158 @@ comment. ### How to declare variables ```python -a = 'some text' # declare a string variable -b = 123 # an integer -c = True # a boolean +a = 'some text' # declare a string variable +b = 123 # an integer +c = True # a boolean +d = "hello world" # declare a string variable menggunakaan tanda ("") petik dua +e = false # a boolean +f = 3.5 # a float +g = 2+3j # a complex +h = [1,2,3,4] # a list +i = ("w","o","r","l","d") # a tuple + +``` +### Conditional + +```python +def test_conditional(): + nama = "wenty" + + if(nama == "wenty"): + print ("this is if") + elif (nama == "tiara"): + print ("this is elif") + else: + print("else") + +test_conditional() +``` +- output :
+this is if + +### How to Looping + +```python +def test_looping(): + for x in range (0,5): + print("nilai x:",x) + + i = 0 + while (i<5): + print ("nilai i:",i) + i += 1 + +test_looping() +``` +- output :
+nilai x: 0
+nilai x: 1
+nilai x: 2
+nilai x: 3
+nilai x: 4
+nilai i: 0
+nilai i: 1
+nilai i: 2
+nilai i: 3
+nilai i: 4 + +### Function and Exception + +```python +def test_function_return_value(input_value): + return input_value * 2 + +print(test_function_return_value(4)) +print("-----------------") + +def my_function(temp_string): + print(temp_string + "<---") + return temp_string + "done" + +print(my_function("Test 1")) +print(my_function("Test 2")) +print(my_function("Test 3")) +print("-----------------") + +def your_function(temp_string): + try: + result = temp_string + 5 + except TypeError: + print(temp_string + "<---") + finally: + print("yeaaay") + +your_function("test 1") +your_function("test 2") +``` +- output :
+8
+-----------------
+Test 1<---
+Test 1done
+Test 2<---
+Test 2done
+Test 3<---
+Test 3done
+-----------------
+test 1<---
+yeaaay
+test 2<---
+yeaaay + +### Namespace + +```python +global_var = 10 +def test_namespace(): + private_var = 5 + global_var = 6 + + print ("private_var :", private_var) + print ("global_var :", global_var) + +print("global_var:",global_var) +test_namespace() +``` +- output :
+global_var: 10
+private_var : 5
+global_var : 6 + +### String Operator + +```python +def test_string_operator(): + full_string = "this is full string" + temp_string = "temp string" + upper_string = "THIS IS UPPER" + lower_string = "this is lower" + + # Concat : + print(full_string+temp_string) + # Slicing : + print(full_string[3:6]) + # Lower : + print(upper_string.lower()) + # Upper : + print(lower_string.upper()) + # Len : + print(len(full_string)) + # Strip : + print(full_string.strip("t")) + # Replace : + print(full_string.replace("full","name")) + # Split + print(full_string.split("is")) + +test_string_operator() ``` +- output :
+this is full stringtemp string
+s i
+this is upper
+THIS IS LOWER
+19
+his is full string
+this is name string
+['th', ' ', ' full string'] diff --git a/defrianafandi/list.py b/defrianafandi/list.py new file mode 100644 index 0000000..20b4e2a --- /dev/null +++ b/defrianafandi/list.py @@ -0,0 +1,13 @@ +my_list = [] + +my_list.append(1) +my_list.append(3) +my_list.append(5) + +my_list.append(("Defrian", "Afandi", "Hasan")) + +colors = {} +colors["Biru"] = "Blue" +my_list.append(colors) + +print(my_list) \ No newline at end of file diff --git a/defrianafandi/list_2D.py b/defrianafandi/list_2D.py new file mode 100644 index 0000000..72974a5 --- /dev/null +++ b/defrianafandi/list_2D.py @@ -0,0 +1,31 @@ +list2d = [] +list2d.append([]) +list2d.append([]) +list2d.append([]) +list2d.append([]) + + +list2d[0].append(1) +list2d[0].append(1) +list2d[0].append(1) +list2d[0].append(1) + +list2d[1].append(1) +list2d[1].append("-") +list2d[1].append("-") +list2d[1].append(1) + +list2d[2].append(1) +list2d[2].append("-") +list2d[2].append("-") +list2d[2].append(1) + +list2d[3].append(1) +list2d[3].append(1) +list2d[3].append(1) +list2d[3].append(1) + +for y in list2d: + for x in y: + print(x, end=" ") + print() \ No newline at end of file diff --git a/defrianafandi/list_comprhension.py b/defrianafandi/list_comprhension.py new file mode 100644 index 0000000..675fc18 --- /dev/null +++ b/defrianafandi/list_comprhension.py @@ -0,0 +1,22 @@ +angka = [1, 2, 3] +print("isi List : ", angka) + +multiply8 = [ m * 8 for m in angka] +print("Nilai 8 kali dari isi elemen List : ", multiply8) + +a = [1, 2, 3, 4, 5] +x = a + a +y = a * 3 + +print("a : ", a) +print("x : ", x) +print("y : ", y) + +jumlah = sum(a) +print("jumlah dari a : ", jumlah) + +terbesar = max(a) +print("Nilai maksimum dari a : ", terbesar) + +terkecil = min(a) +print("Nilai minimum dari a : ", terkecil) \ No newline at end of file diff --git a/defrianafandi/list_loop.py b/defrianafandi/list_loop.py new file mode 100644 index 0000000..c7a9122 --- /dev/null +++ b/defrianafandi/list_loop.py @@ -0,0 +1,20 @@ +friends = ["Fahmi", "Tomi", "Ahmad", "Abi", "Ardi"] +for teman in friends: + print(teman) + +print() +print(friends[0]) +print(friends[3]) +print(friends[2]) +print(friends[4]) +print(friends[1]) + +print() +print("Asli : ", friends) +friends.reverse() +print("Reverse : ", friends) + +friends.pop() +print("Pop : ", friends) +friends.append("Ama") +print("Append : ", friends) \ No newline at end of file diff --git a/defrianafandi/list_method.py b/defrianafandi/list_method.py new file mode 100644 index 0000000..3e9fe27 --- /dev/null +++ b/defrianafandi/list_method.py @@ -0,0 +1,23 @@ +friends = ["Faiz Alauddin"] +friends.append("Ma'ruf") +print("Isi List : ", friends) + +friends.insert(1, "Darmawan") +print("List My friends : ", friends) + +print() +women_friends = ["Tsniyah", "Rahmawati", "Tasya", "Rini"] +friends.extend(women_friends[0:4]) +print("Gabungkan List : ", friends) + +print() +friends.sort() +print("Urutkan Berdasakan abjad : ", friends) + +print() +friends.reverse() +print("Reverse : ", friends) + +print() +friends.remove("Alauddin") +print("Remove Mustofa : ", friends) \ No newline at end of file diff --git a/defrianafandi/tuple.py b/defrianafandi/tuple.py new file mode 100644 index 0000000..25a8a4c --- /dev/null +++ b/defrianafandi/tuple.py @@ -0,0 +1,25 @@ +my_tuple = tuple(["Hot", "Cold", "Windy"]) + +print("Suhu : ", my_tuple) +# list berisi angka +a = (1, 2, 3, 4, 5) + +# Tuple berisi karakter dan string +b = ('a', 'b', 'c', 'd') +c = ('Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat') + +# Tuple berisi list, tuple, dan dictionary +d = ([1, 2, 3], [1, 2, 3], [1, 2, 3]) +e = ((1, 2, 3), (1, 2, 3), (1, 2, 3)) +f = ({'a':1, 'b':2, 'c':3},{'a':2, 'b':3, 'c':4},{'a':3, 'b':4, 'c':5}) + +# Tuple campuran +g = (1, 'b', 2, 'Hot', [1, 2, 3], (1, 2, 3), {'Cold':1, 'Windy':2}) + +print ("Tuple Angka : ", a) +print ("Tuple Karakter : ", b) +print ("Tuple String : ", c) +print ("Tuple berisi List : ", d) +print ("Tuple berisi Tuple : ", e) +print ("Tuple berisi Dictionary : ", f) +print ("Tuple Campuran : ", g) \ No newline at end of file diff --git a/defrianafandi/tuple_immutable.py b/defrianafandi/tuple_immutable.py new file mode 100644 index 0000000..a71e79b --- /dev/null +++ b/defrianafandi/tuple_immutable.py @@ -0,0 +1,13 @@ +Animal = ('bird', 'snake', 'mouse') + +# This causes an error. +# TypeError: 'tuple' object does not support item assignment +# tuple[0] = 'cat' + +#mengubah isi tuple harus diubah ke list dulu +t = list(Animal) +t[0] = "burung" +t[1] = "ular" +Animal = tuple(t) + +print("Mengubah Isi Tuple : ", Animal) \ No newline at end of file diff --git a/defrianafandi/tuple_pack_unpack.py b/defrianafandi/tuple_pack_unpack.py new file mode 100644 index 0000000..39bd4bd --- /dev/null +++ b/defrianafandi/tuple_pack_unpack.py @@ -0,0 +1,11 @@ +# Create packed tuple. +fruits = ("5 kg", "Anggur", "Ungu") + +# Unpack tuple. +(berat, Nama_Buah, warna) = fruits + +# Display unpacked variables. +print("Berat : ", berat) +print("Nama Buah : ", Nama_Buah) +print("Warna : ", warna) +print("Isi List : ", fruits) \ No newline at end of file diff --git a/defrianafandi/week2_dictionary.py b/defrianafandi/week2_dictionary.py new file mode 100644 index 0000000..59d61e8 --- /dev/null +++ b/defrianafandi/week2_dictionary.py @@ -0,0 +1,38 @@ +buah = {} +buah["Apple"] = 20 +buah["Klengkeng"] = 50 +buah[1] = "rambutan" + +print(buah) + +#get, untuk mengambil value dengan key tertentu +#has_key, untuk memeriksa apakah dict mempunyai suatu key +#keys, mengambil key saja +#items, mengambil value saja +#update, menambah isi dict +#pop, membuang salah satu elemen berdasarkan dict +#popitem, membuang salah satu elemen +#clear, mengosongkan dict + +x = {"nama":"defrian", "kelas":"reg A", "jurusan":"IF"} +y = {"zoo":"no"} + +print(x.get("kelas")) + +print(x.keys()) +print(x.items()) + +x.update(y) +print(x) + +x.pop("kelas") +print(x) + +x.popitem() +print(x) + +x.clear() +y.clear() + +print(x) +print(y) \ No newline at end of file