python2 與 pyhton3的輸入語句寫法小結(jié)
什么是輸入
咱們在銀行ATM機器前取錢時,肯定需要輸入密碼,對不?
那么怎樣才能讓程序知道咱們剛剛輸入的是什么呢??
大家應該知道了,如果要完成ATM機取錢這件事情,需要先從鍵盤中輸入一個數(shù)據(jù),然后用一個變量來保存,是不是很好理解啊
1、python2的輸入語句
在python2中有兩種常見的輸入語句,input()
和raw_input()
。
(1)input()函數(shù)
可以接收不同類型的參數(shù),而且返回的是輸入的類型。如,當你輸入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'
當然,可以對輸入的字符加以說明
>>> 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當成有效的表達式來求值并返回計算結(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)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Python線程池ThreadPoolExecutor使用方式
這篇文章主要介紹了Python線程池ThreadPoolExecutor使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02Python實現(xiàn)的檢測web服務器健康狀況的小程序
這篇文章主要介紹了Python實現(xiàn)的檢測web服務器健康狀況的小程序,本文使用socket庫來實現(xiàn),需要的朋友可以參考下2014-09-09Python關(guān)于__name__屬性的含義和作用詳解
在本篇文章里小編給大家分享的是關(guān)于Python關(guān)于__name__屬性的含義和作用知識點,需要的朋友們可以參考下。2020-02-02python3對接mysql數(shù)據(jù)庫實例詳解
這篇文章主要介紹了python3對接mysql數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04python基礎(chǔ)之面對對象基礎(chǔ)類和對象的概念
這篇文章主要介紹了python面對對象基礎(chǔ)類和對象的概念,實例分析了Python中返回一個返回值與多個返回值的方法,需要的朋友可以參考下2021-10-10