Some tricks in Python

Audio version of the article

Python is one of the most popular programming languages ​​for beginner developers, making it the most widely taught language in schools around the world.

However, learning Python is not an easy thing. To get started, you first need to find the best online way to get there, which is difficult in itself. There are thousands of Python courses and tutorials, all claiming to be the best.

True, practice alone is not perfect, but perfect practice is. This means you need to make sure you’re always following the best coding practices (commenting on your code, using correct syntax, etc.), otherwise you’ll likely end up adopting bad habits that could harm your future lines of code.

A universal convention supplies all of maintainability, clarity, consistency, and a foundation for good programming habits too. What it doesn’t do is insist that you follow it against your will. That’s Python! — Tim Peters on comp.lang.python, 2001–06–16

In this article, I’m going to give my top 10 tips to help you code in Python quickly and efficiently.

1. Readability is important

Programs must be written for people to read, and only incidentally for machines to execute.

First of all, try to make your programs easy to read by following some programming conventions. A programming convention is one that a seasoned programmer follows when writing his or her code. There’s no quicker way to reveal that you’re a “newbie” than by ignoring conventions. Some of these conventions are specific to Python; others are used by computer programmers in all languages.

Essentially, readability is the characteristic which specifies how easy another person can understand some parts of your code (and not you!).

As an example, I was not used to write with vertical alignment and to align the function’s parameters with opening delimiter.

Look at other examples in the  and decide what looks best.

Another important thing that we do very often is resembling programs that we have seen or written before, which is why our exposure to readable programs is important in learning programming.

2. Avoid unuseful conditions

Often, a long if & elif & …. & else conditions is the sign of code that needs refactoring, these conditions make your code lengthy and really hard to interpret. Sometimes they can easily be replaced, for example, I used to do the following:

This is just dumb! The function is returning a boolean, so why even use if blocks in the first place? The correct what of doing this would be:

In a  challenge, you are given the year and you have to write a function to check if the year is leap or not. In the Gregorian calendar three criteria must be taken into account to identify leap years:

  • The year can be evenly divided by 4, is a leap year, unless:
  • The year can be evenly divided by 100, it is NOT a leap year, unless:
  • The year is also evenly divisible by 400. Then it is a leap year.

So in this challenge, forget about ifs and elses and just do the following:

3. Adequate use of Whitespace

  • Never mix tabs and spaces
  • A line break between functions
  • Two line breaks between classes
  • Add a space after “,” in dictionaries, lists, tuples, arguments in a list of arguments and after “:” in dictionaries but not before.
  • Put spaces around assignments and comparisons (except for arguments in a list)
  • No space for opening / closing parentheses or just before a list of arguments.

4. Docstrings and Comments

  • Docstrings = How to use the code
  • Comments = Why (rational) and how the code works

The docstrings explain how to use the code :

  • Explain the purpose of a function even if it seems obvious to you because it will not necessarily seem obvious to another person later.
  • Describe the expected parameters, the returned values ​​and the exceptions raised.
  • If the method is strongly coupled to a single caller, mention the calling function.

The comments explain what are for the maintainers of your code. Examples including notes for yourself, such as:

# !!! BUG: …

# !!! FIX: This is a hack

# ??? Why is this here?

It is on your responsibility to write good docstrings and good comments, so always keep them up to date! When making changes, make sure the comments and docstrings are consistent with the code.

You will find a detailed PEP dedicated for Doctsrings : 

5. Variables and Assignment

In other programming languages :

In Python, it’s better to use the assignment in one line code :

You may have already seen it but do you know how it works?

  • The comma is the syntax for building the tuple.
  • A tuple is created on the right (tuple packing).
  • A tuple is the target on the left (tuple unpacking).

Other examples:

Useful in loops on structured data (the variable user above has been kept):

It is also possible to do the opposite way, just make sure you have the same structure on the right and on the left:

6. List Concatenation & Join

Let’s start with a list of strings:

We want to concatenate these chains together to create a long one. Particularly when the number of substrings is large, avoid doing this :

