From point A to point B
Point A: you have Python 3.14 installed and you've already tried the interactive interpreter, but everything you type there disappears when you close it. Point B: by the end of this chapter, you'll write a program in a file, run it from the terminal, ask the user for input and turn it into a command the system runs directly.
Hello world in the interpreter
As you saw in chapter 1, there are 2 ways to run Python code: line by line in an interactive session, or by writing a complete program in a file. Let's start with the first one.
Tradition says the first program in any language should greet the world. In Python, one line is enough. Open the interpreter with python3 (or py on Windows), or with ipython if you installed it, and type:
>>> print("Hello, world")
Hello, world
print() is a function: a named block of code that performs a task. You pass it data inside the parentheses, and it shows that data on screen. You'll study functions in detail later; for now, remember that the parentheses are required.
print "Hola Mundo", without parentheses, because print was a statement in Python 2. In Python 3, it's a function. If you run the old form, Python 3.14 warns you with SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?print() accepts several values separated by commas. By default, it separates them with a space and ends with a newline. You can change both with the sep and end parameters:
>>> print("10.0.0.1", "10.0.0.2", "10.0.0.3", sep="\n")
10.0.0.1
10.0.0.2
10.0.0.3
>>> print("Hello", "world", sep=", ", end="!\n")
Hello, world!
Your first script
Whatever you type in the interpreter is lost when you exit. To save a program and reuse it, or share it with your team, you write it in a text file with the .py extension. That file is called a script.
Open your editor (IDLE, VS Code or whichever you chose), create a file named hello.py and type:
print("Hello, world")
Save it, open a terminal in the same folder and pass the file name to the interpreter:
$ python3 hello.py
Hello, world
On Windows, the equivalent command is py hello.py or python hello.py. Python reads the file from top to bottom and runs each line in order.
You can use accents and other non-ASCII characters without trouble. Since Python 3, source code is read as UTF-8 by default, so print("¿Qué tal, señora?") works as is. Just make sure your editor saves files as UTF-8, which is the usual setting.
Ask for input with input()
A program that only shows fixed text doesn't do much. The input() function shows a message, waits for the user to type something and press Enter, and returns what they typed as text. Create hello_net.py:
print("Hello, world")
hostname = input("What's your router's name? ")
print("Hello,", hostname)
$ python3 hello_net.py
Hello, world
What's your router's name? R1-Lima
Hello, R1-Lima
The second line stores what you type in a variable called hostname. The third line uses it. In chapter 3, you'll see that input() always returns a text string, even if you type a number.
raw_input() to read from the keyboard. In Python 3, that function is called input(). The old Python 2 input(), which evaluated what you typed as code, no longer exists.Run the script on Windows
On Windows, the reliable way to run a script is from the terminal, with py or python before its name. That way, you see the output and any error messages even if the program ends instantly.
If you open a script with a double-click, Windows opens a console, runs the program and closes the console as soon as it ends. With a program this short, you won't have time to read anything. A classic trick is to end the script by waiting for a key:
print("Hello, world")
input("Press Enter to exit...")
Keep in mind that the Python 3.14 install manager doesn't guarantee that .py files open with a double-click. The official documentation recommends typing py before the script name. Get used to the terminal: that's where you'll work with your network tools.
The shebang on Linux and macOS
On Unix-like systems, you can run a script like any other command, without typing python3 first. For that, the system needs to know which interpreter to use. You tell it on the first line of the file:
#!/usr/bin/env python3
print("Hello, world")
hostname = input("What's your router's name? ")
print("Hello,", hostname)
That first line is called the shebang (also hashbang or sharpbang). The #! characters tell the operating system: “run this file with the program that follows.”
You could write the exact path to the interpreter, like #!/usr/bin/python3. The problem is that this path changes from system to system: on macOS with the official installer, it lives somewhere else, and so does the one inside a virtual environment. That's why /usr/bin/env python3 is preferred: the env program looks for python3 in the PATH and uses the first one it finds. If you have a virtual environment active, it uses that environment's Python.
Write python3, not python, in the shebang. On many Linux distributions, the python command doesn't exist or points to an old version.
py command also reads the shebang. If it finds #!/usr/bin/env python3 or #!/usr/bin/python, it uses the default Python version or the active virtual environment. That way, the same script works on every system.Execute permissions with chmod
The shebang isn't enough. On Linux and macOS, a file can only run if it has execute permission. You grant it with chmod:
$ chmod +x hello_net.py
$ ls -l hello_net.py
-rwxr-xr-x 1 ana ana 119 Oct 24 18:10 hello_net.py
The x characters in -rwxr-xr-x show that the file is now executable. Now you launch it like any program, adding ./ in front to say it's in the current folder:
$ ./hello_net.py
Hello, world
What's your router's name? core-sw01
Hello, core-sw01
You need the ./ because, for security reasons, the terminal doesn't look for programs in the current folder. If you move the script to a folder included in your PATH, you can run it from anywhere. Typing its name will be enough.
This is the pattern for almost every automation tool you'll write: a .py file with a shebang, execute permissions and, later on, command-line arguments.
Challenge
Answer before you open the solution.
1. Why does print "Hello" fail in Python 3.14?
Because print is a function. It needs parentheses: print("Hello"). The form without parentheses was valid in Python 2.
2. Which function do you use in Python 3 to read what the user of your program types?
input(). It shows an optional message and returns what was typed as text. It replaces Python 2's raw_input().
3. Why is #!/usr/bin/env python3 preferred over #!/usr/bin/python3?
Because it doesn't depend on a fixed path. env looks for python3 in the PATH, so it works across different systems and respects the active virtual environment.
4. You added the shebang, but ./script.py answers “Permission denied”. What's missing?
Execute permission. Run chmod +x script.py and try again.
5. What does print("R1", "R2", "R3", sep=" | ") show?
R1 | R2 | R3. The sep parameter replaces the space that print() puts between values by default.
Key takeaways
- In Python 3,
print()is a function and takes parentheses. - A script is a
.pyfile you run withpython3 file.pyorpy file.py. input()asks for data and always returns text.- The
#!/usr/bin/env python3shebang says which interpreter should run the file. chmod +xgrants execute permission; then you launch the file with./file.py.
In chapter 3, you'll meet Python's basic types: numbers, text strings and booleans, along with their operators.
Official resources
- Using the Python interpreterdocs.python.org
- print()docs.python.org
- input()docs.python.org
- Using Python on Windows (shebang lines)docs.python.org
Source and credits. This course is based on the book Python para todos by Raúl González Duque, published under the Creative Commons Attribution 2.5 Spain license. The original book was written for Python 2.5. Jesús Espinoza, with AI assistance, brought it to Python 3.14: he rewrote every chapter in his own words, replaced the examples with networking cases, updated the syntax and the modules that no longer exist, fixed errors in the original and tested all the code with Python 3.14.6 against the official documentation.