Declaring and initializing variables
When writing programs, we continuously work with data. As we are using this data, we need a way to keep track of it. To do this, we use variables. Let's look at how this works in the following sections.
Understanding variables
To understand what a variable is, we can start with some code where we assign a value to a variable:
x = 13
Here, we have the value 13
, which is a whole number. Usually, in programming, we refer to these as integers as they can be both positive and negative. Different programming languages treat integer values differently. Most languages will specify how much memory an integer will use. Let's assume that this size is 4 bytes, which is a common size used to store an integer value. Remember that one byte is 8 bits and that each bit can be either 0
or 1
. With 4 bytes, we have 4 times 8 bits, which is 32 zeros or ones, at our disposal.
To store 13
in the computer's memory, the programming language...