Fundamentals

12 min read

Variables

A variable is a named storage location that holds a value. It represents a specific memory address where a value, such as a number, text, or a more complex data structure, can be saved and modified during the execution of a program.
You can think of it as a labeled container; the label allows the program to find and use the data stored inside whenever it is needed.

Variable as a Labeled Memory Container
Variable as a Labeled Memory Container

As a general rule when naming variables in your code, you should use descriptive names that clearly indicate the purpose of the variable. For example, if you are storing a person’s age, you should use a name like “age” instead of a name like “x”.

There are different naming conventions, which are used in different programming languages and projects:

  • camelCase: firstName, lastName, age, isStudent
  • PascalCase: FirstName, LastName, Age, IsStudent
  • snake_case: first_name, last_name, age, is_student
  • kebab-case: first-name, last-name, age, is-student

Data Types

A data type is a classification that specifies what kind of value a variable can hold and what operations can be safely performed on it. In computer programming, it tells the compiler or interpreter how the programmer intends to use the data, ensuring that the system allocates the correct amount of memory and prevents logical errors, such as trying to multiply text or add a number to a file.

Data in the memory is stored as strings of ones and zeros. A data type provides a way to interpret those bytes of data. A byte is a unit of digital information that consists of 8 bits, which are binary digits that can be either 0 or 1.

Same Bits, Different Interpretations
Same Bits, Different Interpretations

Data types are generally divided into primitive and composite categories.

  • Primitive data types are the most basic building blocks built directly into a programming language, representing single values like integers, floating-point numbers, booleans, and individual characters.
  • Composite data types are constructed by combining these primitive types to create more complex structures, such as arrays, lists, and objects, which can manage larger collections of related information.

The most common primitive data types are:

  • Integers: Whole numbers, positive or negative (e.g. 42, -5, 0).
  • Floating-point numbers: Real numbers (e.g. 3.14, -0.0025, 7.0).
  • Booleans: Logical values, either true or false (e.g. true, false).
  • Characters: Single characters, such as letters, numbers, or symbols (e.g. ‘a’, ‘B’, ‘?’).

You can write strings of characters using double quotes (e.g. “Hello World”) or single quotes (e.g. ‘Hello World’).

Operations

The operations you can perform on variables depend primarily on the data type of the value the variable holds.

For numeric variables, such as integers and floating-point numbers, you can perform standard arithmetic operations including addition, subtraction, multiplication, division, and modulus to find remainders.

x = 10
y = 5

# Addition
print(x + y)  # Output: 15

# Subtraction
print(x - y)  # Output: 5

# Multiplication
print(x * y)  # Output: 50

# Division
print(x / y)  # Output: 2.0

# Modulus
print(x % y)  # Output: 0
Info

Text after # or // is ignored by the interpreter or compiler. These are called comments. They are used to explain the code or leave notes for yourself or others.

print means the result is displayed on the screen, in the console or output window.
print is a function, meaning it’s a reusable block of code that performs a specific task. You’ll learn more about functions in a future tutorial.

Modulus is the operator used to find the remainder of a division. It usually is noted with % symbol in the majority of programming languages. In pseudocode, you can also write mod.

Raising a number to the power of another number is noted with ** in Python, while in other programming languages, like C++ or Java, you can use a function like pow.
In pseudocode, I recommend using ^ or pow. For example:

print(10 ^ 5) # Output: 100000
print(pow(10, 5))
print(10 ** 5)

You can also use assignment operations to update the value of a variable, either by replacing it entirely or by modifying it in place using shorthand operators like adding a number directly to the current value.
In most programming languages, = is used for assignment and not for equality comparison.

x = 10
print(x) # Output: 10

For variables holding text, known as strings, you can perform operations like concatenation to join two pieces of text together, or extraction to isolate specific characters or segments.
In pseudocode, you can use + for concatenation and [start:end] or substring(start, end) for extraction. For example:

"Hello" + "World" # Output: "HelloWorld"
"Hello"[0:2] # Output: "He"
"Hello".substring(0, 2) # Output: "He"

Additionally, variables of almost any data type can be used in comparison operations, which evaluate relationships like equality, inequality, greater than, or less than to produce a trueor false result.

result = 10 == 5   # Equal to
print(result)  # Output: False

result = 10 != 5   # Not equal to
print(result)  # Output: True

result = 10 > 5    # Greater than
print(result)  # Output: True

result = 10 < 5    # Less than
print(result)  # Output: False

result = 10 >= 5   # Greater than or equal to
print(result)  # Output: True

result = 10 <= 5   # Less than or equal to
print(result)  # Output: False

Logical operations, such as AND, OR, and NOT, are then used to combine or invert these boolean results, allowing programs to make decisions based on the state of multiple variables.

In pseudocode, you can use and, or, and not, or &&, ||, and !.

result = True and False # False
result = True or False  # True
result = not True       # False
result = True && False  # False
result = True || False  # True
result = !True          # False

Review

### What is the primary function of a variable in programming? > Think of what a labeled container does for its contents. 1. [ ] To execute a specific reusable block of code 1. [ ] To permanently lock a memory address from changes 1. [x] To serve as a named storage location holding a value 1. [ ] To translate text directly into binary strings ### Which of the following illustrates the camelCase naming convention? > This convention capitalizes subsequent words but starts with a lowercase letter. 1. [ ] total_count 1. [ ] TotalCount 1. [x] totalCount 1. [ ] total-count ### What determines how a sequence of ones and zeros in memory is interpreted? > It serves as a classification specifying what kind of value is stored. 1. [ ] The naming convention 1. [ ] The assignment operator 1. [x] The data type 1. [ ] The variable name ### How many bits make up a standard byte of digital information? > It is the fixed number of binary digits that constitute a single byte. 1. [ ] 4 bits 1. [x] 8 bits 1. [ ] 16 bits 1. [ ] 32 bits ### Which of the following is considered a composite data type rather than a primitive one? > All the others are primitive data types. 1. [ ] Floating-point number 1. [ ] Boolean 1. [ ] Character 1. [x] Array ### What does the modulus operator calculate? > This operation is used to find what is left over after division. 1. [ ] The result of raising a number to a power 1. [x] The remainder of a division 1. [ ] The absolute value of an integer 1. [ ] The quotient rounded to the nearest whole number ### What is the function of the symbol "=" in most programming languages? > It is used to store or update a value in a variable, not to check if two things are equal. 1. [x] Value assignment 1. [ ] Equality comparison 1. [ ] Logical inversion 1. [ ] String concatenation ### Which operation is used to join two strings of text together? > It merges "Hello" and "World" into "HelloWorld". 1. [ ] Extraction 1. [x] Concatenation 1. [ ] Modulus 1. [ ] Compiling ### What is the result of evaluating the comparison expression 7 != 5? > Determine if 7 is unequal to 5. 1. [x] True 1. [ ] False 1. [ ] 5 1. [ ] 15 ### Which logical operator is used to invert a boolean value? > This operator turns True into False. 1. [ ] and 1. [ ] or 1. [x] not 1. [ ] mod