程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[learn Python from simple to deep] two cycles

編輯:Python

One 、for loop

# 1.for loop 
# list 
L1=[10,20,'ABC','python']
print(L1[0])
for i in L1:
print(i)

10
10
20
ABC
python

#range function , Generate a sequence 
for i in range(10): #10 representative stop , Can't get 
print(i,end=' ')

0 1 2 3 4 5 6 7 8 9

for i in range(5,10): # 5 representative start,10 representative stop
print(i,end=" ")

5 6 7 8 9

# 5 representative start,10 representative stop,2 representative sep
for i in range(5,10,2):
print(i)

5
7
9

# Calculation 1+2+...+100
# Writing a 
sum=0
for i in range(1,101,1):
sum=sum+i
print("1 To 100 The sum of the :",sum)

1 To 100 The sum of the : 5050

# Calculation 1+2+...+100
# Write two :
sum=0
for i in range(101):
sum+=i
print("1 To 100 The sum of the :",sum)

1 To 100 The sum of the : 5050

Two 、while loop

#2.while loop 
# Calculation 1+2+...+100
i=1;
sum=0
while i<=100:
sum=sum+i
i=i+1
print("1 To 100 The sum of the :",sum)

1 To 100 The sum of the : 5050


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved