如何用 Python 打印图案

在 Python 中,for循环用于打印各种模式。打印各种图案是面试中最常见的编程问题。多重循环用于打印图案,其中第一个外部循环用于打印行数,内部循环用于打印列数。大多数模式使用以下概念。

  • 打印行数的外部循环。
  • 内部循环打印列数。
  • 根据 Python 中所需位置打印空白的变量。

在本教程中,我们将讨论一些常见的模式。

在本节中,我们将学习常见的金字塔模式。

示例-


# This is the example of print simple pyramid pattern
n = int(input("Enter the number of rows"))
# outer loop to handle number of rows
for i in range(0, n):
    # inner loop to handle number of columns
    # values is changing according to outer loop
        for j in range(0, i + 1):
            # printing stars
            print("* ", end="")     

        # ending line after each row
        print()

输出:

* 
* * 
* * * 
* * * * 
* * * * *

说明:

在上面的代码中,我们已经初始化了n个变量来输入模式的行数。我们输入 n = 5,循环的外部范围将是 0 到 4。

  • 内部 for循环的迭代取决于外部循环。内部循环负责打印列数。
  • 第一次迭代,I 的值是 0,增加了 1,所以变成了 0+1,现在第一次内循环迭代,打印一颗星(*)。
  • 在第二次迭代中,I 的值是 1,它增加了 1,所以它变成了 1+1,现在内部循环迭代了两次并打印了两个星号(*)。
  • end 参数防止跳转到另一行。它将打印星形,直到循环有效。
  • 最后一个打印语句负责结束每行后的行。

示例-


# This is the example of print simple reversed right angle pyramid pattern
rows = int(input("Enter the number of rows:"))
k = 2 * rows - 2  # It is used for number of spaces
for i in range(0, rows):
    for j in range(0, k):
        print(end=" ")
    k = k - 2   # decrement k value after each iteration
    for j in range(0, i + 1):
        print("* ", end="")  # printing star
    print("")

输出:

      * 
     * * 
    * * * 
   * * * * 
  * * * * *

代码-


rows = int(input("Enter the number of rows: "))

# the outer loop is executing in reversed order
for i in range(rows + 1, 0, -1):  
    for j in range(0, i - 1):
        print("*", end=' ')
    print(" ")

输出:

Enter the number of rows: 5
* * * * *  
* * * *  
* * *  
* *  
*  

代码-


n = int(input("Enter the number of rows: "))
m = (2 * n) - 2
for i in range(0, n):
    for j in range(0, m):
        print(end=" ")
    m = m - 1  # decrementing m after each loop
    for j in range(0, i + 1):
        # printing full Triangle pyramid using stars
        print("* ", end=' ')
    print(" ")

输出:

Enter the number of rows: 10
                  *   
                 *  *   
                *  *  *   
               *  *  *  *   
              *  *  *  *  *   
             *  *  *  *  *  *   
            *  *  *  *  *  *  *   
           *  *  *  *  *  *  *  *   
          *  *  *  *  *  *  *  *  *   
         *  *  *  *  *  *  *  *  *  *   

代码-


rows = int(input("Enter the number of rows: "))

# It is used to print space
k = 2 * rows - 2
# Outer loop in reverse order
for i in range(rows, -1, -1):
    # Inner loop will print the number of space.
    for j in range(k, 0, -1):
        print(end=" ")
    k = k + 1
    # This inner loop will print the number o stars
    for j in range(0, i + 1):
        print("*", end=" ")
    print("")

输出:

                  * * * * * * * * * * * 
                   * * * * * * * * * * 
                    * * * * * * * * * 
                     * * * * * * * * 
                      * * * * * * * 
                       * * * * * * 
                        * * * * * 
                         * * * * 
                          * * * 
                           * * 
                            *

代码-


rows = int(input("Enter the number of rows: "))

# It is used to print the space
k = 2 * rows - 2
# Outer loop to print number of rows
for i in range(0, rows):
    # Inner loop is used to print number of space
    for j in range(0, k):
        print(end=" ")
    # Decrement in k after each iteration
    k = k - 1
    # This inner loop is used to print stars
    for j in range(0, i + 1):
        print("* ", end="")
    print("")

# Downward triangle Pyramid
# It is used to print the space
k = rows - 2
# Output for downward triangle pyramid
for i in range(rows, -1, -1):
    # inner loop will print the spaces
    for j in range(k, 0, -1):
        print(end=" ")
    # Increment in k after each iteration
    k = k + 1
    # This inner loop will print number of stars
    for j in range(0, i + 1):
        print("* ", end="")
    print("")

