Skip to content

Basic Control Flow, Local Variables, Lists & Arrays

Most chapters in this book will introduce you to one or two new concepts and will illustrate them using one or two small projects. In this first chapter, I need to introduce you to a whole lot of new concepts, simply because we're starting from square one and doing anything remotely interesting in any programming language usually requires you to learn how to do many things first: how to choose between alternatives, how to repeat steps, etc. So, in this chapter, I will teach you how to:

  • Choose between alternatives (if-then-else and relatives),
  • Repeat things (recursion, not iteration),
  • Define local variables to structure your code,
  • Work with tuples composed of multiple values,
  • Store sequences of objects in a list and manipulate lists, and
  • Store sequences of objects in an array and manipulate arrays.

Lists are significantly more important than arrays in functional programs, as you will see. In imperative languages, it is the opposite. I will explain why the preference for lists in functional languages and the preference for arrays in imperative languages are natural choices.

I will illustrate each of these concepts using simple but not particularly interesting examples. Once we understand each of these tools, we will put them to work to cluster points. We will implement the k-means algorithm, which you may be familiar with if you have already taken a machine learning or data science course.