Python基礎知識 python模組的引入方法

2021-07-10 19:35:30 字數 745 閱讀 8217

在python

開發()中用關鍵字import

來引入某個模組,也就類似於c++

中include

某個標頭檔案。比如要引用模組math

,就可以在檔案最開始的地方用

import math

來引入。在呼叫

math

模組中的函式時,必須這樣引用,詳情見**:

>>>improt math

>>>math.sin(0)

匯入自己的模組:

#hello.pyprint "hello world!"

將上面的檔案儲存在c:/python

下面:

import sys

>>>import hello

hello world!

記住只匯入一次:

>>>import hellohello world!

>>>import hello#空

模組用於定義函式:

#hello2.pydef hello():

print "hello, world!"

>>>import hello2

>>>hello2.hello()

hello, world!

下面就是一些雜項:

包 為了更好的組織模組,可以將他們分組,即包。

import contantsprint contants.pi

help幫助

>>>help(copy.copy)

Python 基礎知識

來自 一 識別符號 1.python 中的識別符號是區分大小寫的。2.標示符以字母或下劃線開頭,可包括字母,下劃線和數字。3.以下劃線開頭的識別符號是有特殊意義的。以單下劃線開頭 foo 的代表不能直接訪問的類屬性,需通過類提供的介面進行訪問,不能用 from import 而匯入 以雙下劃線開頭的...

python基礎知識

一.隨機數的生成 都需要 import random 1.用於生成乙個指定範圍內的隨機浮點數。print random.uniform 10,20 print random.uniform 20,10 2.生成乙個指定範圍內的整數。下限必須小於上限制 print random.randint 12,...

python基礎知識

py基礎學習 1.命令列輸入python命令式,例如python test.py c m install sys.argv test.py c m install 2.切片,str 0 5 1 含頭不含尾,表示倒序切 3.unicode和encode unicode str,utf 8 將utf 8...