Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4de37b9

Browse files
committed
Add demo for python containers - list, dictionaries, tuples, bytearrays
1 parentd28f87e commit4de37b9

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

‎Containers/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Containers</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

‎Containers/.pydevproject

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathpropertyname="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/${PROJECT_DIR_NAME}/src</path>
5+
</pydev_pathproperty>
6+
<pydev_propertyname="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.6</pydev_property>
7+
<pydev_propertyname="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
8+
</pydev_project>

‎Containers/src/containerdemo.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
'''
2+
Created on Aug 7, 2017
3+
4+
@author: Aditya
5+
6+
This program demonstrates the CONTAINERS in python
7+
'''
8+
9+
deffunc_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\':', (10int))
29+
print('Check for presence of 50 in\'t\':', (50int))
30+
print('Check for absence of 50 in\'t\':', (50notint))
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+
deffunc_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+
dell[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+
deffunc_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'ina))
94+
print('Check for absence of\'alpha\' in a:', ('alpha'notina))
95+
96+
fork,vina.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+
exceptExceptionase:
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+
defbyte_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+
forlineinfile_in:# iterate through the file
130+
forcinline:# iterate through each character of the line
131+
iford(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+
defmain():
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()

‎Containers/src/sample.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a UTF-8 file.
2+
It has some interesting characters in it.
3+
&#1641;(&#0865;&#3663;&#0815;&#0865;&#3663;)&#1782;
4+

‎Containers/src/sample.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a UTF-8 file.
2+
It has some interesting characters in it.
3+
٩(͡๏̯͡๏)۶

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp