Python skills carry over more than you might expect
Yes, you can move from Python to C++, and the skills you already have help. Loops, functions, conditionals and the habit of breaking a problem into small steps work the same way. What changes is how much C++ asks you to state up front: the type of each variable, how your code is built before it runs, and how memory is set aside and released.
The first few weeks usually feel slower than Python. Most of the extra effort goes into a few ideas, and this guide covers them in a practical order.
What carries over
- Control flow.
if,while,for,breakandcontinuebehave as you expect. C++ uses braces where Python uses indentation. - Functions. You still pass arguments in and return values out. The difference is that each parameter and return value has a declared type.
- Working with data. Strings, collections of items and key-value lookups all exist in the C++ standard library. They have different names and rules, covered below.
- Debugging habits. Printing values, shrinking the problem and reading error messages carefully all help in C++ too.
A first program, side by side
Start with a program that asks for a name and prints a greeting.
Python:
name = input("What is your name? ")
print(f"Hello, {name}!")
C++:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "What is your name? ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
}
The C++ version needs two include lines, a main function and a type for name. std::getline reads a whole line, including spaces. Python’s print adds the line break for you, while in C++ you write \n yourself.
To compile and run it with GCC or Clang:
g++ -std=c++17 -Wall -Wextra -o hello hello.cpp
./hello
The -Wall -Wextra flags turn on the compiler’s warnings. Keep them on while you learn, since they point out many mistakes early. Visual Studio and other IDEs have equivalent settings.
What is new
Types are declared
In Python, a name can refer to a number one moment and a string the next. In C++, a variable keeps its type:
int count = 3;
count = "three"; // compile error: text cannot be stored in an int
When the type is obvious from the value on the right, auto lets the compiler work it out. In auto total = 0;, total is an int.
Code is compiled before it runs
Python runs code as it reaches it, so a bug in a branch that rarely runs can stay hidden until that branch runs. A C++ compiler reads the whole program first. That catches many mistakes early, such as a misspelled name or a missing semicolon, but it cannot catch every bug.
Braces, semicolons and includes
- Every statement ends with a semicolon.
- Blocks use
{and}. Indentation helps readers, but the compiler ignores it. - A class definition ends with
};. Forgetting that last semicolon is a common beginner error. - Library features come from headers, so you write
#include <vector>before you usestd::vector.
Many tutorials use using namespace std; to save typing. It is fine in small practice programs, but many style guides avoid it, especially in header files. The examples here write std:: in full.
Integer division works differently
In Python 3, 7 / 2 gives 3.5. In C++, dividing two int values gives an int, so 7 / 2 is 3. Use a floating-point value to get a fraction:
#include <iostream>
int main() {
std::cout << 7 / 2 << "\n"; // prints 3
std::cout << 7.0 / 2 << "\n"; // prints 3.5
}
Pointers and references
Python hides memory addresses. C++ lets you work with them. A pointer stores the address of a value, and a reference is another name for an existing variable:
#include <iostream>
int main() {
int x = 5;
int* p = &x; // p holds the address of x
*p = 10; // change x through the pointer
int& r = x; // r is another name for x
r += 1;
std::cout << x << "\n"; // prints 11
}
The & operator gives an address, and * follows a pointer to the value it points to. Pointers can be confusing at first. You will use references far more often in everyday code, and you can avoid raw pointers in most of your own programs.
Python structures and their C++ equivalents
| Python | C++ standard library |
|---|---|
str |
std::string |
list |
std::vector |
dict |
std::map or std::unordered_map |
set |
std::set or std::unordered_set |
| class | class with a constructor |
A list becomes a std::vector:
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores = {90, 75, 88};
scores.push_back(95);
for (int score : scores) {
std::cout << score << "\n";
}
}
The Python version uses scores.append(95) and for score in scores:. A std::vector<int> holds only int values, so every element has the same type.
A dict becomes a std::map or a std::unordered_map. A std::map keeps its keys in sorted order, and a std::unordered_map has no defined order. Neither matches Python’s dict exactly, but both support lookup by key:
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> ages = {{"Ann", 31}};
ages["Bob"] = 27;
std::cout << ages["Bob"] << "\n"; // prints 27
}
Classes and objects
A Python class and a C++ class share the same basic idea. The C++ version asks you to say more: which members are public or private, and which functions change the object.
Python:
class Dog:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} says woof")
C++:
#include <iostream>
#include <string>
class Dog {
public:
explicit Dog(const std::string& name) : name_(name) {}
void speak() const {
std::cout << name_ << " says woof\n";
}
private:
std::string name_;
};
int main() {
Dog rex("Rex");
rex.speak();
}
The constructor has the same name as the class and sets up the member variable. The const after speak() promises that the function does not change the object. The private: section keeps name_ hidden from code outside the class, which is a useful habit from the start.
Memory: prefer the standard library
Python frees most objects automatically once nothing refers to them. In C++, memory you allocate yourself stays allocated until you free it, and forgetting to do so is a leak. Modern C++ mostly avoids doing this by hand:
- Use
std::stringandstd::vector, which manage their own memory. - Use
std::unique_ptrwhen one owner should control a heap object. - Ordinary variables are cleaned up automatically when they go out of scope.
#include <iostream>
#include <memory>
int main() {
auto value = std::make_unique<int>(42); // freed automatically
std::cout << *value << "\n";
}
A practical order to learn it
- Set up and compile. Install a compiler (GCC, Clang or MSVC) and an editor. Compile a hello-world program with warnings on.
- Types, variables and operators. Learn
int,double,bool,char,std::string, and how integer division works. - Control flow and functions. Loops,
ifstatements, parameters, return types, and passing values versus references. - The standard library.
std::string,std::vectorandstd::map, and their most common functions. - Classes. Constructors, member variables,
constmember functions, and how objects are copied. - Memory. Pointers and references, then
std::unique_ptr, and why ordinary variables clean up after themselves. - Small projects. A to-do list, a number-guessing game, a bank account class, or a program that reads a text file. Each one combines several of the ideas above.
Mistakes Python learners often make
- Single quotes for text. In C++,
'a'is one character and"abc"is text. - Comparing C-style strings with
==. Forstd::string,==compares contents, which is usually what you want. For older C-style strings, it compares addresses. - Mixing signed and unsigned numbers in loops.
v.size()returns an unsigned type, sofor (int i = 0; i < v.size(); ++i)often triggers a warning. Usestd::size_tfor the index instead. - Reading past the end of a vector with
[]. This is undefined behavior: the program may print garbage, crash, or seem to work.v.at(i)checks the index and throwsstd::out_of_rangewhen it is wrong. - Forgetting that
std::coutdoes not add a new line. Write"\n"at the end of the output. - Reading only the last compiler error. Start with the first error. Later messages often follow from it.
How long it takes
There is no fixed timeline. It depends on how much time you can give, how comfortable you already are with Python, and what you want to build. Basic syntax tends to come quickly. Memory management, pointers and templates take longer, and you will probably revisit them more than once.
Is C++ hard to learn? It is usually harder than Python at first, mainly because of types, compiling and memory. The difficulty is concentrated in a few topics, and the rest builds on the same ideas. Learning C first is not required for C++, although C++ includes most of C’s pointer and memory ideas. If you are still deciding between the two, is C still worth learning in 2026? is a useful question to ask, and is C harder to learn than Python? covers the comparison from the C side.
Where to go next
Keep a reference open while you practice. cppreference documents the standard library with short examples, and it is a good place to check what a function does before you use it. Pair that with a tutorial that follows the order above, and write small programs regularly.
Python gave you good habits. C++ asks you to be explicit about them, and that becomes useful once you get used to it.