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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航