杰瑞科技汇

Python中print如何输出数组形状?

Of course! Printing shapes in Python is a classic exercise for practicing loops and string manipulation. The key is to understand that you're essentially printing a series of lines, where each line is a string of characters.

Python中print如何输出数组形状?-图1
(图片来源网络,侵删)

Let's go through the most common shapes, from simple to more complex.

Rectangle (or Square)

This is the most fundamental shape. You need to control both the number of rows and the number of columns.

Logic:

  • Use an outer loop to control the number of rows.
  • Use an inner loop to control the number of characters in each row.
# --- Solid Rectangle ---
rows = 5
cols = 10
for i in range(rows):
    for j in range(cols):
        print('*', end='')  # Use end='' to print on the same line
    print() # Print a newline to move to the next row
# --- Output ---
# **********
# **********
# **********
# **********
# **********

Right-Angled Triangle

This shape is a great next step. The number of stars in each row corresponds to the row number.

Python中print如何输出数组形状?-图2
(图片来源网络,侵删)

Logic:

  • Loop from 1 to the number of rows.
  • In each row i, print i number of stars.
# --- Right-Angled Triangle ---
rows = 5
for i in range(1, rows + 1):
    for j in range(i):
        print('*', end='')
    print()
# --- Output ---
# *
# **
# ***
# ****
# *****

Pyramid (or Equilateral Triangle)

This is a bit trickier. For each row, you need to print two things: spaces and stars. The number of spaces decreases while the number of stars increases.

Logic:

  • Loop from 1 to the number of rows.
  • For each row i:
    1. Print rows - i spaces.
    2. Print 2*i - 1 stars. (This formula gives you 1, 3, 5, 7... stars per row).
    3. Print a newline.
# --- Pyramid ---
rows = 5
for i in range(1, rows + 1):
    # Print leading spaces
    for j in range(rows - i):
        print(' ', end='')
    # Print stars
    for k in range(2 * i - 1):
        print('*', end='')
    print() # Newline
# --- Output:
#     *
#    ***
#   *****
#  *******
# *********

Inverted Pyramid

This is the reverse of the pyramid. The number of spaces increases, and the number of stars decreases.

Logic:

  • Loop from the number of rows down to 1.
  • For each row i:
    1. Print rows - i spaces.
    2. Print 2*i - 1 stars.
    3. Print a newline.
# --- Inverted Pyramid ---
rows = 5
for i in range(rows, 0, -1):
    # Print leading spaces
    for j in range(rows - i):
        print(' ', end='')
    # Print stars
    for k in range(2 * i - 1):
        print('*', end='')
    print() # Newline
# --- Output:
# *********
#  *******
#   *****
#    ***
#     *

Diamond

A diamond is simply a pyramid on top of an inverted pyramid.

Logic:

  • Print the top half (a pyramid with rows).
  • Print the bottom half (an inverted pyramid with rows - 1).
# --- Diamond ---
rows = 5
# Top half (Pyramid)
for i in range(1, rows + 1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(2 * i - 1):
        print('*', end='')
    print()
# Bottom half (Inverted Pyramid)
for i in range(rows - 1, 0, -1):
    for j in range(rows - i):
        print(' ', end='')
    for k in range(2 * i - 1):
        print('*', end='')
    print()
# --- Output:
#     *
#    ***
#   *****
#  *******
# *********
#  *******
#   *****
#    ***
#     *

Hollow Shapes

For hollow shapes, you only print the border. The key is to use an if condition inside your loops.

Hollow Rectangle

Logic:

  • Print a full line of stars for the first and last rows.
  • For middle rows, print a star, then spaces, then a star.
# --- Hollow Rectangle ---
rows = 5
cols = 10
for i in range(rows):
    if i == 0 or i == rows - 1: # First or last row
        for j in range(cols):
            print('*', end='')
    else: # Middle rows
        print('*', end='')       # Left border
        for j in range(cols - 2): # Inner spaces
            print(' ', end='')
        print('*')               # Right border
    print() # Newline
# --- Output ---
# **********
# *        *
# *        *
# *        *
# **********

Hollow Pyramid

Logic:

  • The first and last rows are special cases (all stars for the first, one star for the last).
  • For middle rows, print leading spaces, then a star, then inner spaces, then a star, then trailing spaces.
# --- Hollow Pyramid ---
rows = 5
for i in range(1, rows + 1):
    # Print leading spaces
    for j in range(rows - i):
        print(' ', end='')
    # Print stars and spaces
    if i == 1 or i == rows: # First or last row
        for k in range(2 * i - 1):
            print('*', end='')
    else: # Middle rows
        print('*', end='')       # Left star
        for k in range(2 * i - 3): # Inner spaces
            print(' ', end='')
        print('*')               # Right star
    print() # Newline
# --- Output:
#     *
#    * *
#   *   *
#  *     *
# *********

Summary of Key Concepts

  1. Nested Loops: You almost always need an outer loop for rows and an inner loop for columns/characters.
  2. print(end=''): This is crucial. It prevents print() from moving to a new line, allowing you to build a line character by character.
  3. print() (with no arguments): This prints a newline, moving the cursor to the start of the next line.
  4. Loop Ranges: Be careful with range(). range(5) gives you 0, 1, 2, 3, 4. range(1, 6) gives you 1, 2, 3, 4, 5.
  5. Mathematical Formulas: For complex patterns like pyramids, simple math (2*i - 1) is your best friend for calculating the number of items per row.
分享:
扫描分享到社交APP
上一篇
下一篇