zoukankan      html  css  js  c++  java
  • 《python基础教程》笔记之 其它语句1

    • 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 从入门到实践
    Python 斐波那契数列
    Python 纸牌游戏
    Python hangman小游戏
    BC #49 1001 Untitled
    BC#50 1003 The mook jong
    BC #50 1001 Distribution money
    vector
    stack
    queue
  • 原文地址:https://www.cnblogs.com/qiantangbanmu/p/4310259.html
Copyright © 2011-2022 走看看