Skip to content

Getting Started

To start coding in Python, you need to have Python installed on your computer. Follow the installation guide on our Installation Page if you haven’t done so already.

Once Python is installed, you can run Python code in several ways. Pick the one that fits what you are doing.

Diagram:

diagram ways to run Python mermaid
Different ways to execute Python code
MethodBest for
Interactive shell (REPL)Quick experiments, testing one expression
Running a .py fileReal programs and scripts
python -c "code"A throwaway one-liner from the terminal
IDE / editor Run buttonDay-to-day development
  1. Open the Command Line (Windows) or Terminal (macOS/Linux):

    • Windows: Press Win + R, type cmd, and press Enter.
    • macOS/Linux: Use the terminal application.
  2. Navigate to Your Python File’s Directory: Use the cd command to navigate to the directory where your Python file is located. For example:

    command
    cd path/to/your/python/files
  3. Run Your Python Script: Execute your Python script using the python command followed by the filename with the .py extension.

    command
    python your_script.py
  1. Open a Text Editor: Use a text editor (e.g., Visual Studio Code, Sublime Text, or Notepad) to write your Python code.

  2. Write Your Python Code: Create a simple Python script, for example, a “Hello, World!” program:

    main.py
    # hello_world.py
    print("Hello, World!")
  3. Save the File: Save the file with a .py extension, such as hello_world.py.

  4. Run Your Python Script: Follow the steps mentioned above to navigate to the file’s directory in the command line or terminal and run the script.

For a quick test you do not even need a file. The -c flag runs a string of code directly:

command
C:\Users\Your Name> python -c "print(2 ** 10)"
1024

The -m flag runs an installed module as a script. This is how you start common tools:

command
python -m venv venv          # create a virtual environment
python -m pip install requests
python -m http.server        # start a quick local web server

Python comes with an interactive mode that allows you to execute Python commands line by line.

  • Open Interactive Mode:
command
C:\Users\Your Name>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!
  • Exit Interactive Mode:
command
C:\Users\Your Name>python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb  7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!
>>> exit()
C:\Users\Your Name>
  • Run a Python Script:
    command
    python your_script.py

Virtual environments help manage dependencies for different projects.

  • Create a Virtual Environment:

    command
    python -m venv venv
  • Activate Virtual Environment:

    • Windows:
      command
      .\venv\Scripts\activate
    • macOS/Linux:
      command
      source venv/bin/activate
  • Deactivate Virtual Environment:

    command
    deactivate

pip is the package installer for Python.

  • Install a Package:

    command
    pip install package_name
  • Install from Requirements File:

    command
    pip install -r requirements.txt

This should give you a solid start on running Python code, creating files, and using some essential commands. Explore more with our tutorials on Python Central Hub!


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading