Python基础知识

  • 官网:http://www.python.com

  • 我的版本:Python 3.5.4 

  • 基础知识
    数字和表达式
    >>> 1+2
    3
    
    >>> 4-1
    3
    
    >>> 3*3
    9
    
    >>> 9/3
    3.0
    
    >>> 1/2
    0.5
    
    #取余
    >>> 10%3
    1
    
    #幂运算
    
    >>> 3**2
    9
    
    >>> 2**3
    8
    
    >>> -2**2
    -4
    
    >>> (-2)**2
    4
    
    #二进制
    >>> 0b1010
    10
    
    #8进制
    >>> 0o10
    8
    
    #16进制
    >>> 0x10
    16
    
    #进制转换
    >>> oct(8)
    '0o10'
    
    >>> hex(16)
    '0x10'
    
    >>> bin(2)
    '0b10'
     
    #字符串
    >>> str = "这是一个字符串"
    >>> str
    '这是一个字符串'
    
    #单引号和双引号区别
    
    >>> 'Hello,world!'
    'Hello,world!'
    
    >>> 'let's go'
    SyntaxError: invalid syntax
     
    >>> "let's go"
    "let's go"
    或转义引号
    >>> 'let\'s go'
    "let's go"
    
    #字符串拼接
    >>> x = "Hello,"
    >>> x = "Hello,"
    >>> x + y
    'Hello,world'
    
    字符串表示 str 和 repr
    >>> temp = 3
    
    >>> print("密码是:"+ temp)
    Traceback (most recent call last):
      File "<pyshell#129>", line 1, in <module>
        print("密码是:"+ temp)
    TypeError: Can't convert 'int' object to str implicitly
    
    >>> print("密码是:"+ repr(temp))
    密码是:3
    
    #原始字符串 r
    >>> print("C:\Users\jm\Desktop")
    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
    
    >>> print(r"C:\Users\jm\Desktop")
    C:\Users\jm\Desktop
    
    >>> print(r"C:\Users\jm\Desktop\")
          
    SyntaxError: EOL while scanning string literal
    
    #小技巧
    >>> print(r"C:\Users\jm\Desktop""\\")
    C:\Users\jm\Desktop\
    
    #输入
    str2 = input("把你输入的内容赋值给str2:")
    把你输入的内容赋值给str2:hemin
    
    #输出
    print("你输入的是内容是:",str2)
    你输入的是内容是: hemin

     

    #模块和函数
    
    #内建函数pow
    >>>pow(2,3)
    8
    >>> abs(-3)
    3
    
    #导入模块 在 python 用 import 或者 from...import 来导入相应的模块。
    >>> import math
    >>> math.floor(18.9)
    18
    #
    >>> from math import sqrt
    >>> sqrt(9)
    3.0
    >>> 
    importfrom...import
    
    """
    (多行注释)
    将整个模块(somemodule)导入,格式为: import somemodule
    
    从某个模块中导入某个函数,格式为: from somemodule import somefunction
    
    从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc
    
    将某个模块中的全部函数导入,格式为: from somemodule import *
    """

      注:参考《Python基础教程》(第2版)·修订版

posted @ 2017-09-02 19:05  __Altair  阅读(287)  评论(0编辑  收藏  举报