python 基礎文法

來源:互聯網
上載者:User

標籤:eva   oat   計算   運算式   求值   hat   2.x   字串   返回   

1. print

3.x 中print作為函數

print("1", "2") 即輸出 1 2 

2.x print不為函數,所以

print "1","2" 即輸出1 2

 

2. input 與 raw_inputraw_input

raw_input() 將所有輸入作為字串看待,返回字串類型。//姑且當成gets()

比如:

gender = raw_input("what‘s you gender?")print(gender)輸入:male輸出:male<type ‘str‘>

 

input

input() 在3.x版本下:

input() 在輸入不為整形和浮點型時,相當於eval(raw_input(prompt))

其中 eval 將字串str當成有效運算式來求值並返回計算結果。比如:

字串轉換成列表>>>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‘>

如果輸入為整數或者浮點數,那麼直接返回該數字。//可以當成輸入、輸出模板的input用了。

如下:

c = input("please input number")print cprint type(c)
輸入:2.31
結果:2.31<type ‘float‘>

 

python 基礎文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.