It is very slow. It uses a lot of memory and performance. The sum will add up, store, and then move on to each intermediate step.

Instead, do this:

or a comma and a space:

To make a grammatically correct sentence, we want commas between each value except the last one, where we prefer an “or”. The syntax for splitting a list does the rest. The [: -1] returns everything except the last value, which we can concatenate with our commas.

7. Test true conditions

It is elegant and quick to take advantage of Python with regard to Boolean values:

8. Use enumerate when it’s possible

The enumerate function takes a list and returns pairs (index, item):

It is necessary to use a list to display the results because enumerate is a lazy function, generating one item (a pair) at a time, only when requested. A for loop requires such a mechanism. Print does not take one result at a time but must be in possession of the entire message to be displayed. We therefore automatically converted the generator to a list before using print.

So, using the loop below is much better:

The version with enumerate is shorter and simpler than the two other versions. An example showing that the enumerate function returns an iterator (a generator is a kind of iterator)

9. List Comprehension

The traditional way with for and if:

Using a list comprehension:

The listcomps are clear and direct. You can have several for loops and if conditions within the same listcomp, but beyond two or three, or if the conditions are complex, I suggest you use the usual for loop.

For example, the list of squares from 0 to 9:

The list of odd numbers within the previous list:

Another example:

10. Generator expressions

Let’s sum the squares of numbers less than 100:

We can also use the sum function which does the job faster for us by building the right sequence.

The generator expressions are like list comprehensions, except in their calculation, they are lazy. Listcomps calculate the entire result in a single pass, to store it in a list. Generator expressions calculate one value at a time, when necessary. This is particularly useful when the sequence is very long and the generated list is only an intermediate step and not the final result.

For example if we have to sum the squares of several billion integers, we will reach a saturation of the memory with a list comprehension, but the generator expressions will not have any problem. Well it will take a while though!

The difference in syntax is that listcomps have square brackets, while generator expressions do not. Generator expressions sometimes require parentheses, so you should always use them.

In short :

  • Use a list comprehension when the expected result is the list.
  • Use a generator expression when the list is only an intermediate result.

In this article, I have presented some of my best tips for learning to program in Python. If you really want to become a programmer or add a coding skill to your skills, learning Python is a great place to start. Look for high quality Python training online and start to find out how to program in Python. I recommend that you learn the basics with an interactive course before moving on to more difficult concepts.

You should not speed up the learning process too much, or you may miss important information. Take notes and make sure to review them regularly and try to practice writing code as often as possible.

Connect with colleagues who are learning like you and don’t be afraid to ask questions when you have them. Helping others when they have problems can be a great review, and working with someone else’s code is a great way to learn new things.

If you do all of this, nothing can stop you! So what are you waiting for? Start programming in Python now!

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link

- Advertisment -

Most Popular

Understanding Nearest Centroids linear classification in Machine Learning

Nearest Centroids is a linear classification machine learning algorithm. It involves predicting a class label for new examples based on which class-based centroid the example...

Development careers that use Python

Over the past few decades, several programming languages ​​have been invented, and many have gained space in the development market. JavaScript, C ++, Swift,...

Data Science Strategies for post Trump Era

Data can be a source of comfort and confidence as the pandemic rages on and the Trump Administration comes to a close. Now that the...

Upcoming opportunities in Crypto

A record-breaking year for DeFi, but can it break away from the negative perceptions around yield farming and pump-and-dump schemes? While 2020 will go down...

Introductory Guide on XCFramework and Swift Package

In WWDC 2019, Apple announced a brand new feature for Xcode 11; the capability to create a new kind of binary frameworks with a special format...

Understanding Self Service Data Management

https://dts.podtrac.com/redirect.mp3/www.dataengineeringpodcast.com/podlove/file/704/s/webplayer/c/episode/Episode-159-Isima.mp3 Summary The core mission of data engineers is to provide the business with a way to ask and answer questions of their data. This often...
- Advertisment -