Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation of tuples

  1. Normal text:

    • I'm having some problems with coding on python 3.2.1. Actually I'm taking online lectures that are on python 2.5.
  2. Here is the code:

    x = 100
    divisors = ()
    for i in range(1,x):
        if x%i == 0:
            divisors = divisors + (i)
    
  3. on running the program, following error appears:

    divisors = divisors + (i)  
    TypeError: can only concatenate tuple (not "int") to tuple
    
like image 874
Muavia Avatar asked Aug 25 '11 14:08

Muavia


People also ask

Can we concatenate a tuple?

Method #1 : Using + operator This is the most Pythonic and recommended method to perform this particular task. In this, we add two tuples and return the concatenated tuple. No previous tuple is changed in this process.

Can you add 2 tuples in Python?

Tuple AdditionYou can combine tuples to form a new tuple. The addition operation simply performs a concatenation with tuples. You can only add or combine same data types.

How do you add a tuple to a tuple?

Use the list() function(converts the sequence/iterable to a list) to convert both the input tuples into a list. Use the extend() function(adds all the elements of an iterable like list, tuple, string etc to the end of the list) to append or concatenate the above 2nd list to the first list.


1 Answers

(1) is not a tuple, its just a parenthesized expression. To make it a tuple, add a trailing comma, (1,)

like image 194
SingleNegationElimination Avatar answered Sep 18 '22 20:09

SingleNegationElimination