

Don't worry, this shouldn't break your computer. Replace the 5 inside the range() function with 5000 or even 5000000.Replace x with whatever_variable_name_I_feel_like (or some other equally obnoxious but valid variable name) to confirm that the literal x character is of no importance.
#FOR LOOP PYTHON CODE#
the use of the keywords for and in, the colon at the end of the statement, and how the block of code to be executed is indented. However, it is critical that you understand the basic elements of a for-loop before moving on – i.e. Luckily, the construction of a for-loop doesn't get more complicated than what I've outlined above. Here's what the code looks like when you type it out in ipython – note how ellipses are used to indicate the continuation of the code block inside the for-loop: That is, the Python interpreter will prompt you for another line of code to execute as a block instead of printing out, "hello world". If you are using the regular python_interpreter, you will have to do this manually, either by hitting _Tab once, or the Spacebar 4 times.Īnother note: When you type in the print("hello world") line and then hit Enter – nothing will happen. Note: if you are using ipython – which you should be doing – as soon as you hit Enter to go to the next line, the interpreter will automatically add an indentation for you. A few nuances will come up even in the execution of those two lines. This is a requirement by Python, not just an aesthetic thing.Įxecute a boring for-loop in interactive Pythonīefore we get bogged down in the details, type out and execute the above code in the interactive Pythong interpreter (i.e. that print() statement, is indented 4 spaces. The block of code to be executed, e.g.It basically tells the Python interpreter: everything after this line is the block of code to be executed That colon at the end of the for statement is required.And one that is superfluous since it's not actually used as anything right now but a placeholder. The keyword in is also another special reserved keyword.

you can't name a variable for, and in a text-editor, it should be highlighted.

The range() function itself is not a fundamental part of the _for-loop – I just use it in _this basic example as it's the easiest way in Python to say: hey, iterate 5 times. It's harder to accurately and completely explain than it is to just intuit: the range() function takes one argument and produces a sequence of numbers from 0 up until that argument. That range() function takes creates one of Python's data types.Here are the highlights, which I'll elaborate on throughout this lesson. Here's what the previous print-hello-world-5-times script looks like, as a basic for-loop in Python: for x in range ( 5 ): print ( "hello world" ) Anatomy of a very boring for-loopīelieve it or not, there's a lot to understand in that boring two-line snippet. Or 5,000,000 times? Copy-and-paste can only get us so far.īut with a loop, we can command the computer to execute that block of code as many times as we want, without physically writing that code, over and over. But what if I wanted to run that block of code 50 times. For example, here's the Python code to print "hello world": print ( "hello world" )Īnd here's the script that repeats that "block of code" 5 times: print ( "hello world" ) print ( "hello world" ) print ( "hello world" ) print ( "hello world" ) print ( "hello world" ) Note: I plan to expand this section a little bit, but you could also just read the excellent lesson here: Chapter 2: Flow Control – for Loops and the range() Function Why loop?Ī loop is a programming construct in which we define a block of code that we want the computer to execute repeatedly, as well as how many times the computer should execute that block of code.īy "block of code", I mean, any code.
