Course topics

By WebNest Studio Engineering Team

Python Tutorial

Hello World Program in Python

Every programming journey starts with printing "Hello, World!". In Python this takes a single line, but the tiny program already teaches you how to write a source file, run it, and read the output — and how the print() function works.

In this lesson you will write Hello World in several ways, run it from a file and from the terminal, learn the main options of print(), and see how a real Python program is usually structured with a main() function.

Why it matters

print() is the first function you learn and one you never stop using: it is the quickest way to see what a program is doing. Its sep and end options, and the main() pattern shown in this lesson, appear in nearly every Python file you will read.

Your First Program

Create a file named hello.py containing print("Hello, World!"), open a terminal in the same folder, and run python hello.py (or python3 hello.py on macOS/Linux). Python reads the file, executes the statement, and prints the text. No class, no main method, no semicolons are needed.

How print() Works

print() converts its arguments to strings, separates them with a space and ends with a newline. The sep parameter changes the separator, end changes what is printed at the end, and file can send output to a file or sys.stderr. Strings can use single or double quotes.

The main() Convention

Larger programs put their code in functions and call a main() function inside if __name__ == "__main__":. This block runs only when the file is executed directly, not when it is imported as a module by another file — a pattern you will see in almost every Python project.

Examples

Hello World

Python
print("Hello, World!")
Output
Hello, World!

print() with several arguments, sep and end

Python
print("Hello", "Webnest", "Studio")
print("2026", "09", "27", sep="-")
print("Loading", end="...")
print("done")
print('Single quotes work too')
Output
Hello Webnest Studio
2026-09-27
Loading...done
Single quotes work too

Structured Hello World with a main function

Python
def greet(name):
    return f"Hello, {name}! Welcome to Python."


def main():
    for name in ["Asha", "Ravi"]:
        print(greet(name))


if __name__ == "__main__":
    main()
Output
Hello, Asha! Welcome to Python.
Hello, Ravi! Welcome to Python.

Running the program from the terminal

Python
# Windows
python hello.py

# macOS / Linux
python3 hello.py
Output
Hello, World!

Common Mistakes

  • Writing Print("Hello") — Python is case-sensitive and the function is print.
  • Forgetting the quotes around text, so Python treats Hello as an undefined variable (NameError).
  • Mixing quote types: "Hello' is a syntax error.
  • Running python hello.py from a different folder than the file is in.

Exercise

Using the sep and end parameters of print(), print a date from three separate values, and then print two messages on the same line with two separate print calls.

Expected output: 2026-10-02 then Loading... done

Python · starter code
# TODO: print "2026", "10" and "02" joined by hyphens, using one print call

# TODO: print "Loading" and "done" with two print calls so they appear on one line
Show hints
  • sep sets what is placed between the values: print("a", "b", sep="-").
  • end replaces the line break that print normally adds: print("Loading", end="... ").
Show solution
Python · solution
print("2026", "10", "02", sep="-")

print("Loading", end="... ")
print("done")

Quick quiz

1. What does print("a", "b") display?

  • A. ab
  • B. a,b
  • C. a b
  • D. ("a", "b")
Show answer

C. The default separator between values is one space.

2. What does print() add after its output by default?

  • A. Nothing
  • B. A space
  • C. A full stop
  • D. A line break
Show answer

D. The default value of end is a newline character.

3. When is the code under if __name__ == "__main__": executed?

  • A. Only when the file is run directly, not when it is imported
  • B. Always
  • C. Only when the file is imported
  • D. Never
Show answer

A. A file that is run directly has __name__ set to "__main__". An imported file has its module name.

Interview questions

What is the purpose of if __name__ == "__main__"?

Show answer

Python sets the variable __name__ to "__main__" in the file that is run directly, and to the module's name in a file that is imported. Putting the start-up code under this check means it runs when the file is executed as a program but not when another file imports it to reuse its functions.

Which parameters does print() accept besides the values to print?

Show answer

sep is the text placed between values and defaults to a space. end is the text added at the end and defaults to a newline. file is where the output is written and defaults to standard output, and flush forces the output to be written immediately.

Key Points to Remember

  • print("Hello, World!") is a complete Python program.
  • Run files with python file.py (python3 on macOS/Linux).
  • print() accepts multiple values and the sep and end parameters.
  • Real programs use a main() function guarded by if __name__ == "__main__".

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering