1
+ '''
2
+ Created on Aug 7, 2017
3
+
4
+ @author: Aditya
5
+
6
+ This program demonstrates the CONTAINERS in python
7
+ '''
8
+
9
+ def func_tuple ():
10
+ print ('Demo of Tuples in python.' )
11
+ print ('NOTE: Tuple is immutable object.' )
12
+
13
+ t = 1 ,2 ,3 ,4 # comma separated list gives a tuple in python
14
+ print ('NOTE: Comma separation results in tuple not the paranthesis.' )
15
+ print ('Tuple: ' ,t )
16
+
17
+ print ('First tuple element:' ,t [0 ])
18
+ print ('Last tuple element:' ,t [- 1 ])
19
+ print ('Length of Tuple:' ,len (t ))
20
+ print ('Minimum:' ,min (t ))
21
+ print ('Maximum:' ,max (t ))
22
+ print ('Tuple with only one element:' , (1 ,))
23
+ print ((1 ,),':' ,type ((1 ,)))
24
+
25
+ t = tuple (range (25 ))
26
+ print ('tuple t:' ,t )
27
+ print (type (t ))
28
+ print ('Check for presence of 10 in\' t\' :' , (10 in t ))
29
+ print ('Check for presence of 50 in\' t\' :' , (50 in t ))
30
+ print ('Check for absence of 50 in\' t\' :' , (50 not in t ))
31
+
32
+ print ('Count Number of 5s in t:' ,t .count (5 ))
33
+ print ('Index of 5 and 6 in t:' ,t .index (5 ))
34
+
35
+
36
+ def func_list ():
37
+ print ('Demo for LISTs in python.' )
38
+ print ('Note: Lists are mutable objects' )
39
+ print ('Created using square brackets.' )
40
+
41
+ l = [1 ,2 ,3 ,4 ,5 ,6 ,5 ]
42
+ print ('List:' ,l )
43
+ print ('First element:' ,l [0 ])
44
+ print ('Last element:' ,l [- 1 ])
45
+ print ('Length:' ,len (l ))
46
+ print ('Max:' ,max (l ))
47
+ print ('Min:' ,min (l ))
48
+ print ('List using list constructor:' ,list (range (10 )))
49
+ print ('Count 5s in l:' ,l .count (5 ))
50
+ print ('Index of 5 in l:' ,l .index (5 ))
51
+ print ('Appending 1000 at the end of list l:' )
52
+ l .append (1000 )
53
+ print (l )
54
+
55
+ print ('Add multiple objects at end of list.' )
56
+ l .extend (range (20 ))
57
+ print (l )
58
+
59
+ print ('Insert 300 at the begining of l:' )
60
+ l .insert (0 ,300 )
61
+ print (l )
62
+
63
+ print ('Remove 300 from l:' )
64
+ l .remove (300 )
65
+ print (l )
66
+
67
+ print ('Delete item at index 10 from l:' )
68
+ del l [10 ]
69
+ print (l )
70
+
71
+ print ('Remove and return that removed value from end of the list l:' )
72
+ a = l .pop ()
73
+ print ('Popped element of l:' ,a )
74
+ print ('l:' ,l )
75
+
76
+ print ('Pop from index 0:' )
77
+ a = l .pop (0 )
78
+ print ('Popped element:' ,a )
79
+ print ('l:' ,l )
80
+
81
+
82
+ def func_dictionaries ():
83
+ print ('Dictionaries are python version of associative arrays or hash arrays.' )
84
+ d = {'one' :2 ,'two' :2 ,'three' :3 ,'four' :4 ,'human' :'me' }
85
+ x = dict (animal = 'dog' ,god = 'almighty' ,hero = 'batman' )
86
+ print ('d-> {}' .format (d ))
87
+ print ('x-> {}' .format (x ))
88
+
89
+ print ('Combine two dictionaries:' )
90
+ a = {** d ,** x }
91
+ print ('Combined Dictionaries:' ,a )
92
+
93
+ print ('Check for\' four\' in a:' , ('four' in a ))
94
+ print ('Check for absence of\' alpha\' in a:' , ('alpha' not in a ))
95
+
96
+ for k ,v in a .items ():print (k ,v ,end = '|' )
97
+
98
+ print ('Use get method of dictionary to get a value for key and not the key-index because key-index with throw exception.' )
99
+ print ('While get method gives\' None\' .' )
100
+ try :
101
+ print (a ['Morrow' ])
102
+ except Exception as e :
103
+ print ('Exception thrown as {}' .format (e ))
104
+
105
+ print ('Not present shown in NONE type:' ,a .get ('Morrow' ))
106
+
107
+ print ('Setting different default:' ,a .get ('Morrow' ,'Not present in dictionary' ))
108
+
109
+ print ('Popping a value from p:' )
110
+ b = a .pop ('god' )
111
+ print (a )
112
+ print ('popped value ->' ,b )
113
+
114
+ def byte_n_byte_array ():
115
+ print ('Bytes and Bytesarray contain bytes and not any other objects like in lists and tuples.' )
116
+ print ('1byte = 8 bits' )
117
+ print ('1byte = 256 different values' )
118
+ print ('bytearray - mutable list of bytes.' )
119
+ print ('Note: ord(\' char\' ) gives the integer equivalent of that character.' )
120
+ print ('Normal ASCII range: 0 to 127' )
121
+
122
+ print ('Open file sample.txt for reading(\' r\' ) and with encoding as utf_8 and ignore the default encoding.' )
123
+ file_in = open ('sample.txt' ,'r' ,encoding = 'utf_8' )# open input file
124
+
125
+ file_out = open ('sample.html' ,'w' )# opening output file to write('w')
126
+
127
+ outbytes = bytearray ()# initialize the byte array
128
+
129
+ for line in file_in :# iterate through the file
130
+ for c in line :# iterate through each character of the line
131
+ if ord (c )> 127 :# check if the integer equivalent of the character is out of normal ascii range
132
+ outbytes += bytes ('&#{:04d};' .format (ord (c )),encoding = 'utf_8' )# append bytes to bytearray container using (+=) operator and formating as utf_8
133
+ else :outbytes .append (ord (c ))# if character is with the normal ascii range append as it is to bytearray container
134
+
135
+ outstring = str (outbytes ,encoding = 'utf_8' )# output as string from bytearray
136
+ print (outstring ,file = file_out )# print the output string to the output file file_out(sample.html)
137
+ print (outstring )
138
+
139
+ print ('Yo!!' )
140
+
141
+ def main ():
142
+ func_tuple ()
143
+ print ()
144
+ func_list ()
145
+ print ()
146
+ func_dictionaries ()
147
+ print ()
148
+ byte_n_byte_array ()
149
+
150
+ if __name__ == '__main__' :main ()