{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Python: Lists and Tuples \n", "\n", "Creating a variable which can store multiple values\n", "\n", "Note that we are using *\"**Python 3**\"*\n", "\n", "Created and updated by John C.S. Lui on August 8th, 2020.\n", "\n", "**Important note:** *If you want to use and modify this notebook file, please acknowledge the author.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists\n", "A collection of items which can be stored in a list variable" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, James, you are so handsome !!!\n", "Hello, John, you are so handsome !!!\n", "Hello, Patrick, you are so handsome !!!\n", "Hello, Michael Fung, you are so handsome !!!\n", "Hello, Siu Hang, you are so handsome !!!\n" ] } ], "source": [ "professors = ['james', 'john', 'patrick', 'michael fung', 'siu hang']\n", "\n", "for teacher in professors:\n", " print ('Hello, ' + teacher.title() + ', you are so handsome !!!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Defining a list\n", "\n", "In Python, square brackets define a list. To create a list, provide a name for the list, add the equals sign, and the values you want within square brackets" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['John C.S. Lui', 'Patrick P.C. Lee', 'James Cheng', 'Wei Meng', 'S.H. Or', 'Michael Fung']\n", "first element in the list is = John C.S. Lui\n", "last element in the list is = Michael Fung\n" ] } ], "source": [ "# You can have different data types within a list, and you can access an item via 'index'\n", "\n", "cse_teachers = ['John C.S. Lui', 'Patrick P.C. Lee', 'James Cheng', 'Wei Meng', 'S.H. Or', 'Michael Fung']\n", "print (cse_teachers)\n", "print (\"first element in the list is =\", cse_teachers[0])\n", "print (\"last element in the list is =\", cse_teachers[-1]) # useful if you don't know the dimension of the list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing various elements in a list" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The second element is = Patrick P. C. Lee\n", "The second to the last element is = S. H. Or\n" ] } ], "source": [ "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "print (\"The second element is =\", cse_teachers[1].title()) # see how we capitalize first character of each word\n", "print (\"The second to the last element is =\", cse_teachers[-2].title()) # see how we capitalize first character of each word" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# we can't use a number larger (or less than) the dimension of the list\n", "# print (\"first element in the list is =\", cse_teachers[10])\n", "# print (\"first element in the list is =\", cse_teachers[-7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List operations\n", "\n", "So, what are some of the built-in functions for list? Let's check." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__delitem__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__gt__',\n", " '__hash__',\n", " '__iadd__',\n", " '__imul__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__reversed__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__setitem__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'append',\n", " 'clear',\n", " 'copy',\n", " 'count',\n", " 'extend',\n", " 'index',\n", " 'insert',\n", " 'pop',\n", " 'remove',\n", " 'reverse',\n", " 'sort']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = [1,2,3]\n", "dir(my_list)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function append:\n", "\n", "append(object, /) method of builtins.list instance\n", " Append object to the end of the list.\n", "\n" ] } ], "source": [ "help(my_list.append)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n", "[1, 2, 3, 4, '5']\n" ] } ], "source": [ "# examples of appending element to the end of the list\n", "\n", "my_list.append(4)\n", "print(my_list)\n", "my_list.append('5') # append a character string !!!!!\n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "Create a list with various programming languages that you know, then print them out\n", "- in increasing order of the index\n", "- in decreasing order of the index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List looping\n", "A loop is a block of code that repeats itself until it uses up all items in the list \n", "(or until a condition is met). Let's try looping which runs once for every item in the list." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "for index = 0 the teacher is Mr. John C. S. Lui\n", "for index = 1 the teacher is Mr. Patrick P. C. Lee\n", "for index = 2 the teacher is Mr. James Cheng\n", "for index = 3 the teacher is Mr. Wei Meng\n", "for index = 4 the teacher is Mr. S. H. Or\n", "for index = 5 the teacher is Mr. Michael Fung\n" ] } ], "source": [ "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "# we are introducing the concept of 'for' iterator, pay attention to the syntax\n", "index = 0\n", "for teacher in cse_teachers :\n", " print(\"for index =\", index, \"the teacher is Mr.\", teacher.title())\n", " index = index + 1" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For teacher John C. S. Lui , I really like this dude.\n", "For teacher Patrick P. C. Lee , I really like this dude.\n", "For teacher James Cheng , I really like this dude.\n", "For teacher Wei Meng , I really hate this dude.\n", "For teacher S. H. Or , I really hate this dude.\n", "For teacher Michael Fung , I really hate this dude.\n", "So why all teachers in the first floor look like dorks?\n" ] } ], "source": [ "# pay attention to indentation \n", "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "index = 0\n", "for teacher in cse_teachers :\n", " if index < 3:\n", " print (\"For teacher\", teacher.title(), \", I really like this dude.\")\n", " else:\n", " print(\"For teacher\", teacher.title(), \", I really hate this dude.\")\n", " index = index + 1\n", " \n", "print(\"So why all teachers in the first floor look like dorks?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Enumerating a list" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Teachers in the 1st floor are:\n", "\n", "------- use enumerate -----------\n", "Position: 0 Teacher: John C. S. Lui\n", "Position: 1 Teacher: Patrick P. C. Lee\n", "Position: 2 Teacher: James Cheng\n", "Position: 3 Teacher: Wei Meng\n", "Position: 4 Teacher: S. H. Or\n", "Position: 5 Teacher: Michael Fung\n", "------- use list.index(value) -----------\n", "Position: 0 Teacher: John C. S. Lui\n", "Position: 1 Teacher: Patrick P. C. Lee\n", "Position: 2 Teacher: James Cheng\n", "Position: 3 Teacher: Wei Meng\n", "Position: 4 Teacher: S. H. Or\n", "Position: 5 Teacher: Michael Fung\n" ] } ], "source": [ "'''\n", "If you want to know the 'index' of the item, you can use either\n", " 1. list.index(value)\n", " 2. enumerate() function tracks the index of each item\n", "'''\n", "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "print(\"Teachers in the 1st floor are:\\n\")\n", "\n", "print (\"------- use enumerate -----------\")\n", "for index, teacher in enumerate(cse_teachers):\n", " position = str(index) # change index to string so we can use cancatenation\n", " print(\"Position: \" + position + \" Teacher: \" + teacher.title())\n", "\n", "print (\"------- use list.index(value) -----------\")\n", "for teacher in cse_teachers:\n", " print(\"Position:\", cse_teachers.index(teacher), \"Teacher: \" + teacher.title()) # list.index(value) returns an integer !!! so we can't cancatenate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List operations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modifying an item in the list" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Prof. John C. S. Lui\n", "['prof. john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n" ] } ], "source": [ "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "cse_teachers[0] = 'prof. john c. s. lui' # we are changing the first item of the list\n", "print(cse_teachers[0].title())\n", "print(cse_teachers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Locating an item in the list" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "index of 's. h. Or' is: 4\n" ] } ], "source": [ "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "print(\"index of 's. h. Or' is: \", cse_teachers.index('s. h. Or'))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# check what this returns\n", "#print (cse_teachers.index('michael lyu')) # It will output ValueError" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing for item membership" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Is 'john c. s. lui' in the list? True\n", "Is 'John C. S. Lui' in the list? False\n" ] } ], "source": [ "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "print (\"Is 'john c. s. lui' in the list?\", 'john c. s. lui' in cse_teachers) \n", "print (\"Is 'John C. S. Lui' in the list?\", 'John C. S. Lui' in cse_teachers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding item into a list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Appending to the end of the list" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "John C. S. Lui is so cool !!!!\n", "Patrick P. C. Lee is so cool !!!!\n", "James Cheng is so cool !!!!\n", "Wei Meng is so cool !!!!\n", "S. H. Or is so cool !!!!\n", "Michael Fung is so cool !!!!\n", "Yat Chiu Law is so cool !!!!\n", "['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung', 'yat chiu law']\n" ] } ], "source": [ "# use the 'append' method\n", "\n", "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "cse_teachers.append('yat chiu law') # append to the end of the lsit\n", "\n", "for teacher in cse_teachers:\n", " print (teacher.title(), \"is so cool !!!!\")\n", "\n", "print(cse_teachers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inserting or removing an item to a list" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "['john c. s. lui', 'patrick p. c. lee', 'james bond', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n" ] } ], "source": [ "cse_teachers = ['john c. s. lui', 'patrick p. c. lee', 'james cheng', 'wei meng', 's. h. Or', 'michael fung']\n", "\n", "print (cse_teachers)\n", "cse_teachers.insert(2,'james bond') # inserting into position 2\n", "\n", "print (cse_teachers)\n", "\n", "del cse_teachers[2] # removing an item\n", "\n", "print (cse_teachers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating an empty list" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The list of Turing_winners is = []\n", "The list of Turing_winners is = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant'] \n", "\n", "The work of Donald Knuth is fantastic !!!\n", "The work of Vint Cerf is fantastic !!!\n", "The work of Andrew Yao is fantastic !!!\n", "The work of Leslie Valiant is fantastic !!!\n" ] } ], "source": [ "# Create an empy list\n", "Turing_winners = []\n", "print(\"The list of Turing_winners is = \", Turing_winners) \n", "\n", "# Add some items in the list\n", "Turing_winners.append('donald knuth') # appending to the end\n", "Turing_winners.append('vint cerf')\n", "Turing_winners.append('andrew yao')\n", "Turing_winners.append('leslie valiant')\n", "\n", "print(\"The list of Turing_winners is = \", Turing_winners,\"\\n\")\n", "\n", "for winner in Turing_winners: \n", " print (\"The work of\", winner.title(), \"is fantastic !!!\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sorting items in a list" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sorted Turing_winners list is ['andrew yao', 'donald knuth', 'leslie valiant', 'vint cerf']\n" ] } ], "source": [ "# sort items in alphabetical order\n", "Turing_winners.sort() # we sort and make update to the list\n", "print (\"The sorted Turing_winners list is\", Turing_winners)\n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sorted Turing_winners list is ['vint cerf', 'leslie valiant', 'donald knuth', 'andrew yao']\n" ] } ], "source": [ "# Let's sort it in reverse order\n", "Turing_winners.sort(reverse=True) # use option to sort in reverse order\n", "print (\"The sorted Turing_winners list is\", Turing_winners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Difference between sort() and sorted()\n", "When using sort(), you **can't recover** the original order. If you just
\n", "want to display a list in sorted order, but preserve the original order,
\n", "you can use the sorted() function." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Turing_winners is ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "Turing_winner is ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "Sorted version is ['andrew yao', 'donald knuth', 'leslie valiant', 'vint cerf']\n", "\n", "Print the original list in reverse sorted fashion:\n", "\t Vint Cerf\n", "\t Leslie Valiant\n", "\t Donald Knuth\n", "\t Andrew Yao\n", "['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "print (\"Turing_winners is\", Turing_winners)\n", "\n", "# Display in sorted fashion but maintain the original ordering\n", "\n", "Turing_winners_sorted = sorted(Turing_winners) # use of sorted() function\n", "\n", "print(\"Turing_winner is\", Turing_winners)\n", "print(\"Sorted version is\", Turing_winners_sorted)\n", "\n", "# Display in reverse sorted fashion\n", "print(\"\\nPrint the original list in reverse sorted fashion:\")\n", "for winner in sorted(Turing_winners, reverse=True):\n", " print(\"\\t\", winner.title())\n", "\n", "print(Turing_winners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reverse a list from its original order\n", "We can use the list built-in function list.reverse()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original ordering: ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "Reverse ordering: ['leslie valiant', 'andrew yao', 'vint cerf', 'donald knuth']\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "print (\"Original ordering:\", Turing_winners)\n", "\n", "Turing_winners.reverse() # using the built-in function list.reverse()\n", "\n", "print (\"Reverse ordering:\", Turing_winners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sorting a numerical list\n", "All the sorting functions we metioned applied for a numerical list" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sorted list of numbers_list: [1, 3, 5, 7, 11, 13]\n", "The reversed sorted list of numbers_list: [13, 11, 7, 5, 3, 1]\n", "[13, 11, 7, 5, 3, 1]\n" ] } ], "source": [ "# Define a numerical list\n", "numbers_list = [1, 7, 3, 5, 13, 11]\n", "\n", "numbers_list.sort() # sort in increasing order\n", "print (\"The sorted list of numbers_list:\", numbers_list)\n", "\n", "numbers_list.sort(reverse=True) # sort in decreasing order\n", "print (\"The reversed sorted list of numbers_list:\", numbers_list)\n", "\n", "print(numbers_list) # see the permanent change?\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The sorted list of numbers_list: [1, 3, 5, 7, 11, 13]\n", "The reverse sorted list of numbers_list: [13, 11, 7, 5, 3, 1]\n", "[1, 7, 3, 5, 13, 11]\n" ] } ], "source": [ "# Define a numerical list\n", "numbers_list = [1, 7, 3, 5, 13, 11]\n", "\n", "# sorted() preserve the original order and return a list !!!\n", "print (\"The sorted list of numbers_list:\", sorted(numbers_list))\n", "print (\"The reverse sorted list of numbers_list:\", sorted(numbers_list, reverse=True))\n", "\n", "print(numbers_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Determining the length of a list\n", "This can be achieved via the len() function" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The length of the Turing_winners list is: 4\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "winners_count = len(Turing_winners) # len() returns an integer\n", "\n", "print(\"The length of the Turing_winners list is:\", winners_count)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The length of the Turing_winners list is: 6\n", "The length of the Turing_winners list is: 6\n" ] } ], "source": [ "## We can keep adding elements to the list and still find the length\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "Turing_winners.append('michael stonebraker')\n", "Turing_winners.append('barbara liskov')\n", "\n", "winners_count = len(Turing_winners)\n", "\n", "print(\"The length of the Turing_winners list is:\", winners_count)\n", "print(\"The length of the Turing_winners list is: \" + str(len(Turing_winners))) # if we want to cancatenate, change to string via str()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Removing some items from a list\n", "We remove items from a list through (1) their position, or (2) through their value." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['donald knuth', 'andrew yao', 'leslie valiant']\n", "['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "# remove and insert an item through position via del() and the list built-in function list.insert()\n", "del Turing_winners[1] # delete via position\n", "print(Turing_winners)\n", "Turing_winners.insert(1,'vint cerf') # insert via position\n", "print (Turing_winners)\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['donald knuth', 'andrew yao', 'leslie valiant']\n", "['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "# remove an item via its value via the list.remove('arg')\n", "Turing_winners.remove('vint cerf')\n", "print(Turing_winners)\n", "Turing_winners.insert(1,'vint cerf') # insert it back\n", "print(Turing_winners)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'vint cerf']\n", "['donald knuth', 'andrew yao', 'leslie valiant', 'vint cerf']\n" ] } ], "source": [ "# Note that the built-in function only removes the \"first occurance\" of the item, not remaining & similar items\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "Turing_winners.append('vint cerf')\n", "print (Turing_winners)\n", "\n", "Turing_winners.remove('vint cerf') # remove the FIRST OCCURANCE of item\n", "print (Turing_winners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Heterogenous items in a list " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "item is: john c. s. lui\n", "item is: 100\n", "item is: 10.01\n", "item is: jack\n", "item is: ['Bill Gates', 30, 'Steve Jobs', 17]\n" ] } ], "source": [ "# list with different types of items\n", "my_list = ['john c. s. lui', 100, 10.01, 'jack', ['Bill Gates', 30, 'Steve Jobs', 17]]\n", "\n", "for item in my_list:\n", " print('item is:', item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The beauty of popping items from a list\n", "The function pop() removes the last item from the list and returns the item to user" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Turing_winners is: ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov']\n", "The popped item is Barbara Liskov\n", "The Turing_winners is: ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "\n", "Turing_winners.append('barbara liskov')\n", "print('The Turing_winners is:', Turing_winners)\n", "last_item = Turing_winners.pop() # popping the last item and assign it to the variable\n", "print (\"The popped item is\", last_item.title())\n", "print (\"The Turing_winners is:\", Turing_winners)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov']\n", "Items that was popped is donald knuth\n", "['andrew yao', 'leslie valiant', 'barbara liskov']\n", "['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov']\n" ] } ], "source": [ "# One can pop any item he/she wants from a list, by giving the index of the item he/she wants to pop\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant']\n", "Turing_winners.append('barbara liskov')\n", "item = Turing_winners.pop(0) # popping the first item\n", "print(Turing_winners)\n", "print(\"Items that was popped is\", item)\n", "\n", "Turing_winners.pop(0)\n", "print (Turing_winners)\n", "Turing_winners.insert(0, 'vint cerf')\n", "Turing_winners.insert(0, 'donald knuth')\n", "print (Turing_winners)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing operations\n", "If you want to get a subset of items from a list, slicing is a convenient way to achieve it" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The first four winners: Donald Knuth\n", "The first four winners: Vint Cerf\n", "The first four winners: Andrew Yao\n", "The first four winners: Leslie Valiant\n", "['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n" ] } ], "source": [ "# Obtain the first 4 items\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "Top_four_names = Turing_winners[0:4] # first index shows the begin index, last index shows the stopping index\n", "\n", "for winner in Top_four_names:\n", " print('The first four winners:', winner.title())\n", " \n", "print(Turing_winners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grap from the beginning to a particular stopping index" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First three names are: Donald Knuth\n", "First three names are: Vint Cerf\n", "First three names are: Andrew Yao\n" ] } ], "source": [ "# Obtain the first 3 items\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "Top_three_names = Turing_winners[:3] # start from index 0 and stop in index 3\n", "\n", "for winner in Top_three_names:\n", " print(\"First three names are:\", winner.title())" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Winners are: Donald Knuth\n", "Winners are: Vint Cerf\n", "Winners are: Andrew Yao\n", "Winners are: Leslie Valiant\n", "Winners are: Barbara Liskov\n" ] } ], "source": [ "# A cool way to grap EVERYTHING without knowing the dimension\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "Top_names = Turing_winners[:-1] # start from index 0 and stop in index 3\n", "\n", "for winner in Top_names:\n", " print(\"Winners are:\", winner.title())" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['vint cerf', 'andrew yao']\n", "['andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n" ] } ], "source": [ "# This should be obvious\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "\n", "Names = Turing_winners[1:3] # get two (3-1) items, starting from index 1\n", "print(Names)\n", "\n", "Names = Turing_winners[2:] # get all items starting from index 2\n", "print(Names)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Duplicating a list and manipulate it" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T_winners: ['leslie valiant', 'barbara liskov', 'leslie lamport']\n", "Turing_winners is not changed: ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n" ] } ], "source": [ "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "\n", "T_winners = Turing_winners[:]\n", "\n", "# let's remove the first three items from the duplicated list\n", "\n", "del T_winners[0]\n", "del T_winners[0]\n", "del T_winners[0]\n", "print (\"T_winners:\", T_winners)\n", "print (\"Turing_winners is not changed:\", Turing_winners)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise\n", "\n", "- Create a list with the first 20 integers, starting from 0\n", "- print the first 7 items out\n", "- remove the first 5 items\n", "- remove the last 3 items\n", "- print out 4 items, starting from index 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question raised by a student in 2019 #" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "print attemp #1: ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "print attemp #2: ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "print attemp #3: ['a', 'b', 'c', 'd', 'e', 'f']\n", "print attemp #4: ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "\n", "So what is the logic?\n" ] } ], "source": [ "my_list = ['a','b','c','d','e','f','g'] # define a list with 7 elements\n", "\n", "print('print attemp #1:', my_list) # print the whole list\n", "print('print attemp #2:', my_list[0:7])\n", "print('print attemp #3:', my_list[0:-1])\n", "print('print attemp #4:', my_list[0:len(my_list)])\n", "print('\\nSo what is the logic?')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numerical list and related functions\n", "Introduce some functions which make working with numerical lists more efficient." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The number is: 1\n", "The number is: 2\n", "The number is: 3\n", "The number is: 4\n", "The number is: 5\n", "The number is: 6\n", "The number is: 7\n", "The number is: 8\n", "The number is: 9\n", "The number is: 10\n", "The number is: 11\n", "The number is: 12\n", "The number is: 13\n", "The number is: 14\n", "The number is: 15\n", "The number is: 16\n", "The number is: 17\n", "The number is: 18\n", "The number is: 19\n", "The number is: 20\n" ] } ], "source": [ "# Create a numerical list with the first 20 numbers\n", "numbers_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]\n", "\n", "for number in numbers_list:\n", " print(\"The number is:\", number)\n", " \n", "# IT WILL BE A PAIN TO CREATE A NUMERICAL LIST WITH THE FIRST MILLION INTEGERS " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The range() function\n", " range(int1, int2) where int1 is the beginning integer and int2 is the stopping integer" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The number is: 1\n", "The number is: 2\n", "The number is: 3\n", "The number is: 4\n", "The number is: 5\n", "The number is: 6\n", "The number is: 7\n", "The number is: 8\n", "The number is: 9\n", "The number is: 10\n", "The number is: 11\n", "The number is: 12\n", "The number is: 13\n", "The number is: 14\n", "The number is: 15\n", "The number is: 16\n", "The number is: 17\n", "The number is: 18\n", "The number is: 19\n" ] } ], "source": [ "# Let's repeat our previous code\n", "for number in range(1,20): \n", " print(\"The number is:\", number)\n", "\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The odd numbers are: 1\n", "The odd numbers are: 3\n", "The odd numbers are: 5\n", "The odd numbers are: 7\n", "The odd numbers are: 9\n", "The odd numbers are: 11\n", "The odd numbers are: 13\n", "The odd numbers are: 15\n", "The odd numbers are: 17\n", "The odd numbers are: 19\n" ] } ], "source": [ "# Let's try range(int1,int2,inc) where int1 is the beginning integer, int2 is the stopping integer, inc is the increment size\n", "for number in range(1,20,2): # Generate only odd numbers\n", " print(\"The odd numbers are:\", number)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The new numbers_list is: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n" ] } ], "source": [ "# Let's try to store the numbers we generate from range() to a list\n", "numbers_list = list(range(1,20,2))\n", "print (\"The new numbers_list is:\", numbers_list)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The numbers_list has 2000000 numbers in it.\n", "\n", "The last five numbers in the list are:\n", "1999996\n", "1999997\n", "1999998\n", "1999999\n", "2000000\n" ] } ], "source": [ "# Store the first 2 million numbers in a list.\n", "numbers_list = list(range(1,2000001))\n", "\n", "# Show the length of the list:\n", "print(\"The numbers_list has \" + str(len(numbers_list)) + \" numbers in it.\") # len() to get the size, str() to convert it to string\n", "\n", "# Show the last five numbers:\n", "print(\"\\nThe last five numbers in the list are:\")\n", "for number in numbers_list[-5:] :\n", " print(number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Finding the minimum, maximum and aggregate of a numerical list\n", "We can use functions like min(), max(), sum() " ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The lowest grade is: 15.5\n", "The highest grade is: 98.0\n", "The average grade is: 65.82000000000001\n" ] } ], "source": [ "grades = [98.0, 20.0, 15.5, 88.0, 60.0, 55.5, 89.0, 95.0, 82.2, 55] # 10 numbers with the last being an integer\n", "\n", "minimum_grade = min(grades) # find the minimum in the list\n", "maximum_grade = max(grades) # find the maximum in the list\n", "average_grade = sum(grades)/len(grades) # find the average grade\n", "\n", "print(\"The lowest grade is:\", minimum_grade)\n", "print(\"The highest grade is:\", maximum_grade)\n", "print(\"The average grade is:\", average_grade)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "- Generate a numerical list with the first 100 integers, starting from 1\n", "- Calculate the sum (you should know this via the geometric series)\n", "- Generate another numerical list with the first 100 even numbers\n", "- Calculate the minimum, maximum, sum and average of the second numerical list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List comprehension\n", "It is a shorthand approach to create and manipulating a list. It is very common in Python programming" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "square root: 1.0\n", "square root: 1.4142135623730951\n", "square root: 1.7320508075688772\n", "square root: 2.0\n", "square root: 2.23606797749979\n", "square root: 2.449489742783178\n", "square root: 2.6457513110645907\n", "square root: 2.8284271247461903\n", "square root: 3.0\n", "square root: 3.1622776601683795\n", "square root: 3.3166247903554\n", "square root: 3.4641016151377544\n", "square root: 3.605551275463989\n", "square root: 3.7416573867739413\n", "square root: 3.872983346207417\n", "square root: 4.0\n", "square root: 4.123105625617661\n", "square root: 4.242640687119285\n", "square root: 4.358898943540674\n", "square root: 4.47213595499958\n" ] } ], "source": [ "# Let's create a numerical list with square root of the first 20 numbers, starting at integer 1\n", "square_roots = [] # create an empty list\n", "\n", "for number in range(1,21):\n", " square_roots.append(number**0.5) # generate the square root of the number\n", " \n", "for number in square_roots: \n", " print(\"square root:\", number)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's see how we can use \"list comprehension\" to write a cleaner code" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "square root: 1.0\n", "square root: 1.4142135623730951\n", "square root: 1.7320508075688772\n", "square root: 2.0\n", "square root: 2.23606797749979\n", "square root: 2.449489742783178\n", "square root: 2.6457513110645907\n", "square root: 2.8284271247461903\n", "square root: 3.0\n", "square root: 3.1622776601683795\n", "square root: 3.3166247903554\n", "square root: 3.4641016151377544\n", "square root: 3.605551275463989\n", "square root: 3.7416573867739413\n", "square root: 3.872983346207417\n", "square root: 4.0\n", "square root: 4.123105625617661\n", "square root: 4.242640687119285\n", "square root: 4.358898943540674\n", "square root: 4.47213595499958\n" ] } ], "source": [ "square_roots = [number**0.5 for number in range(1,21)] # the power of list comprehension, explain the syntax\n", "\n", "for number in square_roots: \n", " print(\"square root:\", number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Non-numerical list comprehension\n", "Let's create an initial list, and use a list comprehension to create a second list from the first one" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here comes Prof. Donald Knuth, this person is brillant!!!\n", "Here comes Prof. Vint Cerf, this person is brillant!!!\n", "Here comes Prof. Andrew Yao, this person is brillant!!!\n", "Here comes Prof. Leslie Valiant, this person is brillant!!!\n", "Here comes Prof. Barbara Liskov, this person is brillant!!!\n", "Here comes Prof. Leslie Lamport, this person is brillant!!!\n" ] } ], "source": [ "# First list\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "\n", "# Second list\n", "second_list = []\n", "for winner in Turing_winners:\n", " second_list.append(\"Prof. \"+ winner.title() + \", this person is brillant!!!\")\n", "\n", "for winner in second_list:\n", " print(\"Here comes \"+ winner)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here comes Prof. Donald Knuth, this person is brillant!!!\n", "Here comes Prof. Vint Cerf, this person is brillant!!!\n", "Here comes Prof. Andrew Yao, this person is brillant!!!\n", "Here comes Prof. Leslie Valiant, this person is brillant!!!\n", "Here comes Prof. Barbara Liskov, this person is brillant!!!\n", "Here comes Prof. Leslie Lamport, this person is brillant!!!\n" ] } ], "source": [ "## Let's use list comprehension\n", "Turing_winners = ['donald knuth', 'vint cerf', 'andrew yao', 'leslie valiant', 'barbara liskov', 'leslie lamport']\n", "second_list = [\"Prof. \"+ winner.title() + \", this person is brillant!!!\" for winner in Turing_winners] # explain syntax\n", "\n", "for winner in second_list:\n", " print(\"Here comes \"+ winner)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert A List To A String or A Set\n", "\n", "You convert a list to a string by using `''.join()`. To covert to a set, use the `set()` function." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "strOfStrings = OneTwoThree\n", "strOfNumbers = 123\n", "set_listOfNumbers = {1, 2, 3, 4, 5}\n" ] } ], "source": [ "# List of Strings to a String\n", "listOfStrings = ['One', 'Two', 'Three']\n", "strOfStrings = ''.join(listOfStrings)\n", "print('strOfStrings = ', strOfStrings)\n", "\n", "# List Of Integers to a String\n", "listOfNumbers = [1, 2, 3]\n", "strOfNumbers = ''.join(str(n) for n in listOfNumbers)\n", "print('strOfNumbers = ', strOfNumbers)\n", "\n", "# List of Intergers to a Set, which all elements are unique\n", "listOfNumbers = [1,2,3,4,4, 5, 5, 5]\n", "set_listOfNumbers = set(listOfNumbers)\n", "print('set_listOfNumbers = ', set_listOfNumbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Difference between `append` and `extend` for list\n", "\n", "`append()` add items to the end of the list.\n", "`extend()`, adds all the elements of an iterable (list, tuple, string etc.) to the end of the list. " ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The new list3_a = ['John', 123, 'CUHK', 666, [1, 2, 3, 4, 5]]\n", "The new list3_b = ['John', 123, 'CUHK', 666, 1, 2, 3, 4, 5]\n" ] } ], "source": [ "list1 = [1,2,3,4,5]\n", "list3_a = ['John', 123, 'CUHK', 666]\n", "list3_b = ['John', 123, 'CUHK', 666]\n", "\n", "list3_a.append(list1)\n", "print('The new list3_a = ', list3_a)\n", "\n", "list3_b.extend(list1)\n", "print('The new list3_b = ', list3_b)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cancatenate a list\n", "\n", "You can use the `+` operator" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list3 = [1, 2, 3, 4, 5, 'John', 123, 'CUHK', 666]\n" ] } ], "source": [ "list1 = [1,2,3,4,5]\n", "list2 = ['John', 123, 'CUHK', 666]\n", "\n", "list3 = list1 + list2\n", "print('list3 =', list3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "- Make a list of the first ten multiples of ten (10, 20, 30... 90, 100). There are multiple ways to do it. Try different ways.\n", "- Make a list of the first ten cubes (1, 8, 27... 1000) using a list comprehension.\n", "- Store five professor names in a list. Make a second list that adds the phrase \"is aweful !\" to each name, using a list comprehension." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## String and characters \n", "We often need to grap a string and process each character within it. Let's see how we do it via list" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the character just read in is: C\n", "the character just read in is: S\n", "the character just read in is: C\n", "the character just read in is: I\n", "the character just read in is: 2\n", "the character just read in is: 0\n", "the character just read in is: 4\n", "the character just read in is: 0\n", "the character just read in is: \n", "the character just read in is: i\n", "the character just read in is: s\n", "the character just read in is: \n", "the character just read in is: r\n", "the character just read in is: e\n", "the character just read in is: a\n", "the character just read in is: l\n", "the character just read in is: l\n", "the character just read in is: y\n", "the character just read in is: \n", "the character just read in is: b\n", "the character just read in is: o\n", "the character just read in is: r\n", "the character just read in is: i\n", "the character just read in is: n\n", "the character just read in is: g\n", "the character just read in is: \n", "the character just read in is: !\n", "the character just read in is: !\n", "the character just read in is: !\n" ] } ], "source": [ "message = \"CSCI2040 is really boring !!!\"\n", "\n", "for char in message: # print each character out\n", " print(\"the character just read in is: \", char)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['C', 'S', 'C', 'I', '2', '0', '4', '0', ' ', 'i', 's', ' ', 'r', 'e', 'a', 'l', 'l', 'y', ' ', 'b', 'o', 'r', 'i', 'n', 'g', ' ', '!', '!', '!']\n" ] } ], "source": [ "# We can create a list from a string. The list will have one element for each character in the string\n", "message = \"CSCI2040 is really boring !!!\"\n", "\n", "message_list = list(message) # list() converts a string to a list, with each character as one item\n", "print(message_list)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first character is: C last charcter is: !\n", "\n", "The first 3 characters are: ['C', 'S', 'C']\n", "The last 4 characters are: [' ', '!', '!', '!']\n" ] } ], "source": [ "# Now we can use various functions and slicing operations to access each character\n", "\n", "message = \"CSCI2040 is really boring !!!\"\n", "message_list = list(message) \n", "\n", "print(\"first character is:\", message_list[0], \"last charcter is:\", message_list[-1])\n", "print(\"\\nThe first 3 characters are:\", message_list[:3]) # Remember slicing return a list !!!\n", "print(\"The last 4 characters are:\", message_list[-4:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Finding substrings within a string\n", "At times, we may want to find a particular words (or patterns) within a large string (document)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The flag is: True\n", "This time the flag is: False\n" ] } ], "source": [ "message = \"CSCI2040 is really boring !!!\"\n", "flag = 'boring' in message # it returns True or False\n", "print (\"The flag is:\", flag)\n", "flag = 'exciting' in message\n", "print(\"This time the flag is:\", flag)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For 'boring' sub-string, it appears in index 19 in the string 'message'\n" ] } ], "source": [ "# The find() method in the string class indicates the index at which the substring begins.\n", "message = \"CSCI2040 is really boring !!!\"\n", "flag_index = message.find('boring') # locate the index\n", "print(\"For 'boring' sub-string, it appears in index\", flag_index, \"in the string 'message'\")" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The index is: 19\n", "The index of the last occurrence is: 43\n" ] } ], "source": [ "# Pay attention that find only locates the FIRST OCCURANCE of the substring\n", "message = \"CSCI2040 is really boring !!!, really very boring.\"\n", "flag_index = message.find('boring')\n", "print('The index is:', flag_index)\n", "\n", "# If you to find the LAST OCCURANCE of the substring, use rfind()\n", "flag_index = message.rfind('boring')\n", "print('The index of the last occurrence is:', flag_index)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Replacing substrings\n", "Use the replace() method to replace any substring with another substring" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CSCI2040 is really exciting !!!, really very exciting.\n" ] } ], "source": [ "message = \"CSCI2040 is really boring !!!, really very boring.\"\n", "message = message.replace('boring', 'exciting')\n", "print (message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Couting substring occurences\n", "Use count() method to determine how many times a substring appears within a string" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The number of times 'boring' appears in 'message' is: 2\n" ] } ], "source": [ "message = \"CSCI2040 is really boring !!!, really very boring.\"\n", "number = message.count('boring')\n", "print(\"The number of times 'boring' appears in 'message' is:\", number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Splitting strings\n", "At times, we find it useful to break a string into, say words.
\n", "The split() method returns a list of substrings.
\n", "The split() takes takes one argument, the character that separates the parts of the string." ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['CSCI2040', 'is', 'really', 'boring', '!!!,', 'really', 'very', 'boring.']\n", "['CSCI2040 is ', ' boring !!!, ', ' very boring.']\n" ] } ], "source": [ "message = \"CSCI2040 is really boring !!!, really very boring.\"\n", "words = message.split(' ') # split uses ' ' (or space) as separator\n", "print(words)\n", "\n", "words = message.split('really') # split uses 'really' as separator\n", "print(words)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other methods for the string class\n", "There are many useful methods for the string class, please refer
\n", "https://docs.python.org/3.3/library/stdtypes.html#string-methods" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interesting exercise\n", "\n", "Now you know string and various methods, you can take on the following exercise.
\n", "The exercise is to **Counting DNA Nucleotides** Please refer to
\n", " Counting DNA Nucleotides." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuples\n", "Tuples are lists that can **never be changed** (or immutable object).
\n", "Tuple uses left/right parenthesis." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The first team in the list is: LA Lakers \n", "\n", "NBA team: LA Lakers\n", "NBA team: Golden State Warriors\n", "NBA team: Cleveland Cavaliers\n", "NBA team: Boston Celtics\n", "NBA team: Houston Rockets\n" ] } ], "source": [ "some_NBA_teams = ('LA Lakers', 'Golden State Warriors', 'Cleveland Cavaliers', 'Boston Celtics', 'Houston Rockets')\n", "\n", "print('The first team in the list is:', some_NBA_teams[0], \"\\n\") # Use [] to access items\n", "\n", "for team in some_NBA_teams:\n", " print(\"NBA team: \" + team)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Can we add things to a tuple?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msome_NBA_teams\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Chicago Bulls'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# Can we del items from a tuple ?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# del some_NBA_teams[0]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "# Can we add things to a tuple?\n", "some_NBA_teams.append('Chicago Bulls')\n", "\n", "# Can we del items from a tuple ?\n", "# del some_NBA_teams[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple and List conversion\n", "\n", "How can we convert a tuple (list) to a list (tuple)? Let's illustrate." ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_tuple = (1, 2, 3, 4, 5)\n", "my_list = [1, 2, 3, 4, 5]\n", "my_tuple2 = (1, 2, 3, 4, 5)\n", "The types are: \n" ] } ], "source": [ "my_tuple = (1,2,3,4,5)\n", "my_list = list(my_tuple)\n", "print('my_tuple = ', my_tuple)\n", "print('my_list = ', my_list)\n", "\n", "my_tuple2 = tuple(my_list)\n", "print(\"my_tuple2 =\", my_tuple2)\n", "print (\"The types are: \", type(my_tuple), type(my_list), type(my_tuple2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists Versus Tuples\n", "\n", "Tuples are used to collect an **immutable** ordered list of elements, so\n", "* There’s no `append()` or `extend()` method for tuples\n", "* There's no `remove()` or `pop()` method\n", "* One can find elements in a tuple via index and you can use `in` to check membership\n", "\n", "If you’re defining a constant set of values and all you’re going to do with it is just to iterate through it, use a tuple instead of a list. It will be **faster** and **safer**.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Formatted output\n", "Sometimes, we may want to print out something similar to the C or C++ style like %d, %s, ..etc" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The #1 team in NBA is: LA Lakers\n", "The #2 team in NBA is: Golden State Warriors\n", "The #3 team in NBA is: Cleveland Cavaliers\n", "The #4 team in NBA is: Boston Celtics\n", "The #5 team in NBA is: Houston Rockets\n" ] } ], "source": [ "some_NBA_teams = ('LA Lakers', 'Golden State Warriors', 'Cleveland Cavaliers', 'Boston Celtics', 'Houston Rockets')\n", "\n", "index = 1\n", "for team in some_NBA_teams:\n", " print(\"The #%d team in NBA is: %s\" %(index, team))\n", " index = index + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Advice on Python Style\n", "\n", "It is important to be a *good* and *elegant* programmer. These are some suggestions\n", "- Use 4 spaces for indentation\n", "- Use **UP** to 79 characters per line of code, and 72 characters for comments.\n", "- Use single blank lines to break up your code into meaningful blocks.\n", "- For comment, use a single space after the pound sign at the beginning of a line.\n", "- Name variables and program files using only lowercase letters, underscores, and numbers." ] }, { "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": 2 }