![]() Now, if you wanted to perform filtering, you could do this: x= If you notice what we’ve done here, apart from skipping multiple lines of code, is that we used the if-else statement in the same sentence. We’ve actually created an if else statement to solve this problem, but there is a simpler way of doing things the Pythonic way. Let’s make the previous example a bit more complex, and place a condition that the elements should be multiplied by 2 only if they are even. You might have noticed, we skipped the entire for loop! Example #2 Now, if you were to write the same code in a Pythonic way, you might want to simply use list comprehensions. The result now contains a function which is 2 multiplied by each of the elements. We would then append the solution of the computation into the result. So, what we did here was, we created an empty list to store the results. Example #1 x=Ĭonsider the above code, where you’re trying to multiply some elements, “x” by 2. It’s the official style guide for Python. Writing Pythonic Codeīefore we get into some examples, you might be wondering if there’s a defined way/method of writing Pythonic code. Okay, now that you have a theoretical idea of what Pythonic (and unpythonic) means, let’s have a look at some Pythonic code in practice. If someone said you are writing un-pythonic code, they might actually mean that you are attempting to write Java/C++ code in Python, disregarding the Python idioms and performing a rough transcription rather than an idiomatic translation from the other language. In other words, it means the most widely adopted idioms that are adopted by the Python community. ![]() When people talk about pythonic code, they mean that the code uses Python idioms well, that it’s natural or displays fluency in the language. Here we’re going to understand what the term Pythonic means and why you should be interested in learning how to not just write Python code, rather write Pythonic code. You might have also wondered why the term and whether they’re just talking about writing Python code. If you’re new to Programming, and Python in particular, you might have heard the term Pythonic being brought up at tech conferences, meetups and even at your own office. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |