Using Python on Replit

Replit (www.replit.com) is a free online service that gives you a way to develop and run programs inside a browser. This is a full-featured environment with a browser-based text editor with syntax highlighting. To use Replit for this class - all you need to write and run Python code is a web browser. There is nothing to install at all.

You save some time by using Dr. Chuck's PY4E Replit template by starting at: https://replit.com/@ChuckSeverance/PY4E?v=1 . This includes the files you will use for the course and sample code.

This means that you can do this course on a "locked-down" environment on systems like Apple's iPad, iPhone, Android, ChromeBooks, or Windows 10 Home S. You can also use Replit if you are using a work or school computer that does not allow any software into be installed.

Sign up for an account

You will need to sign up for an account to use Replit. They have a free level that will cover all of your needs for this course through Chapter 15.

Writing Your First Program on Replit

Once you can log in to Replit, go into the files tab and create a new file called hello.py in your home folder (should be something like /home/home/runner/PY4E). Put the following line in the file:

print('Hello world')
Save the file and press Run and you should see:
Hello world
Then change the text to 'Hello PY4E world', and press Run and it should run your modified program.

While the Run button works for programs that are a few lines, once you start working on more complex programs you may need to use a Linux shell (command line). It might feel a little strange at the beginning but learning a little bit of Linux is a great idea as it is the dominant system that is used for servers.

Using the Shell on Replit

After you switch from the console to the shell in the right panel, you should see something like:

~/PY4E$
This is the Linux command prompt. Lets run your 'hello.py' program from the command line:
~/PY4E$ cd
~/PY4E$ pwd
/home/runner/PY4E
~/PY4E$ ls -l
total 80
-rw-r--r-- 1 runner runner    21 Jul 25 18:16 hello.py
-rw-r--r-- 1 runner runner    20 Jul 25 16:12 main.py
-rw-r--r-- 1 runner runner 62849 Jun 19 04:40 poetry.lock
-rw-r--r-- 1 runner runner   327 Jun 19 04:39 pyproject.toml
-rw-r--r-- 1 runner runner   382 Jul  9 17:22 replit.nix
drwxr-xr-x 1 runner runner    56 Oct 26  2021 venv
~/PY4E$ python3 hello.py
Hello world
Here is what the Linux Shell commands we are typing are doing: We recommend that you get familiar with the Linux bash shell to run your code from the very beginning because eventually you will need to the shell to run more complex programs.

Some Cool Hints on the bash console

You can scroll back through previous commands by pressing the up and down arrows and re-execute commands using the enter key. This can save a lot of typing. If you like keeping your screen uncluttered, you can clear the scroll back buffer by pressing the Command key and the K key at the same time.

Editing Files on Replit

There are three ways to edit files in your Replit environment, ranging from the easiest to the coolest. You only have to edit the file one of these ways.

  1. Go to the main Replit file panel, browse files, navigate to the correct folder and edit the file.
  2. Or in the command line:

    cd ~
    nano hello.py
    

    (The first time you run nano, Replit might need to install it for you)

    Save the File by pressing CTRL-X, Y, and Enter.

  3. Don't try this most difficult and most cool way to edit files on Linux without a helper if it is your first time with the vim text editor.
    cd ~
    vim hello.py
    

    (The first time you run vim, Replit might need to install it for you)

    Once you have opened vim, cursor down where you want to make a change, and press the i key to go into 'INSERT' mode, then type your new text and press the esc key when you are done. To save the file, you type :wq followed by enter. If you get lost press esc :q! enter to get out of the file without saving.

Once you start deploying real applications in production environments like Google, Amazon, Microsoft, etc.. all you will have is command line.

Copyright Creative Commons Attribution 3.0 - Charles R. Severance