sampleqa.in tutorials, Python Programming  tutorial

Python Built-in Functions


MethodDescription
pow
abs
all
any
tuple
dict
set
list
frozenset
zip
map
filter
isinstance
sum
max
min
str
repr
sorted
roundround(number,ndigits=None)
Round a number to a given precision in decimal digits
Return value is integer if ndigits is omitted
otherwise return value is same as number.
ndigits can be positive or negative.

round(999) --> 999
round(9.09)--> 9
round(9.9) --> 10
round(9.5) --> 10
in above cases return value is integer
>>> round(113.090909090,-1)
110.0
>>> round(113.090909090,-2)
100.0
>>> round(113.090909090,-3)
0.0
reversedreversed is a class,returns a reverse iterator for a given sequence

reversed(sequence)

reversed method changes the order of elements in reverse order, i.e last element becomes first, last but one becomes second element...so on. Note: 'set' object is not reversible
range range returns an object that produces sequence of integers from start
  • range(stop)
  • range(start,stop,increment)
range(stop) produces sequence of integers from 0 to stop-1.
range(start,stop,increment) produces range of integeres from start to stop-1 , each integer ,incremented by incr

      	range(5)  produces 0,1,2,3,4
      	range(0,5) produces 0,1,2,3,4
      	range(0,5,2) produces 0,2,4, --> each number incremented by 2, final value should be less than stop value.
      			
Printing in Reverse order
      		range(5,0,-1) produces integers from 5,4,3,2,1
      		
      		initial value decremented by -1, stop value is 0(exclusive)
      	
print,formatprint method prints given text on standard output device.
format formats the given input.
openopen method is used for file operations,
oct >>> oct(7)
'0o7'
>>> oct(8)
'0o10'
>>> oct(9)
'0o11'
>>> oct(10)
'0o12'
hex >>> hex(9)
'0x9'
>>> hex(10)
'0xa'
>>> hex(11)
'0xb'
>>> hex(12)
'0xc'
>>> hex(13)
'0xd'
>>> hex(14)
'0xe'
>>> hex(15)
'0xf'
>>>
chr,ordchr returns unicode string of a ordinal i
			chr(i) ordinal i should be between 0 and 0x10ffff.
			
chr(97)  -->  'a'
chr(65)  -->  'A'
chr(42)  -->  '*'
chr(43)  -->  '+'
chr(45)  -->  '-'
chr(47)  -->  '/'
			
			currency symbols using unicode
			
			chr(0x20B9) --> indian rupee symbol '₹'
			chr(0x20AC) --> Euro currency symbol '€'
			chr(0x24)   --> '$'  currency symbol
			chr(0x00A3) --> UK currency symbol '£'
		
ord returns unicode number of a single character string
ord(c)   c is a single character string 
			
ord('+') -->  unicode number is 43
ord('a') -->  unicode number is 97
ord('A') -->  unicode number is 65
		      
ord('₹') -->  unicode number is 8377
		
Convert lower case to upper case using chr and ord functions
			s="abcd"
			for i in s
			 print( chr( ord(i)+ord('A')-ord('a') )  ,end="")
			
			ABCD
		
binbin return binary representations of a number
bin(number)
bin(4) --> '0b100' bin(100) --> '0b1100100'
Note: bin returns a string starting with 0b indicates binary representation.followed by binary value of the number
ascii
next
iter
len
enumerate enumerate(iterable, start=0) Returns an iterable enumerate object. iterable must be a sequence or other iterable object that supports the iteration protocol. The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start, or zero by default) and the corresponding value ob‐ tained from iterating over iterable. Useful for obtaining an indexed series of both positions and items, in iterations such as for loops (e.g., (0, x[0]), (1, x[1]), (2, x[2]), …).
input
id
divmoddivmod(x,y) returns a tuple (integer division, mod)
divmod(x//y, x%y)
      			divmod(10,3)
      			10//3 integer division gives 3
      			10%3 remainder is 1
      			so tuple is (3,1)
      			
dir

ADS