10 Magical Python Tips For Every Developer

Python is one of the most widely used programming languages today. It’s not just a language; it’s a way of doing things properly, simply, and concisely. Python is one of the most well-known high-level programming languages, particularly in the open source community. Python is a huge programming language. The point is that the more you learn about Python programming, the more you’ll realize how much you still don’t know. I realize the statement is ironic, but Python is like that.

Python programming is a fast, compact programming language that can be easily converted into any operating system. Furthermore, if you look around, you will notice that the bulk of things are written in Python; examples include Google and YouTube. Django and Pyramid are two frameworks that only support Python. Micro-frameworks exist as well, such as Flask and Bottle. Many internet protocols are included in Python’s standard library, including HTML, XML, JSON, email processing, FTP, IMAP support, and its simple Socket interface.Python is most commonly used for scientific and numerical computations. For example, SciPy, Pandas, and iPython.Now that you know what Python is and how it works, let’s look at some of the reasons why I prefer Python to any other programming language. I’ve been working with Python for a long time. Long enough to appreciate the value of both fundamental and sophisticated Python language interpretations. Even those who have worked with Python for a long time will occasionally forget some of the most fundamental aspects of the language. So, to refresh your memory, I’ve created this post.

These are a few tips I’ve picked up over the last couple of years and kept in a text file. So, whenever I am stuck on a piece of code, I just look at these things, and most of the time, my problems are addressed.

1. Condition Inside the print Function

Have you ever considered writing all of the conditions inside the print function and then printing the output based on the criteria? This is how you can accomplish it:

print("odd" if int(input("Enter a num: "))%2 else "even")

Output:

Code demo

2. Conditional List — Any

We can use the same conditional list using Python’s any function to see if anyone’s condition is satisfied.

score=200
wickets=7
catch=4
list_cond=[score>320,
wickets<8, catch>3]
if(any(list_cond)):
print(“Win”)
else:
print(“Lose”)

Output:

Code demo

3. Conditional List — All

This method works wonders when you need to add conditions dynamically. When the number of conditions grows and you have to type them manually, this approach can even become a stress reliever.
All methods can be used to match all conditions or perform work in place of the and condition.

1score=325
2wickets=7
3catch=4
4list_cond=[score>320,
5wickets<8,
6catch>3]
7if(all(list_cond)):
8print(“Codelivly”)
9else:
10print(“Lose”)

4. Multiple Inputs in One Go

The normal way of coding the input() method takes far too long to process the input. Here’s another approach to accomplish the same goal:

a,b,c=input("Enter three inputs ").split()

Output:

Code demo

5. Take Out the Duplicate Data

This method represents in useful whenever competing in provides the feature or working with database data in Python. In Python, we can eliminate duplicate data by combining the list and set methods:

li=[1,5,8,6,5,9,6,9,5,6,9,6,5,4]
print(list(set(li)))

Output:

Code demo

6. Swap Like a Pro

Using a temporary variable is one method to swap variables. The following is a better approach to do it in Python:

salary=85000
exp=3salary,exp=exp,salary
print(exp)

Output:

7. Most Repeated Object

We can use the following line of code to see which object in your list repeats the most. To discover the most repeated element, this method employs the max function in conjunction with the key attribute:

li=[1,5,8,6,5,9,6,9,5,6,9,6,5,4,"a","a","b","b","a","a","a"]
print(max(set(li), key=li.count))

Output:

Code demo

8. Reverse Like a Pro

This is a simple yet useful method for reversing any string:

name="Pranjal Saxena"
print(name[::-1])

Output:

Code demo

9. Infinite Input Arguments

When we don’t know the function length of the input parameter, this approach comes in helpful. With a variable name that accepts a list of input, we can use the * sign:

1def fun1(*a):
res=1
2for ele in a:
3res*=ele
4return res
5ans=fun1(5,6,9,6,9,6,6)
6print(ans)

10. Magic of List Comprehension

This algorithm converts most lines of code into a single line. With this technique, we may loop through a list while simultaneously verifying any condition with a single line of code.

li=[i**3 for i in range(1,15) if i%2==0]
print(li)

Output:

Code demo

Conclusion

This concludes the article. We’ve gone over ten Python tricks that will make you go, “Wow!”
You can make your Python code more optimized and productive if you use these strategies. At some point, these techniques may be useful in competitive programming.

I hope you found the article interesting. Keep an eye out for more fascinating articles.
Thank you for taking the time to read this!

Also Check:

Happy Coding…. ☺️

About us: Codelivly is a platform designed to help newbie developer to find the proper guide and connect to training from basics to advance .

Leave a Comment