Forked from GuillaumeRouja/Python 3 : Tuples & lists.ipynb
Created
January 17, 2019 21:23
-
-
Save binarygalwalkin/8d41da360e008e2693768ad24f5812b9 to your computer and use it in GitHub Desktop.
Tuples & lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat_minor": 1, | |
"cells": [ | |
{ | |
"execution_count": 1, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 1, | |
"metadata": {}, | |
"data": { | |
"text/plain": "('disco', 10, 1.2)" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Create your first tuple\n\ntuple1 = (\"disco\",10,1.2 )\ntuple1" | |
}, | |
{ | |
"execution_count": 2, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "disco\n10\n1.2\n" | |
} | |
], | |
"source": "# Print the variable on each index\n\nprint(tuple1[0])\nprint(tuple1[1])\nprint(tuple1[2])" | |
}, | |
{ | |
"execution_count": 3, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "<class 'str'>\n<class 'int'>\n<class 'float'>\n" | |
} | |
], | |
"source": "\nprint(type(tuple1[0]))\nprint(type(tuple1[1]))\nprint(type(tuple1[2]))" | |
}, | |
{ | |
"execution_count": 4, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 4, | |
"metadata": {}, | |
"data": { | |
"text/plain": "1.2" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Use negative index to get the value of the last element\n\ntuple1[-1]" | |
}, | |
{ | |
"execution_count": 5, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 5, | |
"metadata": {}, | |
"data": { | |
"text/plain": "('disco', 10, 1.2, 'hard rock', 10)" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Concatenate two tuples\n\ntuple2 = tuple1 + (\"hard rock\", 10)\ntuple2" | |
}, | |
{ | |
"execution_count": 6, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 6, | |
"metadata": {}, | |
"data": { | |
"text/plain": "('disco', 10, 1.2)" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Slice from index 0 to index 2\n\ntuple2[0:3]" | |
}, | |
{ | |
"execution_count": 7, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 7, | |
"metadata": {}, | |
"data": { | |
"text/plain": "('hard rock', 10)" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Slice from index 3 to index 4\n\ntuple2[3:5]" | |
}, | |
{ | |
"execution_count": 8, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 8, | |
"metadata": {}, | |
"data": { | |
"text/plain": "5" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Get the length of tuple\n\nlen(tuple2)" | |
}, | |
{ | |
"execution_count": 9, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [], | |
"source": "# A sample tuple\n\nRatings = (0, 9, 6, 5, 10, 8, 9, 6, 2)" | |
}, | |
{ | |
"execution_count": 10, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 10, | |
"metadata": {}, | |
"data": { | |
"text/plain": "[0, 2, 5, 6, 6, 8, 9, 9, 10]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Sort the tuple\n\nRatingsSorted = sorted(Ratings)\nRatingsSorted" | |
}, | |
{ | |
"execution_count": 11, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [], | |
"source": "# Create a nest tuple\n\nNestedT =(1, 2, (\"pop\", \"rock\") ,(3,4),(\"disco\",(1,2)))" | |
}, | |
{ | |
"execution_count": 12, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "Element 2, 0 of Tuple: pop\nElement 2, 1 of Tuple: rock\nElement 3, 0 of Tuple: 3\nElement 3, 1 of Tuple: 4\nElement 4, 0 of Tuple: disco\nElement 4, 1 of Tuple: (1, 2)\n" | |
} | |
], | |
"source": "# Print element on each index, including nest indexes\n\nprint(\"Element 2, 0 of Tuple: \", NestedT[2][0])\nprint(\"Element 2, 1 of Tuple: \", NestedT[2][1])\nprint(\"Element 3, 0 of Tuple: \", NestedT[3][0])\nprint(\"Element 3, 1 of Tuple: \", NestedT[3][1])\nprint(\"Element 4, 0 of Tuple: \", NestedT[4][0])\nprint(\"Element 4, 1 of Tuple: \", NestedT[4][1])" | |
}, | |
{ | |
"execution_count": 13, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 13, | |
"metadata": {}, | |
"data": { | |
"text/plain": "'r'" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Print the first element in the second nested tuples\n\nNestedT[2][1][0]" | |
}, | |
{ | |
"execution_count": 14, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 14, | |
"metadata": {}, | |
"data": { | |
"text/plain": "('pop',\n 'rock',\n 'soul',\n 'hard rock',\n 'soft rock',\n 'R&B',\n 'progressive rock',\n 'disco')" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# sample tuple\n\ngenres_tuple = (\"pop\", \"rock\", \"soul\", \"hard rock\", \"soft rock\", \\\n \"R&B\", \"progressive rock\", \"disco\") \ngenres_tuple" | |
}, | |
{ | |
"execution_count": 15, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 15, | |
"metadata": {}, | |
"data": { | |
"text/plain": "8" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "len(genres_tuple)" | |
}, | |
{ | |
"execution_count": 17, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 17, | |
"metadata": {}, | |
"data": { | |
"text/plain": "('hard rock', 'soft rock', 'R&B')" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "genres_tuple[3:6]" | |
}, | |
{ | |
"execution_count": 20, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 20, | |
"metadata": {}, | |
"data": { | |
"text/plain": "7" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "#Find the first index of \"disco\":\n\ngenres_tuple.index(\"disco\")" | |
}, | |
{ | |
"execution_count": 24, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 24, | |
"metadata": {}, | |
"data": { | |
"text/plain": "[-5, -3, 1]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "#Generate a sorted List from the Tuple C_tuple=(-5, 1, -3):\n\nC_tuple=(-5, 1, -3)\nsortedlist = sorted(C_tuple)\nsortedlist" | |
}, | |
{ | |
"execution_count": 25, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 25, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['Michael Jackson', 10.1, 1982]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Create a list\n\nL = [\"Michael Jackson\", 10.1, 1982]\nL" | |
}, | |
{ | |
"execution_count": 26, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "the same element using negative and positive indexing:\n Postive: Michael Jackson \n Negative: Michael Jackson\nthe same element using negative and positive indexing:\n Postive: 10.1 \n Negative: 10.1\nthe same element using negative and positive indexing:\n Postive: 1982 \n Negative: 1982\n" | |
} | |
], | |
"source": "# Print the elements on each index\n\nprint('the same element using negative and positive indexing:\\n Postive:',L[0],\n'\\n Negative:' , L[-3] )\nprint('the same element using negative and positive indexing:\\n Postive:',L[1],\n'\\n Negative:' , L[-2] )\nprint('the same element using negative and positive indexing:\\n Postive:',L[2],\n'\\n Negative:' , L[-1] )" | |
}, | |
{ | |
"execution_count": 27, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 27, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['MJ', 1]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Sample List\n\nL = [\"Michael Jackson\", 10.1,1982,\"MJ\",1]\nL\n# List slicing\n\nL[3:5]" | |
}, | |
{ | |
"execution_count": 29, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 29, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['Michael Jackson', 10.2, 'pop', 10]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Use extend to add elements to list\n\nL = [ \"Michael Jackson\", 10.2]\nL.extend(['pop', 10])\nL\n" | |
}, | |
{ | |
"execution_count": 30, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 30, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['Michael Jackson', 10.2, ['pop', 10]]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Use append to add elements to list\n\nL = [ \"Michael Jackson\", 10.2]\nL.append(['pop', 10])\nL" | |
}, | |
{ | |
"execution_count": 31, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "Before change: ['disco', 10, 1.2]\nAfter change: ['hard rock', 10, 1.2]\n" | |
} | |
], | |
"source": "# Change the element based on the index\n\nA = [\"disco\", 10, 1.2]\nprint('Before change:', A)\nA[0] = 'hard rock'\nprint('After change:', A)" | |
}, | |
{ | |
"execution_count": 32, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "Before change: ['hard rock', 10, 1.2]\nAfter change: [10, 1.2]\n" | |
} | |
], | |
"source": "# Delete the element based on the index\n\nprint('Before change:', A)\ndel(A[0])\nprint('After change:', A)" | |
}, | |
{ | |
"execution_count": 33, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 33, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['hard', 'rock']" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Split the string, default is by space\n\n'hard rock'.split()" | |
}, | |
{ | |
"execution_count": 34, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 34, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['A', 'B', 'C', 'D']" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "# Split the string by comma\n\n'A,B,C,D'.split(',')" | |
}, | |
{ | |
"execution_count": 35, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "A: ['hard rock', 10, 1.2]\nB: ['hard rock', 10, 1.2]\n" | |
} | |
], | |
"source": "# Copy (copy by reference) the list A\n\nA = [\"hard rock\", 10, 1.2]\nB = A\nprint('A:', A)\nprint('B:', B)" | |
}, | |
{ | |
"execution_count": 36, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "B[0]: hard rock\nB[0]: banana\n" | |
} | |
], | |
"source": "# Examine the copy by reference\n\nprint('B[0]:', B[0])\nA[0] = \"banana\"\nprint('B[0]:', B[0])" | |
}, | |
{ | |
"execution_count": 38, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": "B[0]: banana\nB[0]: banana\n" | |
} | |
], | |
"source": "# Clone (clone by value) the list A. B now doesn't change\n\nB = A[:]\nB\nprint('B[0]:', B[0])\nA[0] = \"hard rock\"\nprint('B[0]:', B[0])" | |
}, | |
{ | |
"execution_count": 42, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 42, | |
"metadata": {}, | |
"data": { | |
"text/plain": "[1, 'hello', [1, 2, 3], True]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "#Create a list\n\nalist=[1,\"hello\",[1,2,3],True]\nalist" | |
}, | |
{ | |
"execution_count": 43, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 43, | |
"metadata": {}, | |
"data": { | |
"text/plain": "'hello'" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "#Find value at index 1\n\nalist[1]" | |
}, | |
{ | |
"execution_count": 44, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 44, | |
"metadata": {}, | |
"data": { | |
"text/plain": "['hello', [1, 2, 3], True]" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "#Retrieve the elements stored at index 1, 2 and 3 of a_list.\n\nalist[1:4]" | |
}, | |
{ | |
"execution_count": 45, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"execution_count": 45, | |
"metadata": {}, | |
"data": { | |
"text/plain": "[1, 'a', 2, 1, 'd']" | |
}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": "#Concatenate the following lists A = [1, 'a'] and B = [2, 1, 'd']:\n\nA = [1, 'a']\nB = [2, 1, 'd']\nC=A+B\nC\n\n" | |
}, | |
{ | |
"execution_count": null, | |
"cell_type": "code", | |
"metadata": {}, | |
"outputs": [], | |
"source": "" | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.5", | |
"name": "python3", | |
"language": "python" | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"version": "3.5.5", | |
"name": "python", | |
"file_extension": ".py", | |
"pygments_lexer": "ipython3", | |
"codemirror_mode": { | |
"version": 3, | |
"name": "ipython" | |
} | |
} | |
}, | |
"nbformat": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment