python 教程筆記day5

7. Input and Output

'Hello,world,'
repr(s)
"'Hello,world,'"
str(1/7)
'0.14285714285714285'
x = 10 * 3.25
y = 200 * 2000
s = 'The value of x is ' + repr(x) + ', and y is' + repr(y) +'...' 
print(s)
The value of x is 32.5, and y is400000...
# The repr() of a string adds string quotes and backslashes:
hello = 'hello, world\n'
hellos =  repr(hello)
print(hellos)
'hello, world\n'
# The argument to repr() may be any Python object:
repr((x,y,('spam','egges')))
"(32.5, 400000, ('spam', 'egges'))"

for x in range(1, 11):
    print(repr(x).rjust(2),repr(x*x).rjust(3),end=' ')
    # Note use of 'and' on previous line
    print(repr(x*x*x).rjust(4))
    
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000
for x in range(2,22):
    print('{0:2d} {1:3d}  {2:4d}'.format(x,x*x,x*x*x))
    
 2   4     8
 3   9    27
 4  16    64
 5  25   125
 6  36   216
 7  49   343
 8  64   512
 9  81   729
10 100  1000
11 121  1331
12 144  1728
13 169  2197
14 196  2744
15 225  3375
16 256  4096
17 289  4913
18 324  5832
19 361  6859
20 400  8000
21 441  9261

菜鳥教程網址: "www.runoob.com!"
print('{0}和{1}'.format('Google','Runoob'))
Google和Runoob
print('{1}和{0}'.format('Google','Runoob'))
Runoob和Google
print('{name}網址: {site}'.format(name='菜鳥教程',site='www.henrytien.com'))
菜鳥教程網址: www.henrytien.com
print('站點列表 {0},{1},和{other}.'.format('Google','Runoob',other='Taobao'))
站點列表 Google,Runoob,和Taobao.

print('My hovercraft is full of {}.'.format(conttens))
My hovercraft is full of eels.
print('My hovercraft if full of {!r}.'.format(conttens))
My hovercraft if full of 'eels'.

print('The value ofPI is approximately {0:.3f}.'.format(math.pi))
The value ofPI is approximately 3.142.
table = {'Google':1,'Runoob':2,'Taobao':3}
for name, number in table.items():
    print('{0:10} ==> {1:10d}'.format(name,number))
    
Google     ==>          1
Runoob     ==>          2
Taobao     ==>          3

7.2 Reading and Writing Files

f = open("C:/Users\henry\desktop/foo.txt",'rb+')
f.write(b'lsldllsas')
9
f.seek(5)
5
f.read(1)
b'l'
f.seek(-3,2)
27
f.read(1)
b'1'

7.2.2. Saving strucured data with json

import json
json.dumps([1,'simple','list'])
'[1, "simple", "list"]'
json.dump(x,f)
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容