An open API service indexing awesome lists of open source software.

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.

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'
```