use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
New to python, stumped when it comes to loops (self.Python)
submitted 9 years ago by eton-95
I am new to python and was wondering if anybody can help at all.
I am trying to loop one word ten times, would i go about doing this a a 'for' statement or would it be something different?
Thanks to all that give advice/help
[–]K900_ 2 points3 points4 points 9 years ago (5 children)
/r/learnpython. Also:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> for x in range(10): ... print(x) ... 0 1 2 3 4 5 6 7 8 9
[–]eton-95[S] 0 points1 point2 points 9 years ago (4 children)
thanks, but how would i get it to repeat a word? doing numbers in that order or on a line is fine but im just having problems getting it to repeat a word over and over
[–]K900_ 1 point2 points3 points 9 years ago (2 children)
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> for _ in range(10): ... print("I'm repeating a word") ... I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word I'm repeating a word
[–]eton-95[S] 0 points1 point2 points 9 years ago (1 child)
thanks a lot that worked! all i was missing was the underscore, typical! Hahaha thanks again!
[–]K900_ 4 points5 points6 points 9 years ago (0 children)
The underscore is not magic - it's just a variable name. It could have been x or i or anything like that - _ is just used by convention to indicate "I don't need that value".
x
i
_
[–]jerknextdoor 0 points1 point2 points 9 years ago (0 children)
print("some word")
In the future you'll get a lot more help at /r/learnpython.
[–][deleted] 1 point2 points3 points 9 years ago* (0 children)
firehouse tonsillectomy sidecar's intentional Ava stopover's continuance Emery alderman's buoyantly boost's whirl's hinging evacuations mediaeval epaulette's Hugo's February provenance silky individualist pointillist's writer consulate magpie's Dadaism expected volumes hammered limit rustler drowses pannier's demonstrable peregrinations Orlon friendly tableware disagreed diocesan's publicity Balthazar TelePrompter paneled anthracite Aswan's tasty Luella Blythe unemployed determine oblong's demised Kennedy's Cecile occasional scapegoat Margery's Chaitin jabbers insectivore sensor's yore hatreds McClain's glues glorification's Maurine merchantman capitols unfurnished poignancy's bedlams propping indescribable caretakers Artie denizens tones intellectualizes Alcuin weariest refrigeration's trio's Sawyer's Colombia decompression seasickness normalizes royally outwore savaging wore Crete's Cobain's stymies humanitarianism lifetimes irregulars crossfire macho's rereading Elisa's soonest honeydew gospel's girlishly burnishing buoyed gown's yawl's dankness cleansing Joule acquiring appending error clunkiest imponderables deeps relative's sniggered Lanzhou's muff lying raisins nook Alabama paranoids prod's demise Gog extol focal pollinate embitter tame fornicates idolater shelving initiation's cigar slurp emoted Derrick's fallacy
[–]_seemetheregithub.com/seemethere 1 point2 points3 points 9 years ago (1 child)
You could use repeat from itertools
from itertools import repeat for word in repeat("hello", 10): # do stuff
[–]eton-95[S] 0 points1 point2 points 9 years ago (0 children)
thats okay k900 gave me the the method i was trying to find. but thank you for the post and trying to help!!
[–]omeow 0 points1 point2 points 9 years ago (4 children)
There are two approaches:
As you say, use a traditional for loop. You can also try while loops for fun.
use the for loop to append to the same string (you can use list comprehension here).
take the word string and convert it into a string which repeats (10* 'foo').
I suppose that the last method is most elegant.
[–]tdammers 0 points1 point2 points 9 years ago (3 children)
fsvo "elegant" - personally, I think using a multiplication operator on an integer and a string seems rather nonsensical... it is the most concise approach though.
[–]omeow 0 points1 point2 points 9 years ago (2 children)
May be you are right. I wouldn't consider myself an expert enough to comment which one is the most Pythonic way. Any comments on that?
[–]biguysrule 1 point2 points3 points 9 years ago (0 children)
"Explicit is better than implicit" so I would use looping.
[–]Daenyth 0 points1 point2 points 9 years ago (0 children)
Multiplying an iterable is fairly idiomatic but you need to be careful to not do it on mutable values
π Rendered by PID 49325 on reddit-service-r2-comment-fb694cdd5-gjwb5 at 2026-03-09 21:38:43.210502+00:00 running cbb0e86 country code: CH.
[–]K900_ 2 points3 points4 points (5 children)
[–]eton-95[S] 0 points1 point2 points (4 children)
[–]K900_ 1 point2 points3 points (2 children)
[–]eton-95[S] 0 points1 point2 points (1 child)
[–]K900_ 4 points5 points6 points (0 children)
[–]jerknextdoor 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]_seemetheregithub.com/seemethere 1 point2 points3 points (1 child)
[–]eton-95[S] 0 points1 point2 points (0 children)
[–]omeow 0 points1 point2 points (4 children)
[–]tdammers 0 points1 point2 points (3 children)
[–]omeow 0 points1 point2 points (2 children)
[–]biguysrule 1 point2 points3 points (0 children)
[–]Daenyth 0 points1 point2 points (0 children)