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