對(duì)python中raw_input()和input()的用法詳解
最近用到raw_input()和input()來(lái)實(shí)現(xiàn)即時(shí)輸入,就順便找了些資料來(lái)看,加上自己所用到的一些內(nèi)容,整理如下:
1、raw_input()
raw_input([prompt]) -> string
系統(tǒng)介紹中是:讀取標(biāo)準(zhǔn)輸入的字符串。因此,無(wú)論輸入的是數(shù)字或者字符或者其他,均被視為字符格式。
如:
print "Please input a num:" k = raw_input() print k print type(k)
運(yùn)行結(jié)果為:
Please input a num: 23 23 <type 'str'>
輸入數(shù)字:23,輸出:23,類型為str;
因此,在不同的場(chǎng)景下就要求輸入的內(nèi)容進(jìn)行轉(zhuǎn)換。
1)轉(zhuǎn)為int型
print "Please input a num:" n = int(raw_input()) print n print type(n)
運(yùn)行結(jié)果為:
Please input a num: 23 23 <type 'int'>
輸入:23,輸出:23,類型為int;
2)轉(zhuǎn)為list型
print "please input list s:" s = list(raw_input()) print s print type(s)
運(yùn)行結(jié)果為:
please input list s: 23 ['2', '3'] <type 'list'>
輸入:23,輸出:[ '2','3' ],類型為list;
如何直接生成數(shù)值型的list尚未解決,算個(gè)思考題吧。
2、input()
input([prompt]) -> value Equivalent to eval(raw_input(prompt))
可以看出,input()的輸出結(jié)果是“值”,相當(dāng)于是對(duì)raw_input()進(jìn)行一個(gè)計(jì)算后的結(jié)果。
如:
print "please input something :" m = input() print m print type(m)
運(yùn)行結(jié)果1為:
please input something : 23 23 <type 'int'>
輸入:23,輸出:23,類型為int;
運(yùn)行結(jié)果2為:
please input something : abc Traceback (most recent call last): File "D:/python test/ceshi1.py", line 24, in <module> m = str(input()) File "<string>", line 1, in <module> NameError: name 'abc' is not defined
輸入:abc,輸出報(bào)錯(cuò)(字符型的輸入不通過(guò));
但也可以把input()的結(jié)果進(jìn)行轉(zhuǎn)換:
1)轉(zhuǎn)為str
print "please input something :" m = str(input()) print m print type(m)
運(yùn)行結(jié)果為:
please input something : 23 23 <type 'str'>
輸入為數(shù)值型的23,輸出:23,類型為str;
2)轉(zhuǎn)為int
print "please input something :" m = int(input()) print m print ty
運(yùn)行結(jié)果為:
please input something : 23.5 23 <type 'int'>
輸入:23.5,輸出:23,類型為int(默認(rèn)為向下取整);
注:input()不可使用list轉(zhuǎn)為列表。
以上這篇對(duì)python中raw_input()和input()的用法詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)126郵箱發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)126郵箱發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Pycharm安裝并配置jupyter notebook的實(shí)現(xiàn)
這篇文章主要介紹了Pycharm安裝并配置jupyter notebook的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Python Web框架Flask信號(hào)機(jī)制(signals)介紹
這篇文章主要介紹了Python Web框架Flask信號(hào)機(jī)制(signals)介紹,本文介紹Flask的信號(hào)機(jī)制,講述信號(hào)的用途,并給出創(chuàng)建信號(hào)、訂閱信號(hào)、發(fā)送信號(hào)的方法,需要的朋友可以參考下2015-01-01python3結(jié)合openpyxl庫(kù)實(shí)現(xiàn)excel操作的實(shí)例代碼
這篇文章主要介紹了python3結(jié)合openpyxl庫(kù)實(shí)現(xiàn)excel操作的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09詳解Python中常用的激活函數(shù)(Sigmoid、Tanh、ReLU等)
激活函數(shù) (Activation functions) 對(duì)于人工神經(jīng)網(wǎng)絡(luò)模型去學(xué)習(xí)、理解非常復(fù)雜和非線性的函數(shù)來(lái)說(shuō)具有十分重要的作用,這篇文章主要介紹了Python中常用的激活函數(shù)(Sigmoid、Tanh、ReLU等),需要的朋友可以參考下2023-04-04python通過(guò)TimedRotatingFileHandler按時(shí)間切割日志
這篇文章主要介紹了python通過(guò)TimedRotatingFileHandler按時(shí)間切割日志的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07使用Pandas和Matplotlib進(jìn)行數(shù)據(jù)清洗與可視化的實(shí)現(xiàn)步驟
在數(shù)據(jù)科學(xué)領(lǐng)域,數(shù)據(jù)清洗和可視化是構(gòu)建數(shù)據(jù)驅(qū)動(dòng)解決方案的重要步驟,本文將詳細(xì)介紹如何使用Pandas進(jìn)行數(shù)據(jù)清洗,并結(jié)合Matplotlib進(jìn)行可視化,文章通過(guò)實(shí)際代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-08-08