1, 2, 3 ... etc
1.0, 2.1, 3.2 ... etc
(1+0j), (2+3j) ... etc
abs... etc
C/C++Bracket delimited blocks |
PythonWhitespace delimited blocks |
if (x)
{
if (y)
{
f1();
}
f2();
}
|
if x:
if y:
f1()
f2()
|
>>> l = [1,2,3,4,5,6,7,8] >>> l[2:3] [3] >>> l[:3] [1, 2, 3] >>> l[4:] [5, 6, 7, 8] >>> l[:-1] [1, 2, 3, 4, 5, 6, 7] |
>>> l = [1,-2,3,-5,10,-99] >>> map(abs,l) [1, 2, 3, 5, 10, 99] |
x = (1,2,3) a=x[0] a,b,c = x
def myFc(): ... return a,b,c a,b,c = myFc()
>>> dict = {"key1":100,"key2":200}
>>> dict["key1"]
100
|
str1 = "hello" str2 = "world" str3 = "%s, %s." % (str1,str2) |
>>> print "hello world" hello world >>> i=1 >>> print "the answer is %d" % i the answer is 1 |
if b1: a=1 elif b2: a=2 else: a=3 |
>>> for i in range(5): ... print i ... 0 1 2 3 4 >>> for i in [1,2,4,5]: ... print i ... 1 2 4 5 >>> import random >>> [random.randint(0,10) for i in range(5)] [2, 5, 8, 5, 6] |
try:
#something potentially bad
except:
#catch all
try:
#something potentially bad
except ExceptionClass:
#catch ExceptionClass only
|
>>> def myFc(arg1, arg2, arg3="blah"): ... return arg1, arg2, arg3 ... >>> print myFc(1,2) (1, 2, 'blah') >>> print myFc(arg1=1,arg2="hello") (1, 'hello', 'blah') |
>>> increment = lambda x:x+1 >>> increment(1) 2 >>> def longFc(a,b,c,d): ... pass ... >>> shortFc = lambda x,y:longFc(1,x,y,4) >>> >>> myList = [1,2,3] >>> map(lambda x:x**2,myList) [1, 4, 9] |
>>> print sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5] >>> a = [5, 2, 3, 1, 4] >>> a.sort() >>> print a [1, 2, 3, 4, 5] >>> a = [5, 2, 3, 1, 4] >>> a.sort(lambda x, y: x-y) >>> print a [1, 2, 3, 4, 5]
class myClass:
def __init__(self):
self.classVar1 = 1
def myFc(self,arg1):
pass
def __str__(self):
return "myClass"
|
class derivedClass(myClass):
def __init__(self):
myClass.__init__(self)
|
>>> python_code = "print 'hello'" >>> exec python_code hello |
|