Concepts / Understanding Values and Data Types

Understanding Values and Data Types

The expression 10 + 5 is evaluated before print displays its result, so the output is 15.

  • Programming

From Editor to Output

When you write Python in this course, your code begins as text in the course editor. A Python interpreter reads that text, understands the instructions, executes them, and sends results to an output area on your screen. This process lets you write and run code directly in your browser without installing Python on your computer.

Course editorPython codePython interpreterReads and executesOutput areaText, numbers, or errorsrun codedisplay result
How does code move from the course editor through the Python interpreter and become output on the screen?

The interpreter acts as both a translator and an executor. You write instructions using Python's vocabulary and rules. The interpreter reads those instructions and performs the requested actions. The result may be text, a number, or an error message, and it appears in the output area below or beside the code.

Your First Statement

The print function is a built-in Python tool for displaying text, numbers, and other values. It requires parentheses, and the value to display is placed inside those parentheses.

python
Output
Hello, World!
Worked example

Tracing a print statement

What happens when you enter print("Hello, World!") in the course editor and run it?

  1. 1
    Enter

    You type the statement into the course editor.

  2. 2
    Run

    You press the run button, or the course environment triggers execution.

  3. 3
    Read

    The Python interpreter reads the word print and recognizes it as a function call.

  4. 4
    Execute

    The interpreter executes print and passes the text Hello, World! to it.

  5. 5
    Display

    The print function displays the text in the output area.

Result

The text Hello, World! appears on the screen.

LearnerEnters statementCourse editorStores statementPython interpreterReads statementprint functionDisplays valueScreen outputHello, World!typerunexecutedisplay
What happens step by step after a learner enters a Python statement and runs it?

Values and Their Types

Definition

A type is a category of values. The types introduced here are integers, whose type name is int; floating-point numbers, whose type name is float; and strings, whose type name is str.

Strings are text values surrounded by quotation marks. They can contain letters, numbers, punctuation, and spaces treated as one piece of text. Numbers are written without quotation marks. Python treats strings and numbers differently even when their printed appearance looks similar.

python
Output
42
42
3.14
15
Written valueCategoryWhat Python treats it as
"42"StringText
42IntegerA number
3.14Floating-point numberA decimal number
10 + 5ExpressionA calculation evaluated to 15

Quotation marks distinguish text from numeric values.

The first two statements display the same visible characters, 42, but they do not represent the same kind of value. The quoted version is text. The unquoted version is a number that Python can treat as numeric data. The expression 10 + 5 is evaluated before print displays its result, so the output is 15.

Writing Versus Running

Typing code into the editor and executing code are separate actions. When you type print("Hello") and leave it in the editor, you have written an instruction, but the instruction has not yet been executed. Running the code causes the interpreter to read it and perform the requested action.

Written codeInstruction in editorRun actionExecution triggeredExecuted codeInterpreter performsinstructionOutputResult or errortriggerinterpreter readsdisplay
What changes when written Python code is run, and when does the interpreter execute it?
Best practice

Use the editor as a place to write, review, and revise instructions. Run the code when you want the interpreter to execute the current version. You can edit and run the code multiple times, including after correcting an error.

python
Output
First
Second
Third

A program can contain multiple statements. Python executes those statements sequentially, from top to bottom, so the output follows the order in which the statements were written.

Mistakes with First Statements

  • Common mistake

    Expecting code to run as soon as it is typed

    What it looks like

    Typing print("Hello") into the editor but not triggering execution

    Why it fails

    Writing code and running code are separate actions. The interpreter reads the instruction when the code is run.

    Do this instead

    Trigger execution after entering or editing the statement.

  • Common mistake

    Leaving quotation marks out of a text value

    What it looks like
    print(Hello)
    Why it fails

    Text must be surrounded by quotation marks so Python recognizes it as a string.

    Do this instead

    Write the text inside quotation marks, as in print("Hello").

  • Common mistake

    Putting quotation marks around a number unintentionally

    What it looks like
    print("42") when the intended value is the number 42
    Why it fails

    Quotation marks make 42 a string rather than a numeric value.

    Do this instead

    Use print(42) when the value should be a number.

  • Common mistake

    Forgetting the parentheses after print

    What it looks like
    print "Hello"
    Why it fails

    The print function requires parentheses around the value it receives.

    Do this instead
    Use the form print("Hello").

Try the Interpreter

EASY

In the course editor, write and run three print statements: one that displays a string, one that displays an integer, and one that displays a floating-point number. Then write a fourth statement that displays the result of a calculation. Before running the code, predict the order of the four output lines.

Hints
  • Put the text value inside quotation marks.
  • Write the integer and floating-point number without quotation marks.
  • Place the calculation inside the parentheses for print.
EASY

Write a print statement in the editor, leave it unchanged for a moment, and observe that writing alone does not produce output. Then run it. Edit the statement to display a different value and run it again. Describe what changed between the written code and each run.

Hints
  • The interpreter acts only when execution is triggered.
  • Compare the value in the editor with the value shown in the output area.

Key Takeaways

  1. 1
    Python code written in the course editor is read and executed by a Python interpreter.
  2. 2
    The print function displays values in the output area and requires parentheses.
  3. 3
    Strings are text surrounded by quotation marks, while numbers are written without quotation marks.
  4. 4
    A program's statements execute sequentially from top to bottom.
  5. 5
    Writing code and running code are separate actions, so you can edit, run, inspect, and fix your code repeatedly.

At a glance

  • The course editor is where you write Python, and the Python interpreter executes the code.
  • Running print displays the supplied value in the output area.
  • Quotation marks make a value a string; values such as 42 and 3.14 are numbers when written without quotation marks.
  • Python executes multiple statements from top to bottom.
  • Writing code does not execute it until you run the code.

Related concepts

More concepts starting with U