Part of the series

Several example codes

~1 min read • Updated Sep 30, 2025

Program Overview

This Python program displays the number of processors (logical CPU cores) available on the system.
It uses the os.cpu_count() function from the built-in os module.


Python Code:


import os

# Display number of CPU cores
cpu_count = os.cpu_count()
print("Number of CPU cores:", cpu_count)

Sample Output:


Number of CPU cores: 8

Technical Explanation:

- os.cpu_count() returns the number of logical cores
- This may include both physical and virtual cores (e.g., Hyper-Threading)
- For more detailed system info, you can use the psutil library


Advanced Version with psutil:


import psutil

# Get logical and physical core counts
logical_cores = psutil.cpu_count(logical=True)
physical_cores = psutil.cpu_count(logical=False)

print("Logical cores:", logical_cores)
print("Physical cores:", physical_cores)

Written & researched by Dr. Shahin Siami