import asyncio
import os as operating_system
from collections import namedtuple
from math import *
from math import acos as soca
from typing import Dict, List
from ..parent import x
from .sibling import y
test_var: int = 10
def free_func():
global test_var
test_var += 1
print(f"Global test_var is now {test_var}")
def func_decorator(func):
def wrapper(*args, **kwargs):
print("Function decorator called")
return func(*args, **kwargs)
return wrapper
@func_decorator
def decorated_func():
print("Inside decorated function")
class TestClass:
class_var = "Class variable"
@staticmethod
def static_decorator(func):
def wrapper(*args, **kwargs):
print("Static method decorator called")
return func(*args, **kwargs)
return wrapper
@classmethod
def class_method(cls) -> None:
cls.class_var += " updated"
print(f"Class variable is now {cls.class_var}")
def instance_method(self) -> None:
self.instance_var = "Instance variable"
print(f"Instance variable is {self.instance_var}")
@staticmethod
@static_decorator
def static_method() -> None:
print("Inside static method")
square = lambda x: x * x
multi_line_str = """
This is a
multi-line string
for testing purposes.
"""
multiline_f_string = f"""This is a
multiline{f_string} string
spanning several lines
"""
raw_string = r"This is a raw string with no special treatment for \n"
bytes_string = b"This is a bytes string"
bytes_string = rf"This is a raw f-string with {raw_string}"
squared_numbers = ["x" + square(x) for x in range(10)]
unique_squares = {square(x) for x in range(10)}
squares_dict = {x: square(x) for x in range(10)}
def exception_handling(x) -> None:
try:
if x < 0:
raise ValueError("Negative value")
elif x == 0:
raise ZeroDivisionError("Division by zero")
result = 10 / x
except ZeroDivisionError as e:
print(f"Caught an exception: {e}")
except ValueError as e:
print(f"Caught an exception: {e}")
else:
print("No exceptions caught")
finally:
print("This will always be printed")
def modify_nonlocal():
nonlocal_var = "Initial value"
def inner():
nonlocal nonlocal_var
nonlocal_var = "Modified value"
inner()
print(f"Nonlocal variable is {nonlocal_var}")
def inplace_operations():
x = 10
x += 5
x -= 3
x *= 2
x /= 4
print(f"Inplace operations result: {x}")
def control_flow():
if test_var > 5:
print("test_var is greater than 5")
else:
print("test_var is 5 or less")
counter = 0
while counter < 3:
print(f"Counter is {counter}")
counter += 1
for i in range(3):
print(f"Loop iteration {i}")
with open(__file__) as f:
content = f.readline()
print("Read from file:", content)
def match_statement(x):
match x:
case 0:
print("Zero")
case 1:
print("One")
case _:
print("Other")
async def async_function():
await asyncio.sleep(1)
print("Async function executed")
if __name__ == "__main__":
free_func()
decorated_func()
TestClass.class_method()
instance = TestClass()
instance.instance_method()
TestClass.static_method()
print(square(5))
exception_handling(0)
modify_nonlocal()
inplace_operations()
control_flow()
match_statement(1)
asyncio.run(async_function())