diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/01. Pengenalan Python - Variabel, Tipe Data, Operator, dan Statement Kondisi.ipynb b/01. Pengenalan Python - Variabel, Tipe Data, Operator, dan Statement Kondisi.ipynb index f47e075..92c9052 100644 --- a/01. Pengenalan Python - Variabel, Tipe Data, Operator, dan Statement Kondisi.ipynb +++ b/01. Pengenalan Python - Variabel, Tipe Data, Operator, dan Statement Kondisi.ipynb @@ -25,11 +25,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "40353607\n" + ] + } + ], "source": [ - "print(\"Hello World\")" + "print(7**9)" ] }, { @@ -42,13 +50,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n" + ] + } + ], "source": [ "nama = \"Guntur Budi Herwanto\"\n", + "ph = 6\n", "\n", - "print(nama)" + "print(type(nama))\n", + "print(type(ph))" ] }, { @@ -60,9 +79,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Adiyo Hidayat\n" + ] + } + ], "source": [ "nama = \"Guntur Budi Herwanto\"\n", "nama = \"Adiyo Hidayat\"\n", @@ -79,9 +106,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "3.2\n", + "Hello\n" + ] + } + ], "source": [ "a, b, c = 5, 3.2, \"Hello\"\n", "\n", @@ -92,11 +129,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "8\n", + "8\n" + ] + } + ], "source": [ - "x = y = z = \"podo\"\n", + "x = y = z = 8\n", "\n", "print (x)\n", "print (y)\n", @@ -151,9 +198,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False mempunyai tipe \n", + "True mempunyai tipe \n" + ] + } + ], "source": [ "a = 5>6\n", "print(a, \"mempunyai tipe\", type(a))\n", @@ -222,14 +278,18 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "81\n" + "9\n", + "1.25\n", + "1\n", + "20\n", + "625\n" ] } ], @@ -237,7 +297,11 @@ "x = 5\n", "y = 4\n", "\n", - "print((x+y)*(x+y))\n" + "print(x+y)\n", + "print(x/y)\n", + "print(x//y)\n", + "print(x*y)\n", + "print(x**y)\n" ] }, { @@ -250,7 +314,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -288,29 +352,34 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "4036000.0\n" + "240000.0\n", + "13000\n", + "Harga Total = 1453000.0\n" ] } ], "source": [ - "harga_buku = 500000\n", + "harga_buku = 300000\n", "diskon = 20.0\n", - "ongkir_pertama = 9000\n", - "ongkir_selanjutnya = 3000\n", - "eksemplar = 10\n", + "ongkir_pertama = 8000\n", + "ongkir_selanjutnya = 1000\n", + "eksemplar = 6\n", "\n", "harga_setelah_diskon = harga_buku * (100-diskon)/100\n", + "print(harga_setelah_diskon)\n", + "\n", "harga_ongkir = ongkir_pertama + ongkir_selanjutnya * (eksemplar-1)\n", - "harga_total = harga_setelah_diskon * eksemplar + harga_ongkir\n", + "print(harga_ongkir)\n", "\n", - "print(harga_total)" + "harga_total = harga_setelah_diskon * eksemplar + harga_ongkir\n", + "print(\"Harga Total = \",harga_total)\n" ] }, { @@ -330,9 +399,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x > y is False\n", + "x < y is True\n", + "x == y is False\n", + "x != y is True\n", + "x >= y is False\n", + "x <= y is True\n" + ] + } + ], "source": [ "x = 10\n", "y = 12\n", @@ -405,7 +487,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -419,10 +501,10 @@ ], "source": [ "nilai = 8 # Coba dirubah menjadi 80\n", - "if nilai>90:\n", + "if nilai>=90:\n", " print(\"Lulus\")\n", - " print(\"Selamat\")\n", - " \n", + "\n", + "print(\"Selamat\")\n", "print(\"Program Selesai\")" ] }, @@ -444,9 +526,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Maaf Anda Belum Berhasil\n", + "Program Selesai\n" + ] + } + ], "source": [ "nilai = 80 \n", "if nilai>90:\n", @@ -474,16 +565,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Perbaikan\n", + "Program Selesai\n" + ] + } + ], "source": [ - "nilai = 85 \n", + "nilai = 65 \n", "if nilai>=90:\n", " print(\"Lulus\")\n", " print(\"Selamat\")\n", "elif nilai>80:\n", " print(\"Anda masuk ke waiting list\")\n", + "elif nilai>60:\n", + " print(\"Perbaikan\")\n", "else:\n", " print(\"Maaf Anda Belum Berhasil\")\n", " \n", @@ -499,18 +601,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Selamat Anda Berhak Mengikuti Program Beasiswa\n", + "Program Selesai\n" + ] + } + ], "source": [ - "nilai = 95\n", - "umur = 10\n", + "nilai = 91\n", + "umur = 18\n", "\n", - "if nilai>90:\n", - " if umur > 17:\n", - " print(\"Selamat Anda Berhak Mengikuti Program Beasiswa\")\n", - " else:\n", - " print(\"Maaf, Anda belum berhak mengikuti program beasiswa\")\n", + "if nilai>90 and umur > 17:\n", + " print(\"Selamat Anda Berhak Mengikuti Program Beasiswa\")\n", + "elif umur<=17:\n", + " print(\"Umur belum cukup\")\n", "else:\n", " print(\"Maaf Anda Belum Berhasil\")\n", " \n", @@ -548,25 +658,32 @@ "\n", "Buatlah sebuah program yang memiliki dua variabel berat badan dengan satuan KG, dan tinggi badan dengan satuan CM, kemudian keluaran dari program adalah Kategori BMI" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.15" + "pygments_lexer": "ipython3", + "version": "3.6.7" } }, "nbformat": 4, diff --git a/02 - Pengenalan Python - List, Tuple, Dictionary, String, Perulangan.ipynb b/02 - Pengenalan Python - List, Tuple, Dictionary, String, Perulangan.ipynb index 5990b81..cdb0eda 100644 --- a/02 - Pengenalan Python - List, Tuple, Dictionary, String, Perulangan.ipynb +++ b/02 - Pengenalan Python - List, Tuple, Dictionary, String, Perulangan.ipynb @@ -10,13 +10,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 3125\n", + "6 46656\n", + "7 823543\n", + "8 16777216\n", + "9 387420489\n" + ] + } + ], "source": [ "#SINTAKS FOR\n", - "for j in range(0,10,2):\n", - " print(j,j**2)" + "for i in range(5,10):\n", + " print(i,i**i)" ] }, { @@ -26,9 +38,9 @@ "outputs": [], "source": [ "#SINTAKS WHILE\n", - "i = 0\n", - "while i<10:\n", - " print(i)" + "i=5\n", + "while(i<10):\n", + " print(i,i**i)\n" ] }, { @@ -41,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 108, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -84,7 +96,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -94,8 +106,7 @@ "30\n", "70\n", "90\n", - "70\n", - "Catra\n" + "70\n" ] } ], @@ -103,9 +114,7 @@ "print(nilai[0])\n", "print(nilai[4])\n", "print(nilai[-1])\n", - "print(nilai[-3])\n", - "\n", - "print(peserta[-2])" + "print(nilai[-3])\n" ] }, { @@ -117,14 +126,14 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[40, 50, 60, 70]\n" + "[30, 40, 50, 60, 70, 80, 90, 90, 10, 20, 30, 40]\n" ] } ], @@ -150,9 +159,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[40, 50, 60, 70, 80, 90, 90, 10, 20, 30, 40]\n", + "[60, 70, 80]\n", + "[40, 50, 60, 70, 80, 90, 90, 10, 20]\n", + "[90, 90, 10, 20, 30, 40]\n", + "[40, 50, 60, 70, 80, 90, 90, 10, 20, 30, 40]\n" + ] + } + ], "source": [ "print(nilai)\n", "\n", @@ -179,37 +200,31 @@ }, { "cell_type": "code", - "execution_count": 109, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "900\n", "1600\n", "2500\n", "3600\n", "4900\n", "6400\n", "8100\n", - "(0, 30)\n", - "(1, 40)\n", - "(2, 50)\n", - "(3, 60)\n", - "(4, 70)\n", - "(5, 80)\n", - "(6, 90)\n" + "8100\n", + "100\n", + "400\n", + "900\n", + "1600\n" ] } ], "source": [ "for n in nilai:\n", " print(n**2)\n", - "\n", - "# Mengikutkan Indeksnya\n", - "for i,n in enumerate(nilai):\n", - " print(i,n)" + "\n" ] }, { @@ -222,20 +237,22 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "60.0\n" + "[40, 50, 60, 70, 80, 90, 90, 10, 20, 30, 40]\n", + "52.72727272727273\n" ] } ], "source": [ "# ALGORITMA RATA-RATA\n", "total = 0.0\n", + "print(nilai)\n", "for n in nilai:\n", " total = total + float(n)\n", " \n", @@ -252,19 +269,19 @@ }, { "cell_type": "code", - "execution_count": 113, + "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "(92, 'tidak ada dalam list')\n" + "90 ada dalam list\n" ] } ], "source": [ - "n = 92\n", + "n = 90\n", "if n in nilai:\n", " print(n, \"ada dalam list\")\n", "else:\n", @@ -285,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -293,11 +310,8 @@ "output_type": "stream", "text": [ "66\n", - "-\n", "88\n", - "-\n", - "54\n", - "-\n" + "54\n" ] } ], @@ -305,8 +319,7 @@ "tugas = [66,77,88,99,55,54]\n", "for n in tugas:\n", " if n%2==0:\n", - " print(n)\n", - " print(\"-\")" + " print(n)" ] }, { @@ -338,20 +351,9 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Januari\n", - "Mei\n", - "Desember\n", - "Oktober\n" - ] - } - ], + "outputs": [], "source": [ "print(bulan[0])\n", "print(bulan[4])\n", @@ -361,20 +363,9 @@ }, { "cell_type": "code", - "execution_count": 122, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Maret', 'April', 'Mei')\n", - "('Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli')\n", - "('Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember')\n", - "('Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember')\n" - ] - } - ], + "outputs": [], "source": [ "print(bulan[2:5])\n", "\n", @@ -395,28 +386,9 @@ }, { "cell_type": "code", - "execution_count": 123, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Januari\n", - "Februari\n", - "Maret\n", - "April\n", - "Mei\n", - "Juni\n", - "Juli\n", - "Agustus\n", - "September\n", - "Oktober\n", - "November\n", - "Desember\n" - ] - } - ], + "outputs": [], "source": [ "for b in bulan:\n", " print(b)" @@ -439,24 +411,11 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Nama': 'Budi', 'JenisKelamin': 'Laki-Laki', 'Umur': 7}\n", - "Budi\n", - "7\n", - "Laki-Laki\n", - "[{'Nama': 'Budi', 'JenisKelamin': 'Laki-Laki', 'Umur': 7}, {'Nama': 'Ani', 'JenisKelamin': 'Perempuan', 'Umur': 8}]\n" - ] - } - ], + "outputs": [], "source": [ - "siswa = {'Nama': 'Budi', 'Umur': 7, 'JenisKelamin': 'Laki-Laki'}\n", - "siswa2 = {'Nama': 'Ani', 'Umur': 8, 'JenisKelamin': 'Perempuan'}\n", + "hasil = \n", "print(siswa)\n", "print(siswa['Nama'])\n", "print(siswa['Umur'])\n", @@ -471,19 +430,9 @@ }, { "cell_type": "code", - "execution_count": 128, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Nama', 'Budi')\n", - "('JenisKelamin', 'Laki-Laki')\n", - "('Umur', 7)\n" - ] - } - ], + "outputs": [], "source": [ "for k,v in siswa.items():\n", " print(k,v)" @@ -498,17 +447,9 @@ }, { "cell_type": "code", - "execution_count": 129, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Nama': 'Deni', 'JenisKelamin': 'Laki-Laki', 'Umur': 7}\n" - ] - } - ], + "outputs": [], "source": [ "siswa['Nama'] = 'Deni'\n", "print(siswa)" @@ -523,17 +464,9 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Nama': 'Deni', 'JenisKelamin': 'Laki-Laki', 'Alamat': 'Jl. Kaliurang KM 5', 'Umur': 7}\n" - ] - } - ], + "outputs": [], "source": [ "siswa['Alamat'] = \"Jl. Kaliurang KM 5\"\n", "print(siswa)" @@ -541,24 +474,9 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Nama': 'Deni', 'JenisKelamin': 'Laki-Laki', 'Alamat': 'Jl. Kaliurang KM 5', 'Nilai': [30, 40, 50, 60, 70, 80, 90], 'Umur': 7}\n", - "30\n", - "40\n", - "50\n", - "60\n", - "70\n", - "80\n", - "90\n" - ] - } - ], + "outputs": [], "source": [ "# List sebagai nilai dari elemen Dictionary\n", "siswa[\"Nilai\"] = nilai\n", @@ -570,26 +488,9 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "============\n", - "('Nama', 'Deni')\n", - "('JenisKelamin', 'Laki-Laki')\n", - "('Alamat', 'Jl. Kaliurang KM 5')\n", - "('Nilai', [30, 40, 50, 60, 70, 80, 90])\n", - "('Umur', 7)\n", - "============\n", - "('Nama', 'Ani')\n", - "('JenisKelamin', 'Perempuan')\n", - "('Umur', 8)\n" - ] - } - ], + "outputs": [], "source": [ "for l in list_siswa:\n", " print(\"============\")\n", @@ -609,19 +510,9 @@ }, { "cell_type": "code", - "execution_count": 137, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Tutori\n", - "ograman Py\n", - "Python\n" - ] - } - ], + "outputs": [], "source": [ "nama = \"Tutorial Bahasa Pemrograman Python\"\n", "print(nama[0:6])\n", @@ -631,50 +522,9 @@ }, { "cell_type": "code", - "execution_count": 138, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "T\n", - "u\n", - "t\n", - "o\n", - "r\n", - "i\n", - "a\n", - "l\n", - " \n", - "B\n", - "a\n", - "h\n", - "a\n", - "s\n", - "a\n", - " \n", - "P\n", - "e\n", - "m\n", - "r\n", - "o\n", - "g\n", - "r\n", - "a\n", - "m\n", - "a\n", - "n\n", - " \n", - "P\n", - "y\n", - "t\n", - "h\n", - "o\n", - "n\n" - ] - } - ], + "outputs": [], "source": [ "for n in nama:\n", " print(n)" @@ -690,20 +540,9 @@ }, { "cell_type": "code", - "execution_count": 152, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Pada hari minggu kuturut ayah ke kota', ' Naik Delman istimewa kududuk di muka', ' Duduk disamping pak kusir yang sedang bekerja']\n", - "['Pada', 'hari', 'minggu', 'kuturut', 'ayah', 'ke', 'kota']\n", - "['Naik', 'Delman', 'istimewa', 'kududuk', 'di', 'muka']\n", - "['Duduk', 'disamping', 'pak', 'kusir', 'yang', 'sedang', 'bekerja']\n" - ] - } - ], + "outputs": [], "source": [ "# kata = nama.split()\n", "# print(kata)\n", @@ -727,19 +566,9 @@ }, { "cell_type": "code", - "execution_count": 155, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "('Python', 'Found!')\n", - "('Java', 'Not Found!')\n", - "('C++', 'Not Found!')\n" - ] - } - ], + "outputs": [], "source": [ "cari = 'Python'\n", "if cari in nama:\n", @@ -764,17 +593,9 @@ }, { "cell_type": "code", - "execution_count": 158, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "pada hari ini saya belajar\n" - ] - } - ], + "outputs": [], "source": [ "paragraf = \"Pd hr ini saya BljR\"\n", "paragraf = paragraf.lower()\n", @@ -793,19 +614,9 @@ }, { "cell_type": "code", - "execution_count": 157, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "34\n", - "TUTORIAL BAHASA PEMROGRAMAN PYTHON\n", - "tutorial bahasa pemrograman python\n" - ] - } - ], + "outputs": [], "source": [ "print(len(nama))\n", "print(nama.upper())\n", @@ -828,17 +639,9 @@ }, { "cell_type": "code", - "execution_count": 160, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[77, 98, 60, 16, 54, 69, 10, 55, 65, 80, 84, 83, 3, 25, 47, 52, 67, 18, 14, 10, 27, 42, 53, 23, 64, 44, 17, 72, 68, 28, 69, 64, 53, 4, 36, 28, 13, 38, 4, 94, 68, 98, 40, 97, 90, 70, 76, 50, 35, 42, 15, 18, 57, 55, 45, 44, 88, 45, 50, 83, 62, 74, 73, 69, 33, 53, 24, 5, 51, 5, 1, 90, 95, 28, 46, 27, 16, 67, 33, 58, 16, 5, 23, 93, 43, 70, 86, 90, 36, 86, 6, 72, 65, 31, 27, 13, 9, 62, 4, 99, 23, 28, 43, 52, 45, 18, 94, 7, 57, 63, 39, 58, 99, 8, 78, 8, 92, 21, 74, 55, 58, 42, 95, 95, 64, 14, 50, 7, 89, 78, 78, 91, 73, 29, 58, 63, 96, 87, 17, 94, 51, 35, 66, 76, 39, 9, 84, 22, 79, 52, 17, 81, 68, 39, 49, 23, 88, 48, 93, 93, 51, 53, 85, 21, 68, 18, 87, 87, 36, 37, 57, 55, 42, 15, 5, 51, 45, 38, 4, 28, 92, 70, 86, 76, 94, 79, 36, 8, 94, 86, 32, 59, 37, 46, 3, 33, 96, 16, 86, 10, 23, 20, 6, 23, 83, 30, 8, 25, 43, 88, 42, 82, 81, 85, 53, 25, 5, 91, 64, 77, 14, 3, 66, 94, 42, 50, 99, 25, 96, 65, 71, 41, 92, 68, 41, 1, 44, 73, 6, 25, 62, 44, 62, 52, 50, 78, 99, 30, 25, 56]\n" - ] - } - ], + "outputs": [], "source": [ "import random\n", "\n", @@ -879,17 +682,9 @@ }, { "cell_type": "code", - "execution_count": 168, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Barcelona', 'berhasil', 'merebut', 'posisi', 'puncak', 'klasemen', 'La', 'Liga', 'Spanyol', 'seusai', 'menang', '4', '2', 'atas', 'Sevilla', 'pada', 'pertandingan', 'pekan', 'kesembilan', 'di', 'Stadion', 'Camp', 'Nou,', 'Sabtu', '20', '10', '2018', 'atau', 'Minggu', 'dini', 'hari', 'WIB.', 'Barcelona', 'membuka', 'keunggulan', 'pada', 'menit', 'ke', '2', 'melalui', 'gol', 'yang', 'dicetak', 'oleh', 'Philippe', 'Coutinho.', 'Lionel', 'Messi', 'menggandakan', 'keunggulan', 'Barcelona,', '10', 'menit', 'berselang.', 'Namun,', 'nahas', 'bagi', 'Barcelona,', 'karena', 'Messi', 'harus', 'ditarik', 'keluar', 'pada', 'menit', 'ke', '26', 'setelah', 'mengalami', 'cedera.', 'Tanpa', 'Messi,', 'skor', '2', '0', 'bertahan', 'hingga', 'babak', 'pertama', 'berakhir.']\n" - ] - } - ], + "outputs": [], "source": [ "paragraf = \"Barcelona berhasil merebut posisi puncak klasemen La Liga Spanyol seusai menang 4-2 atas Sevilla pada pertandingan pekan kesembilan di Stadion Camp Nou, Sabtu (20/10/2018) atau Minggu dini hari WIB. Barcelona membuka keunggulan pada menit ke-2 melalui gol yang dicetak oleh Philippe Coutinho. Lionel Messi menggandakan keunggulan Barcelona, 10 menit berselang. Namun, nahas bagi Barcelona, karena Messi harus ditarik keluar pada menit ke-26 setelah mengalami cedera. Tanpa Messi, skor 2-0 bertahan hingga babak pertama berakhir.\"\n", "paragraf = paragraf.replace(\"/\",\" \")\n", @@ -909,21 +704,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.15" + "name": "python3" } }, "nbformat": 4, diff --git a/2019/.ipynb_checkpoints/Modul 4 - Function Tuple Dictionaries and Data Processing-checkpoint.ipynb b/2019/.ipynb_checkpoints/Modul 4 - Function Tuple Dictionaries and Data Processing-checkpoint.ipynb index 20dd539..16771da 100644 --- a/2019/.ipynb_checkpoints/Modul 4 - Function Tuple Dictionaries and Data Processing-checkpoint.ipynb +++ b/2019/.ipynb_checkpoints/Modul 4 - Function Tuple Dictionaries and Data Processing-checkpoint.ipynb @@ -24,25 +24,9 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please, Enter a value: \n", - "3\n", - "3\n", - "Please, Enter a value: \n", - "3\n", - "3\n", - "Please, Enter a value: \n", - "4\n", - "4\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Motivasi function adalah perulangan kode\n", "\n", @@ -75,27 +59,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Dimulai dari sini\n", - "Please, Enter a value: \n", - "100\n", - "100\n", - "Please, Enter a value: \n", - "100\n", - "100\n", - "Please, Enter a value: \n", - "100\n", - "100\n", - "Berhenti disini\n" - ] - } - ], + "outputs": [], "source": [ "def message():\n", " print(\"Please, Enter a value: \")\n", @@ -118,18 +84,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "pesan: HELLO\n", - "pesan: HELLO JUGA\n" - ] - } - ], + "outputs": [], "source": [ "def message(m):\n", " print(\"pesan: \",m.upper())\n", @@ -140,19 +97,9 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter telephone number 11\n", - "Enter price number 5\n", - "Enter number number number\n" - ] - } - ], + "outputs": [], "source": [ "def message(what, number):\n", " print(\"Enter\", what, \"number\", number)\n", @@ -164,17 +111,9 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Guntur 5.6\n" - ] - } - ], + "outputs": [], "source": [ "def tambahUmur(nama, umur):\n", " umur = umur + 0.5\n", @@ -193,20 +132,9 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is Luke Skywalker\n", - "Hello, my name is Skywalker Luke\n", - "Hello, my name is Jesse Quick\n", - "Hello, my name is Clark Kent\n" - ] - } - ], + "outputs": [], "source": [ "def introduction(firstName, lastName):\n", " print(\"Hello, my name is\", firstName, lastName)\n", @@ -219,18 +147,9 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is James Bond\n", - "Hello, my name is Luke Skywalker\n" - ] - } - ], + "outputs": [], "source": [ "introduction(firstName = \"James\", lastName = \"Bond\")\n", "introduction(lastName = \"Skywalker\", firstName = \"Luke\")" @@ -238,17 +157,9 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 + 6 + 4 = 15\n" - ] - } - ], + "outputs": [], "source": [ "def jumlah(a, b, c):\n", " print(a, \"+\", b, \"+\", c, \"=\", a + b + c)\n", @@ -266,17 +177,9 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is Guntur Smith\n" - ] - } - ], + "outputs": [], "source": [ "#All required parameters must be placed before any default arguments.\n", "\n", @@ -289,17 +192,9 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is James Guntur Bond\n" - ] - } - ], + "outputs": [], "source": [ "def introductionLengkap(firstName, lastName, middleName=\"Guntur\"):\n", " print(\"Hello, my name is\", firstName, middleName, lastName)\n", @@ -316,17 +211,9 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The boringFunction has returned its result. It's: 123\n" - ] - } - ], + "outputs": [], "source": [ "def boringFuncton():\n", " return 123\n", @@ -338,19 +225,9 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This lesson is interesting!\n", - "'Boredom Mode' ON.\n", - "This lesson is boring...\n" - ] - } - ], + "outputs": [], "source": [ "def boringFunction():\n", " print(\"'Boredom Mode' ON.\")\n", @@ -363,17 +240,9 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n" - ] - } - ], + "outputs": [], "source": [ "def jumlah(a,b):\n", " c = a + b\n", @@ -385,17 +254,9 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sorry, you don't carry any value\n" - ] - } - ], + "outputs": [], "source": [ "value = None\n", "if value == None:\n", @@ -404,18 +265,9 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "None\n" - ] - } - ], + "outputs": [], "source": [ "def strangeFunction(n):\n", " if(n % 2 == 0):\n", @@ -427,20 +279,9 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "47" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def sumOfList(lst):\n", " sum = 0\n", @@ -455,27 +296,9 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Insert 0\n", - "[0]\n", - "Insert 1\n", - "[1, 0]\n", - "Insert 2\n", - "[2, 1, 0]\n", - "Insert 3\n", - "[3, 2, 1, 0]\n", - "Insert 4\n", - "[4, 3, 2, 1, 0]\n", - "Hasil = [4, 3, 2, 1, 0]\n" - ] - } - ], + "outputs": [], "source": [ "def strangeListFunction(n):\n", " strangeList = []\n", @@ -526,20 +349,9 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1900 ->OK\n", - "2000 ->OK\n", - "2016 ->OK\n", - "1987 ->OK\n" - ] - } - ], + "outputs": [], "source": [ "def isYearLeap(year):\n", " if (year % 4) == 0:\n", @@ -581,21 +393,9 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'YY' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mscopeTest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mYY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mNameError\u001b[0m: name 'YY' is not defined" - ] - } - ], + "outputs": [], "source": [ "def scopeTest():\n", " YY = 123\n", @@ -606,18 +406,9 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Do I know that variable? 1\n", - "1\n" - ] - } - ], + "outputs": [], "source": [ "def myFunction():\n", " print(\"Do I know that variable?\", var)\n", @@ -632,18 +423,9 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Do I know that variable? 2\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "def myFunction():\n", " global var\n", @@ -661,19 +443,9 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I got 1\n", - "I have 2\n", - "1\n" - ] - } - ], + "outputs": [], "source": [ "def myFunction(n):\n", " print(\"I got\", n)\n", @@ -687,19 +459,25 @@ "#changing the parameter's value doesn't propagate outside the function" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Some Function Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## BMI" + ] + }, { "cell_type": "code", - "execution_count": 85, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "19.283746556473833\n" - ] - } - ], + "outputs": [], "source": [ "def bmi(weight, height):\n", " return weight / height ** 2\n", @@ -707,6 +485,832 @@ "print(bmi(52.5, 1.65))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def bmi(weight, height):\n", + " if height < 1.0 or height > 2.5 or \\\n", + " weight < 20 or weight > 200:\n", + " return None\n", + " \n", + " return weight / height ** 2\n", + "\n", + "print(bmi(352.5, 1.65))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Segitiga" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def isItATriangle(a, b, c):\n", + " if a + b <= c:\n", + " return False\n", + " if b + c <= a:\n", + " return False\n", + " if c + a <= b:\n", + " return False\n", + " return True\n", + "\n", + "print(isItATriangle(1, 1, 1))\n", + "print(isItATriangle(1, 1, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def isItATriangle(a, b, c):\n", + " return a + b > c and b + c > a and c + a > b\n", + "\n", + "a = float(input(\"Enter the first side's length: \"))\n", + "b = float(input(\"Enter the second side's length: \"))\n", + "c = float(input(\"Enter the third side's length: \"))\n", + "\n", + "if isItATriangle(a, b, c):\n", + " print(\"Congratulations - it can be a triangle.\")\n", + "else:\n", + " print(\"Sorry, it won't be a triangle.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Faktorial" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def factorialFun(n):\n", + " if n < 0:\n", + " return None\n", + " if n < 2:\n", + " return 1\n", + " \n", + " product = 1\n", + " for i in range(2, n + 1):\n", + " product *= i\n", + " return product\n", + "\n", + "for n in range(1, 6): # testing\n", + " print(n, factorialFun(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Fibonacci" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fib(n):\n", + " if n < 1:\n", + " return None\n", + " if n < 3:\n", + " return 1\n", + "\n", + " elem1 = elem2 = 1\n", + " sum = 0\n", + " for i in range(3, n + 1):\n", + " sum = elem1 + elem2\n", + " elem1, elem2 = elem2, sum\n", + " return sum\n", + "\n", + "for n in range(1, 10): # testing\n", + " print(n, \"->\", fib(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Recursion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fib(n):\n", + " if n < 1:\n", + " return None\n", + " if n < 3:\n", + " return 1\n", + " return fib(n - 1) + fib(n - 2)\n", + "\n", + "for n in range(1, 10): # testing\n", + " print(n, \"->\", fib(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 4.1.6 Tuples and Dictionaries" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "List datanya bisa dirubah selama eksekusi program, sifatnya bernama mutable\n", + "Sebaliknya tuple tidak bisa, sifatnya dinamakan immutable. Persamaannya keduanya sama-sama bisa menyimpan banyak nilai sekaligus dalam satu tipe data (sequence type)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tuples\n", + "Tuples are ordered and unchangeable (immutable) collections of data. They can be thought of as immutable lists. They are written in round brackets:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 2, True, \"a string\", (3, 4), [5, 6], None)\n", + "print(myTuple)\n", + "\n", + "myList = [1, 2, True, \"a string\", (3, 4), [5, 6], None]\n", + "print(myList)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tuple1 = (1, 2, 4, 8)\n", + "tuple2 = 1., .5, .25, .125\n", + "\n", + "print(tuple1)\n", + "print(tuple2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "emptyTuple = ()\n", + "print(type(emptyTuple))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 10, 100, 1000)\n", + "\n", + "print(myTuple[0])\n", + "print(myTuple[-1])\n", + "print(myTuple[1:])\n", + "print(myTuple[:-2])\n", + "\n", + "for elem in myTuple:\n", + " print(elem)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 10, 100, 1000)\n", + "\n", + "myTuple.append(10000)\n", + "del myTuple[0]\n", + "myTuple[1] = -10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 10, 100)\n", + "\n", + "t1 = myTuple + (1000, 10000)\n", + "t2 = myTuple * 3\n", + "\n", + "print(len(t2))\n", + "print(t1)\n", + "print(t2)\n", + "print(10 in myTuple)\n", + "print(-10 not in myTuple)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Example 1\n", + "t1 = (1, 2, 3)\n", + "for elem in t1:\n", + " print(elem)\n", + "\n", + "# Example 2\n", + "t2 = (1, 2, 3, 4)\n", + "print(5 in t2)\n", + "print(5 not in t2)\n", + "\n", + "# Example 3\n", + "t3 = (1, 2, 3, 5)\n", + "print(len(t3))\n", + "\n", + "# Example 4\n", + "t4 = t1 + t2\n", + "t5 = t3 * 2\n", + "\n", + "print(t4)\n", + "print(t5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dictionaries" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python's world, the word you look for is named a key. The word you get from the dictionary is called a value.\n", + "\n", + "Dictionaries are unordered*, changeable (mutable), and indexed collections of data. (*In Python 3.6x dictionaries have become ordered by default.\n", + "\n", + "This means that a dictionary is a set of key-value pairs. Note:\n", + "\n", + "- each key must be unique - it's not possible to have more than one key of the same value;\n", + "- a key may be data of any type: it may be a number (integer or float), or even a string;\n", + "- a dictionary is not a list - a list contains a set of numbered values, while a dictionary holds pairs of values;\n", + "- the len() function works for dictionaries, too - it returns the numbers of key-value elements in the dictionary;\n", + "- a dictionary is a one-way tool - if you have an English-French dictionary, you can look for French equivalents of English terms, but not vice versa." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "phoneNumbers = {'boss' : 5551234567, 'Suzy' : 22657854310}\n", + "emptyDictionary = {}\n", + "\n", + "print(dict)\n", + "print(phoneNumbers)\n", + "print(emptyDictionary)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(dict['cat'])\n", + "print(phoneNumbers['Suzy'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "words = ['cat', 'lion', 'horse']\n", + "\n", + "for word in words:\n", + " if word in dict:\n", + " print(word, \"->\", dict[word])\n", + " else:\n", + " print(word, \"is not in dictionary\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "for key in dict.keys():\n", + " print(key, \"->\", dict[key])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for key in sorted(dict.keys()):\n", + " print(key, \"->\", dict[key])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### items and values" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "for english, french in dict.items():\n", + " print(english, \"->\", french)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "for french in dict.values():\n", + " print(french)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Replace" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "dict['cat'] = 'minou'\n", + "print(dict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Adding new key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "dict['swan'] = 'cygne'\n", + "print(dict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tuples and Dictionaries work Together\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- line 1: create an empty dictionary for the input data; the student's name is used as a key, while all the associated scores are stored in a tuple (the tuple may be a dictionary value - that's not a problem at all)\n", + "- line 3: enter an \"infinite\" loop (don't worry, it'll break at the right moment)\n", + "- line 4: read the student's name here;\n", + "- line 5-6: if the name is exit, leave the loop;\n", + "- line 8: ask for one of the student's scores (an integer from the range 1-10)\n", + "- line 10-11: if the student's name is already in the dictionary, lengthen the associated tuple with the new score (note the += operator)\n", + "- line 12-13: if this is a new student (unknown to the dictionary), create a new entry - its value is a one-element tuple containing the entered score;\n", + "- line 15: iterate through the sorted students' names;\n", + "- line 16-17: initialize the data needed to evaluate the average (sum and counter)\n", + "- line 18-20: we iterate through the tuple, taking all the subsequent scores and updating the sum, together with the counter;\n", + "- line 21: evaluate and print the student's name and average score." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "schoolClass = {}\n", + "\n", + "while True:\n", + " name = input(\"Enter the student's name (or type exit to stop): \")\n", + " if name == 'exit':\n", + " break\n", + " \n", + " score = int(input(\"Enter the student's score (0-10): \"))\n", + " \n", + " if name in schoolClass:\n", + " schoolClass[name] += (score,)\n", + " else:\n", + " schoolClass[name] = (score,)\n", + " \n", + "for name in sorted(schoolClass.keys()):\n", + " sum = 0\n", + " counter = 0\n", + " for score in schoolClass[name]:\n", + " sum += score\n", + " counter += 1\n", + " print(name, \":\", sum / counter)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Summary\n", + "\n", + "1. Dictionaries are unordered*, changeable (mutable), and indexed collections of data. (*In Python 3.6x dictionaries have become ordered by default.\n", + "\n", + "Each dictionary is a set of key : value pairs. You can create it by using the following syntax:\n", + "\n", + "```python \n", + "myDictionary = {\n", + " key1 : value1,\n", + " key2 : value2,\n", + " key3 : value3,\n", + " }\n", + "```\n", + "2. If you want to access a dictionary item, you can do so by making a reference to its key inside a pair of square brackets (ex. 1) or by using the get() method (ex. 2):\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"kwiat\" : \"flower\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "item1 = polEngDict[\"gleba\"] # ex. 1\n", + "print(item1) # outputs: soil\n", + "\n", + "item2 = polEngDict.get(\"woda\")\n", + "print(item2) # outputs: water\n", + "```\n", + "3. If you want to change the value associated with a specific key, you can do so by referring to the item's key name in the following way:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "polEngDict[\"zamek\"] = \"lock\"\n", + "item = polEngDict[\"zamek\"] # outputs: lock\n", + "```\n", + "4. To add or remove a key (and the associated value), use the following syntax:\n", + "\n", + "```python \n", + "myPhonebook = {} # an empty dictionary\n", + "\n", + "myPhonebook[\"Adam\"] = 3456783958 # create/add a key-value pair\n", + "print(myPhonebook) # outputs: {'Adam': 3456783958}\n", + "\n", + "del myPhonebook[\"Adam\"]\n", + "print(myPhonebook) # outputs: {}\n", + "```\n", + "\n", + "You can also insert an item to a dictionary by using the update() method, and remove the last element by using the popitem() method, e.g.:\n", + "\n", + "```python \n", + "polEngDict = {\"kwiat\" : \"flower\"}\n", + "\n", + "polEngDict = update(\"gleba\" : \"soil\")\n", + "print(polEngDict) # outputs: {'kwiat' : 'flower', 'gleba' : 'soil'}\n", + "\n", + "polEngDict.popitem()\n", + "print(polEngDict) # outputs: {'kwiat' : 'flower'}\n", + "```\n", + "\n", + "5. You can use the for loop to loop through a dictionary, e.g.:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "for item in polEngDict:\n", + " print(item) # outputs: zamek\n", + " # woda\n", + " # gleba\n", + "\n", + "```\n", + "\n", + "\n", + "\n", + "6. If you want to loop through a dictionary's keys and values, you can use the items() method, e.g.:\n", + "\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "for key, value in polEngDict.items():\n", + " print(\"Pol/Eng ->\", key, \":\", value)\n", + "```\n", + "\n", + "\n", + "7. To check if a given key exists in a dictionary, you can use the in keyword:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "if \"zamek\" in polEngDict:\n", + " print(\"Yes\")\n", + "else:\n", + " print(\"No\")\n", + "```\n", + "\n", + "8. You can use the del keyword to remove a specific item, or delete a dictionary. To remove all the dictionary's items, you need to use the clear() method:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "print(len(polEngDict)) # outputs: 3\n", + "del polEngDict[\"zamek\"] # remove an item\n", + "print(len(polEngDict)) # outputs: 2\n", + "\n", + "polEngDict.clear() # removes all the items\n", + "print(len(polEngDict)) # outputs: 0\n", + "\n", + "del polEngDict # removes the dictionary\n", + "```\n", + "\n", + "9. To copy a dictionary, use the copy() method:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "copyDict = polEngDict.copy()\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Project - Tic Tac Toe\n", + "\n", + "Scenario\n", + "Your task is to write a simple program which pretends to play tic-tac-toe with the user. To make it all easier for you, we've decided to simplify the game. Here are our assumptions:\n", + "\n", + "- the computer (i.e., your program) should play the game using 'X's;\n", + "- the user (e.g., you) should play the game using 'O's;\n", + "- the first move belongs to the computer - it always puts its first 'X' in the middle of the board;\n", + "- all the squares are numbered row by row starting with 1 (see the example session below for reference)\n", + "- the user inputs their move by entering the number of the square they choose - the number must be valid, i.e., it must be an integer, it must be greater than 0 and less than 10, and it cannot point to a field which is already occupied;\n", + "- the program checks if the game is over - there are four possible verdicts: the game should continue, or the game ends with a tie, your win, or the computer's win;\n", + "- the computer responds with its move and the check is repeated;\n", + "- don't implement any form of artificial intelligence - a random field choice made by the computer is good enough for the game.\n", + "- The example session with the program may look as follows:\n", + "\n", + "```\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 1 | 2 | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | 8 | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 1\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | 2 | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | 8 | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | 8 | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 8\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 4\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 7\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "You won!\n", + "```\n", + "\n", + "Requirements\n", + "Implement the following features:\n", + "\n", + "- the board should be stored as a three-element list, while each element is another three-element list (the inner lists represent rows) so that all of the squares may be accessed using the following syntax: \n", + "```\n", + "board[row][column]\n", + "```\n", + "- each of the inner list's elements can contain 'O', 'X', or a digit representing the square's number (such a square is considered free)\n", + "- the board's appearance should be exactly the same as the one presented in the example.\n", + "- implement the functions defined for you in the editor.\n", + "\n", + "Drawing a random integer number can be done by utilizing a Python function called randrange(). The example program below shows how to use it (the program prints ten random numbers from 0 to 8).\n", + "\n", + "Note: the from-import instruction provides an access to the randrange function defined within an external Python module callled random.\n", + "\n", + "```python\n", + "from random import randrange\n", + "\n", + "for i in range(10):\n", + " print(randrange(8))\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def DisplayBoard(board):\n", + "#\n", + "# the function accepts one parameter containing the board's current status\n", + "# and prints it out to the console\n", + "#\n", + "\n", + "def EnterMove(board):\n", + "#\n", + "# the function accepts the board current status, asks the user about their move, \n", + "# checks the input and updates the board according to the user's decision\n", + "#\n", + "\n", + "def MakeListOfFreeFields(board):\n", + "#\n", + "# the function browses the board and builds a list of all the free squares; \n", + "# the list consists of tuples, while each tuple is a pair of row and column numbers\n", + "#\n", + "\n", + "def VictoryFor(board, sign):\n", + "#\n", + "# the function analyzes the board status in order to check if \n", + "# the player using 'O's or 'X's has won the game\n", + "#\n", + "\n", + "def DrawMove(board):\n", + "#\n", + "# the function draws the computer's move and updates the board\n", + "#" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/2019/.ipynb_checkpoints/Modul 6 - Object Oriented Approach, Exception Handling, Working with Files-checkpoint.ipynb b/2019/.ipynb_checkpoints/Modul 6 - Object Oriented Approach, Exception Handling, Working with Files-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/2019/.ipynb_checkpoints/Modul 6 - Object Oriented Approach, Exception Handling, Working with Files-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/2019/Modul 2 - Tipe Data, Variabel, Dasar Input Output, Operasi.ipynb b/2019/Modul 2 - Tipe Data, Variabel, Dasar Input Output, Operasi.ipynb index 0c6459b..9e34bb8 100644 --- a/2019/Modul 2 - Tipe Data, Variabel, Dasar Input Output, Operasi.ipynb +++ b/2019/Modul 2 - Tipe Data, Variabel, Dasar Input Output, Operasi.ipynb @@ -1181,7 +1181,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/2019/Modul 3 - Operator kondisi, perulangan, list dan operasinya, bitwise, boolean.ipynb b/2019/Modul 3 - Operator kondisi, perulangan, list dan operasinya, bitwise, boolean.ipynb index 6865788..a8c3df4 100644 --- a/2019/Modul 3 - Operator kondisi, perulangan, list dan operasinya, bitwise, boolean.ipynb +++ b/2019/Modul 3 - Operator kondisi, perulangan, list dan operasinya, bitwise, boolean.ipynb @@ -2009,7 +2009,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.6.7" } }, "nbformat": 4, diff --git a/2019/Modul 4 - Function Tuple Dictionaries and Data Processing.ipynb b/2019/Modul 4 - Function Tuple Dictionaries and Data Processing.ipynb index 20dd539..16771da 100644 --- a/2019/Modul 4 - Function Tuple Dictionaries and Data Processing.ipynb +++ b/2019/Modul 4 - Function Tuple Dictionaries and Data Processing.ipynb @@ -24,25 +24,9 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Please, Enter a value: \n", - "3\n", - "3\n", - "Please, Enter a value: \n", - "3\n", - "3\n", - "Please, Enter a value: \n", - "4\n", - "4\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Motivasi function adalah perulangan kode\n", "\n", @@ -75,27 +59,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Dimulai dari sini\n", - "Please, Enter a value: \n", - "100\n", - "100\n", - "Please, Enter a value: \n", - "100\n", - "100\n", - "Please, Enter a value: \n", - "100\n", - "100\n", - "Berhenti disini\n" - ] - } - ], + "outputs": [], "source": [ "def message():\n", " print(\"Please, Enter a value: \")\n", @@ -118,18 +84,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "pesan: HELLO\n", - "pesan: HELLO JUGA\n" - ] - } - ], + "outputs": [], "source": [ "def message(m):\n", " print(\"pesan: \",m.upper())\n", @@ -140,19 +97,9 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Enter telephone number 11\n", - "Enter price number 5\n", - "Enter number number number\n" - ] - } - ], + "outputs": [], "source": [ "def message(what, number):\n", " print(\"Enter\", what, \"number\", number)\n", @@ -164,17 +111,9 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Guntur 5.6\n" - ] - } - ], + "outputs": [], "source": [ "def tambahUmur(nama, umur):\n", " umur = umur + 0.5\n", @@ -193,20 +132,9 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is Luke Skywalker\n", - "Hello, my name is Skywalker Luke\n", - "Hello, my name is Jesse Quick\n", - "Hello, my name is Clark Kent\n" - ] - } - ], + "outputs": [], "source": [ "def introduction(firstName, lastName):\n", " print(\"Hello, my name is\", firstName, lastName)\n", @@ -219,18 +147,9 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is James Bond\n", - "Hello, my name is Luke Skywalker\n" - ] - } - ], + "outputs": [], "source": [ "introduction(firstName = \"James\", lastName = \"Bond\")\n", "introduction(lastName = \"Skywalker\", firstName = \"Luke\")" @@ -238,17 +157,9 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 + 6 + 4 = 15\n" - ] - } - ], + "outputs": [], "source": [ "def jumlah(a, b, c):\n", " print(a, \"+\", b, \"+\", c, \"=\", a + b + c)\n", @@ -266,17 +177,9 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is Guntur Smith\n" - ] - } - ], + "outputs": [], "source": [ "#All required parameters must be placed before any default arguments.\n", "\n", @@ -289,17 +192,9 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Hello, my name is James Guntur Bond\n" - ] - } - ], + "outputs": [], "source": [ "def introductionLengkap(firstName, lastName, middleName=\"Guntur\"):\n", " print(\"Hello, my name is\", firstName, middleName, lastName)\n", @@ -316,17 +211,9 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The boringFunction has returned its result. It's: 123\n" - ] - } - ], + "outputs": [], "source": [ "def boringFuncton():\n", " return 123\n", @@ -338,19 +225,9 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "This lesson is interesting!\n", - "'Boredom Mode' ON.\n", - "This lesson is boring...\n" - ] - } - ], + "outputs": [], "source": [ "def boringFunction():\n", " print(\"'Boredom Mode' ON.\")\n", @@ -363,17 +240,9 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7\n" - ] - } - ], + "outputs": [], "source": [ "def jumlah(a,b):\n", " c = a + b\n", @@ -385,17 +254,9 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sorry, you don't carry any value\n" - ] - } - ], + "outputs": [], "source": [ "value = None\n", "if value == None:\n", @@ -404,18 +265,9 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "None\n" - ] - } - ], + "outputs": [], "source": [ "def strangeFunction(n):\n", " if(n % 2 == 0):\n", @@ -427,20 +279,9 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "47" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "def sumOfList(lst):\n", " sum = 0\n", @@ -455,27 +296,9 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Insert 0\n", - "[0]\n", - "Insert 1\n", - "[1, 0]\n", - "Insert 2\n", - "[2, 1, 0]\n", - "Insert 3\n", - "[3, 2, 1, 0]\n", - "Insert 4\n", - "[4, 3, 2, 1, 0]\n", - "Hasil = [4, 3, 2, 1, 0]\n" - ] - } - ], + "outputs": [], "source": [ "def strangeListFunction(n):\n", " strangeList = []\n", @@ -526,20 +349,9 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1900 ->OK\n", - "2000 ->OK\n", - "2016 ->OK\n", - "1987 ->OK\n" - ] - } - ], + "outputs": [], "source": [ "def isYearLeap(year):\n", " if (year % 4) == 0:\n", @@ -581,21 +393,9 @@ }, { "cell_type": "code", - "execution_count": 81, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'YY' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mscopeTest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mYY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mNameError\u001b[0m: name 'YY' is not defined" - ] - } - ], + "outputs": [], "source": [ "def scopeTest():\n", " YY = 123\n", @@ -606,18 +406,9 @@ }, { "cell_type": "code", - "execution_count": 82, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Do I know that variable? 1\n", - "1\n" - ] - } - ], + "outputs": [], "source": [ "def myFunction():\n", " print(\"Do I know that variable?\", var)\n", @@ -632,18 +423,9 @@ }, { "cell_type": "code", - "execution_count": 84, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Do I know that variable? 2\n", - "2\n" - ] - } - ], + "outputs": [], "source": [ "def myFunction():\n", " global var\n", @@ -661,19 +443,9 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "I got 1\n", - "I have 2\n", - "1\n" - ] - } - ], + "outputs": [], "source": [ "def myFunction(n):\n", " print(\"I got\", n)\n", @@ -687,19 +459,25 @@ "#changing the parameter's value doesn't propagate outside the function" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Some Function Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## BMI" + ] + }, { "cell_type": "code", - "execution_count": 85, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "19.283746556473833\n" - ] - } - ], + "outputs": [], "source": [ "def bmi(weight, height):\n", " return weight / height ** 2\n", @@ -707,6 +485,832 @@ "print(bmi(52.5, 1.65))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def bmi(weight, height):\n", + " if height < 1.0 or height > 2.5 or \\\n", + " weight < 20 or weight > 200:\n", + " return None\n", + " \n", + " return weight / height ** 2\n", + "\n", + "print(bmi(352.5, 1.65))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Segitiga" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def isItATriangle(a, b, c):\n", + " if a + b <= c:\n", + " return False\n", + " if b + c <= a:\n", + " return False\n", + " if c + a <= b:\n", + " return False\n", + " return True\n", + "\n", + "print(isItATriangle(1, 1, 1))\n", + "print(isItATriangle(1, 1, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def isItATriangle(a, b, c):\n", + " return a + b > c and b + c > a and c + a > b\n", + "\n", + "a = float(input(\"Enter the first side's length: \"))\n", + "b = float(input(\"Enter the second side's length: \"))\n", + "c = float(input(\"Enter the third side's length: \"))\n", + "\n", + "if isItATriangle(a, b, c):\n", + " print(\"Congratulations - it can be a triangle.\")\n", + "else:\n", + " print(\"Sorry, it won't be a triangle.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Faktorial" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def factorialFun(n):\n", + " if n < 0:\n", + " return None\n", + " if n < 2:\n", + " return 1\n", + " \n", + " product = 1\n", + " for i in range(2, n + 1):\n", + " product *= i\n", + " return product\n", + "\n", + "for n in range(1, 6): # testing\n", + " print(n, factorialFun(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Fibonacci" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fib(n):\n", + " if n < 1:\n", + " return None\n", + " if n < 3:\n", + " return 1\n", + "\n", + " elem1 = elem2 = 1\n", + " sum = 0\n", + " for i in range(3, n + 1):\n", + " sum = elem1 + elem2\n", + " elem1, elem2 = elem2, sum\n", + " return sum\n", + "\n", + "for n in range(1, 10): # testing\n", + " print(n, \"->\", fib(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Recursion" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fib(n):\n", + " if n < 1:\n", + " return None\n", + " if n < 3:\n", + " return 1\n", + " return fib(n - 1) + fib(n - 2)\n", + "\n", + "for n in range(1, 10): # testing\n", + " print(n, \"->\", fib(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 4.1.6 Tuples and Dictionaries" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "List datanya bisa dirubah selama eksekusi program, sifatnya bernama mutable\n", + "Sebaliknya tuple tidak bisa, sifatnya dinamakan immutable. Persamaannya keduanya sama-sama bisa menyimpan banyak nilai sekaligus dalam satu tipe data (sequence type)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tuples\n", + "Tuples are ordered and unchangeable (immutable) collections of data. They can be thought of as immutable lists. They are written in round brackets:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 2, True, \"a string\", (3, 4), [5, 6], None)\n", + "print(myTuple)\n", + "\n", + "myList = [1, 2, True, \"a string\", (3, 4), [5, 6], None]\n", + "print(myList)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tuple1 = (1, 2, 4, 8)\n", + "tuple2 = 1., .5, .25, .125\n", + "\n", + "print(tuple1)\n", + "print(tuple2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "emptyTuple = ()\n", + "print(type(emptyTuple))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 10, 100, 1000)\n", + "\n", + "print(myTuple[0])\n", + "print(myTuple[-1])\n", + "print(myTuple[1:])\n", + "print(myTuple[:-2])\n", + "\n", + "for elem in myTuple:\n", + " print(elem)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 10, 100, 1000)\n", + "\n", + "myTuple.append(10000)\n", + "del myTuple[0]\n", + "myTuple[1] = -10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "myTuple = (1, 10, 100)\n", + "\n", + "t1 = myTuple + (1000, 10000)\n", + "t2 = myTuple * 3\n", + "\n", + "print(len(t2))\n", + "print(t1)\n", + "print(t2)\n", + "print(10 in myTuple)\n", + "print(-10 not in myTuple)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Example 1\n", + "t1 = (1, 2, 3)\n", + "for elem in t1:\n", + " print(elem)\n", + "\n", + "# Example 2\n", + "t2 = (1, 2, 3, 4)\n", + "print(5 in t2)\n", + "print(5 not in t2)\n", + "\n", + "# Example 3\n", + "t3 = (1, 2, 3, 5)\n", + "print(len(t3))\n", + "\n", + "# Example 4\n", + "t4 = t1 + t2\n", + "t5 = t3 * 2\n", + "\n", + "print(t4)\n", + "print(t5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dictionaries" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In Python's world, the word you look for is named a key. The word you get from the dictionary is called a value.\n", + "\n", + "Dictionaries are unordered*, changeable (mutable), and indexed collections of data. (*In Python 3.6x dictionaries have become ordered by default.\n", + "\n", + "This means that a dictionary is a set of key-value pairs. Note:\n", + "\n", + "- each key must be unique - it's not possible to have more than one key of the same value;\n", + "- a key may be data of any type: it may be a number (integer or float), or even a string;\n", + "- a dictionary is not a list - a list contains a set of numbered values, while a dictionary holds pairs of values;\n", + "- the len() function works for dictionaries, too - it returns the numbers of key-value elements in the dictionary;\n", + "- a dictionary is a one-way tool - if you have an English-French dictionary, you can look for French equivalents of English terms, but not vice versa." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "phoneNumbers = {'boss' : 5551234567, 'Suzy' : 22657854310}\n", + "emptyDictionary = {}\n", + "\n", + "print(dict)\n", + "print(phoneNumbers)\n", + "print(emptyDictionary)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(dict['cat'])\n", + "print(phoneNumbers['Suzy'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "words = ['cat', 'lion', 'horse']\n", + "\n", + "for word in words:\n", + " if word in dict:\n", + " print(word, \"->\", dict[word])\n", + " else:\n", + " print(word, \"is not in dictionary\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "for key in dict.keys():\n", + " print(key, \"->\", dict[key])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for key in sorted(dict.keys()):\n", + " print(key, \"->\", dict[key])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### items and values" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "for english, french in dict.items():\n", + " print(english, \"->\", french)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "for french in dict.values():\n", + " print(french)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Replace" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "dict['cat'] = 'minou'\n", + "print(dict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Adding new key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dict = {\"cat\" : \"chat\", \"dog\" : \"chien\", \"horse\" : \"cheval\"}\n", + "\n", + "dict['swan'] = 'cygne'\n", + "print(dict)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tuples and Dictionaries work Together\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- line 1: create an empty dictionary for the input data; the student's name is used as a key, while all the associated scores are stored in a tuple (the tuple may be a dictionary value - that's not a problem at all)\n", + "- line 3: enter an \"infinite\" loop (don't worry, it'll break at the right moment)\n", + "- line 4: read the student's name here;\n", + "- line 5-6: if the name is exit, leave the loop;\n", + "- line 8: ask for one of the student's scores (an integer from the range 1-10)\n", + "- line 10-11: if the student's name is already in the dictionary, lengthen the associated tuple with the new score (note the += operator)\n", + "- line 12-13: if this is a new student (unknown to the dictionary), create a new entry - its value is a one-element tuple containing the entered score;\n", + "- line 15: iterate through the sorted students' names;\n", + "- line 16-17: initialize the data needed to evaluate the average (sum and counter)\n", + "- line 18-20: we iterate through the tuple, taking all the subsequent scores and updating the sum, together with the counter;\n", + "- line 21: evaluate and print the student's name and average score." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "schoolClass = {}\n", + "\n", + "while True:\n", + " name = input(\"Enter the student's name (or type exit to stop): \")\n", + " if name == 'exit':\n", + " break\n", + " \n", + " score = int(input(\"Enter the student's score (0-10): \"))\n", + " \n", + " if name in schoolClass:\n", + " schoolClass[name] += (score,)\n", + " else:\n", + " schoolClass[name] = (score,)\n", + " \n", + "for name in sorted(schoolClass.keys()):\n", + " sum = 0\n", + " counter = 0\n", + " for score in schoolClass[name]:\n", + " sum += score\n", + " counter += 1\n", + " print(name, \":\", sum / counter)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Summary\n", + "\n", + "1. Dictionaries are unordered*, changeable (mutable), and indexed collections of data. (*In Python 3.6x dictionaries have become ordered by default.\n", + "\n", + "Each dictionary is a set of key : value pairs. You can create it by using the following syntax:\n", + "\n", + "```python \n", + "myDictionary = {\n", + " key1 : value1,\n", + " key2 : value2,\n", + " key3 : value3,\n", + " }\n", + "```\n", + "2. If you want to access a dictionary item, you can do so by making a reference to its key inside a pair of square brackets (ex. 1) or by using the get() method (ex. 2):\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"kwiat\" : \"flower\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "item1 = polEngDict[\"gleba\"] # ex. 1\n", + "print(item1) # outputs: soil\n", + "\n", + "item2 = polEngDict.get(\"woda\")\n", + "print(item2) # outputs: water\n", + "```\n", + "3. If you want to change the value associated with a specific key, you can do so by referring to the item's key name in the following way:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "polEngDict[\"zamek\"] = \"lock\"\n", + "item = polEngDict[\"zamek\"] # outputs: lock\n", + "```\n", + "4. To add or remove a key (and the associated value), use the following syntax:\n", + "\n", + "```python \n", + "myPhonebook = {} # an empty dictionary\n", + "\n", + "myPhonebook[\"Adam\"] = 3456783958 # create/add a key-value pair\n", + "print(myPhonebook) # outputs: {'Adam': 3456783958}\n", + "\n", + "del myPhonebook[\"Adam\"]\n", + "print(myPhonebook) # outputs: {}\n", + "```\n", + "\n", + "You can also insert an item to a dictionary by using the update() method, and remove the last element by using the popitem() method, e.g.:\n", + "\n", + "```python \n", + "polEngDict = {\"kwiat\" : \"flower\"}\n", + "\n", + "polEngDict = update(\"gleba\" : \"soil\")\n", + "print(polEngDict) # outputs: {'kwiat' : 'flower', 'gleba' : 'soil'}\n", + "\n", + "polEngDict.popitem()\n", + "print(polEngDict) # outputs: {'kwiat' : 'flower'}\n", + "```\n", + "\n", + "5. You can use the for loop to loop through a dictionary, e.g.:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "for item in polEngDict:\n", + " print(item) # outputs: zamek\n", + " # woda\n", + " # gleba\n", + "\n", + "```\n", + "\n", + "\n", + "\n", + "6. If you want to loop through a dictionary's keys and values, you can use the items() method, e.g.:\n", + "\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "for key, value in polEngDict.items():\n", + " print(\"Pol/Eng ->\", key, \":\", value)\n", + "```\n", + "\n", + "\n", + "7. To check if a given key exists in a dictionary, you can use the in keyword:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "if \"zamek\" in polEngDict:\n", + " print(\"Yes\")\n", + "else:\n", + " print(\"No\")\n", + "```\n", + "\n", + "8. You can use the del keyword to remove a specific item, or delete a dictionary. To remove all the dictionary's items, you need to use the clear() method:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "print(len(polEngDict)) # outputs: 3\n", + "del polEngDict[\"zamek\"] # remove an item\n", + "print(len(polEngDict)) # outputs: 2\n", + "\n", + "polEngDict.clear() # removes all the items\n", + "print(len(polEngDict)) # outputs: 0\n", + "\n", + "del polEngDict # removes the dictionary\n", + "```\n", + "\n", + "9. To copy a dictionary, use the copy() method:\n", + "\n", + "```python \n", + "polEngDict = {\n", + " \"zamek\" : \"castle\",\n", + " \"woda\" : \"water\",\n", + " \"gleba\" : \"soil\"\n", + " }\n", + "\n", + "copyDict = polEngDict.copy()\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Project - Tic Tac Toe\n", + "\n", + "Scenario\n", + "Your task is to write a simple program which pretends to play tic-tac-toe with the user. To make it all easier for you, we've decided to simplify the game. Here are our assumptions:\n", + "\n", + "- the computer (i.e., your program) should play the game using 'X's;\n", + "- the user (e.g., you) should play the game using 'O's;\n", + "- the first move belongs to the computer - it always puts its first 'X' in the middle of the board;\n", + "- all the squares are numbered row by row starting with 1 (see the example session below for reference)\n", + "- the user inputs their move by entering the number of the square they choose - the number must be valid, i.e., it must be an integer, it must be greater than 0 and less than 10, and it cannot point to a field which is already occupied;\n", + "- the program checks if the game is over - there are four possible verdicts: the game should continue, or the game ends with a tie, your win, or the computer's win;\n", + "- the computer responds with its move and the check is repeated;\n", + "- don't implement any form of artificial intelligence - a random field choice made by the computer is good enough for the game.\n", + "- The example session with the program may look as follows:\n", + "\n", + "```\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 1 | 2 | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | 8 | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 1\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | 2 | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | 8 | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | 8 | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 8\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | 6 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 4 | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 4\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | 3 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| 7 | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "Enter your move: 7\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | X | X |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "| | | |\n", + "| O | O | 9 |\n", + "| | | |\n", + "+-------+-------+-------+\n", + "You won!\n", + "```\n", + "\n", + "Requirements\n", + "Implement the following features:\n", + "\n", + "- the board should be stored as a three-element list, while each element is another three-element list (the inner lists represent rows) so that all of the squares may be accessed using the following syntax: \n", + "```\n", + "board[row][column]\n", + "```\n", + "- each of the inner list's elements can contain 'O', 'X', or a digit representing the square's number (such a square is considered free)\n", + "- the board's appearance should be exactly the same as the one presented in the example.\n", + "- implement the functions defined for you in the editor.\n", + "\n", + "Drawing a random integer number can be done by utilizing a Python function called randrange(). The example program below shows how to use it (the program prints ten random numbers from 0 to 8).\n", + "\n", + "Note: the from-import instruction provides an access to the randrange function defined within an external Python module callled random.\n", + "\n", + "```python\n", + "from random import randrange\n", + "\n", + "for i in range(10):\n", + " print(randrange(8))\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def DisplayBoard(board):\n", + "#\n", + "# the function accepts one parameter containing the board's current status\n", + "# and prints it out to the console\n", + "#\n", + "\n", + "def EnterMove(board):\n", + "#\n", + "# the function accepts the board current status, asks the user about their move, \n", + "# checks the input and updates the board according to the user's decision\n", + "#\n", + "\n", + "def MakeListOfFreeFields(board):\n", + "#\n", + "# the function browses the board and builds a list of all the free squares; \n", + "# the list consists of tuples, while each tuple is a pair of row and column numbers\n", + "#\n", + "\n", + "def VictoryFor(board, sign):\n", + "#\n", + "# the function analyzes the board status in order to check if \n", + "# the player using 'O's or 'X's has won the game\n", + "#\n", + "\n", + "def DrawMove(board):\n", + "#\n", + "# the function draws the computer's move and updates the board\n", + "#" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/2019/Modul 6 - Object Oriented Approach, Exception Handling, Working with Files.ipynb b/2019/Modul 6 - Object Oriented Approach, Exception Handling, Working with Files.ipynb new file mode 100644 index 0000000..e719049 --- /dev/null +++ b/2019/Modul 6 - Object Oriented Approach, Exception Handling, Working with Files.ipynb @@ -0,0 +1,32 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/2019/images/stack.PNG b/2019/images/stack.PNG new file mode 100644 index 0000000..40e8d06 Binary files /dev/null and b/2019/images/stack.PNG differ diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..fb03a33 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,87 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "error", + "evalue": "OpenCV(3.4.1) C:\\Miniconda3\\conda-bld\\opencv-suite_1533128839831\\work\\modules\\imgproc\\src\\color.cpp:11147: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor\n", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31merror\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[0mret\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mframe\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvideo_capture\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[0mgray\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mframe\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mCOLOR_BGR2GRAY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m faces = faceCascade.detectMultiScale(\n", + "\u001b[1;31merror\u001b[0m: OpenCV(3.4.1) C:\\Miniconda3\\conda-bld\\opencv-suite_1533128839831\\work\\modules\\imgproc\\src\\color.cpp:11147: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor\n" + ] + } + ], + "source": [ + "import cv2\n", + "import sys\n", + "\n", + "cascPath = sys.argv[1]\n", + "faceCascade = cv2.CascadeClassifier(cascPath)\n", + "\n", + "video_capture = cv2.VideoCapture(0)\n", + "\n", + "while True:\n", + " # Capture frame-by-frame\n", + " ret, frame = video_capture.read()\n", + "\n", + " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", + "\n", + " faces = faceCascade.detectMultiScale(\n", + " gray,\n", + " scaleFactor=1.1,\n", + " minNeighbors=5,\n", + " minSize=(30, 30),\n", + " flags=cv2.CASCADE_SCALE_IMAGE\n", + " )\n", + "\n", + " # Draw a rectangle around the faces\n", + " for (x, y, w, h) in faces:\n", + " cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", + "\n", + " # Display the resulting frame\n", + " cv2.imshow('Video', frame)\n", + "\n", + " if cv2.waitKey(1) & 0xFF == ord('q'):\n", + " break\n", + "\n", + "# When everything is done, release the capture\n", + "video_capture.release()\n", + "cv2.destroyAllWindows()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}