author | Nishanth <nishanth@fossee.in> |
Thu, 07 Oct 2010 14:15:30 +0530 | |
changeset 233 | ab748264f726 |
parent 201 | 6b1efb74d914 |
permissions | -rw-r--r-- |
176 | 1 |
Hello friends and welcome to the tutorial on Basic Data types and |
2 |
operators in Python. |
|
3 |
{{{ Show the slide containing title }}} |
|
4 |
||
5 |
{{{ Show the slide containing the outline slide }}} |
|
6 |
||
186 | 7 |
In this tutorial, we shall look at:: |
176 | 8 |
|
9 |
* Various Datatypes in Python |
|
10 |
* Operators with a little hands-on on how they can be applied to |
|
11 |
the different data types. |
|
12 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
13 |
|
176 | 14 |
|
15 |
First we will explore python data structures in the domain of numbers. |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
16 |
There are three built-in data types in python to represent numbers. |
186 | 17 |
|
176 | 18 |
{{{ A slide to make a memory note of this }}} |
19 |
||
20 |
These are: |
|
21 |
||
22 |
* Integers |
|
23 |
* Complex and |
|
24 |
* Boolean |
|
25 |
||
26 |
Lets first talk about integers. :: |
|
27 |
||
186 | 28 |
a = 13 |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
29 |
a |
186 | 30 |
|
176 | 31 |
|
186 | 32 |
Thats it, there we have our first integer variable a. |
176 | 33 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
34 |
|
176 | 35 |
|
36 |
If we now see :: |
|
37 |
||
186 | 38 |
type(a) |
39 |
<type 'int'> |
|
176 | 40 |
|
186 | 41 |
This means that a is a type of int. Being an int data structure |
176 | 42 |
in python means that there are various functions that this variable |
43 |
has to manipulate it different ways. You can explore these by doing, |
|
44 |
||
186 | 45 |
a.<Tab> |
176 | 46 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
47 |
|
176 | 48 |
|
49 |
Lets see the limits of this int. |
|
50 |
||
186 | 51 |
b = 99999999999999999999 |
52 |
b |
|
176 | 53 |
|
54 |
As you can see even when we put a value of 9 repeated 20 times |
|
55 |
python did not complain. However when you asked python to print |
|
56 |
the number again it put a capital L at the end. Now if you check |
|
57 |
the type of this variable b, :: |
|
58 |
||
186 | 59 |
type(b) |
176 | 60 |
<type 'long'> |
61 |
||
62 |
||
63 |
The reason for this is that python recognizes large integer numbers |
|
186 | 64 |
by the data type long. However long type and integer type share there |
176 | 65 |
functions and properties. |
66 |
||
67 |
Lets now try out the second type in list called float. |
|
68 |
||
69 |
Decimal numbers in python are recognized by the term float :: |
|
70 |
||
186 | 71 |
p = 3.141592 |
72 |
p |
|
176 | 73 |
|
74 |
If you notice the value of output of p isn't exactly equal to p. This |
|
75 |
is because computer saves floating point values in a specific |
|
186 | 76 |
format. There is always an aproximationation. This is why we should |
176 | 77 |
never rely on equality of floating point numbers in a program. |
78 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
79 |
The last data type in the list is complex number :: |
176 | 80 |
|
186 | 81 |
c = 3.2+4.6j |
176 | 82 |
|
83 |
as simple as that so essentialy its just a combination of two floats the |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
84 |
imaginary part being define by j notation instead of i. Complex numbers have a lot of functions specific to them. |
176 | 85 |
Lets check these :: |
86 |
||
186 | 87 |
c.<Tab> |
88 |
||
176 | 89 |
Lets try some of them :: |
90 |
||
186 | 91 |
c.real |
92 |
c.imag |
|
176 | 93 |
|
94 |
c.real gives the real part of the number and c.imag the imaginary. |
|
95 |
||
96 |
We can get the absolute value using the function :: |
|
97 |
||
186 | 98 |
abs(c) |
176 | 99 |
|
100 |
Python also has Boolean as a built-in type. |
|
101 |
||
102 |
Try it out just type :: |
|
103 |
||
186 | 104 |
t = True |
176 | 105 |
|
106 |
note that T in true is capitalized. |
|
107 |
||
108 |
You can apply different Boolean operations on t now for example :: |
|
109 |
||
186 | 110 |
f = not t |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
111 |
f |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
112 |
f or t |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
113 |
f and t |
186 | 114 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
115 |
|
176 | 116 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
117 |
The results are explanotary in themselves. |
176 | 118 |
|
119 |
The usage of boolean brings us to an interesting question of precendence. |
|
120 |
What if you want to apply one operator before another. |
|
121 |
||
186 | 122 |
Well you can use parenthesis for precedence. |
176 | 123 |
|
124 |
Lets write some piece of code to check this out. |
|
125 |
||
126 |
In[]: a=False |
|
127 |
In[]: b=True |
|
128 |
In[]: c=True |
|
129 |
||
130 |
To check how precedence changes with parenthesis. We will try two |
|
131 |
expressions and their evaluation. |
|
132 |
||
133 |
one :: |
|
134 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
135 |
(a and b) or c |
176 | 136 |
|
137 |
This expression gives the value True |
|
138 |
||
139 |
where as the expression :: |
|
140 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
141 |
a and (b or c) |
176 | 142 |
|
143 |
gives the value False. |
|
144 |
||
145 |
Lets now discuss sequence data structures in python. Sequence |
|
146 |
datatypes are those in which elements are kept in a sequential |
|
186 | 147 |
order. All the elements accessed using index. |
176 | 148 |
|
149 |
{{{ slide to for memory aid }}} |
|
150 |
||
151 |
The sequence datatypes in python are :: |
|
186 | 152 |
|
176 | 153 |
* list |
154 |
* string |
|
155 |
* tuple |
|
156 |
||
157 |
The list type is a container that holds a number of other |
|
158 |
objects, in the given order. |
|
159 |
||
160 |
We create our first list by typing :: |
|
161 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
162 |
num_list = [1, 2, 3, 4] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
163 |
num_list |
176 | 164 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
165 |
|
176 | 166 |
Items enclosed in square brackets separated by comma |
167 |
constitutes a list. |
|
168 |
||
186 | 169 |
Lists can store data of any type in them. |
176 | 170 |
|
171 |
We can have a list something like :: |
|
186 | 172 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
173 |
var_list = [1, 1.2, [1,2]] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
174 |
var_list |
186 | 175 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
176 |
|
176 | 177 |
|
178 |
Now we will have a look at strings |
|
179 |
||
180 |
type :: |
|
181 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
182 |
In[]: greeting_string="hello" |
176 | 183 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
184 |
|
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
185 |
greeting_string is now a string variable with the value "hello" |
176 | 186 |
|
187 |
{{{ Memory Aid Slide }}} |
|
188 |
||
189 |
Python strings can actually be defined in three different ways :: |
|
190 |
||
191 |
In[]: k='Single quote' |
|
192 |
In[]: l="Double quote contain's single quote" |
|
193 |
In[]: m='''"Contain's both"''' |
|
194 |
||
186 | 195 |
Thus, single quotes are used as delimiters usually. |
196 |
When a string contains a single quote, double quotes are used as delimiters. |
|
197 |
When a string quote contains both single and double quotes, triple quotes are |
|
198 |
used as delimiters. |
|
176 | 199 |
|
200 |
The last in the list of sequence data types is tuple. |
|
201 |
||
202 |
To create a tuple we use normal brackets '(' |
|
203 |
unlike '[' for lists.:: |
|
204 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
205 |
In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
176 | 206 |
|
207 |
Because of their sequential property there are certain functions and |
|
208 |
operations we can apply to all of them. |
|
209 |
||
210 |
{{{ Slide for memory aid }}} |
|
211 |
||
212 |
The first one is accessing. |
|
213 |
||
214 |
They can be accessed using index numbers :: |
|
215 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
216 |
In[]: num_list[2] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
217 |
In[]: num_list[-1] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
218 |
In[]: greeting_string[1] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
219 |
In[]: greeting_string[3] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
220 |
In[]: greeting_string[-2] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
221 |
In[]: num_tuple[2] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
222 |
In[]: num_tuple[-3] |
176 | 223 |
|
224 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
225 |
Indexing starts from 0 from left to right and from -1 when accessing |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
226 |
lists in reverse. Thus num_list[2] refers to the third element 3. |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
227 |
and greetings [-2] is the second element from the end , that is 'l'. |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
228 |
|
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
229 |
|
186 | 230 |
|
176 | 231 |
Addition gives a new sequence containing both sequences :: |
232 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
233 |
In[]: num_list+var_list |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
234 |
In[]: a_string="another string" |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
235 |
In[]: greeting_string+a_string |
176 | 236 |
In[]: t2=(3,4,6,7) |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
237 |
In[]: num_tuple+t2 |
176 | 238 |
|
239 |
len function gives the length :: |
|
240 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
241 |
In[]: len(num_list) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
242 |
In[]: len(greeting_string) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
243 |
In[]: len(num_tuple) |
176 | 244 |
|
186 | 245 |
Prints the length the variable. |
246 |
||
247 |
We can check the containership of an element using the 'in' keyword :: |
|
176 | 248 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
249 |
In[]: 3 in num_list |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
250 |
In[]: 'H' in greeting_string |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
251 |
In[]: 2 in num_tuple |
176 | 252 |
|
186 | 253 |
We see that it gives True and False accordingly. |
254 |
||
255 |
Find maximum using max function and minimum using min:: |
|
176 | 256 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
257 |
In[]: max(num_tuple) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
258 |
In[]: min(greeting_string) |
176 | 259 |
|
260 |
Get a sorted list and reversed list using sorted and reversed function :: |
|
261 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
262 |
In[]: sorted(num_list) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
263 |
In[]: reversed(greeting_string) |
176 | 264 |
|
186 | 265 |
As a consequence of the order one we access a group of elements together. |
266 |
This is called slicing and striding. |
|
176 | 267 |
|
268 |
First Slicing |
|
269 |
||
270 |
Given a list :: |
|
271 |
||
272 |
In[]:j=[1,2,3,4,5,6] |
|
273 |
||
186 | 274 |
Lets say we want elements starting from 2 and ending in 5. |
176 | 275 |
|
276 |
For this we can do :: |
|
277 |
||
278 |
In[]: j[1:4] |
|
279 |
||
280 |
The syntax for slicing is sequence variable name square bracket |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
281 |
first element index, colon, second element index.The last element however is notincluded in the resultant list:: |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
282 |
|
176 | 283 |
|
284 |
In[]: j[:4] |
|
285 |
||
286 |
If first element is left blank default is from beginning and if last |
|
287 |
element is left blank it means till the end. |
|
288 |
||
289 |
In[]: j[1:] |
|
290 |
||
291 |
In[]: j[:] |
|
292 |
||
293 |
This effectively is the whole list. |
|
294 |
||
186 | 295 |
Striding is similar to slicing except that the step size here is not one. |
176 | 296 |
|
297 |
Lets see by example :: |
|
298 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
299 |
new_num_list=[1,2,3,4,5,6,7,8,9,10] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
300 |
new_num_list[1:8:2] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
301 |
[2, 4, 6, 8] |
176 | 302 |
|
186 | 303 |
The colon two added in the end signifies all the alternate elements. This is why we call this concept |
176 | 304 |
striding because we move through the list with a particular stride or step. The step in this example |
305 |
being 2. |
|
306 |
||
186 | 307 |
We have talked about many similar features of lists, strings and tuples. But there are many important |
308 |
features in lists that differ from strings and tuples. Lets see this by example.:: |
|
176 | 309 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
310 |
In[]: new_num_list[1]=9 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
311 |
In[]: greeting_string[1]='k' |
176 | 312 |
|
186 | 313 |
{{{ slide to show the error }}} |
176 | 314 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
315 |
|
186 | 316 |
|
176 | 317 |
As you can see while the first command executes with out a problem there is an error on the second one. |
318 |
||
319 |
Now lets try :: |
|
186 | 320 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
321 |
In[]: new_tuple[1]=5 |
176 | 322 |
|
323 |
Its the same error. This is because strings and tuples share the property of being immutable. |
|
324 |
We cannot change the value at a particular index just by assigning a new value at that position. |
|
186 | 325 |
|
326 |
||
176 | 327 |
We have looked at different types but we need to convert one data type into another. Well lets one |
328 |
by one go through methods by which we can convert one data type to other: |
|
329 |
||
330 |
We can convert all the number data types to one another :: |
|
331 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
332 |
i=34 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
333 |
d=float(i) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
334 |
d |
176 | 335 |
|
186 | 336 |
Python has built in functions int, float and complex to convert one number type |
176 | 337 |
data structure to another. |
338 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
339 |
dec=2.34 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
340 |
dec_con=int(dec) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
341 |
dec_con |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
342 |
|
186 | 343 |
|
344 |
As you can see the decimal part of the number is simply stripped to get the integer.:: |
|
176 | 345 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
346 |
com=2.3+4.2j |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
347 |
float(com) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
348 |
com |
186 | 349 |
|
350 |
In case of complex number to floating point only the real value of complex number is taken. |
|
176 | 351 |
|
352 |
Similarly we can convert list to tuple and tuple to list :: |
|
353 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
354 |
lst=[3,4,5,6] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
355 |
tup=tuple(lst) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
356 |
tupl=(3,23,4,56) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
357 |
lst=list(tuple) |
176 | 358 |
|
359 |
However string to list and list to string is an interesting problem. |
|
360 |
Lets say we have a string :: |
|
361 |
||
362 |
In: somestring="Is there a way to split on these spaces." |
|
363 |
In: somestring.split() |
|
364 |
||
186 | 365 |
|
176 | 366 |
This produces a list with the string split at whitespace. |
367 |
similarly we can split on some other character. |
|
368 |
||
369 |
In: otherstring="Tim,Amy,Stewy,Boss" |
|
370 |
||
371 |
How do we split on comma , simply pass it as argument :: |
|
372 |
||
373 |
In: otherstring.split(',') |
|
374 |
||
375 |
join function does the opposite. Joins a list to make a string.:: |
|
376 |
||
377 |
In[]:','.join['List','joined','on','commas'] |
|
378 |
||
379 |
Thus we get a list joined on commas. Similarly we can do spaces.:: |
|
380 |
||
381 |
In[]:' '.join['Now','on','spaces'] |
|
382 |
||
383 |
Note that the list has to be a list of strings to apply join operation. |
|
384 |
||
186 | 385 |
.. #[Nishanth]: string to list is fine. But list to string can be left for |
386 |
string manipulations. Just say it requires some string |
|
387 |
manipulations and leave it there. |
|
388 |
||
389 |
.. #[Nishanth]: Where is the summary |
|
390 |
There are no exercises in the script |
|
391 |
||
176 | 392 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
393 |
||
394 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
395 |
||
396 |
Hope you have enjoyed and found it useful. |
|
397 |
||
398 |
Thank You. |
|
399 |
||
400 |
||
401 |
||
402 |
Author : Amit Sethi |
|
186 | 403 |
Internal Reviewer 1 : Nishanth |
176 | 404 |
Internal Reviewer 2 : |
405 |
External Reviewer |