Files
python-intermediate/intermediate_course_repl_playaround.ipynb
T

1982 lines
37 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "c1591e66",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1\n",
"SyntaxError: not a chance\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from __future__ import braces"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a379dd3",
"metadata": {},
"outputs": [],
"source": [
"import antigravity"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b78f69e1",
"metadata": {},
"outputs": [],
"source": [
"a = \"Aman, Nalakath\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb764ea9",
"metadata": {},
"outputs": [],
"source": [
"first_name, last_name = a.split(\",\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52be2c69",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
" Nalakath\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"last_name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2db83cae",
"metadata": {},
"outputs": [],
"source": [
"name_raw = a.split(\",\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c8193d3",
"metadata": {},
"outputs": [],
"source": [
"full_name = (\",\").join(name_raw)\n",
"full_name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55ba7954",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Aman, Nalakath\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"full_name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d28a3343",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Aman', ' Nalakath']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"name_raw"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c2710bd",
"metadata": {},
"outputs": [],
"source": [
"full_name_with_an_x = (\"_x \").join(name_raw)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4b897ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Aman_x Nalakath\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"full_name_with_an_x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8854767",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"123\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"int(\"123\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "636dbf8d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a', 'm', 'a', 'n']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list(\"aman\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4fbeafe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"aman\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\"\".join(list(\"aman\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f33ca728",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'str'>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type(\"\".join(list(\"aman\")))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f225cf85",
"metadata": {},
"outputs": [],
"source": [
"names = \"aman, shamil, aman, nalakath\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a52dc0ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'i', ' ', 'a', 'k', 'm', 'n', 's', 'h', 'l', 't', ','}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"set(names)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "981e5cc7",
"metadata": {},
"outputs": [],
"source": [
"names = [\"aman\", \"shamil\", \"aman\", \"nalakath\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99b6b65d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'aman', 'nalakath', 'shamil'}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"set(names)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d82c84c5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['aman', 'nalakath', 'shamil']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list(set(names))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90a0e3d0",
"metadata": {},
"outputs": [],
"source": [
"numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b04aa3f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4\n",
"16\n",
"36\n",
"64\n",
"100\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for num in numbers:\n",
" if num % 2 == 0:\n",
" print(num ** 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bc8c2bf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4\n",
"16\n",
"36\n",
"64\n",
"100\n",
"[None, None, None, None, None]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[print(num ** 2) for num in numbers if num % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42865d44",
"metadata": {},
"outputs": [],
"source": [
"n_a = [(num ** 2) for num in numbers if num % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3934de5c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 16, 36, 64, 100]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(n_a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652aa601",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 10, 2)\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"range(0, 10, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d10ebe62",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list(range(0, 10, 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d59f0b1e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['length: len(i)', 'length: len(i)', 'length: len(i)']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[\"length: len(i)\" for i in list(set(names))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9b3a3f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['length: len(i)', 'length: len(i)', 'length: len(i)']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[f\"length: len(i)\" for i in list(set(names))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4be7347",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['aman', 'nalakath', 'shamil']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list(set(names))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7df92f72",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'list'>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type(list(set(names)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b2afa7b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1\n",
" [(\"length\": len(name)) for name in list(set(names))]\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[(\"length\": len(name)) for name in list(set(names))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd4ce891",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1\n",
" [(\"length:\" len(name)) for name in list(set(names))]\n",
" ^^^^^^^^^^^^^^^^^^^\n",
"SyntaxError: invalid syntax. Perhaps you forgot a comma?\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[(\"length:\" len(name)) for name in list(set(names))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47710a40",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('length:', 4), ('length:', 8), ('length:', 6)]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"[(\"length:\",len(name)) for name in list(set(names))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fefaed11",
"metadata": {},
"outputs": [],
"source": [
"n_a = [(num ** 2) for num in numbers if num % 2 == 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74b072fa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 16, 36, 64, 100]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"n_a = [(num ** 2) for num in numbers if num % 2 == 0]\n",
"print(n_a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31dc6e51",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 9, 25, 49, 81]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"n_b = [(num ** 2) for num in numbers if bool(num % 2)]\n",
"print(n_b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa7ee3a2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[100, 64, 36, 16, 4]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"n_c = sorted([(num ** 2) for num in numbers if num % 2 == 0], reverse=True)\n",
"print(n_c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28892a59",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2304, 2116, 1936, 1764, 1600, 1444, 1296, 1156, 1024, 900, 784, 676, 576, 484, 400, 324, 256, 196, 144, 100, 64, 36, 16, 4, 0]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"n_c = sorted([(num ** 2) for num in range(0, 50) if num % 2 == 0], reverse=True)\n",
"print(n_c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "224a8fa2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1\n",
" type{1}\n",
" ^\n",
"SyntaxError: invalid syntax\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type{1}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0434d00d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'set'>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type({1})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "644014bb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'dict'>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type({1:1})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73f976c5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea116a64",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"numbers[::-1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5d4b44df",
"metadata": {},
"outputs": [],
"source": [
"names = [\"Bob\", \"Alice\", \"Eve\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ec22549",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Bob': 10, 'Alice': 9, 'Eve': 8}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"score_dict = dict(zip(names, numbers[::-1]))\n",
"print(score_dict)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "113172b4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1, in <module>\n",
"TypeError: type 'range' is not subscriptable\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"(num for num in range[0:100])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7cdee127",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<generator object <genexpr> at 0x100e96e90>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"(num*2 for num in range(0, 100))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad62a1a1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next((num*2 for num in range(0, 100)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10151a13",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next((num*2 for num in range(0, 100)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de5fe793",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next((num*2 for num in range(0, 100)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b75ca4a5",
"metadata": {},
"outputs": [],
"source": [
"next((num*2 for num in range(0, 100)))\n",
"next((num*2 for num in range(0, 100)))\n",
"next((num*2 for num in range(0, 100)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de2cf784",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(next((num*2 for num in range(0, 100))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9447d91",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(next((num*2 for num in range(0, 100))))\n",
"print(next((num*2 for num in range(0, 100))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fb0b22d",
"metadata": {},
"outputs": [],
"source": [
"times_two = (num*2 for num in range(0, 100))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34caa341",
"metadata": {},
"outputs": [],
"source": [
"next(times_two)\n",
"next(times_two)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e06e428",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next(times_two)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c8fa9c5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next(times_two)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57db5d9b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next(times_two)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09ad8281",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"next(times_two, \"stopped\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57de4dd4",
"metadata": {},
"outputs": [],
"source": [
"names = [\"Alice\", \"Bob\", \"Charlie\", \"Diana\"]\n",
"ages = [28, 34, 22, 41]\n",
"cities = [\"Berlin\", \"Paris\", \"Tokyo\", \"London\"]\n",
"scores = [88, 95, 72, 90]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d88aa70",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1\n",
" name, age, city, score = [for name, age, city, score in zip(names, ages, cities, scores)]\n",
" ^^^\n",
"SyntaxError: invalid syntax\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"name, age, city, score = [for name, age, city, score in zip(names, ages, cities, scores)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a881d34",
"metadata": {},
"outputs": [],
"source": [
"name, age, city, score = [f\"{name}, age {age}, from {city}, scored {score}\" for name, age, city, score in zip(names, ages, cities, scores)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf77aa88",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Alice, age 28, from Berlin, scored 88 Bob, age 34, from Paris, scored 95 Charlie, age 22, from Tokyo, scored 72 Diana, age 41, from London, scored 90\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(name, age, city, score)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57126452",
"metadata": {},
"outputs": [],
"source": [
"name, age, city, score = [f\"{name}, age {age}, from {city}, scored {score}\\n\" for name, age, city, score in zip(names, ages, cities, scores)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86cddf75",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Alice, age 28, from Berlin, scored 88\n",
" Bob, age 34, from Paris, scored 95\n",
" Charlie, age 22, from Tokyo, scored 72\n",
" Diana, age 41, from London, scored 90\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(name, age, city, score)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4cd285b",
"metadata": {},
"outputs": [],
"source": [
"name, age, city, score = [f\"{name} age {age} from {city} scored {score}\\n\" for name, age, city, score in zip(names, ages, cities, scores)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88365f45",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Alice age 28 from Berlin scored 88\n",
" Bob age 34 from Paris scored 95\n",
" Charlie age 22 from Tokyo scored 72\n",
" Diana age 41 from London scored 90\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(name, age, city, score)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38f6d821",
"metadata": {},
"outputs": [],
"source": [
"new_list = range(20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5630f790",
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d58cfde2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice age 28 from Berlin scored 88\\n': 23}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"{name: random.randint(20, 40)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa064f63",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Bob age 34 from Paris scored 95\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"age"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a71e036",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Charlie age 22 from Tokyo scored 72\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"city"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56680f75",
"metadata": {},
"outputs": [],
"source": [
"names = [\"Alice\", \"Bob\", \"Charlie\", \"Diana\"]\n",
"ages = [28, 34, 22, 41]\n",
"cities = [\"Berlin\", \"Paris\", \"Tokyo\", \"London\"]\n",
"scores = [88, 95, 72, 90]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "142e2410",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice age 28 from Berlin scored 88\\n': 32}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"{name: random.randint(20, 40)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c767862c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1, in <module>\n",
"TypeError: unhashable type: 'list'\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"{names: random.randint(20, 40)}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "762c54a7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Alice': 37, 'Bob': 24, 'Charlie': 27, 'Diana': 31}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"{name: random.randint(20, 40) for name in names}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78b56fc4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"True + True"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3689b8c4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 4}\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"{1, 4, True, True, 0, False}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "222d2997",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1, in <module>\n",
"TypeError: Exception() takes no keyword arguments\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"raise Exception(message = \"trying this out, don't mind me\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d7851f5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 1, in <module>\n",
"Exception: trying this out, don't mind me\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"raise Exception(\"trying this out, don't mind me\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6be71a65",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" int(\"this is not a number\")\n",
"\n",
"except ValueError as e:\n",
" f\"Damn you got a {e}, be more careful, it is not a number. check again\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c240fde",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Damn you got a invalid literal for int() with base 10: 'this is not a number', be more careful, it is not a number. check again\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"try:\n",
" int(\"this is not a number\")\n",
"\n",
"except ValueError as e:\n",
" print(f\"Damn you got a {e}, be more careful, it is not a number. check again\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e40dfe1c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"list(range(10,-1,-1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d88fdd5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in list(range(10,-1,-1)):\n",
" time.sleep(1)\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bab5f481",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 2, in <module>\n",
"TypeError: 'range' object is not an iterator\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in next(range(10,-1,-1)):\n",
" time.sleep(1)\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e16aecc0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Traceback (most recent call last):\n",
" File \"/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py\", line 139, in exec_user_input\n",
" retval = callable_(user_input, user_globals)\n",
" File \"<string>\", line 2, in <module>\n",
"TypeError: 'list' object is not an iterator\n",
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in next(list(range(10,-1,-1))):\n",
" time.sleep(1)\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "318288c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in range(10,-1,-1):\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8705c132",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in range(10,-1,-1):\n",
" print(item)\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9884254",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in range(10, -1, -1):\n",
" print(item)\n",
" if item > 0:\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "423b786a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in range(10,-1,-1):\n",
" print(item, flush=True)\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac8150b7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10\n",
"9\n",
"8\n",
"7\n",
"6\n",
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"0\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"for item in range(10, -1, -1):\n",
" print(item, flush=True)\n",
" time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3440af8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-- <DirEntry 'home'>\n",
"-- <DirEntry 'usr'>\n",
"-- <DirEntry '.resolve'>\n",
"-- <DirEntry 'bin'>\n",
"-- <DirEntry 'sbin'>\n",
"-- <DirEntry '.file'>\n",
"-- <DirEntry 'etc'>\n",
"-- <DirEntry 'var'>\n",
"-- <DirEntry 'Library'>\n",
"-- <DirEntry 'System'>\n",
"-- <DirEntry '.VolumeIcon.icns'>\n",
"-- <DirEntry 'private'>\n",
"-- <DirEntry '.vol'>\n",
"-- <DirEntry 'Users'>\n",
"-- <DirEntry 'Applications'>\n",
"-- <DirEntry 'opt'>\n",
"-- <DirEntry 'dev'>\n",
"-- <DirEntry 'Volumes'>\n",
"-- <DirEntry '.nofollow'>\n",
"-- <DirEntry 'tmp'>\n",
"-- <DirEntry 'cores'>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import os\n",
"with os.scandir(os.getcwd()) as folder:\n",
" for entry in folder:\n",
" print(f\"-- {entry}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c281b76",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-- home\n",
"-- usr\n",
"-- .resolve\n",
"-- bin\n",
"-- sbin\n",
"-- .file\n",
"-- etc\n",
"-- var\n",
"-- Library\n",
"-- System\n",
"-- .VolumeIcon.icns\n",
"-- private\n",
"-- .vol\n",
"-- Users\n",
"-- Applications\n",
"-- opt\n",
"-- dev\n",
"-- Volumes\n",
"-- .nofollow\n",
"-- tmp\n",
"-- cores\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import os\n",
"with os.scandir(os.getcwd()) as folder:\n",
" for entry in folder:\n",
" print(f\"-- {entry.name}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c263640",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.platform"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b30a6c8d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"darwin\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import sys\n",
"print(sys.platform)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd3c4f77",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"working with these arguments ['/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py']\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(f\"working with these arguments {sys.argv}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68090701",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<class 'list'>\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type(sys.argv)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2e39db1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}