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
    

    Try It Now

    Output:

    Hello, World!
    

    Try It Now

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

Try It Now

Then type:

print("Hello, Python!")

Try It Now

Press Enter, and you will see:

Hello, Python!

Try It Now

4. Python Program with Variables

Let’s modify the program to use variables:

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

Try It Now

Output:

Hello, Alice

Try It Now

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!

Try It Now