Guide for C++ developers
Python for C++ Developers: Learn Python Using What You Already Know
Already know C++? Discover how familiar programming concepts translate into Python, what works differently, and the fastest path to getting productive.
C++ vs Python, side by side
Each pair below does the same job. Start with the code you know, then see how Python expresses it.
Static vs dynamic typing
int count = 3;
count = "three"; // compile error
std::string name = "Ada";count = 3
count = "three" # allowed
name: str = "Ada"C++ fixes each variable's type at compile time. In Python a name can be rebound to a value of any type, and types are checked only when the line runs. Type hints such as name: str are read by tools like mypy, not enforced at runtime, and integers never overflow.
Memory, pointers and references
int value = 10;
int* ptr = &value;
*ptr = 20;
void bump(int& n) { n += 1; }a = [1, 2]
b = a
b.append(3)
print(a) # [1, 2, 3]Python has no pointers or manual memory management: every name is a reference to an object, and objects are freed when nothing refers to them. b = a copies the reference, not the list. Integers and strings are immutable, so n += 1 inside a function rebinds a local name instead of changing the caller's value.
std::vector vs list
std::vector<int> nums = {1, 2, 3};
nums.push_back(4);
// holds only ints, stored side by sidenums = [1, 2, 3]
nums.append(4)
mixed = [1, "two", 3.0]std::vector stores values of one type side by side in memory. A Python list stores references to objects, so it can hold mixed types, which is flexible but heavier and slower for large amounts of numeric data. Use the array module or NumPy for compact numeric storage.
Loops and STL algorithms
std::vector<int> evens;
for (int n : nums) {
if (n % 2 == 0) evens.push_back(n);
}
int total = std::accumulate(nums.begin(), nums.end(), 0);
std::sort(nums.begin(), nums.end());evens = [n for n in nums if n % 2 == 0]
total = sum(nums)
nums.sort()Most STL algorithms become comprehensions or built-ins such as sum, min, max, any and all, and you pass the collection itself instead of an iterator pair. nums.sort() sorts in place, while sorted(nums) returns a new list.
What C++ developers need to unlearn
A few habits carry over badly. These shifts matter most.
Indentation is the syntax
Blocks start with a colon and are defined by indentation instead of braces. Use four spaces and stay consistent.
Types are checked at runtime
Dynamic typing means a name can refer to any value, and type errors appear when the line runs. Optional type hints plus a checker such as
mypybring back some of C++'s safety.Iterate over things, not indexes
Loop over the collection itself. Reach for
enumerateorzipbefore writingrange(len(items)).Lean on built-in collections
Lists, dictionaries, sets and tuples cover most of what you import from the STL. A comprehension such as
[len(w) for w in words]replaces a simple loop that builds a list; add anifat the end to filter.No manual memory management
There are no pointers to manage or free, no header and implementation split, and no
private:section: Python cleans up objects for you. Usewithwhere you would rely on RAII, and write names insnake_case.
Your C++-to-Python learning roadmap
Translate familiar concepts
Map familiar syntax and concepts to Python.
Learn Python's differences
Collections, functions, idiomatic style.
Practice with real code
Solve small problems, not just read.
Build confidence
Harder, more realistic challenges.
Hintor's Python for Developers track is designed for developers switching languages, with hands-on practice built into the lessons. Explore the Python for Developers track
Mini challenge
Given a list of integers, write Python code that creates a new list containing the squares of only the even numbers.
- Input
- [1, 2, 3, 4, 5, 6]
- Expected output
- [4, 16, 36]
Hint: a list comprehension can include an if filter, and n % 2 == 0 tests whether n is even.
numbers = [1, 2, 3, 4, 5, 6]
even_squares = [n * n for n in numbers if n % 2 == 0]
print(even_squares) # [4, 16, 36]The comprehension reads left to right: compute n * n for each n in numbers, keeping only the even ones. A regular for loop with append works too; the comprehension is the idiomatic shortcut.
Ready to Turn Your C++ Knowledge into Python Skills?
You already know how to program. Now learn to think in Python with guided lessons and hands-on practice.
Start Learning Python on Hintor