.. meta::
:description lang=en: Learn modern C++ syntax from Python - side-by-side comparison of Python and C++ code snippets
:keywords: C++, Python, C++11, C++14, C++17, C++20, modern C++, syntax comparison, learn C++
=======================
Learn C++ from Python
=======================
.. contents:: Table of Contents
:backlinks: none
Modern C++ (C++11, C++14, C++17, C++20) has evolved to include features that make it
syntactically similar to Python, making the transition easier for Python developers.
This comprehensive guide provides side-by-side comparisons and 1-1 mappings between
Python and modern C++ code snippets, covering essential programming concepts like
variables, data structures, functions, lambdas, classes, and algorithms.
Whether you're a Python developer looking to learn C++ for performance optimization,
system programming, or expanding your programming skills, this tutorial demonstrates
how familiar Python patterns translate to modern C++ syntax. Many popular frameworks
like PyTorch, TensorFlow, and NumPy use C++ extensions for performance-critical operations,
especially in deep learning, LLM training, and CUDA GPU programming. Understanding C++
enables you to write custom extensions, optimize bottlenecks, and contribute to these
high-performance libraries.
To learn more about C++ programming, refer to this `C++ cheatsheet `_
for additional reference and best practices.
**Complete working examples:** See `cpp_from_py.cpp `_
for runnable code with integrated Google Test suite. Each function includes Doxygen comments showing the equivalent Python code.
Hello World
-----------
The traditional first program in any language. Both Python and C++ can print text to
the console, though C++ requires including the iostream library and a main function.
**Python**
.. code-block:: python
print("Hello, World!")
**C++**
.. code-block:: cpp
#include
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Variables
---------
Modern C++ supports automatic type inference with the ``auto`` keyword, making variable
declarations as concise as Python. The compiler deduces types from initialization values.
**Python**
.. code-block:: python
x = 10
y = 3.14
name = "Alice"
is_valid = True
**C++**
.. code-block:: cpp
auto x = 10;
auto y = 3.14;
auto name = "Alice";
auto is_valid = true;
Lists and Vectors
-----------------
Python lists and C++ vectors are dynamic arrays that can grow and shrink. Both support
indexing, appending elements, and querying size. C++ vectors require specifying the element
type, but modern C++ can infer it from initialization.
**Python**
.. code-block:: python
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers[0])
print(len(numbers))
**C++**
.. code-block:: cpp
#include
std::vector numbers = {1, 2, 3, 4, 5};
numbers.push_back(6);
std::cout << numbers[0] << std::endl;
std::cout << numbers.size() << std::endl;
Array Slicing and Access
-------------------------
Python supports powerful slicing syntax with negative indices and ranges. C++ doesn't have
built-in slicing, but you can use iterators or create subvectors. Negative indexing requires
manual calculation from the end.
**Python**
.. code-block:: python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[0])
print(numbers[-1])
print(numbers[2:5])
print(numbers[:3])
print(numbers[7:])
print(numbers[::2])
print(numbers[::-1])
**C++**
.. code-block:: cpp
#include
#include
std::vector numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::cout << numbers[0] << std::endl;
std::cout << numbers[numbers.size() - 1] << std::endl;
std::vector slice1(numbers.begin() + 2, numbers.begin() + 5);
std::vector slice2(numbers.begin(), numbers.begin() + 3);
std::vector slice3(numbers.begin() + 7, numbers.end());
std::vector every_second;
for (size_t i = 0; i < numbers.size(); i += 2) {
every_second.push_back(numbers[i]);
}
std::vector reversed(numbers.rbegin(), numbers.rend());
Dictionaries and Maps
---------------------
Dictionaries in Python and maps in C++ store key-value pairs. Both allow insertion, lookup,
and modification using bracket notation. C++ maps keep keys sorted, while Python dicts
maintain insertion order (Python 3.7+).
**Python**
.. code-block:: python
ages = {"Alice": 30, "Bob": 25}
ages["Charlie"] = 35
print(ages["Alice"])
**C++**
.. code-block:: cpp
#include