What You Will Learn
In the previous lesson you ran Python commands directly in the shell. In this lesson you will write a proper Python script — a file you can save and run again any time.
Creating a Script File
Open VS Code and create a new file called hello.py. The .py extension tells your computer this is a Python file.
Type this inside the file:
print("Hello, world!")
print("My name is Alice.")
print("I am learning Python.")
Save the file, then run it. In the VS Code terminal:
python hello.py
Expected output:
Hello, world!
My name is Alice.
I am learning Python.
Python runs each line from top to bottom, one at a time.
What Does print() Do?
print() is a built-in Python function that displays text on the screen.
Whatever you put inside the parentheses gets printed. The text must be wrapped in quotes:
print("This works")
print('This also works')
Both single quotes and double quotes are fine. Pick one style and be consistent.
Printing Numbers
You can also print numbers without quotes:
print(42)
print(3.14)
print(100 + 200)
Expected output:
42
3.14
300
Notice that print(100 + 200) actually calculates the sum and prints the result. Python evaluates the expression inside print() before displaying it.
Adding Comments
A comment is a line that Python ignores. It is for humans reading the code, not for the computer.
Comments start with #:
# This is a comment
print("Hello!") # This is also a comment
# Python will skip these lines completely
# They are just notes for the programmer
Expected output:
Hello!
Comments are useful for explaining what your code does, especially when you come back to it weeks later.
Blank Lines
Blank lines are also ignored by Python. Use them to separate sections of your code and make it easier to read:
# Greeting
print("Hello!")
# Information
print("I am learning Python.")
print("It is fun.")
What You Learned
- How to create and run a
.pyscript file - How
print()displays text and numbers - How to write comments with
# - That Python runs code from top to bottom
In the next lesson, you will learn about variables — how Python stores and remembers information.