Python

Writing Your First Python Program

The "Hello, World!" program is a time-honored tradition in computer science. It is the simplest possible program that proves your environment (the interpreter and IDE) is configured correctly. In Python, this is famously concise.

1. The Code: Hello, World!

In your IDE (like VS Code or PyCharm), create a new file named hello.py and type the following:

Python
print("Hello, World!")

How to run it:

  • In the IDE: Look for a "Run" button (usually a green play icon).

  • In the Terminal: Type python hello.py and press Enter.

2. The Theory: What Just Happened?

Theoretically, even this single line of code triggers a complex sequence of events within the Python ecosystem.

A. The print() Function

print() is a built-in function. In programming theory, a function is a reusable piece of code that performs a specific action.

  • Arguments: The text inside the parentheses ("Hello, World!") is called an argument. It is the data you are passing into the function to be processed.

  • Strings: The quotation marks tell Python that this data is a String (a sequence of characters), not a variable name or a command.

B. The "Standard Output"

Theoretically, the print() function sends data to the sys.stdout (Standard Output) stream. By default, your operating system directs this stream to your terminal or console window.

3. Long Theory: The Execution Lifecycle

When you execute python hello.py, the computer performs several hidden steps:

  1. Lexical Analysis: The interpreter breaks your code into "tokens." It identifies print as a keyword/function and "Hello, World!" as a literal string.

  2. Parsing: The interpreter checks the syntax. If you forgot a parenthesis—print("Hello"—the parser would fail and throw a SyntaxError.

  3. Bytecode Compilation: Python converts your source code into Bytecode. This is a version of your code that is easier for the "Python Virtual Machine" to read. It creates a hidden folder called __pycache__ to store this.

  4. Runtime: The Python Virtual Machine (PVM) reads the bytecode and executes the instructions, ultimately calling the system's "write" command to display the text on your screen.

4. Expanding the Program: Variables and Input

To make the program more interactive, we can use Variables (containers for data) and the input() function.

Python
# This is a comment - it is ignored by Python
name = input("What is your name? ")
print("Hello, " + name + "!")

Theoretical Concepts here:

  • Assignment: The = operator doesn't mean "mathematical equality" in programming. It means "take the value on the right and store it in the label on the left."

  • Dynamic Typing: You didn't have to tell Python that name is a string. Python figured it out automatically based on what the user typed.

  • Concatenation: The + sign, when used with strings, "glues" them together into one longer string.

5. Long Theory: The "Read-Eval-Print Loop" (REPL)

Python offers a unique way to write code called the REPL. If you just type python in your terminal without a filename, you enter an interactive mode (indicated by >>>).

  • Read: Python reads the line of code you type.

  • Eval: It evaluates (executes) the code immediately.

  • Print: It prints the result to the screen.

  • Loop: It waits for your next line.

Theory: The REPL is an incredible tool for prototyping. It allows developers to test small snippets of logic or verify how a function works without having to create and save a full script file.

6. Summary Table for EMBLAb

ElementDescriptionTheoretical Role
print()Built-in function.Output Interface.
" "Double/Single quotes.String Delimiter (defines text).
#Comment symbol.Documentation (ignored by PVM).
.pyFile extension.Identifies the file as Python source code.
()Parentheses.Invokes (calls) the function.
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now