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/
  • 相关阅读:
    MATLAB中的灰狼优化算法
    关于excel的一个问题的解决--(但是还是没弄清楚解决的原理)
    Windows上面遇到一个问题一直没有解决,求大佬指教
    Visio实用快捷键+比较不错的总结
    通过命令符查看笔记本电脑的电池损耗
    使用小工具QuickLook的使用
    科研工具--HistCite
    win10 效率
    office 2016文件已损坏,无法打开
    关于mathtype的使用方法的一些总结
  • 原文地址:https://www.cnblogs.com/emily_fly/p/2667398.html
Copyright © 2011-2022 走看看