New to python and I need some help with loops.
How to make a for-loop more understandable in python? - Stack Overflow
for loop in Python - Stack Overflow
Can not understand for loops
Videos
For the past 2 months I have been doing the Codecademy Python 3 course and last month I went into loops and functions and I was incredibly overwhelmed just like I was in Java. Functions are easy nothing special, at least yet, but loops are made me take that month break.
I know the theory of loops. I think I know what commands should be used where and when but for the love of god I can not write a simple loop, even after countless errors. Could anyone point me in the right direction? I really dont want to quit another language for the same reason.
Edit: a user pointed out that I need to elaborate on "simple loops". For example if I had a list and wanted to print a sentence as many times as the length of the list I know I would use len and range and have the print statement inside the loop but I can't implement it
You could show them this code for a better understanding:
Copystart = 1
length = 10
for i in range(start,start+length):
print(i)
There is also another feature that works like this, it's called slice.
Remind them that there is a reason the range function works this way. One helpful property of it is that the number of times the loop will run is equal to the second argument of range minus the first argument.
I think people get really hung up on this, but the fact is for loops in Python are very different than from C. In C, for loops are basically a wrapper around a while loop.
These two examples should help show the difference between how loops work in C versus python.
Copy# for(int x=1; x <= 10; x++)
x = 1
while x <= 10:
print(x)
x += 1
i = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # range(1, 11)
for x in i:
print(i)
But honestly, the real problem here is that all loops and arrays are easier to understand and work with if they start at zero, not one. Please consider adjusting your examples to start at zero.
This way, if you want to loop 10 times, you use the number 10.
Copy # for(int x=0; x < 10; x++)
x = 0
while x < 10:
print(x)
x += 1
i = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # range(10)
for x in i:
print(i)
Try using this:
for k in range(1,c+1,2):
You should also know that in Python, iterating over integer indices is bad style, and also slower than the alternative. If you just want to look at each of the items in a list or dict, loop directly through the list or dict.
mylist = [1,2,3]
for item in mylist:
print item
mydict = {1:'one', 2:'two', 3:'three'}
for key in mydict:
print key, mydict[key]
This is actually faster than using the above code with range(), and removes the extraneous i variable.
If you need to edit items of a list in-place, then you do need the index, but there's still a better way:
for i, item in enumerate(mylist):
mylist[i] = item**2
Again, this is both faster and considered more readable. This one of the main shifts in thinking you need to make when coming from C++ to Python.