What is the difference between .py and .pyc files ?

Python compliles the .py files and save it as .pyc file

The .pyc contain the compiled bytecode of Python source files, A .pyc is not created for your main program file that you execute (only for imported modules).

The .pyc file contains encoded python bytecode.

If We Want to import module.The Module calculate Addition of Two Numbers


 Live Demo

def recursive_sum(n):
"""Function to return the sum of recursive numbers"""
if n <= 1:
return n
return n + recursive_sum(n-1)

# change this value for a different result
number = 16

if number < 0:
print("Enter a positive number")
print("The sum is",recursive_sum(number))


The sum is 136

If you store this program with the name "example",then it will be stored as "example.py" when you run a example.py file,it takes some to create a example.pyc file once the code is executed it first executes .py file where as code is first turned into byte code by the compiler in the form of "example.pyc” file.