+ 1
hey, when i code x = [1,2,3] print(x) ->>> I get [1,2,3] When i code x = [1,2,3] print(x[]) I get an error How do i print 1,2,3 on my console without the '[]' around my numbers? how do i print my whole list?
6 odpowiedzi
+ 2
you define list x = [1, 2, 3] , so when you print (x),
it'll print a whole list of x = [1, 2, 3].
if you code print (x[]) , you need to assing index of list
that you want to print but not more than length of list x
if you want to print each item in the whole list ,
you need to use loop like for , example
x = [1, 2, 3] #x has 3 members #x'lenght = 3
for i in range(len(x)): #range(len(x)) = range(3) = [0, 1, 2]
print (x[i]) #print each value of x by index i #this'll print and get new line
#print (x[i] , end = " ") #this'll print with the same line
+ 1
Paranoid has a point, but his code is wrong (missing incrementing i) and non pythonic.
for item in x:
print(item)
+ 1
you're right! I'll edit it!!
0
x[1] print 1
x[2] print 2
x[3] print 3
its an array lile a table
0
it's a list so you always will have the value wrapped into []. you could print the single values without brackets with a for loop
i = 0
for i < len(x)
print(x[i])
i += 1
- 2
Paranoid, it also should be while, not for. And if it's for, it's for i in range(len(x)) :)
Beauty1234's answer is the best.