Some python tips and tricks that I know might also help you with your coding skills. Its not that complicated all you need is practice.
1) Print The File Path Of Imported Modules
1
2
3
4
5
6
7
8
9
10
| >>> #_1 print the file path of imported modules
>>> import os, socket
>>> print (os)
<module 'os' from 'C:\\Program Files\\Python\\Python37\\lib\\os.py'>
>>> print (socket)
<module 'socket' from 'C:\\Program Files\\Python\\Python37\\lib\\socket.py'>
>>> #adding \n
>>> print (str(os)+'\n'+str(socket))
<module 'os' from 'C:\\Program Files\\Python\\Python37\\lib\\os.py'>
<module 'socket' from 'C:\\Program Files\\Python\\Python37\\lib\\socket.py'>
|
2) In-Place Swapping Of Two Or More Numbers
1
2
3
4
5
6
7
| >>> #_2 in-place swapping of two or more numbers
>>> x, y, z = 10, 20, 30
>>> print(x,y,z)
10 20 30
>>> x, y, z = z, x, y
>>> print (x, y, z)
30 10 20
|
3) Reversing String / Strings In Python
1
2
3
4
5
6
7
| #_3 reversing a string in python
>>> a = "I love to code in Python"
>>> print("Reverse is:-",a[::-1])
Reverse is:- nohtyP ni edoc ot evol I
>>> b = "IMPythonProgrammerThePySnakeBlogger"
>>> print("Reverse is:-",b[::-1])
Reverse is:- reggolBekanSyPehTremmargorPnohtyPMI
|
4) Create A Single String From All The Elements In List
1
2
3
4
5
6
7
8
9
10
| >>> #_4 creating a single string from all the elements in a list
>>> c = ["Python", "Is", "Fun"]
>>> print(" ".join(c))
Python Is Fun
>>> print("-".join(c))
Python-Is-Fun
>>> print("\n".join(c))
Python
Is
Fun
|
5) Chaining Of Comparison Operators
1
2
3
4
5
6
7
8
| >>> #_5 chaining of comparison operators
>>> num = 10
>>> output = 1 < num < 20
>>> print(output)
True
>>> output = 1 > num <= 7
>>> print (output)
False
|
6) Find The Most Frequent Value In A List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| >>> #_6 find the most frequent value in a list
>>> mylist = [1,2,3,4,2,2,3,1,4,4,4]
>>> print(max(set(mylist),key = mylist.count))
4
>>> #lets break it up to understand
>>> mylist = [1,2,3,4,2,2,3,1,4,4,4]
>>> a = set(mylist)
>>> print(a)
{1, 2, 3, 4}
>>> key = mylist.count
>>> print(key)
<built-in method count of list object at 0x03A8FE40>
>>> print(max(a,key=mylist.count))
4
|
7) Check The Memory Usage Of An Object
1
2
3
4
5
| >>> #_7 check the memory usage of an object
>>> import sys
>>> x = 1
>>> print(sys.getsizeof(x))
14
|
8) Print String 'N' Number Of Times
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| >>> #_8 print string 'n' number of times
>>> n = 5
>>> a = "Pysnake Blog"
>>> print (a * n)
Pysnake BlogPysnake BlogPysnake BlogPysnake BlogPysnake Blog
>>> print (a * n+'\n')
Pysnake BlogPysnake BlogPysnake BlogPysnake BlogPysnake Blog
>>> print (a+'\n' * n)
Pysnake Blog
>>> print ((a+'\n') * n)
Pysnake Blog
Pysnake Blog
Pysnake Blog
Pysnake Blog
Pysnake Blog
|
9) Return Multiple Values From Functions
1
2
3
4
5
6
7
| >>> #_9 return multiple values from functions
>>> def x():
return 1,2,3,4
>>> a,b,c,d = x()
>>> print (a,b,c,d)
1 2 3 4
|
10)Use Of Enums In Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| >>> #_10 use of enums in python
>>> class myName:
hello, world, of, python = range(4)
>>> print(myName.hello)
0
>>> print(myName.world)
1
>>> print(myName.of)
2
>>> print(myName.python)
3
>>> print(myName.hello,myName.world,myName.of,myName.python)
0 1 2 3
|
11) Checking If Two Words Are Anagrams
1
2
3
4
5
6
7
8
9
| >>> #_11 checking if two words are anagrams
>>> from collections import Counter
>>> def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
>>> print(is_anagram("python","nohtyp"))
True
>>> print(is_anagram("python","jython"))
False
|
Hope you like the tutorial.
Python is simple and fun to play with and yet very powerful. 🔥