Advertisement
here2share

### python test code

Aug 31st, 2023 (edited)
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. code = '''
  2. # Comment
  3. import re, tkinter
  4.  
  5. # Function definition
  6. def add(a, b):
  7.    return a + b
  8.  
  9. # Function call
  10. result = add(2, 3)
  11.  
  12. # Assignment
  13. x = 10
  14.  
  15. # If statement
  16. if x > 5:
  17.    print("x is greater than 5")
  18. else:
  19.    print("x is less than or equal to 5")
  20.  
  21. # For loop
  22. for i in range(5):
  23.    print(i)
  24.    
  25. # For loop with steps
  26. for i in range(20, 100, 10):
  27.    print(i)
  28.  
  29. # While loop
  30. count = 0
  31. while count < 5:
  32.    print(count)
  33.    count += 1
  34.  
  35. # Class definition
  36. class MyClass:
  37.    def __init__(self):
  38.        self.value = 0
  39.  
  40.    def increment(self):
  41.        self.value += 1
  42.  
  43. # Class instantiation
  44. obj = MyClass()
  45.  
  46. # Method call
  47. obj.increment()
  48.  
  49. # Exception handling
  50. try:
  51.    result = 10 / 0
  52. except ZeroDivisionError as e:
  53.    print("Error:", str(e))
  54.  
  55. # Regular expression matching
  56. pattern = r'\w+'
  57. string = "Hello, World!"
  58. match = re.search(pattern, string)
  59. if match:
  60.    print("Match found:", match.group())
  61.  
  62. # Canvas
  63. canvas = Canvas(width=100, height=200)
  64.  
  65. # Entry
  66. my_entry = Entry()
  67.  
  68. # Text
  69. text_widget = Text("Some text")
  70.  
  71. # Listbox
  72. my_listbox = Listbox(values=[1, 2, 3])
  73.  
  74. # Label
  75. label_widget = Label("Hello")
  76.  
  77. # Button
  78. button_widget = Button(text="Click me", command=my_function)
  79.  
  80. # Radiobutton
  81. my_radio = Radiobutton(value="option1")
  82.  
  83. # Slider
  84. my_slider = Slider(min_value=0, max_value=100, step=5)
  85.  
  86. # Bind
  87. bind_event("<click>", "my_element", "my_function")
  88.  
  89. # create_polygon
  90. create_polygon(points=[10, 20, 30, 40, 50, 60])
  91.  
  92. # create_arc
  93. create_arc(x=10, y=20, width=30, height=40, start=50, extent=60)
  94.  
  95. # Menu
  96. my_menu = Menu("My Menu", items=["Item 1", "Item 2", "Item 3"])
  97.  
  98. # OptionMenu
  99. my_variable = OptionMenu(menu=my_menu, options=["Option 1", "Option 2"])
  100.  
  101. # Message
  102. message_widget = Message("Hello")
  103.  
  104. # Exception handling
  105. try:
  106.    # Some code
  107.    pass
  108. except MyException as e:
  109.    # Handle exception
  110.    pass
  111.  
  112. # Global variables
  113. x = None
  114. y = None
  115. z = None
  116.  
  117. # For loop
  118. for i in range(1, 10, 2):
  119.    pass
  120.  
  121. # Range
  122. values = list(range(5))
  123.  
  124. # Dictionary
  125. data = {"a": 1, "b": 2, "c": 3}
  126.  
  127. # ImageFilter
  128. blur_filter = ImageFilter.BLUR()
  129.  
  130. # 'self'
  131. self.value = 10
  132.  
  133. # Import
  134. from module import function, constant
  135.  
  136. # Function definition
  137. def my_function():
  138.    pass
  139.  
  140. # Return statement
  141. def square(x):
  142.    return x * x
  143.  
  144. # Try-except-finally
  145. try:
  146.    # Some code
  147.    pass
  148. except MyException as e:
  149.    # Handle exception
  150.    pass
  151. finally:
  152.    # Cleanup code
  153.    pass
  154.  
  155. # Raise exception
  156. raise ValueError("Invalid value")
  157.  
  158. # Class definition
  159. class MyClass:
  160.    def __init__(self):
  161.        self.value = 0
  162.  
  163.    def increment(self):
  164.        self.value += 1
  165.  
  166. # Print statement
  167. print("Hello, World!")
  168.  
  169. # min() and max()
  170. x = min(5, 3, 8)
  171. y = max(10, 20, 30)
  172.  
  173. # Comparison operators
  174. if x != y:
  175.    print("x is not equal to y")
  176.  
  177. # None
  178. value = None
  179.  
  180. # len()
  181. length = len([1, 2, 3])
  182.  
  183. # sorted()
  184. sorted_list = sorted([3, 1, 2])
  185.  
  186. # rsplit()
  187. parts = "Hello, World!".rsplit(",")
  188.  
  189. # str() and repr()
  190. s = str(10)
  191. r = repr(3.14)
  192.  
  193. # int()
  194. integer = int("42")
  195. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement