Understanding Values and Data Types
Python code you write in the course editor is read by a Python interpreter, which executes your instructions and displays output on your screen.
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.
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.
Hello, World!Tracing a print statement
What happens when you enter print("Hello, World!") in the course editor and run it?
Enter: You type the statement into the course editor.
Run: You press the run button, or the course environment triggers execution.
Read: The Python interpreter reads the word print and recognizes it as a function call.
Execute: The interpreter executes print and passes the text Hello, World! to it.
Display: The print function displays the text in the output area.
The text Hello, World! appears on the screen.
Values and Their Types
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.
42
42
3.14
15| Written value | Category | What Python treats it as |
|---|---|---|
| "42" | String | Text |
| 42 | Integer | A number |
| 3.14 | Floating-point number | A decimal number |
| 10 + 5 | Expression | A 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.
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.
First
Second
ThirdA 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
Expecting code to run as soon as it is typed
Writing code and running code are separate actions. The interpreter reads the instruction when the code is run.
Fix:
Trigger execution after entering or editing the statement.Leaving quotation marks out of a text value
Text must be surrounded by quotation marks so Python recognizes it as a string.
Fix:
Write the text inside quotation marks, as in print("Hello").Putting quotation marks around a number unintentionally
Quotation marks make 42 a string rather than a numeric value.
Fix:
Use print(42) when the value should be a number.Forgetting the parentheses after print
The print function requires parentheses around the value it receives.
Fix:
Use the form print("Hello").
Try the Interpreter
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.
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
- Python code written in the course editor is read and executed by a Python interpreter.
- The print function displays values in the output area and requires parentheses.
- Strings are text surrounded by quotation marks, while numbers are written without quotation marks.
- A program's statements execute sequentially from top to bottom.
- Writing code and running code are separate actions, so you can edit, run, inspect, and fix your code repeatedly.
Key Takeaways
- 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.