Advertisement
brandblox

python lab (02/04/2024)

Apr 2nd, 2024 (edited)
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. def tuple_length(tup):
  2.     return len(tup)
  3. my_tuple = (1, 2, 3, 4, 5)
  4. print("Length of the tuple:", tuple_length(my_tuple))
  5.  
  6. Output:
  7. Length of the tuple: 5
  8.  
  9. def tuple_to_string(tup):
  10.     return ''.join(tup)
  11.  
  12. my_tuple = ('Hello', ' ', 'world', '!')
  13. result = tuple_to_string(my_tuple)
  14. print("Converted string:", result)
  15.  
  16. Output:
  17. Converted string: Hello world!
  18.  
  19. def common_elements(tup1, tup2):
  20.     common = tuple(x for x in tup1 if x in tup2)
  21.     return common
  22.  
  23. tuple1 = (1, 2, 3, 4, 5)
  24. tuple2 = (4, 5, 6, 7, 8)
  25. result = common_elements(tuple1, tuple2)
  26. print("Common elements:", result)
  27.  
  28. Output:
  29. Common elements: (4, 5)
  30.  
  31. def merge_tuples(tup1, tup2):
  32.     merged_list = list(tup1)
  33.     for item in tup2:
  34.         if item not in merged_list:
  35.             merged_list.append(item)
  36.     merged_tuple = tuple(merged_list)
  37.     return merged_tuple
  38.  
  39. tuple1 = (1, 2, 3, 4, 5)
  40. tuple2 = (4, 5, 6, 7, 8)
  41. result = merge_tuples(tuple1, tuple2)
  42. print("Merged tuple without duplicates:", result)
  43.  
  44. Output:
  45. Merged tuple without duplicates: (1, 2, 3, 4, 5, 6, 7, 8)
  46.  
  47. def even_odd_numbers(tup):
  48.     even_numbers = tuple(num for num in tup if num % 2 == 0)
  49.     odd_numbers = tuple(num for num in tup if num % 2 != 0)
  50.     return even_numbers, odd_numbers
  51.  
  52. my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
  53. even, odd = even_odd_numbers(my_tuple)
  54. print("Even numbers:", even)
  55. print("Odd numbers:", odd)
  56.  
  57. Output:
  58. Even numbers: (2, 4, 6, 8)
  59. Odd numbers: (1, 3, 5, 7, 9)
  60.  
  61. def tuple_product(tup):
  62.     product = 1
  63.     for num in tup:
  64.         product *= num
  65.     return product
  66.  
  67. my_tuple = (1, 2, 3, 4, 5)
  68. result = tuple_product(my_tuple)
  69. print("Product of all elements in the tuple:", result)
  70.  
  71. Output:
  72. Product of all elements in the tuple: 120
  73.  
  74.  
  75. emails = tuple()
  76. username = tuple()
  77. domainname = tuple()
  78. n = int(input("How many email ids you want to enter?: "))
  79. for i in range(0, n):
  80.     emid = input("> ")
  81.     emails = emails + (emid,)
  82.     spl = emid.split("@")
  83.     username = username + (spl[0],)
  84.     domainname = domainname + (spl[1],)
  85.  
  86. print("\nThe email ids in the tuple are:")
  87. print(emails)
  88.  
  89. print("\nThe username in the email ids are:")
  90. print(username)
  91.  
  92. print("\nThe domain name in the email ids are:")
  93. print(domainname)
  94.  
  95.  
  96. Output:
  97. How many email ids you want to enter?: 2
  98. > arijit@gmail.com
  99. > abir@outlook.com
  100.  
  101. The email ids in the tuple are:
  102. ('arijit@gmail.com', 'abir@outlook.com')
  103.  
  104. The username in the email ids are:
  105. ('arijit', 'abir')
  106.  
  107. The domain name in the email ids are:
  108. ('gmail.com', 'outlook.com')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement