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


    摘要:在本文中,我们将学习python中的各种模式程序。

    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中的一些星型程序。

    金字塔图案程序

    def pattern(n):
          k = 2 * n - 2
          for i in range(0,n):
               for j in range(0,k):
                   print(end=" ")
               k = k - 1
               for j in range(0, i+1):
                    print("*", end=" ")
               print("
    ")
     
    pattern(5)

    输出:

    反金字塔图案程序

    def pattern(n):
          k = 2*n -2
          for i in range(n,-1,-1):
               for j in range(k,0,-1):
                    print(end=" ")
               k = k +1
               for j in range(0, i+1):
                    print("*", end=" ")
               print("
    ")
     
    pattern(5)

    输出:

    正确启动模式程序

    def pattern(n):
          for i in range(0, n):
               for j in range(0, i + 1):
                    print("* ", end="")
               print("
    ")
          for i in range(n, 0 , -1):
              for j in range(0, i + 1):
                   print("* ", end="")
              print("
    ")
     
    pattern(5)

    输出:

    左启动模式程序

    def pattern(n):
        k = 2 * n - 2
        for i in range(0, n-1):
            for j in range(0, k):
                print(end=" ")
            k = k - 2
            for j in range(0, i + 1):
                print("* ", end="")
            print("
    ")
        k = -1
        for i in range(n-1,-1,-1):
            for j in range(k,-1,-1):
                print(end=" ")
            k = k + 2
            for j in range(0, i + 1):
                print("* ", end="")
            print("
    ")
     
    pattern(5)

    输出:

    沙漏图案程序

    def pattern(n):
         k = n - 2
         for i in range(n, -1 , -1):
              for j in range(k , 0 , -1):
                   print(end=" ")
              k = k + 1    
              for j in range(0, i+1):
                   print("* " , end="")
              print("
    ")
          k = 2 * n  - 2
          for i in range(0 , n+1):
               for j in range(0 , k):
                   print(end="")
               k = k - 1
                for j in range(0, i + 1):
                     print("* ", end="")
                print("
    ")
     
    pattern(5)

    输出:

    半金字塔图案程序

    def pattern(n):
         for i in range(0,n):
             for j in range(0, i+1):
                  print("* " , end="")
             print("
    ")
     
    pattern(5)

    输出:

    左半金字塔图案程序

    def pattern(n):
         k = 2 * n - 2
         for i in range(0, n):
              for j in range(0, k):
                   print(end=" ")
              k = k - 2
              for j in range(0, i + 1):
                  print("* ", end="")
              print("
    ")
     
    pattern(5)

    输出:

    向下半金字塔图案程序

    def pattern(n):
          for i in range(n, -1, -1):
               for j in range(0, i + 1):
                   print("* ", end="")
               print("
    ")
     
    pattern(5)

    输出:

    菱形图案程序

    def pattern(n):
         k = 2 * n - 2
         for i in range(0, n):
              for j in range(0 , k):
                   print(end=" ")
              k = k - 1
              for j in range(0 , i + 1 ):
                   print("* ", end="")
              print("
    ")
         k = n - 2
         for i in range(n , -1, -1):
              for j in range(k , 0 , -1): 
                   print(end=" ")
               k = k + 1
               for j in range(0 , i + 1):
                    print("* ", end="")
               print("
    ")
     
    pattern(5)

    输出:

    钻石星型计划

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

    输出:

    数字模式程序

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

    简单数字程序

    def pattern(n):
        x = 0
        for i in range(0 , n):
            x += 1 
            for j in range(0, i + 1):
                print(x , end=" ") 
            print("
    ") 
    pattern(5)

    输出:

    帕斯卡三角计划

    def pascal(n):
        for i in range(0, n):
            for j in range(0, i + 1):
                print(function(i, j)," ", end="")
            print()
     
    def function(n, k):
        res = 1
        if (k > n - k):
            k = n - k
        for i in range(0, k):
            res = res * (n - i)
            res = res // (i + 1)
     
        return res
     
    pascal(7)

    输出:

    半金字塔图案与数字

    def pattern(n):
         for i in range(1, n):
             for j in range(1, i + 1):
                 print(j, end= " ")
             print("
    ")
    pattern(5)

    输出:

    菱形图案与数字

    def pattern(n):
        k = 2 * n - 2
        x = 0
        for i in range(0, n):
            x += 1
            for j in range(0, k):
                print(end=" ")
            k = k - 1
            for j in range(0, i + 1):
                print(x, end=" ")
            print("
    ")
        k = n - 2
        x = n + 2
        for i in range(n, -1, -1):
            x -= 1
            for j in range(k, 0, -1):
                print(end=" ")
            k = k + 1
            for j in range(0, i + 1):
                print(x, end=" ")
            print("
    ")
     
    pattern(5)

    输出:

    降序模式程序

    def pattern(n):
        for i in range(n, 0, -1):
            for j in range(1, i + 1):
                print(j, end=" ")
     
            print("
    ")
     
    pattern(5)

    输出:

    二进制数字模式程序

    def pattern(n):
        k = 2 * n - 2
        for i in range(0, n):
            for j in range(0, k):
                print(end=" ")
            k = k - 1
            for j in range(0, i + 1):
                print('10', end="")
     
            print("
    ")
     
    pattern(5)

    输出:

    字符模式程序

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

    右字母三角

    def pattern(n):
        x = 65
        for i in range(0, n):
            ch = chr(x)
            x += 1
            for j in range(0, i + 1):
                print(ch, end=" ")
            print("
    ")
     
    pattern(5)

    输出:

    角色模式程序

    def pattern(n):
        k = 2 * n - 2
        x = 65
        for i in range(0, n):
            for j in range(0, k):
                print(end=" ")
            k = k - 1
            for j in range(0, i + 1):
                ch = chr(x)
                print(ch, end=" ")
                x += 1
            print("
    ")
    pattern(7)

    输出:

    K形字符程序

    for i in range(7):
        for j in range(7):
            if j == 0 or i - j == 3 or i + j == 3:
                print("*", end="")
            else:
                print(end=" ")
        print()

    输出:

    三角形字符图案程序

    def pattern(n):
        k = 2 * n - 2
        x = 65
        for i in range(0, n):
            ch = chr(x)
            x += 1
            for j in range(0, k):
                print(end=" ")
            k = k - 1
            for j in range(0, i + 1):
                print(ch, end=" ")
            print("
    ")
    pattern(5)

    输出:

    菱形字符图案程序

    def pattern(n):
        k = 2 * n - 2
        for i in range(0, n):
            for j in range(0, k):
                print(end=" ")
            k = k - 1
            x = 65
            for j in range(0, i + 1):
                ch = chr(x)
                print(ch, end=" ")
                x += 1
            print("
    ")
        k = n - 2
        x = 65
        for i in range(n, -1, -1):
            for j in range(k, 0, -1):
                print(end=" ")
            k = k + 1
            for j in range(0, i + 1):
                ch = chr(x)
                print(ch, end=" ")
                x += 1
            print("
    ")
    pattern(5)

    输出:

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

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

     

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

  • 相关阅读:
    二十二、Linux 进程与信号---进程创建
    二十一、Linux 进程与信号---进程查看和进程状态、进程调度和进程状态变化、进程标识
    二十一、Linux 进程与信号---进程资源限制
    二十、Linux 进程与信号---非局部跳转
    AOSP android 源码下载
    十九、Linux 进程与信号---环境表
    十八、Linux 进程与信号---进程介绍
    09 scroll系列_简单的封装动画函数(变速(缓动)的动画函数_getComputedStyle函数_复杂的动画函数封装_颜色相关_案例_client系列
    A 第二课 栈_队列_堆
    pycharm 的快捷键
  • 原文地址:https://www.cnblogs.com/huaweiyun/p/14585173.html
Copyright © 2020-2023  润新知