Variables in Python for Beginners

Every program consists of commands and data. To store data, every programming language, including Python, uses variables.

A variable is simply a name that refers to data stored in the computer’s memory. In other words, a variable is a named area of memory. To create a variable and assign it a value, use the assignment operator. Here are examples of creating a variable, or as programmers say, “declaring”:

first = "синий"
second = 23

The syntax for declaring is straightforward: write the variable name, the “=” sign, and the variable’s value. Different variables can have different data types. In our example, first is a string, and second is a number. In Python, besides strings and numbers, there are other data types. If you’re new to programming and don’t know what data types are, don’t worry, this will be explained later.

The simplest thing you can do with a variable is display its value on the screen.

first = "синий"
second = 23
print(first, second)

Try running this program to see what it displays on the screen.

Variable Names

Follow a few simple rules when choosing variable names:

  1. A variable name can only contain Latin letters, numbers, and the underscore symbol.
  2. A variable name must not contain spaces.
  3. A variable name must not start with a digit.
  4. Case sensitivity matters: var and Var are different variables.

When naming variables, remember that variable names should not match keywords and built-in functions. Their use in Python is intended for other purposes, which we will learn about later, so avoid naming variables this way.