第1章python基礎

來源:互聯網
上載者:User

標籤:python

一、變數

    1.變數定義規則

         1.1.變數名只能是 字母、數字或底線的任意組合

         1.2 變數名的第一個字元不能是數字

         1.3 以下關鍵字不能聲明為變數名[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]


    2.定義方式

        駝峰體

           NumberOfStudents = 80

        底線

           age_of_oldboy = 56 

           number_of_students = 80



二、基礎資料型別 (Elementary Data Type):

    1.字串:

        定義: ‘, ", ‘‘‘, """引起來的一堆字元

            ‘hi‘ ,"hello world"

        基本運算: +/*       

        常用方法:

        ‘count‘,            =>子字串出現的次數

        ‘index‘, ‘rindex‘,  => 子字串第一次出現的索引位置,如果不存在報錯

        ‘find‘, ‘rfind‘,    => 子字串第一次出現的索引位置,如果不存在返回-1

        

        ‘endswith‘,‘startswith‘, 以某個字串開始或者以某個字串結束時,返回True,否則返回False

        ‘format‘    字串格式化

        ‘join‘, 

        ‘split‘, ‘rsplit‘, 


        ‘lower‘, ‘upper‘

        ‘strip, ‘lstrip‘, ‘rstrip‘,

        ‘replace‘, 


        ‘splitlines‘, 


         ‘isalpha‘, ‘isdigit‘, ‘isalnum‘,, ‘islower‘, ‘isupper‘, 




    2.布爾運算


        a and b | a(True) | a(False)

        ---------------------------------

        b(True) |  True   |  False

        ---------------------------------

        b(False)|  False  |  False

    

        not a | a(True)| a(False)

        --------------------------

                  | False  | True



    3.列表:

        定義: []包含, 每個值之間用逗號分隔,每個值可以是任意類型

        編號: 0-N 索引

                -6   -5   -4   -3   -2  -1

                0   1   2   3   4   5   6

        ages = [25, 26, 28, 25, 34, 27, 29]


        訪問:

            通過索引去訪問list中的值

            ages[0]

            ages[6]


        修改:

            ages[idx] = value


        增加:

            pass

        

        刪除:

            del ages[idx]


        1. 使用的索引必須在list索引範圍內

        2. list_name[idx]

            list_num[idx] = value

            del list_num[idx]


        個數: len()

        判斷是否在list中 in


        => list(seq)

            range(start, end, step)

            start 開始 每次+step, 直到end(不包含end)

            start, start + step, start +2step = end


            start + n * step

            start =  1, end = 10, step = 2


            start = 1


            while True:

                print start

                start += step

                if start >= end:

                    break



            range(start, end, step)

            range(end), start = 0, step=1

            range(start, end), step = 1



        四則運算

            +   [1, 2] + [True, False] 

            *    ‘abc‘*5 


        切片

            從list中擷取其中一部分元素做為一個新的list

            nums = range(10)

            nums = [0, ..., 9]

            nums[start:end:step]

            [nums[start], nums[start + step], ..., nums[start + n * step < end]]


            nums[0:9:2]


            nums[start:end:step]

            nums[start:end] =>step=1

            nums[start:] => end=>len(N)

            nums[:]

            nums[:end]

            nums[:end:step]

            nums[::step]



            nums[:]


            nums = range(10)

            nums2 = nums

            nums3 = nums[:]

        

            地址, 值

            基本的資料類型 => 實值型別

            a = 1

            b = 1

            c = b


            a = []

            a = b


            b發生變化 del, update, 會影響a, 地址傳遞

            nums = range(10)

            nums[start:end] = []


            list的方法

                1. 更新list中的元素, 無傳回值

                2  不更新list中元素,但是有傳回值

                3. 更新list中元素,有傳回值


            a = 1

            b = 2

            

            temp = a

            a = b

            b = temp 


        1. while

        2. raw_input

        3. if

            if ... else ...

            if ... elif ...                 

            if ... elif ... else ...

        4. append

        5. len() == 0 

        6. pop(0)


    4.元組:

        用()包含起來, 用逗號分隔沒一個元素, 只有一個元素時,最後的逗號不能省略

        (1, )


        元組是不能修改的序列(指向不能變,值不能變)

        地址, 值


        mylist = []

        mytuple = (1, 2, 3, mylist, 4)

        tuple()



三、流程式控制制


    if 邏輯運算式0:

        子語句0

    elif 邏輯運算式1:

        子語句1

    elif 邏輯運算式2:

        子語句2

    ....

    else:

        子語句N


    if 邏輯運算式0:

        子語句0

    elif 邏輯運算式1:

        子語句1

    elif 邏輯運算式2:

        子語句2

    ....


    while 邏輯運算式:

        子語句

    else:

        子語句


    for var in seq:

        子語句


    range(N) => [0, ..., N-1]


    for name in [‘kk‘, ‘woniu‘]:

        print name


    for i in range(10):

        if i == 5:

            break/continue

        print i


第1章python基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.