日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python课程知识点总结(循环结构~列表)(0基础学习,后持续更新)

發布時間:2023/12/14 python 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python课程知识点总结(循环结构~列表)(0基础学习,后持续更新) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

五。循環結構

1.內置函數 range函數

  • range的三種創建方式

  • 只有一個參數(小括號中只給了一個數)

    r=range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ,默認從零開始,默認相差一稱為步長 print(r) #range(0,10) print(list(r))#可以用于查看range對象中的整數序列 --list列表2. 給了兩個參數(小括號中給了兩個數)
  • 給了兩個參數(小括號中給了兩個數)

    r=range(1,10) #指定起始值,從一開始,到十結束(不包含十),默認步長為一 print(r) #range(1,10)
  • 給了3個參數(小括號中給了3個數)

    r=range(1,10,2) print(list(r)) #[1, 3, 5, 7, 9] '''判斷指定的的整數 在序列中是否存在 in ,not in''' print(10 in r) #False print(9 in r) #True
  • 2.while 循環

    • 計算0到n之間的累加和
    n=int(input()) a=0 sum=0#用于存儲累加和 while(a<n):a=a+1sum=sum+a print(sum)
    • 計算0到n之間的偶數累加
    # eg 1 n=int(input()) a=0 sum=0#用于存儲累加和 while(a<n):a=a+2sum=sum+a print(sum) # eg 2 n=int(input()) a=0 sum=0#用于存儲累加和 while(a<n):a = a + 1if not bool(a%2):sum=sum+a print(sum)

    3.for-in循環

    pythonfor item in 'Python' : #第一次取出的是p,將p賦值給item,并將其輸出print(item)'''P y t h o n ''' for i in range(1,10,2):print(i)'''1 3 5 7 9 '''

    如果在循環體中不需要使用自定義變量,可將自定義變量寫為”_“

    for _ in range(5):print('人生苦短,我用python')'''人生苦短,我用python人生苦短,我用python人生苦短,我用python人生苦短,我用python人生苦短,我用python''' print('計算一到一百的偶數和') sum=0 for i in range(1,101):if i%2==0:sum=sum+iprint(sum) #結果2550

    輸出100到999之間的水仙花數

    for i in range(100,1000):b=i//100s=i//10%10g=i%10if i==b**3+s**3+g**3:print('水仙花數為'+str(i)+'') '''153 為水仙花數 370 為水仙花數 371 為水仙花數 407 為水仙花數'''

    4.流程控制語句break

    從鍵盤錄入密碼,最多錄入三次,如果正確,就退出循環(for in)

    for item in range(3):pwd=input('請輸入密碼')if pwd=='1234':printf('密碼正確')breakelse:print('密碼不正確')

    從鍵盤錄入密碼,最多錄入三次,如果正確,就退出循環(while)

    a=0 while a<3:pwd=input('請輸入密碼')if pwd=='1234':print('密碼正確')breakelse:print('密碼不正確')

    5.流程控制語句continue

    要求輸出1到n之間所有5的倍數

    n=int(input()) for item in range(1,n+1):if item%5==0:print(item)else:continue n=int(input()) for item in range(1,n+1):if item%5!=0:continueelse:print(item)

    6.else語句

    for item in range(3):pwd=input('請輸入密碼')if pwd=='8888':print('密碼正確')else:print('密碼錯誤') else:print('對不起,三次輸入密碼錯誤') a=0 while a<3 :pwd=input('請輸入密碼')if pwd=='8888':print('密碼正確')else:print('密碼錯誤')a=a+1 else:print('對不起,三次輸入密碼錯誤')

    7.嵌套循環

    for i in range(1,10):for j in range(1,i+1):print(''+str(i)+'*'+str(j)+'='+str(i*j)+'',end='\t') print() ''' 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 ''' for i in range(6):for j in range(0,10-i):print(end=" ")for j in range(10-i,10):print('*',end=' ') #end=‘ ’不換行輸出print('') #換行 '''* * * * * * * * * * * * * * * '''

    8.二重循環中的break和continue

    for i in range(5):for j in range(1,11):if j%2==0:breakprint(j,end='')print( ) '''11111 ''' for i in range(5):for j in range(1,11):if j%2==0:continueprint(j,end='')print( ) ''' 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 1 3 5 7 9 '''

    總結

    六,列表

    1.列表start

    a=10 #變量存儲一個對象的引用 lst=['hello','56','hcj']#存儲多個對象的引用 print(id(lst)) print(type(lst)) print(lst)

    2.列表的創建

    • 列表的第一種創建方式,使用 []
    lst=['hello','world','hcj'] print(lst)
    • 列表的第二種創建方式,使用使用內置函數list()
    lst2=list(['hello','world','hcj']) print(lst2)

    3.列表的特點

    • 索引映射唯一一個數據

      lst=['hello','world',23,'hcj'] print(lst[1],lst[-1]) # world hcj

    ? **注意 ** 從前往后正數,從后往前負數

    4.列表的查詢操作

    + 獲取列表中指定元素的索引

    lst=['hello','world','hcj''hello'] print(lst.index('hello'))#如果列表中有相同的元素只返回列表中相同元素的第一個元素的索引 # 0 print(lst.index('hcj')) # 2 print(lst.index('tn'))''' 報錯 raceback (most recent call last):File "D:\編碼\pythonProject\main.py", line 4, in <module>print(lst.index('tn'))alueError: 'tn' is not in list''' print(lst.index('hello',1,4)) # 3

    + 獲取列表中的單個元素

    lst=['hello','hcj','python','tn'] print(lst[3]) # tn print(lst[-1]) # tn

    + 獲取列表當中的多個元素

    lst=['hello','hcj','python','tn'] print(lst [1:3:1]) #步長為一 # ['hcj', 'python']lst=['hello','hcj','python','tn'] print(lst [1:4:2]) #步長為二 # ['hcj', 'tn']lst=['hello','hcj','python','tn'] print(lst [:4:3]) #默認從0開始 # ['hello', 'tn']lst=['hello','hcj','python','tn'] print(lst [1:4:]) #默認步長為1 # ['hcj', 'python', 'tn']lst=['hello','hcj','python','tn'] print(lst [1::2]) #默認到最后結束 # ['hcj', 'tn']print('--------step 步長為負數--------') print('原列表',lst) print(lst[3::-1]) '''原列表 ['hello', 'hcj', 'python', 'tn']['tn', 'python', 'hcj', 'hello'] '''

    +判斷指定元素在列表中是否存在及遍歷

    print('------in和not in--------') lst=['hello','hcj','python','tn',10,20] print(10 in lst) #True print(10 not in lst) #Falseprint('------for in--------') for item in lst:print(item,end=' ') # hello hcj python tn 10 20 ```!### 5.列表元素的增刪操作#### + 元素增加[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-IKKLkBf0-1639395454600)(C:\Users\hcj\AppData\Roaming\Typora\typora-user-images\image-20211129211612737.png)]```python lst=['hello','hcj','python','tn',10,20] print('添加之前',lst)#添加之前['hello', 'hcj', 'python', 'tn', 10, 20] lst.append(100) print('添加之后',lst)#添加之后['hello', 'hcj', 'python', 'tn', 10, 20, 100] lst2=[1,2] lst.extend(lst2) print('添加之后',lst)#添加之后 ['hello', 'hcj', 'python', 'tn', 10, 20, 1, 2] '''extend 可將列表化為元素加到后面 append將列表直接加入到后面eg lst.append(lst2)print('添加之后',lst)['hello', 'hcj', 'python', 'tn', 10, 20, [1, 2]] '''lst.insert(1,5) print('添加之后',lst) #['hello', 5, 'hcj', 'python', 'tn', 10, 20]lst2=[1,2] lst[1:]=lst2 print('添加之后',lst) #['hello', 1, 2] '''start決定從哪切stop決定切到哪'''

    + 列表元素的刪除操作

    lst=[10,20,30,5,6,8,30] lst.remove(30) print(lst) # [10, 20, 5, 6, 8,30]lst=[10,20,30,5,6,8] lst.pop(2) print(lst) # [10, 20, 5, 6, 8,30] lst=[10,20,30,5,6,8,30] lst.pop() # 默認刪除倒數第一個 print(lst) # [10, 20, 30, 5, 6, 8]lst=[10,20,30,5,6,8,30] lst2=[1,2,3,4] lst[2:]=lst2 print(lst) # [10, 20, 1, 2, 3, 4]lst.clear() print(lst) # []del lst print(lst) '''報錯 NameError: name 'lst' is not defined. '''

    +列表元素的修改操作

    lst=[1,2,3,4] #一次修改一個值 lst[2]=10 print(lst) # [1, 2, 10, 4]lst[1:3]=[30 ,40 ,50] print(lst) # [1, 30, 40, 50, 4]

    + 列表元素的排序操作

    lst=[1,10,20,30,5,6] print('輸出前的列表',lst,id(lst)) lst.sort() print('排序后的列表',lst,id(lst)) '''輸出前的列表 [1, 10, 20, 30, 5, 6] 2433745549632排序后的列表 [1, 5, 6, 10, 20, 30] 2433745549632 '''print('排序后的列表',lst,id(lst)) lst.sort(reverse= True) # reverse=True表示降序排序,reverse=False就是升序排序 print(lst) # [30, 20, 10, 6, 5, 1]print('-----使用內置函數sorted()對列表進行排序,將產生一個新的列表對象---------') lst=[10,2,30,4,50,6] print('原列表',lst) #開始排序 new_list=sorted(lst) print(lst) print(new_list) '''原列表 [10, 2, 30, 4, 50, 6][10, 2, 30, 4, 50, 6][2, 4, 6, 10, 30, 50] ''' # 指定關鍵字參數,實現列表元素的降序排序 new_list=sorted(lst,reverse=True) print(new_list) # [50, 30, 10, 6, 4, 2]

    6. 列表生成式

    ]

    lst=[ i for i in range(1,10)] print(lst) # [1, 2, 3, 4, 5, 6, 7, 8, 9]lst=[ i+i for i in range(1,10)] # i+i 表示列表的表達式 print(lst) # [2, 4, 6, 8, 10, 12, 14, 16, 18]

    總結


    參考視頻:
    https://www.bilibili.com/video/BV1wD4y1o7AS?spm_id_from=333.999.0.0

    總結

    以上是生活随笔為你收集整理的python课程知识点总结(循环结构~列表)(0基础学习,后持续更新)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。