Creating Your First Program in Python

Welcome to codeswithpankaj.com! If you're just starting with Python, this guide will help you set up Python, write your first program, and run it in multiple environments. Let's get started!


Step 1: Installation of Python

  1. Download Python

    • Visit the official Python website: https://www.python.org.

    • Click on the Download Python button for your operating system (Windows, macOS, or Linux).

  2. Install Python

    • Run the downloaded installer.

    • Ensure you check the box that says "Add Python to PATH" before clicking "Install Now."

    • Follow the on-screen instructions to complete the installation.

  3. Verify Installation

    • Open a terminal or command prompt.

    • Type the following command and press Enter:

      python --version
    • You should see the Python version displayed (e.g., Python 3.x.x).


Step 2: Creating and Running a Python Program

  1. Write Your First Program Open a text editor (e.g., Notepad on Windows) and type the following code:

    print("Hello, World!")
  2. Save the File Save the file with the .py extension, for example, hello.py.

  3. Run the Program

    • Open a terminal or command prompt.

    • Navigate to the folder where you saved hello.py using the cd command.

    • Type the following command and press Enter:

      python hello.py
    • Output:

      Hello, World!

Step 3: Writing a Python Program using the Command-line Window

  1. Open a terminal or command prompt.

  2. Type python and press Enter to enter the interactive mode.

  3. In the Python shell, type:

    print("Welcome to CodesWithPankaj!")
  4. Press Enter, and you'll see:

    Welcome to CodesWithPankaj!

This is a quick way to test Python code without creating a file.


Step 4: Using Python IDLE

  1. Open Python IDLE

    • After installation, search for IDLE in your system and open it.

  2. Write Your Code

    • In the IDLE shell, type:

      print("Learning Python is fun!")
    • Press Enter, and you'll see:

      Learning Python is fun!
  3. Create a Script in IDLE

    • Click on File > New File to open the editor window.

    • Type the following code:

      print("This is my first Python script in IDLE!")
    • Save the file with a .py extension (e.g., script.py).

  4. Run the Script

    • Press F5 or go to Run > Run Module.

    • Output in the IDLE shell:

      This is my first Python script in IDLE!

Step 5: Interactive Python

Python's interactive mode allows you to test snippets of code quickly.

  1. Open a terminal or command prompt.

  2. Type python to enter interactive mode.

  3. You can directly type Python commands and see immediate results. Example:

    name = "Pankaj"
    print(f"Hello, {name}!")

    Output:

    Hello, Pankaj!

Conclusion

Congratulations! You've written and executed your first Python program using multiple methods. Python's simplicity and flexibility make it an excellent choice for beginners and professionals alike. Keep practicing, and stay tuned to codeswithpankaj.com for more tutorials!


Example Summary

Environment

Code

Output

Command Prompt/Terminal

print("Hello, World!")

Hello, World!

Python IDLE Shell

print("Learning Python is fun!")

Learning Python is fun!

Interactive Mode

name = "Pankaj"; print(f"Hello, {name}!")

Hello, Pankaj!

Happy Coding! 🚀

Last updated