Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Input and Output

Display table of values using tabular format - Python Program

Example 1:

print("Num\tSquare\tCube") for i in range(1, 6): print(f"{i}\t{i**2}\t{i**3}")

Output

 
OUTPUT  :
Num Square  Cube
1   1   1
2   4   8
3   9   27
4   16  64
5   25  125 

Example 2:

Displaying Tabular Data in Python

Python provides several built-in and third-party libraries that make it easy to display tabular data in a variety of formats. Here are some of the most commonly used techniques:

Using the print() Function

The simplest way to display tabular data in Python is by using the print() function. You can create a list of lists or a 2D array and print it row by row:

 

data = [ ["Name", "Age", "Gender"], ["John", 25, "Male"], ["Jane", 30, "Female"], ["Bob", 35, "Male"] ] for row in data: print(", ".join(str(x) for x in row))

Output

 
OUTPUT  :
Name, Age, Gender
John, 25, Male
Jane, 30, Female
Bob, 35, Male

Example 3:

Using the tabulate Library

The tabulate library provides a more sophisticated way to display tabular data. It can format the data in various styles, such as grid, simple, and fancy grid:

 

from tabulate import tabulate data = [ ["Name", "Age", "Gender"], ["Ronnie", 21, "Male"], ["Anu", 20, "Female"], ["Rajesh", 25, "Male"] ] print(tabulate(data, headers="firstrow", tablefmt="grid"))

Output

 
OUTPUT  :
+----------+-----+----------+
| Name     | Age | Gender   |
+----------+-----+----------+
| Ronnie   | 21  | Male        |
| Anu       | 20  | Female     |
| Rajesh   | 25  | Male        |
+----------+-----+----------+

Example 4:

Using the pandas Library

The pandas library is a powerful data manipulation and analysis tool that provides a DataFrame object, which can be used to represent and display tabular data. DataFrame offers a wide range of formatting options and customization features:

 

import pandas as pd data = [ ["Ronnie", 21, "Male"], ["Anu", 20, "Female"], ["Raja", 25, "Male"] ] df = pd.DataFrame(data, columns=["Name", "Age", "Gender"]) print(df)

Output

 
OUTPUT  :
     Name  Age  Gender
0  Ronnie   21    Male
1     Anu   20  Female
2    Raja   25    Male
 

Example 5:

Customizing Tabular Data Presentation

Basic capability for showing tabular data is provided by the built-in and third-party libraries, but there are many situations in which you might wish to further modify the presentation to meet your unique requirements. Here are a few popular methods for altering how tabular data is shown in Python

Adjusting Column Widths and Alignments

In your tabular data, you have control over the column width and alignment. This is very helpful when working with data that has different column lengths or when you wish to make the output easier to read.

from tabulate import tabulate data = [ ["Name", "Age", "Gender"], ["Ronnie", 21, "Male"], ["Anu", 20, "Female"], ["Rajesh", 25, "Male"] ] print(tabulate(data, headers=["Name", "Age", "Gender"], tablefmt="grid", colalign=("left", "right", "center")))

Output

 
OUTPUT  :
+----------+-----+----------+
| Name     | Age | Gender   |
+----------+-----+----------+
| Ronnie   | 21  | Male        |
| Anu       | 20  | Female     |
| Rajesh   | 25  | Male        |
+----------+-----+----------+
 

Example 6:

Applying Conditional Formatting

You can apply conditional formatting to highlight specific values or patterns in your tabular data. This can be particularly useful for data analysis and reporting tasks.

 

import pandas as pd data = [ ["John", 25, "Male"], ["Jane", 30, "Female"], ["Bob", 35, "Male"] ] df = pd.DataFrame(data, columns=["Name", "Age", "Gender"]) ## Apply conditional formatting def highlight_gender(gender): if gender == "Male": return 'color: blue' else: return 'color: red' print(df.style.apply(lambda x: highlight_gender(x["Gender"]), axis=1))

Output

 
OUTPUT  :

This will output the DataFrame with the gender column highlighted in blue for "Male" and red for "Female".

Name Age Gender ------------------------------ John 25 Male  Jane 30 Female  Bob 35 Male 

Advanced Program

To display a table of values in a tabular format in Python, the tabulate  library is a highly effective and recommended choice due to its ease of use and various formatting options.

  1. Installation:

First, install the tabulate library using pip if you haven't already:

 

pip install tabulate

 

  1. Program to Display a Table:

Here is a complete program demonstrating how to display a table using tabulate:

Example 7: Advanced Program

from tabulate import tabulate # Sample data as a list of lists data = [ ["Ronnie", 24, "Engineer"], ["Raja", 30, "Data Scientist"], ["Danny", 28, "Teacher"], ["Navita", 35, "Doctor"] ] # Define the headers for the table headers = ["Name", "Age", "Profession"] # Display the table using tabulate # tablefmt="grid" creates a table with borders print("--- Employee Information ---") print(tabulate(data, headers=headers, tablefmt="grid")) print("\n--- Another Example (using 'simple' format) ---") # Another example with a different table format data_products = [ ["Laptop", 1200, "Electronics"], ["Desk Chair", 250, "Furniture"], ["Coffee Maker", 80, "Appliances"] ] headers_products = ["Product", "Price ($)", "Category"] print(tabulate(data_products, headers=headers_products, tablefmt="simple"))

Output

 
OUTPUT  :
--- Employee Information ---
+--------+-------+----------------+
| Name   |   Age | Profession     |
+========+=======+================+
| Ronnie |    24 | Engineer       |
+--------+-------+----------------+
| Raja   |    30 | Data Scientist |
+--------+-------+----------------+
| Danny  |    28 | Teacher        |
+--------+-------+----------------+
| Navita |    35 | Doctor         |
+--------+-------+----------------+

--- Another Example (using 'simple' format) ---
Product         Price ($)  Category
------------  -----------  -----------
Laptop               1200  Electronics
Desk Chair            250  Furniture
Coffee Maker           80  Appliances