python2 與 pyhton3的輸入語(yǔ)句寫法小結(jié)
什么是輸入
咱們?cè)阢y行ATM機(jī)器前取錢時(shí),肯定需要輸入密碼,對(duì)不?
那么怎樣才能讓程序知道咱們剛剛輸入的是什么呢??
大家應(yīng)該知道了,如果要完成ATM機(jī)取錢這件事情,需要先從鍵盤中輸入一個(gè)數(shù)據(jù),然后用一個(gè)變量來保存,是不是很好理解啊
1、python2的輸入語(yǔ)句
在python2中有兩種常見的輸入語(yǔ)句,input()
和raw_input()
。
(1)input()函數(shù)
可以接收不同類型的參數(shù),而且返回的是輸入的類型。如,當(dāng)你輸入int類型數(shù)值,那么返回就是int型;其中字符型需要用單引號(hào)或雙引號(hào),否則,報(bào)錯(cuò)。
a.數(shù)值型輸入
>>> a = input() >>> type(a) <type 'int'> >>> a >>> a = input() 1.23 >>> type(a) <type 'float'> >>> a 1.23
b.字符類型
如果輸入的字符不加引號(hào),就會(huì)報(bào)錯(cuò)
>>> r = input() hello Traceback (most recent call last): File "<pyshell#50>", line 1, in <module> r = input() File "<string>", line 1, in <module> NameError: name 'hello' is not defined
正確的字符輸入
>>> r = input() 'hello' >>> r 'hello' >>> r = input() "hello" >>> r 'hello'
當(dāng)然,可以對(duì)輸入的字符加以說明
>>> name = input('please input name:') please input name:'Tom' >>> print 'Your name : ',name Your name : Tom
(2)raw_input()
函數(shù)raw_input()是把輸入的數(shù)據(jù)全部看做字符類型。輸入字符類型時(shí),不需要加引號(hào),否則,加的引號(hào)也會(huì)被看做字符。
>>> a = raw_input() >>> type(a) <type 'str'> >>> a '1' >>> a = raw_input() 'hello' >>> type(a) <type 'str'> >>> a "'hello'"
如果想要int類型數(shù)值時(shí),可以通過調(diào)用相關(guān)函數(shù)轉(zhuǎn)化。
>>> a = int(raw_input()) >>> type(a) <type 'int'> >>> a >>> a = float(raw_input()) 1.23 >>> type(a) <type 'float'> >>> a 1.23
在同一行中輸入多個(gè)數(shù)值,可以有多種方式,這里給出調(diào)用map()
函數(shù)的轉(zhuǎn)換方法。map使用方法請(qǐng)參考python-map的用法
>>> a, b = map(int, raw_input().split()) 20 >>> a >>> b >>> l = list(map(int, raw_input().split())) 2 3 4 >>> l [1, 2, 3, 4]
(3)input() 和raw_input()的區(qū)別
通過查看input()
幫助文檔,知道input函數(shù)也是通過調(diào)用raw_input函數(shù)實(shí)現(xiàn)的,區(qū)別在于,input函數(shù)額外調(diào)用內(nèi)聯(lián)函數(shù)eval()。eval使用方法參考Python eval 函數(shù)妙用 (見下面)
>>> help(input) Help on built-in function input in module __builtin__: input(...) input([prompt]) -> value Equivalent to eval(raw_input(prompt)). >>> help(eval) Help on built-in function eval in module __builtin__: eval(...) eval(source[, globals[, locals]]) -> value Evaluate the source in the context of globals and locals. The source may be a string representing a Python expression or a code object as returned by compile(). The globals must be a dictionary and locals can be any mapping, defaulting to the current globals and locals. If only globals is given, locals defaults to it.
Python eval 函數(shù)妙用
eval
功能:將字符串str當(dāng)成有效的表達(dá)式來求值并返回計(jì)算結(jié)果。
語(yǔ)法: eval(source[, globals[, locals]]) -> value
參數(shù):
source:一個(gè)Python表達(dá)式或函數(shù)compile()返回的代碼對(duì)象
globals:可選。必須是dictionary
locals:可選。任意map對(duì)象
實(shí)例展示:
可以把list,tuple,dict和string相互轉(zhuǎn)化。 ################################################# 字符串轉(zhuǎn)換成列表 >>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" >>>type(a) <type 'str'> >>> b = eval(a) >>> print b [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> type(b) <type 'list'> ################################################# 字符串轉(zhuǎn)換成字典 >>> a = "{1: 'a', 2: 'b'}" >>> type(a) <type 'str'> >>> b = eval(a) >>> print b {1: 'a', 2: 'b'} >>> type(b) <type 'dict'> ################################################# 字符串轉(zhuǎn)換成元組 >>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))" >>> type(a) <type 'str'> >>> b = eval(a) >>> print b ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0)) >>> type(b) <type 'tuple'>
2、Python3輸入語(yǔ)句
python3中的輸入語(yǔ)句只有input()
函數(shù),沒有raw_input();
而且python3中的input()
函數(shù)與python2中的raw_input()
的使用方法一樣。
>>> a = input() 10 >>> type(a) <class 'str'> >>> a '10'
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Python線程池ThreadPoolExecutor使用方式
這篇文章主要介紹了Python線程池ThreadPoolExecutor使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02淺談Python實(shí)現(xiàn)貪心算法與活動(dòng)安排問題
本篇文章主要介紹了淺談Python實(shí)現(xiàn)貪心算法與活動(dòng)安排問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序
這篇文章主要介紹了Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序,本文使用socket庫(kù)來實(shí)現(xiàn),需要的朋友可以參考下2014-09-09python函數(shù)默認(rèn)參數(shù)使用避坑指南
這篇文章主要為大家介紹了python函數(shù)默認(rèn)參數(shù)使用的踩雷避坑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Python關(guān)于__name__屬性的含義和作用詳解
在本篇文章里小編給大家分享的是關(guān)于Python關(guān)于__name__屬性的含義和作用知識(shí)點(diǎn),需要的朋友們可以參考下。2020-02-02python3對(duì)接mysql數(shù)據(jù)庫(kù)實(shí)例詳解
這篇文章主要介紹了python3對(duì)接mysql數(shù)據(jù)庫(kù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04解決Python 寫文件報(bào)錯(cuò)TypeError的問題
這篇文章主要介紹了解決Python 寫文件報(bào)錯(cuò)TypeError的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10關(guān)于如何把Python對(duì)象存儲(chǔ)為文件的方法詳解
本文將給大家介紹如何把Python對(duì)象存儲(chǔ)為文件的方法,pickle可以用二進(jìn)制表示并讀寫python數(shù)據(jù),這個(gè)功能并不安全,如果把一個(gè)pickle暴露給別人,有被植入惡意程序的風(fēng)險(xiǎn),文中通過代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2024-01-01python基礎(chǔ)之面對(duì)對(duì)象基礎(chǔ)類和對(duì)象的概念
這篇文章主要介紹了python面對(duì)對(duì)象基礎(chǔ)類和對(duì)象的概念,實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下2021-10-10