Python-3.4.3/C3/Manipulating-lists/English-timed

From Script | Spoken-Tutorial
Revision as of 11:52, 4 June 2019 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Manipulating Lists.
00:06 In this tutorial, we will learn about

slicing and striding of lists

00:12 Sort and reverse lists.
00:15 To record this tutorial, I am using Ubuntu Linux 16.04 operating system
00:23 Python 3.4.3

and IPython 5.1.0

00:30 To practise this tutorial, you should know how to run basic Python commands on the ipython console and use lists
00:41 If not, see the relevant Python tutorials on this website.
00:46 We have already learnt about list and how to access individual elements in the list.
00:53 Now we will see about slicing of lists.
00:57 The syntax for slicing is p inside square brackets start colon stop.
01:04 It returns all the elements of p between start and stop values.
01:11 The element with the stop index value will not be included.
01:16 Let us start ipython.

Open the terminal.

01:21 Type ipython3 and press Enter.
01:27 From here onwards, remember to press the Enter key after typing every command on the terminal.
01:34 Let us understand the slicing with an example.

Type, primes is equal to then type as shown.

01:45 Now we will try to obtain all the primes between 10 and 20 from the above list of primes.
01:52 Type, primes inside square brackets 4 colon 8
01:59 Recall that the start index value is 0.
02:03 Observe that the first element we want is 11 which has index 4 in the list.

So, start value is 4.

02:12 Also, 19 is the last element we require which has index 7 in the list.
02:19 The element with index equal to stop value will not be included.

So, end value is 8.

02:26 Pause the video.

Try this exercise and then resume the video.

02:32 Obtain the primes less than 10, from the list primes.
02:37 Switch back to terminal for the solution
02:41 Type, primes inside square brackets 0 colon 4
02:47 Observe that in this case, our start value is 0.
02:52 7 is the last element we require which has index 3 in the list.

So, the end value is 4.

03:01 Next we will learn to use step value in slicing.
03:06 Type 0 to 13 in a list and assign it to a variable num as shown.
03:13 Let us say, we want all the odd numbers less than 10 from the list num.
03:19 We will specify the step value in which the slice must be obtained.
03:25 Type num inside square brackets 1 colon 10 colon 2
03:31 We have to start from element with index 1 upto index 10 in steps of 2.
03:39 It is called striding of list.

Press Enter to get the output.

03:45 We got the odd numbers less than 10.
03:48 When no step is specified, it is assumed to be 1.
03:53 Similarly, there are default values for start and stop as well.
03:59 Type, num inside square brackets colon 10
04:04 It gives the first 10 elements of the list.
04:08 If we don't specify the start value, the first element of the list is taken as starting.
04:15 Type, num inside square brackets 10 colon
04:21 This gives us all the elements from 10th element to the end.
04:26 If we don't specify the stop value, the elements till the last index of the list will be returned.
04:33 Start or end value can be negative to indicate that they are counted from the end of the list.
04:40 Next let us get all the even numbers in the list "num"
04:45 Observe that all the even numbers are at even index locations.
04:51 So, type num inside square brackets colon colon 2
04:58 We got all the even numbers.

This is called striding.

05:04 Pause the video.

Try this exercise and then resume the video.

05:10 Obtain all the multiples of three from the list num.
05:15 Switch back to terminal for the solution
05:19 Type, num inside square brackets colon colon 3
05:25 It gives us every third element from the list (i.e) multiples of 3.
05:31 Next let us learn to sort a list.
05:35 Type, a is equal to inside square brackets 5, 1, 6, 7, 7, 10
05:45 sort method is used to sort a list.
05:49 Type, a.sort open and close brackets
05:55 Type, a to get the output.

We can see that the contents of the list a is sorted now.

06:04 Python provides a built-in function called sorted.
06:08 sorted function sorts the list which is passed as an argument to it.
06:14 It returns a new sorted list.
06:17 Again we will assign the same value to a as shown.
06:23 Type, sorted inside brackets a
06:28 We can store this sorted list into another list variable sa.
06:34 Type, sa is equal to sorted inside brackets a
06:41 To see the sorted list, type sa
06:46 Python also provides the reverse method which reverses the list in place.
06:52 Type, r is equal to inside square brackets 1 comma 2 comma 3 comma 4 comma 5
07:01 r dot reverse open and close brackets
07:06 To see the list r, type, r
07:10 We got the reverse of the list.
07:13 The original list r is changed now.
07:17 To reverse a list, we can also use striding with negative values.
07:22 Again we will assign the same value to r as shown.
07:27 Type, r inside square brackets colon colon minus 1
07:33 We can also store this new reversed list in another variable.
07:38 Type, ra is equal to r inside square brackets colon colon minus 1
07:46 To see the reversed list, type ra
07:51 Pause the video.

Try this exercise and then resume the video.

07:57 Given below is the list of marks of a student in an examination.

Obtain a list with marks in descending order.

08:06 Switch back to the terminal for the solution.
08:10 Type the list of marks as shown.
08:14 To get the marks in descending order type,

sorted inside brackets marks inside square brackets colon colon minus 1

08:26 We can also get the same output by typing,

sorted inside brackets marks comma reverse equal to True

08:38 This brings us to the end of this tutorial.

Let us summarize.

08:44 In this tutorial, we have learnt to, Obtain the parts of lists using slicing and striding.
08:52 Sort lists using the sort method.

Use the method reverse to reverse the lists.

09:00 Here are some self assessment questions for you to solve
09:05 First. Given the list primes. How do you obtain the last 4 prime numbers?
09:11 Second. Given a list p, of unknown length. Obtain the first 3 characters of it.
09:19 And the answers,

First. The last four primes can be obtained from the given list as,

primes inside square brackets minus 4 colon

09:30 Second. The first 3 characters can be obtained as,

p inside square brackets colon 3

09:37 Please post your timed queries in this forum.
09:41 Please post your general queries on Python in this forum.
09:46 FOSSEE team coordinates the TBC project.
09:50 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

10:01 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya