Related Articles

Related Articles

10 Essential Python Tips And Tricks For Programmers
  • Difficulty Level : Easy
  • Last Updated : 31 Mar, 2020

Python is one of the most preferred languages out there. Its brevity and high readability makes it so popular among all programmers.
So here are few of the tips and tricks you can use to bring up your Python programming game.

1. In-Place Swapping Of Two Numbers.

filter_none

edit
close

play_arrow

link
brightness_4
code

x, y = 10, 20
print(x, y)
x, y = y, x
print(x, y)

chevron_right


Output:

10 20
20 10

2. Reversing a string in Python

filter_none

edit
close

play_arrow

link
brightness_4
code

a = "GeeksForGeeks"
print("Reverse is", a[::-1])

chevron_right


Output:



Reverse is skeeGroFskeeG

3. Create a single string from all the elements in list

filter_none

edit
close

play_arrow

link
brightness_4
code

a = ["Geeks", "For", "Geeks"]
print(" ".join(a))

chevron_right


Output:

Geeks For Geeks

4. Chaining Of Comparison Operators.

filter_none

edit
close

play_arrow

link
brightness_4
code

n = 10
result = 1 < n < 20
print(result)
result = 1 > n <= 9
print(result)

chevron_right


Output:

True
False

4. Print The File Path Of Imported Modules.

filter_none

edit
close

play_arrow

link
brightness_4
code

import os
import socket
  
print(os)
print(socket)

chevron_right


Output:

<module 'os' from '/usr/lib/python3.5/os.py'>
<module 'socket' from '/usr/lib/python3.5/socket.py'>

5. Use Of Enums In Python.

filter_none

edit
close

play_arrow

link
brightness_4
code

class MyName:
    Geeks, For, Geeks = range(3)
  
print(MyName.Geeks)
print(MyName.For)
print(MyName.Geeks)

chevron_right


Output:



2
1
2

6. Return Multiple Values From Functions.

filter_none

edit
close

play_arrow

link
brightness_4
code

def x():
    return 1, 2, 3, 4
a, b, c, d = x()
  
print(a, b, c, d)

chevron_right


Output:

1 2 3 4

7. Find The Most Frequent Value In A List.

filter_none

edit
close

play_arrow

link
brightness_4
code

test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]
print(max(set(test), key = test.count))

chevron_right


Output:

4

8. Check The Memory Usage Of An Object.

filter_none

edit
close

play_arrow

link
brightness_4
code

import sys
x = 1
print(sys.getsizeof(x))

chevron_right


Output:

28

9. Print string N times.

filter_none

edit
close

play_arrow

link
brightness_4
code

n = 2
a = "GeeksforGeeks"
print(a * n)

chevron_right


Output:

GeeksforGeeksGeeksforGeeks

10. Checking if two words are anagrams

filter_none

edit
close

play_arrow

link
brightness_4
code

from collections import Counter
def is_anagram(str1, str2):
     return Counter(str1) == Counter(str2)
  
# or without having to import anything 
def is_anagram(str1, str2): 
    return sorted(str1) == sorted(str2) 
  
print(is_anagram('geek', 'eegk'))
print(is_anagram('geek', 'peek'))    

chevron_right


Output:

True
False

References:
1.10 Neat Python Tricks Beginners Should Know
2.30 Essential Python Tips And Tricks For Programmers

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :