Skip to content

Commit 48be3d5

Browse files
committed
Add code for generating lists of even and odd numbers without using loops
1 parent c80a7f0 commit 48be3d5

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

Python_Class.ipynb

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,113 @@
13511351
" ls.remove(9)\n",
13521352
"print(ls)"
13531353
]
1354+
},
1355+
{
1356+
"cell_type": "code",
1357+
"execution_count": 3,
1358+
"metadata": {},
1359+
"outputs": [
1360+
{
1361+
"data": {
1362+
"text/plain": [
1363+
"[6, 5, 4, 3]"
1364+
]
1365+
},
1366+
"execution_count": 3,
1367+
"metadata": {},
1368+
"output_type": "execute_result"
1369+
}
1370+
],
1371+
"source": [
1372+
"lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
1373+
"lst[5:1:-1]\n"
1374+
]
1375+
},
1376+
{
1377+
"cell_type": "code",
1378+
"execution_count": 2,
1379+
"metadata": {},
1380+
"outputs": [
1381+
{
1382+
"data": {
1383+
"text/plain": [
1384+
"[3, 4, 5, 6]"
1385+
]
1386+
},
1387+
"execution_count": 2,
1388+
"metadata": {},
1389+
"output_type": "execute_result"
1390+
}
1391+
],
1392+
"source": [
1393+
"lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
1394+
"lst[2:6]"
1395+
]
1396+
},
1397+
{
1398+
"cell_type": "code",
1399+
"execution_count": 6,
1400+
"metadata": {},
1401+
"outputs": [
1402+
{
1403+
"data": {
1404+
"text/plain": [
1405+
"[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]"
1406+
]
1407+
},
1408+
"execution_count": 6,
1409+
"metadata": {},
1410+
"output_type": "execute_result"
1411+
}
1412+
],
1413+
"source": [
1414+
"lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
1415+
"lst[::-1]"
1416+
]
1417+
},
1418+
{
1419+
"cell_type": "markdown",
1420+
"metadata": {},
1421+
"source": [
1422+
"\n",
1423+
"generate list of 0-20\n",
1424+
"generate another list were all even numbers are in ascending order \n",
1425+
"generate another list from the parent list were all odd numbers are in descending order\n",
1426+
"CANNOT USE LOOPS\n",
1427+
"Use splicing to make the lists except the list of 0-20\n"
1428+
]
1429+
},
1430+
{
1431+
"cell_type": "code",
1432+
"execution_count": 12,
1433+
"metadata": {},
1434+
"outputs": [
1435+
{
1436+
"name": "stdout",
1437+
"output_type": "stream",
1438+
"text": [
1439+
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n",
1440+
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
1441+
"[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]\n",
1442+
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]\n"
1443+
]
1444+
}
1445+
],
1446+
"source": [
1447+
"\n",
1448+
"numbers = list(range(21))\n",
1449+
"evens = numbers[::2]\n",
1450+
"odds = numbers[20:0:-2]\n",
1451+
"\n",
1452+
"final_list = evens + odds\n",
1453+
"\n",
1454+
"\n",
1455+
"\n",
1456+
"print(numbers)\n",
1457+
"print(evens)\n",
1458+
"print(odds)\n",
1459+
"print(final_list)\n"
1460+
]
13541461
}
13551462
],
13561463
"metadata": {

0 commit comments

Comments
 (0)