Posts

Python float() Built in Function

The float() function in Python is used to convert a value into a floating point number, which is a number with a decimal point. For example, float(3) returns 3.0 and float(“5.5”) returns 5.5. You can also use float() with numbers that are too large or too small for Python to represent as integers, such as 1.82e310 or -32.54e100. Here are some examples of using float() in Python: # Example 1: Convert an integer to a float x = 25 y = float(x) print(y) # Output: 25.0 # Example 2: Convert a string to a float s = "12.34" z = float(s) print(z) # Output: 12.34 # Example 3: Convert an infinity value to a float inf = "infinity" w = float(inf) print(w) # Output: inf If you have any questions about this code, you can drop a line in comment .

Python format() Built Function

Python format() is a string method that allows you to format and insert values into a string. It can be used with positional arguments, keyword arguments, or both. You can also specify formatting options such as alignment, width, precision, etc. Here are some examples of how to use Python format() : To insert values into a string using positional arguments: txt = "My name is {0}, I'm {1}".format("John", 36) print(txt) Output: My name is John, I'm 36 To insert values into a string using keyword arguments: txt = "My name is {fname}, I'm {age}".format(fname="John", age=36) print(txt) Output: My name is John, I'm 36 To insert values into a string using both positional and keyword arguments: txt = "Hi {name}, welcome to {0} tutorials".format("Guru99", name="Jessica") print(txt) Output: Hi Jessica, welcome to Guru99 tutorials To align and pad values in a string using formatting options: txt = "{:...

Python frozenset() Built in Function

Python frozenset() is a built-in function that returns an immutable set object. A set is a collection of unique and unordered elements that supports operations like union, intersection, difference, etc. A frozenset is like a set, but it cannot be modified or changed once created. Here are some examples of how to use Python frozenset() : To create a frozenset from a list of fruits: fruits = ["apple", "banana", "cherry"] fset = frozenset(fruits) print(fset) Output: frozenset({'apple', 'banana', 'cherry'}) To create an empty frozenset: fset = frozenset() print(fset) Output: frozenset() To perform set operations with frozensets: a = frozenset([1, 2, 3]) b = frozenset([3, 4, 5]) print(a.union(b)) # returns a new frozenset with elements from both a and b print(a.intersection(b)) # returns a new frozenset with elements common to both a and b print(a.difference(b)) # returns a new frozenset with elements in a but not in b Output: fro...

Python getattr() Built in Function

Python getattr() is a built-in function that returns the value of a named attribute of an object. It can also take a default value as an optional argument, which is returned if the attribute does not exist. Here are some examples of how to use Python getattr() : To get the value of an existing attribute of an object: class Person:     name = "John"     age = 36     country = "Norway" person = Person() print(getattr(person, "name")) # returns "John" print(getattr(person, "age")) # returns 36 To get the value of a non-existing attribute of an object with a default value: class Person:     name = "John"     age = 36     country = "Norway" person = Person() print(getattr(person, "city", "Unknown")) # returns "Unknown" To implement a custom getattr() method for an object that dynamically computes attribute values: class Square:     def __init__(self, side):     ...

Python globals() Built in Function

Python globals() is a built-in function that returns a dictionary containing the current global symbol table. A symbol table is a data structure that stores information about the names and values of variables, functions, classes, etc. in a program. A global symbol table contains the names and values that are accessible from any scope in the program. Here are some examples of how to use Python globals() : To access and modify a global variable using globals() : x = 10 # global variable print("The value of x is:", x) # prints 10 globals()["x"] = 20 # modifies x using globals() print("The value of x is:", x) # prints 20 To get information about the current program using globals() : print("The name of this module is:", globals()["__name__"]) # prints __main__ print("The file name of this module is:", globals()["__file__"]) # prints the file name To define a new global variable using globals() :  globals()["y...