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.

--

--