473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help With Python


I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, … Spam").

Can anybody help me get started? I am completely new to programming!

Thanks in advance!

Jul 18 '05 #1
15 1867
Am Wed, 26 Jan 2005 15:55:28 +0000 schrieb Judi Keplar:

I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, … Spam").

Can anybody help me get started? I am completely new to programming!


Hi,

# This way, there is a comma after the last:
import sys
for i in range(511):
sys.stdout.write("Spam, ")

# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
print ", ".join(mylist)

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
Jul 18 '05 #2
Judi Keplar schrieb:
I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, … Spam").

Can anybody help me get started? I am completely new to programming!


Online:

- http://www.python.org/moin/BeginnersGuide (Beginner, Advanced)
- http://www.freenetpages.co.uk/hp/alan.gauld/ (Beginner)
- http://docs.python.org/tut/tut.html (Beginner)
- http://diveintopython.org/ (Advanced)

Books (Look for most recent editions):

- Mark Lutz, Learning Python (Beginner)
- Alex Martelli, Python in a Nutshell (Beginner, Advanced)
- Frederik Lundh, Python Standard Library (Beginner, Advanced)
- Alex Martelli, Python Cookbook (Beginner, Advanced)
- Mark Pilgrim Dive Into Python (Advanced)

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Jul 18 '05 #3
Judi Keplar wrote:
I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, … Spam").

Can anybody help me get started? I am completely new to programming!

Thanks in advance!

print "Spam, " * 510 + "Spam"


Will McGugan
Jul 18 '05 #4
>>> print "Spam, " * 510 + "Spam"
Or if you have 2.4..
print ", ".join( "Spam" for _ in xrange( 511 ) )


Although, the replys with links will ultimately be more helpful!

Will McGugan
Jul 18 '05 #5
Py>>> print "Spam, " * 115
Spam, Spam, Spam, Spam, Spam, ................. Spam,

Multiplies (repeats) the string 115 times.

To eliminate the last ", " (that is, a comma followed by space), you
can do it using the slice notation and say:
Py>>> print ("Spam, " * 115) [:-2]
Spam, Spam, Spam, Spam, Spam, ................., Spam

The [:-2] means that you asking Python to print everything from the
beginning to the last but two characters of the string. You can also
write this as [0:-2]

[or [0:length_of_spam - 2] where length_of_spam = len("Spam, " * 115)]

Since you are starting off, please read and follow the tuturial that
available with your Python installation or on the Python.org site at
http://docs.python.org/tut/tut.html

Thank you,
--Kartic

Jul 18 '05 #6
Thomas Guettler wrote:
Am Wed, 26 Jan 2005 15:55:28 +0000 schrieb Judi Keplar:

I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, � Spam").

Can anybody help me get started? I am completely new to programming!

Hi,

# This way, there is a comma after the last:
import sys
for i in range(511):
sys.stdout.write("Spam, ")

# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
print ", ".join(mylist)


# also no comma at the end, using a list comprehension
print ", ".join(["Spam" for _ in xrange(511)])

# also no comma at the end, using a generator expresssion (Python 2.4)
print ", ".join("Spam" for _ in xrange(511))

Steve
Jul 18 '05 #7
Thomas Guettler wrote:
# No comma at the end:
mylist=[]
for i in range(511):
mylist.append("Spam")
or just
mylist = ["Spam"] * 511

Kent
print ", ".join(mylist)

Thomas

Jul 18 '05 #8
In article <ma***************************************@python. org>,
Judi Keplar <ju*********@charter.net> wrote:

I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, =85 Spam").

Can anybody help me get started? I am completely new to programming!


Well, I'll point you in a couple of good directions.

The first direction would be to approach this as a traditional
procedural programming problem. You need some kind of loop that can
exectute a print statement 511 times. In Python, that looks something
like:

for i in range(511):
print "spam"

Almost immediately, the question that comes to mind is where, exactly,
does the loop start and stop? For example, you might expect that

for i in range(5):
print i

would print the numbers 1 through 5, but it really prints 0 through
4. This kind of "off by one" thing is a very common common issue in
writing loops in almost any programming language. It's a common
enough issue that a whole class of bugs is named after it, known as
"fencepost errors". So, left as an exercise for the reader
(i.e. you), is to figure out if "for i in range(511)" really does the
loop 511 times, or maybe 510 or 512?

Next, you'll notice that the first loop I wrote prints one "spam" on
each line. The way you described the problem, you want all the spams
on one big long line, with commas between each one. The way you get
Python to not advance to the next line is to end the print statement
with a comma, like this:

for i in range(511):
print "spam",

Notice that there's something tricky going on here; the comma doesn't
get printed, it's just a way of telling the print statement, "don't go
on to the next line". To really get it to print a comma, you need
something like:

for i in range(511):
print "spam,",

The first comma is inside the quoted string, so it gets printed. The
second one is the one that says "don't go to the next line".

You're still not quite done, but I've given you enough hints. Go play
with that, see what you get, and see if you can figure out what
problems you still need to fix.

I said I'd point you in a couple of good directions, and the second
one is to look up how the "join()" string method works. It's really
cool, and solves your problem in a different way. But, first I think
you should master the straight-forward way.

Lastly, the really cool Pythonic way would be to get a bunch of
Vikings into a restaurant and give them all breakfast menus. The only
problem there is you'd have trouble handing all those Vikings in with
your assignment.
Jul 18 '05 #9
Peter Maas wrote:
Can anybody help me get started? I am completely new to programming!
Online:

- http://www.python.org/moin/BeginnersGuide (Beginner, Advanced)
- http://www.freenetpages.co.uk/hp/alan.gauld/ (Beginner)
- http://docs.python.org/tut/tut.html (Beginner)
- http://diveintopython.org/ (Advanced)
also:

http://www.byteofpython.info/
Books (Look for most recent editions):

- Mark Lutz, Learning Python (Beginner)
- Alex Martelli, Python in a Nutshell (Beginner, Advanced)
- Frederik Lundh, Python Standard Library (Beginner, Advanced)
on-line here: http://effbot.org/librarybook
on-line here: http://effbot.org/zone/librarybook.htm (pdf version)
- Alex Martelli, Python Cookbook (Beginner, Advanced)
- Mark Pilgrim Dive Into Python (Advanced)


on-line here: http://diveintopython.org

</F>

Jul 18 '05 #10

Here's my Monty Pythonic answer:

## cut here
class Viking():

def __init__():
pass

def order():
return 'Spam'

# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.
v = Viking()
orders = [ v.order() ] * 511

print ', '.join(orders)
## cut here

With no apologies to Eric Idle,

Nick
--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python
print reduce(lambda x,y:x+chr(ord(y)-1),' Ojdl!Wbshjti!=obwAcboefstobudi/psh?')
Jul 18 '05 #11
Nick Vargish wrote:
Here's my Monty Pythonic answer:

## cut here
class Viking():

def __init__():
pass

def order():
return 'Spam'

# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.
v = Viking()
orders = [ v.order() ] * 511

print ', '.join(orders)


No need to write a loop:

py> class Viking(object):
.... def order(self):
.... return 'Spam'
....
py> v = Viking()
py> orders = [v.order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'
py> orders = [Viking().order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'

Steve
Jul 18 '05 #12
Nick Vargish wrote:
Here's my Monty Pythonic answer:

## cut here
class Viking():

def __init__():
pass

def order():
return 'Spam'

# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.
v = Viking()
orders = [ v.order() ] * 511

print ', '.join(orders)
## cut here

With no apologies to Eric Idle,

Nick
--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python print reduce(lambda x,y:x+chr(ord(y)-1),'

Ojdl!Wbshjti!=obwAcboefstobudi/psh?')

Not to be nit-picky, but you got some issues here ;-). Never forget
the importance of "self"!

#######################
class Viking:
.. def __init__(self):
.. pass
.. def sing(self):
.. return 'Spam'
v = Viking()
spam_song = [ v.sing() ] * 511
print ', '.join(spam_song)

Jul 18 '05 #13
"Matt" <ma*************@countrywide.com> writes:
Not to be nit-picky, but you got some issues here ;-). Never forget
the importance of "self"!


Teach me to post untested code. *hangs head*

Nick

--
# sigmask || 0.2 || 20030107 || public domain || feed this to a python
print reduce(lambda x,y:x+chr(ord(y)-1),' Ojdl!Wbshjti!=obwAcboefstobudi/psh?')
Jul 18 '05 #14
Steven Bethard <st************@gmail.com> wrote:
Nick Vargish wrote:
# this is one viking making one order repeated 511 times. if you want
# 511 vikings making seperate orders, you'll have to write a loop.


No need to write a loop:

py> class Viking(object):
... def order(self):
... return 'Spam'
...
py> v = Viking()
py> orders = [v.order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'
py> orders = [Viking().order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'


Thats still one Viking making 7 orders surely?

Eg
vikings = [Viking()] * 7
vikings[0] is vikings[1] True

whereas
vikings = [Viking() for _ in range(7)]
vikings[0] is vikings[1] False

So you want this...
orders = [ Viking().order() for _ in range(7) ]


--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Jul 18 '05 #15
Nick Craig-Wood wrote:
Steven Bethard <st************@gmail.com> wrote:
py> orders = [Viking().order()] * 7
py> ', '.join(orders)
'Spam, Spam, Spam, Spam, Spam, Spam, Spam'


Thats still one Viking making 7 orders surely?

So you want this...
orders = [ Viking().order() for _ in range(7) ]


Right, right. Thanks for the correction! This is why I never use the *
operator on lists. ;)

Steve
Jul 18 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Guido van Rossum | last post by:
I'm pleased to announce that the Python Software Foundation (PSF) is now accepting donations from individuals and companies interested in supporting our mission to improve the Python programming...
10
by: Jacek Generowicz | last post by:
Where can I find concise, clear documentation describing what one has to do in order to enable Python's internal help to be able to provide descriptions of Python keywords ? I am in a situation...
0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
3
by: stuart_white_ | last post by:
I've just upgraded from Python 2.3.3 to Python 2.4.2, and, although the new version of Python seems to be running correctly, I can't seem access the help from the interpreter. On Python 2.3.3...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: Paul Rubin | last post by:
In Windows if you click the Help dropdown, IDLE launches a help window as it should. The help contents are included in the installation. In Linux, clicking Help launches a web browser, which is...
0
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please...
2
by: Scott Smith | last post by:
To all you vi/vim users out there..... I am just getting into python and am trying to learn how to use the python.vim script. I really like the fact that it autoindents for me while inserting...
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
0
by: Ahmed, Shakir | last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server: Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.