吐槽下,python2 官方文件的print資料好少啊,看的別人部落格才知道咋用╮(╯_╰)╭
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
print(sys.version)
#輸入與輸出
#str()與repr()
for x in range(1, 11):
print repr(x).rjust(2), repr(x*x).rjust(3),
#逗號代表不換行
print repr(x*x*x).rjust(4)
#格式化輸出
for x in range(1, 11):
print ' '.format(x, x*x, x*x*x)
print 'we are the {} who say "{}!"'.format('knights', 'ni')
print ' and '.format('spam', 'eggs')
print 'this is .'.format(food='spam', adjective='absolutely horrible')
print 'the story of , , and .'.format('bill', 'manfred', other='georg')
import math
#保留三位小數點
table =
for name, phone in table.items():
print ' ==> '.format(name, phone)
print 'jack: ; sjoerd: ; dcab: '.format(**table)
#old style, 語法類似sprintf
print 'x = %d, y = %d' % (3, 4)
# #檔案的讀寫
# f = open('/tmp/workfile', 'r+')
# print f
## #f.write('this is a test\n')
## f.read()
## f.readline()
## f.readlines()
## for line in f:
# print line
## #write something other than string
# value = ('the answer', 42)
# s = str(value)
# f.write(s)
## f = open('/tmp/workfile', 'r+')
# f.write('0123456789abcdef')
# f.seek(5) # go to the 6th byte in the file
# f.read(1)
# f.seek(-3, 2) # go to the 3rd byte before the end
# f.read(1)
## f.close()
# f.read()
## with open('/tmp/workfile', 'r') as f:
# read_data = f.read()
## f.closed
if __name__ == "__main__":
a = 1
b = 2
python(2) 函式相關
可變引數 def enroll name,gender,age 6,city beijing print name name print gender gender print age age print city city 呼叫 enroll bob m 7 enroll adam m city ...
python2和3版本 print輸出到檔案
要將程式的輸出送到乙個檔案中,需要在 print 語句後面使用 指定乙個檔案,如下所示 principal 1000 初始金額 rate 0.05 利率 numyears 5 年數 year 1f open out.txt w 開啟檔案以便寫入 while year numyears princip...
Python2語法簡記(7) 函式
def printme str1,str2 列印傳入的字串 函式的第一行語句可以選擇性地使用文件字串 用於存放函式說明。print str1,str2 return str1,str2 return只能返回乙個值,可以是任何型別。寫成這樣是返回乙個元祖,間接實現了返回多個值。不可變物件傳入函式 不能...