Python Programming Idioms And Conventions - Part 1

One reason I like using Python is that it's easy to share good practices within your team, once you have learned them yourself.

Here is a first part of randomly presented, idioms and techniques we share in daily Python code, to keep in mind and use when needed.

Multiple assignment a.k.a. Iterable unpacking

Python allows us to do multiple variable assignment like this:

>>> firstname, lastname = "John", "Doe"
>>> firstname
'John'
>>> lastname
'Doe'

Since Python 3, there is the extended form, using *. For example:

>>> numbers = [1, 2, 3, 4, 5, 6]
>>> first, *rest = numbers
>>> first
1
>>> rest
[2, 3, 4, 5, 6]

Let me point you to this well written article by Trey Hunner for more on this technique.

Formatting strings

Strings are everywhere in a common Python program, since text is the most common data type. So we often need to output them.

If you are using Python version before 3.6, you can use the .format() method on strings.

>>> firstname, lastname = "John", "Doe"
>>> s = "Hello {} {}".format(firstname, lastname)
>>> s
'Hello John Doe'

In Python version >= 3.6, we can use F-strings. They are nice to use, they help save space, and they improve code readability.

Here is how you do the same thing as previously using F-strings:

>>> firstname, lastname = "John", "Doe"
>>> s = f"Hello {firstname} {lastname}"
>>> s
'Hello John Doe'

By the way, always remember that a string is a sequence

A string, actually of type str or bytes, is a sequence (some may call it an "array") of characters.

Since it's a sequence, you can do a number of things with a string. Here are some of them as shown below.

  • Get a character in the sequence, using indexing, for example to get the first and the last characters here:
>>> greeting_string = "Hello"
>>> greeting_string[0]
'H'
>>> greeting_string[-1]
'o'
  • Get the length of the string, using the len() function:
>>> greeting_string = "Hello"
>>> len(greeting_string)
5
  • Iterate over it, in a for loop:
>>> greeting_string = "Hello"
>>> for i in greeting_string:
...     print(i)
... 
H
e
l
l
o

Is another string in that string?

To test if another given string is in the string you have at hand, you can use the in operator.

See what you get in the following interpreter session:

>>> greeting_string = "Hello"
>>> "ell" in greeting_string
True
>>> greeting_string = "Hello World"
>>> "lo Wo" in greeting_string
True
>>> "Yo" in greeting_string
False

As a side note, to obtain the same result, you can also use the .find() method that exists on string objects. This method returns the index of the beginning of the "substring" if Python finds it, otherwise -1. For example, you could have this:

>>> greeting_string = "Hello"
>>> greeting_string.find("ell") != -1
True

The _ trick

You can use the _ variable, to do an assignment of a value that you do not care about. That way, you avoid doing an assignment to a normal ("named") variable that you do not intent to use anyway. To make things clear, let's see the following example:

>>> name = "John Doe"
>>> firstname, _ = name.split(" ")
>>> print(firstname.upper())

It is a convenience so we do not define and carry along variables that we are using later in the code. And it is practical when calling a function that would return a tuple, when we do not need some members of the tuple.

>>> three_word_sentence = "Python is great"
>>> first, _, _ = three_word_sentence.split(" ")
>>> first
'Python'

Conclusion

Through these idioms, writing Python code is fun and in many cases the code shows your intent.

That's all for Part 1 of this series. Keep following us to discover other interesting techniques we use everyday.

comments powered by Disqus

Need help for your project?

Our team can contribute to your project, working on a specific task, or doing all the coding based on your specifications, using Python, a web framework such as Django or a CMS.