Loading...

Python essentialsGetting Started

Your first Python program

4 min lesson

Next
  • A "Hello, world!" program is a small first program that prints text to the screen.
  • Running one confirms that Python is installed and that you can execute a saved script.
  • The print() function displays the value passed between its parentheses; quoted text is a string.
  • Python runs a script from top to bottom, so each print() call appears in order.
  • A basic example is print("Hello, world!"). Python does not require a semicolon at the end of the line.
  • Printing is useful for showing a user a message or checking the value your program calculated.
  • Text needs quotes; without them, Python treats the word as a variable name.
  • This lesson builds on setting up Python and leads into syntax, indentation, and comments.
Keep in mind
  • Use print() to show text on screen.
  • Put text inside quotes, like print("Hello").
  • Save your file as main.py and run it with python3 main.py (or python main.py on Windows).
  • Seeing your first output is a big step; celebrate it and keep experimenting.
main.py
# Store a greeting in a variable, then display it.
greeting = "Hello, world!"
print(greeting)
print("This is my first Python program.")

What happens: Hello, world! This is my first Python program.

Breaking it down

Variable assignment

greeting = "Hello, world!"

The assignment stores a string in the name greeting. Python creates the variable when the assignment runs.

Calling print()

print(greeting)

Parentheses call the print function, and the variable's value is written to standard output.

String literal

"This is my first Python program."

Quotes tell Python that these characters are text. Without quotes, Python looks for a variable with that name.

Useful string escape sequences

EscapeWhat it doesExample output
\nStarts a new lineLine break
\tInserts a horizontal tabWide gap
\\Prints a literal backslash\
\"Prints a double quote inside a string"

A helpful analogy

print() is like a megaphone: you say something into it (put text in the parentheses), and everyone (the screen) hears it. The quotes are like putting your message in a sealed envelope so Python knows it is text, not a variable name.

Nesting rules

  • 1.Python executes top-level statements in order; this example does not need a main() function.
  • 2.Use matching quotes around text and parentheses around the arguments passed to print().
  • 3.A newline is added after each print() call by default; use end=" " when you want to continue on the same line.

Common mistakes

Leaving quotes off a string

Avoid

print(Hello)

Use instead

print("Hello")

Without quotes, Python treats Hello as a variable name and raises NameError if that variable has not been defined.

Writing print without calling the function

Avoid

print "Hello"

Use instead

print("Hello")

In Python 3, print is a function. Its value must be supplied inside parentheses.

Quick tips

  • •Change the greeting and run the script again to see the new output.
  • •Try storing your name in a variable and printing a personalized message.
  • •Add another print() call and notice that each call starts on a new line by default.
  • •Run the script after each small edit so errors are easy to locate.
Previous
Next