Python環境安裝 Hello World

python3.0已推出,但據說很多庫都不能用了,建議使用2.6版本,我目前使用的是2.5版,與2.6版差距不大。

注意:2.6版本開始,print需要加上括號,否則會提示語法錯誤。

安裝python運行環境:

下載for windows的安裝包,http://www.python.org/,正式對外的下載地址被和諧了

到這裏下載:http://www.python.org/ftp/python/ 運行下載的.msi文件執行安裝程序,

默認會安裝在系統盤符:/python25目錄下,當然你可以更改該目錄,

但建議使用默認值,安裝完成後會自動註冊環境變量

運行cmd,執行python:

D:>python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

表示安裝成功,>>>爲python默認的提示符。

首先來一個經典的hello,world

>>> print hello world
hello world

在此,有必要先認識一些系統內置的非常有用的一些函數

dir()函數用來顯示一個類的所有屬性和方法,如:

>>> dir(this is a string)
[__add__, __class__, __contains__, __delattr__, __doc__, __eq__, __
ge__, __getattribute__, __getitem__, __getnewargs__, __getslice__, __g
t__, __hash__, __init__, __le__, __len__, __lt__, __mod__, __mul__
, __ne__, __new__, __reduce__, __reduce_ex__, __repr__, __rmod__,
__rmul__, __setattr__, __str__, capitalize, center, count, decode,
encode, endswith, expandtabs, find, index, isalnum, isalpha, isdi
git, islower, isspace, istitle, isupper, join, ljust, lower, lst
rip, partition, replace, rfind, rindex, rjust, rpartition, rsplit
, rstrip, split, splitlines, startswith, strip, swapcase, title,
translate, upper, zfill]
>>>

基本上,以__(雙劃線)開頭的方法,你是不能直接通過類去調用(但可以通過其它的途徑去調用),它們相當於php類中的”魔術方法”。

type()函數用來顯示當前變量的類型,如:

>>> m=1L
>>> n=1
>>> i=1.3
>>> a=123
>>> b=range(10)
>>> type(m)

>>> type(n)

>>> type(i)

>>> type(a)

>>> type(b)

>>> type(type(b))

id()函數用來顯示指定變量的內存地址

>>> a=b=123
>>> m=n=123
>>> id(a),id(b)
(3178240, 3178240)
>>> id(m),id(n)
(5817024, 5817024)
>>> m=1
>>> id(m)
5792000

help()顧名思義是用來查看幫助的

>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within s[start,end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.

另外,python的語句塊是用縮進來標識的,不像c/c++/java/c#那樣用{}來匹分,初次接觸可能會不習慣,但時間長了,你會喜歡上這樣的風格,因爲它會讓你感覺看所有的python代碼都是一樣的,不會出現參差不齊的”括號”風格。

開始我們的python之旅吧。

發佈了50 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章