输出:

Enter the number of rows: 8
              * 
             * * 
            * * * 
           * * * * 
          * * * * * 
         * * * * * * 
        * * * * * * * 
       * * * * * * * * 
      * * * * * * * * * 
       * * * * * * * * 
        * * * * * * * 
         * * * * * * 
          * * * * * 
           * * * * 
            * * * 
             * * 
              *

代码-


rows = input("Enter the number of rows: ")

# Outer loop will print the number of rows
for i in range(0, rows):
    # This inner loop will print the stars
    for j in range(0, i + 1):
        print("*", end=' ')
    # Change line after each iteration
    print(" ")
# For second pattern
for i in range(rows + 1, 0, -1):
    for j in range(0, i - 1):
        print("*", end=' ')
    print(" ")

输出:

Enter the number of rows: 7
*  
* *  
* * *  
* * * *  
* * * * *  
* * * * * *  
* * * * * * *  
* * * * * * *  
* * * * * *  
* * * * *  
* * * *  
* * *  
* *  
*

代码-


rows = int(input("Enter the number of rows: "))
k = rows - 2
# This is used to print the downward pyramid
for i in range(rows, -1 , -1):
    for j in range(k , 0 , -1):
        print(end=" ")
    k = k + 1
    for j in range(0, i+1):
        print("* " , end="")
    print()

# This is used to print the upward pyramid
k = 2 * rows  - 2
for i in range(0 , rows+1):
    for j in range(0 , k):
        print(end="")
    k = k - 1
    for j in range(0, i + 1):
        print("* ", end="")
    print()

输出:

Enter the number of rows: 5
   * * * * * * 
    * * * * * 
     * * * * 
      * * * 
       * * 
        * 
        * 
       * * 
      * * * 
     * * * * 
   * * * * * 
  * * * * * *

我们已经讨论了用于循环的基本金字塔模式。模式的概念取决于 for循环的逻辑和正确使用。

在这一节中,我们描述了几种不同类型的数字模式的程序。让我们逐一了解以下模式。

代码-


rows = int(input("Enter the number of rows: "))
# Outer loop will print number of rows
for i in range(rows+1):
    # Inner loop will print the value of i after each iteration
    for j in range(i):
        print(i, end=" ")  # print number
    # line after each row to display pattern correctly
    print(" ")

输出:

Enter the number of rows: 5
1  
2 2  
3 3 3  
4 4 4 4  
5 5 5 5 5  

解释-

在上面的代码中,我们已经根据行值打印了数字。第一行打印一个数字。接下来,两个数字打印在第二行,接下来的三个数字打印在第三行,以此类推。在……里

代码-


rows = int(input("Enter the number of rows: "))

# This will print the rows
for i in range(1, rows+1):
    # This will print number of column
    for j in range(1, i + 1):
        print(j, end=' ')
    print("")

输出:

Enter the number of rows: 5
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

在上面的代码中,我们在内部 for循环中打印了列值 j。

代码-


rows = int(input("Enter the number of rows: 5"))
k = 0
# Reversed loop for downward inverted pattern
for i in range(rows, 0, -1):
    # Increment in k after each iteration
    k += 1
    for j in range(1, i + 1):
        print(k, end=' ')
    print()

输出:

Enter the number of rows: 5
1 1 1 1 1 
2 2 2 2 
3 3 3 
4 4 
5

说明:

在上面的代码中,我们使用了反向循环来打印向下的倒金字塔,其中数字在每次迭代后都会减少。

代码-


rows = int(input("Enter the number of rows: "))
# rows value assign to n variable
n = rows
# Download reversed loop
for i in range(rows, 0, -1):
    for j in range(0, i):
        # this will print the same number
        print(n, end=' ')
    print()

输出:

Enter the number of rows: 6
6 6 6 6 6 6 
6 6 6 6 6 
6 6 6 6 
6 6 6 
6 6 
6

代码-


rows = int(input("Enter the number of rows: "))
# Downward loop for inverse loop
for i in range(rows, 0, -1):
    n = i   # assign value to n of i after each iteration
    for j in range(0, i):
        # print reduced value in each new line
        print(n, end=' ')
    print("\r")

输出:

Enter the number of rows: 6
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

代码-


current_Number = 1
stop = 2
rows = 3  # Number of rows to print numbers

