
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Common Header Format for Python Files
The common header format of Python files is a simple and essential element in any Python script. The Header Format is just an introduction that provides context.
In Python, we commonly use a docstring as the header format. A docstring is a special kind of comment enclosed within triple quotes, i.e., either single or double. It's placed right at the beginning of the script, even before any import statements, making it easily visible and accessible to anyone who reads the code.
The standard structure of a Python file header is adorned with a docstring. Following is an example of doc string -
""" File: filename.py Author: Your Name Date: Date of creation/modification Description: A brief explanation of what this script does. """
Let's break down the components of this utilitarian header format as mentioned above -
- File: Here, we include the name of the Python file, like filename.py, so that there's no confusion about the script's identity.
- Author: This line allows us to attribute the code to the ingenious individual behind it, you or whoever created or modified the script.
- Date: The timeline of creation or modification of a valuable piece of information that helps us to keep things organized and transparent.
- Description: This is the heart of the header, and this part presents a concise yet impactful summary of what the Python script is all about.
A Basic Python Script with Header
Here is the example of basic python script with a header that contains the docstring in triple quotes at the beginning of the file by providing essential information about the script -
""" File: hello.py Author: John Doe Date: 2025-04-09 Description: A simple Python script to print "Hello, world!" """ print("Hello, world!")
Below are the details of the header elements in the above Python script -
- File indicates the name of the Python file, which is hello.py in this case.
- Author credits the author of the script as "John Doe."
- Date specifies the creation or modification date as April 09, 2025.
- Description offers a brief explanation that this script achieves the objective of printing "Hello, world!"
Below is the output of the basic python script with header -
Hello, world!
A Python Script with a Function and Header
We can create a python script as a collection of functions along with a docstring that gives the necessary details about the script's purpose and the author of the script.
Following is the example where we are using the docstring and the functions such as add_numbers, subtract_numbers, multiply_numbers, and divide_numbers in the script to perform addition, subtraction, multiplication and division, respectively -
""" File: math_operations.py Author: Jane Smith Date: 2025-04-09 Description: A Python script to perform basic math operations. """ def add_numbers(a, b): return a + b def subtract_numbers(a, b): return a - b def multiply_numbers(a, b): return a * b def divide_numbers(a, b): return a / b # Example usage result = add_numbers(5, 3) print("Result of addition:", result)
Here is the output of the above example, which prints the result of the respective called function -
Result of addition: 8
Python Script with Class and Header
The docstring at the very beginning clearly outlines the script's objective: to define a Student class and its methods.
In this example, we created a Student class that is provided with an __init__ constructor and a greet method. The constructor initializes the name and age attributes for each student object, and the 'greet method' gives the output as a personalized greeting for the student with their name and age and the usage section at the very end demonstrates creating a Student object and calling the greet method.
""" File: student.py Author: Michael Johnson Date: 2025-04-09 Description: A Python script defining a Student class and its methods. """ class Student: def __init__(self, name, age): self.name = name self.age = age def greet(self): return f"Hello, my name is {self.name} and I'm {self.age} years old." # Example usage student1 = Student("Riyaansh", 20) print(student1.greet())
Here is the output of the above code -
Hello, my name is Riyaansh and I'm 20 years old.
Python Script with Conditional Statements and Header
As we know the docstring introduces the script's purpose. Here in this example it gives the description, how to convert temperatures between Celsius and Fahrenheit. The two functions celsius_to_fahrenheit and fahrenheit_to_celsius perform the respective temperature conversions and the usage section at the end of the code shows how to convert a temperature from Celsius to Fahrenheit and print the result.
""" File: temperature_converter.py Author: Emily Davis Date: 2025-04-09 Description: A Python script to convert temperatures between Celsius and Fahrenheit. """ def celsius_to_fahrenheit(celsius): fahrenheit = (celsius * 9/5) + 32 return fahrenheit def fahrenheit_to_celsius(fahrenheit): celsius = (fahrenheit - 32) * 5/9 return celsius # Example usage temperature_celsius = 35 temperature_fahrenheit = celsius_to_fahrenheit(temperature_celsius) print(f"{temperature_celsius} degrees Celsius is equal to {temperature_fahrenheit:.2f} degrees Fahrenheit.")
Below is the output of the above Python code -
35 degrees Celsius is equal to 95.00 degrees Fahrenheit.