Files
python-bootcamp/max submission/6_9_HW.py
T

110 lines
4.6 KiB
Python
Raw Normal View History

2023-06-09 13:05:45 -05:00
# question 1:
#boolean context:
#In programming, a boolean context refers to a situation where a condition or expression is evaluated as either true or false.
#A boolean context typically occurs in control flow statements, such as if statements or while loops, where the condition determines whether
#a certain block of code should be executed or repeated.
#Ellipsis:
#an ellipsis (...) is a special token that represents an ellipsis object. It is often used as a placeholder in code or data structures where
#you intend to fill in additional functionality or content later.
#machine-level double precision floating point numbers
#Machine-level double precision floating-point numbers, often referred to as double, are a common representation of real numbers in computer systems.
#They are a binary format used to store and manipulate floating-point values with higher precision compared to single precision.
#Callable types:
#callable types refer to objects that can be called as functions. These objects are capable of executing code when invoked with parentheses, just
#like a regular function.
#cell_contents:
#the term "cell contents" refers to the value stored in a cell of a closure. A closure is a function object that retains references to variables
#in its enclosing lexical scope, even when the outer function has finished executing.
#When a closure is created, each variable it references is stored in a cell, which is an internal data structure associated with the closure.
#The cell contents refer to the value stored within the cell associated with a particular variable in a closure. It represents the value of the
#variable as seen by the closure, regardless of any changes made to the variable in the outer scope.
#example for "When an instance method object is created by retrieving a user-defined function object from a class via one of its instances,
#its __self__ attribute is the instance, and the method object is said to be bound. The new method’s __func__ attribute is the original function
#object."
2023-06-09 18:53:55 -05:00
"""""
2023-06-09 13:05:45 -05:00
class MyClass:
def my_method(self):
print("Hello, world!")
# Creating an instance of MyClass
my_instance = MyClass()
# Retrieving the method object from the instance
method_object = my_instance.my_method
# Accessing the attributes of the method object
print(method_object.__self__) # Output: <__main__.MyClass object at 0x...>
print(method_object.__func__) # Output: <function MyClass.my_method at 0x...>
# Calling the method through the method object
method_object() # Output: Hello, world!
2023-06-09 18:53:55 -05:00
"""""
2023-06-09 13:05:45 -05:00
#async def:
# the async def syntax is used to define an asynchronous function, also known as a coroutine. An asynchronous function allows concurrent
# execution of multiple tasks by allowing the program to proceed to other tasks while waiting for certain operations to complete.
2023-06-09 18:53:55 -05:00
"""""
2023-06-09 13:05:45 -05:00
import asyncio
async def my_coroutine():
print("Coroutine started")
await asyncio.sleep(1) # Simulating an asynchronous operation
print("Coroutine completed")
# Calling the coroutine
asyncio.run(my_coroutine())
2023-06-09 18:53:55 -05:00
"""""
2023-06-09 13:05:45 -05:00
#Static method and class method:
#In Python, static methods and class methods are two types of methods that can be defined within a class.
#Static methods:
#Static methods are defined using the @staticmethod decorator or by declaring them inside the class without any special decorator.
#Static methods are not bound to a specific instance or the class itself. They behave like regular functions but are defined within the class for organizational purposes.
#Static methods do not have access to the instance or class attributes and cannot modify them directly.
#Static methods can be called on the class itself or on an instance of the class.
#Class methods:
#Class methods are defined using the @classmethod decorator.
#Class methods are bound to the class itself rather than the instances. They receive the class as the first argument (usually named cls) instead of the instance (self).
#Class methods have access to the class attributes and can modify them if needed.
#Class methods can be called on the class itself or on an instance of the class.
# question 2:
def add_numbers(a, b):
result = a + b
return result
def multiply_numbers(x, y):
result = x * y
return result
def main():
number1 = 5
number2 = 10
sum_result = add_numbers(number1, number2)
2023-06-09 18:53:55 -05:00
breakpoint() # Set breakpoint here
2023-06-09 13:05:45 -05:00
product_result = multiply_numbers(sum_result, number2)
print("Product:", product_result)
if __name__ == "__main__":
main()
# question 3:
2023-06-09 18:53:55 -05:00
def main():
2023-06-09 13:05:45 -05:00
x = 5
2023-06-09 18:53:55 -05:00
y = 2
breakpoint()
result = x + y # Line of interest
2023-06-09 13:05:45 -05:00
2023-06-09 18:53:55 -05:00
print(result)
2023-06-09 13:05:45 -05:00
2023-06-09 18:53:55 -05:00
main()
2023-06-09 13:05:45 -05:00