The following small series for everyone to bring a basic example of Python grammar practice. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
1. Print 99 multiplication table
#只打印结果for i in range (1,10): for J in Range (1,i+1): Print (i*j,end= "") print () #打印算数表达式for I in Range (1,10): for J in Range (1,i+1): Print ("{0}*{1} = {2:2}". Format (j,i,i*j), end= "") print () 1*1 = 1 1*2 = 2 2*2 = 4 1*3 = 3 2*3 = 6 3*3 = 9 1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16 1*5 = 5 2*5 = 10 3*5 = 15 4*5 = 20 5*5 = 25 1*6 = 6 2*6 = 12 3*6 = 18 4*6 = 24 5*6 = 30 6*6 = 36 1*7 = 7 2*7 = 14 3*7 = 21 4*7 = 28 5*7 = 35 6*7 = 42 7*7 = 49 1*8 = 8 2*8 = 16 3*8 = 24 4*8 = 32 5*8 = 40 6*8 = 7*8 = 8*8 = 1*9 = 9 2*9 = 3*9 = 4*9 = 5*9 = 6*9 = Si 7*9 = 8*9 = 9*9 = Bayi #将打印矩阵转置一下for i in Range (1,10): Print ("" *10* (i-1), end = "") for J in Range (i,10): Print ("{0}*{1} = {2:<2}". Format (i,j,i*j), end= " Print () 1*1 = 1 1*2 = 2 1*3 = 3 1*4 = 4 1*5 = 5 1*6 = 6 1*7 = 7 1*8 = 8 1*9 = 9 2*2 = 4 2*3 = 6 4 = 8 2*5 = 10 2*6 = 12 2*7 = 14 2*8 = 16 2*9 = 18 3*3 = 9 3*4 = 12 3*5 = 15 3*6 = 18 3*7 = 21 3*8 = 24 3*9 = 27 4*4= 16 4*5 = 20 4*6 = 24 4*7 = 28 4*8 = 32 4*9 = 36 5*5 = 25 5*6 = 30 5*7 = 35 5*8 = 40 5*9 = 45 6*6 = 36 6*7 = 42 6*8 = 48 6*9 = 54 7*7 = 49 7*8 = 56 7*9 = 63 8*8 = 64 8*9 = 72 9*9 = 81
2. Print a diamond (the number of lines is odd)
For I in Range ( -3,4): prespace= (-i if i<0 else i) print ("" *prespace + ' * ' * (7-2*prespace)) * * * * ********** * * * * * * * * #变形之打印一道闪电while True: line_max = int (input ("Please input a odd number:")) if Line_max% 2: Breakrange_num = (Line_max + 1)//2 for I in Range (-range_num+1,range_num): if i = = 0: print (' * ' *line_max) C12/>elif i > 0: Print ("* (range_num-1) +" * "* (range_num-i)) else: print (' * (-i) + ' * ' * (range_num+i ) * * * * * * * * ********** * * #变形之打印掏空的菱形for i in Range ( -4,5): if i = = 4 or I = = 4: print (' * ' *9) Else: prespace= (-i+1 if i<0 else i+1) print ("*" *prespace + "* (9-2*prespace) +" * "*prespace) * * * * * * ********* ******* ***** *** *** ***** ******* *************
3. Print the Fibonacci sequence within 100
F0, f1 = 0, 1while F1 <=: print (f1, end = "") t = f1 f1 = t + f0 F0 = t
The wording that is not understood for the time being:
F0, f1 = 0, 1while F1 <=: print (F1) F0, f1 = f1, F0 + F1 #python对这样的赋值怎样处理
4. The 101th of the Fibonacci sequence
F0, f1 = 0, 1for i in Range (3,102): t = f1 f1 = t + f0 F0 = telse: print (F1)