Programming languages, syntax, source code, and variables

~8 min

In this module, you will look at real code for the first time.

But be clear about the goal. You are not learning how to build software or write programs from scratch. You are learning how to read code well enough to recognize its basic building blocks and follow what a simple program is doing.

That skill is more reachable than it may sound because source code is written as text, follows consistent rules, and is designed to be understood by people as well as processed by computers.

What is a programming language?

In Module 1, you learned that software contains instructions telling a computer what to do, and in Module 2, you learned that the CPU ultimately executes machine code represented through binary patterns.

But people generally do not write those binary instructions by hand. They use programming languages instead.

A Concept · lights on your mapprogramming languageA structured way of writing instructions for a computer, precise enough to be translated into something the machine can execute and readable enough for people to create, inspect, and maintain. Python, JavaScript, and Java are programming languages. is a structured way of writing instructions for a computer: precise enough to be translated into something the machine can execute, but readable enough for people to create, inspect, and maintain.

Python, JavaScript, and Java are examples of programming languages, and while they can all express instructions, they use different words, symbols, and conventions.

You will explore the broader landscape of programming languages at the end of this module. For now, the examples will use Python because its basic syntax is relatively easy to read.

Syntax: the grammar of code

Every programming language has rules governing how its instructions must be written. These rules are called Concept · lights on your mapsyntaxA language’s grammar rules: which words and symbols have meaning, where punctuation belongs, and how the pieces of an instruction must be arranged. Code that violates syntax may fail to run at all; that failure is called a syntax error..

Syntax determines which words and symbols have meaning, where punctuation belongs, and how different pieces of an instruction must be arranged.

Human readers can often understand a sentence even when it contains a missing comma or misspelled word. But a computer cannot rely on context and judgment in the same way, so if code violates the language’s syntax rules, the computer may be unable to interpret it.

This kind of problem is called a syntax error.

A missing quotation mark or closing parenthesis, for example, could prevent a program from running. But code can also follow the syntax perfectly and still produce the wrong result, which is a different problem. The instructions are valid, but their logic is incorrect.

The machine follows the instructions it is given, and it does not automatically know what the person intended.

What is source code?

The text written by a programmer is called Concept · lights on your mapsource codeThe human-readable text of a program, written by programmers and stored in plain-text files. Other tools may translate or package it before the CPU runs it, but the source code is where engineers read and change a program’s behavior..

Source code is usually stored in plain-text files like the ones introduced in Module 2. A Python source-code file commonly ends in .py, while a JavaScript file commonly ends in .js.

The source code is the human-readable form of the program. Other tools may translate, package, or prepare it before the CPU runs it, but the source code is where engineers normally read and change the program’s behavior.

Now, consider these four lines of real Python:

Your first source codePython
1cart_items = 2
Associate the name cart_items with the value 2.
2cart_items = cart_items + 1
Evaluate the right side first: 2 + 1 is 3. Then store 3 under the name cart_items.
3cart_items = cart_items + 1
The same process again: 3 + 1 is 4. The variable is updated.
4print(cart_items)
print displays output. The program shows: 4

You do not need to know how to write this code. The goal is to understand what it says.

Variables: names for values

The name cart_items is a Concept · lights on your mapvariableA name that a program associates with a value. The program can use the name to retrieve the value or associate the name with a new one. Variables are how a program remembers information while it runs, a small-scale example of Module 1’s state..

A variable is a name that a program associates with a value, and the program can later use that name to retrieve the value or associate the name with a new one.

A common beginner metaphor is to think of a variable as a labeled container:

  • The label is cart_items.
  • The value inside begins as 2.

The first line says: cart_items = 2

The single equals sign tells Python to associate the name cart_items with the value 2.

Variables allow a program to remember information while it is running, which makes them a small-scale example of the state you learned about in Module 1.

A shopping application might use variables to temporarily track information such as the number of items in a cart, the current price, or whether a discount has been applied.

Assignment is not the same as mathematical equality

Now look at the second line: cart_items = cart_items + 1

This may look impossible if you read it as a mathematical equation, since a number cannot equal itself plus one.

In code, however, the equals sign often means assignment, not mathematical equality.

The computer first evaluates the expression on the right: cart_items + 1

At this moment, cart_items contains 2, so the right side produces 3.

The computer then associates the name on the left, cart_items, with that new value. The variable now contains 3.

The third line repeats the process: cart_items = cart_items + 1

The current value is 3, so adding one produces 4. The variable is updated again.

A useful way to read an assignment statement is: calculate the right side, then store the result under the name on the left.

Reading the program in order

The final line is: print(cart_items)

In Python, print tells the program to display something as output. Because cart_items now contains 4, the program displays: 4

The complete program can therefore be read as:

  1. Start with two items in the cart.
  2. Add one item.
  3. Add another item.
  4. Display the current number of items.

Simple programs like this are often read from top to bottom, one instruction at a time.

Larger programs are not always executed in such a straightforward order. They may call other sections of code, wait for a user action, or perform work in the background. Still, reading a simple sequence line by line is the right starting point.

What is a script?

A relatively small program written to perform a particular task is often called a Recognition — just know it existsscriptAn informal word for a relatively small program written to perform one task: “I’ll script that.” The boundary between a script and a larger program is not exact; the term mainly communicates that the software is focused and lightweight..

A script might rename a group of files, organize data, send a recurring report, or automate a repetitive task, and when an engineer says, “I’ll script that,” they usually mean they will write a small program that performs the work automatically.

The boundary between a script and a larger program is not exact, and the term mainly communicates that the software is relatively focused and lightweight.

The mental model to remember

A programming language is a structured way of writing instructions that people can understand and computers can execute after the appropriate translation.

Syntax is the grammar of a programming language. Code that breaks the language’s syntax rules may not run.

Source code is the human-readable text written by programmers and usually stored in plain-text files.

A variable is a name associated with a value that the program can use or update.

In many programming languages, a single equals sign performs assignment. The program calculates the right side and stores the result using the name on the left.

You should now be able to look at a few lines of simple source code, identify a variable, and follow how its value changes from one instruction to the next.

Check — then the lesson continues

Read this line of Python and decide what it tells the computer to do: score = score + 10

▼ answer the check to continue ▼