author | bhanu |
Mon, 15 Nov 2010 15:07:22 +0530 | |
changeset 505 | 394c3642cf9c |
parent 412 | bb45826efe74 |
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 |
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
|
20 |
in Python. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
21 |
|
176 | 22 |
{{{ Show the slide containing title }}} |
23 |
||
24 |
{{{ Show the slide containing the outline slide }}} |
|
25 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
26 |
In this tutorial, we shall look at |
176 | 27 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
28 |
* Datatypes in Python |
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
29 |
* Numbers |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
30 |
* Boolean |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
31 |
* Sequence |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
32 |
* Operators in Python |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
33 |
* Arithmetic Operators |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
34 |
* Boolean Operators |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
35 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
36 |
* Python Sequence Data types |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
37 |
* list |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
38 |
* string |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
39 |
* tuple |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
40 |
|
176 | 41 |
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
|
42 |
There are three built-in data types in python to represent numbers. |
186 | 43 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
44 |
{{{ A slide to make a memory note of the different datatypes }}} |
176 | 45 |
|
46 |
These are: |
|
47 |
||
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
48 |
* int |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
49 |
* float |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
50 |
* complex |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
51 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
52 |
Lets first talk about int. :: |
176 | 53 |
|
186 | 54 |
a = 13 |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
55 |
a |
186 | 56 |
|
176 | 57 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
58 |
Now, we have our first int variable a. |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
59 |
|
176 | 60 |
|
61 |
If we now see :: |
|
62 |
||
186 | 63 |
type(a) |
64 |
<type 'int'> |
|
176 | 65 |
|
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
66 |
This means that a is a type of int. There are lot of functions associated |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
67 |
with the int datatype, to manipulate it in different ways. These can be |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
68 |
explored by doing, :: |
176 | 69 |
|
186 | 70 |
a.<Tab> |
176 | 71 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
72 |
*int* datatype can hold integers of any size lets see this by an example. |
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
73 |
:: |
176 | 74 |
|
186 | 75 |
b = 99999999999999999999 |
76 |
b |
|
176 | 77 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
78 |
As you can see even when we put a value of 9 repeated 20 times python did |
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
79 |
not complain. This is because python's int data-type can hold integers of any |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
80 |
size. |
176 | 81 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
82 |
Let us now look at the float data-type. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
83 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
84 |
Decimal numbers in python are represented by the float data-type :: |
176 | 85 |
|
186 | 86 |
p = 3.141592 |
87 |
p |
|
176 | 88 |
|
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
89 |
If you notice the value of output of ``p`` isn't exactly equal to ``p``. |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
90 |
This is because computer saves floating point values in a specific format. |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
91 |
There is always an approximation. This is why we should never rely on |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
92 |
equality of floating point numbers in a program. |
176 | 93 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
94 |
The last data type in the list is complex number :: |
176 | 95 |
|
186 | 96 |
c = 3.2+4.6j |
176 | 97 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
98 |
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
|
99 |
imaginary part being defined by j notation instead of i. Complex numbers |
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
100 |
have a lot of functions specific to them. Let us look at these :: |
176 | 101 |
|
186 | 102 |
c.<Tab> |
103 |
||
176 | 104 |
Lets try some of them :: |
105 |
||
186 | 106 |
c.real |
107 |
c.imag |
|
176 | 108 |
|
109 |
c.real gives the real part of the number and c.imag the imaginary. |
|
110 |
||
111 |
We can get the absolute value using the function :: |
|
112 |
||
186 | 113 |
abs(c) |
176 | 114 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
115 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
116 |
Following is are exercises that you must do. |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
117 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
118 |
%% %% Find the absolute value of 3+4j |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
119 |
:: |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
120 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
121 |
abs(3+4j) |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
122 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
123 |
%% %% What is the datatype of number 999999999999999999? Is it |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
124 |
not int? |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
125 |
:: |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
126 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
127 |
Long |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
128 |
Big integers are internally stored in python |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
129 |
as Long datatype. |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
130 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
131 |
Please, pause the video here. Do the exercises and then continue. |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
132 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
133 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
134 |
{{ Slide for showing Boolean datatypes }} |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
135 |
|
176 | 136 |
Python also has Boolean as a built-in type. |
137 |
||
138 |
Try it out just type :: |
|
139 |
||
186 | 140 |
t = True |
176 | 141 |
|
142 |
note that T in true is capitalized. |
|
143 |
||
144 |
You can apply different Boolean operations on t now for example :: |
|
145 |
||
186 | 146 |
f = not t |
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
147 |
f |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
148 |
f or t |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
149 |
f and t |
186 | 150 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
151 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
152 |
The results are self explanatory. |
176 | 153 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
154 |
What if you want to apply one operator before another. |
176 | 155 |
|
186 | 156 |
Well you can use parenthesis for precedence. |
176 | 157 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
158 |
Lets write some piece of code to check this out.:: |
176 | 159 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
160 |
a=False |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
161 |
b=True |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
162 |
c=True |
176 | 163 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
164 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
165 |
To check how precedence changes with parenthesis, we will try two |
176 | 166 |
expressions and their evaluation. |
167 |
||
168 |
one :: |
|
169 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
170 |
(a and b) or c |
176 | 171 |
|
172 |
This expression gives the value True |
|
173 |
||
174 |
where as the expression :: |
|
175 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
176 |
a and (b or c) |
176 | 177 |
|
178 |
gives the value False. |
|
179 |
||
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
180 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
181 |
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
|
182 |
these data types. |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
183 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
184 |
Python uses '+' for addition :: |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
185 |
|
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
186 |
23 + 74 |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
187 |
|
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
188 |
'-' for subtraction :: |
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
189 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
190 |
23 - 56 |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
191 |
|
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
192 |
'*' for multiplication :: |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
193 |
|
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
194 |
45*76 |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
195 |
|
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
196 |
'/' for division :: |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
197 |
|
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
198 |
384/16 |
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
199 |
8/3 |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
200 |
8.0/3 |
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
201 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
202 |
When we did 8/3 the first case results in am integer |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
203 |
output as both the operands are integer however when |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
204 |
8.0/3 is used the answer is float as one of the operands is |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
205 |
float. |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
206 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
207 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
208 |
'%' for modulo operation :: |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
209 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
210 |
87 % 6 |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
211 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
212 |
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
|
213 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
214 |
7**8 |
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 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
217 |
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
|
218 |
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
|
219 |
`equal to`. :: |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
220 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
221 |
a=73 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
222 |
a*=34 |
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 |
is same as :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
225 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
226 |
a=a*34 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
227 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
228 |
and :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
229 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
230 |
a/=23 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
231 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
232 |
is same as :: |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
233 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
234 |
a=a/23 |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
235 |
|
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
236 |
Following is are exercises that you must do. |
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
237 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
238 |
%% %% Using python find sqaure root of 3? |
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
239 |
|
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
240 |
%% %% Is 3**1/2 and 3**0.5 same |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
241 |
|
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
242 |
Please, pause the video here. Do the exercises and then continue. |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
243 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
244 |
:: |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
245 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
246 |
3**0.5 |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
247 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
248 |
:: |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
249 |
No,One gives an int answer and the other float |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
250 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
251 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
252 |
Lets now discuss sequence data types in Python. Sequence data types |
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
253 |
are those in which elements are kept in a sequential order and all the |
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
254 |
elements are accessed using index numbers. |
176 | 255 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
256 |
{{{ slide introducing sequence datatype }}} |
176 | 257 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
258 |
The sequence datatypes in Python are :: |
186 | 259 |
|
176 | 260 |
* list |
261 |
* string |
|
262 |
* tuple |
|
263 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
264 |
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
|
265 |
given order. |
176 | 266 |
|
267 |
We create our first list by typing :: |
|
268 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
269 |
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
|
270 |
num_list |
176 | 271 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
272 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
273 |
Items enclosed in square brackets separated by comma constitutes a list. |
176 | 274 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
275 |
Lists can store data of any type in them. |
176 | 276 |
|
277 |
We can have a list something like :: |
|
186 | 278 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
279 |
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
|
280 |
var_list |
186 | 281 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
282 |
Lets look at another sequence data type, strings |
176 | 283 |
|
284 |
type :: |
|
285 |
||
362 | 286 |
greeting_string="hello" |
176 | 287 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
288 |
|
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
289 |
greeting_string is now a string variable with the value "hello" |
176 | 290 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
291 |
{{{ All the different types of strings shown }}} |
176 | 292 |
|
293 |
Python strings can actually be defined in three different ways :: |
|
294 |
||
362 | 295 |
k='Single quote' |
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
296 |
l="Let's see how to include a single quote" |
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
297 |
m='''"Let's see how to include both"''' |
176 | 298 |
|
364
91d16630c90f
Made changes to basic data type script based on review
amit
parents:
362
diff
changeset
|
299 |
As you can see, single quotes are used as delimiters usually. |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
300 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
301 |
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
|
302 |
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
|
303 |
triple quotes are used as delimiters. |
176 | 304 |
|
305 |
The last in the list of sequence data types is tuple. |
|
306 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
307 |
To create a tuple we use normal brackets '(' unlike '[' for lists.:: |
176 | 308 |
|
362 | 309 |
num_tuple = (1, 2, 3, 4, 5, 6, 7, 8) |
176 | 310 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
311 |
Because of their sequential property there are certain functions and |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
312 |
operations we can apply to all of them. |
176 | 313 |
|
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
314 |
|
176 | 315 |
|
316 |
The first one is accessing. |
|
317 |
||
318 |
They can be accessed using index numbers :: |
|
319 |
||
362 | 320 |
num_list[2] |
321 |
num_list[-1] |
|
322 |
greeting_string[1] |
|
323 |
greeting_string[3] |
|
324 |
greeting_string[-2] |
|
325 |
num_tuple[2] |
|
326 |
num_tuple[-3] |
|
176 | 327 |
|
328 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
329 |
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
|
330 |
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
|
331 |
[-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
|
332 |
|
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
333 |
|
186 | 334 |
|
176 | 335 |
Addition gives a new sequence containing both sequences :: |
336 |
||
362 | 337 |
num_list+var_list |
338 |
a_string="another string" |
|
339 |
greeting_string+a_string |
|
340 |
t2=(3,4,6,7) |
|
341 |
num_tuple+t2 |
|
176 | 342 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
343 |
len function gives the length :: |
176 | 344 |
|
362 | 345 |
len(num_list) |
346 |
len(greeting_string) |
|
347 |
len(num_tuple) |
|
176 | 348 |
|
186 | 349 |
Prints the length the variable. |
350 |
||
351 |
We can check the containership of an element using the 'in' keyword :: |
|
176 | 352 |
|
362 | 353 |
3 in num_list |
354 |
'H' in greeting_string |
|
355 |
2 in num_tuple |
|
176 | 356 |
|
186 | 357 |
We see that it gives True and False accordingly. |
358 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
359 |
Find maximum using max function and minimum using min:: |
176 | 360 |
|
362 | 361 |
max(num_tuple) |
362 |
min(greeting_string) |
|
176 | 363 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
364 |
Get a sorted list :: |
176 | 365 |
|
362 | 366 |
sorted(num_list) |
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
367 |
|
176 | 368 |
|
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
369 |
As a consequence of their order, we can access a group of elements in a |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
370 |
sequence, together. This is called slicing and striding. |
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
371 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
372 |
First lets discuss Slicing, |
176 | 373 |
|
374 |
Given a list :: |
|
375 |
||
362 | 376 |
j=[1,2,3,4,5,6] |
176 | 377 |
|
186 | 378 |
Lets say we want elements starting from 2 and ending in 5. |
176 | 379 |
|
380 |
For this we can do :: |
|
381 |
||
362 | 382 |
j[1:4] |
176 | 383 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
|
176 | 388 |
|
362 | 389 |
j[:4] |
176 | 390 |
|
391 |
If first element is left blank default is from beginning and if last |
|
392 |
element is left blank it means till the end. |
|
393 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
394 |
:: |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
395 |
|
362 | 396 |
j[1:] |
176 | 397 |
|
362 | 398 |
j[:] |
176 | 399 |
|
400 |
This effectively is the whole list. |
|
401 |
||
186 | 402 |
Striding is similar to slicing except that the step size here is not one. |
176 | 403 |
|
404 |
Lets see by example :: |
|
405 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
406 |
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
|
407 |
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
|
408 |
[2, 4, 6, 8] |
176 | 409 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
410 |
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
|
411 |
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
|
412 |
a particular stride or step. The step in this example being 2. |
176 | 413 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
414 |
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
|
415 |
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
|
416 |
tuples. Lets see this by example.:: |
176 | 417 |
|
362 | 418 |
new_num_list[1]=9 |
419 |
greeting_string[1]='k' |
|
176 | 420 |
|
186 | 421 |
{{{ slide to show the error }}} |
176 | 422 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
423 |
|
186 | 424 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
425 |
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
|
426 |
an error on the second one. |
176 | 427 |
|
428 |
Now lets try :: |
|
186 | 429 |
|
362 | 430 |
new_tuple[1]=5 |
176 | 431 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
432 |
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
|
433 |
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
|
434 |
by assigning a new value at that position. |
186 | 435 |
|
436 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
437 |
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
|
438 |
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
|
439 |
one data type to other: |
176 | 440 |
|
441 |
We can convert all the number data types to one another :: |
|
442 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
443 |
i=34 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
444 |
d=float(i) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
445 |
d |
176 | 446 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
447 |
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
|
448 |
type data structure to another. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
449 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
450 |
:: |
176 | 451 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
452 |
dec=2.34 |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
453 |
dec_con=int(dec) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
454 |
dec_con |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
455 |
|
186 | 456 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
457 |
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
|
458 |
integer.:: |
176 | 459 |
|
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
460 |
com=2.3+4.2j |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
461 |
float(com) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
462 |
com |
186 | 463 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
464 |
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
|
465 |
number is taken. |
176 | 466 |
|
467 |
Similarly we can convert list to tuple and tuple to list :: |
|
468 |
||
201
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
469 |
lst=[3,4,5,6] |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
470 |
tup=tuple(lst) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
471 |
tupl=(3,23,4,56) |
6b1efb74d914
Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents:
186
diff
changeset
|
472 |
lst=list(tuple) |
176 | 473 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
474 |
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
|
475 |
interesting problem. Let's say we have a string :: |
176 | 476 |
|
477 |
In: somestring="Is there a way to split on these spaces." |
|
478 |
In: somestring.split() |
|
479 |
||
186 | 480 |
|
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
481 |
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
|
482 |
split on some other character. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
483 |
|
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
484 |
:: |
176 | 485 |
|
486 |
In: otherstring="Tim,Amy,Stewy,Boss" |
|
487 |
||
488 |
How do we split on comma , simply pass it as argument :: |
|
489 |
||
490 |
In: otherstring.split(',') |
|
491 |
||
492 |
join function does the opposite. Joins a list to make a string.:: |
|
493 |
||
362 | 494 |
','.join['List','joined','on','commas'] |
176 | 495 |
|
496 |
Thus we get a list joined on commas. Similarly we can do spaces.:: |
|
497 |
||
362 | 498 |
' '.join['Now','on','spaces'] |
176 | 499 |
|
500 |
Note that the list has to be a list of strings to apply join operation. |
|
501 |
||
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
502 |
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
|
503 |
|
406
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
504 |
Following is an (are) exercise(s) that you must do. |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
505 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
506 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
507 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
508 |
%% %% Check if 3 is an element of the list [1,7,5,3,4]. In case |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
509 |
it is change it to 21. |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
510 |
:: |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
511 |
l=[1,7,5,3,4] |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
512 |
3 in l |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
513 |
l[3]=21 |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
514 |
l |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
515 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
516 |
%% %% Convert the string "Elizabeth is queen of england" to |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
517 |
"Elizabeth is queen" |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
518 |
:: |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
519 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
520 |
s="Elizabeth is queen of england" |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
521 |
stemp=s.split() |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
522 |
' '.join(stemp[:3]) |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
523 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
524 |
Please, pause the video here. Do the exercise(s) and then continue. |
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
525 |
|
a534e9e79599
Completed basic data type based on review and improved on slides
Amit Sethi
parents:
364
diff
changeset
|
526 |
|
412
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
527 |
This brings us to the end of the tutorial. In this tutorial we have |
bb45826efe74
Changes to basic data-types.
Puneeth Chaganti <punchagan@fossee.in>
parents:
406
diff
changeset
|
528 |
discussed |
320
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
529 |
|
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
530 |
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
|
531 |
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
|
532 |
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
|
533 |
4. Accesing sequence |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
534 |
5. Slicing sequences |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
535 |
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
|
536 |
7. Immutability. |
223044cf254f
Adding new format st-scripts with questions etc for basic-data-type and
amit
parents:
201
diff
changeset
|
537 |
|
176 | 538 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
539 |
||
540 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
541 |
||
542 |
Hope you have enjoyed and found it useful. |
|
543 |
||
544 |
Thank You. |
|
545 |
||
546 |
||
337
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
547 |
.. |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
548 |
Local Variables: |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
549 |
mode: rst |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
550 |
indent-tabs-mode: nil |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
551 |
sentence-end-double-space: nil |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
552 |
fill-column: 75 |
c65d0d9fc0c8
Reviewed Basic datatypes LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
320
diff
changeset
|
553 |
End: |