Difference between revisions of "Python-3.4.3/C3/Manipulating-lists/English"
Nancyvarkey (Talk | contribs) |
|||
Line 15: | Line 15: | ||
* '''slicing''' and '''striding''' of '''lists''' | * '''slicing''' and '''striding''' of '''lists''' | ||
− | * '''Sort''' and '''reverse | + | * '''Sort''' and '''reverse lists'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 28: | Line 26: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 60: | Line 56: | ||
− | The element with the | + | The '''element''' with the '''stop index''' value will not be included. |
|- | |- | ||
Line 83: | Line 79: | ||
'''<nowiki>primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] </nowiki>''' | '''<nowiki>primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us understand the slicing with an example. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us understand the '''slicing''' with an example. |
Line 101: | Line 97: | ||
− | Observe that the first element we want is 11 which has index 4 in the list. | + | Observe that the first '''element''' we want is 11 which has index 4 in the list. |
Line 107: | Line 103: | ||
− | Also, 19 is the last element we require which has index 7 in the list. | + | Also, 19 is the last '''element''' we require which has '''index''' 7 in the '''list'''. |
− | The element with '''index''' equal to '''stop''' | + | The '''element''' with '''index''' equal to '''stop value''' will not be included. |
Line 140: | Line 136: | ||
− | 7 is the last element we require which has index 3 in the list. | + | 7 is the last '''element''' we require which has '''index''' 3 in the '''list'''. |
Line 150: | Line 146: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] </nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn to use | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn to use '''step value''' in slicing. |
− | Type 0 to 13 in a list and assign it to a variable '''num''' as shown. | + | Type 0 to 13 in a '''list''' and assign it to a variable '''num''' as shown. |
|- | |- | ||
Line 165: | Line 161: | ||
− | We will specify the '''step ''' | + | We will specify the '''step value''' in which the '''slice''' must be obtained. |
Line 174: | Line 170: | ||
− | It is called '''striding''' of list. | + | It is called '''striding''' of '''list'''. |
Line 219: | Line 215: | ||
− | '''Start''' or '''end''' value can be negative to indicate that they are counted from the end of the list. | + | '''Start''' or '''end''' value can be negative to indicate that they are counted from the end of the '''list'''. |
|- | |- | ||
Line 259: | Line 255: | ||
− | It gives us every third element from the list (i.e) multiples of 3. | + | It gives us every third '''element''' from the '''list''' (i.e) multiples of 3. |
Line 266: | Line 262: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>a = [5, 1, 6, 7, 7, 10] </nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>a = [5, 1, 6, 7, 7, 10] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us learn to sort a list. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us learn to sort a '''list'''. |
− | Type, '''a '''''is equal to | + | Type, '''a '''''is equal to inside square brackets '''''5, 1, 6, 7, 7, 10 ''' |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''a.sort()''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''a.sort()''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''sort method''' is used to sort a list. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''sort method''' is used to sort a '''list'''. |
Line 285: | Line 281: | ||
− | We can see that the contents of the '''list | + | We can see that the contents of the '''list a''' is sorted now. |
|- | |- | ||
Line 291: | Line 287: | ||
'''Sorted()''' | '''Sorted()''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| * Python provides a built-in '''function''' called '''sorted.''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| |
− | * '''sorted | + | * Python provides a built-in '''function''' called '''sorted.''' |
+ | * '''sorted function''' sorts the '''list''' which is passed as an '''argument''' to it. | ||
* It returns a new '''sorted list. ''' | * It returns a new '''sorted list. ''' | ||
− | |||
− | |||
|- | |- | ||
Line 307: | Line 302: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa = sorted(a) ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa = sorted(a) ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can store this '''sorted | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can store this '''sorted list''' into another '''list variable sa.''' |
Line 314: | Line 309: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''sa''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the sorted list, type '''sa''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''sorted list''', type '''sa''' |
|- | |- | ||
Line 332: | Line 327: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''r''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''r''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''list r''', type, '''r''' |
Line 338: | Line 333: | ||
− | The original '''list | + | The original '''list r''' is changed now. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''<nowiki>r = [1, 2, 3, 4, 5] </nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, '''<nowiki>r = [1, 2, 3, 4, 5] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To '''reverse''' a list, we can also use '''striding''' with negative '''values'''. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To '''reverse''' a '''list''', we can also use '''striding''' with negative '''values'''. |
Line 353: | Line 348: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>ra = r[::-1] </nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>ra = r[::-1] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also store this new '''reversed | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also store this new '''reversed list''' in another''' variable'''. |
Line 360: | Line 355: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''ra''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''ra''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the reversed list, type '''ra''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To see the '''reversed list''', type '''ra''' |
|- | |- | ||
Line 376: | Line 371: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Given below is the list of marks of a student in an examination. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Given below is the '''list''' of marks of a student in an examination. |
− | Obtain a list with marks in descending order. | + | Obtain a '''list''' with marks in descending order. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;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.097cm;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.097cm;padding-right:0.191cm;"| Switch back to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>marks = [99, 67, 47, 100, 50, 75, 62] </nowiki>''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type '''<nowiki>marks = [99, 67, 47, 100, 50, 75, 62] </nowiki>''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type the list of marks as shown. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type the '''list''' of marks as shown. |
|- | |- | ||
Line 425: | Line 420: | ||
* Obtain the parts of '''lists''' using '''slicing''' and '''striding'''. | * Obtain the parts of '''lists''' using '''slicing''' and '''striding'''. | ||
* '''Sort lists''' using the '''sort''' method. | * '''Sort lists''' using the '''sort''' method. | ||
− | * Use the method '''reverse''' to | + | * Use the method '''reverse''' to reverse the '''lists'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 439: | Line 432: | ||
− | # Given the | + | # Given the '''list primes'''. How do you obtain the last 4 prime numbers? |
− | # Given a list | + | # Given a '''list p''', of unknown length. Obtain the first 3 characters of it. |
− | + | ||
− | + | ||
|- | |- | ||
Line 453: | Line 444: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' ''' | ||
− | # The last four primes can be obtained from the given list as, | + | # The last four primes can be obtained from the given '''list''' as, |
'''primes''' ''inside square brackets ''minus 4 ''colon'' | '''primes''' ''inside square brackets ''minus 4 ''colon'' |
Latest revision as of 09:17, 8 June 2018
|
|
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() |
|
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. |