Inbuilt Functions - Notes
Inbuilt Functions - Notes
Topics Covered:
● Inbuilt Functions
○ abs() Function:
○ all() Function:
○ any() Function:
○ bin() Function:
○ bool() Function:
○ sum() Function:
○ eval() Function:
○ float() Function:
○ complex() function:
○ min() Function:
○ max() Function:
○ hex() Function:
○ oct() Function:
○ sorted() Function:
○ input() Function:
○ int() Function:
○ pow() Function:
○ range() Function:
○ reversed() Function
○ round() Function:
○ type() Function:
Inbuilt Functions
Functions that are built into Python and readily available for use are known as Inbuilt Functions.
abs() Function:
Return the absolute value of a number.
x = abs(-7.25)
print(x)
1
all() Function:
Check if all items in a list are True
any() Function:
Returns True if any item in an iterable object is true
bin() Function:
Returns the binary version of a number
x = 10
y = bin(x)
print (y)
bool() Function:
Returns the boolean value of the specified object
sum() Function:
Sum() function is used to get the sum of numbers of an iterable, i.e., list.
s = sum([1, 2,4 ])
print(s)
s = sum([1, 2, 4], 10)
print(s)
2
eval() Function:
The function parses the expression passed to it and runs python expression(code)
within the program.
x=8
print(eval('x*2'))
float() Function:
The function returns a floating-point number from a number.
# for integers
print(float(9))
# for floats
print(float(8.19))
complex() function:
It is used to convert numbers or strings into a complex number. This method takes two
optional parameters and returns a complex number. The first parameter is called a real
and second as imaginary parts.
min() Function:
Returns the smallest item
3
max() Function:
Returns the largest item
hex() Function:
It is used to generate the hex value of an integer argument. It takes an integer argument
and returns an integer converted into a hexadecimal string.
result = hex(1)
print(result)
oct() Function:
Converts a number into an octal
val = oct(10)
print(val)
sorted() Function:
Returns a sorted list
input() Function:
It is used to get input from the user. It prompts for the user input and reads a line. After
reading data, it converts it into a string and returns it. It throws an error EOFError if EOF
is read.
4
int() Function:
Python int() function is used to get an integer value. It returns an expression converted
into an integer number.
● If the argument is a floating-point, the conversion truncates the number.
● If the argument is outside the integer range, then it converts the number into a
long type.
● If the number is not a number or if a base is given, the number must be a string.
pow() Function:
The python pow() function is used to compute the power of a number.
It returns x to the power of y. If the third argument(z) is given, it returns x to the power of
y modulus z, i.e. (x, y) % z.
# positive x, positive y (x**y)
print(pow(4, 2))
# negative x, positive y
print(pow(-4, 2))
# negative x, negative y
print(pow(-4, -2))
range() Function:
Returns a sequence of numbers, starting from 0 and increments by 1 (by default)
# empty range
print(list(range(0)))
5
reversed() Function:
Returns a reversed iterator
# for string
String = 'Java'
print(list(reversed(String)))
# for tuple
Tuple = ('J', 'a', 'v', 'a')
print(list(reversed(Tuple)))
# for range
Range = range(8, 12)
print(list(reversed(Range)))
# for list
List = [1, 2, 7, 5]
print(list(reversed(List)))
round() Function:
Rounds a numbers
# for integers
print(round(10))
# even choice
print(round(6.6))
type() Function:
Returns the type of an object
List = [4, 5]
print(type(List))