1. 程式人生 > >Python基礎筆記_Day04_資料型別、math模組、random模組、string模組

Python基礎筆記_Day04_資料型別、math模組、random模組、string模組

Day04_資料型別、math模組、random模組、string模組

 

04.01_Python語言基礎(Python中的資料型別)(瞭解)

04.02_Python語言基礎(Num資料型別)(掌握)

04.03_Python語言基礎(int型別)(掌握)

04.04_Python語言基礎(float型別)(掌握)

04.05_Python語言基礎(bool型別)(掌握)

04.06_Python語言基礎(complex型別)(掌握)

04.07_Python語言基礎(math模組)(掌握)

04.08_Python語言基礎(random模組)(掌握)

04.09_Python語言基礎(string模組)(掌握)

04.10_day04總結

 

## 04.01_Python語言基礎(Python中的資料型別)(瞭解)
* Number
* Stirng
* List
* Tuple
* Sets
* Dictionary

## 04.02_Python語言基礎(Num資料型別)(掌握)
* Python3中Num支援的資料型別有以下幾類:
    * int
    * float
    * bool
    * complex

## 04.03_Python語言基礎(int型別)(掌握)
* int表示長整型,Python3.X只有這一種整數型別,包含正整數和負整數
#
    a = 123
    b = -345
    # 接下來的寫法和上面的寫法作用相同
    # a, b = 123, 345
    print(a + b)
    print(a - b)
    print(a * b)
    print(a / b)
*
    輸出結果
#
    -222
    468
    -42435
    -0.3565217391304348

## 04.04_Python語言基礎(float型別)(掌握)
* float表示小數,只有這一種小數型別,包含正小數和負小數
#
    i = 11.1
    j = -22.2
    print(i + j)
    print(i - j)
    print(i * j)
    print(i / j)

    輸出結果
#
    -11.1
    33.3
    -246.42
    -0.5

## 04.05_Python語言基礎(bool型別)(掌握)
* Python3中表示真或假的型別
    * Python3中bool型別只有兩個值:True和False
    * python2中沒有True和False,用0和1表示
    * True和False可以直接和數字運算
#
    h = True
    k = False
    print(h + k)
    print(h + 1)
    print(h - k)
    print(h * k)
    print(h / k)    # 報錯
 
輸出結果
#
    1
    2
    1
    0
    Traceback (most recent call last):
    File "Demos/Demo_Key.py", line 42, in <module>
    print(h / k)
    ZeroDivisionError: division by zero

## 04.06_Python語言基礎(complex型別)(掌握)
* complex在Python中表示覆數型別
    * 這裡的複數解釋為常量和變數的組合
    * 例如:k = 123+45j
        * 123表示常量
        * 45j表示變數
#
    g = 123+45j
    print(g)
    print(g.real)
    print(g.imag)

輸出結果:
#
    (123+45j)
    123.0
    45.0

## 04.07_Python語言基礎(math模組)(掌握)
###Math模組
* 絕對值    fabs
* 最大值    max
* 最小值 min
* 四捨五入 round
* 冪pow
* 天花板數字 ceil
* 地板數字    floor
* 取整數和小數部分    modf
* 開平方        sprt
#
    print(math.ceil(13.4))
    print(math.ceil(-13.4))
    print(math.floor(13.4))
    print(math.floor(-13.4))
    print(math.copysign(-18, 15))
    print(math.fabs(18.9))
    print(round(-18.9))
    print(math.modf(12.34))
    print(math.sqrt(7))
    print(random.randint(1, 10))
輸出結果:
#
    14
    -13
    13
    -14
    18.0
    18.9    
    -19
    (0.33999999999999986, 12.0)
    2.6457513110645907
    10
