Skip to content

Instantly share code, notes, and snippets.

@c4t4r
Created February 23, 2024 03:34
Show Gist options
  • Save c4t4r/601d7562d808d2671a6cfdb6152fb510 to your computer and use it in GitHub Desktop.
Save c4t4r/601d7562d808d2671a6cfdb6152fb510 to your computer and use it in GitHub Desktop.
Mastering Python Science
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"id": "a64fbbe7-d193-4392-a7a7-4fe20bae05b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#fff\n"
]
}
],
"source": [
"x = [ 1 , 'Hello', True, 1.093E-3 , '#fff']\n",
"print(x[-1])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8ad9a197-a29e-4a74-bed3-cc7676885c1e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"1\n",
"4\n",
"5\n"
]
}
],
"source": [
"x = [9, 3, 4, 1, 4, 5, 6, 3, 7, 8]\n",
"for i in x[2:6]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "a7b50498-e1a8-467f-9dda-13512ba32950",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
" 2\n",
" 3\n",
" 4\n",
" 5\n",
" 6\n",
" 7\n",
"8\n"
]
}
],
"source": [
"x = [1, [2, [3]], [4, [5, [6]]], [7], 8]\n",
"for i in x:\n",
" if isinstance (i, list):\n",
" for j in i:\n",
" if isinstance (j, list):\n",
" for k in j:\n",
" if isinstance (k, list):\n",
" for l in k:\n",
" print(' ', l)\n",
" else:\n",
" print(' ', k)\n",
" else:\n",
" print(' ', j)\n",
" else:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "667e1383-e8b3-4672-a638-33c785b4d57e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 4, 5, 'a', 2, 'b', 'c']\n"
]
}
],
"source": [
"x = [1, 2, 3, 4, 5]\n",
"y = ['a', 'b', 'c']\n",
"z = x.pop(1)\n",
"y.insert(1, z)\n",
"z = x + y\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "95f3988f-89d4-4b79-b215-07a08d0845c4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 2000, 'i', 'o', 'u']\n",
"['a', 'e', 'i', 'o', 'u']\n"
]
}
],
"source": [
"a = 'aeiou'\n",
"b = tuple(a)\n",
"c = list(b)\n",
"d = c\n",
"e = list(d)\n",
"\n",
"c[1] = 2000\n",
"print(d)\n",
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "f68c4b67-3d64-4c32-baf3-f09d3cdb83a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 3, 4, 5]\n",
"[2, 4, 3, 1, 0, 5]\n",
"[5, 4, 3, 2, 1, 0]\n"
]
}
],
"source": [
"x = [5, 0, 1, 3, 4, 2]\n",
"a = x.copy()\n",
"a.sort()\n",
"print(a)\n",
"b = x.copy()\n",
"b.reverse()\n",
"print(b)\n",
"c = x.copy()\n",
"c.sort(reverse=True)\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "8a2f58ef-2351-400a-82f0-9b1748ae8d3d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"count of 1: 4\n",
"\n",
"index of 1: 1\n",
"index of 1: 4\n",
"index of 1: 8\n",
"index of 1: 11\n"
]
}
],
"source": [
"x = [0, 1, 2, 3, 1, 4, 5, 6, 1, 9, 9, 1]\n",
"y = x.copy()\n",
"print('count of 1: ', y.count(1))\n",
"print()\n",
"for i in range(y.count(1)):\n",
" print('index of 1: ', (y.index(1) + i))\n",
" y.pop(y.index(1))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "fe14f230-9fbc-4bce-9e3b-7589f856e53a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 2, 4]\n",
"[1, 3, 5]\n",
"[1, 3, 5, 3, 5, 7, 5, 7, 9]\n"
]
}
],
"source": [
"x = [i for i in range (0, 5, 2)]\n",
"y = [i for i in range (1, 6, 2)]\n",
"z = [i+j for i in x for j in y]\n",
"print(x)\n",
"print(y)\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d34ffea2-4f71-416f-9a5a-241fac298712",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"original\n",
"[1, 5, 2, 4, 3] \n",
"\n",
"after bubble sort\n",
"[5, 4, 3, 2, 1]\n"
]
}
],
"source": [
"x = [1, 5, 2, 4, 3]\n",
"print('original'); print(x, '\\n')\n",
"N = len(x)\n",
"for i in range(N):\n",
" for j in range(i+1, N):\n",
" if x[i] < x[j]:\n",
" x[i], x[j] = x[j], x[i]\n",
"print('after bubble sort'); print(x)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a327b852-70e0-4f2d-93fb-a583e2bf1e1c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"range(0, 6)\n",
"['a', 1, 'b', 2, 'c', 3] \n",
"\n",
"['a', 'b', 'c'] \n",
"\n",
"[1, 2, 3]\n"
]
}
],
"source": [
"def sep_odd_even_index(z):\n",
" N = len(z)\n",
" r = range(N)\n",
" x = [z[i] for i in r if i % 2 == 0]\n",
" y = [z[i] for i in r if i % 2 == 1]\n",
" return[x,y]\n",
"\n",
"mix = ['a', 1, 'b', 2, 'c', 3]\n",
"x, y = sep_odd_even_index(mix)\n",
"print(mix, '\\n')\n",
"print(x, '\\n')\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"id": "db76595f-a5ea-499b-8868-fcc49d6402d4",
"metadata": {},
"source": [
"\"sep_odd_even_index\" is use to use separate the odd or even elements from the list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69d28d50-bdd4-4989-b0dc-995db77b7a3d",
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment