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

Python First Program

Your first program is like teaching a parrot to say its first word. You tell the computer to show a message on the screen, and it does exactly that.

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')  # single quotes work for strings
print("Hello")  # double quotes work the same way
print("It's a good day")  # double quotes avoid escaping the apostrophe

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  # value starts as an integer
print(value)
value = "ten"  # same variable reassigned to a string
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"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the parentheses in print, as in print "Hello", which is a SyntaxError in Python 3.
  2. Leaving a string unquoted, as in print(Hello), so Python looks for a variable called Hello and raises NameError.
  3. Mixing quote types such as print("Hello'), which leaves the string unterminated and gives a SyntaxError.

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.