To run Python code in the terminal, follow these steps.
python
or python3
to start the Python interpreter.Enter
to run it.For example, to print “Hello, World!” in Python, type the following in the terminal:
python3
>>> print("Hello, World!")
Hello, World!
Alternatively, you can also run a Python script saved in a file by typing python3 file_name.py
in the terminal, where file_name.py
is the name of the Python file you want to run.
Here is an example of executing Python on the command line to start the Python interpreter:
$ python3
Python 3.9.0 (default, Oct 5 2021, 14:01:47)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
In this example, $
represents the terminal prompt and python3
is the command to start the Python interpreter. After you run this command, you’ll see the Python prompt (>>>
) indicating that the interpreter is ready to accept your commands.
You can type Python code at the prompt and press Enter
to run it. For example:
>>> print("Hello, World!")
Hello, World!
>>>
You can exit the Python interpreter by typing exit()
or quit()
at the prompt, or by pressing Ctrl + D
.
Here is a simple Python script to monitor network traffic. import psutil def monitor_network(): #…
Here's a basic example of web scraping in Python using the requests and BeautifulSoup libraries:…
Here's an example of how you can use Python to SSH into a remote server…
Here's an example of a Python class that represents a rectangle: class Rectangle: def __init__(self,…
Here is a simple Python script that interacts with Active Directory (AD) using the pyad…
Here's a Python script to monitor disk usage on a system and send an email…