天天看點

Python程式設計之路----day2

Python開發IDE(Integrated Development Environment):

PyCharm、Eclipse

1.Python運算符

運算結果是值

1.算術運算

    a = 10 * 10
    print(a)

    100

2.指派運算

    a = 11
    a = a + 1   或   a += 1
    print(a)

    12

運算結果是布爾值

1.比較運算

    a = 1 > 5
    print(a)

    False

2.邏輯運算

    a = 1 > 6 or 1 == 1
    print(a)

    True

3.成員運算

    a = "老土" in "瘋狂坦克"
    print(a)

    False
           

2.Python基本資料類型

數值型    int    所有功能都放在int裡
    a1 = 123
    a1 = 456
    print(a1)

    456

    - int
        字元串轉換為數字
        a = "123"
        print(type(a),a)

        str 123

        b = int(a)
        print(type(b),b)

        int  123

        num = "0011"
        v = int(num,base=16)
        print(v)

     - bit_length
        #目前數字的二進制,至少用n位表示
        r = age.bit_length()
           
字元串    str
    
    s1 = "welcome"
    s2 = "hello"

#首字母大寫
    name = "ocean"
    v = name.capitalize()
    print(v)

#所有字元轉換為小寫,casefold很厲害,很多位置的對相應轉換小寫
    v1 = name.casefold()
    print(v1)
    v2 = name.lower()
    print(v2)

#設定寬度,并将内容居中

    10代表總長度
    *填充空白位置

    v3 = name.center(10,"中")
    print(v3)

    ****中****

#搜尋字元串中的關鍵字,計算子序列的出現次數

    name = "ocaenoceaneanaenn"
    v4 = name.count('ea')
    print(v4)
    
    2

    name = "ocaenoceaneanaenn"
    v5 = name.count('ea',4,9)
    print(v5)
    
    1    

#字元串開始、字元串結尾

    name = "ocean"
    v6 = name.endswith('oc')
    v7 = name.startswith('oc')
    print(v6)
    print(v7)

    False
    True

#字元串由左向右,搜尋子序列之後,輸出位置

    name = "oceanocean"
    v8 = name.find('ce')
    print(v8)

#index找不到,報錯

    name = "ocaenocaen"
    v9 = name.index(6)
    print(v9)

#格式化,講一個字元串中的占位符替換成指定的值

    name = 'i am {bb}, age{aa}'
    print(name)
    v10 = name.format(bb='ocean',aa=18)
    print(v10)

#格式化,傳入的值 {"bb":'ocean',"aa":18}

    name =  'i am {bb}, age{aa}'
    v11 = name.format(name='dd',a=1-)
    v12 = name.format_map({"bb":'ocean',"aa":18})

#字元串中是否包含 字母或數字

    name = "123"
    v13 = name.isalnum()
    print(v13)