Skip to content

Instantly share code, notes, and snippets.

@CyberianRonin
Created February 19, 2024 04:14
Show Gist options
  • Save CyberianRonin/7aa626aec55180e3e5500e123186df93 to your computer and use it in GitHub Desktop.
Save CyberianRonin/7aa626aec55180e3e5500e123186df93 to your computer and use it in GitHub Desktop.
Python List
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "674b6e44",
"metadata": {},
"outputs": [],
"source": [
"x = [1,'Hello', True, 1.093E-3, '#fff']"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "85b32fb3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#fff\n"
]
}
],
"source": [
"print(x[-1])\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "29d0dc89",
"metadata": {},
"outputs": [],
"source": [
"x = [9, 3, 4, 1, 4, 5, 6, 3, 7, 8]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "aa6ad5e3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n",
"1\n",
"4\n",
"5\n"
]
}
],
"source": [
"for i in x[2:6]:\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f0c8e91f",
"metadata": {},
"outputs": [],
"source": [
"x = [1, 2, 3, 4, 5]\n",
"y = ['a', 'b', 'c']\n",
"z = x.pop(1)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ef771edd",
"metadata": {},
"outputs": [],
"source": [
"y.insert(1, z)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "8fb327be",
"metadata": {},
"outputs": [],
"source": [
"z = x + y"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "574a2c8f",
"metadata": {},
"outputs": [],
"source": [
"x = [1, 3, 4, 5]\n",
"y = ['a', 2, 'b', 'c']\n",
"z = [1, 3, 4, 5, 'a', 2, 'b', 'c']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d327f13a",
"metadata": {},
"outputs": [],
"source": [
"a = 'aiueo'\n",
"b = tuple(a)\n",
"c = list (b)\n",
"d = c\n",
"e = list (d)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "2cefc7b7",
"metadata": {},
"outputs": [],
"source": [
"c[1] = 2000"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "6bd70cba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 2000, 'u', 'e', 'o']\n"
]
}
],
"source": [
"print(d)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "1ebf4f1b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'i', 'u', 'e', 'o']\n"
]
}
],
"source": [
"print(e)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "3c6ecf6c",
"metadata": {},
"outputs": [],
"source": [
"result = sorted([5, 0, 1, 3, 4, 2])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9f872686",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"count of 1: 4\n",
"\n",
"1st index of 1: 1\n",
"\n",
"2nd index of 1: 2\n"
]
}
],
"source": [
"x = [0, 1, 2, 3, 1, 4, 5, 6, 1, 9, 9, 1]\n",
"print('count of 1:', x.count(1))\n",
"print()\n",
"\n",
"c0 = x.index(1)\n",
"print('1st index of 1:', c0)\n",
"print()\n",
"\n",
"c1 = x[c0 + 1:].index(1)\n",
"print('2nd index of 1:', c1)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "73d52525",
"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",
"\n",
"print (x)\n",
"print (y)\n",
"print (z)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2d419136",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"original\n",
"[1, 5, 2, 4, 3] \n",
"\n",
"after bubble sort\n",
"[1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"x = [1, 5, 2, 4, 3]\n",
"print('original'); print(x, '\\n')\n",
"\n",
"N = len(x)\n",
"\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",
" \n",
"print('after bubble sort'); print(x)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "491d22ab",
"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",
"N = len(x)\n",
"\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",
" \n",
"print('after bubble sort'); print(x)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "97ea3e94",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 1, 'b', 2, 'c', ['a', 2000, 'u', 'e', 'o']] \n",
"\n",
"[1, 2, ['a', 2000, 'u', 'e', 'o']]\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',d]\n",
"x , y = sep_odd_even_index(mix)\n",
"print(mix, '\\n')\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9f54de3",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment