Python basic syntax example and python basic syntax example

Source: Internet
Author: User

Python basic syntax example and python basic syntax example

1. Print the 9-9 multiplication table

# Print only the result for I in range (): for j in range (1, I + 1): print (I * j, end = "") print () # print arithmetic expression for I in range (): for j in range (1, I + 1): print ("{0} * {1 }={ 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 = 48 7*8 = 56 8*8 = 64 1*9 = 9 2*9 = 18 3*9 = 27 4*9 = 36 5*9 = 45 6*9 = 54 7*9 = 63 8*9 = 72 9*9 = 81 # transpose the print matrix to I in range): 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 2*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 rows is odd)

For I in range (-3, 4): prespace = (-I if I <0 else I) print ("* prespace + '* (7-2 * prespace )) * ************************ # print a piece of deformation Lightning 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) elif I> 0: print (''' * (range_num-1) + "*" * (range_num-i) else: print (''' * (-I) + '* (range_num + I )) * ***************** # deformation to print the hollow diamond 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 series of less than 100

f0, f1 = 0, 1while f1 <= 100:  print(f1, end = " ")  t = f1  f1 = t + f0  f0 = t

Not yet understood:

F0, f1 = 0, 1 while f1 <= 100: print (f1) f0, f1 = f1, f0 + f1 # How does python assign values like this?

4. Calculate the 101st items of the Fibonacci series

f0, f1 = 0, 1for i in range(3,102):  t = f1  f1 = t + f0  f0 = telse:  print(f1)

The above example of the basic python syntax practice is all the content shared by the editor. I hope you can give me a reference and support the help house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.