diff --git a/Kelas Ragnarok-1 Rabu 02 Maret.ipynb b/Kelas Ragnarok-1 Rabu 02 Maret.ipynb new file mode 100644 index 0000000..f183257 --- /dev/null +++ b/Kelas Ragnarok-1 Rabu 02 Maret.ipynb @@ -0,0 +1,1001 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 9, + "id": "d8d27fa8", + "metadata": {}, + "outputs": [], + "source": [ + "x3 = [1, 2, 3]\n", + "y3 = [1, 2, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "858b072c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(x3 == y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0c8ea0d6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print(x3 != y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e9a7b37b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print(x3 is y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "2cda1366", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(x3 is not y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "bd5f7df3", + "metadata": {}, + "outputs": [], + "source": [ + "x4 = []\n", + "y4 = []" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "c31fbf8a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print(x4 is y4)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f11476c1", + "metadata": {}, + "outputs": [], + "source": [ + "x4.append(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "eef6fd8c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([10], [])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x4, y4" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "721f1565", + "metadata": {}, + "outputs": [], + "source": [ + "y5 = x4" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "ebbc99cb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(y5 is x4)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "786c38b9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 50, 50] [10, 50, 50]\n" + ] + } + ], + "source": [ + "print(y5,x4)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "0ddb8353", + "metadata": {}, + "outputs": [], + "source": [ + "x4.append(50)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "82c69dfd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 50, 50, 50] [10, 50, 50, 50]\n" + ] + } + ], + "source": [ + "print(y5,x4)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "f4a78485", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "x = {1:'a', 2:'b','tiga': 3}\n", + "print(1 in x)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "545d6107", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(2 in x)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "7b5e9ca5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print('tiga' in x[2])" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "e3936de0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x['tiga']" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "99ad15f2", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "argument of type 'int' is not iterable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_7628/1235531082.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'a'\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'tiga'\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[0m", + "\u001b[1;31mTypeError\u001b[0m: argument of type 'int' is not iterable" + ] + } + ], + "source": [ + "print('a' in x['tiga'])" + ] + }, + { + "cell_type": "markdown", + "id": "d2ee685c", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "b5471d02", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tolong masukkan nama: dino\n" + ] + } + ], + "source": [ + "name = input(\"Tolong masukkan nama: \")" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "fa1b011e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dino\n" + ] + } + ], + "source": [ + "print(name)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "eff41e36", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Masukkan umur kamu ya: 23\n" + ] + } + ], + "source": [ + "umur = float(input(\"Masukkan umur kamu ya: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "74ac7923", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23.0 \n" + ] + } + ], + "source": [ + "print(umur, type(umur))" + ] + }, + { + "cell_type": "markdown", + "id": "9e4adc71", + "metadata": {}, + "source": [ + "## If else" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "id": "aa7d86b3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "B Lebih besar dari A\n" + ] + } + ], + "source": [ + "b = 30\n", + "a = 10\n", + "c = a + b #c = 10 + 10 = 20\n", + "\n", + "if(a == b and c > a):\n", + " print('A dan B sama atau C lebih besar dari A')\n", + "elif (a > b):\n", + " print('A lebih besar dari B')\n", + "elif(b > a):\n", + " print('B Lebih besar dari A')\n", + "else:\n", + " print('A dan B beda')" + ] + }, + { + "cell_type": "markdown", + "id": "ee84f802", + "metadata": {}, + "source": [ + "## For Loop" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "e3fe6fb5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n", + "Hai\n" + ] + } + ], + "source": [ + "for k in range(0,10):\n", + " print('Hai')" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "a09cedaf", + "metadata": {}, + "outputs": [], + "source": [ + "list1 = ['apel', 'banana', 'mango', 'star fruit', 'durian']" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "465ec2b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apel\n", + "banana\n", + "mango\n", + "star fruit\n", + "durian\n" + ] + } + ], + "source": [ + "for ele in list1 :\n", + " print(ele)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "5a3488bf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "4\n", + "5\n", + "11\n", + "24\n", + "13\n" + ] + } + ], + "source": [ + "list2 = [1,2,3,4,10,23,12]\n", + "\n", + "for ele in list2:\n", + " print(ele+1)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "9ab8e2eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Masukkan angka awal: 20\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n", + "Angka bertambah\n" + ] + } + ], + "source": [ + "angka = int(input(\"Masukkan angka awal: \"))\n", + "\n", + "while(angka<30):\n", + " print('Angka bertambah')\n", + " angka = angka+1\n" + ] + }, + { + "cell_type": "markdown", + "id": "ccf007ee", + "metadata": {}, + "source": [ + "## break, continue, and pass" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "a07d53bb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "list2 = [1,2,3,4,10,23,12]\n", + "\n", + "for ele in list2:\n", + " if(ele==10):\n", + " break\n", + " else:\n", + " print(ele)" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "f82de2ca", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "23\n", + "12\n" + ] + } + ], + "source": [ + "list2 = [1,2,3,4,10,23,12]\n", + "\n", + "for ele in list2:\n", + " if(ele==10):\n", + " continue\n", + " else:\n", + " print(ele)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "id": "670e4afd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "23\n", + "12\n" + ] + } + ], + "source": [ + "for ele in list2:\n", + " if(ele==10):\n", + " pass\n", + " else:\n", + " print(ele)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "id": "16a66763", + "metadata": {}, + "outputs": [], + "source": [ + "def tambah(angka):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "d13dc382", + "metadata": {}, + "outputs": [], + "source": [ + "tambah(10)" + ] + }, + { + "cell_type": "markdown", + "id": "22f6fd67", + "metadata": {}, + "source": [ + "## List Comprehension" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "2bbaa0e5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 10, 23, 12]" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list2 = [1,2,3,4,10,23,12]\n", + "list2" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "id": "4b3dc937", + "metadata": {}, + "outputs": [], + "source": [ + "new_list = [i**2 for i in list2]" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "id": "139e6c5e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 100, 529, 144]" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "81c904b8", + "metadata": {}, + "outputs": [], + "source": [ + "new_list=[]" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "e88b0711", + "metadata": {}, + "outputs": [], + "source": [ + "for ele in list2:\n", + " new_list.append(ele**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "bfc5767f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 100, 529, 144]" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_list" + ] + }, + { + "cell_type": "markdown", + "id": "a488f9c6", + "metadata": {}, + "source": [ + "## Dictionary Comprehension" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "5e0e3519", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'apple': [30, 10], 'mango': 25, 'banana': 40, 'grapes': 15}" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict1 = {\"apple\":[30,10], \"mango\":25, \"banana\":40, \"grapes\":15}\n", + "dict1" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "id": "de290bc8", + "metadata": {}, + "outputs": [], + "source": [ + "#new_dict = {key:value+10 for (key,value) in dict1.items()}" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "899f459e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'apple': 40, 'mango': 35, 'banana': 50, 'grapes': 25}" + ] + }, + "execution_count": 124, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "id": "62695733", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('apple', [30, 10]), ('mango', 25), ('banana', 40), ('grapes', 15)])" + ] + }, + "execution_count": 125, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict1.items()" + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "id": "339ec386", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([[30, 10], 25, 40, 15])" + ] + }, + "execution_count": 126, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict1.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "id": "0b7d38f2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['apple', 'mango', 'banana', 'grapes'])" + ] + }, + "execution_count": 127, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict1.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "id": "62887179", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('apple', 30)\n", + "('mango', 25)\n", + "('banana', 40)\n", + "('grapes', 15)\n" + ] + } + ], + "source": [ + "for i in dict1.items():\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "74c53881", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple 30\n", + "mango 25\n", + "banana 40\n", + "grapes 15\n" + ] + } + ], + "source": [ + "for (key,value) in dict1.items():\n", + " print(key, value)" + ] + }, + { + "cell_type": "code", + "execution_count": 128, + "id": "ed2443c8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 10, 23, 12]" + ] + }, + "execution_count": 128, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad7c2b2a", + "metadata": {}, + "outputs": [], + "source": [ + "new_list2 = list2.copy()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}