Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Array Ipynb

The document discusses using lists in Python. It shows how to create lists, access elements by index, slice lists, use built-in list functions like len() and append(), iterate through lists using for loops, create and access 2D lists, and check if an element is in a list. Examples are provided for each concept using sample code cells and output.

Uploaded by

Erwin Tampubolon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Array Ipynb

The document discusses using lists in Python. It shows how to create lists, access elements by index, slice lists, use built-in list functions like len() and append(), iterate through lists using for loops, create and access 2D lists, and check if an element is in a list. Examples are provided for each concept using sample code cells and output.

Uploaded by

Erwin Tampubolon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

{

"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#array menggunkan list\n",
"a = []\n",
"b =
[\"senin\",\"selasa\",\"rabu\",\"kamis\",\"jumat\",\"sabtu\",\"minggu\
"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"senin\n",
"selasa\n",
"rabu\n",
"kamis\n",
"jumat\n"
]
}
],
"source": [
"#menampilkan data setiap elemen dg index\n",
"a = []\n",
"b =
[\"senin\",\"selasa\",\"rabu\",\"kamis\",\"jumat\",\"sabtu\",\"minggu\
"]\n",
"print(b[0])\n",
"print(b[1])\n",
"print(b[2])\n",
"print(b[3])\n",
"print(b[4])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"minggu\n",
"sabtu\n",
"selasa\n"
]
}
],
"source": [
"#menampilkan data setiap elemen dg negatif\n",
"a = []\n",
"b =
[\"senin\",\"selasa\",\"rabu\",\"kamis\",\"jumat\",\"sabtu\",\"minggu\
"]\n",
"print(b[-1])\n",
"print(b[-2])\n",
"print(b[-6])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"senin\n",
"selasa\n",
"rabu\n",
"kamis\n",
"jumat\n",
"sabtu\n",
"minggu\n"
]
}
],
"source": [
"#menampilkan data dengan perulanagan index\n",
"b =
[\"senin\",\"selasa\",\"rabu\",\"kamis\",\"jumat\",\"sabtu\",\"minggu\
"]\n",
"for i in range (7):\n",
" print(b[i])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"senin\n",
"selasa\n",
"rabu\n",
"kamis\n",
"jumat\n",
"sabtu\n",
"minggu\n"
]
}
],
"source": [
"#menampilkan data dengan perulangan \n",
"b =
[\"senin\",\"selasa\",\"rabu\",\"kamis\",\"jumat\",\"sabtu\",\"minggu\
"]\n",
"for i in b :\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 4, 5, 6]\n"
]
}
],
"source": [
"#memotong ArraY list\n",
"a = [1,2,3,4,5,6,7,8,9,10]\n",
"print(a[2:6])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd', 'e']\n"
]
}
],
"source": [
"a = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\"]\n",
"print(a[:5])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['d', 'e', 'f', 'g']\n"
]
}
],
"source": [
"print(a[3:])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['d', 'e', 'f']\n"
]
}
],
"source": [
"print(a[-4:-1])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"#FungsibuiltinList\n",
"b = [\"senin\", \"selasa\", \"rabu\", \"kamis\", \"jumat\",
\"sabtu\", \"minggu\"]\n",
"print(len(b))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu',
'minggu', 'libur', 'libur']\n"
]
}
],
"source": [
"b.append(\"libur\")\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['senin', 'selasa', 'rabu', 'random', 'kamis', 'jumat',
'sabtu', 'minggu', 'libur', 'libur']\n"
]
}
],
"source": [
"b.insert(3, \"random\")\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu',
'minggu', 'libur', 'libur']\n"
]
}
],
"source": [
"b.remove(\"random\")\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu',
'minggu', 'libur']\n"
]
}
],
"source": [
"del b[7]\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hari senin ada\n"
]
}
],
"source": [
"if \"senin\" in b:\n",
" print(\"hari senin ada\")\n",
"else:\n",
" print(\"maaf hari belum terdaftar\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n",
"17\n"
]
}
],
"source": [
"#MenggunakanList\n",
"\n",
"#menampilkan data setiap elemen dg menunjuk index 2D\n",
"\n",
"a = [[11,12,13],\n",
" [14,15,16],\n",
" [17,18,19]]\n",
"\n",
"print(a[1][2])\n",
"print(a[2][0])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11 12 13 \n",
"14 15 16 \n",
"17 18 19 \n"
]
}
],
"source": [
"#menampilkan data setiap elemen dg perulangan menunjuk index
2D\n",
"for i in range (3):\n",
" for j in range(3):\n",
" print(a[i][j], end=' ')\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11 12 13 \n",
"14 15 16 \n",
"17 18 19 \n"
]
}
],
"source": [
"#menampilkan data setiap elemen dg perulangan tanpa index\n",
"for i in a:\n",
" for j in i:\n",
" print(j, end = ' ')\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

You might also like