Skip to content

Instantly share code, notes, and snippets.

@syamdev
Created January 20, 2020 23:10
Show Gist options
  • Save syamdev/eb5bae7b2d3f0bcb7b54cdc617e8fd95 to your computer and use it in GitHub Desktop.
Save syamdev/eb5bae7b2d3f0bcb7b54cdc617e8fd95 to your computer and use it in GitHub Desktop.
Intro to Python for Data Science - 1 & 2
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Intro to Python for Data Science - 1 & 2",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyP7DPXZ2pZPJCQH0ICKOYu9",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/syamdev/eb5bae7b2d3f0bcb7b54cdc617e8fd95/intro-to-python-for-data-science-1-2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "sXppMDwkharE",
"colab_type": "code",
"outputId": "57cf4143-5a10-4be7-8df4-648b43b8aac6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print('hello')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"hello\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0rUlYdHAi7-4",
"colab_type": "text"
},
"source": [
"# Python"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6jtPluwMjRVL",
"colab_type": "text"
},
"source": [
"## Data Type"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Zxnf-f1cjc4X",
"colab_type": "text"
},
"source": [
"### String"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Dx61Na8-jiKL",
"colab_type": "code",
"colab": {}
},
"source": [
"teks = \"ini string\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "yQcCeonftUtg",
"colab_type": "code",
"outputId": "cba354cb-1a6f-4f71-c4e0-60f7eb8c5fb5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"teks[:4]"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'ini '"
]
},
"metadata": {
"tags": []
},
"execution_count": 55
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "9zSK-oxxj4mh",
"colab_type": "code",
"outputId": "1b6b1145-04e2-4855-c694-8ad2333d7621",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(teks)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"ini string\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gZGvaNvKkCW7",
"colab_type": "text"
},
"source": [
"### Integer"
]
},
{
"cell_type": "code",
"metadata": {
"id": "oibSfQfVkPgP",
"colab_type": "code",
"colab": {}
},
"source": [
"nomor = 35"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "xWivVAOQkTFW",
"colab_type": "code",
"outputId": "d70d3311-00aa-4d3d-d3ba-bd77782d743f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(nomor)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"35\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Q51qfyahkjHd",
"colab_type": "text"
},
"source": [
"### Float"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OI60CZ9WkmDT",
"colab_type": "code",
"colab": {}
},
"source": [
"nomor_desimal = 3.76"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "F4SaM4HKkrWu",
"colab_type": "code",
"outputId": "8bea68c7-93a6-4d54-a28f-9e0e77386eb7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(nomor_desimal)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"3.76\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4mw5fATPk90J",
"colab_type": "text"
},
"source": [
"### List"
]
},
{
"cell_type": "code",
"metadata": {
"id": "7l25VSbeqgDl",
"colab_type": "code",
"colab": {}
},
"source": [
"words = []"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "D1aj1WKBk_Rv",
"colab_type": "code",
"colab": {}
},
"source": [
"words = ['aku', 'adalah', 50]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "SVafVo6_lJjH",
"colab_type": "code",
"outputId": "2d72bcca-6f66-4bac-cc6e-a8d35f16ccb3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(words[0])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"aku\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "19LN2iYRlOJN",
"colab_type": "code",
"colab": {}
},
"source": [
"bilangan = [1, 2, 3, 4, 5, 6]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "aEuU3WTIpHmW",
"colab_type": "code",
"colab": {}
},
"source": [
"bilangan.insert(2, 'sisipan')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9KKT1jqkndje",
"colab_type": "code",
"outputId": "a4fb1514-d011-4d91-f054-1ee666456639",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(bilangan)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[1, 2, 'sisipan', 3, 4, 5, 6, 7, 7, 7, 7, 7]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "XjEZzsytlZ8R",
"colab_type": "code",
"outputId": "eb71dcea-11bc-40cd-9729-6d1857cc696d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(bilangan[1:3])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[2, 3]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "kpNNWp2zljo7",
"colab_type": "code",
"outputId": "86ac7004-6b1f-48a3-a0df-7a0c39bb8f2c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(bilangan[-1])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"6\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "SMUW6PCXnZxD",
"colab_type": "code",
"colab": {}
},
"source": [
"bilangan.append(7)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "8tkLf677oFur",
"colab_type": "code",
"outputId": "f69a1f1c-8fb3-4a80-fef6-75fcce0b9fc3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(bilangan)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bSlHBSvfmYf_",
"colab_type": "text"
},
"source": [
"### Dict"
]
},
{
"cell_type": "code",
"metadata": {
"id": "j2EjaDwqmdjm",
"colab_type": "code",
"colab": {}
},
"source": [
"dictionary = {\n",
" 'a': ['satu', 'one', 'siji'],\n",
" 'b': 'dua',\n",
" 'c': 'tiga'\n",
"}"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "94aXBgkzmx3j",
"colab_type": "code",
"outputId": "96b9a559-495d-4af6-93d5-ef0e34177145",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(dictionary['a'])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"['satu', 'one', 'siji']\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "iN9oqtlwm8F3",
"colab_type": "code",
"colab": {}
},
"source": [
"dictionary['d'] = 'empat'"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mi0lYWPgo3ay",
"colab_type": "code",
"outputId": "442f00b8-35d1-47d4-fab9-5a2e66c35f97",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(dictionary)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"{'a': ['satu', 'one', 'siji'], 'b': 'dua', 'c': 'tiga', 'd': 'empat'}\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "2FYKc-Rcpb77",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "XjaIKhfEpgo0",
"colab_type": "text"
},
"source": [
"### Boolean"
]
},
{
"cell_type": "code",
"metadata": {
"id": "rSLc6JM7pkUz",
"colab_type": "code",
"colab": {}
},
"source": [
"a = True\n",
"b = False"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "uXjVDb0epmc5",
"colab_type": "code",
"outputId": "a7c04466-0cbd-4a50-84c2-51c123136f6a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"type(dictionary)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dict"
]
},
"metadata": {
"tags": []
},
"execution_count": 41
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VBhfJE4gq8D5",
"colab_type": "text"
},
"source": [
"### Tuple"
]
},
{
"cell_type": "code",
"metadata": {
"id": "wLp5shyHrEYF",
"colab_type": "code",
"colab": {}
},
"source": [
"contoh = (1, 2, 3)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "68mfGAY3rI8r",
"colab_type": "code",
"outputId": "92da36e7-fd0e-4cc8-ecd9-cb5ce338ca5b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"contoh[0]"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 46
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lTyYvJ9Rtguk",
"colab_type": "text"
},
"source": [
"## Operators"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pkMi8WHbtvR7",
"colab_type": "text"
},
"source": [
"### Arithmetic"
]
},
{
"cell_type": "code",
"metadata": {
"id": "-USQDfgdtmYN",
"colab_type": "code",
"colab": {}
},
"source": [
"a = 5\n",
"b = 4"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mJl4VEZPt63N",
"colab_type": "code",
"outputId": "d9ba3410-40f7-413c-eb5d-ae8fb05204e1",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
}
},
"source": [
"# Penjumlahan\n",
"print(a + b)\n",
"\n",
"# Pengurangan\n",
"print(a - b)\n",
"\n",
"# Pembagian\n",
"print(a / b)\n",
"\n",
"# Modulo (sisa hasil bagi)\n",
"print(a % b)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"9\n",
"1\n",
"1.25\n",
"1\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BpKVlXh9trNW",
"colab_type": "text"
},
"source": [
"### Comparison\n",
"< > <= >= == !="
]
},
{
"cell_type": "code",
"metadata": {
"id": "uS6PPRrPuxnF",
"colab_type": "code",
"outputId": "fbfec98e-4e9f-420a-bff1-d55aa62e5e2b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"5 > 3"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 60
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "cc0Q9PGtu4Dv",
"colab_type": "code",
"outputId": "87ebd177-9a3c-4185-ac38-d0e61f996d23",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"10 < 5"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 61
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "DXxy0HZVu5SD",
"colab_type": "code",
"outputId": "7708702e-6d15-46e2-b57b-892936e8e28b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"20 >= 20"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 62
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "c6nEzKXrvL8u",
"colab_type": "code",
"outputId": "dc5d2b14-8086-40c2-cf0c-1d06db1bdbc9",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"'satu' == 'sau'"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 63
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "WbZoAHpVvMVN",
"colab_type": "code",
"outputId": "67e940f9-67d7-4c48-c586-7f45ca9397a6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"'satu' != 'sau'"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 64
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0NAptP15trbG",
"colab_type": "text"
},
"source": [
"### Logical\n",
"and or not"
]
},
{
"cell_type": "code",
"metadata": {
"id": "JdgM5R4zvxXe",
"colab_type": "code",
"colab": {}
},
"source": [
"# T and T -> T\n",
"\n",
"# F or F -> F"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "1Yrg0U-mtrmK",
"colab_type": "text"
},
"source": [
"### Membership\n",
"in not in"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OSvrY4vDwKxg",
"colab_type": "code",
"colab": {}
},
"source": [
"bilangan = [1, 2, 3, 4, 5]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tG178YtqwT72",
"colab_type": "code",
"outputId": "beb274b5-9cdd-4dd4-ff5f-16f9c8d5c94a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"1 in bilangan"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 66
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "pfWZOvg1wZJC",
"colab_type": "code",
"outputId": "150a0fd8-d88c-4ca7-b77c-50d4bf09f35c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"1 not in bilangan"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 68
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "t0jOUot6tr1S",
"colab_type": "text"
},
"source": [
"### Conditional\n",
"if:\n",
"elif:\n",
"else:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "ELudRbDgxEvn",
"colab_type": "code",
"outputId": "8d501137-5985-4e26-8cea-b31ff3d598bd",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"if 3 > 1:\n",
" print('benar')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"benar\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "1Zk6CDpYxG9f",
"colab_type": "code",
"outputId": "6d3e5fd1-1af3-482c-ec67-1ae7b44f8040",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"if 3 > 1:\n",
" print('benar')\n",
"else:\n",
" print('salah')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"benar\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "f7d4rfqFxfHY",
"colab_type": "code",
"outputId": "2a810851-9f91-4ed4-9184-097462690345",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"a = 3\n",
"\n",
"if a == 1:\n",
" print('satu')\n",
"elif a == 2:\n",
" print('dua')\n",
"else:\n",
" print('lebih dari dua')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"lebih dari dua\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TldK9nw0yG4j",
"colab_type": "code",
"outputId": "8a16c34f-3501-454c-b04c-e294dabae1e2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"bilangan = [1, 2, 3, 4, 5]\n",
"\n",
"if 1 in bilangan:\n",
" print('ada di list')\n",
"else:\n",
" print('tidak ada di list')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"ada di list\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wfc8eh4y3tve",
"colab_type": "text"
},
"source": [
"## Loop\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yylwcv113ynD",
"colab_type": "text"
},
"source": [
"### For"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Fcq3lh-L39Yn",
"colab_type": "code",
"outputId": "bb6747c4-8303-4d33-ddf3-b79e672d69d0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
}
},
"source": [
"bilangan = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for i in bilangan:\n",
" print(i*2)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2\n",
"4\n",
"6\n",
"8\n",
"10\n",
"12\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mVB9gZW3325P",
"colab_type": "text"
},
"source": [
"### While"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WV5m6ABf4b53",
"colab_type": "code",
"outputId": "190367e8-e418-49b5-8a70-195fcdfa8f1e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
}
},
"source": [
"number = 10\n",
"\n",
"while number < 20:\n",
" number = number + 1\n",
" print(number)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0Wf5xwbg94m6",
"colab_type": "text"
},
"source": [
"#### Break"
]
},
{
"cell_type": "code",
"metadata": {
"id": "eqkjRMfV99yz",
"colab_type": "code",
"outputId": "b402a2d2-b7ca-4d03-b4be-462ddfd494a9",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
}
},
"source": [
"bilangan = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for angka in bilangan:\n",
" if angka == 4:\n",
" break\n",
" else:\n",
" print(angka)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7tTEv1QE9-yC",
"colab_type": "text"
},
"source": [
"#### Continue"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OZYwi8-5-GVI",
"colab_type": "code",
"outputId": "e8d76cf7-ee75-4749-d67a-06b527db255c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 102
}
},
"source": [
"bilangan = [1, 2, 3, 4, 5, 6]\n",
"\n",
"for angka in bilangan:\n",
" if angka == 4:\n",
" continue\n",
" else:\n",
" print(angka)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"5\n",
"6\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "E4XcC9S7TZqz",
"colab_type": "text"
},
"source": [
"## Function"
]
},
{
"cell_type": "code",
"metadata": {
"id": "YRFsDDC-TgIG",
"colab_type": "code",
"colab": {}
},
"source": [
"def greeting(name):\n",
" print('Halo ' + name)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "6TizMhwQT2oB",
"colab_type": "code",
"outputId": "6749c754-eff9-401f-ca76-99bbb32c781c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"greeting('Budi')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Halo Budi\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "pcErnPYyVMZw",
"colab_type": "code",
"colab": {}
},
"source": [
"def contoh(bilangan):\n",
" print(bilangan[2])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Vr-BQhgaViwP",
"colab_type": "code",
"outputId": "8a1fd6c4-29cc-48e2-9a6a-adb1d85bd72c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"contoh([1, 2, 3, 4, 5])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "FTwmaMviV80g",
"colab_type": "code",
"colab": {}
},
"source": [
"def get_identity(first_name, last_name):\n",
" print('First name: ' + first_name)\n",
" print('Last name: ' + last_name)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vN2n7EWYV9qF",
"colab_type": "code",
"outputId": "980b2d31-5c01-4ef0-9439-73b876838426",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"source": [
"get_identity('bill', 'gates')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"First name: bill\n",
"Last name: gates\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "bthX6OISke-6",
"colab_type": "code",
"colab": {}
},
"source": [
"def contoh_print(bilangan):\n",
" print(bilangan[2])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jJ_VbO0_kfU3",
"colab_type": "code",
"colab": {}
},
"source": [
"def contoh_return(bilangan):\n",
" return bilangan[2]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mEL1-RvAkflk",
"colab_type": "code",
"outputId": "fd6c2028-c8c3-4f21-bad0-b8fc68a1de6c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"contoh_print([1, 2, 3])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "91Z6Nrt6kf0c",
"colab_type": "code",
"outputId": "3a3d294d-54b9-403a-cfb5-8db1272d8643",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"contoh_return([1, 2, 3])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3"
]
},
"metadata": {
"tags": []
},
"execution_count": 157
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DLNpWq4QcFF7",
"colab_type": "text"
},
"source": [
"### Built-in Functions"
]
},
{
"cell_type": "code",
"metadata": {
"id": "yYBhiLVYV93R",
"colab_type": "code",
"outputId": "0abf89a8-a83a-4a27-f033-6088d5b3816d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
}
},
"source": [
"a = [1, 2, 3, 4, 5]\n",
"b = 'bill gates'\n",
"\n",
"print(len(a))\n",
"print(len(b))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"5\n",
"10\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jTDRoZrecor-",
"colab_type": "code",
"outputId": "a4feebf2-a238-47a5-aadf-ab573e216d66",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"max(a)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5"
]
},
"metadata": {
"tags": []
},
"execution_count": 137
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "gmydSKX_ctXr",
"colab_type": "code",
"outputId": "3f737ce4-adad-4e0b-9ea7-7a2854b5459b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"min(a)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {
"tags": []
},
"execution_count": 138
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "HUiMFdtSctoG",
"colab_type": "code",
"outputId": "bd6a5de7-5470-4fcb-ba4d-2e95c2e04d3b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"sum(a)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"15"
]
},
"metadata": {
"tags": []
},
"execution_count": 139
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "JG0EPvPcct3t",
"colab_type": "code",
"outputId": "a155fc1a-0fef-4dc9-a330-550b8fd43da8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"c = \"aku adalah anak gembala\"\n",
"c.split()"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['aku', 'adalah', 'anak', 'gembala']"
]
},
"metadata": {
"tags": []
},
"execution_count": 140
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rbL17kvrrkHC",
"colab_type": "text"
},
"source": [
"### Modules / Libraries"
]
},
{
"cell_type": "code",
"metadata": {
"id": "F2YfeedWrvTi",
"colab_type": "code",
"colab": {}
},
"source": [
"import random"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "3p7JMIXDr5sW",
"colab_type": "code",
"outputId": "85863bd9-a53a-40e3-f427-1543c50d0eb7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"random.randint(1, 10)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"6"
]
},
"metadata": {
"tags": []
},
"execution_count": 185
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aoHA3L3irYG8",
"colab_type": "text"
},
"source": [
"## Exercise"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pyUi2IVjrbX5",
"colab_type": "code",
"outputId": "a0f9022a-5b44-464c-be35-50b1380d5ea1",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# Exercise 1\n",
"\n",
"# Coba print list di bawah sehingga muncul tulisan ['anak', gembala', 'selalu', 'riang']\n",
"kalimat = ['aku', 'adalah', 'anak', 'gembala', 'selalu', 'riang']\n",
"kalimat[2:6]"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['anak', 'gembala', 'selalu', 'riang']"
]
},
"metadata": {
"tags": []
},
"execution_count": 54
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "-gPcM_niyf6g",
"colab_type": "code",
"outputId": "56e38700-415f-4674-8068-9ba26c2e7171",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# Exercise 2\n",
"\n",
"# Coba buat kondisional untuk mencari tahu sebuah bilangan genap atau ganjil\n",
"# Hint: pakai modulo\n",
"\n",
"bilangan = 5\n",
"\n",
"if bilangan % 2 == 1:\n",
" print('ganjil')\n",
"else:\n",
" print('genap')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"ganjil\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "7aqP7FRg0Nby",
"colab_type": "code",
"outputId": "af6168a8-8d3f-4571-b569-828a721aefa5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# Exercise 3\n",
"\n",
"# Buat program untuk mengelompokkan nilai mahasiswa\n",
"# 0 - 20 : D\n",
"# 21 - 50 : C\n",
"# 51 - 80 : B\n",
"# 81 - 100 : A\n",
"\n",
"nilai = 75\n",
"\n",
"if (nilai <= 100) and (nilai >= 81):\n",
" print('A')\n",
"elif (nilai <= 80) and (nilai >= 51):\n",
" print('B')\n",
"elif (nilai <= 50) and (nilai >= 21):\n",
" print('C')\n",
"else:\n",
" print('D')"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"B\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "8tZnLVhZ6rFD",
"colab_type": "code",
"outputId": "1272f350-6d07-4056-dbbf-6601e71c85b8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 102
}
},
"source": [
"# Exercise 4\n",
"\n",
"# Buat program (menggunakan for) untuk melakukan print bilangan di dalam list (hanya bilangan ganjil saja)\n",
"\n",
"bilangan = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"\n",
"for angka in bilangan:\n",
" if angka % 2 == 1:\n",
" print(angka)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "PrsBS25vQby1",
"colab_type": "code",
"outputId": "57aecbbe-c27e-4555-b953-e4afedd0110c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# Exercise 5\n",
"\n",
"# Buat program untuk memasukkan angka ke dalam list (hanya bilangan genap saja) dari sebuah list\n",
"# Hint: for, if, append\n",
"\n",
"a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"b = []\n",
"\n",
"for i in a:\n",
" if i % 2 == 0:\n",
" b.append(i)\n",
"print(b)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[2, 4, 6, 8, 10]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "FSCSG8KaXG2r",
"colab_type": "code",
"colab": {}
},
"source": [
"# Exercise 6\n",
"\n",
"# Buat sebuah function, dimana parameternya adalah list of integers dan outputnya adalah angka terakhir dari list\n",
"\n",
"def angka_terakhir(bilangan):\n",
" print(bilangan[-1])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "7MlhjxiBXIFG",
"colab_type": "code",
"outputId": "7d624e86-de8d-4713-dfb5-a5a73b9e64f1",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"angka_terakhir([1, 2, 3, 4, 10])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "t-b3S66xXIIg",
"colab_type": "code",
"colab": {}
},
"source": [
"# Exercise 7\n",
"\n",
"# Buat sebuah function, dimana parameternya adalah list of integers\n",
"# Function tersebut berfungsi untuk memeriksa apakah angka pertama dan terakhir dari list tersebut sama atau tidak\n",
"\n",
"def check(list_of_intergers):\n",
" if list_of_intergers[0] == list_of_intergers[-1]:\n",
" print('sama')\n",
" else:\n",
" print('beda')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4zd3wnY_XIOC",
"colab_type": "code",
"outputId": "2683740a-3676-47ea-dfd4-99f444c87d5b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"check([1, 2, 3, 4, 1])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"sama\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "mPbAITW_XISS",
"colab_type": "code",
"colab": {}
},
"source": [
"# Exercise 8\n",
"\n",
"# Buat sebuah function, parameter ada 2, yaitu 2 list\n",
"# Function tsb harus melakukan print terhadap list yang elemennya paling banyak\n",
"\n",
"# Input: [1, 2, 3, 4], [1, 2, 3]\n",
"# Output: [1, 2, 3, 4]\n",
"\n",
"def get_longer_list(list1, list2):\n",
" if len(list1) > len(list2):\n",
" print(list1)\n",
" elif len(list1) < len(list2):\n",
" print(list2)\n",
" else:\n",
" print('sama')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "5T9CYvbZXILw",
"colab_type": "code",
"outputId": "ed825744-ff13-4ead-ce55-792bdbf70aa2",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"get_longer_list([1, 2, 3, 4],[1, 2, 3])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[1, 2, 3, 4]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ASI5xL2jlrtS",
"colab_type": "code",
"colab": {}
},
"source": [
"# Exercise 8b\n",
"\n",
"# Buat sebuah function, parameter ada 2, yaitu 2 list\n",
"# Function tsb harus melakukan print terhadap list yang elemennya paling banyak\n",
"\n",
"# Input: [1, 2, 3, 4], [1, 2, 3]\n",
"# Output: [1, 2, 3, 4]\n",
"\n",
"def get_longer_list(list1, list2):\n",
" if len(list1) > len(list2):\n",
" return list1\n",
" return list2"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-ger0vCAl042",
"colab_type": "code",
"colab": {}
},
"source": [
"a = [1, 2, 3]\n",
"b = [1, 2]\n",
"\n",
"c = get_longer_list(a, b)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "jkQq2x3vmGHe",
"colab_type": "code",
"outputId": "eb6ca8ea-5d17-46b8-cdf4-120334fcb823",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"c"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"metadata": {
"tags": []
},
"execution_count": 160
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "SsMV4MoXmKZ1",
"colab_type": "code",
"colab": {}
},
"source": [
"# Exercise 9\n",
"\n",
"# Buat sebuah function, parameternya ada 2, para 1 = int, para 2 = list_of_integers\n",
"# Function tsb hrs me-return True (jika int tsb ada di list_of_integers) atau False (jika sebaliknya)\n",
"\n",
"def check(integer, list_of_integers):\n",
" if integer in list_of_integers:\n",
" return True\n",
" return False"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "py-7sQkqmKnl",
"colab_type": "code",
"outputId": "8540e3ca-dddd-447d-bb58-a457438035df",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"check(1, [1, 2, 3])"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 172
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "sS8gQON-mKsg",
"colab_type": "code",
"colab": {}
},
"source": [
"# Exercise 10\n",
"\n",
"# Buat sebuah program suit, dimana parameternya adalah objek pilihan kita\n",
"# Outputnya adalah kita menang atau tidak\n",
"# Pakai Dictionary\n",
"\n",
"# Outputnya:\n",
"# \"(menang|kalah|seri)! Kamu memilih (gunting|batu|kertas), komputer memilih (gunting|batu|kertas)\"\n",
"\n",
"import random\n",
"\n",
"def suit(my_choice):\n",
"\n",
" player = {\n",
" 1: 'batu',\n",
" 2: 'gunting',\n",
" 3: 'kertas'\n",
" }\n",
"\n",
" game = {\n",
" 1: {1: 'seri', 2: 'menang', 3: 'kalah'},\n",
" 2: {1: 'kalah', 2: 'seri', 3: 'menang'},\n",
" 3: {1: 'menang', 2: 'kalah', 3: 'seri'}\n",
" }\n",
"\n",
" comp_choice = random.randint(1, 3)\n",
" match_result = game[my_choice][comp_choice]\n",
"\n",
" return match_result + '! Kamu memilih ' + player[my_choice] + ', komputer memilih ' + player[comp_choice]\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JqifVlbz3W4t",
"colab_type": "code",
"outputId": "59f76ea2-2fb4-47b7-d423-409952ce37e5",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"suit(1)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'kalah! Kamu memilih batu, komputer memilih kertas'"
]
},
"metadata": {
"tags": []
},
"execution_count": 9
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z-vFHS_6w5qu",
"colab_type": "text"
},
"source": [
"## Extra"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IcVQXHgtJsh8",
"colab_type": "text"
},
"source": [
"### Add HTML link to notebook"
]
},
{
"cell_type": "code",
"metadata": {
"id": "_6rV9vtaIN-0",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 63
},
"outputId": "1b09a0bb-c0f8-48f5-c254-143eac8be105"
},
"source": [
"%%html\n",
"<p>Right click on link, then click \"Open link on new tab\"</p>\n",
"<a href=\"https://sy4m.com\">sy4m.com</a> | \n",
"<a href=\"https://kerjaonline.id\">Kerja Online</a> | \n",
"<a href=\"https://OwHub.com\">OwHub</a> | \n",
"<a href=\"https://www.SpinnerArtikelIndonesia.com\">Spinner Artikel Indonesia</a>"
],
"execution_count": 27,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/html": [
"<p>Right click on link, then click \"Open link on new tab\"</p>\n",
"<a href=\"https://sy4m.com\">sy4m.com</a> | \n",
"<a href=\"https://kerjaonline.id\">Kerja Online</a> | \n",
"<a href=\"https://OwHub.com\">OwHub</a> | \n",
"<a href=\"https://www.SpinnerArtikelIndonesia.com\">Spinner Artikel Indonesia</a>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {
"tags": []
}
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "B9CLGjfBMD9G",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment