>>> mylist=[0,1,2,3,4,5,6,7,8] >>> mylist[-3] >>> 6
>>> from random import shuffle >>> shuffle(mylist) >>> mylist
>>> ','.join('12345') >>> '1,2,3,4,5'split() lets us split a string around the character we specify.
>>> '1,2,3,4,5'.split(',') >>> ['1','2','3','4','5']
>>> 'pYthnOnCircle'.lower() >>> ‘pythoncircle’To convert it into uppercase, then, we use upper().
>>> 'pYthnOnCircle'.upper() ‘PYTHONCIRCLE’
>>> 5//2 >>> 2The normal division would return 2.5 Similarly, ** performs exponentiation. a**b returns the value of a raised to the power b.
>>> 2**3 >>> 8Finally, % is for modulus. This gives us the value left after the highest achievable division.
>>> 13%7 >>> 6
>>> a,b,c=3,4,5 #This assigns 3, 4, and 5 to a, b, and c respectively >>> a=b=c=3 #This assigns 3 to a, b, and c
def extendList(val, list=[]): list.append(val) return list list1 = extendList(10) list2 = extendList(123,[]) list3 = extendList('a') print "list1 = %s" % list1 print "list2 = %s" % list2 print "list3 = %s" % list3
list1 = [10, 'a'] list2 = [123] list3 = [10, 'a']You may erroneously expect list1 to be equal to [10] and list3 to match with [‘a’], thinking that the list argument will initialize to its default value of [] every time there is a call to the extendList.
def extendList(val, list=None): if list is None: list = [] list.append(val) return listWith this revised implementation, the output would be:
list1 = [10] list2 = [123] list3 = ['a']
for i in range(10): if i%2 == 0: pass else: print(i)