Warning: sizeof(): Parameter must be an array or an object that implements Countable in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php on line 245

Warning: sizeof(): Parameter must be an array or an object that implements Countable in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php on line 245

Warning: Cannot modify header information - headers already sent by (output started at /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wordpress-firewall.php:245) in /homepages/9/d213665487/htdocs/clickandbuilds/CoalGeology/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 62
Some useful built in functions in Python with examples – Coal Geology and Mining: Consulting Services

Some useful built in functions in Python with examples

There are many built in functions in Python. I am only showing you some that I feel most important. Explore and learn more at your free time.

abs()

Return the absolute value of a number.

>>> abs(-5)
5
>>> abs(-3.4*4)
13.6

divmod()

Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division. (a // b, a % b)

>>> divmod(10,6)
(1, 4)
>>> a=10//6
>>> a
1
>>> b=10%6
>>> b
4
>>> c=(a,b)
>>> c
(1, 4)

enumerate()

Return an enumerate object.

>>> enumerate(test)
<enumerate object at 0x02CF2C60>
>>> list(enumerate(test))
[(0, ‘jan’), (1, ‘feb’), (2, ‘march’)]

int()

Convert a number or string x to an integer, or return 0 if no arguments are given

>>> int()
0
>>> int(20)
20
>>> int(’20’)
20

str()

Return a string containing a nicely printable representation of an object

>>> 300
300
>>> p=str(300)
>>> p
‘300’
>>> print(p)
300

eval()

>>> x=10
>>> print(eval(‘x+1’))
11

pow()

equivalent to using the power operator: x**y.

>>> pow(2,2)
4
>>> 2**2
4

pow(x,y,z)

Return x to the power y, modulo z; computed more efficiently than pow(x, y) % z)

>>> pow(3,3,2)
1
>>> pow(3,3,3)
0
>>> pow(7,7,3)
1
>>>

Sum()

Sums start and the items of an iterable from left to right and returns the total

>>> alist=[1,2,3,4,5]
>>> sum(alist)
15

bin()

Convert an integer number to a binary string.

>>> bin(100)
‘0b1100100’
>>> bin(30)
‘0b11110’

bool()

Convert a value to a Boolean

>>> bool(100)
True
>>> bool(-1)
True
>>> bool(1=5)
SyntaxError: keyword can’t be an expression
>>> bool(1==5)
False
>>> bool(3>5)
False
>>>

reversed()

Return a reverse iterator

>>> a=[1,2,3,4,5]
>>> reversed(a)
<list_reverseiterator object at 0x02D00AB0>
>>> list(reversed(a))
[5, 4, 3, 2, 1]

zip()

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables

>>> a=[1,2,3,4,5]
>>> b=[2,3,4,5,6]
>>> zip(a,b)
<zip object at 0x02D3CAA8>
>>> print(zip(a,b))
<zip object at 0x02D3CBC0>
>>> list(zip(a,b))
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

>>> c=[1,2,3]
>>> list(zip(a,c))
[(1, 1), (2, 2), (3, 3)]

 

zip()

Return the floating point value number rounded to ndigits digits after the decimal point.

>>> round(100,2)
100
>>> round(11.2222,2)
11.22
>>> round(1e10,2)
10000000000.0
>>> round(22*3,1)
66
>>> round(22.2*3,1)
66.6
>>> round(22.2223*3,1)
66.7
>>> round(22.2223*3,3)
66.667


Leave a Comment

Your email address will not be published. Required fields are marked *