Bittu
Bittu

Reputation: 31

python for loop for multiple condition in python

Below is the code:

for x in range(0, 7) + 100:
    print x

Expected output:

 0
 1
 2
 3
 4
 5
 6
 100

Please help me get this output.

Below is the error of the code:

TypeError: can only concatenate list (not "int") to list

Upvotes: 0

Views: 64

Answers (1)

micsthepick
micsthepick

Reputation: 603

Since you are using Python 2, range is creating a list. To add a number to the end of a list, first put it in a list, then you can use the addition operator:

for x in range(0, 7) + [100]:

(To do this in python 3, you will need to convert the range into a list, as it range(...) creates a different datatype):

for x in list(range(0, 7)) + [100]:

Upvotes: 3

Related Questions