zoukankan      html  css  js  c++  java
  • python基本用法

    1.可同时给几个变量赋值,如x=y=z=10

    2.字符串可用单引号或双引号包裹,同样可使用转义字符\,如'doesn\'t',

    "\"Yes,\" he said."

    3.字符串可以用+连接,用*重复,如 '<' + word*5 + '>'即为 '<HelpAHelpAHelpAHelpAHelpA>';

    4.可以用片段(slice)记号来指定子串,片段即用冒号隔开的两个下标,看到一个不错的片段记忆方法,

     | H | e | l | p | A |  

    0    1   2  3   4    5

    -5 -4   -3 -2 -1

      片段还有如下表达方式:

      >>> word[0:2] 

       'He'

      >>> word[2:4]

      'lp'  

     片段有很好的缺省值:第一下标省略时缺省为零,第二下标省略时缺省为字符串的长度。

      >>> word[:2] # 前两个字符

      'He'

      >>> word[2:] # 除前两个字符串外的部分

      'lpA'

    5.不合理的片段下标可以很好地得到解释:过大的下标被换成字符串长度,上界小于下界时 返回空串。 

    6.多行的长字符串也可以用行尾反斜杠续行,续行的行首空白不被忽略 

    hello = "This is a rather long string containing\n\         
          several lines of text just as you would do in C.\n\            
          Note that whitespace at the beginning of the line is\         
          significant.\n"         
    print hello 

    结果为

     

            This is a rather long string containing  

            several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant.

    7.列表

       如,a = ['spam', 'eggs', 100, 1234] 

       >>> a[1:-1]  

       ['eggs', 100]

       7.1与字符串不同的是列表是可变的,可以修改列表的每个元素:
        >>> a  

        ['spam', 'eggs', 100, 1234]

        >>> a[2] = a[2] + 23

        >>> a

        ['spam', 'eggs', 123, 1234]

        7.2也可以给一个片段重新赋值,这甚至可以改变表的大小:
    >>> # 替换若干项: 

    ... a[0:2] = [1, 12]

    >>> a

    [1, 12, 123, 1234]

    >>> # 去掉若干项:

    ... a[0:2] = []

    >>> a

    [123, 1234]

    >>> # 插入若干项:

    ... a[1:1] = ['bletch', 'xyzzy']

    >>> a

    [123, 'bletch', 'xyzzy', 1234]

    >>> a[:0] = a # 在开头插入自身

    >>> a

    [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234] >>>


    欢迎访问SuperMap技术问答社区http://qa.supermap.com/
  • 相关阅读:
    tomcat启动超时
    sqlserver存储过程及mybatis调用——待续
    linux各种顔色代表
    linux ngix安装
    vue 报错解决:TypeError: Cannot read property '_t' of undefined"
    给iview项目加一个i18n国际化翻译
    初探iview
    vue-eslint配置文件
    js中通过Object.prototype.toString方法----精确判断对象的类型
    判断是对象还是数组的方法
  • 原文地址:https://www.cnblogs.com/emily_fly/p/2667398.html
Copyright © 2011-2022 走看看