国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python2 與 pyhton3的輸入語句寫法小結

2020-01-04 14:33:37
字體:
來源:轉載
供稿:網友

什么是輸入

咱們在銀行ATM機器前取錢時,肯定需要輸入密碼,對不?

那么怎樣才能讓程序知道咱們剛剛輸入的是什么呢??

大家應該知道了,如果要完成ATM機取錢這件事情,需要先從鍵盤中輸入一個數據,然后用一個變量來保存,是不是很好理解啊

1、python2的輸入語句

在python2中有兩種常見的輸入語句,input()raw_input() 。

(1)input()函數

可以接收不同類型的參數,而且返回的是輸入的類型。如,當你輸入int類型數值,那么返回就是int型;其中字符型需要用單引號或雙引號,否則,報錯。

a.數值型輸入

>>> a = input()>>> type(a)<type 'int'>>>> a>>> a = input()1.23>>> type(a)<type 'float'>>>> a1.23

b.字符類型

如果輸入的字符不加引號,就會報錯

>>> r = input()helloTraceback (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 : ',nameYour name : Tom

(2)raw_input()

函數raw_input()是把輸入的數據全部看做字符類型。輸入字符類型時,不需要加引號,否則,加的引號也會被看做字符。

>>> a = raw_input()>>> type(a)<type 'str'>>>> a'1'>>> a = raw_input()'hello'>>> type(a)<type 'str'>>>> a"'hello'"

如果想要int類型數值時,可以通過調用相關函數轉化。

>>> a = int(raw_input())>>> type(a)<type 'int'>>>> a>>> a = float(raw_input())1.23>>> type(a)<type 'float'>>>> a1.23

在同一行中輸入多個數值,可以有多種方式,這里給出調用map() 函數的轉換方法。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()的區別

通過查看input()幫助文檔,知道input函數也是通過調用raw_input函數實現的,區別在于,input函數額外調用內聯函數eval()。eval使用方法參考Python eval 函數妙用 (見下面)

>>> 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 函數妙用

eval

功能:將字符串str當成有效的表達式來求值并返回計算結果。

語法: eval(source[, globals[, locals]]) -> value

參數:

  source:一個Python表達式或函數compile()返回的代碼對象

  globals:可選。必須是dictionary

  locals:可選。任意map對象

實例展示:

可以把list,tuple,dict和string相互轉化。#################################################字符串轉換成列表>>>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'>#################################################字符串轉換成字典>>> a = "{1: 'a', 2: 'b'}">>> type(a)<type 'str'>>>> b = eval(a)>>> print b{1: 'a', 2: 'b'}>>> type(b)<type 'dict'>#################################################字符串轉換成元組>>> 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()函數,沒有raw_input();而且python3中的input()函數與python2中的raw_input()的使用方法一樣。

>>> a = input()10>>> type(a)<class 'str'>>>> a'10'

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香格里拉县| 开封县| 湘阴县| 乐东| 苏尼特左旗| 临江市| 多伦县| 德阳市| 泽库县| 兰西县| 大竹县| 堆龙德庆县| 仙居县| 梧州市| 茂名市| 安顺市| 大新县| 泸溪县| 磐安县| 武乡县| 中西区| 深水埗区| 武安市| 泸州市| 彭山县| 武义县| 平原县| 柞水县| 洪泽县| 渑池县| 和田市| 刚察县| 孟连| 湄潭县| 阜宁县| 剑河县| 江津市| 二连浩特市| 遂宁市| 武山县| 开化县|