python2 與 pyhton3的輸入語句寫法小結(jié)
什么是輸入
咱們在銀行ATM機器前取錢時,肯定需要輸入密碼,對不?
那么怎樣才能讓程序知道咱們剛剛輸入的是什么呢??
大家應(yīng)該知道了,如果要完成ATM機取錢這件事情,需要先從鍵盤中輸入一個數(shù)據(jù),然后用一個變量來保存,是不是很好理解啊
1、python2的輸入語句
在python2中有兩種常見的輸入語句,input()和raw_input() 。
(1)input()函數(shù)
可以接收不同類型的參數(shù),而且返回的是輸入的類型。如,當(dāng)你輸入int類型數(shù)值,那么返回就是int型;其中字符型需要用單引號或雙引號,否則,報錯。
a.數(shù)值型輸入
>>> a = input() >>> type(a) <type 'int'> >>> a >>> a = input() 1.23 >>> type(a) <type 'float'> >>> a 1.23
b.字符類型
如果輸入的字符不加引號,就會報錯
>>> 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)然,可以對輸入的字符加以說明
>>> name = input('please input name:')
please input name:'Tom'
>>> print 'Your name : ',name
Your name : Tom
(2)raw_input()
函數(shù)raw_input()是把輸入的數(shù)據(jù)全部看做字符類型。輸入字符類型時,不需要加引號,否則,加的引號也會被看做字符。
>>> a = raw_input() >>> type(a) <type 'str'> >>> a '1' >>> a = raw_input() 'hello' >>> type(a) <type 'str'> >>> a "'hello'"
如果想要int類型數(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
在同一行中輸入多個數(shù)值,可以有多種方式,這里給出調(diào)用map() 函數(shù)的轉(zhuǎn)換方法。map使用方法請參考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ù)實現(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)成有效的表達式來求值并返回計算結(jié)果。
語法: eval(source[, globals[, locals]]) -> value
參數(shù):
source:一個Python表達式或函數(shù)compile()返回的代碼對象
globals:可選。必須是dictionary
locals:可選。任意map對象
實例展示:
可以把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輸入語句
python3中的輸入語句只有input()函數(shù),沒有raw_input();而且python3中的input()函數(shù)與python2中的raw_input()的使用方法一樣。
>>> a = input() 10 >>> type(a) <class 'str'> >>> a '10'
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
python中requests爬去網(wǎng)頁內(nèi)容出現(xiàn)亂碼問題解決方法介紹
這篇文章主要介紹了python中requests爬去網(wǎng)頁內(nèi)容出現(xiàn)亂碼問題解決方法,2017-10-10
python實現(xiàn)按鍵精靈找色點擊功能教程,使用pywin32和Pillow庫
這篇文章主要介紹了python實現(xiàn)按鍵精靈找色點擊功能教程,使用pywin32和Pillow庫,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

