Getting Started
Running Python on Your Computer
Section titled “Running Python on Your Computer”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:
graph TD
A[Write Python code] --> B[Interactive shell / REPL]
A --> C[Run a .py file]
A --> D[One-liner with -c]
A --> E[IDE Run button]
B --> F[Output]
C --> F
D --> F
E --> F
| Method | Best for |
|---|---|
| Interactive shell (REPL) | Quick experiments, testing one expression |
Running a .py file | Real programs and scripts |
python -c "code" | A throwaway one-liner from the terminal |
| IDE / editor Run button | Day-to-day development |
Using the Command Line or Terminal
Section titled “Using the Command Line or Terminal”-
Open the Command Line (Windows) or Terminal (macOS/Linux):
- Windows: Press
Win + R, typecmd, and press Enter. - macOS/Linux: Use the terminal application.
- Windows: Press
-
Navigate to Your Python File’s Directory: Use the
cdcommand to navigate to the directory where your Python file is located. For example:command cd path/to/your/python/files -
Run Your Python Script: Execute your Python script using the
pythoncommand followed by the filename with the.pyextension.command python your_script.py
Creating Your First Python File
Section titled “Creating Your First Python File”-
Open a Text Editor: Use a text editor (e.g., Visual Studio Code, Sublime Text, or Notepad) to write your Python code.
-
Write Your Python Code: Create a simple Python script, for example, a “Hello, World!” program:
main.py # hello_world.py print("Hello, World!") -
Save the File: Save the file with a
.pyextension, such ashello_world.py. -
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.
Running a One-Liner
Section titled “Running a One-Liner”For a quick test you do not even need a file. The -c flag runs a string of code directly:
C:\Users\Your Name> python -c "print(2 ** 10)"
1024Running a Module
Section titled “Running a Module”The -m flag runs an installed module as a script. This is how you start common tools:
python -m venv venv # create a virtual environment
python -m pip install requests
python -m http.server # start a quick local web serverImportant Python Commands
Section titled “Important Python Commands”Interactive Mode
Section titled “Interactive Mode”Python comes with an interactive mode that allows you to execute Python commands line by line.
- Open Interactive Mode:
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:
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>Running a Python File
Section titled “Running a Python File”- Run a Python Script:
command python your_script.py
Virtual Environment
Section titled “Virtual Environment”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
- Windows:
-
Deactivate Virtual Environment:
command deactivate
Package Management (pip)
Section titled “Package Management (pip)”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!
Try it: Hands-on Exercises
Section titled “Try it: Hands-on Exercises”Exercise 1 – Hello, World!
Section titled “Exercise 1 – Hello, World!”Exercise 2 – Print Your Name
Section titled “Exercise 2 – Print Your Name”Exercise 3 – Run a Simple Script
Section titled “Exercise 3 – Run a Simple Script”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading