Python 3 basic multiplication table

Keywords: PHP Python Google Ubuntu Pycharm

  •        Python : 3.7.3
  •          OS : Ubuntu 18.04.2 LTS
  •         IDE : pycharm-community-2019.1.3
  •       Conda : 4.7.5
  •    typesetting : Markdown

code_1

"""
@Author : Xing Xin
@Date   : 2019/7/2
@Blog   : www.cnblogs.com/xingchuxin
@Gitee  : gitee.com/zhichengjiu
"""


def main():
    # The final value is 9
    end_num = 9

    # Row counter
    row = 1
    while row <= end_num:
        col = 1
        while col <= row:
            print("%d * %d = %d" % (row, col, row * col), end="  ")
            col += 1
        print("")
        row += 1


if __name__ == '__main__':
    main()

result_1

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/SimpleExample/demo.py
1 * 1 = 1  
2 * 1 = 2  2 * 2 = 4  
3 * 1 = 3  3 * 2 = 6  3 * 3 = 9  
4 * 1 = 4  4 * 2 = 8  4 * 3 = 12  4 * 4 = 16  
5 * 1 = 5  5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25  
6 * 1 = 6  6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36  
7 * 1 = 7  7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49  
8 * 1 = 8  8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64  
9 * 1 = 9  9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81  

Process finished with exit code 0

code_2

"""
@Author : Xing Xin
@Date   : 2019/7/2
@Blog   : www.cnblogs.com/xingchuxin
@Gitee  : gitee.com/zhichengjiu
"""


def main():
    # The final value is 9
    end_num = 9

    # Row counter
    row = 1
    while row <= end_num:
        col = 1
        while col <= row:
            if col == row:
                # The end of the last item in each line, without spaces
                print("%d * %d = %d" % (row, col, row * col), end="")
            else:
                print("%d * %d = %d" % (row, col, row * col), end="  ")
            col += 1
        print("")
        row += 1


if __name__ == '__main__':
    main()

result_2

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/SimpleExample/demo.py
1 * 1 = 1
2 * 1 = 2  2 * 2 = 4
3 * 1 = 3  3 * 2 = 6  3 * 3 = 9
4 * 1 = 4  4 * 2 = 8  4 * 3 = 12  4 * 4 = 16
5 * 1 = 5  5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25
6 * 1 = 6  6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36
7 * 1 = 7  7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49
8 * 1 = 8  8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64
9 * 1 = 9  9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81

Process finished with exit code 0

code_3

"""
@Author : Xing Xin
@Date   : 2019/7/2
@Blog   : www.cnblogs.com/xingchuxin
@Gitee  : gitee.com/zhichengjiu
"""


def main():
    # The final value is 9
    end_num = 9

    # Row counter
    row = 1
    while row <= end_num:
        # Column counter
        col = 1

        while col <= row:

            if col == row:
                # The end of the last item in each line, without spaces
                print("%d * %d = %d" % (row, col, row * col), end="")
            else:
                if (row == 3 or row == 4) and (col == 2):
                    # Add an extra space to the second column of the third and fourth lines
                    print("%d * %d = %d" % (row, col, row * col), end="   ")
                else:
                    print("%d * %d = %d" % (row, col, row * col), end="  ")
            col += 1

        print("")
        row += 1


if __name__ == '__main__':
    main()

result_3

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/SimpleExample/demo.py
1 * 1 = 1
2 * 1 = 2  2 * 2 = 4
3 * 1 = 3  3 * 2 = 6   3 * 3 = 9
4 * 1 = 4  4 * 2 = 8   4 * 3 = 12  4 * 4 = 16
5 * 1 = 5  5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25
6 * 1 = 6  6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36
7 * 1 = 7  7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49
8 * 1 = 8  8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64
9 * 1 = 9  9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81

Process finished with exit code 0

code_4

"""
@Author : Xing Xin
@Date   : 2019/7/2
@Blog   : www.cnblogs.com/xingchuxin
@Gitee  : gitee.com/zhichengjiu
"""


def main():
    # The final value is 9
    end_num = 9

    # Row counter
    row = 1
    while row <= end_num:
        # Column counter
        col = 1

        while col <= row:
            # Use escape character \ t, tab, align vertically, easy to use
            print("%d * %d = %d" % (row, col, row * col), end="\t")
            col += 1

        print("")
        row += 1


if __name__ == '__main__':
    main()

result_4

/home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/SimpleExample/demo.py
1 * 1 = 1   
2 * 1 = 2   2 * 2 = 4   
3 * 1 = 3   3 * 2 = 6   3 * 3 = 9   
4 * 1 = 4   4 * 2 = 8   4 * 3 = 12  4 * 4 = 16  
5 * 1 = 5   5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25  
6 * 1 = 6   6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36  
7 * 1 = 7   7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49  
8 * 1 = 8   8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64  
9 * 1 = 9   9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81  

Process finished with exit code 0

resource

  • [document - English] docs.python.org/3
  • [document Chinese] docs.python.org/zh-cn/3
  • [Specification] www.python.org/dev/peps/pep-0008
  • [Specification] zh Google styleguide.readthedocs.io/en/latest/google-python-styleguide/python'language'rules
  • [source] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [platform] www.cnblogs.com
  • [platform] gitee.com

Python has the characteristics of open source, cross platform, interpretive, interactive and so on, which is worth learning.
Python's design philosophy: elegant, clear, simple. Advocate one way, preferably only one way to do one thing.
The code should be written in accordance with the standard, which is conducive to communication and understanding.
Each language has its own unique ideas. Beginners need to change their thinking, practice and accumulate.

Posted by grga on Thu, 31 Oct 2019 15:56:11 -0700