Value

New:

  • value
  • string
  • integer

Before we proceed to complex structures of code we should introduce the notion of value: the smallest, indivisible piece of information the interpreter creates in its memory and marks as being of some type. The interpreter does not display values in the output unless the source code contains a special instruction to do so.

Okay, so, if the interpreter doesn't display values without a special instruction, then why when we put text in quotes in the previous lesson — "Pasta Carbonara" — the interpreter didn't throw some sort of error? Or didn't at least show us it accepted the value as something Python understands? It seems it'd be nice if the work done by the interpreter without any visual feedback wasn't that silent, and we could see each step the interpreter performs. Soon you'll realize that it would only make sense for educational purposes. Mostly because this isn't the only type of work the interpreter does in complete silence. It behaves similarly in the majority of cases, so you'll be constantly dealing with its hidden work.

Go back to "Pasta Carbonara". What silent work was performed? At the very least, the interpreter recognized a valid fragment of code, treated it as a single value (not two words with a space in between, not a sentence, nor a meaningful phrase), and placed it into memory. Additionally, it marked it as a value of specific type: string, which is a formal term for the notion of text in programming. The syntax rule for strings is to write any series of characters within two quotation marks. All that recognition, storage and marking happens when you press .

String is only one of several1 types recognizable by Python. If you write a number and push :

241

You'll see empty output again. In this case the interpreter recognized the unit of code and marked it as another type — integer: a whole (non-fractional) number with the value 241. The syntax rule for integers is to write a number without quotation marks (though, you can put + or in front to mark it as positive or negative). There are other types of values in Python: lists, facts, sets and more. Each type has its own unique syntax rule so that the interpreter can distinguish between them.

Starting next lesson, you'll discover other ways of expressing values in programs, ways that require the interpreter to perform additional work of computing a value. It could be a simple arithmetic addition or arbitrary complex computation involving many lines of code or even whole other programs. Regardless of way of expression, values may appear in code in various contexts and may be a part of different units of code.

From this moment it's important to distinguish between text you write and units of code that can be recognized and treated as valid parts of a program, and may be shown to you in output. All the visible code written in any editor (including repl.it) before the program runs always remains as text styled by rules of the programming language. So when we use the word value in the context of visible code, we always imply a value from the interpreter's point of view: a valid, indivisible chunk of data in the memory of a running program.


Summary:

  1. A value is the smallest, indivisible piece of information the interpreter creates in its memory and marks as being of some type.
  2. String is the name of the type for text. "Pasta", "c3po" are string values.
  3. Integer is the name of the type for whole numbers. 42, 0, -102 are integer values.
1

There are other types of values in Python — lists, facts, sets and more. Each type has its own unique syntax rule, so that the interpreter can distinguish between them.