Built-In Python Functions- Part 4

Built-in python functions are used a lot in python programs because they will make your code look professional and more productive and manageable. We’ve discussed these Built-in python functions in the previous three parts, and we will complete learning more about them. If you didn’t see any part of this series, then I suggest seeing the first one.

1. Float Function

This float() function will make any integer number (Which must be added as a string) into a float number by adding the command and one 0 after the comma. If the number has many zeros at the end, which doesn’t make any sense in mathematics, it will remove them automatically. Let’s see an example:

# Convert String Values
x = float("3.500")
y = float("3")

# Print The Result
print(x)
print(y)

You can see that the values inside the float() function are numbers but as strings. The float() function converted them to real numbers that you can use in mathematical operations.

2. Getattr Function

When you create a class, you need to specify the attributes of this class. These attributes will be used later in your python code, and to access them, you can use the getattr() function. Let’s see an example:

# Create a Class
class Employee:
    # Define The Attributes
    experience = 4
    name = 'John'

# Get The Name Attributes
name = getattr(Employee, 'name')
print(name)

# Get The Experience Attributes
experience = getattr(Employee, 'experience')
print(experience)

First, you will create a class, and we name it Employee and specify its attributes which are experience & name. Then we use the getattr() function to get the name attribute. 

You specify the class name and the attribute name you need to get. We did the same also for the experience attribute. If the attribute name can’t be found, it will raise an AttributeError error.

3. Delattr Function

Like you can access the class attributes using a built-in function, you can also remove them using the delattr() built-in python function. Let’s see a simple example of this process:

# Create a Class
class Employee:
    # Define The Attributes
    experience = 4
    name = 'John'

# Remove The Experience Attributes
delattr(Employee, 'experience')

# Get The Name Attributes
experience = getattr(Employee, 'experience')
print(experience)

We’ve used the delattr() function to remove the experience attribute of the Employee class. You need to specify only the class name and the attribute name. When you run the code, you will get the AttributeError error because we are trying to access the experience attribute using the getattr() function, but it is not there anymore.

4. Hasattr Function

Python allows you to access and delete class attributes and verify if a specific attribute is there or not. You can use the hasattr() function to verify this. Let’s see an example:

# Create a Class
class Employee:
    # Define The Attributes
    experience = 4
    name = 'John'

# Remove The Experience Attributes
attribute = hasattr(Employee, 'experience')

# Print The Result
print(attribute)

Run this code, and you will get the True expression. The hasattr() function will return True if the attribute is there or False if it hasn’t. You will need only specify the class name and the attribute name.

5. Setattr Function

Like you can access, delete, and verify the class attribute, the python developer has added another function called the setattr() function to set a value for an attribute. Let’s see this example:

# Create a Class
class Employee:
    # Define The Attributes
    experience = 4
    name = 'John'

# Remove The Experience Attributes
setattr(Employee, 'experience', 7)

# Get The Name Attributes
experience = getattr(Employee, 'experience')
print(experience)

We’ve specified the class name, the attribute name, the experience attribute, and the new value of this attribute inside the setattr() function. You will see a different value when accessing this attribute using the getattr() function.

6. Iter Function

This iter() function will return an iterator object from a given object. The iter() function will create an object that one element can iterate simultaneously. Let’s see an example:

# Define a List
countries = ["Germany", "USA", "UK"]

# Iter() Function
itererator = iter(countries)

# Print The Elements
print(next(itererator))
print(next(itererator))
print(next(itererator))

Define first a list and use the iter() function followed by the list variable. You will notice that every print() statement will print one element of the list, and the next print() will print the followed one, and so on. The iteration object keeps remembering the iteration count. 

Once the iteration is complete and you still print another item, it will raise the StopIteration exception. Also, the iteration count can’t be reassigned again to 0 and start printing the first element. Therefore, you can only use it once in your python code.

7. Int Function

This int() function is similar to the first one which is float() function. It can convert a string-based number to a real value that you can use mathematically. Let’s see an example:

# Convert String Value
x= int("3")

# Print The Result
print(x)

It will give you the value 3 as a real value instead of a string. If you specify a float value, it will raise a ValueError error.

Conclusion

Thanks for reading! These built-in python functions will surely make your work easy and more efficient. You will need to learn more than what we cover in these four parts of this series since you need these built-in python functions a lot in your following python code.

Scroll to Top