飞道的博客

从零开始学习python | 实例讲解如何制作Python模式程序

444人阅读  评论(0)

本文分享自华为云社区《从零开始学习python | 通过示例学习如何制作Python模式程序》,原文作者:Yuchuan 。

Python编程语言很容易学习。易于语法实现的各种库使其脱颖而出,这是它成为本世纪最流行的编程语言的众多原因之一。虽然学习很容易,但访问员通常会在建立模式程序逻辑时寻求您的方法。尽管听起来很棘手,但使用python却是小菜一碟。在本文中,我们将学习python中的各种模式程序。此博客涵盖以下主题:

  • Star Pattern Programs
    • Pyramid Pattern Program
    • Half-Pyramid Pattern Program
    • Diamond Shaped Pattern Program
    • Start Pattern Program
    • Hourglass Pattern Program
  • Number Pattern Programs
    • Simple Numbers In A Pyramid
    • Pascal’s Triangle Pattern
    • Diamond Pattern Program
  • Characters Patter Programs

模式程序包含许多嵌套循环。因此,如果您不熟悉python中的循环,请确保查看有关python中的循环的详细教程。

星型程序

以下是python中的一些星型程序。

金字塔图案程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. for i in range( 0,n):
  4. for j in range( 0,k):
  5. print(end= " ")
  6. k = k - 1
  7. for j in range( 0, i+ 1):
  8. print( "*", end= " ")
  9. print( "\r")
  10. pattern( 5)

输出:

反金字塔图案程序


  
  1. def pattern(n):
  2. k = 2*n - 2
  3. for i in range(n,- 1,- 1):
  4. for j in range(k, 0,- 1):
  5. print(end= " ")
  6. k = k + 1
  7. for j in range( 0, i+ 1):
  8. print( "*", end= " ")
  9. print( "\r")
  10. pattern( 5)

输出:

正确启动模式程序


  
  1. def pattern(n):
  2. for i in range( 0, n):
  3. for j in range( 0, i + 1):
  4. print( "* ", end= "")
  5. print( "\r")
  6. for i in range(n, 0 , - 1):
  7. for j in range( 0, i + 1):
  8. print( "* ", end= "")
  9. print( "\r")
  10. pattern( 5)

输出:

左启动模式程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. for i in range( 0, n- 1):
  4. for j in range( 0, k):
  5. print(end= " ")
  6. k = k - 2
  7. for j in range( 0, i + 1):
  8. print( "* ", end= "")
  9. print( "\r")
  10. k = - 1
  11. for i in range(n- 1,- 1,- 1):
  12. for j in range(k,- 1,- 1):
  13. print(end= " ")
  14. k = k + 2
  15. for j in range( 0, i + 1):
  16. print( "* ", end= "")
  17. print( "\r")
  18. pattern( 5)

输出:

沙漏图案程序


  
  1. def pattern(n):
  2. k = n - 2
  3. for i in range(n, - 1 , - 1):
  4. for j in range(k , 0 , - 1):
  5. print(end= " ")
  6. k = k + 1
  7. for j in range( 0, i+ 1):
  8. print( "* " , end= "")
  9. print( "\r")
  10. k = 2 * n - 2
  11. for i in range( 0 , n+ 1):
  12. for j in range( 0 , k):
  13. print(end= "")
  14. k = k - 1
  15. for j in range( 0, i + 1):
  16. print( "* ", end= "")
  17. print( "\r")
  18. pattern( 5)

输出:

半金字塔图案程序


  
  1. def pattern(n):
  2. for i in range( 0,n):
  3. for j in range( 0, i+ 1):
  4. print( "* " , end= "")
  5. print( "\r")
  6. pattern( 5)

输出:

左半金字塔图案程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. for i in range( 0, n):
  4. for j in range( 0, k):
  5. print(end= " ")
  6. k = k - 2
  7. for j in range( 0, i + 1):
  8. print( "* ", end= "")
  9. print( "\r")
  10. pattern( 5)

输出:

向下半金字塔图案程序


  
  1. def pattern(n):
  2. for i in range(n, - 1, - 1):
  3. for j in range( 0, i + 1):
  4. print( "* ", end= "")
  5. print( "\r")
  6. pattern( 5)

输出:

菱形图案程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. for i in range( 0, n):
  4. for j in range( 0 , k):
  5. print(end= " ")
  6. k = k - 1
  7. for j in range( 0 , i + 1 ):
  8. print( "* ", end= "")
  9. print( "\r")
  10. k = n - 2
  11. for i in range(n , - 1, - 1):
  12. for j in range(k , 0 , - 1):
  13. print(end= " ")
  14. k = k + 1
  15. for j in range( 0 , i + 1):
  16. print( "* ", end= "")
  17. print( "\r")
  18. pattern( 5)

输出:

钻石星型计划


  
  1. for i in range( 5):
  2. for j in range( 5):
  3. if i + j == 2 or i - j == 2 or i + j == 6 or j - i == 2:
  4. print( "*", end= "")
  5. else:
  6. print(end= " ")
  7. print()

输出:

数字模式程序

以下是一些带有数字模式的Java程序。

简单数字程序


  
  1. def pattern(n):
  2. x = 0
  3. for i in range( 0 , n):
  4. x += 1
  5. for j in range( 0, i + 1):
  6. print(x , end= " ")
  7. print( "\r")
  8. pattern( 5)

输出:

帕斯卡三角计划


  
  1. def pascal(n):
  2. for i in range( 0, n):
  3. for j in range( 0, i + 1):
  4. print( function(i, j), " ", end= "")
  5. print()
  6. def function(n, k):
  7. res = 1
  8. if (k > n - k):
  9. k = n - k
  10. for i in range( 0, k):
  11. res = res * (n - i)
  12. res = res // (i + 1)
  13. return res
  14. pascal( 7)

输出:

半金字塔图案与数字


  
  1. def pattern(n):
  2. for i in range( 1, n):
  3. for j in range( 1, i + 1):
  4. print(j, end= " ")
  5. print( "\r")
  6. pattern( 5)

输出:

菱形图案与数字


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. x = 0
  4. for i in range( 0, n):
  5. x += 1
  6. for j in range( 0, k):
  7. print(end= " ")
  8. k = k - 1
  9. for j in range( 0, i + 1):
  10. print(x, end= " ")
  11. print( "\r")
  12. k = n - 2
  13. x = n + 2
  14. for i in range(n, - 1, - 1):
  15. x -= 1
  16. for j in range(k, 0, - 1):
  17. print(end= " ")
  18. k = k + 1
  19. for j in range( 0, i + 1):
  20. print(x, end= " ")
  21. print( "\r")
  22. pattern( 5)

输出:

降序模式程序


  
  1. def pattern(n):
  2. for i in range(n, 0, - 1):
  3. for j in range( 1, i + 1):
  4. print(j, end= " ")
  5. print( "\r")
  6. pattern( 5)

输出:

二进制数字模式程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. for i in range( 0, n):
  4. for j in range( 0, k):
  5. print(end= " ")
  6. k = k - 1
  7. for j in range( 0, i + 1):
  8. print(' 10', end= "")
  9. print( "\r")
  10. pattern( 5)

输出:

字符模式程序

这是一些带有字符的python模式程序。

右字母三角


  
  1. def pattern(n):
  2. x = 65
  3. for i in range( 0, n):
  4. ch = chr(x)
  5. x += 1
  6. for j in range( 0, i + 1):
  7. print(ch, end= " ")
  8. print( "\r")
  9. pattern( 5)

输出:

角色模式程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. x = 65
  4. for i in range( 0, n):
  5. for j in range( 0, k):
  6. print(end= " ")
  7. k = k - 1
  8. for j in range( 0, i + 1):
  9. ch = chr(x)
  10. print(ch, end= " ")
  11. x += 1
  12. print( "\r")
  13. pattern( 7)

输出:

K形字符程序


  
  1. for i in range( 7):
  2. for j in range( 7):
  3. if j == 0 or i - j == 3 or i + j == 3:
  4. print( "*", end= "")
  5. else:
  6. print(end= " ")
  7. print()

输出:

三角形字符图案程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. x = 65
  4. for i in range( 0, n):
  5. ch = chr(x)
  6. x += 1
  7. for j in range( 0, k):
  8. print(end= " ")
  9. k = k - 1
  10. for j in range( 0, i + 1):
  11. print(ch, end= " ")
  12. print( "\r")
  13. pattern( 5)

输出:

菱形字符图案程序


  
  1. def pattern(n):
  2. k = 2 * n - 2
  3. for i in range( 0, n):
  4. for j in range( 0, k):
  5. print(end= " ")
  6. k = k - 1
  7. x = 65
  8. for j in range( 0, i + 1):
  9. ch = chr(x)
  10. print(ch, end= " ")
  11. x += 1
  12. print( "\r")
  13. k = n - 2
  14. x = 65
  15. for i in range(n, - 1, - 1):
  16. for j in range(k, 0, - 1):
  17. print(end= " ")
  18. k = k + 1
  19. for j in range( 0, i + 1):
  20. ch = chr(x)
  21. print(ch, end= " ")
  22. x += 1
  23. print( "\r")
  24. pattern( 5)

输出:

这使我们到了本文的结尾,在这里我们了解了如何在python的循环帮助下使用星号,数字和字符在python中实现不同的模式。我希望您对本教程中与您共享的所有内容一清二楚。

 

点击关注,第一时间了解华为云新鲜技术~


转载:https://blog.csdn.net/devcloud/article/details/115261348
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场