Difference between revisions of "Python-3.4.3/C2/Getting-started-with-Lists/English"
Nancyvarkey (Talk | contribs) |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 6: | Line 6: | ||
'''Keywords: Python, IPython,''' '''list, del, remove, append''' | '''Keywords: Python, IPython,''' '''list, del, remove, append''' | ||
− | |||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
Line 13: | Line 12: | ||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Slide | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Title Slide |
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Hello Friends. Welcome to the tutorial on "'''Getting started with lists'''". | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Hello Friends. Welcome to the tutorial on "'''Getting started with lists'''". | ||
Line 22: | Line 20: | ||
Objectives | Objectives | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| At the end of this tutorial, you will be able to, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| At the end of this tutorial, you will be able to, | ||
− | |||
* Create '''lists ''' | * Create '''lists ''' | ||
Line 32: | Line 27: | ||
* '''Append elements''' to '''lists''' | * '''Append elements''' to '''lists''' | ||
* '''Delete elements''' from '''lists ''' | * '''Delete elements''' from '''lists ''' | ||
− | |||
− | |||
|- | |- | ||
Line 40: | Line 33: | ||
System Specifications | System Specifications | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To record this tutorial, I am using | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To record this tutorial, I am using | ||
− | |||
* '''Ubuntu Linux 14.04''' operating system | * '''Ubuntu Linux 14.04''' operating system | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Slide |
Pre-requisites | Pre-requisites | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to | ||
− | |||
* run basic '''Python '''commands on the '''ipython console''' | * run basic '''Python '''commands on the '''ipython console''' | ||
Line 64: | Line 53: | ||
List | List | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| What is a List? | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| What is a List? | ||
− | |||
* A List can store a sequence of elements. | * A List can store a sequence of elements. | ||
* All elements need not be of the same data types | * All elements need not be of the same data types | ||
− | |||
− | |||
|- | |- | ||
Line 76: | Line 62: | ||
'''ipython3''' | '''ipython3''' | ||
− | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0cm;padding-right:0.191cm;"| Let us open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously | |
− | + | ||
− | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0cm;padding-right:0.191cm;"| | + | |
− | + | ||
Now, type '''ipython3''' and press '''Enter'''. | Now, type '''ipython3''' and press '''Enter'''. | ||
− | |||
We are now in the '''ipython''' prompt. | We are now in the '''ipython''' prompt. | ||
− | + | Let us clear the '''terminal''' by pressing '''Ctrl + L''' for better view. | |
− | Let us clear the terminal by pressing Ctrl + L for better view. | + | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
'''mylist<nowiki> = ['spam', 'eggs', 100, 1.234] </nowiki>''' | '''mylist<nowiki> = ['spam', 'eggs', 100, 1.234] </nowiki>''' | ||
− | |||
Highlight spam and eggs | Highlight spam and eggs | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us define a '''list''' with a variable name '''mylist''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us define a '''list''' with a variable name '''mylist''' | ||
− | |||
Type | Type | ||
Line 104: | Line 82: | ||
'''mylist '''''equal to'' ''inside square brackets inside quotes''''' spam '''''comma''''' '''''inside quotes '''''eggs''' ''comma''' ''hundred '''''comma''''' one point two three four '''and''' '''press '''Enter'''. | '''mylist '''''equal to'' ''inside square brackets inside quotes''''' spam '''''comma''''' '''''inside quotes '''''eggs''' ''comma''' ''hundred '''''comma''''' one point two three four '''and''' '''press '''Enter'''. | ||
− | + | In '''Python, strings''' are always placed inside single or double quotes. | |
− | In Python, strings are always placed inside single or double quotes. | + | |
|- | |- | ||
Line 113: | Line 90: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now enter''' type '''within brackets '''mylist '''and'' ''press '''Enter'''. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now enter''' type '''within brackets '''mylist '''and'' ''press '''Enter'''. | ||
− | Here type is a function which will return the datatype of the variable. | + | Here '''type''' is a '''function''' which will return the '''datatype''' of the variable. |
− | '''mylist''' is a list datatype. | + | '''mylist''' is a '''list datatype'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight according to narration | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight according to narration | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| As we can see, | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| As we can see, '''lists''' can contain '''elements''' of different '''datatypes''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | In '''mylist''', | ||
+ | *'''spam''' and '''eggs''' are '''strings''', | ||
+ | *whereas '''hundred''' and''' one point two three four''' are '''integer''' and '''float''' respectively. | ||
|- | |- | ||
Line 134: | Line 109: | ||
* variable must either start with an alphabet or an underscore. | * variable must either start with an alphabet or an underscore. | ||
− | * They cannot start with numbers and cannot be the same as Python keywords. | + | * They cannot start with numbers and cannot be the same as '''Python keywords.''' |
− | + | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the code typed earlier, the name '''mylist''' is a variable. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| In the code typed earlier, the name '''mylist''' is a variable. | ||
+ | In '''Python''', a variable must either start with an alphabet or an '''underscore'''. | ||
− | + | They cannot start with numbers and cannot be the same as '''Python keywords.''' | |
− | + | ||
− | + | ||
− | They cannot start with numbers and cannot be the same as Python keywords. | + | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 154: | Line 123: | ||
* A variable name cannot have spaces or punctuation characters or any arithmetic characters. | * A variable name cannot have spaces or punctuation characters or any arithmetic characters. | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| You will learn about '''keywords''' as we proceed. | ||
− | + | Few examples of keywords are '''“for, if, else, elif, while, in, def, or, and”.''' | |
− | + | ||
− | + | ||
− | Few examples of keywords are “for, if, else, elif, while, in, def, or, and”. | + | |
Line 179: | Line 146: | ||
* for, in, elif | * for, in, elif | ||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| These are some of the valid and invalid variable names. | |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| These are some of the valid and invalid variable names | + | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the terminal | ||
− | |||
Highlight '''<nowiki>mylist = ['spam', 'eggs', 100, 1.234] </nowiki>''' | Highlight '''<nowiki>mylist = ['spam', 'eggs', 100, 1.234] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Getting back to our lists, the list could be created as | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Getting back to our '''lists''', the '''list''' could be created as |
− | num ''underscore ''list is ''equal to inside square brackets '''''one''' ''comma '''''two''' and press''' Enter''' | + | '''num ''underscore ''list is ''equal to''' inside square brackets ''''''''one''' ''comma '''''two'''''' and press''' Enter''' |
or | or | ||
− | '''x '''''equal to inside square brackets inside quotes ''a'' comma inside quotes '''''b''''' ''and press''' Enter''' | + | '''x '''''equal to inside square brackets inside quotes '''''a''''' comma inside quotes '''''b''''' ''and press''' Enter''' |
− | + | ||
Any valid variable name could be used. | Any valid variable name could be used. | ||
Line 203: | Line 164: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
'''<nowiki>myemptylist = []</nowiki>''' | '''<nowiki>myemptylist = []</nowiki>''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us now create an '''empty list '''with no elements. | ||
− | + | Type '''myemptylist '''''is equal to open and close square brackets ''and press '''Enter'''. | |
− | + | ||
− | + | ||
− | Type '''myemptylist '''''is equal to open and close square brackets ''and | + | |
This is an '''empty list''' without any '''element''' | This is an '''empty list''' without any '''element''' | ||
Line 220: | Line 178: | ||
list index | list index | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can access an '''element''' of a '''list''' using its corresponding '''index''' or '''position'''. | ||
− | + | '''Index value''' of the '''elements''' starts from 0, 1, 2 and so on. | |
− | + | ||
− | + | ||
− | + | ||
− | Index value of the elements starts from 0, 1, 2 and so on. | + | |
− | + | ||
'''Negative indices''' are used to access '''elements''' from the end. | '''Negative indices''' are used to access '''elements''' from the end. | ||
− | |||
Which starts from -1, -2, and so on | Which starts from -1, -2, and so on | ||
+ | The syntax to get a specific '''list element''' is | ||
− | + | '''variable '''''inside square brackets '''''elements index value''' | |
− | + | ||
− | '''variable '''''inside square brackets | + | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the terminal | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the '''terminal'''. |
|- | |- | ||
Line 252: | Line 204: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type '''mylist'' '''inside square brackets''' ''zero '''and press '''Enter'''. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type '''mylist'' '''inside square brackets''' ''zero '''and press '''Enter'''. | ||
− | + | As we can see'''<nowiki> mylist[0]</nowiki>''' gives the first '''element spam''' | |
− | As we can see'''<nowiki> mylist[0]</nowiki>''' gives the first | + | |
− | + | ||
Type '''mylist '''''inside square brackets''' ''one '''and press '''Enter'''. | Type '''mylist '''''inside square brackets''' ''one '''and press '''Enter'''. | ||
− | |||
'''<nowiki>mylist[1] </nowiki>'''gives the second '''element'''. | '''<nowiki>mylist[1] </nowiki>'''gives the second '''element'''. | ||
Line 263: | Line 212: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Slide | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Slide | ||
− | |||
Exercise1 | Exercise1 | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | ||
− | |||
What happens when you type '''<nowiki>mylist[-1]</nowiki>'''. | What happens when you type '''<nowiki>mylist[-1]</nowiki>'''. | ||
Line 273: | Line 220: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
'''<nowiki>mylist[-1]</nowiki>''' | '''<nowiki>mylist[-1]</nowiki>''' | ||
Line 286: | Line 232: | ||
List in list | List in list | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can also create a '''list''' inside a '''list.''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can also create a '''list''' inside a '''list.''' | ||
− | |||
This property makes '''lists heterogeneous data structures'''. | This property makes '''lists heterogeneous data structures'''. | ||
− | + | This is the syntax for '''list''' inside a '''list'''. | |
− | This is the syntax for list inside a list. | + | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
'''<nowiki>doublelist=[‘a’, [‘b’,’c’,’d’], 'and', </nowiki>5, 6, 7, 8] ''' | '''<nowiki>doublelist=[‘a’, [‘b’,’c’,’d’], 'and', </nowiki>5, 6, 7, 8] ''' | ||
Line 308: | Line 249: | ||
Type the code as shown here: | Type the code as shown here: | ||
− | Here '''b c d''' is a list in list which is represented within square brackets inside a list | + | Here '''b c d''' is a '''list''' in '''list''' which is represented within square brackets inside a '''list'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type:'''<nowiki> doublelist[1] </nowiki>'''and press '''Enter''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type:'''<nowiki> doublelist[1] </nowiki>'''and press '''Enter''' | ||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now let us fetch some element from '''doublelist'''. | |
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | + | |
Type:''' doublelist '''''inside square brackets '''''one''' and press '''Enter''' | Type:''' doublelist '''''inside square brackets '''''one''' and press '''Enter''' | ||
− | We can see the output as '''b c d '''within square brackets | + | We can see the output as '''b c d '''within square brackets. |
− | This is because the | + | This is because the '''index value one''' contains the '''list''' inside a '''list''' as its '''element'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight according to narration | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Highlight according to narration | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now let us try to access a specific element, say''' b''' from the list inside a list | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now let us try to access a specific '''element''', say''' b''' from the '''list''' inside a '''list'''. |
For this type: '''doublelist''''' inside square brackets ''one ''inside square brackets '''''zero '''and press '''Enter'''. | For this type: '''doublelist''''' inside square brackets ''one ''inside square brackets '''''zero '''and press '''Enter'''. | ||
− | Here the 1st parameter represents the index of the list in the main list | + | Here the 1st '''parameter''' represents the '''index''' of the '''list''' in the main '''list'''. |
− | The second parameter represents the index value of '''b''' in the list inside the list | + | The second '''parameter''' represents the '''index value''' of '''b''' in the '''list''' inside the '''list'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Slide | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Show Slide | ||
− | |||
Exercise2 | Exercise2 | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | ||
− | |||
'''<nowiki>doublelist=[‘a’, [‘b’,’c’,’d’], 'and', </nowiki>5, 6, 7, 8] ''' | '''<nowiki>doublelist=[‘a’, [‘b’,’c’,’d’], 'and', </nowiki>5, 6, 7, 8] ''' | ||
− | + | # What is the command to get the '''element 'and'''' in the '''list ''doublelist'''''? | |
− | # What is the command to get the element 'and' in the list doublelist? | + | # How would you get ''''and'''' using negative '''indices'''? |
− | # How would you get 'and' using negative indices? | + | # How would you get '''element 'd'''' from the '''list ''doublelist'''''? |
− | # How would you get element 'd' from the list doublelist? | + | |
− | + | ||
− | + | ||
|- | |- | ||
Line 358: | Line 289: | ||
Solution 2 | Solution 2 | ||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| The solution is on your screen. | |
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| The solution is on your screen | + | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''slide len function''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''slide len function''' | ||
− | '''len''' | + | '''len function''' is used to check the number of '''elements''' in the '''list'''. |
− | + | ||
Syntax: | Syntax: | ||
Line 372: | Line 300: | ||
len(variable) | len(variable) | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can use '''len function''' to check the number of '''elements/length of '''in the '''list'''. | ||
− | + | The syntax is: '''len''' ''inside parentheses '''''variable''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 385: | Line 310: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us find the number of elements in '''mylist''' which we created earlier. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us find the number of elements in '''mylist''' which we created earlier. | ||
− | + | Switch to the''' terminal '''and clear the screen. | |
− | Switch to the''' terminal '''and clear the screen | + | |
− | + | ||
Recall '''mylist''' by pressing up arrow key and press''' Enter.''' | Recall '''mylist''' by pressing up arrow key and press''' Enter.''' | ||
− | |||
Type '''len '''''inside parentheses '''''mylist '''and press '''Enter.''' | Type '''len '''''inside parentheses '''''mylist '''and press '''Enter.''' | ||
− | + | We get the length of '''mylist''' as '''four'''. | |
− | We get the length of mylist as '''four''' | + | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''Slide Append function''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''Slide Append function''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can append '''elements''' to the '''list''' using the '''append function'''. | ||
+ | This '''function''' will add the '''element''' to the end of the '''list'''. | ||
− | + | The syntax is: '''variable''' ''dot'' '''append''' within ''parentheses '''element''' to be added.'' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| <nowiki>[IPython Terminal]</nowiki> | ||
− | |||
'''mylist.append('sharp') ''' | '''mylist.append('sharp') ''' | ||
Line 421: | Line 337: | ||
Now type | Now type | ||
− | '''mylist''''' dot'' append ''inside parentheses within quotes''' ''sharp''' and''' '''press '''Enter'''. | + | '''mylist''''' dot'' '''append''' ''inside parentheses within quotes''' ''sharp''' and''' '''press '''Enter'''. |
+ | Type''' mylist '''''dot'' '''append''' ''inside parentheses''' ''six'' '''''and''' '''press '''Enter.''' | ||
− | + | Let us check the '''updated mylist.''' | |
− | + | Type''' mylist '''and press '''Enter ''' | |
− | + | ||
− | + | ||
− | Type''' mylist '''and | + | |
We can see '''mylist''' is '''appended''' with '''sharp''' and '''six''' at the end. | We can see '''mylist''' is '''appended''' with '''sharp''' and '''six''' at the end. | ||
Line 435: | Line 349: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''slide del ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''slide del ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can also remove '''elements''' from ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can also remove '''elements''' from '''lists'''. |
There are two ways of doing it. | There are two ways of doing it. | ||
− | + | * One is by using the '''index '''with''' del keyword'''. | |
− | * One is by using the '''index '''with''' del''' | + | * Syntax is '''del '''variable ''inside square brackets '''element’s ''index value'''. |
− | * Syntax is '''del '''variable ''inside square brackets element’s ''index value | + | |
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''slide remove''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''slide remove''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| * The other way is removing element by the value using remove function | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| |
+ | * The other way is removing '''element''' by the value using '''remove function''' | ||
* Syntax is '''variable '''''dot '''''remove '''''inside parentheses '''''element to be removed''' | * Syntax is '''variable '''''dot '''''remove '''''inside parentheses '''''element to be removed''' | ||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''mylist ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| '''mylist ''' | ||
− | |||
<nowiki>[IPython Terminal]</nowiki> | <nowiki>[IPython Terminal]</nowiki> | ||
'''<nowiki>del mylist[1]</nowiki>''' | '''<nowiki>del mylist[1]</nowiki>''' | ||
− | |||
'''mylist ''' | '''mylist ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the''' terminal ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Switch to the''' terminal.''' |
− | + | ||
− | + | ||
− | + | ||
+ | Type '''mylist''' and press''' Enter '''to see the updated '''list'''. | ||
Type '''del '''''space '''''mylist '''''inside square brackets''''' one '''and press '''Enter.''' | Type '''del '''''space '''''mylist '''''inside square brackets''''' one '''and press '''Enter.''' | ||
+ | The '''keyword del''' deletes the '''element''' at '''index''' one, | ||
− | + | i.e the second '''element''' of the '''list, eggs'''. | |
+ | Again type '''mylist''' and press''' Enter'''. | ||
− | + | We can see that the '''element eggs''' has been deleted from the '''list'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | We can see that the | + | |
|- | |- | ||
Line 486: | Line 388: | ||
'''mylist.remove(100) ''' | '''mylist.remove(100) ''' | ||
− | |||
'''mylist''' | '''mylist''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us delete the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Let us delete the '''element hundred''' from '''mylist list'''. |
− | + | ||
− | + | ||
− | + | ||
+ | For this we can use the '''function remove'''. | ||
Type '''mylist''''' dot '''''remove '''within parentheses''' hundred '''and press '''Enter.''' | Type '''mylist''''' dot '''''remove '''within parentheses''' hundred '''and press '''Enter.''' | ||
− | + | Let us check the '''updated mylist.''' | |
− | Let us check the | + | |
|- | |- | ||
Line 504: | Line 402: | ||
'''mylist.append('spam') ''' | '''mylist.append('spam') ''' | ||
− | |||
Highlight spam | Highlight spam | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| What if the '''elements''' are repeated? | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| What if the '''elements''' are repeated? | ||
− | |||
To check that, let’s do a small experiment. | To check that, let’s do a small experiment. | ||
− | |||
Type | Type | ||
− | '''mylist''''' dot '''''append '''within parentheses within quotes '''spam '''and press''' | + | '''mylist''''' dot '''''append '''within parentheses within quotes '''spam '''and press''' Enter''' |
+ | Type '''mylist '''and press''' Enter''' | ||
− | ''' | + | Here is the '''updated mylist.''' |
+ | It contains the '''element spam''' twice. | ||
− | + | One is at the start and the next is at the end of the '''list'''. | |
− | + | ||
− | + | ||
− | + | ||
− | One is at the start and the next is at the end of the list. | + | |
|- | |- | ||
Line 531: | Line 424: | ||
Type''' mylist ''' | Type''' mylist ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now remove the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Now remove the '''element spam''' by typing: |
'''mylist '''''dot '''''remove '''''inside parentheses inside quotes'' '''spam '''and press '''Enter.''' | '''mylist '''''dot '''''remove '''''inside parentheses inside quotes'' '''spam '''and press '''Enter.''' | ||
− | + | Let us check '''updated mylist.''' | |
− | Let us check | + | |
|- | |- | ||
Line 542: | Line 434: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can see that only the first occurrence of ''''spam'''' has been removed. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We can see that only the first occurrence of ''''spam'''' has been removed. | ||
+ | The '''function remove''' removes the first occurrence of the '''element''' in the sequence. | ||
− | + | Remember, the '''del keyword''' removes the '''element''' by '''index value'''. | |
− | + | '''remove function''' removes on the basis of '''element''' being passed on. | |
− | + | ||
− | + | ||
− | ''' | + | |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Using '''remove''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Using '''remove function''', we can also remove an '''element''' with its '''index value'''. |
− | + | ||
Let us try this with an example. | Let us try this with an example. | ||
Line 567: | Line 456: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type''' k '''''is equal to inside square brackets '''''one''''' comma '''''two''''' comma '''''one''''' comma '''''three '''and press''' Enter''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Type''' k '''''is equal to inside square brackets '''''one''''' comma '''''two''''' comma '''''one''''' comma '''''three '''and press''' Enter''' | ||
+ | Then type '''k '''''dot '''remove''' inside parentheses''''' k inside square brackets two '''and press''' Enter''' | ||
− | + | Type''' k '''and press '''Enter ''' | |
− | + | ||
− | + | ||
− | Type''' k '''and | + | |
We can see the output as'''<nowiki> [2,1,3]. </nowiki>''' | We can see the output as'''<nowiki> [2,1,3]. </nowiki>''' | ||
Line 581: | Line 468: | ||
Exercise 3 | Exercise 3 | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Pause the video. Try this exercise and then resume the video. | ||
− | 1. Delete the fourth element | + | 1. Delete the fourth '''element''' from the '''list doublelist'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | 2. Remove ''''and'''' from the '''list doublelist'''. | ||
|- | |- | ||
Line 597: | Line 479: | ||
Exercise 3 | Exercise 3 | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| The solution is on your screen. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| The solution is on your screen. | ||
Line 607: | Line 487: | ||
Summary slide | Summary slide | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| This brings us to the end of this tutorial. | ||
− | + | In this tutorial, we have learnt to create: | |
− | + | * '''List''' with '''elements''' | |
− | + | * '''Empty list''' | |
− | + | * '''List''' within a '''list''' | |
− | * | + | |
− | + | ||
− | * | + | |
− | + | ||
− | + | ||
|- | |- | ||
Line 622: | Line 498: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We also learnt to, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| We also learnt to, | ||
− | * Find out the '''list''' length using '''len | + | * Find out the '''list''' length using '''len function'''. |
* Access '''elements''' using their '''index''' numbers. | * Access '''elements''' using their '''index''' numbers. | ||
− | * Append '''elements''' to | + | * Append '''elements''' to '''list''' using the '''function append'''. |
− | * Delete '''element''' from list using the '''del''' and '''remove''' | + | * Delete '''element''' from list using the '''del''' and '''remove function''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 633: | Line 507: | ||
Evaluation | Evaluation | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | ||
− | + | # How do you create an '''empty list'''? | |
− | # How do you create an empty list? | + | # Can you have a '''list''' inside a '''list'''? |
− | # Can you have a list inside a list? | + | # How would you access the last '''element''' of a '''list''' without finding its length? |
− | # How would you access the last element of a list without finding its length? | + | |
− | + | ||
− | + | ||
|- | |- | ||
Line 650: | Line 519: | ||
Solutions | Solutions | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| And the answers, | ||
− | + | # We create an '''empty list''' just by leaving the space inside the square brackets empty. | |
− | + | #:'''myemptylist''' ''equal to open and close square brackets''. | |
− | + | # Yes, '''list''' can contain all the other '''data types''', including '''list'''. | |
− | + | # Using negative '''indices''', we can access the last '''element''' from the '''list'''. | |
− | # Yes, list can contain all the other data types, including list. | + | |
− | # Using negative indices, we can access the last element from the list. | + | |
− | + | ||
− | + | ||
|- | |- | ||
Line 682: | Line 548: | ||
Thank You | Thank You | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| This is | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.079cm;padding-right:0.191cm;"| This is Trupti Kini from IIT Bombay signing off. |
− | Thank | + | Thank you. |
|} | |} |
Latest revision as of 08:45, 8 September 2017
Python/C2/Getting Started with Lists/English
Title of script: Getting Started with Lists
Author: Aditya Palaparthy
Keywords: Python, IPython, list, del, remove, append
|
|
Show Title Slide | Hello Friends. Welcome to the tutorial on "Getting started with lists". |
Show Slide
Objectives |
At the end of this tutorial, you will be able to,
|
Show slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show Slide
List |
What is a List?
|
[Terminal]
ipython3 |
Let us open the Terminal by pressing Ctrl+Alt+T keys simultaneously
Now, type ipython3 and press Enter. We are now in the ipython prompt. Let us clear the terminal by pressing Ctrl + L for better view. |
[IPython Terminal]
mylist = ['spam', 'eggs', 100, 1.234] Highlight spam and eggs |
Let us define a list with a variable name mylist
Type mylist equal to inside square brackets inside quotes spam comma inside quotes eggs comma hundred comma one point two three four and press Enter. In Python, strings are always placed inside single or double quotes. |
type(mylist)
Highlight the command |
Now enter type within brackets mylist and press Enter.
Here type is a function which will return the datatype of the variable. mylist is a list datatype. |
Highlight according to narration | As we can see, lists can contain elements of different datatypes
In mylist,
|
Show slide
What is a variable?
|
In the code typed earlier, the name mylist is a variable.
In Python, a variable must either start with an alphabet or an underscore. They cannot start with numbers and cannot be the same as Python keywords. |
Examples of Keywords:
|
You will learn about keywords as we proceed.
Few examples of keywords are “for, if, else, elif, while, in, def, or, and”.
|
show slide
Valid and invalid variables Valid variable names:
Invalid names:
|
These are some of the valid and invalid variable names. |
Switch to the terminal
Highlight mylist = ['spam', 'eggs', 100, 1.234] |
Getting back to our lists, the list could be created as
num underscore list is equal to inside square brackets '''one comma two' and press Enter or x equal to inside square brackets inside quotes a comma inside quotes b and press Enter Any valid variable name could be used. |
[IPython Terminal]
myemptylist = [] |
Let us now create an empty list with no elements.
Type myemptylist is equal to open and close square brackets and press Enter. This is an empty list without any element |
slide
list index |
We can access an element of a list using its corresponding index or position.
Index value of the elements starts from 0, 1, 2 and so on. Negative indices are used to access elements from the end. Which starts from -1, -2, and so on The syntax to get a specific list element is variable inside square brackets elements index value |
Switch to the terminal. | |
[IPython Terminal]
mylist[0] mylist[1] mylist[3] |
Type mylist inside square brackets zero and press Enter.
As we can see mylist[0] gives the first element spam Type mylist inside square brackets one and press Enter. mylist[1] gives the second element. |
Show Slide
Exercise1 |
Pause the video. Try this exercise and then resume the video.
What happens when you type mylist[-1]. |
[IPython Terminal]
mylist[-1] |
Switch to the terminal for solution
Type mylist inside square brackets minus one and press Enter. As you can see you get the last element which is one point two three four. |
slide
List in list |
We can also create a list inside a list.
This property makes lists heterogeneous data structures. This is the syntax for list inside a list. |
[IPython Terminal]
doublelist=[‘a’, [‘b’,’c’,’d’], 'and', 5, 6, 7, 8] Highlight [‘b’,’c’,’d’] |
Let us include a list within a list for the variable doublelist.
Type the code as shown here: Here b c d is a list in list which is represented within square brackets inside a list. |
Type: doublelist[1] and press Enter | Now let us fetch some element from doublelist.
Type: doublelist inside square brackets one and press Enter We can see the output as b c d within square brackets. This is because the index value one contains the list inside a list as its element. |
Highlight according to narration | Now let us try to access a specific element, say b from the list inside a list.
For this type: doublelist inside square brackets one inside square brackets zero and press Enter. Here the 1st parameter represents the index of the list in the main list. The second parameter represents the index value of b in the list inside the list. |
Show Slide
Exercise2 |
Pause the video. Try this exercise and then resume the video.
doublelist=[‘a’, [‘b’,’c’,’d’], 'and', 5, 6, 7, 8]
|
Show Slide
Solution 2 |
The solution is on your screen. |
slide len function
len function is used to check the number of elements in the list. Syntax: len(variable) |
We can use len function to check the number of elements/length of in the list.
The syntax is: len inside parentheses variable |
[IPython Terminal]
len(mylist) |
Let us find the number of elements in mylist which we created earlier.
Switch to the terminal and clear the screen. Recall mylist by pressing up arrow key and press Enter. Type len inside parentheses mylist and press Enter. We get the length of mylist as four. |
Slide Append function | We can append elements to the list using the append function.
This function will add the element to the end of the list. The syntax is: variable dot append within parentheses element to be added. |
[IPython Terminal]
mylist.append('sharp') mylist.append(6) |
Switch to the terminal
Now type mylist dot append inside parentheses within quotes sharp and press Enter. Type mylist dot append inside parentheses six and press Enter. Let us check the updated mylist. Type mylist and press Enter We can see mylist is appended with sharp and six at the end. |
slide del | We can also remove elements from lists.
There are two ways of doing it.
|
slide remove |
|
mylist
[IPython Terminal] del mylist[1] mylist |
Switch to the terminal.
Type mylist and press Enter to see the updated list. Type del space mylist inside square brackets one and press Enter. The keyword del deletes the element at index one, i.e the second element of the list, eggs. Again type mylist and press Enter. We can see that the element eggs has been deleted from the list. |
[IPython Terminal]
mylist.remove(100) mylist |
Let us delete the element hundred from mylist list.
For this we can use the function remove. Type mylist dot remove within parentheses hundred and press Enter. Let us check the updated mylist. |
[IPython Terminal]
mylist.append('spam') Highlight spam |
What if the elements are repeated?
To check that, let’s do a small experiment. Type mylist dot append within parentheses within quotes spam and press Enter Type mylist and press Enter Here is the updated mylist. It contains the element spam twice. One is at the start and the next is at the end of the list. |
mylist.remove('spam')
Type mylist |
Now remove the element spam by typing:
mylist dot remove inside parentheses inside quotes spam and press Enter. Let us check updated mylist. |
Pause for a while | We can see that only the first occurrence of 'spam' has been removed.
The function remove removes the first occurrence of the element in the sequence. Remember, the del keyword removes the element by index value. remove function removes on the basis of element being passed on. |
Using remove function, we can also remove an element with its index value.
Let us try this with an example. | |
[Ipython Terminal]
k = [1,2,1,3] k.remove(k[2]) k |
Type k is equal to inside square brackets one comma two comma one comma three and press Enter
Then type k dot remove inside parentheses k inside square brackets two and press Enter Type k and press Enter We can see the output as [2,1,3]. Since it deletes the first occurrence of what is returned by k inside square brackets two which is one. |
Show Slide
Exercise 3 |
Pause the video. Try this exercise and then resume the video.
1. Delete the fourth element from the list doublelist. 2. Remove 'and' from the list doublelist. |
Show Slide
Exercise 3 |
The solution is on your screen. |
Show Slide
Summary slide |
This brings us to the end of this tutorial.
In this tutorial, we have learnt to create:
|
We also learnt to,
| |
Show Slide
Evaluation |
Here are some self assessment questions for you to solve
|
Show Slide
Solutions |
And the answers,
|
Show Slide: Forum | Please post your timed queries in this forum. |
Show Slide: Fossee Forum | Please post your general queries on Python in this forum. |
Show Slide: Textbook Companion | FOSSEE team coordinates the TBC project. |
Show Slide: Acknowledgement | Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Show Slide
Thank You |
This is Trupti Kini from IIT Bombay signing off.
Thank you. |