Home
Forums
New posts
Search forums
What's new
New posts
New resources
New profile posts
Latest activity
Resources
Latest reviews
Search resources
Members
Current visitors
New profile posts
Search profile posts
DMCA Policy
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
FEEL FREE TO SHARE TUTORIALS, YOUR SKILS & KNOWLEDGE ON CODING, SCRIPTS, THEMES, PLUGINS OR ANY RESOURCES YOU HAVE WITH THE COMMUNITY-
Click Here To Post Your Request,
JOIN COMPUTER REPAIR FORUM
Home
Forums
WEB DEVELOPMENT CODING
Python
10 Monty Python Loops
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Akubay" data-source="post: 197" data-attributes="member: 55"><p>10 Monty Python Loops</p><p>10 different common ways to do loops in Python, with a Monty Python theme + a small quiz</p><p>Try to learn from the examples, and your task is a better way to solve Number 0, than 7 lines of print statements</p><p>Please comment with your own favorites or just other ways of doing it, so we can all learn new cool ways of using loops</p><p></p><p>Enjoy <img class="smilie smilie--emoji" loading="lazy" alt="🥳" title="Partying face :partying_face:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f973.png" data-shortname=":partying_face:" /> </p><p></p><p>-----Code below - see pic for indents --</p><p>[ATTACH=full]4[/ATTACH]</p><p></p><p>#0 Don't do this</p><p></p><p>print("And now for something completely different...")</p><p>print("aNd now for something completely different...")</p><p>print("anD now for something completely different...")</p><p>print("and now for something completely different...")</p><p>print("and Now for something completely different...")</p><p>print("and nOw for something completely different...")</p><p>print("and noW for something completely different...")</p><p></p><p># 1. start with classic for loop over a silly list of Monty Python quotes</p><p></p><p>quotes = ["It's just a flesh wound.", "Ni!", "I'm not dead yet!"]</p><p>for quote in quotes:</p><p> print(quote)</p><p> </p><p># 2. while loops are great for knights that don't know how many times they need to say 'Ni!'. They keep repeating themselves until a certain condition is met</p><p></p><p>while True:</p><p> answer = input("What is the airspeed velocity of an unladen swallow?")</p><p> if answer == "African or European?":</p><p> break</p><p> </p><p># 3. range() with for loop is perfect for counting dead parrots. It's a straightforward way of iterating a fixed number of times, for when Mr Death, or the plague, comes knocking <img class="smilie smilie--emoji" loading="lazy" alt="⚰️" title="Coffin :coffin:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/26b0.png" data-shortname=":coffin:" /></p><p></p><p>for i in range(10):</p><p> print("This parrot is no more! He has ceased to be!") </p><p></p><p># or</p><p>for i in range(10): </p><p> print("Bring out your dead! <img class="smilie smilie--emoji" loading="lazy" alt="🛎️" title="Bellhop bell :bellhop:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f6ce.png" data-shortname=":bellhop:" />")</p><p></p><p># 4. For loop with index, using enumerate; perfect for knighting fluffy bunnies like a proper loopy Monarch. Get ready for a schedule of 8 AM Pointy-toothed Knighting Ceremony!</p><p></p><p>bunnies = ["Thumper", "Benny", "Lola"]</p><p>print("Lagomorphic Knighting Schedule:")</p><p></p><p>for i, bunny in enumerate(bunnies):</p><p> print(f"{8+i} AM - Knighting of Sir {bunny}")</p><p> </p><p># 5. items() method to loop through dictionary can be used to find the Holy Grail. A great way to get key-value pairs and iterate through them</p><p></p><p>grail_location = {"France": "False", "Coconut": "False", "England": "True"}</p><p>for location, has_grail in grail_location.items():</p><p> if has_grail == "True":</p><p> print(f"The grail is in {location}")</p><p># 6. Recursion is a creative way to loop, like going down the proverbial rabbit hole. Be careful not to get stuck in an infinite rabbit-hole-loop!<img class="smilie smilie--emoji" loading="lazy" alt="🐇" title="Rabbit :rabbit2:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f407.png" data-shortname=":rabbit2:" /> ctrl+c/break is your friend <img class="smilie smilie--emoji" loading="lazy" alt="👍" title="Thumbs up :thumbsup:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f44d.png" data-shortname=":thumbsup:" /></p><p></p><p>def rabbit_hole<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite23" alt="(n)" title="Thumbs down (n)" loading="lazy" data-shortname="(n)" />:</p><p> if n > 0:</p><p> print("Down the rabbit hole...")</p><p> rabbit_hole(n-1)</p><p> else:</p><p> print("I'm out!")</p><p></p><p>rabbit_hole(3)</p><p></p><p># 7. os.scandir() allows efficient looping through files in a directory. It's a great way to loop through a treasure trove of files, in search of the worlds funniest joke...Just don't read it <img class="smilie smilie--emoji" loading="lazy" alt="👍" title="Thumbs up :thumbsup:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f44d.png" data-shortname=":thumbsup:" /></p><p></p><p>import os</p><p></p><p>for file in os.scandir("quest_files"):</p><p> if file.name.endswith(".txt"):</p><p> with open(file, "r") as f:</p><p> print(f.read())</p><p></p><p># 8. enumerate() function gives access to both index and value when looping through terable Perhaps keep track of brave knights, Robin not really the ’leading from the front’-type, however. Completely different to previous enumerate example, or maybe I like enumerate alot<img class="smilie smilie--emoji" loading="lazy" alt="😉" title="Winking face :wink:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f609.png" data-shortname=":wink:" /></p><p></p><p>knights = ["Lancelot", "Galahad", "Robin"]</p><p>for index, knight in enumerate(knights):</p><p> print(f"{index+1}. {knight}")</p><p></p><p># 9. zip() is great for looping through multiple iterables in parallel, like holy hand grenades and their counts. Is it throw on 3, or... <img class="smilie smilie--emoji" loading="lazy" alt="🤔" title="Thinking face :thinking:" src="https://cdn.jsdelivr.net/joypixels/assets/6.6/png/unicode/64/1f914.png" data-shortname=":thinking:" /></p><p></p><p>counts = [3,2,1]</p><p>grenades = ["Holy Hand Grenade of Antioch", "Holy Hand Grenade of Jerusalem", "Dynamite"]</p><p></p><p>for count, grenade in zip(counts, grenades):</p><p> print(f"{count} {grenade}")</p><p></p><p># 10. itertools for unusual looping, like creating infinite sequences of 'Ni!'. A great way to loop indefinitely, if you hate trees...or shrubberies</p><p></p><p>import itertools</p><p>for ni in itertools.cycle(["Ni!"]):</p><p> print(ni)</p></blockquote><p></p>
[QUOTE="Akubay, post: 197, member: 55"] 10 Monty Python Loops 10 different common ways to do loops in Python, with a Monty Python theme + a small quiz Try to learn from the examples, and your task is a better way to solve Number 0, than 7 lines of print statements Please comment with your own favorites or just other ways of doing it, so we can all learn new cool ways of using loops Enjoy 🥳 -----Code below - see pic for indents -- [ATTACH type="full"]4[/ATTACH] #0 Don't do this print("And now for something completely different...") print("aNd now for something completely different...") print("anD now for something completely different...") print("and now for something completely different...") print("and Now for something completely different...") print("and nOw for something completely different...") print("and noW for something completely different...") # 1. start with classic for loop over a silly list of Monty Python quotes quotes = ["It's just a flesh wound.", "Ni!", "I'm not dead yet!"] for quote in quotes: print(quote) # 2. while loops are great for knights that don't know how many times they need to say 'Ni!'. They keep repeating themselves until a certain condition is met while True: answer = input("What is the airspeed velocity of an unladen swallow?") if answer == "African or European?": break # 3. range() with for loop is perfect for counting dead parrots. It's a straightforward way of iterating a fixed number of times, for when Mr Death, or the plague, comes knocking ⚰️ for i in range(10): print("This parrot is no more! He has ceased to be!") # or for i in range(10): print("Bring out your dead! 🛎️") # 4. For loop with index, using enumerate; perfect for knighting fluffy bunnies like a proper loopy Monarch. Get ready for a schedule of 8 AM Pointy-toothed Knighting Ceremony! bunnies = ["Thumper", "Benny", "Lola"] print("Lagomorphic Knighting Schedule:") for i, bunny in enumerate(bunnies): print(f"{8+i} AM - Knighting of Sir {bunny}") # 5. items() method to loop through dictionary can be used to find the Holy Grail. A great way to get key-value pairs and iterate through them grail_location = {"France": "False", "Coconut": "False", "England": "True"} for location, has_grail in grail_location.items(): if has_grail == "True": print(f"The grail is in {location}") # 6. Recursion is a creative way to loop, like going down the proverbial rabbit hole. Be careful not to get stuck in an infinite rabbit-hole-loop!🐇 ctrl+c/break is your friend 👍 def rabbit_hole(n): if n > 0: print("Down the rabbit hole...") rabbit_hole(n-1) else: print("I'm out!") rabbit_hole(3) # 7. os.scandir() allows efficient looping through files in a directory. It's a great way to loop through a treasure trove of files, in search of the worlds funniest joke...Just don't read it 👍 import os for file in os.scandir("quest_files"): if file.name.endswith(".txt"): with open(file, "r") as f: print(f.read()) # 8. enumerate() function gives access to both index and value when looping through terable Perhaps keep track of brave knights, Robin not really the ’leading from the front’-type, however. Completely different to previous enumerate example, or maybe I like enumerate alot😉 knights = ["Lancelot", "Galahad", "Robin"] for index, knight in enumerate(knights): print(f"{index+1}. {knight}") # 9. zip() is great for looping through multiple iterables in parallel, like holy hand grenades and their counts. Is it throw on 3, or... 🤔 counts = [3,2,1] grenades = ["Holy Hand Grenade of Antioch", "Holy Hand Grenade of Jerusalem", "Dynamite"] for count, grenade in zip(counts, grenades): print(f"{count} {grenade}") # 10. itertools for unusual looping, like creating infinite sequences of 'Ni!'. A great way to loop indefinitely, if you hate trees...or shrubberies import itertools for ni in itertools.cycle(["Ni!"]): print(ni) [/QUOTE]
Insert quotes…
Verification
Post reply
Richest Freecoded User
Most Freecoin
freecoded
4,876 Freecoin
J
Johnhendrick
645 Freecoin
S
Smith16
607 Freecoin
Davy200
590 Freecoin
nathan69
426 Freecoin
Laureine
415 Freecoin
A
anajeen
395 Freecoin
P
Peterparker87
331 Freecoin
C
codeguru
282 Freecoin
Tekera
267 Freecoin
Home
Forums
WEB DEVELOPMENT CODING
Python
10 Monty Python Loops
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top