python學習手冊13:while及for迴圈
點選(此處)摺疊或開啟
-
#!/usr/bin/env python
- # –*– coding:utf8 –*–
- # Import python libs
- from __future__ import print_function
- for x in [“spam”, “eggs”, “ham”]:
-
print(x, end=`
`) - sum=0
- for x in [1,2,3,4]:
- sum +=x
- print(sum)
- prod=1
- for item in [1,2,3,4]:
- prod*=item
- print(prod)
- #for迴圈應用於字串,元組
- S=`asdfafafdag[4qqqfqf`
- T=(“a”,“b”,“c”)
- for x in S:
- print(x,end=`,`)
- for y in T:
- print(y,end=`_`)
- Y=((1,2),(3,4),(5,6))
- for (a,b) in Y:
- print (a,b)
- D={`a`:1,`b`:2,`c`:3}
- print(list(D.items()))
- L=list(D.items())
- print(L)
- for (key,value) in D.items():
- print(key, `=>`, value)
- ((a,b),c) = ((1,2),3)
- print(a,b,c)
- for ((a,b),c) in [((1,2),3),((4,5),6)]:
- print(a,b,c)
- all = (1,2,3,4)
- #a,*b,c=(1,2,3,4)
- a,b,c=all[0],all[1:3],all[3]
- print(a,b,c)
- items=[`aaa`,111,(4,5),2.01]
- tests=[(4,5),3.14]
- for key in tests:
- for item in items:
- if key == item:
- print(key,`was found.`)
- break
- else:
- print(key,`was not found.`)
- for key in tests:
- if key in items:
- print(key,`was found.`)
- else:
- print(key,`was not found.`)
- seq1=`adfklo2`
- seq2=`aqwerl2`
- res=[]
- for x in seq1:
- if x in seq2:
- res.append(x)
- print(res)
- print(range(5))
- print(range(2,100,2))
- print(range(10,–5,–1))
- print(range(–5,5,2))
- for i in range(–10,50,2):
- print(i,`:line`)
- X=`fghjkl;`
- for i in range(len(X)):
- print(X[i],end=`,`)
- for i in X:
- print(i,end=`.`)
- print(`================`)
- Y=`asdfghjklwertyuiodfghjkertyu`
- for i in range(0,len(Y),3):
- print(Y[i],`;`)
- print(Y[::4])
- Z=[1,2,3,4,5]
- for i in range(len(Z)):
- Z[i]+=1
- print(Z)
- A=[10,20,30,40,50]
- B=[1,2,3,4]
- print(zip(A,B))
- for (i,j) in zip(A,B):
- print(i,`+`,j,`=`,i+j)
- C=[`ADF`,`WER`,`HKL`]
- D=[1,2,3]
- E={}
- for k,v in zip(C,D):
- E[k]=v
- print(E)
- E=dict(zip(C,D))
- print(E)
- #產生偏移和元素:enumerate
- S=`asdfghjkl`
- for offset,item in enumerate(S):
- print(item,`appears at offset`,offset)
-
“`
- a appears at offset 0
- s appears at offset 1
- d appears at offset 2
- f appears at offset 3
- g appears at offset 4
- h appears at offset 5
- j appears at offset 6
- k appears at offset 7
- l appears at offset 8
- `“
- #依次取出
- E=enumerate(S)
- print(next(E))
- print(next(E))
- print(next(E))
- print(next(E))
- print(next(E))
- print(next(E))
- print(next(E))
- print(next(E))
- print(next(E))
-
“`
-
(0, `a`)
-
(1, `s`)
-
(2, `d`)
-
(3, `f`)
-
(4, `g`)
-
(5, `h`)
-
(6, `j`)
-
(7, `k`)
-
(8, `l`)
- `“
- #生成與偏移的相對應的個數的元素
- print([a * b for a,b in enumerate(S)])
-
“`
-
[``, `s`, `dd`, `fff`, `gggg`, `hhhhh`, `jjjjjj`, `kkkkkkk`, `llllllll`]
- `“
相關文章
- Python學習-while迴圈練習PythonWhile
- python while迴圈PythonWhile
- Java 迴圈 - for, while 及 do…whileJavaWhile
- python基礎 while迴圈練習PythonWhile
- python-while迴圈PythonWhile
- python 基礎習題6--for迴圈和while迴圈PythonWhile
- C語言程式設計學習中while迴圈和do……while迴圈C語言程式設計While
- while迴圈以及do while迴圈While
- Java迴圈結構-for,while及do…whileJavaWhile
- Java 迴圈結構 - for, while 及 do...whileJavaWhile
- Python學習小結—使用者輸入和While迴圈PythonWhile
- for 迴圈與 while 迴圈While
- while迴圈 case迴圈While
- 15-python之while迴圈PythonWhile
- C語言——迴圈結構(for迴圈,while迴圈,do-while迴圈)C語言While
- while迴圈While
- python04: while迴圈語句 break continue for in 迴圈PythonWhile
- Python學習之路6-使用者輸入和while迴圈PythonWhile
- Python基礎-While迴圈語句PythonWhile
- PHP For & While 迴圈PHPWhile
- C#練習,應用for,while,do-while迴圈C#While
- 04流程控制 for迴圈,while迴圈While
- Python中for迴圈和while迴圈有什麼區別?Python入門教程PythonWhile
- while迴圈補充While
- Python的if else 巢狀 和forin while 迴圈Python巢狀While
- 【廖雪峰python入門筆記】while迴圈Python筆記While
- Python趣味入門5:迴圈語句whilePythonWhile
- Java while和do while迴圈詳解JavaWhile
- Python基礎學習之迴圈Python
- python學習手冊(8)Python
- python學習手冊(10)Python
- python學習手冊(4)Python
- 七 while迴圈語句While
- sass的迴圈for,while,eachWhile
- JavaScript中的while迴圈JavaScriptWhile
- while + else 使用,while死迴圈與while的巢狀,for迴圈基本使用,range關鍵字,for的迴圈補充(break、continue、else) ,for迴圈的巢狀,基本資料型別及內建方法While巢狀資料型別
- 分別使用while迴圈、do…while迴圈和for迴圈輸出1~100之間的所有偶數While
- c#入門-while迴圈C#While
- C#程式設計基礎第七課:C#中的基本迴圈語句:while迴圈、do-while迴圈、for迴圈、foreach迴圈的使用C#程式設計While