author | amit |
Wed, 27 Oct 2010 15:13:17 +0530 | |
changeset 362 | a77a27916f81 |
parent 337 | c65d0d9fc0c8 |
child 364 | 91d16630c90f |
permissions | -rw-r--r-- |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
1 |
.. Objectives |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
2 |
.. ---------- |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
3 |
|
362 | 4 |
.. At the end of this tutorial, you should know -- |
5 |
||
6 |
.. 1. Learn about Python Data Structures and Operators.(Remembering) |
|
7 |
.. #.Use them to do basic operations.(Applying) |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
8 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
9 |
.. Prerequisites |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
10 |
.. ------------- |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
11 |
|
362 | 12 |
.. None |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
13 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
14 |
.. Author : Amit Sethi |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
15 |
Internal Reviewer : |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
16 |
External Reviewer : |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
17 |
Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
18 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
19 |
.. #[Puneeth: Fill in pre-requisites.] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
20 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
21 |
Hello friends and welcome to the tutorial on Basic Data types and operators |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
22 |
in Python. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
23 |
|
176 | 24 |
{{{ Show the slide containing title }}} |
25 |
||
26 |
{{{ Show the slide containing the outline slide }}} |
|
27 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
28 |
In this tutorial, we shall look at |
176 | 29 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
30 |
* Datatypes in Python |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
31 |
* Operators in Python |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
32 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
33 |
.. #[Puneeth: Use double colon only for code blocks.] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
34 |
.. #[Puneeth: include more details in the outline.] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
35 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
36 |
with a little hands-on on how they can be applied to the different data types. |
176 | 37 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
38 |
|
176 | 39 |
|
40 |
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
|
41 |
There are three built-in data types in python to represent numbers. |
186 | 42 |
|
176 | 43 |
{{{ A slide to make a memory note of this }}} |
44 |
||
45 |
These are: |
|
46 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
47 |
* int for integers |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
48 |
* float for floating point numbers and |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
49 |
* complex for complex numbers |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
50 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
51 |
.. #[Puneeth: Changed to int, float and complex.] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
52 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
53 |
.. #[Puneeth: Loss of consistency. You talk of built-in data types, but |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
54 |
.. then you were calling them integers, floats and complex. Clean up |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
55 |
.. required.] |
176 | 56 |
|
57 |
Lets first talk about integers. :: |
|
58 |
||
186 | 59 |
a = 13 |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
60 |
a |
186 | 61 |
|
176 | 62 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
63 |
Now, we have our first integer variable a. |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
64 |
|
176 | 65 |
|
66 |
If we now see :: |
|
67 |
||
186 | 68 |
type(a) |
69 |
<type 'int'> |
|
176 | 70 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
71 |
This means that a is a type of int. Being an int data structure in python |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
72 |
means that there are various functions that this variable has to manipulate |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
73 |
it different ways. You can explore these by doing, |
176 | 74 |
|
186 | 75 |
a.<Tab> |
176 | 76 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
77 |
.. #[Puneeth: Why are we suddenly talking of limits? |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
78 |
.. Something like this would be better. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
79 |
.. int data-type can hold integers of any size. for example - ] |
176 | 80 |
|
81 |
Lets see the limits of this int. |
|
82 |
||
186 | 83 |
b = 99999999999999999999 |
84 |
b |
|
176 | 85 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
86 |
As you can see even when we put a value of 9 repeated 20 times python did |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
87 |
not complain. However when you asked python to print the number again it |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
88 |
put a capital L at the end. Now if you check the type of this variable b, |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
89 |
:: |
176 | 90 |
|
186 | 91 |
type(b) |
176 | 92 |
<type 'long'> |
93 |
||
94 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
95 |
The reason for this is that python recognizes large integer numbers by the |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
96 |
data type long. However long type and integer type share there functions |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
97 |
and properties. |
176 | 98 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
99 |
.. #[Puneeth: again, the clean-up that I talked of above. Decide if you are |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
100 |
.. talking about the different type of numbers and the datatypes that are |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
101 |
.. used to represent them or if you are talking of the data-types and what |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
102 |
.. kind of numbers they represent. I think you should choose the former.] |
176 | 103 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
104 |
Let us now look at the float data-type. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
105 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
106 |
Decimal numbers in python are represented by the float data-type :: |
176 | 107 |
|
186 | 108 |
p = 3.141592 |
109 |
p |
|
176 | 110 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
111 |
If you notice the value of output of p isn't exactly equal to p. This is |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
112 |
because computer saves floating point values in a specific format. There is |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
113 |
always an aproximationation. This is why we should never rely on equality |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
114 |
of floating point numbers in a program. |
176 | 115 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
116 |
The last data type in the list is complex number :: |
176 | 117 |
|
186 | 118 |
c = 3.2+4.6j |
176 | 119 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
120 |
as simple as that so essentialy its just a combination of two floats the |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
121 |
imaginary part being defined by j notation instead of i. Complex numbers |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
122 |
have a lot of functions specific to them. Lets check these :: |
176 | 123 |
|
186 | 124 |
c.<Tab> |
125 |
||
176 | 126 |
Lets try some of them :: |
127 |
||
186 | 128 |
c.real |
129 |
c.imag |
|
176 | 130 |
|
131 |
c.real gives the real part of the number and c.imag the imaginary. |
|
132 |
||
133 |
We can get the absolute value using the function :: |
|
134 |
||
186 | 135 |
abs(c) |
176 | 136 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
137 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
138 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
139 |
{{ Slide for memory aid }} |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
140 |
|
176 | 141 |
Python also has Boolean as a built-in type. |
142 |
||
143 |
Try it out just type :: |
|
144 |
||
186 | 145 |
t = True |
176 | 146 |
|
147 |
note that T in true is capitalized. |
|
148 |
||
149 |
You can apply different Boolean operations on t now for example :: |
|
150 |
||
186 | 151 |
f = not t |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
152 |
f |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
153 |
f or t |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
154 |
f and t |
186 | 155 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
156 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
157 |
The results are self explanatory. |
176 | 158 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
159 |
.. #[Puneeth: Why does booleans bring us to precedence? I don't see the |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
160 |
.. connection. Am I missing something?] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
161 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
162 |
The usage of boolean brings us to an interesting question of precedence. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
163 |
What if you want to apply one operator before another. |
176 | 164 |
|
186 | 165 |
Well you can use parenthesis for precedence. |
176 | 166 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
167 |
Lets write some piece of code to check this out.:: |
176 | 168 |
|
169 |
In[]: a=False |
|
170 |
In[]: b=True |
|
171 |
In[]: c=True |
|
172 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
173 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
174 |
.. #[Puneeth: Consistency. In[]: is not present at other places.] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
175 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
176 |
To check how precedence changes with parenthesis, we will try two |
176 | 177 |
expressions and their evaluation. |
178 |
||
179 |
one :: |
|
180 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
181 |
(a and b) or c |
176 | 182 |
|
183 |
This expression gives the value True |
|
184 |
||
185 |
where as the expression :: |
|
186 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
187 |
a and (b or c) |
176 | 188 |
|
189 |
gives the value False. |
|
190 |
||
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
191 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
192 |
Let's now look at some operators available in Python to manipulate |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
193 |
these data types. |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
194 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
195 |
.. #[Puneeth: A mention of other operators would be good? Starting |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
196 |
.. with % and ** is a bit weird.] |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
197 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
198 |
Python uses % for modulo operation :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
199 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
200 |
87 % 6 |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
201 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
202 |
and two stars for a exponent. :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
203 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
204 |
7**8 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
205 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
206 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
207 |
In case one wishes to use the current value of variable in which the result |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
208 |
is stored in the expression one can do that by putting the operator before |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
209 |
`equal to`. :: |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
210 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
211 |
a=73 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
212 |
a*=34 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
213 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
214 |
is same as :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
215 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
216 |
a=a*34 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
217 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
218 |
and :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
219 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
220 |
a/=23 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
221 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
222 |
is same as :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
223 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
224 |
a=a/23 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
225 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
226 |
Lets now discuss sequence data types in Python. Sequence data types |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
227 |
are those in which elements are kept in a sequential order. All the |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
228 |
elements accessed using index. |
176 | 229 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
230 |
.. #[Puneeth: fix the last sentence - it sounds incomplete] |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
231 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
232 |
{{{ slide for memory aid }}} |
176 | 233 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
234 |
The sequence datatypes in Python are :: |
186 | 235 |
|
176 | 236 |
* list |
237 |
* string |
|
238 |
* tuple |
|
239 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
240 |
The list type is a container that holds a number of other objects, in the |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
241 |
given order. |
176 | 242 |
|
243 |
We create our first list by typing :: |
|
244 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
245 |
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
|
246 |
num_list |
176 | 247 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
248 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
249 |
Items enclosed in square brackets separated by comma constitutes a list. |
176 | 250 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
251 |
Lists can store data of any type in them. |
176 | 252 |
|
253 |
We can have a list something like :: |
|
186 | 254 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
255 |
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
|
256 |
var_list |
186 | 257 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
258 |
.. #[Puneeth: some continuity, when jumping to strings?] |
176 | 259 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
260 |
Now we will have a look at strings |
176 | 261 |
|
262 |
type :: |
|
263 |
||
362 | 264 |
greeting_string="hello" |
176 | 265 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
266 |
|
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
267 |
greeting_string is now a string variable with the value "hello" |
176 | 268 |
|
269 |
{{{ Memory Aid Slide }}} |
|
270 |
||
271 |
Python strings can actually be defined in three different ways :: |
|
272 |
||
362 | 273 |
k='Single quote' |
274 |
l="Double quote contain's single quote" |
|
275 |
m='''"Contain's both"''' |
|
176 | 276 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
277 |
.. #[Puneeth: Contain's? That's not a word!] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
278 |
|
186 | 279 |
Thus, single quotes are used as delimiters usually. |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
280 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
281 |
.. #[Puneeth: Thus?] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
282 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
283 |
When a string contains a single quote, double quotes are used as |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
284 |
delimiters. When a string quote contains both single and double quotes, |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
285 |
triple quotes are used as delimiters. |
176 | 286 |
|
287 |
The last in the list of sequence data types is tuple. |
|
288 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
289 |
To create a tuple we use normal brackets '(' unlike '[' for lists.:: |
176 | 290 |
|
362 | 291 |
num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
176 | 292 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
293 |
Because of their sequential property there are certain functions and |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
294 |
operations we can apply to all of them. |
176 | 295 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
296 |
|
176 | 297 |
|
298 |
The first one is accessing. |
|
299 |
||
300 |
They can be accessed using index numbers :: |
|
301 |
||
362 | 302 |
num_list[2] |
303 |
num_list[-1] |
|
304 |
greeting_string[1] |
|
305 |
greeting_string[3] |
|
306 |
greeting_string[-2] |
|
307 |
num_tuple[2] |
|
308 |
num_tuple[-3] |
|
176 | 309 |
|
310 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
311 |
Indexing starts from 0 from left to right and from -1 when accessing lists |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
312 |
in reverse. Thus num_list[2] refers to the third element 3. and greetings |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
313 |
[-2] is the second element from the end , that is 'l'. |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
314 |
|
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
315 |
|
186 | 316 |
|
176 | 317 |
Addition gives a new sequence containing both sequences :: |
318 |
||
362 | 319 |
num_list+var_list |
320 |
a_string="another string" |
|
321 |
greeting_string+a_string |
|
322 |
t2=(3,4,6,7) |
|
323 |
num_tuple+t2 |
|
176 | 324 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
325 |
len function gives the length :: |
176 | 326 |
|
362 | 327 |
len(num_list) |
328 |
len(greeting_string) |
|
329 |
len(num_tuple) |
|
176 | 330 |
|
186 | 331 |
Prints the length the variable. |
332 |
||
333 |
We can check the containership of an element using the 'in' keyword :: |
|
176 | 334 |
|
362 | 335 |
3 in num_list |
336 |
'H' in greeting_string |
|
337 |
2 in num_tuple |
|
176 | 338 |
|
186 | 339 |
We see that it gives True and False accordingly. |
340 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
341 |
Find maximum using max function and minimum using min:: |
176 | 342 |
|
362 | 343 |
max(num_tuple) |
344 |
min(greeting_string) |
|
176 | 345 |
|
346 |
Get a sorted list and reversed list using sorted and reversed function :: |
|
347 |
||
362 | 348 |
sorted(num_list) |
349 |
reversed(greeting_string) |
|
176 | 350 |
|
186 | 351 |
As a consequence of the order one we access a group of elements together. |
352 |
This is called slicing and striding. |
|
176 | 353 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
354 |
.. #[Puneeth: Fix the sentence above. ] |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
355 |
|
176 | 356 |
First Slicing |
357 |
||
358 |
Given a list :: |
|
359 |
||
362 | 360 |
j=[1,2,3,4,5,6] |
176 | 361 |
|
186 | 362 |
Lets say we want elements starting from 2 and ending in 5. |
176 | 363 |
|
364 |
For this we can do :: |
|
365 |
||
362 | 366 |
j[1:4] |
176 | 367 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
368 |
The syntax for slicing is, sequence variable name square bracket first |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
369 |
element index, colon, second element index. The last element however is not |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
370 |
included in the resultant list:: |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
371 |
|
176 | 372 |
|
362 | 373 |
j[:4] |
176 | 374 |
|
375 |
If first element is left blank default is from beginning and if last |
|
376 |
element is left blank it means till the end. |
|
377 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
378 |
:: |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
379 |
|
362 | 380 |
j[1:] |
176 | 381 |
|
362 | 382 |
j[:] |
176 | 383 |
|
384 |
This effectively is the whole list. |
|
385 |
||
186 | 386 |
Striding is similar to slicing except that the step size here is not one. |
176 | 387 |
|
388 |
Lets see by example :: |
|
389 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
390 |
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
|
391 |
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
|
392 |
[2, 4, 6, 8] |
176 | 393 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
394 |
The colon two added in the end signifies all the alternate elements. This |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
395 |
is why we call this concept striding because we move through the list with |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
396 |
a particular stride or step. The step in this example being 2. |
176 | 397 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
398 |
We have talked about many similar features of lists, strings and tuples. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
399 |
But there are many important features in lists that differ from strings and |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
400 |
tuples. Lets see this by example.:: |
176 | 401 |
|
362 | 402 |
new_num_list[1]=9 |
403 |
greeting_string[1]='k' |
|
176 | 404 |
|
186 | 405 |
{{{ slide to show the error }}} |
176 | 406 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
407 |
|
186 | 408 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
409 |
As you can see while the first command executes with out a problem there is |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
410 |
an error on the second one. |
176 | 411 |
|
412 |
Now lets try :: |
|
186 | 413 |
|
362 | 414 |
new_tuple[1]=5 |
176 | 415 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
416 |
Its the same error. This is because strings and tuples share the property |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
417 |
of being immutable. We cannot change the value at a particular index just |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
418 |
by assigning a new value at that position. |
186 | 419 |
|
420 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
421 |
We have looked at different types but we need to convert one data type into |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
422 |
another. Well lets one by one go through methods by which we can convert |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
423 |
one data type to other: |
176 | 424 |
|
425 |
We can convert all the number data types to one another :: |
|
426 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
427 |
i=34 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
428 |
d=float(i) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
429 |
d |
176 | 430 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
431 |
Python has built in functions int, float and complex to convert one number |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
432 |
type data structure to another. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
433 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
434 |
:: |
176 | 435 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
436 |
dec=2.34 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
437 |
dec_con=int(dec) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
438 |
dec_con |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
439 |
|
186 | 440 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
441 |
As you can see the decimal part of the number is simply stripped to get the |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
442 |
integer.:: |
176 | 443 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
444 |
com=2.3+4.2j |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
445 |
float(com) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
446 |
com |
186 | 447 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
448 |
In case of complex number to floating point only the real value of complex |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
449 |
number is taken. |
176 | 450 |
|
451 |
Similarly we can convert list to tuple and tuple to list :: |
|
452 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
453 |
lst=[3,4,5,6] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
454 |
tup=tuple(lst) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
455 |
tupl=(3,23,4,56) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
456 |
lst=list(tuple) |
176 | 457 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
458 |
However converting a string to a list and a list to a string is an |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
459 |
interesting problem. Let's say we have a string :: |
176 | 460 |
|
461 |
In: somestring="Is there a way to split on these spaces." |
|
462 |
In: somestring.split() |
|
463 |
||
186 | 464 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
465 |
This produces a list with the string split at whitespace. Similarly we can |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
466 |
split on some other character. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
467 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
468 |
:: |
176 | 469 |
|
470 |
In: otherstring="Tim,Amy,Stewy,Boss" |
|
471 |
||
472 |
How do we split on comma , simply pass it as argument :: |
|
473 |
||
474 |
In: otherstring.split(',') |
|
475 |
||
476 |
join function does the opposite. Joins a list to make a string.:: |
|
477 |
||
362 | 478 |
','.join['List','joined','on','commas'] |
176 | 479 |
|
480 |
Thus we get a list joined on commas. Similarly we can do spaces.:: |
|
481 |
||
362 | 482 |
' '.join['Now','on','spaces'] |
176 | 483 |
|
484 |
Note that the list has to be a list of strings to apply join operation. |
|
485 |
||
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
486 |
With this we come to the end of this tutorial . |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
487 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
488 |
In this tutorial we have discussed |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
489 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
490 |
1. Number Datatypes , integer,float and complex |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
491 |
2. Boolean and datatype and operators |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
492 |
3. Sequence data types ,List,String and Tuple |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
493 |
4. Accesing sequence |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
494 |
5. Slicing sequences |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
495 |
6. Finding length , sorting and reversing operations on sequences. |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
496 |
7. Immutability. |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
497 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
498 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
499 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
500 |
|
186 | 501 |
.. #[Nishanth]: string to list is fine. But list to string can be left for |
502 |
string manipulations. Just say it requires some string |
|
503 |
manipulations and leave it there. |
|
504 |
||
505 |
.. #[Nishanth]: Where is the summary |
|
506 |
There are no exercises in the script |
|
507 |
||
176 | 508 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
509 |
||
510 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
511 |
||
512 |
Hope you have enjoyed and found it useful. |
|
513 |
||
514 |
Thank You. |
|
515 |
||
516 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
517 |
.. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
518 |
Local Variables: |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
519 |
mode: rst |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
520 |
indent-tabs-mode: nil |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
521 |
sentence-end-double-space: nil |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
522 |
fill-column: 75 |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
523 |
End: |