How to Print a List in Python

0 votes

Hi guys, I've been practicing lists in python recently and I've been having issues trying to print a list in python. I want to print out all the items in a list with the code below, especially the last two lines

def primefind(n):
  mylist = []
  x = 3
  while (x < n/2):
    if ((n % x) == 0):
      mylist.append(x)
      x = x + 2
  for item in mylist:
    print item

I am getting a syntax error when I run this. It highlights "item" in the last line. As far as I can tell it's syntactically correct so I am confused! Even this site shows the same syntax as I've used http://effbot.org/zone/python-list.htm#looping

Any ideas where I've gone wrong?

Aug 31, 2018 in Python by ariaholic
• 7,340 points
77 views

1 answer to this question.

0 votes

ou are using Python 2.x syntax with a 3.x interpreter. print is a function in Python 3.x, so you should use

print(item)

instead.

answered Aug 31, 2018 by charlie_brown
• 7,720 points

Related Questions In Python

+2 votes
11 answers
0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,809 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 13,160 points
69 views
0 votes
1 answer

List comprehension on a nested list - How to do it in Python?

Not sure what your desired output is, ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,520 points
89 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 230 views
+4 votes
6 answers
0 votes
1 answer

How to round a floating point number up to certain decimal place in Python?

This is normal (and has nothing to do ...READ MORE

answered Oct 8, 2018 in Python by charlie_brown
• 7,720 points
43 views
0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
111 views