Step 4 · Ubuntu
Python 3
Python is the fastest way to get a working program. There is no compiler to run and nothing to build — you write a file and run it.
Run this
$ curl -fsSL https://aikaryashala.com/system_setup/scripts/install_py.sh | bash
It installs python3 and writes a sample program into
~/python-samples.
What gets installed
python3The interpreter. It reads your file and does what each line says.
That is the whole list. Stopping a program in the middle to see what it is doing is step 12, and installing libraries is step 8. Neither is needed to write a program and run it.
The sample program
This is the same program as sum.c from
step 3, written in Python. You will find it at
~/python-samples/sum.py, and you can also
download it.
print("To add two numbers.")
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num1 = num1 + num2
print(f"The sum of two numbers is {num1}.")
Go to the folder:
$ cd ~/python-samples
And run it:
$ python3 sum.py
To add two numbers.
Enter the first number: 3
Enter the second number: 4
The sum of two numbers is 7.
In C the same program needed two commands: one to build it, one to run it. Here there is only one, and no new file appeared. Nothing was built.
python3 reads your file and carries out each line as it goes.
Chapter 7 of the book explains
what that costs and what it buys you.
Write your own
Make a new file with nano:
$ nano hello.py
Type this in, then press Ctrl+O and Enter to save, and Ctrl+X to leave:
name = input("What is your name? ")
print(f"Namasthey, {name}!")
$ python3 hello.py
Edit, run, edit, run, with nothing in between. That short loop is why Python is used so widely for learning, and for getting a first version working.
Try one line at a time
Run python3 with no file and it waits for you to type. This is
handy for checking what something does before you put it in a program.
$ python3
>>> 2 + 3
5
>>> "hello".upper()
'HELLO'
>>> numbers = [12, 7, 3, 21]
>>> sum(numbers)
43
>>> len(numbers)
4
>>> exit()
Type exit() and press Enter, or press
Ctrl+D. The >>> prompt is
Python waiting, not your shell — your normal commands will not work while
it is showing.
Check it worked
Which Python you have:
$ python3 --version
That your sample arrived:
$ ls ~/python-samples
And the one that really proves Python works — a program that runs and gives the right answer:
$ cd ~/python-samples && python3 sum.py
To stop a program in the middle and look at its variables, go to step 12.
To install a library such as requests, go to
step 8. Do not use pip against
the system Python — Ubuntu depends on it, and modern releases refuse the
change anyway.
Next
Step 5 is the Java toolchain.