Python First Program

Now that you have Python installed, let’s write and run your first Python program! 🚀

1. Writing Your First Python Program

Open any text editor or Python IDE and create a new file named hello.py.
Now, type the following code:

print("Hello, World!")

Try It Now

💡 Explanation:

  • print() is a built-in function that displays output.
  • "Hello, World!" is a string that gets printed on the screen.

2. Running the Python Program

Method 1: Using the Command Line

  1. Open the terminal or command prompt.
  2. Navigate to the folder where hello.py is saved.
  3. Run the program:
    python hello.py
    

    Output:

    Hello, World!
    

Method 2: Using Python IDLE

  1. Open Python IDLE.
  2. Click File → New File and enter the code.
  3. Save the file as hello.py.
  4. Click Run → Run Module (F5).

3. Running Python in Interactive Mode

You can also run Python commands directly in the terminal without saving a file.

python

Then type:

print("Hello, Python!")

Press Enter, and you will see:

Hello, Python!

4. Python Program with Variables

Let’s modify the program to use variables:

name = "Alice"
print("Hello,", name)

Try It Now

Output:

Hello, Alice

5. Taking User Input

We can also ask for user input and display a message:

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

Try It Now

Example Output:

Enter your name: John  
Welcome, John!