Fundamentals 4 min read

10 Fun One‑Line Python Programs (Quine, Web Server, FizzBuzz, and More)

This article showcases ten intriguing one‑line Python programs—including a Quine, a minimal web server, variable swapping, FizzBuzz, heart‑shaped text, Mandelbrot rendering, multiplication table, prime number generator, Fibonacci sequence, and quicksort—illustrating how concise code can achieve diverse tasks.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
10 Fun One‑Line Python Programs (Quine, Web Server, FizzBuzz, and More)

A program that can print its own source code is called a Quine.

Below is a one‑line Python Quine:

Some say that without a semicolon it is not a line; here is a version without a semicolon:

If you search for "language name + quine" you can find many implementations. For language enthusiasts, writing an elegant quine is an important achievement.

Similarly, each new language must achieve bootstrapping (implementing its own compiler or interpreter) to be considered respectable.

One line of code outputs “The Zen of Python”:

From “The Zen of Python” we see Python promotes principles such as Beautiful, Explicit, and Simple; the following one‑liners may even violate the Explicit principle.

1. One‑line code to start a web service:

2. One‑line code to swap variable values:

3. One‑line code to solve the FizzBuzz problem (print numbers 1‑100, replacing multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and multiples of both with “FizzBuzz”):

4. One‑line code to output a heart shape made of the characters “Love”:

5. One‑line code to generate a Mandelbrot image (each pixel corresponds to a complex number N = x + y*i):

6. One‑line code to print the multiplication table (九九乘法表):

7. One‑line code to compute prime numbers between 1 and 100 (two versions):

8. One‑line code to output the Fibonacci sequence:

9. One‑line code to implement the quicksort algorithm:

10. One‑line code to solve the eight‑queen problem:

- END -

PythonProgrammingCode GolfOne‑LinerQuine
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.