This content originally appeared on DEV Community and was authored by codeifys
6 magical Python Tips And Tricks For Programmers
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.
x, y = 10 , 20 print (x, y) x, y = y, x print (x, y) |
10 20
20 10
2. Reversing a string in Python
a = "codelivly" print ( "Reverse is" , a[:: - 1 ]) |
Reverse is ylviledoc
3. Create a single string from all the elements in list
a = [ "Coding" , "is" , "life" ] print ( " " .join(a)) |
Coding is life
4. Chaining Of Comparison Operators.
n = 10 result = 1 < n < 20 print (result) result = 1 > n < = 9 print (result) |
True
False
4. Print The File Path Of Imported Modules.
import os import socket print (os) print (socket) |
<module 'os' from '/usr/lib/python3.5/os.py'>
<module 'socket' from '/usr/lib/python3.5/socket.py'>
5. Use Of Enums In Python.
class MyName: Coding, is, life = range ( 3 ) print (MyName.Coding) print (MyName.is) print (MyName.life) |
2
1
2
6. 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
Article Was Originally Published to :
10 Magical Python Tips For Every Developer
This content originally appeared on DEV Community and was authored by codeifys
codeifys | Sciencx (2021-08-10T09:29:25+00:00) 6 Magical Python Tips For Developer. Retrieved from https://www.scien.cx/2021/08/10/6-magical-python-tips-for-developer/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.