10 Things Every Python Developer Should know about
Last Updated on July 21, 2022 by Editorial Team
Author(s): Rijul Singh Malik
Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.
A blog covering best practices inΒ python.
1. Using βwithβ for cleanerΒ code.
Python is a beautiful language, but itβs not necessarily very pretty. For example, what is the code below supposed to do? It looks like itβs setting the variable x to 1, but itβs actually setting it to True. It goes on to print βx is trueβ which is not the same as βx is set to Trueβ. If you donβt believe me, try running the following code in your Python interpreter: x =Β True
Python is a versatile programming language that supports a wide range of applications. This versatility makes it a good choice for many different types of projects, but the learning curve can be steep. When you first start out with the language, it can be difficult to get used to the syntax. In this article, weβll take a look at one of Pythonβs most common syntax constructs, the with the statement, and show you how it can help make your code more readable.
2. Customizing print() for smarter debugging.
Pythonβs print() function can be used in a multitude of ways to display information to the user. It can display output as a series of characters or as formatted text. However, print() is essentially a wrapper for the sys.stdout.write() function, which means that it can be used to write to any stream accessible through the sys module. In this article, we will explore the different ways in which you can useΒ print().
It is common for Python developers to use print() for debugging. However, when itβs time to write code for a production server, itβs recommended that you use logging instead. The advantage of logging is that it can be written to disk, or set to be sent to a remote server. However, debugging print() is a great way to learn how to use logging, so letβs build a logging framework that uses print() behind the scenes. Letβs look at how to use print() in a custom logging framework.
3. Using the nonlocal keyword for more flexibleΒ loops.
The Python programming language includes a keyword that Python programmers use relatively infrequently but can be quite useful: nonlocalΒ . The purpose of this blog is to provide some examples of how you can use the nonlocal keyword for more flexible code. This blog is going to assume that you already know how to use the for loop in Python. If you donβt, the Python Tutorial has a good explanation. Specifically, this blog is going to focus on the use of the nonlocal keyword within forΒ loops.
Python comes with a powerful set of built-in tools. But its powerful syntax can also lead to some confusion. Lots of developers have questions about how certain patterns work in the language. For example, what is the difference between the global and nonlocal keywords? Which should you use when? The short answer is that you should use nonlocal. Itβs a simpler and more flexibleΒ keyword.
4. Using proper nouns to make your code more readable.
There are a lot of things that make Python unique, one of them being the way it handles the names of objects. Python is the only programming language that uses a proper noun approach to naming. In other languages, the names of objects usually contain only alphanumeric characters, with a few exceptions. Python, on the other hand, allows you to use any alphanumeric characters and any of the following punctuation symbols: _.!@$%^&*()_+-={}[]|:;ββ<>,.?/
5. Using βoperatorβ for betterΒ math.
Operators in Python can help you do more math with less code. If you need to add two numbers, you donβt have to write it like this: a = 5 b = 6 c = a + b You can use the + operator and write it like this: c = 5 + 6 This is the same thing, but using the operator. Here are all the operators you can use: / \ % //Β **
6. Using xrange() for cleanerΒ loops.
The range() function returns a list of integers within a given range. This can be useful in many situations when you need to iterate over a range of numbers, but the list returned by range() is not very memory efficientβββitβs a list, after all. The xrange() function is similar to range(), but it returns an xrange object which is more memory efficient. xrange() is the Python 2 solution to this problem, but the Python 3 solution is the range() function.
7. Using functools.lru_cache() to cache your most-used objects.
Python is a dynamically-typed programming language that is famous for its readability, flexibility, and ease of use, especially for beginners. With Python, beginners can get started with just a few lines of code and begin creating applications that run on both desktops and servers. However, there are certain things that even experienced Python developers need to learn a few tricks for. In this blog, I will be talking about one such concept: how to make use of functools.lru_cache() to cache your most-used objects.
Python is a great language that allows you to do a lot of amazing things. However, with the sheer amount of different libraries and frameworks, it can be hard to keep up with all the new developments. On top of that, there are some things that you may not even think about but could drastically improve your code and your application. Here are some of the lesser-known best practices that Python developers should knowΒ about.
8. Slicing collections the smartΒ way.
Python is a beautiful language, but it is not perfect. One of the most useful and frequently used functions in the python language is slicing. Slicing enables an object to be treated as a sequence of items. This means you can iterate over the items in a collection, access individual items by their index, and more. This can be helpful for creating lists, arrays, and dictionaries. However, slicing is also one of the most misused and misunderstood tools in the Python language. This article will walk through the different ways slicing can be used, as well as show some common pitfalls.
Python is interpreted, interactive, and object-oriented programming language. Python is easy to learn with its simple syntax and English-like keywords. It has classes and methods, but it does not have static typing. Python is a perfect language for beginners. It is a language for professionals. It has a massive community and thousands of third-party modules. It is used for many different purposes: web programming, network programming, software development, gaming, scientific programming, GUI development, Artificial Intelligence, and Machine Learning, Image Processing, and so on. This blog will give you insights into Python programming. We will learn the best practices of programming in Python and common idioms. We will also see examples of PythonΒ code.
9. Using tuple unpacking with *args and **kwargs to write more flexible functions.
I was reading the recent list of things every Python developer should know about and I was inspired to write a blog post on how to use tuple unpacking with *args and **kwargsΒ . This is a very handy technique I have used in the past to handle passing in an arbitrary number of arguments to functions. To illustrate this I will write a simple function that takes in an arbitrary number of arguments and returns the sum of those arguments.
10. Using the super() keyword for better inheritance.
Inheritance is a way to re-use code from a parent class in a child class. Itβs a common programming practice that lets you reuse code by separating it into individual parts and putting them together. Hereβs an example of how you can use inheritance to solve real-world programming problems. Letβs say youβre working on a project that has a User class, which has some properties like first nameΒ , last nameΒ , and emailΒ . Maybe the requirement for this project is that if you want to create a new user, you first have to create a new project, and then you can add users to that project. You can use inheritance (along with a little bit of Python magic) to create a new Project class that inherits from the User class and requires users first to have a project before it can beΒ created.
Conclusion:
This list is a great reference guide to get you startedΒ with
10 Things Every Python Developer Should know about was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Join thousands of data leaders on the AI newsletter. Itβs free, we donβt spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI