python常用函數(shù)與用法示例
本文實(shí)例講述了python常用函數(shù)與用法。分享給大家供大家參考,具體如下:
自定義函數(shù)實(shí)例
# 定義一個(gè)函數(shù) def printme( str ): "打印任何傳入的字符串" print str; return; # 使用這個(gè)函數(shù) printme("chtml.cn");
運(yùn)行結(jié)果:
chtml.cn
刪除一個(gè)文件函數(shù)實(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)
運(yùn)行結(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!'
運(yùn)行結(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è)計(jì)入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python3的urllib.parse常用函數(shù)小結(jié)(urlencode,quote,quote_plus,unquote,unquote_plus等)
- Python time模塊詳解(常用函數(shù)實(shí)例講解,非常好)
- Python pandas常用函數(shù)詳解
- Python OS模塊常用函數(shù)說明
- Python numpy 常用函數(shù)總結(jié)
- Python正則表達(dá)式常用函數(shù)總結(jié)
- Python學(xué)習(xí)筆記之常用函數(shù)及說明
- Python字符串和文件操作常用函數(shù)分析
- python常用函數(shù)詳解
- Python中functools模塊的常用函數(shù)解析
相關(guān)文章
詳解Python中高階函數(shù)(map,filter,reduce,sorted)的使用
高階函數(shù)就是能夠把函數(shù)當(dāng)成參數(shù)傳遞的函數(shù)就是高階函數(shù),換句話說如果一個(gè)函數(shù)的參數(shù)是函數(shù),那么這個(gè)函數(shù)就是一個(gè)高階函數(shù)。本文為大家詳細(xì)講解了Python中常用的四個(gè)高階函數(shù),感興趣的可以了解一下2022-04-04Swin?Transformer模塊集成到Y(jié)OLOv5目標(biāo)檢測(cè)算法中實(shí)現(xiàn)
這篇文章主要為大家介紹了Swin?Transformer模塊集成到Y(jié)OLOv5目標(biāo)檢測(cè)算法中實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04WIndows10系統(tǒng)下面安裝Anaconda、Pycharm及Pytorch環(huán)境全過程(NVIDIA?GPU版本)
這篇文章主要給大家介紹了關(guān)于WIndows10系統(tǒng)下面安裝Anaconda、Pycharm及Pytorch環(huán)境(NVIDIA?GPU版本)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02selenium+python自動(dòng)化測(cè)試環(huán)境搭建步驟
在本文中小編給大家分享了關(guān)于selenium+python自動(dòng)化測(cè)試環(huán)境搭建的相關(guān)步驟以及知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考學(xué)習(xí)下。2019-06-06OpenCV特征匹配和單應(yīng)性矩陣查找對(duì)象詳解
這篇文章主要為大家介紹了OpenCV特征匹配和單應(yīng)性矩陣查找對(duì)象詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04python GUI庫圖形界面開發(fā)之PyQt5動(dòng)態(tài)加載QSS樣式文件
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5動(dòng)態(tài)加載QSS樣式表,需要的朋友可以參考下2020-02-02用Python的SimPy庫簡化復(fù)雜的編程模型的介紹
這篇文章主要介紹了用Python的SimPy庫簡化復(fù)雜的編程模型的介紹,本文來自于官方的開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03