python

Python Logo

The Basics Of Object Oriented Programming In Python

19th September 2021 - 22 minutes read time
I have been learning Python recently, and whilst I'm familiar with PHP and other C-like languages, Python has been a bit of a challenge to get used to due to the different syntax. This is especially the case with objects as although Python is object oriented there are a couple of gotchas when creating custom objects. This article sets out how to use classes and objects, but I will assume that you have a basic understanding of object oriented programming in other languages.
Python Logo

A Guide To Using Different Tkinter Widgets In Python

12th September 2021 - 12 minutes read time
The Python Tkinter module has a lot of widgets built in that provide different parts of the graphical user interface. Everything from labels to buttons are all widgets and they extend from the base Widget class.  Some Widgets are easy to put together, but others require a little bit of configuration to get working. This article will show each Tkinter widget available and how to add each one to an application. There are two sorts of Widgets available. Simple widgets exist in their own right, but some widgets are made to contain other widgets.
Python Logo

Slicing Arrays and Strings Using The Colon Operator In Python

5th September 2021 - 6 minutes read time

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.

an_array = [1,2,3,4,5,6,7,8,9]

To grab a particular element of the array you can use the simplest section slicing syntax. The number in the square brackets represents the position of the item to be returned. Remember that the counting starts at 0, so using 1 will return the item from the "second" position.

Python Logo

Getting Started Creating Graphical User Interfaces With Python And Tkinter

29th August 2021 - 10 minutes read time

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.

Python Logo

Pretty Print JSON With Python

23rd December 2014 - 3 minutes read time

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).

Python Logo

Multi-line Comments In Python

13th April 2012 - 2 minutes read time

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.

'''
This is
a multiline
comment
'''

Because this is a multi-line string it is therefore parsed by Python, so you will need to be careful what you put in it.

Python Logo

Stopping Code Execution In Python

13th April 2012 - 2 minutes read time

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.