Posts

Python property() Built in Function

The property() function is used to create and return a property object that has a getter, a setter, and a deleter method. The syntax of this function is: property(fget=None, fset=None, fdel=None, doc=None) The fget parameter is optional and specifies a function to get the value of the attribute. The fset parameter is optional and specifies a function to set the value of the attribute. The fdel parameter is optional and specifies a function to delete the attribute. The doc parameter is optional and specifies a string that serves as the documentation for the attribute. The property() function can be used as a decorator or as a normal function. Here are some examples of using the property() function in Python: To create a read-only property using @property decorator: class Person:     def __init__(self, name):         self._name = name # private attribute     @property     def name(self): # getter method ...

Python range() Built in Function

The range() function is used to create and return a sequence of integer numbers as per the argument passed. The syntax of this function is: range(start, stop, step) The start parameter is optional and specifies the starting number of the sequence. The default value is 0. The stop parameter is required and specifies the end number of the sequence (exclusive). The step parameter is optional and specifies the increment or decrement of the sequence. The default value is 1. The range() function can be used with for loops to iterate over a sequence of numbers. It can also be used with list() function to create a list of numbers. Here are some examples of using the range() function in Python: To create a sequence of numbers from 0 to 5, and print each item in the sequence: x = range(6) for n in x:     print(n) # prints 0 1 2 3 4 5 To create a sequence of numbers from 10 to -10, with a step of -2, and print each item in the sequence: x = range(10, -11, -2) for n in x:  ...

Python repr() Built in Function

The repr() function is used to return a printable representation of the given object. The syntax of this function is: repr(object) The object parameter is required and specifies the object whose representation is needed. The repr() function returns a string that can be evaluated by eval() function to recreate the original object. The repr() function can be useful for debugging and logging purposes, as it shows more details about the object than str() function. It can also be used to customize the representation of user-defined classes by overriding the repr() method. Here are some examples of using the repr() function in Python: To get the representation of a built-in type such as list: x = [1, 2, 3] print(repr(x)) # prints '[1, 2, 3]' To get the representation of a custom class without overriding repr() method: class Student:     def __init__(self, name):         self.name = name s = Student("Alice") print(repr(s)) # prints '...

Python reversed() Built in Function

The reversed() function is used to return a reversed iterator object of a given sequence object. The syntax of this function is: reversed(sequence) The sequence parameter is required and specifies the sequence object (such as list, tuple, string, range) whose reverse order is needed. The reversed() function returns an iterator object that can be used with for loops or converted to a list using list() function. The reversed() function can be useful for reversing the order of elements in a sequence without modifying the original sequence. It can also be used with sorted() function to sort a sequence in descending order. Here are some examples of using the reversed() function in Python: To reverse the order of a string and print each character: s = "Python" r = reversed(s) for c in r:     print(c) # prints n o h t y P To reverse the order of a list and create a new list: l = [1, 2, 3, 4, 5] r = reversed(l) new_l = list(r) print(new_l) # prints [5, 4, 3, 2, 1] To sor...

Python round() Built in Function

The round() function in Python returns a floating-point number rounded to the specified number of decimals. The syntax is: round(number, ndigits) where number is the number to be rounded and ndigits is the number of decimal places to round to. If ndigits is omitted or None, then the number is rounded to the nearest integer. Here are some examples of using round() with different values of ndigits: # Round 13.46 to the nearest integer print(round(13.46)) # Output: 13 # Round 13.46 to one decimal place print(round(13.46, 1)) # Output: 13.5 # Round 1.5 up and 2.5 down print(round(1.5)) # Output: 2 print(round(2.5)) # Output: 2 # Round -1.25 to zero decimal places print(round(-1.25)) # Output: -1 If you have any questions about this code, you can drop a line in comment .