+ 1

Please I need help to understand tuples in Python

Without removing the + concatenation operator, what must I change to get this output: (-1, 0, 1 ‘bananas’) tup11 = (-1, 0, 1) tup12 = (‘bananas’) tup13 = tup11 + tup12

13th Oct 2021, 8:30 PM
Jenkins
8 Antworten
+ 4
All you need to do is to change structure to list then back it to tuple: print(tuple(list(tup11)+list(tup12))) or just tup12=('banana',) now print(tup11+tup12)
13th Oct 2021, 8:48 PM
Amirreza
Amirreza - avatar
+ 1
Amirreza Hashemi Thanx bruh!
13th Oct 2021, 8:56 PM
Jenkins
+ 1
Amirreza Hashemi I tried the second example you gave and it’s giving me an error tup11 = (-1, 0, 1) tup12 = (‘bananas',) print(tup11 + tup12)
13th Oct 2021, 9:05 PM
Jenkins
+ 1
tup11 = (-1, 0, 1) tup12 = ('bananas',) print(tup11 + tup12) #now use
13th Oct 2021, 9:07 PM
Amirreza
Amirreza - avatar
+ 1
Amirreza Hashemi thanx it worked, just to clarify i don’t really need to convert it to a list and then back to a tuple?
13th Oct 2021, 9:12 PM
Jenkins
+ 1
Jenkins no you don't just all you need is to change the tup12 data type because In your example tup12 is a string but if you want to plus them you should change tup12 Data type to tuple
13th Oct 2021, 9:15 PM
Amirreza
Amirreza - avatar
+ 1
Amirreza Hashemi Understood! Thank you for everything
13th Oct 2021, 9:36 PM
Jenkins
+ 1
Amirreza Hashemi Good explication. Jenkins can also put this code(I was try) tup11 = [-1, 0, 1] tup12 = ["bananas"] tup13 = tup11 + tup12 print(tuple(tup13)
15th Oct 2021, 12:51 AM
CGO!
CGO! - avatar