|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +#when we want to find the absolute value of any number |
| 4 | +print abs(-1) |
| 5 | + |
| 6 | +#when we want to find out if a object is iterable or not |
| 7 | +a = [1,2,3,4] |
| 8 | +print all(a) #prints True |
| 9 | + |
| 10 | +#get the binary of number |
| 11 | +print bin(12) #takes an int object |
| 12 | + |
| 13 | +#returns character whose ASCII code is 97 |
| 14 | +print chr(97) #takes an int object |
| 15 | + |
| 16 | +#classmethod |
| 17 | + |
| 18 | +Class C: |
| 19 | + @classmethod |
| 20 | + def f(arg1, arg2): |
| 21 | + #some code here |
| 22 | + |
| 23 | +#now we can call f as C.f(1,2) |
| 24 | + |
| 25 | +#to compare two objects |
| 26 | + |
| 27 | +print cmp(2,3) |
| 28 | +#returns -ve if 2<3, +ve if 2>3, zero if 2==3 |
| 29 | + |
| 30 | +#creating a complex number |
| 31 | +print complex(1,2) #creates 1+2j, both args are optional |
| 32 | + |
| 33 | +#calculate divisor & quotient in one go |
| 34 | +print divmod(3,2) #returns (1,1) |
| 35 | + |
| 36 | +#evaluating expressions |
| 37 | +x=1 |
| 38 | +print eval(`x+1`) # ` is not single quote, evaluates the expression and returns value |
| 39 | + |
| 40 | +#executefile and return result |
| 41 | +execfile('./type.py') |
| 42 | + |
| 43 | +#constructor to a file |
| 44 | +a = file('./demofile','w',3444) # creates a file demofile in write mode with buffer size 3444 |
| 45 | +#default open mode is 'r': read, and last two args are optional |
| 46 | + |
| 47 | +#get the hexadecimal value |
| 48 | +print hex(12) #takes a int |
| 49 | +print float.hex(12.12) |
| 50 | + |
| 51 | +#identification of an object |
| 52 | +print id(a) |
| 53 | + |
| 54 | +#converting any base into int |
| 55 | +print int(344, 8) #converts octal 344 to decimal number, you can use '344' as well |
| 56 | + |
| 57 | +#finding if a object is instance of a class or not |
| 58 | +print isinstance(a,file) #prints True if a is file object |
| 59 | + |
| 60 | +#get the length |
| 61 | +print len('python rocks') |
| 62 | + |
| 63 | +#list |
| 64 | +print list((1,2,3,4)) # returns [1,2,3,4] |
| 65 | +print list('abcde') # returns ['a','b','c','d','e'] |
| 66 | + |
| 67 | +#converts any base to long int |
| 68 | +long('2334',8) |
| 69 | + |
| 70 | +#maximum |
| 71 | +print max([1,2,3,4,5]) #prints 5 |
| 72 | + |
| 73 | +#minimum |
| 74 | +print min([1,2,3,4,5]) #prints 1 |
| 75 | + |
| 76 | +#convert integer to octal |
| 77 | +print oct(23) |
| 78 | + |
| 79 | +#opening a file |
| 80 | +a = open(name, mode, buffering) |
| 81 | + |
| 82 | +#returns unicode character |
| 83 | +print ord('a') #prints 67 |
| 84 | + |
| 85 | +#power |
| 86 | +print pow(2,3) #8 |
| 87 | +print (2,3,4) #2^3%4 computed more efficiently than manual operation |
| 88 | + |
| 89 | +#printing values |
| 90 | +print('hello %s, %d'%(name, 34)) |
| 91 | +print('') |
| 92 | + |
| 93 | +#range |
| 94 | +range(12) #returns a tuple [0,1,...12] |
| 95 | +range(23,34) #returns a tuple [23,24,25.....34] |
| 96 | +range(1,12,3) #returns a tuple [1,4,7..] |
| 97 | + |
| 98 | +#taking input from user |
| 99 | +name = raw_input('enter your name') |
| 100 | +print 'Hello %s'%(name) |
| 101 | + |
| 102 | +#reversing a sequence |
| 103 | + |
| 104 | +a = reversed((2,435,1,345)) |
| 105 | +for i in a: |
| 106 | + print i |
| 107 | + |
| 108 | +#will print a reverse of the given sequence |
| 109 | + |
| 110 | +#sorting |
| 111 | +a = sorted((23,252,1,2352)) #returns a sorted sequence of the given arg |
| 112 | +for i in a: |
| 113 | + print i |
| 114 | + |
| 115 | +#string constructor |
| 116 | +print str(234) #returns '234' |
| 117 | + |
| 118 | +#sum |
| 119 | +print sum([1,2,3,4]) |
| 120 | + |
| 121 | +#construcor of tuple |
| 122 | +a = tuple((1,2,3,4)) #creates a tuple object and links it with object a |
| 123 | + |
| 124 | +#checking the type of object |
| 125 | +type(12) #returns 'int' |
| 126 | +type('a') #returns 'str' |
| 127 | +type(12.2) #returns 'float' |
| 128 | + |
0 commit comments