for i in range(rows):
    for j in range(1, stop):
        print(current_Number, end=' ')
        current_Number += 1
    print("")
    stop += 2

输出:

1 
2 3 4 
5 6 7 8 9

代码-


rows = int(input("Enter the number of rows: "))
for i in range(0, rows + 1):
    # inner loop for decrement in i values
    for j in range(rows - i, 0, -1):
        print(j, end=' ')
    print()

输出:

Enter the number of rows: 6
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

代码-


rows = int(input("Enter the number of rows: "))
i = 1
# outer file loop to print number of rows
while i <= rows:
    j = 1
    # Check the column after each iteration
    while j <= i:
        # print odd values
        print((i * 2 - 1), end=" ")
        j = j + 1
    i = i + 1
    print()

输出:

Enter the number of rows: 5
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9

代码-


rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
    for j in range(1, rows + 1):
        # Check condition if value of j is smaller or equal than
        # the j then print i otherwise print j
        if j <= i:
            print(i, end=' ')
        else:
            print(j, end=' ')
    print()

输出:

Enter the number of rows: 5
1 2 3 4 5 
2 2 3 4 5 
3 3 3 4 5 
4 4 4 4 5 
5 5 5 5 5

rows = int(input("Enter the number of rows: "))
for i in range(1, rows):
    for j in range(1, i + 1):
        # It prints multiplication or row and column
        print(i * j, end='  ')
    print()

输出:

Enter the number of rows: 8
1  
2  4  
3  6  9  
4  8  12  16  
5  10  15  20  25  
6  12  18  24  30  36  
7  14  21  28  35  42  49  

在上面的部分,我们已经讨论了所有基本的数字模式。它将在 Python 上为循环生成一个强命令。我们可以使用任何类型的循环模式。

我们知道,每个字母表都有自己的 ASCII 值,所以定义一个字符并将其打印到屏幕上。让我们通过下面的例子来理解这些模式

代码-


print("The character pattern ")
asciiValue = 65     #ASCII value of A
for i in range(0, 5):
    for j in range(0, i + 1):
        # It will convert the ASCII value to the character
        alphabate = chr(asciiValue)
        print(alphabate, end=' ')
        asciiValue += 1
    print()

输出:

The character pattern 
A 
B C 
D E F 
G H I J 
K L M N O

解释-

在上面的代码中,我们将整数值 65 赋给了ASCII 值变量,它是一个 ASCII 值 a。我们为循环定义了打印五行。在内部循环体中,我们使用 char()函数将 ASCII 值转换为字符。它将打印字母,在每次迭代后增加 asciiValue。

代码-


print("The character pattern ")
asciiValue = int(input("Enter the ASCII value to print pattern: "))
# User - define value
if (asciiValue >= 65 or asciiValue <=122):
    for i in range(0, 5):
        for j in range(0, i + 1):
            # It will convert the ASCII value to the character
            alphabate = chr(asciiValue)
            print(alphabate, end=' ')
        print()
else:
    print("Enter the valid character value")

输出:

The character pattern 
Enter the ASCII value to print pattern: 75
K 
K K 
K K K 
K K K K 
K K K K K

代码-


str1 = "JavaTpoint"
x = ""
for i in str1:
    x += i
    print(x)

输出:

J
Ja
Jav
Java
JavaT
JavaTp
JavaTpo
JavaTpoi
JavaTpoin
JavaTpoint

我们可以用任何单词来打印字符。

代码-


print("Print equilateral triangle Pyramid with characters ")
s = 5
asciiValue = 65
m = (2 * s) - 2
for i in range(0, s):
    for j in range(0, m):
        print(end=" ")
    # Decreased the value of after each iteration
    m = m - 1
    for j in range(0, i + 1):
        alphabate = chr(asciiValue)
        print(alphabate, end=' ')
        # Increase the ASCII number after each iteration
        asciiValue += 1
    print()

输出:

Print equilateral triangle Pyramid with characters 
        A 
       B C 
      D E F 
     G H I J 
    K L M N O

在本文中,我们已经讨论了所有基本的模式程序。这些模式在面试中经常被问到,理解 Python for loop 的概念也很有帮助。一旦我们得到了程序的逻辑,我们就可以使用 Python 循环打印任何模式。


原创文章,作者:SY99E,如若转载,请注明出处:https://www.506064.com/n/126203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
SY99E的头像SY99E
上一篇 2024-10-03 23:07
下一篇 2024-10-03 23:07

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29

发表回复

登录后才能评论