Advertisement
GamerBhai02

Python Exp 4

Mar 27th, 2025 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | Source Code | 0 0
  1. import threading as t
  2. def square(num):
  3.     print(f"Square of {num} is {num**2}")
  4. def cube(num):
  5.     print(f"Cube of {num} is {num**3}")
  6. number = int(input("Enter a number to find square and cube:"))
  7. thread1 = t.Thread(target=square,args=(number,))
  8. thread2 = t.Thread(target=cube,args=(number,))
  9. thread1.start()
  10. thread2.start()
  11. thread1.join()
  12. thread2.join()
  13. print("Both threads have executed successfully")
  14.  
  15.  
  16. import threading as t
  17. import time
  18. def print_numbers():
  19.     for i in range(1,6):
  20.         print(f"Number: {i}")
  21.         time.sleep(1)
  22. def print_alphabets():
  23.     for char in 'ABCDE':
  24.         print(f"Alphabet: {char}")
  25.         time.sleep(1)
  26. thread1 = t.Thread(target=print_numbers)
  27. thread2 = t.Thread(target=print_alphabets)
  28. thread1.start()
  29. thread2.start()
  30. thread1.join()
  31. thread2.join()
  32.  
  33. print("Both threads have executed successfully")
Tags: Exp4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement