本文實例講述了python常用函數(shù)與用法。分享給大家供大家參考,具體如下:
自定義函數(shù)實例
# 定義一個函數(shù) def printme( str ): "打印任何傳入的字符串" print str; return; # 使用這個函數(shù) printme("chtml.cn");
運行結(jié)果:
chtml.cn
刪除一個文件函數(shù)實例
def dellFile(pathFile): import os filename = pathFile if os.path.exist(filename): os.remove(filename) print filename return;
python打印金子塔
def printPyramid(level): for i in range(level): print ' ' * (level-i-1) + '*' * (2*i+1) printPyramid(5)
運行結(jié)果:
??? *
?? ***
? *****
?*******
*********
python寫九九乘法表
print '\n9x9 Table\n' for i in range(1, 10) : for j in range(1, i+1) : print j, 'x', i, '=', j*i, '\t', # print '%d x %d = %d\t' %(j, i, j*i), print '\n' print '\nDone!'
運行結(jié)果:
9x9 Table1 x 1 = 1 ?
1 x 2 = 2 ?
2 x 2 = 4 ?
1 x 3 = 3 ?
2 x 3 = 6 ?
3 x 3 = 9 ?
1 x 4 = 4 ?
2 x 4 = 8 ?
3 x 4 = 12 ?
4 x 4 = 16 ?
1 x 5 = 5 ?
2 x 5 = 10 ?
3 x 5 = 15 ?
4 x 5 = 20 ?
5 x 5 = 25 ?
1 x 6 = 6 ?
2 x 6 = 12 ?
3 x 6 = 18 ?
4 x 6 = 24 ?
5 x 6 = 30 ?
6 x 6 = 36 ?
1 x 7 = 7 ?
2 x 7 = 14 ?
3 x 7 = 21 ?
4 x 7 = 28 ?
5 x 7 = 35 ?
6 x 7 = 42 ?
7 x 7 = 49 ?
1 x 8 = 8 ?
2 x 8 = 16 ?
3 x 8 = 24 ?
4 x 8 = 32 ?
5 x 8 = 40 ?
6 x 8 = 48 ?
7 x 8 = 56 ?
8 x 8 = 64 ?
1 x 9 = 9 ?
2 x 9 = 18 ?
3 x 9 = 27 ?
4 x 9 = 36 ?
5 x 9 = 45 ?
6 x 9 = 54 ?
7 x 9 = 63 ?
8 x 9 = 72 ?
9 x 9 = 81 ?
Done!
讀取文件內(nèi)容
all_the_text = open('thefile.txt').read( )
讀取文件夾里的所有文件
all_the_data = open('abinfile','rb').read( )
關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
