How Python prints a code sample of diamonds and triangles and rectangles to share

Source: Internet
Author: User
Recently encountered a problem in the development, the need to use Python to achieve the shape of diamond, triangle and rectangle, and found that there is less information on the net, so the summary sharing, this article mainly on the use of Python to print out the diamond, triangle and rectangle related data, Need friends can reference, below to see together.

Objective

This article mainly introduces the use of Python to print out the diamond, triangle and the relevant content of the rectangle, share it for everyone to refer to the study, words do not say, come together to see the detailed introduction:

Instance Code


#coding: utf-8rows = Int (raw_input (' Number of input columns: ')) i = j = k = 1 #声明变量, I is used to control the outer loop (number of graphic lines), J is used to control the number of spaces, K is used to control the number of * # isosceles right triangle 1print "isosceles right Angle Three Angle 1 "For I in range (0, rows): For K in range (0, rows-i): print" * ", #注意这里的", ", must not be omitted, can play a non-newline effect k + = 1 i + = 1 print" \ n "#打印实心等边三角形print" Print the hollow equilateral triangle, here remove if-else condition judgment is solid "for I in range (0, rows + 1): #变量i控制行数 for J in range (0, rows-i): # (1,r ows-i) Print "", j + = 1 for k in range (0, 2 * i-1): # (1,2*i) If k = = 0 or k = = 2 * I-2 or i = = rows:if i = = rows : if k% 2 = = 0: #因为第一个数是从0开始的, so if even print *, odd print blank "*", Else:print "", #注意这里的 ",", must not be omitted, can play the role of non-newline Els E:print "*", Else:print "", k + = 1 print "\ n" i + = 1 #打印菱形print "Print hollow and other diamonds, here to remove if-else condition judgment is solid" for I in range (row  s): #变量i控制行数 for J in Range (Rows-i): # (1,rows-i) print "", j + = 1 for k in range (2 * i-1): # (1,2*i) If k = = 0 or k = = 2 * i-2: Print "*", Else:print "", k + = 1 print "\ n" i + = 1 #菱形的下半部分for i in range (rows): For j in Range (i): # (1 , rows-i) print "", J += 1 for k in range (2 * (rows-i)-1): # (1,2*i) If k = = 0 or k = = 2 * (rows-i)-2:print "*", Else:print "", K + = 1 print "\ n" i + = # solid square print "Solid square" for I in range (0, rows): For K in range (0, rows): print "*", #注意这里的 ",", must not omit , can play a non-newline function k + = 1 i + = 1 print "\ n" #空心正方形print "Hollow Square" for I in range (0, rows): For K in range (0, rows): if I! = 0 and I! = rows-1: if k = = 0 or k = = rows-1: #由于视觉效果看起来更像正方形, so here * add a space on both sides, increase the distance print "*", #注意这里的 ",", must not be omitted, can play a non-newline function Else:print "", #该处有三个空格 else:print "*", #这里 * with space on both sides k + = 1 i + = 1 print "\ n"

Execution output:


Number of input columns: 4 isosceles right Triangle 1 * * * * * * * * * * * *  print hollow equilateral triangle, here to remove the if-else condition judgment is solid * * * * * * * * * *  Print Hollow diamond, this  In the if-else condition judgment is solid * * * * * * * * * * * * *   Solid Square *  * * * * * * * * * * * * * * * * *  Hollow Square * * * * * * * * * * * * * *  

Summarize

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.