Skip to Main Content
The Rules of Programming
book

The Rules of Programming

by Chris Zimmerman
December 2022
Beginner content levelBeginner
343 pages
7h 36m
English
O'Reilly Media, Inc.
Audiobook available
Content preview from The Rules of Programming

Appendix A. Reading C++ for Python Programmers

The examples in this book are all presented in C++. That’s the language I do most of my programming in, and it’s the language I’m most proficient in. That said, I’ve written a reasonable amount of Python, too—it’s the second-most used programming language at Sucker Punch. At the moment, we have about 2.8 million lines of C++ in our codebase and about 600,000 lines of Python.

If you’re a Python programmer, you don’t need to learn how to program in C++ in order to read the examples in this book. Code is code, basically—a loop is a loop, variables are variables, and functions are functions. There are some cosmetic differences, but the basic ideas in this book’s C++ examples translate pretty directly to Python, even when that translation isn’t immediately obvious!

This chapter is about explaining the translation. You won’t be able to write C++ code after working your way through this appendix—that’s at least a whole book’s worth of content—but you should be much more capable of reading it.

Types

Nothing like an example to show how straightforward reading C++ can be for a Python programmer! Here’s a simple function that calculates the sum of an array of numbers, first in Python:

def calculateSum (numbers):

    sum = 0

    for number in numbers:
        sum += number

    return sum

And then in C++:

int calculateSum(const vector<int> & numbers)
{
    int sum = 0;

    for (int number : numbers)
        sum += number;

    return sum;
}

It’s the same code, right? There’s some ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

The Programmer's Brain

The Programmer's Brain

Felienne Hermans
Design It!

Design It!

Michael Keeling
The Art of Clean Code

The Art of Clean Code

Christian Mayer

Publisher Resources

ISBN: 9781098133108Errata PageSupplemental Content