Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the important language features (idioms) of Python to learn early on [duplicate]

Tags:

python

idioms

I would be interested in knowing what the StackOverflow community thinks are the important language features (idioms) of Python. Features that would define a programmer as Pythonic.

Python (pythonic) idiom - "code expression" that is natural or characteristic to the language Python.

Plus, Which idioms should all Python programmers learn early on?

Thanks in advance

Related:

  • Code Like a Pythonista: Idiomatic Python
  • Python: Am I missing something?
like image 513
John Burley Avatar asked Feb 19 '09 21:02

John Burley


People also ask

What are Python idioms?

Idiomatic Python is what you write when the only thing you're struggling with is the right way to solve your problem, and you're not struggling with the programming language or some weird library error or a nasty data retrieval issue or something else extraneous to your real problem.

What language is Python written in?

Python is written in C (actually the default implementation is called CPython).

What is unique about Python?

The python language is one of the most accessible programming languages available because it has simplified syntax and not complicated, which gives more emphasis on natural language. Due to its ease of learning and usage, python codes can be easily written and executed much faster than other programming languages.

What is the pythonic way?

In short, “pythonic” describes a coding style that leverages Python's unique features to write code that is readable and beautiful.


1 Answers

Python is a language that can be described as:

"rules you can fit in the palm of your hand with a huge bag of hooks".

Nearly everything in python follows the same simple standards. Everything is accessible, changeable, and tweakable. There are very few language level elements.

Take for example, the len(data) builtin function. len(data) works by simply checking for a data.__len__() method, and then calls it and returns the value. That way, len() can work on any object that implements a __len__() method.


Start by learning about the types and basic syntax:

  1. Dynamic Strongly Typed Languages
  2. bool, int, float, string, list, tuple, dict, set
  3. statements, indenting, "everything is an object"
  4. basic function definitions

Then move on to learning about how python works:

  1. imports and modules (really simple)
  2. the python path (sys.path)
  3. the dir() function
  4. __builtins__

Once you have an understanding of how to fit pieces together, go back and cover some of the more advanced language features:

  1. iterators
  2. overrides like __len__ (there are tons of these)
  3. list comprehensions and generators
  4. classes and objects (again, really simple once you know a couple rules)
  5. python inheritance rules

And once you have a comfort level with these items (with a focus on what makes them pythonic), look at more specific items:

  1. Threading in python (note the Global Interpreter Lock)
  2. context managers
  3. database access
  4. file IO
  5. sockets
  6. etc...

And never forget The Zen of Python (by Tim Peters)

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
like image 169
gahooa Avatar answered Oct 14 '22 04:10

gahooa