https://github.com/manitreasure1/cpp-for-pythonistas
Learn C++ the Pythonic way. A beginner-friendly C++ wrapper and course for Python learners β with familiar syntax like print(), input(), and len() to ease the transition.
https://github.com/manitreasure1/cpp-for-pythonistas
beginner-friendly cpp cpp-library custom-library data-structures educational exceptions learning-cpp pycpp python pythonic pythonista standard-template-library syntax-wrapper
Last synced: 5 months ago
JSON representation
Learn C++ the Pythonic way. A beginner-friendly C++ wrapper and course for Python learners β with familiar syntax like print(), input(), and len() to ease the transition.
- Host: GitHub
- URL: https://github.com/manitreasure1/cpp-for-pythonistas
- Owner: manitreasure1
- License: mit
- Created: 2025-07-15T08:43:45.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-07-15T11:41:16.000Z (5 months ago)
- Last Synced: 2025-07-16T01:52:49.024Z (5 months ago)
- Topics: beginner-friendly, cpp, cpp-library, custom-library, data-structures, educational, exceptions, learning-cpp, pycpp, python, pythonic, pythonista, standard-template-library, syntax-wrapper
- Language: C++
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π C++ for Pythonistas
**Write C++ like it's Python.**
This project brings Python-style syntax, functions, exceptions, and data structures into modern C++. Itβs aimed at learners transitioning from Python to C++, or anyone who prefers Pythonβs readability.
---
## π§ What This Project Provides
β
`pylist` β Python-like list with methods like `append()`, `pop()`, `insert()`, `remove()`
β
`pydict` β Python-like dictionary with `get()`, `pop()`, `keys()`, `values()`, `items()`, `clear()`, `copy()`
β
`print()` β C++ wrapper for `std::cout`
β
Python-like constants β `True`, `False`, `None`
β
Custom exceptions with ANSI-colored output β `IndexError`, `KeyError`, `ValueError`, `TypeError`, `ZeroDivisionError`
All written in clean, modern C++ with beginner readability in mind.
---
# π¦ Example: input() and range()
## πΉ `input(prompt)`
```cpp
#include "include/pycpp_io.hpp"
int main() {
std::string name = input("What is your name? ");
print("Hello", name);
return 0;
}
```
---
## πΉ `range(start, stop)` `(or range(stop))`
```cpp
#include "include/pycpp_iter.hpp" // hypothetical or future header
for (int i : range(5)) {
print(i); // 0 to 4
}
for (int i : range(2, 6)) {
print(i); // 2 to 5
}
```
## π¦ Example: `pylist`
```cpp
#include "include/pycpp_data_structures.hpp"
#include "include/pycpp_keywords.hpp"
int main() {
pylist nums = {1, 2, 3};
nums.append(4);
nums.insert(1, 10);
nums.remove(3);
nums.pop();
nums.print(); // Output: [1, 10, 2]
return 0;
}
```
---
## π¦ Example: `pydict`
```cpp
#include "include/pycpp_data_structures.hpp"
#include "include/pycpp_exceptions.hpp"
int main() {
pydict ages = {
{"Alice", 25},
{"Bob", 30}
};
print(ages.get("Bob")); // 30
print(ages.get("Eve")); // throws KeyError
ages.set("Charlie", 22);
ages.update({{"Bob", 31}});
ages.print(); // {"Alice": 25, "Bob": 31, "Charlie": 22}
for (auto key : ages.keys())
print("Key:", key);
auto age = ages.pop("Charlie"); // Removes and returns 22
ages.clear(); // Empties the dict
}
```
## β οΈ Example: `Python-style Errors`
```cpp
try {
pylist empty;
empty.pop(); // throws IndexError
} catch (const PyIndexError& e) {
std::cout << e.what() << std::endl;
}
try {
pydict d = {{"x", 1}};
d.get("y"); // throws KeyError
} catch (const PyKeyError& e) {
std::cout << e.what() << std::endl;
}
```
Output:
```
IndexError: Pop from empty pylist
KeyError: key not found in pydict: 'y'
```