ตัวอย่างการเขียนโค้ดภาษาไพทอน (Python) ในการสร้างแม่สูตรคูณ 2-13 ที่แสดงผลคล้ายๆ ที่อยู่ในปกสมุดเรียน
ตัวอย่างโค้ดสำหรับ Python 2
# -*- coding: utf-8 -*-
#-------------------------------------------------------#
# Author : Computer for Education #
# Author URI: https://www.computerforedu.com #
# Facebook : https://www.facebook.com/computerforedu #
#-------------------------------------------------------#
row = 1
while row <= 12:
num = 2
col = 1
while col <= 6:
print "%3dx%2d=%3d" % (num, row, num * row),
num += 1
col += 1
print("")
row += 1
print("")
row = 1
while row <= 12:
num = 8
col = 1
while col <= 6:
print "%3dx%2d=%3d" % (num, row, num * row),
num += 1
col += 1
print("")
row += 1
ตัวอย่างโค้ดสำหรับ Python 3
# -------------------------------------------------------#
# Author : Computer for Education #
# Author URI: https://www.computerforedu.com #
# Facebook : https://www.facebook.com/computerforedu #
# -------------------------------------------------------#
row = 1
while row <= 12:
num = 2
col = 1
while col <= 6:
print("%3dx%2d=%3d" % (num, row, num * row), end="")
num += 1
col += 1
print("")
row += 1
print("")
row = 1
while row <= 12:
num = 8
col = 1
while col <= 6:
print("%3dx%2d=%3d" % (num, row, num * row), end="")
num += 1
col += 1
print("")
row += 1
อธิบายโค้ด
- row คือ ตัวแปรที่ใช้เก็บค่า แถว 1-12
- col คือ ตัวแปรที่ใช้เก็บค่า คอลัมน์ 1-6
- num คือ ตัวแปรที่ใช้เก็บค่า แม่สูตรคูณ
- การแสดงผลจะแบ่งออกเป็น 2 ชุด คือ 1.จะแสดง 2-7 และ 2.จะแสดง 8-13
- ในแต่ละชุดจะใช้ while ในการวนลูปเพื่อแสดงผล while ที่ 1 จะแสดงแถว และ while ที่ 2 จะแสดงคอลัมน์
แสดงผล