## 04.08_Python語言基礎(random模組)(掌握)
### Python隨機數
* 從序列裡面取值 choice
* 按指定的規則隨機取數字 randrange(range(1,100,2)
* 產生隨機數 random.random()範圍是0--1
* 隨機排序 shuffle(list)
* 隨機產生規定範圍內的小數uniform(3,9)
* 隨機產生規定範圍內的一個整數ranInt()
#
    import random
    print(dir(random))
    #返還一個隨機的0-1之間的隨機數
    print(random.random())
    #choice()返回隨機的一個元素
    print(random.choice('abcdefg'))

    #randrange()隨機返回一個元素  做微信隨機搶紅包
    print(random.randrange(1,10,2))
    """
    randrange(start,stop,step)
    start起始值
    stop結束值
    step步長
    """
    #shuffle()可以隨機打亂列表裡面的元素值
    list = [3,1,6,20,4,True,'abc']
    print(list)
    random.shuffle(list)
    print(list)
輸出結果:
#
    ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
    0.05391211483671399
    g
    5
    [3, 1, 6, 20, 4, True, 'abc']
    [1, 3, 20, 6, True, 4, 'abc']




## 04.09_Python語言基礎(string模組)(掌握)
* 字串簡介
* 字串定義方式
    * 書寫規則單引號或者雙引號
    * 字串不可變
* 字串輸出
    * print("hello")
    * print("%s" % "hello")
* 字串的輸入
    * input()獲取鍵盤錄入資料
    * 輸入的所有內容都是以字串儲存的
* 下標和切片
    * 下標:編號,超市櫃子的號碼,車票的位置,可以是負數:從最後一位往前數
        * 字串/列表和元組支援切片
        * name = "abcd",print(name[0])
        * 如果修改某個位置的資料,可以通過下標實現
    * 切片:擷取目標物件的一部分資料
        * 字串/列表和元組支援切片 
        * 格式:[開始:結束:步長]
#
    word = "abcdef"
    print(word[0:3])
    print(word[0:5])
    print(word[2:5])
    print(word[1:])
    print(word[1:-1])
    print(word[:3])
    print(word[::2])
    print(word[1:5:2])
    print(word[5:1:2])
    print(word[5:1:-2])
    print(word[::-2])
*
    輸出結果:
#
    3
    abc
    abcde
    cde
    bcdef
    bcde
    abc
    ace
    bd
    
    fd
    fdb
* 字串常見操作
    * 字串連線,使用+實現
    * 輸出重複字串print("--" * 10)
    * 切割字串  split()
    * find()
    * index()
    * count()
    * replace()
    * capitalze()把第一個字元大寫
    * title()
    * swapcase()大小寫轉換
    * startswith()
    * endswith()
    * lower()
    * upper()
    * isupper()
    * islower()
    * ljust()左對齊
    * rjust()右對齊
    * center()
    * zfill()右對齊補0
    * strip()去除兩端的空字元
    * rfind()從右側查詢
    * rindex()從右側查詢索引
    * partition()以傳入內容分割三部分
#
    # 下標index
    name = 'xiao huaWWW'
    print(name[len(name) - 1])

    # 把字串切片,後面的數字表示要切幾次,如果超出元素個數就以最大來執行
    print(name.split("a", 0))
    print(name[0:5:2])
    print(name[6:1:-2])
    print(name[::-1])

    # 重複輸出字串
    print("*" * 50)

    # 查詢字串元素第一次出現的位置,後面的引數表示從哪裡開始
    print(name.find("a", 3))

    # 替換字串
    print(name.replace("a", "w", 1))

    # 把首字母大寫
    print(name.capitalize())

    # 把所有單詞的首字母大寫
    print(name.title())
    
    # 大小寫轉換
    print(name.swapcase())
    
    # 判斷開頭或者結尾是否是以什麼開始/結束的
    print(name.startswith("xiao"))
    print(name.endswith("xiao"))

    num = ord("a")
    word = chr(num - 32)
    print(word)
    
    # 左側對齊,width表示這個元素的總寬度,如果字元長度不夠用空格補齊
    word = "abcdefg"
    print(word.ljust(20))
    print(word.ljust(20), "*")
    
    # 右側對齊,width表示這個元素的總寬度,如果字元長度不夠用空格補齊
    print(word.rjust(20))
    print(word.rjust(20), "*")
    
    # 居中顯示,width表示這個元素的總寬度,如果字元長度不夠用空格補齊
    print(word.center(20))
    print(word.center(20), "*")
    
    # zfill()右對齊,左補0
    print(word.zfill(20))
    
    # strip() 去除空格
    word = "   word  wordw  "
    print(word.strip())
    print(word.strip().strip("w"))
    
    # rfind從右側開始查詢
    # aaa.aaa.aaa.txt
    print(word.rfind("w"))
    # print(word.rfind("w", 14, 20))
    
    # index查詢下標
    # print(word.index("a"))
    
    # rindex
    print(word.rindex("w"))
    
    # partition()
    print(word.partition("w"))
    print(word.rpartition("w"))
輸出結果:
#
    W
    ['xiao huaWWW']
    xa 
    u a
    WWWauh oaix
    **************************************************
    7
    xiwo huaWWW
    Xiao huawww
    Xiao Huawww
    XIAO HUAwww
    True
    False
    A
    abcdefg             
    abcdefg              *
                 abcdefg
                 abcdefg *
          abcdefg       
          abcdefg        *
    0000000000000abcdefg
    word  wordw
    ord  word
    13
    13
    ('   ', 'w', 'ord  wordw  ')
    ('   word  word', 'w', '  ')
### 轉義字元:把普通的字串轉化成具有特殊含義的字串
* \n
* \t
* r""--消除轉義功能
#
    # 轉義字元
    print("hello01", end="\t")
    print("hello02", end="\n")
    print("hello03", end="\r")
    print("hello04", end="\r\n")
    print("hello05", end="\r")
    print("hello06", end="\r")
輸出結果:
#
    hello01    hello02
    hello04
    hello06


## 04.10_day01總結
* 把今天的知識點總結一遍。