Fun Python Code Examples: Heart Pattern, Multiplication Table, Wave Display, and Simple Login Simulation
This article presents a collection of playful Python scripts—including a heart-shaped Love pattern, a formatted 9×9 multiplication table, a random wave animation, and a simple username/password login demo—to illustrate Python’s versatility and inspire readers with creative coding ideas.
Python is a clear and easy-to-learn language, and this article showcases several entertaining code examples that reveal its expressive power.
Heart pattern – a one‑liner that prints a stylized “Love” heart using mathematical conditions.
print('\n'.join([''.join(('Love'[(x-y)%4] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else ' ') for x in range(-30,30)]) for y in range(15,-15,-1)))9×9 multiplication table – generates the full table with formatted strings.
print('\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)]))Wave display – an infinite loop that randomly prints the characters “╱” or “╲” to simulate a moving wave.
while 1: import random; print(random.choice('╱╲'), end='')Simple login simulation – creates a random username (5 letters) and password (6 alphanumeric characters) and then prompts the user to enter them, allowing three attempts for each before restarting.
import random, string
p = "".join([random.choice(string.ascii_letters) for i in range(5)])
q = "".join([random.choice(string.ascii_letters+string.hexdigits) for i in range(6)])
print(p)
print(q)
# login logic
flag = 0
count = 0
while True:
username = input("输入你的名字")
if username == p:
while True:
passwd = input("输入你的密码")
if passwd == q:
print("成功进入")
break
else:
flag += 1
if flag == 3:
break
else:
count += 1
if count == 3:
breakImages illustrating the output of the heart pattern and wave animation are included in the original article.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.