CodeBlessU
learn code with examples

Python Built-in Functions

int()


Syntax:
class int(x, base=10)

  • x - any number or string (representing number)
  • base
    • Valid values: 0, 2 to 36
  • return value
    • Type: class 'int'
    • It returns integer object, representing number x in base.
    • If both arguments missing, it returns 0.

bool()

Syntax:
class bool( x )
Return Value:
Type: class 'bool'

x is any other datatype. Its Optional.

It returns boolean False, if x not specified.

It returns boolean object by converting x to bool as per standard truth testing procedure.


Truth testing procedure

Following cases are evaluated as boolean False:

  • built-in objects: None and False are evaluated as False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1) are evaluated as False.
  • empty sequences and collections: '', (), [], {}, set(), range(0) - empty string, empty tuple, empty list, empty dictionary, empty set are evaluated as False.
  • object is evaluated as False, if __bool__() method returns False or __len__() method returns 0 (zero).

# Examples, bool() function
# all below evaluates to False
bool(0)
bool(0.0)
bool("")
bool( tuple() ) # tuple to bool
bool( [] ) # empty list to bool
bool( {} ) # empty dictionary to bool
bool( set() ) #empty set to bool
bool( range(0) ) # range to bool

# all below evaluates to True
bool(-1)
bool(1)
bool(10)
bool(0.000001)
bool("False")
bool("0")
bool( (0,0) ) # tuple with multiple 0 to bool
bool( [1, "Hi"] ) # list to bool
bool( {0: 0} ) # dictionary to bool
bool( {0} ) # set to bool
bool( range(5) ) # range to bool

tuple()


Syntax:
class tuple( [iterable] )

  • [iterable] - any iterable (list / array / tuple etc.)
  • return value
    • Type: class 'tuple' See tuple data type
    • It returns tuple with all elements from iterable.
    • If [iterable] is not specified, it returns empty tuple.

In this page...

Copyright © All rights reserved.