Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def tuple_length(tup):
- return len(tup)
- my_tuple = (1, 2, 3, 4, 5)
- print("Length of the tuple:", tuple_length(my_tuple))
- Output:
- Length of the tuple: 5
- def tuple_to_string(tup):
- return ''.join(tup)
- my_tuple = ('Hello', ' ', 'world', '!')
- result = tuple_to_string(my_tuple)
- print("Converted string:", result)
- Output:
- Converted string: Hello world!
- def common_elements(tup1, tup2):
- common = tuple(x for x in tup1 if x in tup2)
- return common
- tuple1 = (1, 2, 3, 4, 5)
- tuple2 = (4, 5, 6, 7, 8)
- result = common_elements(tuple1, tuple2)
- print("Common elements:", result)
- Output:
- Common elements: (4, 5)
- def merge_tuples(tup1, tup2):
- merged_list = list(tup1)
- for item in tup2:
- if item not in merged_list:
- merged_list.append(item)
- merged_tuple = tuple(merged_list)
- return merged_tuple
- tuple1 = (1, 2, 3, 4, 5)
- tuple2 = (4, 5, 6, 7, 8)
- result = merge_tuples(tuple1, tuple2)
- print("Merged tuple without duplicates:", result)
- Output:
- Merged tuple without duplicates: (1, 2, 3, 4, 5, 6, 7, 8)
- def even_odd_numbers(tup):
- even_numbers = tuple(num for num in tup if num % 2 == 0)
- odd_numbers = tuple(num for num in tup if num % 2 != 0)
- return even_numbers, odd_numbers
- my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
- even, odd = even_odd_numbers(my_tuple)
- print("Even numbers:", even)
- print("Odd numbers:", odd)
- Output:
- Even numbers: (2, 4, 6, 8)
- Odd numbers: (1, 3, 5, 7, 9)
- def tuple_product(tup):
- product = 1
- for num in tup:
- product *= num
- return product
- my_tuple = (1, 2, 3, 4, 5)
- result = tuple_product(my_tuple)
- print("Product of all elements in the tuple:", result)
- Output:
- Product of all elements in the tuple: 120
- emails = tuple()
- username = tuple()
- domainname = tuple()
- n = int(input("How many email ids you want to enter?: "))
- for i in range(0, n):
- emid = input("> ")
- emails = emails + (emid,)
- spl = emid.split("@")
- username = username + (spl[0],)
- domainname = domainname + (spl[1],)
- print("\nThe email ids in the tuple are:")
- print(emails)
- print("\nThe username in the email ids are:")
- print(username)
- print("\nThe domain name in the email ids are:")
- print(domainname)
- Output:
- How many email ids you want to enter?: 2
- > arijit@gmail.com
- > abir@outlook.com
- The email ids in the tuple are:
- ('arijit@gmail.com', 'abir@outlook.com')
- The username in the email ids are:
- ('arijit', 'abir')
- The domain name in the email ids are:
- ('gmail.com', 'outlook.com')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement