《python基礎教程》筆記之其它語句1

來源:互聯網
上載者:User
PRint 相關

print可以列印多個運算式,只要將它們用逗號隔開就好,結果中每個參數之間都會插入一個空格,使用+可以避免空格,如

>>> print 'age:',42
age: 42
>>> print 'hello'+','+'world'
hello,world

在print語句結尾處加上逗號,接下來的語句會與前一條語句在同一行列印,如

print 'hello',print 'world'

結果

>>>
hello world

import相關

from sometmodule import somefunc,anotherfunc,yetanotherfunc

from somemodule import * -- 匯入模組中所有的函數

import somemoudle as othermodulename -- 給模組取個別名

form somemodule import somefunc ad otherfuncname -- 給匯入的函數取個別名

賦值相關

序列解包或者可選代解包 -- 將多個值的序列解開,然後放到變數的序列中,如

>>> scoundrel = {'name':'Robin','firlfriend':'marion'}
>>> key,value = scoundrel.popitem()
>>> key
'firlfriend'
>>> value
'marion'

鏈式賦值 -- 將同一個值賦值給多個變數的捷徑,如

x=y=somefunction()

增量賦值 -- 將運算式運算子放置在賦值運算子=的左邊,如

x += 1

語句塊

冒號(:)用來標識語句塊的開始,塊中的每一個語句都是縮排的(縮排量相同)。當回退到和已經閉合的塊一樣的縮排量時,就表示當前塊已經結束了。

三人行

pass -- 程式什麼事情都不用做。

del -- 刪除對象,但不會影響值,如

>>> x = y = [1,2]
>>> y[1] = 'p'
>>> y
[1, 'p']
>>> x
[1, 'p']

>>> del x
>>> x

Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
x
NameError: name 'x' is not defined
>>> y
[1, 'p']

exec -- 執行一個字串的語句

>>> exec("print 'hello, world!'")
hello, world!

eval -- 求一個運算式的值

>>> eval("4 + 56")
60

以上就是《python基礎教程》筆記之其它語句1的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.