Python Notes Chapter 3

Original link:

for loop statement

 for i in range(5): #[1,5) print(i)
 for i in range(-10,-100,-30): #步长-30 print(i) # >> -10 -40 -70(每个数单独一行)
 for i in range(0) print(i) #>> 无输出
for i in range(2,2) print(i) #>> 无输出

(Note : , there are four spaces before print)

for loop iterates over the list

spelling one

 a = ['vx','QQ','YEB'] for i in range(len(a)):# len 求列表长度(元素个数) print(i,a[i]) #>>0 vx #1 QQ #2 YEB
  • len can also be used to find the length of strings, the number of elements in tuples, sets, and dictionaries
 print(len("abc")) #>>3

spelling two

 a = ['vx','QQ','YEB'] for i in a: print(i) #>>vx QQ YEB(转行)
 for letter in 'omage': print (letter) #>>omage(转行)

break statement

 a = ['vx','QQ','YEB'] for i in a: if i =='QQ': print("over") break #跳出循环print(i) else: print("Not break") print("Done!") #>>vx #over #Done!

continue statement

 for letter in 'omage': if letter == 'a': continue #不执行此次循环,执行下一次循环print (letter) #>>omge


  • character encoding

    • ord(x) find the encoding of the character x
    • chr(x) find the character encoded as x

(using ASCII encoding scheme)

 for i in range(26): print(chr(ord('a') + i),end="") #连续输出26 个英文字符


 n = int(input("请输入将要输入数字的个数:")) total = 0 for i in range(n): #进行n 次total +=int(input("请输入要加的数字:")) print(total) #输入n 个整数求和
n = int(input("请输入一个整数:")) for i in range (1,n+1): if n % i == 0: print(i) # 输入一个正整数n,从小到大输出它的公因数
n = int(input("请输入一个整数:")) for i in range (n,0,-1): if n % i == 0: print(i) #从大到小

multiple loops

 for i in range n: # ... for j in range m: # ...
 #从n 里取两个数使其和等于m,且每个数只能取一次c = input("请输入nm:").split() n,m = int(c[0]),int(c[1]) for i in range(1,n):# n-1 种取法for j in range(i + 1,n + 1):#使i>j,避免重复if m % (i + j) == 0: print(i,j) break #后面的j 不再取,换下一个i
  • Break in multiple loops will only jump out of that loop, not multiple loops

while loop

 count = 0 while count<5: print(count,"小于5") count = count + 1 else: print(count,"大于或等于5")
 i=0 while i<26: print(chr(ord('a') + i),end="") i+=1
 s = input().split() x,y,z = int(s[0]),int(s[1]),int(s[2]) n = m =max(x,y,z)#从最大的数开始试while not(n%x==0 and n%y==0 and n%z==0): n +=m #隔m 个试一次print(n) #求三个数的最小公倍数

Comprehensive example

 #求斐波那契数列第n 项n =int(input()) c1=1 c2=1 for i in range (n-2): c3=c1+c2 c1=c2 c2=c3 print(c3)
 #求阶乘的和n = int(input()) s=0 for i in range (1,n+1): f=1 for j in range (1,i+1): f*=j #重复计算多s+=f print(s)
 # 角谷猜想n=int(input("请输入一个正整数:")) while n!=1: if n%2==1: print(str(n)+"*3+1="+str(n*3+1)) n=n*3+1 else : print(str(n)+"/2="+str(n//2)) n=n//2 print("End") # 法2: n=int(input("请输入一个正整数:")) b = n for i in range(n):#n 取多少合适temp=b if b==1: break if b%2==1: b1=b*3+1 b =b1 print("%d = %d * 3 + 1"%(b1,temp)) if b%2==0: b1=b/2 b=b1 print("%d = %d / 2"%(b1,temp)) #上式如何改正,已改;法2:
 #输入一个范围,找出2 的个数s=input().split() ran1,ran2=int(s[0]),int(s[1]) tol=0 for i in range(ran1,ran2+1): s=str(i) for x in s: if x =="2": tol+=1 print(tol) #法2: s=input().split() ran1,ran2=int(s[0]),int(s[1]) tol=0 for i in range(ran1,ran2+1): if i/10==2 and i%10==2: tol+=2 continue if i/10==2 or i%10==2: tol+=1 print("%d"%(tol))

Byte Planet Forest Stack2022-08-21

This article is reproduced from:
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment