Important Built in Function in Python 3

1786

In this article, we will discuss important built-in function in Python 3.

type() function in Python

Role of type() Function:

The type() returns the type of an object

Syntax:
type(object)

 

Code: 
a_string = "I am a string"
a_integer = 3
a_float = 5.9
a_complex = 3 + 4j
a_list = [1, 2, 3, 4]

print(type(a_string))
print(type(a_integer))
print(type(a_float))
print(type(a_complex))
print(type(a_list))

 

Output: 
<class 'str'>
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'list'>

 

 

abs() function in Python

Role of abs() Function:

The abs() function is used to return the absolute value of a number.

Syntax:
abs(number)

number : Can be integer, a floating point
number or a complex number

 

Code: 
# Floating point number
x_float = -101.89
print('Absolute value of float is:', abs(x_float))

# Integer Number
y_int = -61
print('Absolute value of integer is:', abs(y_int))

# Complex number
z_complex = (-4 - 3j)
print('Absolute value or Magnitude of complex is:', abs(z_complex))

 

Output: 
Absolute value of float is: 101.89
Absolute value of integer is: 61
Absolute value or Magnitude of complex is: 5.0

 

 

all () function in Python

Role of all() Function:

The all() function returns True if all items in an iterable object are true

Syntax:
all(object)

object can be a list

 

Code: 
x = [False, True, True, True, True]
print(all(x))
x = [True, True, True, True, True]
print(all(x))

 

Output: 
False
True

 

any () function in Python

Role of any() Function:

The any() function returns True if any item in an iterable object is true

Syntax:
any(object)

object can be a list

 

Code: 
x = [False, True, True, True, True]
print(any(x))
x = [True, True, True, True, True]
print(any(x))

 

Output: 
True
True


 

bin () function in Python

Role of bin() Function:

The bin() function returns the binary version of a number

Syntax:
bin(number)

object can be a list

 

Code: 
x = 32
print(bin(x))
y = 255
print(bin(y))

 

Output: 
0b100000
0b11111111


 

bool () function in Python

Role of bool() Function:

The bool() function returns the boolean value of the specified object

Code: 
print(bool(3 < 5))
print(bool(3 == 5))

 

Output:
True
False

 

 

float () function in Python

Role of float() Function:

The float() function returns the float value 

Code: 
a = input("Enter a Float number: ")
if type(a) == float:
    print("Entered number is not float")
else:
    print("Entered Number is {}".format(type(a)))

print("Let's convert string type to float type")
a = float(a)
if type(a) == float:
    print("String Type is finally converted to float")

 

Output:
Enter a Float number: 3.0
Entered Number is <class 'str'>
Let's convert string type to float type
String Type is finally converted to float

 

int () function in Python

Role of int() Function:

The int() function returns the integer value

Code: 
a = input("Enter a Integer number: ")
if type(a) == float:
    print("Entered number is not Integer")
else:
    print("Entered Number is {}".format(type(a)))

print("Let's convert string type to Integer type")
a = float(a)
if type(a) == float:
    print("String Type is finally converted to Integer")

 

Output:
Enter a Integer number: 3
Entered Number is <class 'str'>
Let's convert string type to Integer type
String Type is finally converted to Integer

 

 str () function in Python

Role of str() Function:

The str() function returns the string data type

Code: 

a_string = "I am a string"
a_integer = 3
a_float = 5.9
a_complex = 3 + 4j
a_list = [1, 2, 3, 4]

print(type(str(a_string)))
print(type(str(a_integer)))
print(type(str(a_float)))
print(type(str(a_complex)))
print(type(str(a_list)))

 

Output:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>

 

len () function in Python

Role of len() Function:

The len() function returns the length of an object

Code: 
a = input("Enter some Text: ")
print("The length of the text entered is: {} ".format(len(a)))

a_list = ["I", "am", "a", "Python", "Programmer"]
print("The length of the list is: {} ".format(len(a_list)))

 

Output:
Enter some Text: I am a Python Programmer
The length of the text entered is: 24
The length of the list is: 5

 

list () function in Python

Role of list() Function:

The list() function returns a list

Code: 
a = input("Enter some Text: ")
print("Now we will convert the text in to list: ")
a = list(a)
print(a)

 

Output:
Enter some Text: Python
Now we will convert the text in to list:
['P', 'y', 't', 'h', 'o', 'n']

 

sorted () function in Python

Role of sorted() Function:

The sorted() function returns a sorted list

Code: 
a = [5, 104, 109, -1, 12, 3]
print("Printing the sorted list: ")
print(sorted(a))

 

Output:
Printing the sorted list:
[-1, 3, 5, 12, 104, 109]

 

reversed () function in Python

Role of reversed() Function:

The reversed() function returns a reversed iterator

Code: 
a = [5, 104, 109, -1, 12, 3]
a_iterator = reversed(a)
a = list(a_iterator)
print("Printing the reversed list: ")
print(list(a))

 

Output:
Printing the reversed list:
[3, 12, -1, 109, 104, 5]

 

round () function in Python

Role of round() Function:

The round() function returns a round value

Code: 
print(round(3.5))
print(round(-3.48))
print(round(5.19))
print(round(7.9))

 

Output:
4
-3
5
8

 

iter () function in Python

Role of iter() Function:

The iter() function returns an iterator object

Code: 
a = iter([5, 4, 3, 2, 1])
print(a)
print("We will pass the list_iterator object to sort the list ")
b = sorted(a)
print("Sorted List is: ")
print(b)

 

Output:
<list_iterator object at 0x000001B9CC263208>
We will pass the list_iterator object to sort the list
Sorted List is:
[1, 2, 3, 4, 5]

 

next () function in Python

Role of next() Function:

The next() function returns the next item in an iterable

Code: 
a_list = [5, 4, 3, 2, 1]
a_iter = iter(a_list)
a_length = len(a_list)
print("Length of the list is: {}".format(a_length))
print("We will run the for loop {} times".format(a_length))
for i in range(0,a_length):
    print(next(a_iter))

 

Output:
Length of the list is: 5
We will run the for loop 5 times
5
4
3
2
1

 

range () function in Python

Role of range() Function:

The range() function returns a sequence of numbers, starting from 0 and increments by 1 (by default)

Code:
a_list = [5, 4, 3, 2, 1]
a_length = len(a_list)
a_iter = iter(a_list)
print("Length of the list is: {}".format(a_length))
print("We will run the for loop {} times".format(a_length))
for i in range(0,a_length):
    print(next(a_iter))

#Default increment value is 1 in range function
#Now we will change the increment/step value to 3

for i in range(0, 15, 3):
    print("Value of i is {}".format(i))

 

Output:
Length of the list is: 5
We will run the for loop 5 times
5
4
3
2
1
Value of i is 0
Value of i is 3
Value of i is 6
Value of i is 9
Value of i is 12

 

 

sum () function in Python

Role of sum() Function:

The sum() function sums the items of an iterator

Code:
a_list = [5, 4, 3, 2, 1]
print("Sum of all integers in the list is: {}".format(sum(a_list)))

 

Output:
Sum of all integers in the list is: 15

 

min () function in Python

Role of min() Function:

The min() function returns the smallest item in an iterable

Code:
a_list = [5, 4, 3, 2, 1]
print("Minimum of all integers in the list is: {}".format(min(a_list)))

 

Output:
Minimum of all integers in the list is: 1

 

max () function in Python

Role of max() Function:

The max() function returns the largest item in an iterable

Code:
a_list = [5, 4, 3, 2, 1]
print("Largest of all integers in the list is: {}".format(max(a_list)))

 

Output:
Largest of all integers in the list is: 5