"hello world"是編程界一個(gè)經(jīng)久不衰的例子,幾乎所有語言的學(xué)習(xí)教程都把它當(dāng)做第一個(gè)程序的范例。學(xué)習(xí)的過程就是再造輪子的過程,千萬不要以為有人做過的,就不去學(xué)習(xí)了。
我們先打開CPython解釋器。
o@o-pc:~$ python2.7Python 2.7.10 (default, Jun 17 2015, 14:15:05) [GCC 4.9.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> 打開之后就可以在>>>的后面輸入python語句了。
我們先試一下PRint這個(gè)命令,看是否成功輸出"hello world"
>>> print "hello world"hello world因?yàn)檫@是在python2.7環(huán)境下,如果切換到python3.x這就行不通了。不信請看
o@o-pc:~$ python3.4Python 3.4.3 (default, Mar 26 2015, 22:03:40) [GCC 4.9.2] on linuxType "help", "copyright", "credits" or "license" for more information.>>> print "hello world"  File "<stdin>", line 1    print "hello world"                      ^SyntaxError: Missing parentheses in call to 'print'在python3.4下報(bào)了一個(gè)錯(cuò)誤,意思是語法錯(cuò)誤,調(diào)用print的時(shí)候缺少括號。
我們使用有括號的版本就好了。
>>> print ("hello world")hello world上面是在python的交互界面下輸出的"hello world",那么有沒有辦法不使用這種交互界面,而是寫一個(gè)python腳本文件呢?當(dāng)然是可以了啦。
我們可以新建一個(gè)文件2.py,然后寫入
print("hello world")
然后我們使用python 2.py來執(zhí)行它。
o@o-pc:~$ python 2.py hello world關(guān)于python腳本的文件名,并不一定要以.py做后綴,這只是比較通用的做法。
如果你想指定這個(gè)腳本的解釋器,那么可以在腳本的最前面加上一行來指明你所選擇的解釋器,例如#! /bin/python3.4
#! /bin/python3.4
print("hello world")
學(xué)習(xí)過linux/unix系統(tǒng)編程的人,應(yīng)該對manpage這個(gè)東西是比較熟悉的。而python中提供了一個(gè)類似的東西,那就是help函數(shù)了。如果不清楚某個(gè)函數(shù)怎么用,就使用help(函數(shù)名)來獲取相關(guān)的文檔信息。
python 官方文檔中文站
先來獲取一下print函數(shù)的用法
>>> help(print)print(...)    print(value, ..., sep=' ', end='/n', file=sys.stdout, flush=False)        Prints the values to a stream, or to sys.stdout by default.    Optional keyWord arguments:    file:  a file-like object (stream); defaults to the current sys.stdout.    sep:   string inserted between values, default a space.    end:   string appended after the last value, default a newline.    flush: whether to forcibly flush the stream.全是英文的,看起來有困難吧。那就別管它了,我來解釋一下。
print函數(shù)可以用來打印一些值,默認(rèn)是輸出到標(biāo)準(zhǔn)輸出。file參數(shù)可以控制輸出流到別的位置。end參數(shù)控制字符串輸出后是否自動(dòng)加'/n',你可以改為別的。sep參數(shù)用于控制分割符,默認(rèn)是空格。flush參數(shù)控制是否強(qiáng)制刷新流。
這樣說是不是還不清除,沒關(guān)系,舉個(gè)栗子嘛。
---
>>> print(1,"+",2,"=",1+2,sep='/t',end=' ')1	+	2	=	3 >>> print(1,"+",2,"=",1+2)1 + 2 = 3>>> 看上面的輸出,分割符換成了'/t',結(jié)尾沒有加'/n'的。對比一下就很清楚了吧。
這是在python3.4下做的,如果換做python2.7,那么輸出會(huì)變成
>>> print(1,"+",2,"=",1+2)(1, '+', 2, '=', 3)這是兩者的區(qū)別。
print的格式化輸出,可以參考C語言的printf函數(shù)的格式化選項(xiàng),這是類似的。
看下面的代碼
>>> a=1000>>> print("%d"%a)1000>>> b=12.345>>> print("%f"%b)12.345000>>> print("%f"%(a+b))1012.345000>>> print("%d"%(a+b))1012分析一下。
這里先是定義了一個(gè)變量a,并賦值為1000,然后使用print來格式化輸出。%d表示以整數(shù)的方式來輸出,后面緊跟的%a是取變量a的值的意思,和shell腳本中的$有點(diǎn)類似。
后門又定義了一個(gè)變量b,賦值為123.45,然后以浮點(diǎn)數(shù)的形式輸出。
最后兩個(gè)是輸出鏈路這兩者的和。注意,python中的數(shù)據(jù)是向下取整的。python中的變量不像C/C++這類強(qiáng)類型的語言,它的變量類型只與其最后一次被賦值有關(guān)。看下面的語言,重新給變量b賦值了一個(gè)字符串"hello",然后輸出它。
>>> b="hello">>> print("%s"%b)hello再看這個(gè)
>>> type(a)<class 'int'>>>> type(b)<class 'str'>>>> b=123>>> type(b)<class 'int'>要注意,格式化字符串一定要用""包含起來,并且后面緊跟要輸出的變量。有兩個(gè)格式化選項(xiàng)%s和%r比較特殊,無論變量保存的數(shù)據(jù)類型是什么,都能正常輸出。
原因是%s調(diào)用的是str()函數(shù)把對象轉(zhuǎn)化為str類型,而%r是調(diào)用了repr()將對象轉(zhuǎn)化為字符串。
下面是在python2.7下進(jìn)行的,python3.x已經(jīng)不支持這種默認(rèn)轉(zhuǎn)換了。
>>> import time>>> d=time.localtime()>>> print dtime.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)>>> print "%s"%dtime.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)>>> print "%r"%dtime.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)python3.x可以這樣做,而且這樣做是值得提倡的做法。
>>> print("%s"%(str(d)))time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=44, tm_sec=49, tm_wday=2, tm_yday=168, tm_isdst=0)>>> print("%s"%repr(d))time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=44, tm_sec=49, tm_wday=2, tm_yday=168, tm_isdst=0)print可以直接輸出變量的,是按照變量的類型來輸出的。關(guān)于python變量,將在下一篇文檔中詳述。
>>> a=10.10>>> b=123>>> c="nihao">>> d='c'>>> print(a,b,c,d)10.1 123 nihao c新聞熱點(diǎn)
疑難解答
圖片精選