Ask coding questions

← Back to all posts
I am such a noob at this...
beanthexenoxide (1)

Pls can someone tell me a loop code for python code? Thank you!

Commentshotnewtop
PYer (2308)

Many different ways:
while True:
#code
for i in range(30):
#code
https://www.w3schools.com/python/python_for_loops.asp
Upvote if this is helpful! Thanks!

Appleloop06 (5)

@fullern000 where for i in range(x is how many times from 0 i think)

PYer (2308)

@Appleloop06 I don't understand what you are saying.

cerotothree (0)

@Appleloop06 If I understand what you're saying, range(x) would be the range from 0 up to x. So, the loop iterates x times.

OwenBradstreet (81)

There are two types of loop: Definite and Indefinite.
Definite loops have a certain ending - they will definitely end at some point.
Indefinite loops can end, but don't necessarily have to.

The while loop will execute some code over and over again indefinitely while a condition is met. For example:

x = 0
while (x < 10):
    #Do some things here
    x += 1

This will execute the code 11 times, because x starts at zero and increments by one each time, and while loop will loop indefinitely until x isn't less than 10. Another example code be:

while (True):
     # Doing some cool things forever here
     if (youwantto):
          break

This code will run forever, unless the loop is broken from the inside using break.

The For loop will take a list and execute the code for each item in that list. For example:

names = ["Owen Bradstreet", "Taya Fitzgerald", "Jerry Smith", "John Doe"]
for name in names:
     print(name)

This code will go through every item in the "names" list, replace the temporary variable "name" with the current name it's on and print out that name. The for loop is often used to run some code a certain amount of time like this:

for i in range(0, 9):
     # Cool code here

This will execute that code 10 times.

Hope this helped!
Owen

PS: If this did help you, can you please leave an upvote :) This took me a while to write.

dasagrivamanu (0)

Hi - Python has many types of loop constructs such as the famous for loop, while, and do-while loops. Here is a well-knit tutorial for the quick ref and it has tons of examples for practice: https://www.techbeamers.com/python-for-loop/

laksh5 (79)

u can do for x in range (1, 60)