+ 18

what is pythonic?

i know pythonic is thinking like python is thinking. but what exactly does that mean? it is not the same as the zen - right?

7th Dec 2017, 9:43 PM
Oma Falk
Oma Falk - avatar
15 Answers
+ 41
Yes, indeed :) Simply saying - you can almost always solve a problem in many ways. But 'pythonic' is only when you pick the easiest and most clear way. For example, if you want to print a string letter by letter, you can use a for loop, a while loop and some other solutions. But a for loop seems natural. Within that, you can iterate: for i in range(len(string)): print(string[i]) which will work. But the most pythonic would be: for i in string: print(i)
7th Dec 2017, 10:16 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 19
There is a Python coding convention (PEP8) which outlines that. In general, the code is Pythonic when it is clear to read and it is predictable what it's doing without having to actually run it. The concept is really vague, though, and even if your code is not considered 'pythonic', if it works -- it's fine :)
7th Dec 2017, 9:59 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 10
@Edgar True. Corrected :)
8th Dec 2017, 11:00 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 9
Simplest and Clearest..... for ex: In C++ int temp temp = a; a = b; b = temp; Pythonic way: a, b = b, a
9th Dec 2017, 9:06 PM
Rahul Y
Rahul Y - avatar
+ 8
For me pythonic is when the code uses the structures that are specific to Python (such as list comprehensions or generator expressions). I understand them as closer to the usual human communication. I agree with Kuba's example. I would say that an alternative pythonic way for the loop would also be: [print(i) for i in string] (a human would say to another: "Print all elements of the string" / "For each element of the string, print it") Having first learnt languages like Basic, Pascal, C, it was a bit difficult for me not to code in Python following the habits from the other languages. Since I joined Sololearn, I think I learned to code more pythonic in Python :) My non-pythonic approach of https://www.sololearn.com/Discuss/902040/m-challenge-number-squeezer: https://code.sololearn.com/cyPjvtD8k99D/ A pythonic approach: https://code.sololearn.com/c9vfDMD4YT7p/
8th Dec 2017, 3:06 PM
Cristian Stollberg
Cristian Stollberg - avatar
+ 7
@kuba thanks for the answer. clear to read and no side effects. isn't it a good idea for any language?
7th Dec 2017, 10:04 PM
Oma Falk
Oma Falk - avatar
+ 6
I just came across a quote about Python from McKinney, the creator of Pandas: “I loved it for its economy of expressions. You can express complicated ideas in Python with very little code, and it is very easy to read.” That sounds like a definition for "pythonic".
8th Dec 2017, 8:51 PM
Cristian Stollberg
Cristian Stollberg - avatar
+ 6
pythonic is putting on your spiked helmet with a ale in hand. you and your bros hopping in a longship, sailing off into the great unknown looking for villages to raid. chopping down trees with a flannel shirt on. waking up in the morning and eating spam and bacon, then topping it off with more spam. it could also be interpreted as clean, efficient and explicit code.
11th Dec 2017, 4:22 PM
stephen barter
stephen barter - avatar
+ 5
I'm a coding newbie who is learning all of this stuff. I'm grateful for everything you guys have shared. Thank you.
8th Dec 2017, 3:40 PM
Mike C
Mike C - avatar
+ 5
(Keep it simple and consise.) There is also another term in the Python world and that is Pythonist is someone who lives and breathes this language everyday. Basically speaks and thinks Python.
10th Dec 2017, 8:25 AM
Anthony Perez
+ 5
no I haven't. can't say I've read any fiction in a long time. sounds like a interesting book though. my comment was just a monty python reference. the creator named it after it hence why u see so many references of spam and what not in tutorials/books.
11th Dec 2017, 7:13 PM
stephen barter
stephen barter - avatar
+ 3
You missed the `range` builtin in the first for-loop.
8th Dec 2017, 6:26 AM
Edgar Garrido
Edgar Garrido - avatar
+ 3
The way I see it, being pythonic is the surest way to improve your code's efficiency and optimality by using a more idiomatic syntax than what first comes to mind (like pseudo - code for instance). But you may not always want to be really pythonic if you feel otherwise : it's a matter of style.
10th Dec 2017, 3:05 PM
Nonov Urbiznes
Nonov Urbiznes - avatar
+ 2
@stephen you read Thoreaus Walden?
11th Dec 2017, 6:18 PM
Oma Falk
Oma Falk - avatar
0
It is something greag
11th Dec 2017, 6:28 AM
Ayush Surana
Ayush Surana - avatar