Python Program Structure

Code "Hello,World!" in Python.

* In Python, you do not necessarily need a 'main()' function like you often do in C or other languages.

Python - Hello, World!

In Python, a simple "Hello, World!" program typically consists of just one line of code. Here's the structure of Python's first program:

py Copy Code
# Code your first program in Python
print("Hello, World!")
Hello, world!


'#' is used to denote comments, and comments are lines of text that are ignored by the Python interpreter when the script is executed.

'print()' is a built-in Python function used to display text on the screen.

"Hello, World!" is a string literal enclosed in double quotes, indicating the text to be displayed.

Code Execution:

VS Code: Use one of the following methods to run your script with Code Runner: Press 'Ctrl+Alt+N'.


1. Open a text editor and paste the code into a new file. You can name the file something like ''.

2. Save the file.

3. Open a terminal or command prompt.

4. Navigate to the directory where you saved the file.

5. Type python and press Enter.

6. Output to the console.

Python Console I/O

Let's delve into the basic I/O functions in Python, specifically for console-based interactions.

Console Input:

name = input("Enter your name: ")

input() This function prompts the user to enter input and waits for the user to type something, and it takes an optional prompt string as an argument and returns the input as a string.

Console Output:

print("Hello, Ayan Sarkar!")

output() This function prints the given objects to the standard output, and after printing all the objects, it appends the 'end' string (default is a newline).

PDF Copy Code
# Python console I/O functions
def main():
    name = input("Enter your name: ")
    print("Hello,", name, "!")

if __name__ == "__main__":

Recommended: use a Python code formatter and configuration it on VS Code settings to format the code before execution.


json Copy Code
"[python]": {
    "editor.defaultFormatter": "",
    "editor.codeActionsOnSave": {
      "source.organizeImports": "explicit"
Enter your name: Ayan Sarkar
Hello, Ayan Sarkar !


We define a 'main()' function where we'll handle the main logic.

The 'input()' function to prompt the user to enter their name, and the input is stored in the variable 'name'.

Print a greeting message using the entered name.

Wait: Q: Why 'main()' function used here? A: It's not strictly necessary to have a 'main()' function, but, using this function is a common practice in Python when you want to encapsulate the main logic of your program.

Python main() Function:

Python doesn't require a 'main()' function like some other programming languages, it provides a way to structure your code more cleanly, especially for larger programs or when you plan to reuse your code as a module. Here are a few reasons why using a 'main()' function can be beneficial:

1. Clarity and Readability: Having a 'main()' function makes it clear where the main execution of the program begins. This can improve readability, especially for larger programs.

2. Encapsulation: By encapsulating the main logic of your program within a 'main()' function, you separate it from other parts of your code. This can make your code easier to understand and maintain.

3. Testability: It can be easier to test the main logic of your program if it's contained within a 'main()' function.

4. Modularity: If you later decide to use parts of your code as a module in another program, having a 'main()' function allows you to import the module without executing the main logic immediately.

Conclusion: While Python doesn't require a 'main()' function, using one can help improve the organization, readability, and maintainability of your code, especially for larger or more complex programs.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.