Getting Started
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 using the command line or terminal.
Using the Command Line or Terminal
-
Open the Command Line (Windows) or Terminal (macOS/Linux):
- Windows: Press
Win + R
Win + R
, typecmd
cmd
, and press Enter. - macOS/Linux: Use the terminal application.
- Windows: Press
-
Navigate to Your Python File’s Directory: Use the
cd
cd
command to navigate to the directory where your Python file is located. For example:commandcd path/to/your/python/files
commandcd path/to/your/python/files
-
Run Your Python Script: Execute your Python script using the
python
python
command followed by the filename with the.py
.py
extension.commandpython your_script.py
commandpython your_script.py
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!")
main.py# hello_world.py print("Hello, World!")
-
Save the File: Save the file with a
.py
.py
extension, such ashello_world.py
hello_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.
Important Python Commands
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!
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>
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
- Run a Python Script:
command
python your_script.py
commandpython your_script.py
Virtual Environment
Virtual environments help manage dependencies for different projects.
-
Create a Virtual Environment:
commandpython -m venv venv
commandpython -m venv venv
-
Activate Virtual Environment:
- Windows:
command
.\venv\Scripts\activate
command.\venv\Scripts\activate
- macOS/Linux:
command
source venv/bin/activate
commandsource venv/bin/activate
- Windows:
-
Deactivate Virtual Environment:
commanddeactivate
commanddeactivate
Package Management (pip)
pip is the package installer for Python.
-
Install a Package:
commandpip install package_name
commandpip install package_name
-
Install from Requirements File:
commandpip install -r requirements.txt
commandpip 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!
Was this page helpful?
Let us know how we did