本節主要介紹print,import和input,t函數,包括他們在python2.7和python3 的區別以及用法。下面附有以前的文章;
html
python3以前的print是簡單的語句好比要打印hello worldpython
>>> print 'hello world' hello world >>>
而python3以後的版本中print已經變爲了函數。好比要打印必須加上();以下:linux
#直接按語句打印會出現錯誤: peace@peace:~$ python Python 3.4.3 (default, Mar 26 2015, 22:03:40) [GCC 4.9.2] on linux Type "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' #應該用函數打印 >>> print ('hello world') hello world >>>
注意(python3以前的print語句功能基本沒有變)正則表達式
使用print時,也能夠在語句中添加多個表達式,每一個表達式用逗 號分隔;在用逗號分隔輸出時,print語句會在每一個輸出項後面自動添加一 個空格;
注意:無論時字符串仍是其餘類型都是轉化爲字符串進行打印python2.7
>>> print('peace',22) peace 22 >>> print(1,2,3) 1 2 3 #輸出元祖必須這樣輸出; >>> print((1,2,3)) (1, 2, 3) >>> #變量也是能夠的 >>> name='peace' >>> print(name,22) peace 22 >>> #可使用‘+’鏈接字符串 >>> print('hello'+','+'peace') hello,peace >>> #若是在結尾加上逗號,name接下來的語句會與前一行、打印在一行;(python3以前纔有效) #創建douhao.py在裏面輸入 print 'peace', print 22 #在輸入以下語句便可: peace@peace:~/workspace/python$ python2.7 douhao.py peace 22
將整個模塊導入,格式爲:import somemodule;
從某個模塊中導入某個函數,格式爲:from somemodule import somefunction;
從某個模塊中導入多個函數,格式爲:from somemodule import firstfunc, secondfunc, thirdfunc
將某個模塊中的所有函數導入,格式爲:from somemodule import *函數
1導入模塊使用模塊名進行調用;spa
#導入模塊 import module1 import module2 #調用同名函數的方法 module1.open() module2.open()
2使用關鍵字as進行更名code
#導入函數,並給函數取相應的別名 from module1 import open as open1 from module2 import open as open2
3例子orm
>>> from cmath import cos as s >>> from math import cos as c >>> s(30) (0.15425144988758405+0j) >>> c(30) 0.15425144988758405 >>>
Python3中用input()取代了raw_input(),固然這僅僅是重命名,使用上並無不一樣;python3以前的input()再也不取用;
input()函數無論你輸入什麼返回的是字符串;htm
#與python3以前的raw_input()相同 >>> k=input('intput int ') intput int 12 >>> k '12' >>>
python3入門之類
python3入門之函數
python3入門之循環
python3之if語句
python3入門之賦值語句介紹
python3入門之print,import,input介紹
python3入門之set
python3入門之字典
python3入門之字符串
python3入門之列表和元組
python3入門之軟件安裝
python3爬蟲之入門和正則表達式