Skip to content

Instantly share code, notes, and snippets.

@mde-2590
Created April 5, 2019 00:45
Show Gist options
  • Save mde-2590/057b132e3f521ab14a98a6c090d0a4f5 to your computer and use it in GitHub Desktop.
Save mde-2590/057b132e3f521ab14a98a6c090d0a4f5 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a list\n",
"\n",
"L = [\"Michael Jackson\", 10.1, 1982]\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the same element using negative and positive indexing:\n",
" Postive: Michael Jackson \n",
" Negative: Michael Jackson\n",
"the same element using negative and positive indexing:\n",
" Postive: 10.1 \n",
" Negative: 10.1\n",
"the same element using negative and positive indexing:\n",
" Postive: 1982 \n",
" Negative: 1982\n"
]
}
],
"source": [
"# Print the elements on each index\n",
"\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[0],\n",
"'\\n Negative:' , L[-3] )\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[1],\n",
"'\\n Negative:' , L[-2] )\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[2],\n",
"'\\n Negative:' , L[-1] )"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a list\n",
"\n",
"L = [\"Michael Jackson\", 10.1, 1982]\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'L' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-d7bfeec8d475>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Print the elements on each index\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m print('the same element using negative and positive indexing:\\n Postive:',L[0],\n\u001b[0m\u001b[1;32m 4\u001b[0m '\\n Negative:' , L[-3] )\n\u001b[1;32m 5\u001b[0m print('the same element using negative and positive indexing:\\n Postive:',L[1],\n",
"\u001b[0;31mNameError\u001b[0m: name 'L' is not defined"
]
}
],
"source": [
"# Print the elements on each index\n",
"\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[0],\n",
"'\\n Negative:' , L[-3] )\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[1],\n",
"'\\n Negative:' , L[-2] )\n",
"print('the same element using negative and positive indexing:\\n Postive:',L[2],\n",
"'\\n Negative:' , L[-1] )"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982, [1, 2], ('A', 1)]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample List\n",
"\n",
"[\"Michael Jackson\", 10.1, 1982, [1, 2], (\"A\", 1)]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.1, 1982, 'MJ', 1]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample List\n",
"\n",
"L = [\"Michael Jackson\", 10.1,1982,\"MJ\",1]\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['MJ', 1]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# List slicing\n",
"\n",
"L[3:5]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.2, 'pop', 10]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use extend to add elements to list\n",
"\n",
"L = [ \"Michael Jackson\", 10.2]\n",
"L.extend(['pop', 10])\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.2, ['pop', 10]]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use append to add elements to list\n",
"\n",
"L = [ \"Michael Jackson\", 10.2]\n",
"L.append(['pop', 10])\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Michael Jackson', 10.2, ['pop', 10], ['a', 'b']]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use append to add elements to list\n",
"\n",
"L.append(['a','b'])\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before change: ['disco', 10, 1.2]\n",
"After change: ['hard rock', 10, 1.2]\n"
]
}
],
"source": [
"# Change the element based on the index\n",
"\n",
"A = [\"disco\", 10, 1.2]\n",
"print('Before change:', A)\n",
"A[0] = 'hard rock'\n",
"print('After change:', A)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before change: ['hard rock', 10, 1.2]\n",
"After change: [10, 1.2]\n"
]
}
],
"source": [
"# Delete the element based on the index\n",
"\n",
"print('Before change:', A)\n",
"del(A[0])\n",
"print('After change:', A)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hard', 'rock']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Split the string, default is by space\n",
"\n",
"'hard rock'.split()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A', 'B', 'C', 'D']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Split the string by comma\n",
"\n",
"'A,B,C,D'.split(',')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A: ['hard rock', 10, 1.2]\n",
"B: ['hard rock', 10, 1.2]\n"
]
}
],
"source": [
"# Copy (copy by reference) the list A\n",
"\n",
"A = [\"hard rock\", 10, 1.2]\n",
"B = A\n",
"print('A:', A)\n",
"print('B:', B)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B[0]: hard rock\n",
"B[0]: banana\n"
]
}
],
"source": [
"# Examine the copy by reference\n",
"\n",
"print('B[0]:', B[0])\n",
"A[0] = \"banana\"\n",
"print('B[0]:', B[0])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['banana', 10, 1.2]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Clone (clone by value) the list A\n",
"\n",
"B = A[:]\n",
"B\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"B[0]: banana\n",
"B[0]: banana\n"
]
}
],
"source": [
"print('B[0]:', B[0])\n",
"A[0] = \"hard rock\"\n",
"print('B[0]:', B[0])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 'hello', [1, 2, 3], True]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"a_list = [1, 'hello', [1, 2, 3] , True]\n",
"a_list"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"a_list[1]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['hello', [1, 2, 3], True]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"a_list[1:4]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 'a', 2, 1, 'd']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"A = [1, 'a'] \n",
"B = [2, 1, 'd']\n",
"A + B"
]
}
],
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment