Posts

Python exec() Built in Function

The exec() function in Python is used to execute a dynamically generated code, which is a code that is created or modified at runtime. The exec() function takes a string or an object that contains a valid Python code and executes it as if it was written in a file. You can also pass a dictionary of global and local variables to the exec() function to modify the execution environment. Here are some examples of using exec() in Python: Execute a Python statement stored as a string: code = 'print("Hello, world!")' exec(code) Output: Hello, world! In this example, exec(code) executes the Python statement 'print("Hello, world!")', which prints the string "Hello, world!" to the console. Use exec() to execute a Python script stored in a file: with open('script.py', 'r') as file:     code = file.read() exec(code) In this example, we open the file 'script.py', read its contents into a string, and then use exec() to execu...

Python eval() Built in Function

The eval() function in Python is used to evaluate a string or an object that contains a valid Python expression, which is a code that returns a value. The eval() function takes a string or an object that contains a valid Python expression and evaluates it as if it was written in a file. You can also pass a dictionary of global and local variables to the eval() function to modify the evaluation environment. Here are some examples of using eval() in Python: Evaluate a mathematical expression stored as a string: result = eval('3 + 4 * 2') print(result) Output: 11 In this example, eval('3 + 4 * 2') evaluates the mathematical expression '3 + 4 * 2' and returns the result (11). Note that eval() can be dangerous if used improperly, as it allows arbitrary code execution. You should only use eval() with trusted input. Evaluate a Python expression that references variables in your program: x = 5 y = 10 result = eval('x * y') print(result) Output: 50 In this...

Python divmod() Built in Function

The divmod() function in Python is used to find the quotient and remainder of dividing two numbers. The divmod() function takes two numbers as arguments and returns a tuple containing the quotient and remainder as (quotient, remainder). The numbers can be integers or floats, but they must be non-complex. Here are some examples of using divmod() in Python: Divide two numbers and get the quotient and remainder: >>> divmod(10, 3) (3, 1) In this example, divmod(10, 3) divides 10 by 3, and returns a tuple containing the quotient 3 and the remainder 1. Calculate the number of hours and minutes in a given number of minutes: >>> total_minutes = 135 >>> hours, minutes = divmod(total_minutes, 60) >>> print(hours, "hours", minutes, "minutes") 2 hours 15 minutes In this example, we start with a total number of minutes (135) and use divmod(total_minutes, 60) to calculate the number of hours and minutes. The divmod() function returns a tu...

Python dir() Built in Function

The dir() function in Python is used to get a list of attributes and methods of an object. The dir() function takes an object as an argument and returns a list of strings containing the names of its attributes and methods. The object can be any Python data type or user-defined class. Here are some examples of using dir() in Python: Get all the attributes and methods of a built-in Python object:  >>> dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr_...

Python enumerate() Built in Function

Python enumerate() is a built-in function that returns an enumerate object. It allows you to loop over a collection (such as a list, tuple, string, etc.) and keep track of the index and value of each element. You can use it in for loops or convert it to a list or dictionary. Here are some examples of how to use Python enumerate() : To print the index and value of each element in a list: Iterate over a list and print the index and value of each item: fruits = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(fruits):     print(index, fruit) Output: 0 apple 1 banana 2 cherry 0 apple 1 banana 2 cherry In this example, enumerate(fruits) returns an iterator that produces tuples containing the index and value of each item in the list. We use a for loop to iterate over this iterator and print the index and value of each item. Create a dictionary that maps the index of each word in a list to the word itself: words = ['foo', 'bar', 'baz...