10 Top Essential Python Tricks & Tips for Developers

By | December 21, 2019

As compared to other high-level programming languages python has unique syntax unlike other programming languages Python does not use curly brackets and semi-colons, instead, it uses indentation which makes code more readable. Python has many inbuilt and third-party libraries which makes python the most versatile programming language, Python also contains many tricks which comes very handy while writing code, these tricks often used to increase the readability of code and keep the length of the code short.

Python Tricks & Tips

Here in this article, we have provided some of the most important Python tips and tricks, if you want to set a career in Python development you can add these tricks to your python skill inventory.

1. SWAP two Numbers

ten = 10
hundred = 100
ten, hundred = hundred, ten       # swapping hundred with ten
print("hundred is:",hundred)
print("ten is:", ten)

Output:

hundred is: 10
ten is: 100

Note: In other programming languages such as Java and C++ we need a temporary variable to swap two numbers but in but we can do it without a temporary variable.

2. Convert a string into a list

string = "TechGeekBuzz"
print(list(string))

Output:

['T', 'e', 'c', 'h', 'G', 'e', 'e', 'k', 'B', 'u', 'z', 'z']

3. Reverse a String

string = "TechGeekBuzz"
print(string[ : :-1])

Output:

zzuBkeeGhceT

4. Create a string from a list

my_list = ['this', "is", "an", "article","from", "TechGeekBuzz"]
print("-".join(my_list))

Output:

this-is-an-article-from-TechGeekBuzz

5. To print the path of the imported library

import django
print(django)

Output:

<module 'django' from 'C:\\Users\\xyz\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\django\\__init__.py'>

6. Combine two iterable corresponding with their index value.

a = ['one', 'two','three']
b = [1 , 2 , 3 ]
c= zip(a,b)
print(list(c))

Output:

[('one', 1), ('two', 2), ('three', 3)]

7. Get the maximum from an iterable

my_list = [1, 2, 3,200,12,342]
print("Max is:", max(my_list))
print("Minimum value is:",min(my_list))

Output:

Max is: 342
Minimum value is: 1

8. To get the index value of an element

my_list = [1, 2, 3,200,12,342]
print("Index value of 200:", my_list.index(200))

Output:

Index value of 200: 3

9. If else statement in one line

print("TechGeekbuzz") if True else print("Thanks")

Output:

TechGeekbuzz

10. Python List Comprehension

cube = [x ** 3 for x in range(10)]
print(cube)

Output:

[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

11. Set comprehension

cube = {x ** 3 for x in range(10)}
print(cube)

Output

{0, 1, 64, 512, 8, 343, 216, 729, 27, 125}

Note: Unlike list, the set does not store its element in contagious memory location

12. Dictionary Comprehension

cube = {x:x ** 3 for x in range(10)}
print(cube)

Output:

{0: 0, 1: 1, 2: 8, 3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729} 

13. Generators Comprehension

cube = (x ** 3 for x in range(10))
print(cube)

Output:

<generator object <genexpr> at 0x031E9568>

14. Execute a command written in string

string = 'print("The sum of 2+3 is:", 2+3)'
eval(string)

Output:

The sum of 2+3 is: 5

15. Get the Size of an Object

import sys
a= 100
print(sys.getsizeof(a))

Output:

14

16. Print a string n time

string = "Hello World!"
print(string * 8)

Output:

Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!

17. Make a list of length 1 to length n

my_list = [0]
my_list *=10
print(my_list)

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

18. Create a list up to N even numbers

n= 20
my_list = list(range(0,n,2))
print(my_list)

Output:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

You may also Interested In:

Leave a Reply

Your email address will not be published. Required fields are marked *