Programming
Top 10 Useful Python Tricks For Writing Better Code
-These tricks will save time and increase your efficiency
![](https://miro.medium.com/max/842/1*FtlJkl3RhlYIaFly8szOBA.png)
The following is a list of really useful python tricks. It is in no particular order.
- 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.
![](https://miro.medium.com/max/1000/1*Ny7FkBxBN92fGwy9GasNig.png)
2. Ternary Conditions
- Saves you from writing unnecessary code when evaluating something based on a condition being true or false.
![](https://miro.medium.com/max/1032/1*hCAuJ5sXmGaY_Ejtvp1S4A.png)
3. In place swapping of two numbers.
![](https://miro.medium.com/max/1212/1*qUoRwxXzM9akk1Dz-N9KzA.png)
4. Unpacking
- Case 1: Using only a certain number of values and ignoring others.
![](https://miro.medium.com/max/990/1*KjHeBaPRAPmPev8yDB3WNg.png)
- Case 2: Telling python to set the remaining values to the last variable
![](https://miro.medium.com/max/1146/1*kCDRrVH-X9F0nuldWB6s_g.png)
5. Reversing a string
![](https://miro.medium.com/max/1400/1*yZQAMPKBlrBDpCrerftxhQ.png)
6.Use of Enumerate
- Used when you want a counter while looping over a list
![](https://miro.medium.com/max/1400/1*3BVT7R4gWMERCqU0hJxr9A.png)
7. Zip
- Allows you to loop over 2 lists at once.
![](https://miro.medium.com/max/1272/1*pXl_4imyBzWy4arZf8jGMQ.png)
8.Create a string from all elements in a list
![](https://miro.medium.com/max/1400/1*eh0gqZ4Bhei2MyZ0ycZyoQ.png)
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.
- Morris Bundi
- Mar, 31 2022
Add New Comments
Please login in order to make a comment.