Your First Python Program (Hello World and Beyond)


Table of Contents

  • Introduction
  • Setting Up Your Python Environment
  • Writing Your First Python Script
    • Printing “Hello, World!”
    • Breaking Down the Code
  • Running Python Programs
    • Using the Terminal or Command Prompt
    • Running Python Code in VSCode
    • Running Python Code in PyCharm
    • Using Jupyter Notebook
  • Understanding Basic Concepts
    • Syntax
    • Indentation
    • Comments
  • Moving Beyond Hello World
    • Accepting User Input
    • Simple Calculations
    • Displaying Output
  • Best Practices for Beginners
  • Final Thoughts

Introduction

Every journey into programming begins with the timeless tradition of writing a simple “Hello, World!” program. While it may seem basic, creating and running your first Python program is a critical step. It ensures your development environment is correctly set up and familiarizes you with basic Python syntax. In this module, we will write our first Python script, run it using different methods, and dive slightly deeper into simple but essential concepts beyond just printing text.


Setting Up Your Python Environment

Before writing your first program, ensure you have Python installed on your machine. If you have not installed Python yet, refer to the previous module for a complete installation guide for Python, VSCode, PyCharm, and Jupyter Notebook.

Verify Python installation by running:

python --version

or on macOS/Linux:

python3 --version

If Python is installed correctly, you are ready to start coding.


Writing Your First Python Script

Let’s create the simplest Python program possible.

  1. Open any text editor (VSCode, PyCharm, or even Notepad for now).
  2. Create a new file and save it with the .py extension, for example, hello.py.
  3. Write the following code:
print("Hello, World!")

Breaking Down the Code

  • print(): This is a built-in Python function used to display output to the console.
  • “Hello, World!”: This is a string — a sequence of characters enclosed in quotation marks.

Python reads this line and outputs exactly what is inside the quotation marks.


Running Python Programs

After writing the code, it is time to run your first Python script.

Using the Terminal or Command Prompt

  1. Navigate to the directory where your hello.py file is located.
  2. Run the script by typing: On Windows: python hello.py On macOS/Linux: python3 hello.py

You should see:

Hello, World!

printed to the console.

Running Python Code in VSCode

  1. Open VSCode.
  2. Open your project folder or the file directly.
  3. Press Ctrl+Shift+P and choose Run Python File in Terminal.

Alternatively, after installing the Python extension, you can click on the small Run button at the top right corner of the editor.

Running Python Code in PyCharm

  1. Open PyCharm and create a new project.
  2. Create a new Python file.
  3. Right-click inside the editor and choose Run ‘hello’.

PyCharm will execute the program and display the output in the terminal pane at the bottom.

Using Jupyter Notebook

  1. Launch Jupyter Notebook.
  2. Create a new Python 3 notebook.
  3. In the first cell, write:
print("Hello, World!")
  1. Click Run or press Shift+Enter.

Understanding Basic Concepts

Syntax

Python’s syntax is designed to be readable and straightforward. Unlike many other languages, Python does not require semicolons or braces.

Indentation

Python uses indentation to define code blocks. Missing or incorrect indentation will lead to errors.

Example:

# Correct
if True:
print("Correct indentation.")

# Incorrect (will cause an IndentationError)
if True:
print("Wrong indentation.")

Comments

Comments are notes you write for yourself and others. Python ignores them during execution.

  • Single-line comment:
# This is a comment
  • Multi-line comment:
"""
This is a multi-line comment.
Useful for detailed explanations.
"""

Using comments effectively makes your code more readable and maintainable.


Moving Beyond Hello World

Now that you have printed your first output, let’s add more interactivity.

Accepting User Input

name = input("Enter your name: ")
print("Hello, " + name + "!")

This program asks the user for their name and greets them personally.

Simple Calculations

a = 5
b = 3
print("Sum:", a + b)

You can perform mathematical operations easily in Python.

Displaying Output

You can customize your output formatting:

age = 25
print(f"I am {age} years old.")

The f before the string enables f-strings, which allow for cleaner and more readable code.


Best Practices for Beginners

  • Always save your files with the .py extension.
  • Use clear and meaningful names for your files and variables.
  • Run your programs often to catch syntax or logical errors early.
  • Write comments as you code to describe what each part of the code does.
  • Practice small programs daily to build a strong foundation.

Final Thoughts

Congratulations on creating and running your first Python program! Although printing “Hello, World!” may seem trivial, it symbolizes the first step into the vast world of programming. As you move forward, you will learn how Python’s simplicity and power make it one of the most versatile languages in the industry.

Syskoolhttps://syskool.com/
Articles are written and edited by the Syskool Staffs.