pandas crosstab in Python: Counts, Totals and Sums

pandas crosstab: Command Prompt output of pd.crosstab summing sales by region and category with totals

A pandas crosstab counts how often each pair of values shows up: call pd.crosstab(rows, columns) with two columns and you get a frequency table, one row per unique value of the first and one column per unique value of the second. Every table below came from a real run on Python 3.12.5, pandas 3.0.6 and … Read more >>

NumPy split(): How to Split Arrays in Python

NumPy split: Command Prompt output of np.split failing on an uneven split and np.array_split working

To split a NumPy array, call np.split(arr, n) for n equal pieces or np.split(arr, [i, j]) to cut at specific indices. When the length doesn’t divide evenly, use np.array_split() instead, which allows unequal pieces: In this tutorial, we’ll cover NumPy split from the basics to 2D arrays. I ran every example with Python 3.12.5 and … Read more >>

How to Indent Multiple Lines in Python

Indent multiple lines in Python: two lines indented with Indent Region in the IDLE editor

To indent multiple lines in Python, select the lines in your editor and press Tab (or Ctrl+] in VS Code and IDLE). Shift+Tab or Ctrl+[ moves them back out. For a multi-line string inside your code, textwrap.indent() does the same job: Every run below happened on Windows 11 with Python 3.12.5, and the editor screenshots … Read more >>

Go vs Python: Which Language Should You Choose?

Go programming language vs Python: Command Prompt timing Python threads for I/O and CPU work

Comparing the Go programming language vs Python, Go wins on raw speed, concurrency and single-file deployment, while Python wins on ease of learning, libraries, data science and AI. Pick Go for fast network services; pick Python for almost everything else. Go isn’t installed on my test machine, so the Go snippets appear as code only. … Read more >>

How to Sum All Values in a Python Dictionary

Sum all values in a Python dictionary: Command Prompt output of sum() adding a monthly budget in USD

If you need to sum all values in a Python dictionary, call sum(d.values()). It adds every value in one pass, and an empty dictionary simply gives 0: My test data is a household budget in US dollars, and I ran each script in a Command Prompt with Python 3.12.5. In this tutorial, we’ll total a … Read more >>

How to Create a Dictionary in Python Using a For Loop

Create a dictionary in Python using a for loop: Command Prompt output of a loop filling city keys

Start with empty braces and fill them inside the loop. To create a dictionary in Python using a for loop, assign d[key] = value on each pass, and the key is added the first time it shows up: Every output below is copied from runs on Python 3.12.5, using small US store and order data. … Read more >>

How to Count the Number of Keys in a Python Dictionary

Count the number of keys in a Python dictionary: Command Prompt output of len() on an inventory dictionary

Python’s built-in len() is all you need: to count the number of keys in a Python dictionary, pass the dictionary to len() and it returns the key total instantly, because a dict keeps that number on hand. Every result on this page came straight out of a Command Prompt window running Python 3.12.5. In this … Read more >>

How to Create a Dictionary from a List in Python

Create a dictionary from a list in Python: Command Prompt output of dict() turning a list of tuples into a dictionary

To create a dictionary from a list in Python, match the method to what the list holds: dict(pairs) for a list of key-value pairs, dict.fromkeys(keys) for a list of keys,: I pasted every output below from real runs on Python 3.12.5, so you can trust them. In this tutorial, we’ll go through each list shape, … Read more >>

51 Python Data Science Interview Questions and Answers (2026)

Python data science interview questions: Command Prompt output of a churn model's baseline, cross-validation and precision and recall

Most python data science interview questions fall into five groups: core Python for data work, NumPy, pandas, statistics and the machine learning workflow, followed by a live coding round on a messy DataFrame. Below are 52 of them with short answers and real output. Here’s a warm-up that catches a lot of candidates. NumPy and … Read more >>

Scipy Stats: Statistical Analysis in Python

scipy stats

Over my 10+ years of Python development, I’ve found that data analysis always comes back to one essential skill: understanding statistics. And when it comes to statistical analysis in Python, SciPy Stats has been my go-to library. Whether I’m analyzing customer behavior for e-commerce platforms or processing election polling data, Scipy Stats has proven to … Read more >>