10 supported by Python namely Lists and Tuples. We have already been |
10 supported by Python namely Lists and Tuples. We have already been |
11 introduced to lists in some of the previous tutorials, here we |
11 introduced to lists in some of the previous tutorials, here we |
12 shall look at them in little more detail. |
12 shall look at them in little more detail. |
13 |
13 |
14 The list type is a container that holds a number of other |
14 The list type is a container that holds a number of other |
15 objects, in the given order. The list type implements the sequence |
15 objects, in the given order. Lists allow you to add and |
16 protocol, and also allows you to add and remove objects from |
16 remove objects from the sequence. |
17 the sequence. |
|
18 |
17 |
19 First lets start the interpreter by typing ipython in terminal. |
18 First lets start the interpreter by typing ipython in terminal. |
20 We create our first list by typing |
19 We create our first list by typing |
21 num = [1, 2, 3, 4] |
20 num = [1, 2, 3, 4] |
22 Items enclosed in square brackets separated by comma |
21 Items enclosed in square brackets separated by comma |
23 constitutes a list. |
22 constitutes a list. |
24 One neat feature of Python list is that we can store data of any |
23 One neat feature of Python list is that we can store data of any |
25 type in them. We can have a list something like: |
24 type in them. We can have a list something like: |
26 var = [1, 1.2, 'string'] |
25 var = [1, 1.2, 'string'] |
27 print var |
26 print var |
28 and with this list we can perform most of list operations. |
|
29 Python lists are very versatile, that is we can change it as we |
|
30 wish. It supports features like removal, addition, sort, etc. |
|
31 |
27 |
32 Similar to strings, we can concatenate two lists using '+' |
28 Similar to strings, we can concatenate two lists using '+' |
33 operator |
29 operator |
34 so num + var will return a new list with 'var' added in end of |
30 so num + var will return a new list with the contents of both |
35 'num' |
31 'num' and 'var' one after the other. |
36 We have already covered the append function. |
32 Let's look at what num contains now |
|
33 print num |
|
34 As you can see num is unchanged by the '+' operator. |
|
35 |
|
36 We have already covered the append function in one of our previous |
|
37 tutorials. |
37 To add single object at the end of a list the 'append' |
38 To add single object at the end of a list the 'append' |
38 function is used |
39 function is used |
39 num |
40 Let's now append -5 to it. |
40 num.append(-5) |
41 num.append(-5) |
41 num |
42 The contents of num have been changed now. |
|
43 print num |
42 append takes only one argument. And append behaves different |
44 append takes only one argument. And append behaves different |
43 from + operator. While + will return new list with two lists |
45 from + operator. While + returns a new list with two lists |
44 added if we try similar with append function like: |
46 added, append will simply add the entire object to the |
|
47 end of the list: |
45 num.append([9, 10, 11]) |
48 num.append([9, 10, 11]) |
46 num |
49 print num |
47 It changes original list and add the argument as one element |
50 It adds the entire list as one element and not separate elements. |
48 and not separate elements. |
51 In order to add separate elements we use the 'extend' function |
49 To extend list with new list elements we use 'extend' function |
52 Let's reinitialize num |
50 num = [1, 4, -6] |
53 num = [1, 4, -6] |
51 num.extend([2, 8, 0]) |
54 num.extend([2, 8, 0]) |
52 num |
55 print num |
53 As we can notice extend and append behave differently. |
56 |
54 To reverse a list 'reverse' function is available. |
57 Let's now move on to see more functions available |
55 num |
58 with lists. |
56 This is current content of list |
59 To reverse a list, we have the 'reverse' function. |
|
60 Please note the order of the elements in num. Let's now do: |
57 num.reverse() |
61 num.reverse() |
58 Now after using reverse function, lets check the value of 'num' |
62 Now after using reverse function, lets check the value of 'num' |
59 num |
63 print num |
60 Please note, reverse actually manipulated the list. |
64 Please note, the reverse() function actually manipulated the list. |
61 To remove a particular element from the list Python provides |
65 To remove a particular element from the list Python provides |
62 the remove() function |
66 the remove() function |
63 num.remove(8) |
67 num.remove(8) |
64 if the given argument is present more than once in the list, |
68 if the given argument is present more than once in the list, |
65 then the first occurrence of that element is removed from list. |
69 then the first occurrence of that element is removed from list. |
66 |
70 |
67 The Slicing and Striding concepts which we covered for Arrays work |
71 The Slicing and Striding concepts which we covered for Arrays work |
68 with lists as well. Lets revisit the concept by looking at some examples |
72 with lists as well. Lets revisit the concept by looking at some examples |
69 a = [1, 2, 3, 4, 5] |
73 a = [1, 2, 3, 4, 5] |
70 a[1:3] returns a list with second and third element of 'a' |
74 print a[1:3] returns a list with second and third element of 'a' |
71 One important feature of list indexing is the negative index. In |
75 One important feature of list indexing is the negative index. In |
72 Lists -1 indicates last element of the list |
76 Lists -1 indicates last element of the list |
73 a[-1] |
77 print a[-1] |
74 similarly -2 will be second last and so forth. Now these |
78 similarly -2 will be second last and so forth. Now these |
75 negative indexes can also be used with slicing. If we try |
79 negative indexes can also be used with slicing. If we try |
76 a[1:-1] |
80 print a[1:-1] |
77 we get list which excludes first and last element of a. |
81 we get list which excludes first and last element of a. |
78 and if we do not specify the start or the end index value the default |
82 and if we do not specify the start or the end index value the default |
79 values are taken. The default values being the first element and the |
83 values are taken. The default values being the first element and the |
80 last element. |
84 last element. |
81 a[:3] will return a list from beginning upto the fourth element of a. |
85 print a[:3] will return a list from beginning upto the fourth element of a. |
82 We can perform striding as well, by specifying the step size |
86 We can perform striding as well, by specifying the step size |
83 a[1:-1:2] |
87 print a[1:-1:2] |
84 This gives second, fourth and so on items of a till we reach |
88 This gives second, fourth and so on items of a till we reach |
85 last item of list. |
89 last item of list. |
86 a[::2] will skip all the even placed elements of a |
90 print a[::2] will skip all the even placed elements of a |
87 With step sizes, if we specify negative values we get some |
91 With step sizes, if we specify negative values we get some |
88 interesting results. Lets try |
92 interesting results. Lets try |
89 a[::-1] |
93 print a[4:1:-1] |
90 It returns reversed 'a' |
94 Here we begin at the 5th element and go upto the 2nd element in the |
91 We can check for containership with lists as well. |
95 reverse order since step size is -1 |
|
96 print a[::-1] |
|
97 This returns a slice with all the elements in 'a' reversed in order. |
|
98 Here the negative step indicates that the start point has to be the |
|
99 last element and the end point has to be the first element and the order |
|
100 has to be reversed. |
|
101 |
|
102 Let's now move on to other functionality |
|
103 We can check for containership of elements within lists as well. |
92 Let's look at the contents of num |
104 Let's look at the contents of num |
93 num |
105 print num |
94 To check if the number 4 is present in the list we type |
106 To check if the number 4 is present in the list we type |
95 4 in a |
107 4 in num |
96 True |
108 True |
97 |
109 |
|
110 Now let's move onto Tuples. |
98 Python provides support for special immutable lists known as |
111 Python provides support for special immutable lists known as |
99 'tuple' To create a tuple instead we use normal brackets '(' |
112 'tuple' To create a tuple instead we use normal brackets '(' |
100 unlike '[' for lists. |
113 unlike '[' for lists. |
101 t = (1, 2, 3, 4, 5, 6, 7, 8) |
114 t = (1, 2, 3, 4, 5, 6, 7, 8) |
102 its elements can also be accessed using indexes |
115 its elements can also be accessed using indexes just like lists. |
103 t[0] + t[3] + t[-1] |
116 print t[0] + t[3] + t[-1] |
104 but operation like |
117 but operation like |
105 t[4] = 7 are not allowed |
118 t[4] = 7 are not allowed |
106 These features of tuples have their advantages. To see where |
119 These features of tuples have their advantages. To see where |
107 are they used we first create two variables |
120 are they used we first create two variables |
108 a, b = 1, 6 |
121 a, b = 1, 6 |