From 2556ad33ca8436359656dc1a030e8dd01297c024 Mon Sep 17 00:00:00 2001 From: shunan zhao Date: Fri, 9 Jun 2023 13:05:45 -0500 Subject: [PATCH] shunan zhao HW --- max submission/6_9_HW.py | 110 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 max submission/6_9_HW.py diff --git a/max submission/6_9_HW.py b/max submission/6_9_HW.py new file mode 100644 index 0000000..cd0e28f --- /dev/null +++ b/max submission/6_9_HW.py @@ -0,0 +1,110 @@ +# 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." +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: + +# Calling the method through the method object +method_object() # Output: Hello, world! + + #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. +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()) + + #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) + product_result = multiply_numbers(sum_result, number2) + print("Product:", product_result) + +if __name__ == "__main__": + main() + +# question 3: +def foo(): + a = 10 + b = 20 + return a + b + +def bar(): + x = 5 + y = 7 + return x * y + +result = foo() + bar() +print(result) +# In this example, using "Step Over" and "Step Into" may not yield different results, but the behavior is +# dependent on the implementation of the functions being called. + + + + +