i tried reading wikibooks online for loops,but i have no idea what to even do.can someone pls help me
this is a study group for MIT's 6.00 course. you're asking in the wrong forum - try a different website.
Depending on what you want to do, you can either use while, or for. For both, indent everything you want to use in the loop. If you use for, type break after the loop content to end it. That's all I know, but I might be able to tell you more once I watch another video. Good luck. I apologize for luciandusk's rudeness. We should accept all questions here.
Loops generally come in two flavors: while and for loops. While loops continue to execute the code that they contain as long as a condition is true. Example: i = 0 while( i < 10 ): print i i = i + 1 Counting from x to y can be done easier with a for loop: for i in xrange( 10 ): print i There's also a variant of for loops called for in: foo = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] for i in foo: print i BTW, to be overly technical at this point, if you use the range() function it creates something like foo in the background. xrange() just does the count. Not important for small loops, but once you get over a million or so, you'll have a quick spike in memory usage.
Repeating code over and over again via a construct is what we know as a loop. def main(): x = 0 if __name__ == "__main__": main()
@nilsarp That snippet is redundant. You don't need a main function in a Python program. The if( __name__ == "__main__" ): statement checks to see whether the program is executing in the main context, as opposed to having been imported, and if it is the main context, the code below is executed.
Join our real-time social learning platform and learn together with your friends!