This guide assumes you have:
There are two main ways to use Python from the command line:
Open your terminal and type:
python
python3
You should see something like this:
Python 3.13.1 (main, Jun 7 2023, 00:00:00) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Or, on Windows, you'll probably see:
Python 3.13.1 (tags/v3.13.1:0671451, Dec 3 2024, 19:06:28) [MSC v.1942 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
The >>>
prompt means Python is ready for your commands!
Try these commands:
>>> print("Hello, World!") Hello, World! >>> 2 + 2 4 >>> name = "Coder" >>> print(f"Hello, {name}!") Hello, Coder!
To exit the Python shell, type:
>>> exit()
Or press Ctrl+D (Mac/Linux) or Ctrl+Z then Enter (Windows)
To run a Python file, navigate to its folder in the terminal, then:
python filename.py
python3 filename.py
greet.py
with this content:
name = input("What's your name? ") print(f"Nice to meet you, {name}!") print("Welcome to Python programming!")
python greet.py
python3 greet.py
python --version
Or on Mac/Linux:
python3 --version
You can pass information to your Python script:
python script.py argument1 argument2
python --help
This means Python isn't in your system's PATH. Try:
C:\Python311\python.exe
)This means Python can't find your file. Make sure:
cd
to navigate)dir
(Windows) or ls
(Mac/Linux) to see files in current folderThis means there's a mistake in your Python code. Check for:
Open the Python shell and try these calculations:
>>> 15 + 7 >>> 100 - 33 >>> 12 * 8 >>> 50 / 5 >>> 2 ** 10 # This means 2 to the power of 10
calculator.py
print("Simple Calculator") num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) print(f"{num1} + {num2} = {num1 + num2}") print(f"{num1} - {num2} = {num1 - num2}") print(f"{num1} * {num2} = {num1 * num2}") print(f"{num1} / {num2} = {num1 / num2}")
Now that you can run Python from the command line, try: