.py#
The most common file extension for Python code files, officially known as Python source code files.
No need for much explanation~
.ipynb#
This is quite common, .ipynb is the extension for Jupyter Notebook files, which stands for "IPython Notebook".
Those who have studied data analysis, machine learning, and deep learning must be familiar with it!
.pyi#
.pyi files are type hint files in Python, used to provide static type information for the code.
They are generally used to assist developers in type checking and static analysis.
Example code:
# hellp.pyi
def hello(name: str) -> None:
print(f"hello {name}")
.pyi files are usually named the same as the corresponding .py files, so that they can be automatically associated with each other.
.pyc#
.pyc is the extension for Python bytecode files, used to store the intermediate representation of compiled Python source code, which cannot be read normally because it is a binary file.
.pyc files contain the compiled bytecode, which can be loaded and executed faster by the Python interpreter because the interpreter does not need to compile the source code again.
.pyd#
.pyd is the extension for Python extension module files, which represent binary Python extension modules written in C or C++.
.pyd files are compiled binary files that contain the compiled extension module code and the information required for interaction with the Python interpreter.
In addition, .pyd files can be imported and used in Python through the import statement, just like importing regular Python modules.
Since the execution speed of C or C++ is usually faster than pure Python code, extension modules can be used to optimize the performance of Python code, especially for computationally intensive tasks.
.pyw#
.pyw is the extension for Python windowed script files.
It represents a special type of Python script file used to create windowed applications without a command-line interface (i.e., console window).
Normally, running a Python script opens a command-line window that displays the script output and accepts user input. However, for certain applications such as graphical user interface (GUI) applications, a command-line interface is not needed and it is desired to display the interactive interface in a window. This is where .pyw files can be used.
Example code:
# click_button.pyw
import tkinter as tk
def button_click():
label.config(text="Button Clicked!")
window = tk.Tk()
button = tk.Button(window, text="Click Me", command=button_click)
button.pack()
label = tk.Label(window, text="Hello, World!")
label.pack()
window.mainloop()
.pyx#
.pyx is the extension for Cython source code files.
Cython is a compiled static typing extension language that allows the use of C language syntax and features in Python code to improve performance and interact with C language libraries.
I compared the running speed of Cython and regular Python:
fb.pyx (needs to be compiled using the cythonize command)
import fb
import timeit
def fibonacci(n):
if n <= 0:
raise ValueError("n must be a positive integer")
if n == 1:
return 0
elif n == 2:
return 1
else:
a, b = 0, 1
for _ in range(3, n + 1):
a, b = b, a + b
return b
# Pure Python version
python_time = timeit.timeit("fibonacci(300)", setup="from __main__ import fibonacci", number=1000000)
# Cython version
cython_time = timeit.timeit("fb.fibonacci(300)", setup="import fb", number=1000000)
print("Execution time for pure Python version:", python_time)
print("Execution time for Cython version:", cython_time)
run.py
import fb
import timeit
def fibonacci(n):
if n <= 0:
raise ValueError("n must be a positive integer")
if n == 1:
return 0
elif n == 2:
return 1
else:
a, b = 0, 1
for _ in range(3, n + 1):
a, b = b, a + b
return b
# Pure Python version
python_time = timeit.timeit("fibonacci(300)", setup="from __main__ import fibonacci", number=1000000)
# Cython version
cython_time = timeit.timeit("fb.fibonacci(300)", setup="import fb", number=1000000)
print("Execution time for pure Python version:", python_time)
print("Execution time for Cython version:", cython_time)
Results obtained: