Python中的简单图案打印程序

Pattern 1:

模式1:

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

Code:

码:

for row in range (0,5):
    for column in range (0, row+1):
        print ("*", end="")

    # ending row
    print('\r')

Pattern 2:

模式2:

Now if we want to print numbers or alphabets in this pattern then we need to replace the * with the desired number you want to replace. Like if we want pattern like,

现在,如果要在此模式下打印数字或字母,则需要将*替换为要替换的所需数字。 就像我们想要图案一样

1
1  1
1  1  1
1  1  1  1
1  1  1  1  1

Code:

码:

#row operation
for row in range(0,5):

# column operation

    for column in range(0,row+1):
        print("1 ",end="")

    # ending line
    print('\r')

Pattern 3:

模式3:

If want increasing numbers in this pattern like,

如果要以这种模式增加数字,

1
1  2  
1  2  3  
1  2  3  4
1  2  3  4  5

Here we need to declare a starting number from which the patter will start. In the above case the number is starting from 1. So, here we have to create a variable and assigns its value to 1 then we need to print only the value of variable.

在这里,我们需要声明一个起始编号,从该起始编号开始。 在上述情况下,数字从1开始。因此,这里我们必须创建一个变量并将其值分配为1,然后只需要打印变量的值即可。

As its value is increasing every row by 1, but starting value is always 1.

由于其值每行增加1,但起始值始终为1。

So, for that we have to declare the value of the starting number before column operation (second for loop) and need to increase it by 1 after the column operation section after the printing value.

因此,为此,我们必须在列运算之前声明起始编号的值(循环的第二个),并且需要在打印值后的列运算部分之后将起始编号增加1。

Code:

码:

#row operation
for row in range (0, 5):
    n = 1
    # column operation
    for column in range (0, row+1):
        print(n, end=" ")
        n = n+1
    # ending line
    print('\r')

Pattern 4:

模式4:

1
2 3
4 5 6
7 8 9 10
11 12 13 14

To get the above pattern only we have to declare the variable before the row operation. Follow the code below,

为了获得上述模式,我们只需要在行操作之前声明变量。 请遵循以下代码,

Code:

码:

n = 1
#row operation
for row in range (0, 5):

    # column operation
    for column in range (0, row+1):
        print(n, end=" ")
        n = n+1
    # ending line
    print('\r')

Pattern 5:

模式5:

A
A  B
A   B  C
A  B  C  D
A  B  C  D  E

The above pattern can also be another type.

上面的模式也可以是其他类型。

For that should have the knowledge of ASCII values of 'A'.

为此,应具有ASCII值 “ A”的知识。

Its ASCII value is 65.

ASCII值为 65。

In column operation We have to convert the ASCII value to character using chr() function.

在列操作中,我们必须使用chr()函数将ASCII值转换为字符。

Code:

码:

#row operation
for row in range (0, 5):
    n = 65
    # column operation
    for column in range (0, row+1):
        c = chr(n)
        print(c, end=" ")
        n = n+1
    # ending line
    print('\r')

Practice more python experiences here: python programs

在这里练习更多python经验: python程序

翻译自: https://www.includehelp.com/python/simple-pattern-printing-programs.aspx

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值