Formázási leírások:

Vizuális Kódelemzés lépésenként (Python, js, java, c…)


Letöltött Python Interpreter fordítása Linuxon:

# you need these to build all python module
sudo apt-get install build-essential
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

./configure –enable-optimizations –prefix=/home/bnyiro/NNG/compiled_python
make

Tanulás:

 

modul újratöltés 3.4-től (eredeti cikk):

>> import modul
>>> importlib.reload(modul)


Rendezés adott függvény szerint

>> def reverse(word):

return word[::-1]
>>> reverse(‘testing’)
‘gnitset’
>>> sorted(fruits, key=reverse)
[‘banana’, ‘apple’, ‘fig’, ‘raspberry’, ‘strawberry’, ‘cherry’]


Könyvek:

 

Bináris adat írása fájlba (eredeti oldal):

import binascii hs="5B7F888489FEDA" hb=binascii.a2b_hex(hs) file.write(hb) file.close()

 

search substring:

  1. >>> s = “lalahelloblabla”
  2. >>> “hello” in s
  3. True

ugyan ez működik listákra is: l=[“alma”, “répa”]   esetében “korte” in l  eredménye False

lista kulcs-érték párok megfordítása, 2-es pythonban:

def swap_dictionary(original_dict):
   return dict([(v, k) for (k, v) in original_dict.iteritems()])
szótár véletlen összekeverése és bejárása, 3-as Py – list() azért kell hogy a view-ből lista legyen:
keys=list(fejezet_szotar.keys()    )
random.shuffle( keys   )for eng_key in keys: ...

Statikus változó szimulálása:

def static_num():
   static_num.x += 1
   return static_num.x
   static_num.x = 0

if __name__ == '__main__':
   for i in range(10):
      print static_num()


Why len is not a method (Fluent Python, 2015)

I asked this question to core developer Raymond Hettinger in 2013 and the key to his
answer was a quote from the Zen of Python: “practicality beats purity”. In “How special
methods are used” on page 8 I described how len(x) runs very fast when x is an instance
of a built-in type. No method is called for the built-in objects of CPython: the length is
simply read from a field in a C struct. Getting the number of items in a collection is a
common operation and must work efficiently for such basic and diverse types as str ,
list , memoryview etc.
In other words, len is not called as a method because it gets special treatment as part of
the Python Data Model, just like abs . But thanks to the special method __len__ you
can also make len work with your own custom objects. This is fair compromise between
the need for efficient built-in objects and the consistency of the language. Also from the
Zen of Python: “Special cases aren’t special enough to break the rules.”