Back to Blog

The Building Blocks of Code: Understanding Programming Variables, Data Types, and Operators

Posted by Alvee Akand on March 15, 2024
Understanding Programming Variables, Data Types, and Operators

Most software engineers end up using multiple programming languages throughout their careers. The great thing about programming languages is that certain core principles or concepts translate easily across languages. Once you learn and understand the building blocks of code, you’ll be able to pick up any technology on your own. Programming variables, data types, and operators are three of the most common building blocks across programming languages. In this post, we will be briefly exploring each of these fundamental concepts. 

I’ll be using Python examples since concrete examples are more illustrative. But the concepts will easily translate to other languages.

Understanding Data Types in Programming

A computer does not understand information like humans do. For example, when we look at a text or string like “Hi! My name is Al!” or a number like “10” we can distinguish between them easily. We understand that strings and numbers have different properties and are used in various contexts.

The computer cannot distinguish between different types of information automatically. There are unique ways to represent values so that the computer can differentiate them. Each unique type of value is assigned a “data type” to classify or organize them, allowing us to know what contexts a certain value can be used.

We will look at the most common data types used in Python:

  • str: Represents text or string values
  • int: Represents integer number values (whole numbers like 1, 5)
  • float: Represents floating-point numbers (decimal numbers like 1.556, 3.14)
  • bool: Represents the idea of correctness, (i.e., whether something is true or false)

String Values

We use the values of the str data type to represent textual information to the computer. Quotation marks are usually used to denote text as “string” in programming languages.

Here are some examples in Python:

Code for displaying various string values in the console using Python

Here’s the output of the above code:

Output of the code for displaying various string values in the console using Python

Notice a few important things:

  • The text is displayed exactly as it is written (i.e., the string values are case-sensitive)
  • Numbers wrapped in quotation marks are treated as strings
  • Single and double quotations can be used together. The opening and closing quotation marks must be the same. For example, we can’t start a string with a double quotation mark (“) and close it with a single quotation mark (‘).

Numbers

We can represent numbers by writing them without the quotations.

Code for displaying various numbers in the console in Python

A few notes on the code:

  • Numbers written without quotations are interpreted as numerical values
  • A whole number (both positive and negative) is categorized as an “integer”
  • A decimal number (both positive and negative) is categorized as “float”

Bool or Boolean 

Human languages have the concept of correctness. For example, the sentence “elephants can fly” is incorrect or false. Similarly, computers also understand this concept. Programming languages provide a boolean data type (bool in Python) to express this concept.

In Python, the following values represent this concept:

  • True: indicates correctness in Python
  • False: indicates incorrectness in Python

Basic Operators in Programming

In mathematics, we can perform calculations using operators like +, -, / and so on. Programming languages also provide operators to perform calculations using values. Here’s an example of addition:

Code for displaying the plus operator usage in Python

Output:

Code output for the code displaying the plus operator usage in Python

Operators are usually used within expressions to get desired results.

What is an Expression?

An “expression” in Python is any code that evaluates to a value. A single value is also an expression. Here are a few examples:

  • 1 produces 1
  • “Hello!” produces “Hello!”
  • 5 * 2 produces 10
  • num > 100 produces either True or False

An expression can contain multiple expressions within it. Let’s take a look at a more complex expression:

An arithmetic expression example in Python

The above line of code has five expressions:

  • 5 (produces 5)
  • 2 (produces 2)
  • 3 (produces 3)
  • 5 + 2 (produces 7)
  • (5 + 2) + 3 (produces 10)

Here’s the expression used with the print function in Python:

Displaying output of the arithmetic expression example in Python

The computer evaluates the expression by simplifying it incrementally. Here are the intermediate steps that are hidden from us:

A breakdown of an expression into its final return value

Expressions in parentheses are evaluated first.

Variables in Coding

Variables are a way to make the computer remember values. A variable is a label to which a value can be assigned.

Variables allow programmers to give meaning to values and use those values throughout the program easily. For example, the number 238,900 doesn’t mean anything but if we label it as “the distance to the Moon from the Earth” it’s easier to reference. We don’t necessarily have to remember the exact value as we can look it up using the phrase “distance to the moon from the Earth.”

Here’s how we would use a variable in Python:

Code for assigning and displaying a variable in Python

We can use _ (underscore) to make big numbers easier to read in Python. Notice that we’re also using the _ to separate words in the variable name.

Output:

Code output for the code assigning and displaying a variable in Python

Variable Declaration and Assignment Walkthrough

Let’s look at how to create a variable again:

Code for declaring a variable in Python

There are three parts to this line:

  • cat_name is the variable name
  • = is an assignment operator
  • ‘Fifi’ is an expression that produces the value Fifi of type str.

So this is the general structure:

Shows the general variable declaration and assignment structure
  • The right side of the = operator is evaluated first
  • The value produced by the expression evaluation is then assigned to the variable name or label

The line cat_name = ‘Fifi’ is usually verbalized as “declare a variable called cat_name and assign the value ‘Fifi’ to it”

Learn About Programming Variables and More at Flatiron

If you’d like to learn more about foundational concepts such as fundamental data types, programming variables, operators, and functions, check out Flatiron School’s Software Engineering Bootcamp. You’ll find info on upcoming course start dates, career paths in the field, student success stories, and an FAQ.

About Alvee Akand

Alvee Akand is the senior curriculum manager for software engineering at Flatiron School. He has a degree in electrical engineering and loves building communities.

More articles by Alvee Akand