python常用函數(shù)與用法示例
本文實例講述了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 = 81Done!
讀取文件內(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è)計有所幫助。
- Python3的urllib.parse常用函數(shù)小結(jié)(urlencode,quote,quote_plus,unquote,unquote_plus等)
- Python time模塊詳解(常用函數(shù)實例講解,非常好)
- Python pandas常用函數(shù)詳解
- Python OS模塊常用函數(shù)說明
- Python numpy 常用函數(shù)總結(jié)
- Python正則表達式常用函數(shù)總結(jié)
- Python學(xué)習(xí)筆記之常用函數(shù)及說明
- Python字符串和文件操作常用函數(shù)分析
- python常用函數(shù)詳解
- Python中functools模塊的常用函數(shù)解析
相關(guān)文章
node.js獲取參數(shù)的常用方法(總結(jié))
下面小編就為大家?guī)硪黄猲ode.js獲取參數(shù)的常用方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
PyTorch?可視化工具TensorBoard和Visdom
這篇文章主要介紹了PyTorch?可視化工具TensorBoard和Visdom,TensorBoard?一般都是作為?TensorFlow?的可視化工具,與?TensorFlow?深度集成,它能夠展現(xiàn)?TensorFlow?的網(wǎng)絡(luò)計算圖,繪制圖像生成的定量指標(biāo)圖以及附加數(shù)據(jù)等,下面來看文章得具體內(nèi)容介紹吧2022-01-01
Python實現(xiàn)監(jiān)控Nginx配置文件的不同并發(fā)送郵件報警功能示例
這篇文章主要介紹了Python實現(xiàn)監(jiān)控Nginx配置文件的不同并發(fā)送郵件報警功能,涉及Python基于difflib模塊的文件比較及smtplib模塊的郵件發(fā)送相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
python如何發(fā)送xml格式請求數(shù)據(jù)
這篇文章主要介紹了python如何發(fā)送xml格式請求數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06

