Python print right triangle, equi triangle, diamond, square code, python equi-edge

Source: Internet
Author: User

Python print right triangle, equi triangle, diamond, square code, python equi-edge

Triangle

Isosceles right triangle 1 2.7

# 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"

Python: print right triangle

Coding = UTF-8

Method 1

i = 1while i <= 5:  j = 1  while j <=i:    print '*',    j+=1  print '\n'  i+=1

Method 2

for i in range(1, 6):  for j in range(1, i+1):    print "*",  print '\n'

# Print a solid equi-edge triangle

Print "print hollow equi triangle. Here, the if-else condition is determined to be 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 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: # because the first number starts from 0, if an even number is printed *, the odd number prints the space print "*", 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, the if-else condition is determined to be solid." for I in range (rows): # variable I control 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 the following ",", it must not be omitted. It can be used for non-line breaks. 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"

Knowledge POint Description:

Python, end = ''remarks

That is, do not wrap the line after printing. Use "," In Python2.7
The following is an example of 2.7:
Def test ():
Print 'hello ',
Print 'World'

In python3, 'end = 'means no line break.

Python print flip triangle

1. Write Functions in the above mode.

Prompt the user to enter n and call displayPattern (n) to display this mode.

Def displayPattern (n): dict ={} for I in range (0, int (n): ''' int (I) + 2: Left closed and right open eg: n = 3, I = 0, print [1] I = 1, print [1, 2]... str (j) [:-1]: Reverse type of a single string. For example: 12 ==> 21, add to the dictionary ''' dict [I] = [str (j) [:-1] for j in range (1, int (I) + 2)] listNum = [] # print (dict) for key, value in dict. items (): ''' {0: ['1'], 1: ['1', '2'], 2: ['1', '2 ', '3'], 3: ['1', '2', '3', '4'], cyclically retrieve the value in the dictionary (corresponding list) to remove [], (replace comma with space and single quotation marks with null) ==> add to list ''' value = str (value ). strip ('[]'). replace (',', '\ t '). replace ("'", '') listNum. append (value) # print (value) for k in range (0, int (n): ''' "\ t" * % s: space * string form % (int (n)-int (k)-1) [:-1]: %: Format mark int (n) -int (k)-1: how many spaces are required? ''' listNum [k] = (listNum [k] + (eval ('"\ t" * % s' % (int (n)-int (k) -1) [:-1] print (listNum [k]) # print (dict) # print (listNum) num = int (input ("enter a number:") # Call the displayPattern (num) function)

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.