Python Built-In Functions

(55) reversed()

Definition:

  • The reserved() function returns a reversed iterator object.

Syntax:

reversed(sequence)

Parameter Values:

  • sequence = Required. Any iterable object.

Examples:

alph = ["a", "b", "c", "d"]
ralph = reversed(alph)
for x in ralph:
    print(x)
Output:
d
c
b
a

(56) round()

Definition:

  • The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.
  • The default number of decimals is 0, meaning that the function will return the nearest integer.

Syntax:

round(number, digits)

Parameter Values:

  • number = Required. The number to be rounded
  • digits = Optional. The number of decimals to use when rounding the number. Default is 0

Examples:

x = round(5.76543, 2)
y = round(5.76543)

print(x)
print(y)
Output:
5.77
6

(56) set()

Definition:

  • The set( ) function creates a set object.
  • The items in a set list are unordered, so it will appear in random order.

Syntax:

set(iterable)

Parameter Values:

  • iterable = Required. A sequence, collection or an iterator object

Examples:

x = set(('Subrat', 'Abhispa', 'Arpita', 'Abhispa'))

print(x)
Output:
{'Arpita', 'Subrat', 'Abhispa'}

(58) slice()

Definition:

  • The slice() function returns a slice object.
  • A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end.
  • You can also specify the step, which allows you to e.g. slice only every other item.

Syntax:

slice(start, end, step)

Parameter Values:

start = Optional. An integer number specifying at which position to start the slicing. Default is 0
end = An integer number specifying at which position to end the slicing
step = Optional. An integer number specifying the step of the slicing. Default is 1

Examples:

a = ("a", "b", "c", "d", "e", "f", "g", "h")
x = slice(2)
y = slice(3, 5)
z = slice(0, 8, 3)


print(a[x])
print(a[y])
print(a[z])
Output:
('a', 'b')
('d', 'e')
('a', 'd', 'g')

(59) sorted()

Definition:

  • The sorted() function returns a sorted list of the specified iterable object.
  • You can specify ascending or descending order. Strings are sorted alphabetically, and numbers are sorted numerically.

Syntax:

sorted(iterable, key=key, reverse=reverse)

Parameter Values:

  • iterable = Required. The sequence to sort, list, dictionary, tuple etc.
  • key = Optional. A Function to execute to decide the order. Default is None
  • reverse = Optional. A Boolean. False will sort ascending, True will sort descending. Default is False

Examples:

a = ("b", "g", "a", "d", "f", "c", "h", "e")
b = (1, 11, 2)

print(sorted(a))
print(sorted(b))
Output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
[1, 2, 11]

(60) staticmethod()

Definition:

  • The staticmethod() built-in function returns a static method for a given function.

Syntax:

staticmethod(function)

Parameter Values:

  • function = function that needs to be converted to a static method

Examples:

class Calculator:
    def add_numbers(num1, num2):
        return num1 + num2

# convert add_numbers() to static method
Calculator.add_numbers = staticmethod(Calculator.add_numbers)

sum = Calculator.add_numbers(5, 7)
print('Sum:', sum)
Output:
Sum: 12

(61) str()

Definition:

  • The str() function converts the specified value into a string.

Syntax:

str(object, encoding=encoding, errors=errors)

Parameter Values:

  • object = Any object. Specifies the object to convert into a string
  • encoding = The encoding of the object. Default is UTF-8
  • errors = Specifies what to do if the decoding fails

Examples:

x = str(3.5)
y = str(0.9999)

print(x,type(x))
print(y,type(x))
x = str(3.5)
y = str(0.9999)

print(x,type(x))
print(y,type(x))

(62) sum()

Definition:

  • The sum() function returns a number, the sum of all items in an iterable.

Syntax:

sum(iterable, start)

Parameter Values:

  • iterable = Required. The sequence to sum.
  • start = Optional. A value that is added to the return value

Examples:

a = (1, 2, 3, 4, 5)
b = (1, 2, 3, 4, 5)

x = sum(a)
y = sum(b, 7)

print(x)
print(y)
Output:
15
22

(63) super()

Definition:

  • The super() function is used to give access to methods and properties of a parent or sibling class.
  • The super() function returns an object that represents the parent class.

Syntax:

super()

Parameter Values:

  • No parameters

Examples:

class Parent:
    def __init__(self, txt):
        self.message = txt

    def printmessage(self):
        print(self.message)

class Child(Parent):
    def __init__(self, txt):
        super().__init__(txt)

x = Child("Hello, and welcome!")

x.printmessage()
Output:
Hello, and welcome!

(64) tuple()

Definition:

  • The tuple() function creates a tuple object.

Syntax:

tuple(iterable)

Parameter Values:

  • iterable = Required. A sequence, collection or an iterator object.

Examples:

x = tuple(['India', 'Finland', 'Poland'])
y = tuple('Abhispa')

print(x)
print(y)
Output:
('India', 'Finland', 'Poland')
('A', 'b', 'h', 'i', 's', 'p', 'a')

(65) type()

Definition:

  • The type() function returns the type of the specified object

Syntax:

type(object, bases, dict)

Parameter Values:

  • object = Required. If only one parameter is specified, the type() function returns the type of this object.
  • bases = Optional. Specifies the base classes
  • dict = Optional. Specifies the namespace with the definition for the class

Examples:

a = ('Subrat', 'Abhispa', 'Arpita')
b = "Hello World"
c = 33

print(type(a))
print(type(b))
print(type(c))
Output:
<class 'tuple'>
<class 'str'>
<class 'int'>

(66) vars()

Definition:

  • The vars() function returns the __dic__ attribute of an object.
  • The __dict__ attribute is a dictionary containing the object’s changeable attributes.

Syntax:

vars(object)

Parameter Values:

  • object = Any object with a __dict__attribute

Examples:

class Person:
    name = "Abhispa"
    age = 30
    country = "India"

x = vars(Person)
print(x)
Output:
{'__module__': '__main__', 'name': 'Abhispa', 'age': 30, 'country': 'India', '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}

(67) zip()

Definition:

  • The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.
  • If the passed iterators have different lengths, the iterator with the least items decides the length of the new iterator.

Syntax:

zip(iterator1, iterator2, iterator3 ...)

Parameter Values:

  • iterator1, iterator2, iterator3 … = Iterator objects that will be joined together

Examples:

a = ("Subrat", "Abhispa", "Arpita")
b = ("Monica", "Juli", "Namita")

x = zip(a, b)

for i,j in x:
    print(i)
    print(j)
Output:
Subrat
Monica
Abhispa
Juli
Arpita
Namita

Leave a Reply

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