author | Puneeth Chaganti <punchagan@gmail.com> |
Wed, 21 Apr 2010 14:28:38 +0530 | |
changeset 94 | 57ae1f75b7e0 |
parent 92 | fa26bdda8f32 |
permissions | -rw-r--r-- |
91 | 1 |
* Data Types |
2 |
*** Outline |
|
3 |
***** Introduction |
|
4 |
******* What are we going to do? |
|
5 |
******* How are we going to do? |
|
6 |
******* Arsenal Required |
|
7 |
********* None |
|
8 |
*** Script |
|
9 |
Welcome friends. |
|
10 |
||
11 |
In this tutorial we shall look at data types available in Python and |
|
12 |
how to perform simple Input and Output operations. |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
13 |
for 'Numbers' we have: int, float, complex datatypes |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
14 |
for Text content we have strings. |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
15 |
For conditional statements, 'Booleans'. |
91 | 16 |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
17 |
Lets get started by opening IPython interpreter. |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
18 |
Lets start with 'numbers' |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
19 |
All integers irrespective of how big they are, are of 'int' |
91 | 20 |
data type |
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
21 |
Now we will create a variable, say |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
22 |
x = 13 |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
23 |
print x |
91 | 24 |
|
25 |
To check the data type of any variable Python provides 'type' function |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
26 |
type(x) |
91 | 27 |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
28 |
y = 999999999999 |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
29 |
print y |
91 | 30 |
|
92 | 31 |
Floating point numbers comes under 'float' |
32 |
p = 3.141592 |
|
33 |
type(p) |
|
34 |
||
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
35 |
Python by default provides support for complex numbers. |
92 | 36 |
c = 3+4j |
37 |
c is a complex number. 'j' is used to specify the imaginary part. |
|
38 |
type(c) |
|
39 |
Python also provides basic functions for their manipulations like |
|
40 |
abs(c) will return the absolute value of c(sqrt(a^2 + b^2)) |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
41 |
c.imag returns imaginary part and c.real gives the real part. |
92 | 42 |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
43 |
Next we will look at Boolean datatype: |
92 | 44 |
Its a primitive datatype having one of two values: True or False. |
45 |
t = True |
|
46 |
print t |
|
47 |
||
48 |
Python is case sensitive language, so True with 'T' is boolean type but |
|
49 |
true with 't' would be a variable. |
|
50 |
||
51 |
f = not True |
|
52 |
||
53 |
we can do binary operation like 'or' and 'not' with these variables |
|
54 |
f or t |
|
55 |
f and t |
|
56 |
||
57 |
in case of multiple binary operations to make sure of precedence use |
|
58 |
'brackets ()' |
|
59 |
a = False |
|
60 |
b = True |
|
61 |
c = True |
|
62 |
(a and b) or c |
|
63 |
True |
|
64 |
first a and b is evaluated and then the 'or' statement |
|
65 |
a and (b or c) |
|
66 |
False |
|
91 | 67 |
|
92 | 68 |
Now we shall look at Python Strings. |
69 |
In python anything enclosed inside quotes(single or double) is a string |
|
70 |
so |
|
71 |
a = 'This is a string' |
|
72 |
print a |
|
73 |
b = "This too!" |
|
74 |
print b |
|
75 |
c = '''This one too!''' |
|
76 |
print c |
|
77 |
d = """And one more.""" |
|
78 |
print d |
|
79 |
||
80 |
Similar to lists we covered earlier even string elements can be accessed |
|
81 |
via index numbers starting from 0 |
|
82 |
||
83 |
print a[0] |
|
84 |
print a[5] |
|
85 |
will |
|
86 |
To access last element we can use a[-1] which is one of Pythons feature. |
|
87 |
print a[-1] |
|
88 |
len function works with the strings also as it does with the arrays and |
|
89 |
returns length of the string. |
|
90 |
||
91 |
One thing to notice about the string variables is that they are |
|
92 |
immutable, that is |
|
93 |
a[0] = 't' |
|
94 |
will throw an error |
|
95 |
||
96 |
Some of methods available for string are: |
|
97 |
a.startswith('Thi') |
|
98 |
returns true if initial of the string is same |
|
99 |
similarly there is endswith |
|
100 |
a.endswith('ING') |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
101 |
a.upper() returns a string with all letters capitalized. |
92 | 102 |
and a.lower() returns a string with all smaller case letters. |
103 |
As we have seen earlier use of split function, it returns the list after |
|
104 |
splitting the string, so |
|
105 |
a.split() |
|
106 |
will give list with three elements. |
|
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
107 |
we also have 'join' function, which does the opposite of what |
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
108 |
split does. |
92 | 109 |
''.join(['a','b','c']) will return a joined string of the list we pass |
110 |
to it. Since join is performed on '' that is empty string we get 'abc' |
|
111 |
if we do something like |
|
112 |
'-'.join(['a','b','c']) |
|
113 |
||
94
57ae1f75b7e0
Minor edits to data-file.org.
Puneeth Chaganti <punchagan@gmail.com>
parents:
92
diff
changeset
|
114 |
we come to the end of this tutorial on introduction of Data types in |
91 | 115 |
Python. In this tutorial we have learnt what are supported data types, |
116 |
supported operations and performing simple IO operations in Python. |
|
117 |
||
118 |
*** Notes |