User Guide
Table Of Contents
141
type some text and echoes what they type back to them, until
they press the Enter key without typing anything.
text = "?"
while not(text == ""):
text = input("Type something (or nothing to quit). >")
print(text)
The for loop is for “iterating” over a list or sequence. It runs as
many times as there are items in the list, and assigns each one to
a variable so that you can access it. For example, using the
Beatles list that you saw earlier, you can print each item using a
for loop.
Beatles = ["John", "Paul", "Ringo", "George"]
for beatle in Beatles:
print(beatle)
There are two Python commands that you can use inside while
and for loops, and that affect how a loop runs.
6.5 Functions
Functions are one of the ways that you break a Python program
into smaller chunks. This can help you manage projects, and it
makes it easier for you to reuse parts of one project in another.
You have already seen a few examples of functions built-in to
Python, but you can also create your own. When writing a
function, you can choose whether it should accept arguments
It is easy to write a loop that never ends. To stop a program that
is running in the Python Shell or Linux terminal, press Ctrl + C.
Command Description
break Immediately exits the loop.
continue Restarts the loop, ignoring any other commands
that are underneath the continue command.