Jan 1, 2018

[Python] an example of print-formatting, list-handling

a=3.141592
b=1.23
c=89
print(format(a, '.2f'), format(a, '10.2f'))
print('{0:5f}'.format(a), '{0:5f}'.format(b), '{0:5d}'.format(c))
print('{0:>8f}'.format(a), '{0:>8f}'.format(b), '{0:>8d}'.format(c))
print('{0:<8f}'.format(a), '{0:<8f}'.format(b), '{0:<8d}'.format(c))
print()

d="AbCdbeFgbhcC"
print(d[1:3], d[0], d[1:], d[:5])
print(d.upper(), d.lower())
print(d[1:3].upper(), d[:5].lower())
print(d.find('de'))
print(d.find('de',3))
print(d.find('de',4))
print(d.find('the'))
print(d.count('c'))
print(d.replace('C','Z'))
print(d.split('b'))

e=d.split('b')
f="="
print(f.join(e))
print()

e.append('ddddd')
e.append(3)
print(e)
e.insert(2,'xx')
print(e)
e.extend([1,1,2,'vv'])
print(e)
print(e.index(1), e.index('xx'))
print(e.count(1))
e.pop(0)
print(e)
e.pop()
print(e)
e.remove('xx')
print(e)
print()

g=[1,3,4,2,0]
g.sort()
print(g)
g.sort(reverse=True)
print(g)

h=['x','aa','b','yy','c','d','e']
h.sort()
print(h)
h.reverse()
print(h)


Result--------------------------------------------------------
3.14       3.14
3.141592 1.230000    89
3.141592 1.230000       89
3.141592 1.230000 89      

bC A bCdbeFgbhcC AbCdb
ABCDBEFGBHCC abcdbefgbhcc
BC abcdb
-1
-1
-1
-1
1
AbZdbeFgbhcZ
['A', 'Cd', 'eFg', 'hcC']
A=Cd=eFg=hcC

['A', 'Cd', 'eFg', 'hcC', 'ddddd', 3]
['A', 'Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3]
['A', 'Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2, 'vv']
7 2
2
['Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2, 'vv']
['Cd', 'xx', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2]
['Cd', 'eFg', 'hcC', 'ddddd', 3, 1, 1, 2]

[0, 1, 2, 3, 4]
[4, 3, 2, 1, 0]
['aa', 'b', 'c', 'd', 'e', 'x', 'yy']
['yy', 'x', 'e', 'd', 'c', 'b', 'aa']


No comments:

Post a Comment