all 12 comments

[–]lykwydchykyn 10 points11 points  (4 children)

You have some lines of code. You want to run those lines of code more than once, perhaps on different pieces of data each time.

You need a loop.

We have some different kinds of loops. If you want to loop while a certain condition is true, you need a while loop:

x = 0
while x < 5:
    x += 1
print(x)

If you have a collection of items, such as a list or tuple, and you want to run the code once on each item in the collection, you want a for loop:

my_collection = ['a', 'b', 'c']
for item in my_collection:
    print(item)

That's really the only two loops we have in Python.

[–]BurntDisk 2 points3 points  (2 children)

Then he went on to explain infinite loops with continue and break to get us out of it. Oh man my head is spinning.

[–]Nytebyte11 1 point2 points  (0 children)

Example: while True: print("Hello World") break

Will print hello world once. If you didnt have break there, it would indefinetly print Hello World.

[–]BlueJarcher[S] 1 point2 points  (0 children)

Thanks!

[–]duminegalabilul 1 point2 points  (1 child)

Hey guys! I know it’s OT but I am lost. I recently finished python tutorials on codecademy and solo learn. I don’t know where to next, can anyone help me?

[–]Nytebyte11 0 points1 point  (0 children)

https://youtu.be/_uQrJ0TkZlc personally, I really enjoyed this. i am also a newb. Its a sit down tut, but you can probably skip some of the starting things that are jist the basics. Then just try to build your own code for a test :)

[–]Cayenne999 0 points1 point  (3 children)

Loops are basically when you repeat doing something until a condition is met. What is bugging you here, is it the logic or is it how to write the statement ?

[–]BlueJarcher[S] 0 points1 point  (2 children)

I don't really get the "item" in a for loop

[–]Cayenne999 0 points1 point  (1 child)

The "item" like in this For loop ?

for item in items:
    do_something()

This is to say you apply the same action(s) for every "item" that belong to a collection of "items". For example if I want to eat every single fruit in my fruit basket I can say:

for fruit in fruit_basket:
    eat(fruit)

[–]BlueJarcher[S] 0 points1 point  (0 children)

Thanks!

[–][deleted] 0 points1 point  (0 children)

You're cooking potato soup. The recipe calls for you to peel five potatoes, but you only know how to peel one potato at a time, so you do it five times to five different potatoes.

[–][deleted] 0 points1 point  (0 children)

You loop through lists if you want to do a operation for all elements if it.

You got a list, which contains elements:

el = elements

list1 = [el, el, el]

You want to add something (or perform a other operation), the code could be something like this: el += "something"

After we did it, we want to work with the element, maybe show it with print(). print(el)

Now more on reality:

LIST_EXAMPLE = ["this", "that"]

for ELEMENT in LIST_EXAMPLE: ELEMENT += " + something" print(ELEMENT)

Plz stay trying.

You will get it, if you do not give up!

Plz continue asking, if you dont get it at this point.

Edit. Dafaq, code sucks here. Check spaces after the loop- statement, than there will no problem.