User:QuasiBot/Python

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

Main[edit]

quit()		# Exit the Python interpreter.
#			# Comment out the rest of the line.
+			# Addition binary operator.  String concatenation binary operator.
*			# Multiplication binary operator.  String repetition binary operator.  Unary operator which unpacks function arguments out of a list.
/			# Division binary operator.
-			# Subtraction binary operator.  Negation unary operator.  Set difference binary operator.
=			# Variable assignment.
j			# Imaginary unit.
J			# Imaginary unit.
complex(real, imag) 		# Returns a complex number.
z.real			# Method which returns real part of z.
z.imag		# Method which returns imaginary part of z.
abs(z)		# Returns magnitude of complex number z.
_			# Recall the last printed expression.
Control+C		# Exit secondary (...) prompt and returns to primary (>>>) prompt.
len()			# Returns the length of a string.  Returns the length of a list.  Returns the length of a tuple.
u			# In front of a string indicates that the string is Unicode.
<			# "Less than" binary operator.
>			# "Greater than" binary operator.
==			# "Equal to" binary operator.
<=			# "Less than or equal to" binary operator.
>=			# "Greater than or equal to" binary operator.
!=			# "Not equal to" binary operator.
and                 # AND logical operator
or                    # OR logical operator
not                  # NOT logical operator
while condition:	# Start of a while loop. (Indent the interior of the block.)
print			# Displays items with spaces between them.
raw_input()         # Prompts for a string, which it then returns.  It can be passed a prompt string as first argument.
if condition:	# Start of an if statement. (Indent the interior of the block, except for elif 's and else.)
elif condition:	# else-if part of an if statement.
else:			# else default part of an if statement.
for element in list:	# Start of an iterative block.  (Indent the interior of the block.)
range(x)		# Returns the list [0,1,2,..., x-1]
range(x,y)		# Returns the list [x, x+1, x+2, ..., y-1]
range(x,y,z)	# Returns the list [x, x+z, x+2*z, ..., x+n*z] where x+(n+1)*z >= y
break		# Breaks out of the smallest enclosing for or while loop.
continue		# Skips to next iteration of the smallest enclosing loop.
pass			# Do nothing (and proceed to next statement).
def function(params):	# Start of a function definition.  (Indent the interior of the block.)
return result	# Stops a function definition and returns result as the value of that function.
obj.methodname		# Method methodname of object obj.
list.append(element)	# Method which appends element to the end of list.
*listname			# Captures extra positional arguments passed to a function and places them in list listname.
**dictname			# Captures extra named arguments passed to a function and places them in dictionary dictname.
**			# Unary operator which unpacks keyword (i.e., named) function arguments out of a dictionary.  Binary operator which raises first operand to power of second operand.
(lambda x: F(x))	# Anonymous function which takes argument x and returns F(x).  (Restricted to single line.  Examples: (lambda x: x*x)(7)  yields 49, (lambda x,y:x+y)(3,4) returns 7.)
functionname.__doc__		# Method which returns documentation of function functionname.
listname.append(x)		# Method which appends item x to list listname.
listname.extend(L)		# Method which appends all items in list L to list listname.
listname.insert(i, x)		# Method which inserts item x at position i in list listname.
listname.remove(x)		# Method which removes item x from list listname.
listname.pop(i)			# Method which removes and returns item at position i of list listname.
listname.pop()			# Method which removes and returns last item of list listname.
listname.index(x)		# Method which returns position of first occurrence of item x in list listname.
listname.count(x)		# Method which returns the number of occurrences of item x in list listname.
listname.sort()			# Method which sorts the items of list listname.
listname.reverse()		# Method which reverses the order of the items in list listname.
filter(functionname, sequencename)	# Returns sequencename but with items x which fail condition functionname(x) removed.
map(functionname, sequencename) 	# Returns sequencename but with each item x replaced by functionname(x).
sum(sequencename)				# Returns sum of all elements in sequence sequencename, or 0 if sequencename is empty.
del listname[i]		# Removes item indexed by i in list listname.
del listname[i:j]		# Removes items indexed by i and j (in slice notation) from list listname.
del variablename	# Deletes variable variablename (from the symbol table).
set(listname)		# Turns list listname into a set.
set(stringname)		# Turns string stringname into a set of letters.
|				# Set union binary operator.
&				# Set intersection binary operator.
^				# Symmetric difference of sets binary operator.
dictname.keys()		# Method which returns a list of all keys used by dictionary dictname.
x in sequencename			# Returns true iff item x is found in sequence sequencename.
x not in sequencename		# Returns true iff item x is not found in sequence sequencename.
keyname in dictname			# Returns true if key keyname is found in dictionary dictname.
del dictname[keyname]		# Deletes key keyname and its associated value from dictionary dictname.
dictname[keyname] = x		# Associates value x with key keyname within dictionary dictname.
dict(listname)				# Returns a dictionary out of list listname of key:value binary tuples.
zip(list1, list2)				# Returns a list of binary tuples: the n^th tuple contains the n^th item of list list1 paired off with the n^th item of list list2.
stringname.format(value0, value1, ...)		# Returns a string obtained by replacing {0} in string stringname with value0, {1} in stringname with value1, etc.
and				# AND binary logical operator.
or				# OR binary logical operator.
not				# NOT unary logical operator.
__name__			# The name (as a string) of the containing module.
import modname	# Imports module modname.  (modname should not contain .py at the end)
modname.__name__		# Method which returns the name (as a string) of module modname.
from modname import name1, name2, ...		# Imports names (functions or variables) name1, name2, etc. from module modname directly into the symbol table, so that function name1 can be called directly with 'name1' instead of 'modname.name1'.
from modname import *	# Imports directly all names defined in module modname (except those beginning with an underscore).
reload(modname)		# Re-imports module modname.  (Used if modname was modified after it was imported.)
dir(modname)			# Returns a list of strings which are the names defined by module modname.
dir()					# Returns a list of all the names currently defined.
stringname.rjust(num)		# Makes string stringname fit width num by padding it on the left with spaces.
stringname.ljust(num)		# Makes string stringname fit width num by padding it on the right with spaces.
stringname.center(num)	# Makes string stringname fit width num by padding it in a balanced way on both the left and right with spaces.
numstringname.zfill(num)	# Makes numeric string numstringname fit width num by padding it on the left with zeroes.
open(filename, mode)		# Returns file object filename for type of use given in string mode.
fileobj.read()			# Returns entire contents of file object fileobj.
fileobj.read(num)		# Returns the next num bytes read from the file object fileobj.
fileobj.readline()		# Returns the next line read from the file object fileobj.
fileobj.readlines()		# Returns a list containing each line of the file object fileobj.
fileobj.write(stringname)	# Writes the contents of string stringname to file object fileobj, returning None.
fileobj.tell() 			# Returns an integer giving the file object fileobj's current position in the file (measured in bytes from the beginning of the file).
fileobj.seek(offset, from_what)		# Sets file object fileobj's current position in the file to be equal to (number) offset from from_what (from_what = 0 means the beginning of the file, from_what=1 means the current position, and from_what=2 means the end of the file).
fileobj.close()			# Closes file object fileobj.
pickle.dump(x, f)			# Stores object x in "pickled" format in (already open for writing) file object f.
pickle.load(f)			# Loads a "pickled" object from file object f and returns in "unpickled" (ready to be assigned to a name).
classobj.__doc__		#  Returns the documentation string of class object classobj.
classobj()				# Returns a new instance object of class object classobj.  (This can then be assigned to a variable.)
classobj.__init__()		# Constructor method of class object classobj.
instanceobj.__class__		# Method which returns the class of an instance object.
isinstance(instanceobj, classname)		# Returns True iff instanceobj.__class__ == classname or some class derived from class classname.
issubclass(class1, class2)	# Returns True iff class class1 is a subclass of class class2.
string.replace(substring1, substring2)		# Replaces any instances of substring1 in string with substring2.

