Python Cheat Sheet (Python Cheat Sheet), Lektion, Seite 724035
https://www.purl.org/stefan_ram/pub/cheat-sheet_python (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
Python-Kurs

Python  Cheat Sheet

Die folgenden Texte dienen nur als Gedächtnisstütze für fortgeschrittene Leser, die sie ohne weitere Erklärung verstehen.

Listen

l = list()

l = []

l.append( item )

item in l

l.index( term )

print( l )

Listen append
l = []; l.append( [ 0, 1, 2 ]); print( l )
[[0, 1, 2]]
l = []; l.append( 'abc' ); print( l )
['abc']
Listen +=
l = []; l +=[ 0, 1, 2 ]; print( l )
[0, 1, 2]
l = []; l += 'abc'; print( l )
['a', 'b', 'c']
Zuordnungen

d = dict()

d[ key ]= value

print( d )

for key in dict:

Installieren

https://pypi.org/search/?q=example

py -m pip install pywin32-224-cp37-cp37m-win_amd64.whl

ungzip example.tar.gz; cd example; py setup.py install

ungzip example.tar.gz; cd example; py ez_setup.py

Schlafen
__import__( "time" ).sleep( 1 )
Zeitstempel (Uhrzeit und Datum, Stampade)
__import__( "datetime" ).datetime.now().replace( microsecond=0 ).isoformat( 'T' )
Jahr und Monat
current=__import__( "datetime" ).datetime.now().strftime('%Y-%m')
Zeitrechnen (was ist in 11 Tagen und 2 Stunden?)
__import__( "datetime" ).datetime.now()+__import__( "datetime" ).timedelta(days=11,hours=2)
Dezimalzahlen
__import__( "decimal" ).Decimal( 1 / 10 )
Zufallszahlen

__import__( "random" ).random()

__import__( "random" ).randint( 0, 2 )

__import__( "random" ).choice( [ 'heads', 'tails' ])

Aktuelles Verzeichnis

__import__( "os" ).getcwd()

__import__( "os" ).chdir( 'C:/example' )

Suchen und Ersetzen
__import__( "re" ).sub( r'(b)', r'[\1\1]', 'abcabbc' )
Unterprozesse

subprocess.check_call( [ sys.executable, 'hello.py'])

subprocess.check_call( [ sys.executable, '-m', 'pip', 'install', 'numpy'])

subprocess.Popen(['start', 'hello.txt'], shell=True)

Unterprozesse (ungetestet)

args = [ sys.executable, '-m', 'pip', 'install', 'numpy' ]

command=__import__( 'subprocess' ).list2cmdline(args)

print( f'EXECUTING {command}' )

with __import__( 'subprocess' ).Popen \

( command,

stdout=__import__( 'subprocess' ).PIPE,

stderr=__import__( 'subprocess' ).STDOUT,

bufsize=1, universal_newlines=True) as p:

for line in p.stdout:

print( end=line )

print( 'returncode = ', p.returncode )

execfile
def execfile( filename ):
assert( type( filename )== type( '' ))
file = open( filename, 'rb' )
contents = file.read()
del file
compiled = compile( contents, filename, 'exec' )
del contents
del filename
exec( compiled )
del compiled
exceptions
except Exception as inst:
print( sys.exc_info()[ 0 ])
print( type( inst ))
print( inst.args )
print( inst )
Python-Version

__import__( "sysconfig" ).get_python_version()

__import__( "sys" ).version

Logging

import logging

logger = logging.getLogger()

handler = logging.StreamHandler()

formatter = logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' )

handler.setFormatter( formatter )

logger.addHandler( handler )

logger.setLevel( logging.DEBUG )

logger.debug( 'example' )

Super aufrufen
super().__init__( *args, **kwargs ):
Nicht portabel, nur CPython  (Stand 2018)
__import__( "inspect" ).currentframe().f_back.f_locals[ 'a' ]
__import__( "inspect" ).currentframe().f_back.f_globals[ 'a' ]
Quellcode
bltinmodule.c
bltinmodule.c.h
unicodeobject.h

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724035 stefan_ram:724035 Python Cheat Sheet Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724035, slrprddef724035, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/cheat-sheet_python