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