• Python 有点意思


    基本语法

    >>> width = 20
    >>> height = 2 * 3
    >>> width * height
    120
    
    >>> x = y = z = 0
    >>> x
    0
    >>> y
    0
    >>> z
    0
    
    

    变量在使用前,必须定义

    >>> n
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'n' is not defined
    
    

    对浮点数支持很好

    >>> 7.0 / 2
    3.5
    >>> 7 / 2
    3
    
    
    >>> hello = "This is a string
     Hello Python."
    >>> print hello
    This is a string
     Hello Python.
    >>> hello
    'This is a string
     Hello Python.'
    
    

    print 可以输出内容。

    字符串可以由 + 操作符连接(粘到一起),可以由 * 重复。

    >>> word = 'Help' + 'A'
    >>> word
    'HelpA'
    >>> word * 5
    'HelpAHelpAHelpAHelpAHelpA'
    

    这个厉害了。

    >>> word[4]
    'A'
    >>> word[0:2]
    'He'
    >>> word[2:4]
    'lp'
    >>> word[:2]
    'He'
    >>> word[2:]
    'lpA'
    
    >>> word[1:100]
    'elpA'
    >>> word[10:0]
    ''
    >>> word[2:1]
    ''
    
    >>> word[-1]
    'A'
    >>> word[-2]
    'p'
    >>> word[-2:]
    'pA'
    >>> word[:-2]
    'Hel'
    
     +---+---+---+---+---+
     | H | e | l | p | A |
     +---+---+---+---+---+
     0   1   2   3   4   
    -5  -4  -3  -2  -1
    
    >>> s = "hello world"
    >>> len(s)
    11
    
    >>> u'Hellou0020World !'
    u'Hello World !'
    

    转码序列 u0020 表示在指定位置插入编码为 0x0020 的 Unicode 字符(空格)。

    >>> h = u'Hellou0020World !'
    >>> print h
    Hello World !
    >>> h
    u'Hello World !'
    

    列表

    >>> a = ['hello','world',100,20]
    >>> a
    ['hello', 'world', 100, 20]
    >>> len(a)
    4
    
    >>> a[0]
    'hello'
    >>> a[3]
    20
    
    >>> a[0:2] + ['python',2*2]
    ['hello', 'world', 'python', 4]
    

    编程

    >>> a,b = 0,1
    >>> a
    0
    >>> b
    1
    >>> while b < 10:
    ...     print b
    ...     a,b = b,a+b
    ... 
    1
    1
    2
    3
    5
    8
    
    
    >>> i = 2 * 2
    >>> print 'The value of i is',i
    The value of i is 4
    

    学习给我快乐,没有别的目的。

  • 相关阅读:
    黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战a基础篇
    单点登录业务逻辑解析
    C++ do{ } while(0)
    esp8266+MQTT+DHT11(温湿度计) platformio
    ESP8266+ MQTT+SG90(舵机) platformio
    esp8266+http (PlatformIO)
    ESP32+L298N+MQTT+4G无线远程监控+四驱动小破车
    ESP8266 + MQTT + 土壤湿度传感器
    esp8266 + mqtt + 温度计 (platformio)
    根据配置生成表格布局
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/9886115.html
Copyright © 2020-2023  润新知