What is a PY File?
A PY file is a Python source code file: plain text containing a program written in Python, the language created by Guido van Rossum and first released in 1991. Python is one of the most popular programming languages, used for web development, automation, data analysis and machine learning.
You run a PY file with the Python interpreter (python sample.py), and you can open or edit it in any text editor or in an IDE such as VS Code or PyCharm. Python uses indentation to structure code, so editors and tools must keep tabs and spaces intact.
Download PY Sample Files
Below are three short Python 3 programs.
PY Sample File Details
| Sample | What it does | Lines | File size | Asks for input |
|---|---|---|---|---|
| Sample1.PY | Checks whether 29 is a prime number | 25 | 556 bytes | No |
| Sample2.PY | Menu-driven calculator (add, subtract, multiply, divide) | 54 | 1,398 bytes | Yes |
| Sample3.PY | Prints the square root of 8 | 10 | 272 bytes | No |
The files are ASCII text with Windows (CRLF) line endings, which Python accepts on every platform. They use no imports and run on any Python 3 version. Samples 1 and 3 print their result straight away; sample 2 keeps asking for calculations until you answer “no”:
$ python sample-1.py
29 is a prime number
$ python sample-3.py
The square root of 8.000 is 2.828
What You Can Test With These PY Files
- Code editors: syntax highlighting, indentation and previews.
- Code runners: online interpreters and sandboxes, including programs that wait for input.
- Upload filters: accepting or blocking .py files and
text/x-python. - Line endings: CRLF files in Linux and macOS tools.
- Static analysis: linters and formatters such as Ruff, Flake8 or Black.
How to Run or Check a PY File
# Run a sample
python sample-1.py
# Check the syntax without running the program
python -m py_compile sample-2.py
# Show the encoding and line endings
file sample-1.py
PY vs Other Script Files
| Language | Extension | Run with |
|---|---|---|
| Python | .py | python |
| Shell script | .sh | bash |
| Ruby | .rb | ruby |
| Perl | .pl | perl |
| PHP | .php | php or a web server |
| Java | .java | javac, then java |






