Difference between revisions of "Python-3.4.3/C2/Getting-started-with-Lists/English"
Line 162: | Line 162: | ||
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. | ||
− | |||
− | |||
− | |||
|- | |- |
Revision as of 17:06, 6 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 Slide
containing title, name of the production team along with the logo of MHRD |
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
|
doneAnimate the slideShow 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
|
[IPython Terminal]
|
Let us define a list with a variable name mylist
mylist equal to inside square brackets inside quotes spam comma inside quotes eggs comma hundred comma one point two three four and press Enter.
|
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 data types
|
Show slide
What is a variable?
|
In the code typed earlier, the name mylist is a variable.
|
Examples of Keywords:
|
You will learn about keywords as we proceed.
|
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
|
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
|
[IPython Terminal]
|
Let us now create an empty list with no Can we use 'elements' instead of items. This is more in line with terms used when referring to a listelements.
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.
variable inside square brackets element’s index value |
Switch to the terminal | |
[IPython Terminal]
mylist[0] mylist[1] mylist[3] |
Type mylist inside square brackets zero and press Enter.
|
Show Slide
|
Pause the video. Try this exercise and then resume the video.
|
[IPython Terminal]
|
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.
|
[IPython Terminal]
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
|
Nowdonefetching the vale from index 0 is already explained, use some other index value as an example 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
|
Pause the video. Try this exercise and then resume the video.
|
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.
len(variable)
|
We can use len function to check the number of elements/length of in the list.
|
[IPython Terminal]
len(mylist) |
Let us find the number of elements in mylist which we created earlier.
|
Slide Append function
|
We can append elements to the list using the append function.
|
[IPython Terminal]
mylist.append(6) |
Switch to the terminal
Now type mylist dot append inside parentheses within quotes sharp and press Enter.
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 | * The other way is removing element by the value using remove function
|
mylist
del mylist[1]
|
Switch to the terminal
|
[IPython Terminal]
mylist.remove(100)
|
Let us delete the element hundred from mylist list.
|
[IPython Terminal]
mylist.append('spam')
|
What if the elements are repeated?
mylist dot append within parentheses within quotes spam and press enter
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.
|
Pause for a while | We can see that only the first occurrence of 'spam' has been removed.
remove function removes on the basis of element being passed on. |
Using remove function, we can also remove an element with its index value
| |
[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
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,
|
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 _________ from IIT Bombay signing off.
Thank You |