Submit a blog to KDnuggets -- Top Blogs Win A Reward

Topics: AI | Data Science | Data Visualization | Deep Learning | Machine Learning | NLP | Python | R | Statistics

KDnuggets Home » News » 2021 » Jun » Tutorials, Overviews » 10 Python Code Snippets We Should All Know ( 21:n24 )

10 Python Code Snippets We Should All Know


Check out these Python code snippets and start using them to solve everyday problems.



By Pralabh Saxena, Software Developer

Figure
Image by Johnson Martin from Pixabay

 

Python has seen enormous growth in its user base over recent years. Beginner programmers are opting for Python as their first language seeing its simple syntax and its applications.

In this article, I’ll share some Python code snippets that can be used to solve day-to-day problems. Let’s get started!

 

1. Merge two dictionaries

 
Merging multiple dictionaries has become easier after Python 3.5. We can merge multiple dictionaries in a single line using (**) operator. We need to pass dictionaries in {} along with (**) operator, and it's done.


Syntax: {**dictionary1, **dictionary2}


Output:Merged dictionary: {'name': 'Joy', 'age': 25, 'city': 'New York'}


 

2. Chain Comparison

 
This snippet allows you to make multiple comparisons with all kinds of the operator in a single line.

Output:True
False


 

3. Print a String N times

 
We can use this code snippet to output a string N times without using any loop.

Output:
Hello!Hello!Hello!Hello!Hello!


 

4. Checking if a File Exists

 
While working with file handling and any other operation, it is necessary to know if the file we are using in our code exists. Using this code snippet, we can know whether the file exists or not in our directory or given path.

Output:Does file exist: False


 

5. Retrieve the Last Element of a List

 
We can use the following ways to retrieve the last element from the list.

 

6. List Comprehension

 
List comprehension can be used to create a new list based on the elements of an existing list in just a single line of code.

Output:Vowels are:  ['i', 'i', 'o', 'e', 'a', 'o', 'i']


 

7. Calculate Code Execution Time

 
We can use the time library to calculate the time taken to execute a particular code.

Output:Sum: 45
Time:  0.0009965896606445312


 

8. Find Element with Most Occurrence

 
This code snippet returns the most frequent item that appears in the list.

Output:most frequent item is: 2


 

9. Convert Two Lists into a Dictionary

 
This code snippet can be used to convert two lists into a dictionary. In this method, we take two lists as input values. The first list will be the key of the dictionary, and values from the other list will be values of the dictionary.

Output:{1: 'one', 2: 'two', 3: 'three'}


 

10. Error Handling

 
Like other programming languages, Python also provides a way to handle exceptions using tryexcept and finally block.

Output:Can not divide by zero
Executing finally block


 

Conclusion

 
That’s all from this article. We have discussed some code snippets that I find extremely useful and can be used in everyday problems. You can use these snippets while day-to-day coding and competitive programming problems to speed up your work and make your code faster.

Thanks for reading!

For an additional 12 snippets, see the original article.

 

Level Up Coding

 
Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

 
Coding Interview Questions + Land Your Dev Job | Skilled.dev
The course to master the coding interview

 
Bio: Pralabh Saxena is a software developer with 1 year of experience. Pralabh writes articles on topics such as Python, Machine Learning, Data Science, and SQL.

Original. Reposted with permission.

Related:


Sign Up

By subscribing you accept KDnuggets Privacy Policy