{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Format String\n", "\n", "String seperti tuple, elemennya tidak bisa diganti/diassign " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "website = 'http://www.python.org'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'org'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "website[-3:]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mwebsite\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'com'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "website[-3:]='com'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, Mas. hari ini mendung ya?\n" ] } ], "source": [ "format = \"Hallo, %s. hari ini %s ya?\"\n", "isi = ('Mas','mendung')\n", "print(format % isi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tanda %s adalah penentu konversi (conversion specifiers), menandai dimana nilai akan diisikan/disisipkan. \"s\" artinya bahwa nilai harus dikonversikan sebagai string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](Gambar/konv.png)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pi dengan 3 desimal angka di belakang koma: 3.142\n" ] } ], "source": [ "format = \"Pi dengan 3 desimal angka di belakang koma: %.3f\"\n", "from math import pi\n", "print(format % pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modul string menawarkan cara lain untuk menformat nilai menggunakan fungsi Template, yang harus diimport terlebih dahulu " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sekali merdeka, tetap merdeka!'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from string import Template\n", "s = Template('Sekali $x, tetap $x!')\n", "s.substitute(x='merdeka')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'A gentleman must never show his shocks'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = Template('A $thing must never $action')\n", "d={}\n", "d['thing']='gentleman'\n", "d['action']='show his shocks'\n", "s.substitute(d)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1 plus 1 equals 2'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%s plus %s equals %s' %(1, 1, 2)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 3.141593'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%10f' %pi" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 3.14'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%10.2f' %pi" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%.2f' %pi" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Wiwit'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%.5s' %'Wiwit Suryanto'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Kita dapat memakai * sebagai pengatur lebar atau presisinya, dengan input nanti dibaca dari tuple" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Wiwit'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%.*s' % (5,'Wiwit Suryanto')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tanda, perataan, penambahan nol di depan\n", "\n", "Sebelum memberikan informasi lebar dan presisi dari sebuah bilangan, kita dapat mmeberi tanda, berupa nol, plus, minus atau kosong." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0000003.14'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%010.2f' % pi" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14 '" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'%-10.2f' %pi" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 10\n", " -10\n" ] } ], "source": [ "print(('% 5d' %10) + '\\n' + ('% 5d' % -10))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " +10\n", " -10\n" ] } ], "source": [ "print(('%+5d' %10) + '\\n' + ('%+5d' % -10))" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Masukkan lebar tabel: 40\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "========================================\n", "Item Price\n", "----------------------------------------\n", "Apples 0.40\n", "Pears 0.50\n", "Cantaloupes 1.92\n", "Dried Apricots 8.00\n", "Prunes (4 lbs.) 12.00\n", "----------------------------------------\n" ] } ], "source": [ "width = input('Masukkan lebar tabel: ')\n", "price_width = 10\n", "item_width = int(width) - price_width\n", "\n", "header_format = '%-*s%*s'\n", "format = '%-*s%*.2f'\n", "\n", "print('='*int(width))\n", "print(header_format %(item_width,'Item',price_width,'Price'))\n", "\n", "print('-'*int(width))\n", "print(format %(item_width,'Apples',price_width,0.4))\n", "print(format %(item_width,'Pears',price_width,0.5))\n", "print(format %(item_width,'Cantaloupes',price_width,1.92))\n", "print(format %(item_width,'Dried Apricots',price_width,8))\n", "print(format %(item_width,'Prunes (4 lbs.)',price_width,12))\n", "\n", "print('-'*int(width))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Metode String" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. find" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'With a moo-moo here, and a moo-moo there'.find('moo')" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "title = \"Monthy Python's Flying Circus\"\n", "title.find('Flying')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "title.find('monty')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. join" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "sequence item 0: expected str instance, int found", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\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 1\u001b[0m \u001b[0mseq\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0msep\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'+'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0msep\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: sequence item 0: expected str instance, int found" ] } ], "source": [ "seq = [1,2,3,4,5]\n", "sep = '+'\n", "sep.join(seq)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1+2+3+4+5'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seq = ['1','2','3','4','5']\n", "sep = '+'\n", "sep.join(seq)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "dirs = '','usr','bin','env'" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('', 'usr', 'bin', 'env')" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dirs" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/usr/bin/env'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'/'.join(dirs)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\usr\\bin\\env\n" ] } ], "source": [ "print('C:'+'\\\\'.join(dirs))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. lower" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'trondheim hammer dance'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Trondheim Hammer Dance'.lower()" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "if 'Gumby' in ['gumby','smith','jones']: print('Found it!')" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found it!\n" ] } ], "source": [ "name = 'Gumby'\n", "names = ['gumby','smith','jones']\n", "if name.lower() in names: print('Found it!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. title" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Model Kantong Magma Gunung Merapi'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"model kantong magma gunung merapi\".title()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatifnya \"capwords\"" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Model Kantong Magma Gunung Merapi'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import string\n", "string.capwords(\"model kantong magma gunung merapi\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. replace" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Theez eez a test'" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'This is a test'.replace('is','eez')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. split" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4', '5']" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'1+2+3+4+5'.split('+')" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['', 'usr', 'bin', 'env']" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'/usr/bin/env'.split('/')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "7. strip" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'internal whitespace is kept'" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' internal whitespace is kept '.strip()" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "names = ['gumby','smith','jones']\n", "name = 'gumby '\n", "if name in names: print(\"Found it!\")" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found it!\n" ] } ], "source": [ "names = ['gumby','smith','jones']\n", "name = 'gumby '\n", "if name.strip() in names: print(\"Found it!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## lebih jauh dengan perintah print dan import\n", "\n", "Dengan perintah print, kita dapat menampilkan banyak ekspresi, yaitu cukup dipisahkan dengan tanda koma, misalnya" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Umur: 42\n" ] } ], "source": [ "print('Umur:', 42)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, Mrs. Anna\n" ] } ], "source": [ "nama = 'Anna'\n", "panggilan = 'Mrs.'\n", "sapaan = \"Hallo, \"\n", "print(sapaan, panggilan, nama)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import dapat mengambil sesuatu dan manamai dengan nama baru, misalnya\n", "```python\n", "import somemodule \n", "```\n", "atau\n", "```python\n", "from somemodule import somefunction\n", "```\n", "atau\n", "```python\n", "from somemodule import somefunction, anotherfunction, yetanotherfunction\n", "```\n", "atau\n", "```python\n", "from somemodule import *\n", "```" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math as matematika\n", "matematika.sqrt(16)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import sqrt as akar\n", "akar(4)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oyot = akar\n", "oyot(16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Augmented assignment\n", "\n", "Kita dapat menuliskan ```x = x + 1 ``` dengan menuliskan tanda oprasi matematika sebelum tanda ```=```. Ini berlaku untuk operasi ```+ - x /``` dan ```%```" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2\n", "x += 1\n", "x *=2\n", "x" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'foobarfoobar'" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fnord = 'foo'\n", "fnord += 'bar'\n", "fnord *= 2\n", "fnord" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Selingan, the joy of Python\n", "# Zen of Python" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](Gambar/zen.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional execution and if statement" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Sebutkan nama Anda: Wiwit Suryanto\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Hallo Mr. Suryanto\n" ] } ], "source": [ "nama = input('Sebutkan nama Anda: ')\n", "if nama.endswith('Suryanto'):\n", " print('Hallo Mr. Suryanto')\n", "\n" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Sebutkan nama Anda: Wiwit\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Hallo orang asing\n" ] } ], "source": [ "nama = input('Sebutkan nama Anda: ')\n", "if nama.endswith('Suryanto'):\n", " print('Hallo Mr. Suryanto')\n", "else:\n", " print('Hallo orang asing')" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "masukkan sebuah angka: 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "angka nol\n" ] } ], "source": [ "num = input('masukkan sebuah angka: ')\n", "if int(num) > 0:\n", " print('angka positif')\n", "elif int(num)<0 :\n", " print('angka negatif')\n", "else:\n", " print('angka nol')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tabel oprator komparasi di Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](Gambar/comp.png)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "# While Loops\n", "x = 1\n", "while x < 10:\n", " print(x)\n", " x +=1" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "this\n", "is\n", "an\n", "ex\n", "parrot\n" ] } ], "source": [ "# for Loops\n", "words = ['this','is','an','ex','parrot']\n", "for word in words:\n", " print(word)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "numbers = [0,1,2,3,4,5,6,7,8,9,10]\n", "for number in numbers:\n", " print(number)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for number in range(1,10):\n", " print(number)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(10)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "anne is 12 tahun\n", "beth is 45 tahun\n", "george is 32 tahun\n", "damon is 102 tahun\n" ] } ], "source": [ "# Parallel iteration\n", "nama = ['anne','beth','george','damon']\n", "umur = [12,45,32,102]\n", "for i in range(len(nama)):\n", " print(nama[i], 'is', umur[i], ' tahun')" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Slighty loopy\n", "[x*x for x in range(10)]" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 9, 36, 81]" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x*x for x in range(10) if x % 3 == 0]" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[(x,y) for x in range(3) for y in range(3)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fungsi" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "def kuat(x):\n", " 'Menghitung kuadrat dari x'\n", " return x*x" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Menghitung kuadrat dari x'" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kuat.__doc__" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "kuat(3)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function kuat in module __main__:\n", "\n", "kuat(x)\n", " Menghitung kuadrat dari x\n", "\n" ] } ], "source": [ "help(kuat)" ] }, { "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.0" } }, "nbformat": 4, "nbformat_minor": 2 }