Your First Python Program
Python combines performance and features in a way that makes programming both fun and easy
A Language Built for Both Learning and Work
Python is a programming language: a formal set of instructions that a computer can understand and execute. What makes Python especially approachable is its balance. It prioritizes readability and simplicity without giving up the power needed for useful programming tasks.
Python combines three important characteristics. Its performance helps programs do useful work in reasonable time. Its extensive features, including a large standard library of pre-written code for common tasks, give programmers tools for diverse problems. Its clear syntax makes it easier to learn and express ideas. Together, these characteristics reduce the amount of boilerplate code you need to write while keeping the language capable enough for sophisticated applications.
| Python characteristic | What it contributes |
|---|---|
| Performance | Programs can do useful work in reasonable time |
| Extensive features | The language provides tools for diverse tasks |
| Ease of use | Clear syntax makes learning and expressing ideas more approachable |
Python is not presented as a toy language. The same approachable language can support beginners learning to program and professionals building production systems.
From Browser to Python Output
This course runs Python code in your browser using a cloud-based interpreter. Your code does not run directly on your computer. When you write a line of code and press Run, the code is sent to a server, the Python interpreter on that server reads and executes it, and the result is sent back to your browser.
Writing Versus Running Code
Writing code means creating instructions in a form the Python interpreter can understand. Running code means asking the interpreter to read and execute those instructions. A line can exist in the editor without having produced any output yet; execution begins when you run it.
| Activity | What happens |
|---|---|
| Write | You place a Python instruction in the editor |
| Run | The interpreter reads and executes the instruction |
| Observe | The result is displayed in the course environment |
Your First Print Statement
A Python statement is a complete instruction. The simplest statement in this lesson is a call to the print function. The print function displays what you give it on the screen.
The word print is the function name. A function is a reusable instruction. The parentheses indicate that you are calling the function, rather than merely mentioning its name. The text inside the parentheses is the input, called an argument. When Python executes this call, it displays the text Hello, Python.
Hello, PythonWhat Happens During Execution
Tracing print("Hello, Python")
What does Python do when this statement is run?
Read: The Python interpreter reads the statement and recognizes print as a function.
Identify the input: The text Hello, Python inside the parentheses is the function's argument.
Execute: Python calls the print function with that text as input.
Display: The text Hello, Python appears on the screen.
The statement produces the output Hello, Python.
Two Ways to Run Python
Python code can run line by line in the REPL or as a complete script file. The REPL is useful for experimenting and testing small ideas quickly. A script is a saved file containing multiple lines of Python code that you run as a complete unit.
| REPL | Script file |
|---|---|
| Runs code line by line | Runs a complete saved file |
| Useful for quick experiments | Useful for longer programs |
| You receive a result after each submitted line | Multiple lines can be organized and executed together |
In this first lesson, the REPL gives you a direct way to run a statement and see its result. As the course progresses, you will also write longer scripts and run them as complete files.
Names and Stored Values
After a print statement, an important next idea is assignment. A variable is a name that holds a value. The equals sign assigns a value to a variable name so Python can remember it and allow you to use the name later.
Alice
25In the first line, the text Alice is assigned to the variable name. In the second line, the number 25 is assigned to age. When Python reaches print(name), it looks up the value associated with name and displays it. The same process happens for age. Assignment lets you use a readable name instead of repeatedly typing the same value.
Mistakes Beginners Make
Expecting output immediately after writing code
Writing places the instruction in the editor; it does not execute the instruction.
Fix:
Run the statement, then inspect the output returned by the course environment.Leaving out the parentheses in a function call
A function call follows the pattern of a function name followed by parentheses containing its arguments.
Fix:
Write print("Hello, Python") with the argument inside parentheses.Thinking the code runs on your own computer
The course sends your code to a cloud-based interpreter on a server.
Fix:
Use the browser environment provided by the course; no installation is needed for these lessons.Treating the REPL and a script as different languages
The REPL and script files use the same Python language.
Fix:
Remember that the difference is how code is organized and executed: line by line in the REPL or as a complete file in a script.
Your First Practice Run
Run this statement in the course environment, then change the text inside the parentheses and run it again: print("I am learning Python")
Hints
- The function name is print.
- Keep the text argument inside the parentheses.
- Run the statement after changing the message and compare the new output with the old output.
Write two assignment statements and one print statement. Store a name in a variable, store a number in another variable, and print one of the variables.
Hints
- Assignment uses the equals sign.
- A variable is a name associated with a value.
- Use the print function to display a stored value.
What do you think happens?
Before running the statement, predict what will appear in the output: print("Python is readable")
Reveal answer
Answer: Python is readable
The interpreter recognizes print as a function and displays the text supplied as its argument.
The Foundation for Later Programs
You have now followed the basic path of a Python program: write an instruction, run it through the interpreter, and inspect the output. You also saw the structure of a function call and learned that variables give names to values. These ideas will reappear in longer scripts and more advanced programs.
- Python combines performance, extensive features, and ease of use.
- Readable syntax and a large standard library reduce the amount of boilerplate code you need to write.
- This course runs Python in the cloud through a browser-based environment, so installation is not required.
- The REPL runs code line by line, while a script runs a saved collection of lines as a complete file.
- A print call uses a function name, parentheses, and an argument to display output.
- Assignment connects a variable name with a value that Python can retrieve later.
Key Takeaways
- Python is designed to be readable and approachable while remaining capable of useful and sophisticated programming.
- In this course, code travels from the browser to a cloud-based Python interpreter and the result returns to the browser.
- Writing a statement and running a statement are separate actions.
- The simplest Python instruction is a call to print, using a function name, parentheses, and an argument.
- The REPL supports line-by-line experimentation, while scripts organize multiple lines for complete execution.