ตัวอย่างในการเขียนโค้ดภาษา Python โดยนำเอาตัวเลข 1-9 มาสร้างเป็นรูปสามเหลี่ยม ซึ่งมีลูป while เป็นการแสดงผลในแต่ละบรรทัด และมีลูปย่อยอีก 3 ลูป คือ ลูปที่ 1 แสดงพื้นที่ว่าง ลูปที่ 2 แสดงตัวเลขที่เพิ่มขึ้น และลูปที่ 3 แสดงตัวเลขที่ลดลง
ตัวอย่างโค้ด Python
# -------------------------------------------------------#
# Author : Computer for Education #
# Author URI: https://www.computerforedu.com #
# Facebook : https://www.facebook.com/computerforedu #
# -------------------------------------------------------#
max_num = 9
i = 0
while i < max_num:
i += 1
space = max_num - i
for s in range(space):
print(" ", end='')
for n in range(1, (max_num - space) + 1):
print(n, end='')
for n in reversed(range(1, i)):
print(n, end='')
print("")
แสดงผล

