Strings and arrays can be manipulated in a number of ways in python. Splitting a string or an array into sections can be done using the section slicing that is built into the core python syntax and uses the colon (:) symbol.
Generating a simple array in python is straightforward, the following creates an array of 9 elements.
When starting out learning Python you will probably start by looking at the command line a lot. This is understandable as it's important to learn about variables, conditionals and loops and the command line is the best way to start out with the language.
I have always found it useful to quickly introduce graphical user interfaces when learning programming as it allows people learning programming to have something they can engage with. Being able to see the output of their program in real, tangible things that they can interact with.
There are a couple of different libraries in Python that can be used to create user interfaces, and perhaps the first one Python developers tend to learn is Tkinter or the "Tk interface" module. This is a Python binding to the Tcl/Tk toolkit to create graphical user interface.
JSON is a very common data format, but reading it can be a little difficult, especially if the JSON contains very little white space. If you have Python 2.6 or above you have use the json.tool to format the JSON so that you can read it correctly. This is also a good way to validate JSON strings that you have had to hand edit before they cause errors upstream.
If you have a file called file.json, which contains a bunch of JSON output, you can use Python to format this into a readable structure in the following way.
python -m json.tool file.json
You can also write this output directly into a file, in this case called formatted.json.
python -m json.tool file.json > formatted.json
The most common use of this tool is to pipe JSON to it from the command line (either directly or via a script).
The other day I was inspired by a story on Reddit about a guy who had created a Python script to automatically play the olympic hurdles Google Doodle. The Python script just passed the correct keyboard commands to the game so that the game was finished in 1.5 seconds, earning a gold medal. The problem was that his script was specifically for Windows, so I set about trying to create a Linux version that I could run on Kubuntu.
After a bit of research I found a decent plugin that sends keyboard commands through Python called uinput. To get this installed I had to add the following lines to /etc/apt/sources.list.
Python doesn't officially support multi-line comments, but there is a way of implementing the same functionality using an existing language construct. Single line comments in Python are written like this:
# This is a single line comment.
Multi-line comments are not officially supported in Python. That said, you can create the same effect in Python by using a multi-line string. Unless it is part of a docstring at the start of a class, function or module then it parsed into a string variable, but nothing is done with it.
I am currently in the process of learning Python, so I thought I would start a series of mini blog posts detailing different things that I have found useful whilst learning how to use the language.
To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program running. It is the most reliable, cross-platform way of stopping code execution. Here is a simple example.