Macrons[edit]

>>> word = 'indicat\xc4\x81'   # byte-string
>>> unicode(word,'UTF-8')        # decode from byte-string
u'indicat\u0101'
>>> print unicode(word,'UTF-8')            # Unichar string
indicatā
>>> unicode(word,'UTF-8').encode('utf-8')       # encode back to byte-string
'indicat\xc4\x81'

Uploading macronized content using Pywikibot[edit]

>>> mypage = "User:AugPi/Sandbox2"
>>> page = Page(site,mypage)
>>> word = "indicat\xc4\x81"
>>> word2 = unicode(word,'UTF-8')
>>> word2
u'indicat\u0101'
>>> 
>>> page.put(word2)
Updating page [[User:AugPi/Sandbox2]] via API
(302, 'OK', {u'pageid': 2162227, u'title': u'User:AugPi/Sandbox2', u'newtimestamp': u'2010-11-27T17:05:53Z', u'result': u'Success', u'oldrevid': 10896301, u'newrevid': 11049362})

Result: http://en.wiktionary.org/w/index.php?title=User:AugPi/Sandbox2&oldid=11049362

String-handling functions[edit]

strng.find(sbstrng)                        # returns -1 if sbstrng not find in strng
strng.replace(old_sbstrng, new_sbstrng)               # returns modified strng (with old_sbstrng replaced by new_sbstrng)


Reference: http://docs.python.org/library/stdtypes.html#string-methods

References[edit]