#### Overview of this book

Dancing with Python helps you learn Python and quantum computing in a practical way. It will help you explore how to work with numbers, strings, collections, iterators, and files. The book goes beyond functions and classes and teaches you to use Python and Qiskit to create gates and circuits for classical and quantum computing. Learn how quantum extends traditional techniques using the Grover Search Algorithm and the code that implements it. Dive into some advanced and widely used applications of Python and revisit strings with more sophisticated tools, such as regular expressions and basic natural language processing (NLP). The final chapters introduce you to data analysis, visualizations, and supervised and unsupervised machine learning. By the end of the book, you will be proficient in programming the latest and most powerful quantum computers, the Pythonic way.
Free Chapter
Chapter 1: Doing the Things That Coders Do
Part I: Getting to Know Python
Chapter 2: Working with Expressions
Chapter 3: Collecting Things Together
Chapter 4: Stringing You Along
Chapter 5: Computing and Calculating
Chapter 6: Defining and Using Functions
Chapter 7: Organizing Objects into Classes
Chapter 8: Working with Files
PART II: Algorithms and Circuits
Chapter 9: Understanding Gates and Circuits
Chapter 10: Optimizing and Testing Your Code
Chapter 11: Searching for the Quantum Improvement
PART III: Advanced Features and Libraries
Chapter 12: Searching and Changing Text
Chapter 13: Creating Plots and Charts
Chapter 14: Analyzing Data
Chapter 15: Learning, Briefly
References
Other Books You May Enjoy
Index
Appendices
Appendix B: Staying Current
Appendix C: The Complete UniPoly Class
Appendix D: The Complete Guitar Class Hierarchy
Appendix E: Notices
Appendix F: Production Notes

# 1.7 Loops

If I ask you to close your eyes, count to 10, then open them, the steps look like this:

2. Set `count` to 1
3. While `count` is not equal 10, increment `count` by 1

Steps 2 and 3 together constitute a loop. In this loop, we repeatedly do something while a condition is met. We do not move to step 4 from step 3 while the test returns true.

Compare the simplicity of Figure 1.3 to

2. Set `count` to 1
3. Increment `count` to 2
4. Increment `count` to 3
5. Increment `count` to 10

If that doesn’t convince you, imagine if I asked you to count to 200. Here is how we do it in C++:

``````int n = 1;

while( n < 201 ) {
n++;
}``````

Exercise 1.6

Create a similar while-loop flowchart for counting backward from 100 to 1.

That loop was a while-loop, but this is an until-loop:

2. Set `count` to 1
3. Increment `count` by 1 until `count` equals 10

Many languages do not have until-loops, but VBA does:

``````n = 1
Do Until n>200
n = n + 1
Loop``````

We saw earlier that the code within a function’s definition is its body. The repeated code in a loop is also called its body.

Exercise 1.7

What is different between while-loops and until-loops regarding when you test the condition? Compare this until-loop flowchart with the previous while-loop example.

Our next example is a for-loop, so named because of the keyword that many programming languages use. A for-loop is very useful when you want to repeat something a specified number of times. This example uses the Go language:

``````sum := 0
for n := 1; n <= 50; n++ {
sum += n
}``````

It adds all the numbers between 1 and 50, storing the result in `sum`. Here, `:=` is an assignment, `n++` means “replace the value of `n` by its previous value plus 1,” and ```sum += n``` means “replace the value of `sum` by its previous value plus the value of `n`.”

There are four parts to this particular syntax for the for-loop:

• the initialization: `n := 1`
• the condition: `n <= 50`
• the post-body processing code: `n++`
• the body: `sum += n`

The sequence is: do the initialization once; test the condition and, if true, execute the body; execute the post-body code; test the condition again and repeat. If the condition is ever false, the loop stops, and we move to whatever code follows the loop.

Exercise 1.8

What are the initialization, condition, post-body processing code, and body of the “count to 10” example rewritten with a for-loop?

Exercise 1.9

Draw a flowchart of a for-loop, including the initialization, condition, post-body processing code, and the body. Use the template in Figure 1.5.