Python-3.4.3/C3/Manipulating-lists/English
| |
|
| Show Slide Title | Welcome to the spoken tutorial on Manipulating Lists. |
| Show Slide
Objectives |
In this tutorial, we will learn about
|
| 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. |
| We have already learnt about list and how to access individual elements in the list. | |
| Show Slide
|
Now we will see about slicing of lists.
|
| Open the terminal | Let us start ipython.
|
| Type ipython3
|
Type ipython3 and press Enter.
|
| Type
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
Let us understand the slicing with an example.
|
| Type
primes[4:8] |
Now we will try to obtain all the primes between 10 and 20 from the above list of primes.
|
| Pause the video.
| |
| Show Slide Exercise 1 | Obtain the primes less than 10, from the list primes. |
| Switch to terminal | Switch back to terminal for the solution |
| Type
primes[0:4] |
Type, primes inside square brackets 0 colon 4
|
| Type num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] | Next we will learn to use step value in slicing.
|
| Type
num[1:10:2]
|
Let us say, we want all the odd numbers less than 10 from the list num.
|
| Type
num[:10]
|
When no step is specified, it is assumed to be 1.
|
| Type
num[10:]
|
Type, num inside square brackets 10 colon
|
| Type
num[::2] |
Next let us get all the even numbers in the list "num"
|
| Pause the video.
| |
| Show Slide Exercise 2 | Obtain all the multiples of three from the list num. |
| Switch to terminal | Switch back to terminal for the solution |
| Type, num[::3] | Type, num inside square brackets colon colon 3
|
| Type a = [5, 1, 6, 7, 7, 10] | Next let us learn to sort a list.
|
| Type, a.sort() | sort method is used to sort a list.
|
| Type
a |
Type, a to get the output.
|
| Slide:
Sorted() |
* Python provides a built-in function called sorted.
|
| Type a = [5, 1, 6, 7, 7, 10] | Again we will assign the same value to a as shown. |
| Type sorted(a) | Type, sorted inside brackets a |
| Type sa = sorted(a) | We can store this sorted list into another list variable sa.
|
| Type sa | To see the sorted list, type sa |
| Type, r = [1, 2, 3, 4, 5]
|
Python also provides the reverse method which reverses the list in place.
|
| Type r.reverse() | r.reverse open and close brackets |
| Type r | To see the list r, type, r
|
| Type, r = [1, 2, 3, 4, 5] | To reverse a list, we can also use striding with negative values.
|
| Type r[::-1] | Type, r inside square brackets colon colon minus 1 |
| Type ra = r[::-1] | We can also store this new reversed list in another variable.
|
| Type ra | To see the reversed list, type ra |
| Pause the video.
| |
| Show Slide
Exercise 3
|
Given below is the list of marks of a student in an examination.
|
| Switch to the terminal | Switch back to the terminal for the solution. |
| Type marks = [99, 67, 47, 100, 50, 75, 62] | Type the list of marks as shown. |
| Type,
sorted(marks)[::-1] |
To get the marks in descending order type,
sorted inside brackets marks inside square brackets colon colon minus 1 |
| Type
sorted(marks, reverse = True) |
We can also get the same output by typing,
sorted inside brackets marks, reverse equal to True
|
| Show Slide
Summary slide
|
This brings us to the end of this tutorial.
|
| Show Slide
Evaluation
|
Here are some self assessment questions for you to solve
|
| Show Slide
Solutions
|
And the answers,
primes inside square brackets minus 4 colon
p inside square brackets colon 3 |
| Show Slide Forum | Please post your timed queries in this forum. |
| Slide Fossee Forum | Please post your general queries on Python in this forum. |
| Slide Textbook Companion | FOSSEE team coordinates the TBC project. |
| Show Slide
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
| Show Slide Thank You | This is Priya from IIT Bombay signing off. Thanks for watching. |