《python基础教程》笔记之其它语句1

黄舟
Libérer: 2016-12-20 17:21:45
original
934 Les gens l'ont consulté

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 "", line 1, in
    x
NameError: name 'x' is not defined
>>> y
[1, 'p']

exec -- 执行一个字符串的语句

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

eval  -- 求一个表达式的值

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

 以上就是《python基础教程》笔记之其它语句1的内容,更多相关内容请关注PHP中文网(www.php.cn)! 


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!