Title: 5
Course: Start programming with Python in 10 steps

Get out of your notebooks

Jupyter notebooks are great for learning how to program and for documenting your steps in a data analysis that includes programming. But programming is actually most useful when you work with large files and with many of them. Your Jupyter app that runs your notebooks will become too tight once you start working with real data. Not only your notebook, your computer will be too tight and you will need to run your programs on a server. This is why you will have to learn how to run your programs from a command line (from a shell) at some point. Why not now! The good news is that, once you know how to work in a shell, connecting to a server and working there is really easy! Another good news is that you will look really cool (🕶️) in the command line mode.

👉    8 things to know at this step

1. Command line is a powerful interface that allows you to gain more control over computers. It is often described as a text interface, as opposed to the graphical user interface (GUI), because you do everything by typing things.
2. To enter the command line mode, you use an app that exists on all computers, but may be called differently. Most often, this app is called Terminal and it is actually part of the GUI, which means that you can start it by clicking on it.
3. A lot of things that you type in the command line are equivalent to clicking in GUI (e.g. moving through folders, opening files, starting programs), but you can do much more with the command line than with GUI.
4. Security becomes a much more important issue in the command line than in GUI, this is why there are different levels of power and authorisations. By default, you cannot access the parts of the computer where you can cause serious damage.
5. Locations of files and programs also become much more important in the command line: you have to know where you are, where your programs are and where your files are. Locations are, in fact, paths from the root of the computer file system to specific folders and files.
6. A computer file system is a tree and a path is a sequence of all nodes between the root and the target file or folder. The exact structure of the tree and the format of paths can be slightly different on different operating systems.
7. Directories are folders which contain files. Programs are special files, they are executable. To run your program from the command line, you need to write it in an editor and save it as a file with the suffix .py (you choose the name!). Once saved like this, your program becomes a script. Most of the people write their scripts in a GUI editor (Sublime Text is really sublime, Notepad++ will do the job on Windows), but run them from the command line.
8. If you have an account (user name and password) on a server, you can connect to the server’s Terminal. In most cases, this Terminal works the same as the command line of your computer.

🧑‍🎓    Learn actively

🛠️   Get yourself a nice text editor: Sublime Text for Linux, Mac and Windows, Notepad++ for Windows. You can write your code in any editor, but these guys will help you keep things organised and easy to follow.

▶️   Convert all the programs that you wrote in your notebooks up to now into scripts that you can execute, as mentioned in 7️⃣. Do it like this:
  - Open your nice text editor that you just installed.
  - Copy/paste the code cells into the editor; there are no cells here! (watch the indentation!)
  - Save your code naming your file so that it has .py in the end.
  - Do the same for all programs that you wrote so far.

▶️   Find and open your Terminal (Tip: It could also be named differently, like command prompt window; if your computer language isn't set to English, try searching for cmd or terminal.) Once there, you will see a simple screen (black or white, depending on your default settings) and a blinking cursor showing you where to type. You can actually write and test out simple Python code directly in the command line. Here’s a short video showing you how to do just that:

📹  Starting a Python interpreter by Martin Volk

❗  You might need to type python instead of python3 in order to get started.
❗  By typing python here, what we actually did is open the Python interpreter. This made us enter interactive Python. At this point, you're using the command line, but everything you see is still only what's inside the Python interpreter and you can only type Python code.

▶️   Of course, the command line is much more powerful than that: what we really want to do is be able to navigate the entire computer from the Terminal. Before getting to the real deal, though, you need to be at ease with the basics. These are the commands that you will use all day every day working on the command line: pwd, cd, ls, touch, rm, cp, mkdir, mv (and ssh, scp if you're working with a server). Learn how they work from one or both of these sources:

❗  The two resources below use Linux, but everything works the same way if you're using Mac. If you're using Windows, however, you can refer to this GeeksforGeeks cheat sheet to see the corresponding Windows commands. Most commands work similarly despite the different names. The only ones without a direct equivalent on Windows are touch, ssh and scp.

📹  Linux command line essentials on Joseph Delgadillo's YouTube channel (watch Linux command line essentials for pwd, cd and ls, from How to create files using the command line interface (CLI) to Copying, renaming, and removing files for touch, rm, cp, mkdir and mv, and start from Using SSH to access the command line of a remote host for operations on servers)

📖  The Linux Command Line  a book by William Shotts (mostly Chapters 1-6, ssh and scp are in Chapter 16)

❗  Don't forget to press the key "Enter" or "Return" (the one of the two that you have on your keyboard) once you are done typing, otherwise, nothing will happen.

▶️   Take another look at 3️⃣, 5️⃣ and 6️⃣. Do they make more sense? Once you get used to the command line a little, it's time to run your Python scripts. You move to the folder where you saved them (using cd) and watch the following video to find out how to start the Python interpreter and then your script:

📹  How to Run Python Code From the Command-Line on Real Python YouTube channel (start from the beginning if you're using Windows, from 1:50 if you're using Linux, or from 3:09 if you're using Mac)

❗  This video also shows how to write a minimal program with some editors, but you don't need to do this because you already have your programs.
❗  An important note: This might not seem obvious yet, but the command line is really powerful. For instance, you could delete everything from your computer, all files and programs, with a single command! All this without the computer even asking you Are you sure?! This is why, as a beginner, you should stay with the default settings and NOT try anything that requires sudo rights. You don’t need to know much about this for now, but you can refer to Chapter 9 (Permissions) of the book above if you’re interested in the topic.

▶️   As a reward for learning the basics of the command line, you can now save the output of your programs in a file really easily. Check Chapter 6 (Redirection) of the book above!

❗  Another really important note: When typing things, you might press "Enter" accidentally before you are actually done. Most of the time, you will just see an error and your command line will be ready for typing again. If you see the symbol "$" or "%" before your cursor, all is fine and you can act as if nothing happened. If you don't see it, that means that you started some program. Most of the time, you just need to press "Ctrl+c" to interrupt the program and come back to the command line. If you see ">>>", that means that you started Python interactive mode. In this case, you can type "quit()" (then "Enter", of course) to get back to the command line.

✅  If you feel tired of typing the same things over and over again, try your keyboard arrows ⬆️ and ⬇️. They will scroll through your command history and show you what you typed before.
✅  If you would like to check later all the commands that you typed in your Terminal session, you can list the commands with the command "history" and save them in a file just like any other output.

❓  In case you can obtain an account on a server, it is now a good time to try the commands ssh and scp. Just look up the sources above and follow the instructions to connect. Once you are there, copy some of your programs and run them on the server in the same way as you did it on your computer. If you cannot access any server now, just put your mental bookmark here and come back later.

Remember: