Getting cozy with Python in your Kali environment

There are two primary components you'll use during Python development: the interactive interpreter and the editor. The interpreter is called up with a simple command:

# python

The interpreter is just what it sounds like: it will interpret Python code on the fly. This is a real time-saver when you're coding, as you can, for instance, check your formula without closing out the editor and running the code, looking for the line in question.

In this example, we issued print "Hello, world!" and the interpreter simply printed the string. I then try a formula, getting a weird rounded integer; I try again with decimal-zero and get the answer I was expecting. Thus, I experimented with my formula and learned a little about Python without needing to write this out and run it:

It should come as no surprise to learn that most Python coders work on their projects with two screens open: the interpreter and the editor. The interpreter is built into the Python installation; what you get when you punch in python and hit return is what people will use. The editor, on the other hand, can be a personal choice. And once again, opinions in this arena can be passionate! 

The editor is just a text editor; technically, a Python file is text. I could write up a Python script with Windows Notepad and it would work fine—but I wouldn't recommend it. (Telling people that's how you code would be a fun way to get weird looks.) If it's just a text editor, what's the big deal? The main feature you're looking for in an editor is syntax awareness—the editor understands the language you're typing in and displays the syntax in a distinctive way for you. It turns text that just happens to be Python into a living piece of code, and it makes your life a lot easier.

The tiniest of errors—such as forgetting a single closing quotation mark—stick out like a sore thumb as the editor tries to understand your syntax. There are several great options for syntax-aware editors; some of the popular ones are Notepad++, gedit, nano, Kate, Vim, and so on. Now, the more serious developer will probably use an integrated development environment (IDE), which is a more comprehensive solution for understanding what your code is doing, but also assists in writing it. An IDE may have a debugger and a class browser, for example, whereas the editor will not. There are many IDEs to choose from, most of them free with commercial versions and supporting a variety of operating systems; a couple good ones are Wing IDE and PyCharm.

IDEs are cool, but please note that we won't be working in one for our purposes here. I recommend you get familiar with your favorite IDE, but our objective here is one of minimalism and flexibility. Having a cozy IDE setup is the kind of thing you have on a designated machine, which will be fantastic for writing up a new tool set to carry around with you on your assignments. The context of our discussion here, on the other hand, is writing up Python scripts on a bare-bones machine where having your favorite IDE may not be practical. Being able to get by with just a plain Python install plus an editor is more important than learning an IDE, so I encourage you to master one outside of this book. For now, we're going to proceed with an editor that's ready to go on just about any Linux box and should natively understand Python syntax. My choice of editor may cause some readers to literally burn this book with fire, and other readers will cheer. Yes, I'm going to work with Vim.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.36.141