Enumerate()
When working with iterable elements, the enumerate() function can give you an associate count and value in the form of a tuple.
In the following example, we’re creating a collection of items and using the enumerate() function to turn it into a numbered list.
animals = [‘dog’, ‘cat’, ‘rabbit’]
enumerate_prime = enumerate(animals)
print(list(enumerate_prime))
Help()
This simple function will give you access to built-in documentation and resources from the Python library you’re using.
You can also create documentation when working on Python projects and allow other users to access this information with the help() function.
Here is an example of how to use this function.
help(‘integers’)
This function would give you access to documentation about integers, but you can enter any object, class, or function in parentheses.
Input()
When working on projects with interactive elements that allow users to enter text or data, the input() function will come in handy.
This Python function can turn anything a user types into a string or tensor. It allows you to place raw data in a container to store it and call it back later.
Here is an example where the input() function allows users to type a message.
message = input(“Type your message: “)
Type()
The type() function returns the object type. It’s an excellent tool for debugging your code. However, you can use a variation of this function to associate a new type to a given object.
If you want to find the type of an object, you should use the syntax type(object).
If you want to create a new type for the object, you’ll need to use type(name, bases, dict).
Setattr()
The setattr() function associates an attribute with an object. It can associate a new attribute to the object or change an existing one. The syntax is seattr(object, name, value).
In this example, we’re using setattr() to update the status of a client.
class Details:
name = ‘John’
status = ‘standard’
setattr(Details, ‘status’, VIP)