Python中的简单图案打印程序
Pattern 1:
模式1:
* * * * * * * * * * * * * * *Code:
碼:
for row in range (0,5):for column in range (0, row+1):print ("*", end="")# ending rowprint('\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 1Code:
碼:
#row operation for row in range(0,5):# column operationfor column in range(0,row+1):print("1 ",end="")# ending lineprint('\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 5Here 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 operationfor column in range (0, row+1):print(n, end=" ")n = n+1# ending lineprint('\r')Pattern 4:
模式4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14To 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 operationfor column in range (0, row+1):print(n, end=" ")n = n+1# ending lineprint('\r')Pattern 5:
模式5:
A A B A B C A B C D A B C D EThe 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 operationfor column in range (0, row+1):c = chr(n)print(c, end=" ")n = n+1# ending lineprint('\r')Practice more python experiences here: python programs
在這里練習更多python經驗: python程序
翻譯自: https://www.includehelp.com/python/simple-pattern-printing-programs.aspx
總結
以上是生活随笔為你收集整理的Python中的简单图案打印程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Dictionary elem
- 下一篇: 数据分析 数据清理_数据清理| 数据科学