1 |
#!/usr/local/bin/python |
2 |
#---------------------------------------------------------------------------------------------------- |
3 |
#$Header$ |
4 |
#---------------------------------------------------------------------------------------------------- |
5 |
#David T. Ashley, 11/11/2018 |
6 |
#Chapter 5 Python experiments |
7 |
#---------------------------------------------------------------------------------------------------- |
8 |
#Importing of modules. |
9 |
import sys |
10 |
|
11 |
#Determining which system one is running on. |
12 |
print(sys.platform) |
13 |
|
14 |
#Arbitrary-precision arithmetic: integers grow automatically. |
15 |
print(2**100) |
16 |
|
17 |
#String repetition. |
18 |
x = 'Spam! ' |
19 |
print(x*8) |
20 |
|
21 |
#Pausing to accept the <ENTER> key--useful when using an icon to launch |
22 |
#a script. |
23 |
#input() |
24 |
|
25 |
#String concatenation, comment. |
26 |
print('The bright side of' + ' Life ...') #Comment. |
27 |
|
28 |
#Built in methods. Conversion of strings to lists. |
29 |
s='Spam' |
30 |
print(s.find('pa')) |
31 |
print(s.replace('pa', 'XYZ')) |
32 |
line='aaa,bbb,ccccc,dd' |
33 |
print(line.split(',')) |
34 |
print(s.upper()) |
35 |
print(s.isalpha()) |
36 |
print(s.isdigit()) |
37 |
|
38 |
#Standard string operations that may be useful. |
39 |
line='aaa,bbb,ccccc,dd' |
40 |
print(line.rstrip()) |
41 |
|
42 |
#Combine two operations. Note that rstrip() runs first, because Python does |
43 |
#this from left to right. This is counterintuitive, at least to me. |
44 |
print(line.rstrip().split(',')) |
45 |
|
46 |
#Formatting. This seems to behave essentially like sprintf() in C. |
47 |
print('%s, eggs, and %s' % ('spam', 'SPAM!')) |
48 |
print('{0}, eggs, and {1}'.format('spam', 'SPAM!')) |
49 |
print('{1}, eggs, and {0}'.format('spam', 'SPAM!')) |
50 |
print('{1}, eggs, and {1}'.format('spam', 'SPAM!')) |
51 |
print('{:,.2f}'.format(296999.2567)) |
52 |
print('%.2f | %+05d' % (3.14159, -42)) |
53 |
|
54 |
#Find out what methods are available for strings. The names with |
55 |
#double underscores are for operator overloading, discussed later. |
56 |
#In general, the names with leading or trailing double underscores |
57 |
#have to do with implementation of Python internals. |
58 |
q='Now is the time' |
59 |
print(dir(q)) |
60 |
|
61 |
|
62 |
|
63 |
|