New to python and I need some help with loops.
Skip for loop steps in Python like in C++
For loop faster than generator expression?
Best way for an ever-running loop?
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
Hi all,
I am trying to skip values with an if statement in a for loop in Python and I cannot seem to do it.
What I am trying to do in Python is the following in C++:
[in]:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0;i < 10;i++)
{if (i == 3) i++;
cout<<i;}
}
[out]: 012456789.
When I do this in Python, the for loop does not "skip" value 3, it just prints 4 twice instead of 3 and 4.
I can take this to an extreme and say if i=3: i == 20 and weirdly Python distinguishes between the i inside the for loop and the i with the given value 20 which baffles me.
[in]:
for i in range(10):if i == 3: i = i+1 print(i)
[out] :0 1 2 4 4 5 6 7 8 9
Is there any solution for this?
Thank you in advance for the help!