Files
python-bootcamp/max submission/6_9_HW.py
T
2023-06-11 16:23:26 -05:00

119 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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: <function MyClass.my_method at 0x...>
# 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)
breakpoint() # Set breakpoint here
product_result = multiply_numbers(sum_result, number2)
print("Product:", product_result)
if __name__ == "__main__":
main()
# question 3:
def calculate_square(x):
square = x ** 2
return square
def calculate_cube(x):
cube = x ** 3
return cube
def main():
number = 5
breakpoint() # Set breakpoint here
square_result = calculate_square(number)
cube_result = calculate_cube(number)
print("Square:", square_result)
print("Cube:", cube_result)
if __name__ == "__main__":
main()