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