Variables in Python & C++
Let's compare this C++ statement:
int x = 3;
with the equivalent to the Python statement:
x = 3
Now, have a think about this question...
Python and C++ Similarities
As in Python, every C++ variable has a type, a name, a value, and a location in memory.
Looking at our declarations again,
C++ | Python |
---|---|
int x = 3 ; |
x = 3 |
we can see some important similarities:
- In both languages, we, as the programmers, explicitly choose a name for our variable: in this case,
x
. - In both languages, we, as the programmers, explicitly choose a value for our variable: in this case,
3
. - In both languages, we use the equal sign (
=
) to set the initial value of our variable.
There are some important differences between the two languages, though.
Python and C++ Differences
Differences Related to Variable Types
Both Python and C++ have types (e.g., integers, floating point numbers, and strings are types that exist in both languages), but, unlike C++, Python variables are generally allowed to hold values of any type, and we're allowed to change not just the value of a Python variable, but the type as well.
So Python is completely fine with code like
x = 3
x = Cow()
x = "Now I'm a string with the word Cow in it!"
We don't have to tell Python what type we're going to store in a variable, it's implicit (i.e., determined without us saying).
But it's also the case that in Python code, if we make a mistake and store a string in a variable that is supposed to hold an integer, Python won't detect our error until it actually causes a problem, which might not be for a while.
In C++, by contrast, every variable has a specific type that is known when we declare it and that type can't change. So if we declare int x
, we can't suddenly decide to store a string in x
, because x
is an integer variable. Enforcing this rule can catch bugs, so while unchangeable types may seem like a limitation, they have benefits. It's also a reason why C++ runs faster than Python—when you say x+y
in C++, if x
and y
are both declared int
s, C++ knows what it needs to do. In Python, x+y
might be addition if the variables hold integers, but it could be trying to join two strings, or it could just be nonsense if, say, x
and y
hold Cow
s, so Python needs to do some figuring out before it can get down to work.
In our C++ example, the type was explicit—we included the type (int
) as part of the variable declaration. (C++ actually allows implicit determination of the type via the auto
keyword, which we'll see later, but for now we'll give types explicitly.)
Differences Related to Variable Locations
In both languages, the memory location where our variable will be stored is implicit. In other words, we don't get to pick where the variable goes in memory; we just trust that it will end up being stored as a bunch of ones and zeros somewhere.
In C++, though, we'll discover later this semester that the location is easily accessible to us as programmers. Python, by contrast, tries really hard to hide a variable's memory location from us!
This access to where our variables are stored gives us a lot of power—and a lot of opportunities to introduce new kinds of bugs in our code!
Differences Related to Variable Values
In Python, we're required to give an initial (starting) value for a variable when we declare it.
While it's good practice to give a starting value for a C++ variable, and we'll (almost always) require you to explicitly define the starting value of your variables ("initializing" them), it's also possible to let C++ implicitly choose the starting value.
If we write
Cow bessie;
string s;
int x;
we've declared three new variables and specified what type each holds, but we have not said what value they should hold—we've left it up to C++. For some types (the ones we'll make ourselves, like Cow
in this example, and some built-in C++ classes like string
s), there may be a sensible default value—for example, the default string
is an empty string.
However, for C++'s most basic built-in types (e.g., int
and double
), the variable will hold an indeterminate value—the initial value it holds could be different on different computers, or even between different runs of the same program on the same computer! So x
could start off holding 42
, -12345678
or perhaps 0
: We don't know. In fact, for some types, an indeterminate value might be invalid and cause problems if we try to use it (e.g., our program might crash in a weird way).
Leaving a variable with an indeterminate value because we didn't initialize it can cause bugs, often subtle ones. Imagine that we have a variable that will hold a running total and we forget to initialize it. If the variable's value started out with -298424932
, we'd probably realize quickly that something was wrong, but if that indeterminate value happened to be zero, perhaps everything would seem to work, but suppose that later, when the code was deployed to a client, the different environment causes its indeterminate value to be 3
. Oops, their results are subtly wrong and no one realizes!
Thankfully, our compilers usually give us warnings about these sorts of issues.
Summary of Variables in Python and C++
This table summarizes the similarities (and differences) between Python and C++:
Variable Attribute | Python Behavior | C++ Behavior |
---|---|---|
Name | explicit | explicit |
Type | implicit (any; can change) | explicit (fixed; can't change) |
Value | explicit | explicit (mostly) |
Location in Memory | implicit, hidden | implicit, accessible |
(When logged in, completion status appears here.)