Example of how to print a diamond, triangle, and rectangle using python.

Source: Internet
Author: User

Example of how to print a diamond, triangle, and rectangle using python.

Preface

This article describes how to use python to print out Diamond, triangle, and rectangle. Let's share the content for your reference. If you don't talk about it, let's take a look at the details:

Instance code

# Coding: utf-8rows = int (raw_input ('number of input columns: ') I = j = k = 1 # declare variable, I used to control the outer loop (number of rows in the graph ), j is used to control the number of spaces, k is used to control the number of * # isosceles right triangle 1 print "isosceles right triangle 1" for I in range (0, rows ): for k in range (0, rows-I): print "*", # note that "," must not be omitted, can play the role of not wrapping k + = 1 I + = 1 print "\ n" # print solid equilateral triangle print "print hollow equilateral triangle, here we remove the if-else condition judgment as solid "for I in range (0, rows + 1): # variable I control the number of rows for j in range (0, rows-I ): # (1, rows-I) print "", j + = 1 for k in rang E (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: # because the first number starts from 0, print * for an even number and print "*" for an odd number, else: print "", # note that "," must not be omitted. else: print "*", else: print "", k + = 1 print "\ n" I + = 1 # print diamond print "print hollow diamond, here we remove the if-else condition judgment as solid "for I in range (rows): # variable I controls the number of rows 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 # lower half of the diamond 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 + = 1 # solid square print "solid square" for I in range (0, rows): for k in range (0, rows): print "*", # note that "," must not be omitted. Line feed function k + = 1 I + = 1 print "\ n" # hollow square 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: # because the visual effect looks more like a square, spaces are added on both sides, increase the distance from print "*", # note that "," must not be omitted. else: print "can be used to avoid line breaks.", # There are three spaces in else: print "*", # Here * Space k + = 1 I + = 1 print "\ n" is added on both sides"

Execution output result:

Number of input columns: 4 isosceles right triangle 1 ********* print hollow equilateral triangle, here, the if-else condition judgment is removed as a solid ********* to print hollow diamond, here, the if-else condition is removed and the result is a solid **************** hollow Square ************

Summary

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

Related Article

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.