Top 10 really useful Python tricks for writing better code

Morris Bundi
3 min readJul 8, 2021

-These tricks will save time and increase your efficiency

The following is a list of really useful python tricks. It is in no particular order.

  1. Underscore place holders
  • Used when working with large numbers. It helps you to keep track of how many digits there are in a number which would be hard by just looking at them. It is the substitute of commas in paperwork as separators.

2. Ternary Conditions

  • Saves you from writing unnecessary code when evaluating something based on a condition being true or false.

3. In place swapping of two numbers.

4. Unpacking

  • Case 1: Using only a certain number of values and ignoring others.
  • Case 2: Telling python to set the remaining values to the last variable

5. Reversing a string

6.Use of Enumerate

  • Used when you want a counter while looping over a list

7. Zip

  • Allows you to loop over 2 lists at once.

8.Create a string from all elements in a list

9. Help

  • This function opens the documentation of a module

10.Getpass

  • It is a module used for inputting sensitive information e.g passwords since users don’t want their passwords displayed on the screen.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Morris Bundi
Morris Bundi

Written by Morris Bundi

Statistics @ UON | ML | DL | Data

Responses (1)

Write a response

Great stuff, keep them coming.