Created
November 10, 2024 09:33
-
-
Save devops-school/082ee2640365eda326b5eb61aa1aef32 to your computer and use it in GitHub Desktop.
Jupyter notebook – Lab Session – 10 – Introduction of Data Types, Variables & checkpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "9559f9de", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hacktiv8\n", | |
"<class 'str'>\n" | |
] | |
} | |
], | |
"source": [ | |
"a = \"hacktiv8\"\n", | |
"print(a)\n", | |
"print(type(a))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "3de3f8c9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"8\n", | |
"<class 'int'>\n" | |
] | |
} | |
], | |
"source": [ | |
"a = 8\n", | |
"print(a)\n", | |
"print(type(a))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "c488e634", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"8\n", | |
"<class 'str'>\n" | |
] | |
} | |
], | |
"source": [ | |
"a = \"8\"\n", | |
"print(a)\n", | |
"print(type(a))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "57c38734", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"This string contains a single quote (') character.\n", | |
"This string contains a double quote (\") character.\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"This string contains a single quote (') character.\")\n", | |
"print('This string contains a double quote (\") character.')\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "12864f02", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "SyntaxError", | |
"evalue": "invalid syntax (Temp/ipykernel_2300/1099109788.py, line 1)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;36m File \u001b[1;32m\"C:\\Users\\ACER\\AppData\\Local\\Temp/ipykernel_2300/1099109788.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print('This string contains a single quote (') character.')\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" | |
] | |
} | |
], | |
"source": [ | |
"print('This string contains a single quote (') character.')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "6cfe59c4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"This string contains a single quote (') character.\n", | |
"This string contains a double quote (\") character.\n" | |
] | |
} | |
], | |
"source": [ | |
"print('This string contains a single quote (\\') character.')\n", | |
"\n", | |
"print(\"This string contains a double quote (\\\") character.\")\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "b1ce4c58", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"20" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = 30\n", | |
"a\n", | |
"b = 20\n", | |
"b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "36031841", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"123123123123123123124\n" | |
] | |
} | |
], | |
"source": [ | |
"print(123123123123123123123 + 1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "e4bae283", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4.2\n", | |
"<class 'float'>\n", | |
"4.0\n", | |
"0.4\n", | |
"4000000.0\n", | |
"0.00042\n" | |
] | |
} | |
], | |
"source": [ | |
"print(4.2)\n", | |
"print(type(4.2))\n", | |
"print(4.)\n", | |
"print(.4)\n", | |
"print(.4e7)\n", | |
"print(4.2e-4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "29d0c247", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<class 'bool'>\n", | |
"<class 'bool'>\n" | |
] | |
} | |
], | |
"source": [ | |
"print(type(True))\n", | |
"print(type(False))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "ef704deb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"300\n" | |
] | |
} | |
], | |
"source": [ | |
"n = 300\n", | |
"print(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"id": "ccb85b58", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"300" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"id": "8cc778d6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1000\n" | |
] | |
} | |
], | |
"source": [ | |
"n = 1000\n", | |
"print(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "b9173ee5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1000" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"id": "a94e026b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"300 300 300\n" | |
] | |
} | |
], | |
"source": [ | |
"a = b = c = 300\n", | |
"print(a, b, c)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"id": "c4fb3dff", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"24.5\n", | |
"Now I'am a String\n" | |
] | |
} | |
], | |
"source": [ | |
"var = 24.5\n", | |
"print(var)\n", | |
"\n", | |
"var = \"Now I'am a String\"\n", | |
"print(var)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"id": "6080cb3b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hacktiv8 54 True\n" | |
] | |
} | |
], | |
"source": [ | |
"name = \"Hacktiv8\"\n", | |
"Age = 54\n", | |
"has_laptops = True\n", | |
"print(name, Age, has_laptops)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "3f64cdfa", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "SyntaxError", | |
"evalue": "invalid decimal literal (Temp/ipykernel_2300/687628429.py, line 1)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;36m File \u001b[1;32m\"C:\\Users\\ACER\\AppData\\Local\\Temp/ipykernel_2300/687628429.py\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m 9_kepala_naga = True\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid decimal literal\n" | |
] | |
} | |
], | |
"source": [ | |
"9_kepala_naga = True" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "2fc58400", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 2 3 4 5 6 7 8\n" | |
] | |
} | |
], | |
"source": [ | |
"age = 1\n", | |
"Age = 2\n", | |
"aGe = 3\n", | |
"AGE = 4\n", | |
"a_g_e = 5\n", | |
"_age = 6\n", | |
"age_ = 7\n", | |
"_AGE_ = 8\n", | |
"\n", | |
"print(age, Age, aGe , AGE , a_g_e , _age , age_ , _AGE_)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "b3a2b6f5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"30" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = 10\n", | |
"b = 20\n", | |
"a + b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "d99198e6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"25" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a + b - 5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"id": "70914014", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"7\n", | |
"1\n", | |
"12\n", | |
"1.3333333333333333\n", | |
"1\n", | |
"64\n" | |
] | |
} | |
], | |
"source": [ | |
"#Here are some examples of these operators in use:\n", | |
"\n", | |
"a = 4\n", | |
"b = 3\n", | |
"\n", | |
"print(a + b)\n", | |
"print(a - b)\n", | |
"print(a * b)\n", | |
"print(a / b)\n", | |
"print(a % b)\n", | |
"print(a ** b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "24669326", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2.0" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#The result of standard division (/) is always a float, even if the dividend is evenly divisible by the divisor :\n", | |
"\n", | |
"10/5" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"id": "ac1a2633", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"False\n", | |
"True\n", | |
"True\n", | |
"False\n", | |
"True\n", | |
"False\n", | |
"True\n", | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"#Here are examples of the comparison operators in use:\n", | |
"\n", | |
"a = 10\n", | |
"b = 20\n", | |
"print(a == b)\n", | |
"print(a != b)\n", | |
"print(a <= b)\n", | |
"print(a >= b)\n", | |
"\n", | |
"a = 30\n", | |
"b = 30\n", | |
"print(a == b)\n", | |
"print(a != b)\n", | |
"print(a <= b)\n", | |
"print(a >= b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "b49260e2", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"foobar\n", | |
"foobarbaz\n", | |
"Hackiv8 PTP\n" | |
] | |
} | |
], | |
"source": [ | |
"#+ Operators\n", | |
"s = 'foo'\n", | |
"t = 'bar'\n", | |
"u = 'baz'\n", | |
"\n", | |
"print(s+t)\n", | |
"\n", | |
"print(s+t+u)\n", | |
"\n", | |
"print(\"Hackiv8 \" + \"PTP\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "87c70935", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'foo.foo.foo.foo.'" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#* Operators\n", | |
"s = 'foo.'\n", | |
"s * 4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "b1201ef9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n", | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"#in Opeators\n", | |
"s = 'foo'\n", | |
"print(s in 'That food for us')\n", | |
"print(s in 'That good for us')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "c804d265", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hacktiv8\n", | |
"hacktiv8\n", | |
"hACKtiv8\n", | |
"Hacktiv8\n", | |
"HACKTIV8\n" | |
] | |
} | |
], | |
"source": [ | |
"#Case Conversion\n", | |
"s = 'HackTIV8'\n", | |
"\n", | |
"#capitalize\n", | |
"print(s.capitalize())\n", | |
"\n", | |
"#lower\n", | |
"print(s.lower())\n", | |
"\n", | |
"#swap case\n", | |
"print(s.swapcase())\n", | |
"\n", | |
"#title\n", | |
"print(s.title())\n", | |
"\n", | |
"#uppercase\n", | |
"print(s.upper())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "e8d7b480", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"ANI\n" | |
] | |
} | |
], | |
"source": [ | |
"name = \"ANI\"\n", | |
"name\n", | |
"'ANI'\n", | |
"NAME = \"ANITA\"\n", | |
"print(name)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "92b38599", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"ANI\n", | |
"ANITA\n" | |
] | |
} | |
], | |
"source": [ | |
"print(name)\n", | |
"print(NAME)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "bb83e105", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"30" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Operator and Expression\n", | |
"\n", | |
"a = 10\n", | |
"b = 20\n", | |
"a + b # print(a+b)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "d1bbd784", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Capitalized Hello world\n", | |
"Titled Hello World\n" | |
] | |
} | |
], | |
"source": [ | |
"# .capitalize() vs .title()\n", | |
"s = 'hello world'\n", | |
"print('Capitalized ', s.capitalize())\n", | |
"print('Titled ', s.title())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"id": "f488e5b3", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['foo', 'bar', 'baz', 'qux']\n" | |
] | |
} | |
], | |
"source": [ | |
"a = ['foo', 'bar', 'baz', 'qux']\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "1020dca7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = ['foo', 'bar', 'baz', 'qux']\n", | |
"b = ['baz', 'foo', 'bar', 'qux']\n", | |
"a == b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"id": "bd1e3da6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[21.4, 'adrian', 3, 4, 'bark', False, 3.14159]\n" | |
] | |
} | |
], | |
"source": [ | |
"a = [21.4, 'adrian', 3, 4, 'bark', False, 3.14159]\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"id": "346401af", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"foo\n", | |
"corge\n" | |
] | |
} | |
], | |
"source": [ | |
"a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"print(a[0])\n", | |
"print(a[5])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "5fb2bb96", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"corge\n", | |
"foo\n" | |
] | |
} | |
], | |
"source": [ | |
"print(a[-1])\n", | |
"print(a[-6])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"id": "fc733cb5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['baz', 'qux', 'quux']" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"a[2:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"id": "72d0ee03", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'grault', 'garply']\n", | |
"['foo', 'bar', 'baz', 'qux', 'quux', 'corge', 'foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n" | |
] | |
} | |
], | |
"source": [ | |
"#The concatenation (+) and replication (*) operators:\n", | |
"print(a)\n", | |
"\n", | |
"print(a + ['grault', 'garply'])\n", | |
"print(a * 2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"id": "b36cef26", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"6\n", | |
"bar\n", | |
"qux\n" | |
] | |
} | |
], | |
"source": [ | |
"#len(), min(), max() berdasarkan dari urutan abjad\n", | |
"print(a)\n", | |
"\n", | |
"print(len(a))\n", | |
"print(min(a))\n", | |
"print(max(a))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"id": "4cfe8ef7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"['foo', 'bar', 10, 'qux', 'quux', 20]\n" | |
] | |
} | |
], | |
"source": [ | |
"#Modifying a Single List Value\n", | |
"a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"\n", | |
"print(a)\n", | |
"\n", | |
"a[2] = 10\n", | |
"a[-1] = 20\n", | |
"\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"id": "b2f2e950", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['foo', 'bar', 10, 'quux', 20]\n" | |
] | |
} | |
], | |
"source": [ | |
"#A list item can be deleted with the del command\n", | |
"del a[3]\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"id": "725087ee", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['bar', 'baz', 'qux']\n", | |
"['foo', 1.1, 2.2, 3.3, 4.4, 5.5, 'quux', 'corge']\n" | |
] | |
} | |
], | |
"source": [ | |
"#Modifying Multiple List Values\n", | |
"a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']\n", | |
"\n", | |
"print(a[1:4])\n", | |
"\n", | |
"a[1:4] = [1.1, 2.2, 3.3, 4.4, 5.5]\n", | |
"\n", | |
"print(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"id": "8ccd4b46", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"('foo', 'bar', 'baz', 'qux', 'quux', 'corge')\n" | |
] | |
} | |
], | |
"source": [ | |
"#Python Tuples\n", | |
"#Defining and Using Tuples\n", | |
"\n", | |
"t = ('foo', 'bar', 'baz', 'qux', 'quux', 'corge')\n", | |
"\n", | |
"print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"id": "a5b09d9c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"foo\n", | |
"corge\n" | |
] | |
} | |
], | |
"source": [ | |
"print(t[0])\n", | |
"print(t[-1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"id": "a856511c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'foo'" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#packing and unpacking\n", | |
"(s1, s2, s3, s4) = ('foo', 'bar', 'baz', 'qux')\n", | |
"s1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"id": "bea9dd33", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Twins\n", | |
"Mariners\n" | |
] | |
} | |
], | |
"source": [ | |
"#Python Dictionary\n", | |
"#Defining a Dictionary\n", | |
"\n", | |
"MLB_team = {\n", | |
" 'Colorado': 'Rockies',\n", | |
" 'Boston': 'Red Sox',\n", | |
" 'Minnesota' : 'Twins',\n", | |
" 'Milwaukee' : 'Brewers',\n", | |
" 'Seattle' : 'Mariners'\n", | |
"}\n", | |
"\n", | |
"print(MLB_team['Minnesota'])\n", | |
"print(MLB_team['Seattle'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"id": "6463e523", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Colorado': 'Rockies',\n", | |
" 'Boston': 'Red Sox',\n", | |
" 'Minnesota': 'Twins',\n", | |
" 'Milwaukee': 'Brewers',\n", | |
" 'Seattle': 'Mariners',\n", | |
" 'Kansas City': 'Royals'}" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Adding an entry to an existing dictionary is simply a matter of assigning a new key and value:\n", | |
"\n", | |
"MLB_team['Kansas City'] = 'Royals'\n", | |
"MLB_team" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"id": "bf236963", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Colorado': 'Rockies',\n", | |
" 'Boston': 'Red Sox',\n", | |
" 'Minnesota': 'Twins',\n", | |
" 'Milwaukee': 'Brewers',\n", | |
" 'Seattle': 'Seahawks',\n", | |
" 'Kansas City': 'Royals'}" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#If you want to update an entry, u can just assign a new value to an existing key:\n", | |
"\n", | |
"MLB_team['Seattle'] = 'Seahawks'\n", | |
"MLB_team" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"id": "640bd28c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Colorado': 'Rockies',\n", | |
" 'Boston': 'Red Sox',\n", | |
" 'Minnesota': 'Twins',\n", | |
" 'Milwaukee': 'Brewers',\n", | |
" 'Kansas City': 'Royals'}" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#To delete an entry, use the del statement, specifying the key to delete:\n", | |
"del MLB_team['Seattle']\n", | |
"MLB_team" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"id": "311863aa", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"person = {}\n", | |
"type(person)\n", | |
"\n", | |
"person['fname'] = 'Hack'\n", | |
"person['lname'] = 'PTP'\n", | |
"person['age'] = 51\n", | |
"person['spouse'] = 'Edna'\n", | |
"person['children'] = ['Ralph', 'Betty', 'Joey']\n", | |
"person['pets'] = {'dog': 'Fido', 'cat': 'Sox'}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"id": "fa4cef47", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'fname': 'Hack',\n", | |
" 'lname': 'PTP',\n", | |
" 'age': 51,\n", | |
" 'spouse': 'Edna',\n", | |
" 'children': ['Ralph', 'Betty', 'Joey'],\n", | |
" 'pets': {'dog': 'Fido', 'cat': 'Sox'}}" | |
] | |
}, | |
"execution_count": 65, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"person" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"id": "dd15092d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hack\n", | |
"PTP\n" | |
] | |
} | |
], | |
"source": [ | |
"print(person['fname'])\n", | |
"print(person['lname'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"id": "17330695", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['Ralph', 'Betty', 'Joey']\n", | |
"Betty\n" | |
] | |
} | |
], | |
"source": [ | |
"print(person['children'])\n", | |
"print(person['children'][1])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"id": "789708cb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'dog': 'Fido', 'cat': 'Sox'}\n", | |
"Sox\n" | |
] | |
} | |
], | |
"source": [ | |
"print(person['pets'])\n", | |
"print(person['pets']['cat'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"id": "f214d924", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"dict_items([('a', 10), ('b', 20), ('c', 30)])\n", | |
"dict_keys(['a', 'b', 'c'])\n", | |
"dict_values([10, 20, 30])\n" | |
] | |
} | |
], | |
"source": [ | |
"#Built-in Methods\n", | |
"d = {'a':10, 'b':20, 'c':30}\n", | |
"\n", | |
"#items\n", | |
"print(d.items())\n", | |
"\n", | |
"#keys\n", | |
"print(d.keys())\n", | |
"\n", | |
"#values\n", | |
"print(d.values())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 72, | |
"id": "dfbf5bc1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 72, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#Line Continuation\n", | |
"person1_age = 42\n", | |
"person2_age = 16\n", | |
"person3_age = 71\n", | |
"\n", | |
"someone_is_of_working_age = (person1_age >= 18 and person1_age <= 65) or (person2_age >= 18 and person2_age <= 65) or (person3_age >= 18 and person3_age <= 65)\n", | |
"someone_is_of_working_age" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"id": "1485d8d5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 73, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"someone_is_of_working_age = (\n", | |
" (person1_age >= 18 and person1_age <= 65) \n", | |
" or (person2_age >= 18 and person2_age <= 65) \n", | |
" or (person3_age >= 18 and person3_age <= 65)\n", | |
")\n", | |
"someone_is_of_working_age" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ea4647e6", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.8.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment