← Back to Python Course | Chapter 1: Basics | Lesson 4 of 14

Python First Program

The print() Function

print() is the built-in function that writes output to the screen — pass it text, a number, or a variable, and it displays the result followed by a newline. It's the fastest way to see what your code is actually doing while you're learning.

Example: The print() Function

python
print("Hello, World!")

Basic Strings

Python treats text wrapped in either single or "double" quotes identically as a str value, so you can pick whichever avoids awkward escaping (for example, using double quotes for a string that itself contains an apostrophe). Both forms are functionally interchangeable.

Example: Basic Strings

python
print('Hello')
print("Hello")
print("It's a good day")

Standard Calculations

Because print() evaluates its argument before displaying it, you can write print(2 + 3) directly and Python will compute 5 and show that, without a separate calculation step first. This is a quick way to sanity-check arithmetic while experimenting.

Example: Standard Calculations

python
print(2 + 3)

Variables in Action

A variable is created the moment you assign it a value with =, and passing that variable's name to print() displays whatever it currently holds. Because Python is dynamically typed, the same variable can be reassigned to a different type later without any special syntax.

Example: Variables in Action

python
value = 10
print(value)
value = "ten"
print(value)

Running the Program

Once your first script runs cleanly, it's worth deliberately introducing a typo (like a missing parenthesis) to see what a Python error message looks like — recognizing common errors early makes debugging far less intimidating once your programs grow.

Example: Running the Program

python
# Missing closing parenthesis -- try running this to see the error
print("Hello"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.