However, learning Python is not an easy thing.

There are thousands of Python courses and tutorials, all claiming to be the best.

True, practice alone is not perfect, but perfect practice is.

I thought I had mastered Python — until I discovered these tricks

), otherwise youll likely end up adopting bad habits that could harm your future lines of code.

What it doesnt do is insist that you follow it against your will.

Readability is important

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

Image for post

Hal Abelson

40% off TNW Conference!

A programming convention is one that a seasoned programmer follows when writing his or her code.

Theres no quicker way to reveal that youre anewbiethan by ignoring conventions.

Article image

Some of these conventions are specific to Python; others are used by computer programmers in all languages.

Look at other examples in theStyle Guide for Python Codeand decide what looks best.

The function is returning a boolean, so why even use if blocks in the first place?

Article image

Adequate use of Whitespace

4.

Docstrings and Comments

The docstrings explain how to use the code :

!

BUG: …

!

Article image

FIX: This is a hack

?

Why is this here?

You will find a detailed PEP dedicated for Doctsrings :Docstring Conventions

5.

Article image

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.

Article image

Instead, do this:

The join () method makes the entire copy in one pass.

When you only process a few strings, it makes no difference.

Here are some techniques for using the join ()method.

Article image

The syntax for splitting a list does the rest.

The [: -1] returns everything except the last value, which we can concatenate with our commas.

We therefore automatically converted the generator to a list before using print.

Article image

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.

Article image

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.

Article image

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.

Article image

Connect with colleagues who are learning like you and dont be afraid to ask questions when you have them.

If you do all of this, nothing can stop you!

So what are you waiting for?

Article image

Start programming in Python now!

Thisarticlewas written byKamal Chouhbiand originally published onTowards Data Science.

you’ve got the option to read the piecehere.

Article image

Also tagged with

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Image for post