Scientific Computing in Python

numerical computing in python and scientific computing python vs matlab and scientific computing python tutorial and scientific computation python hacking math junkies
Prof.WilliamsHibbs Profile Pic
Prof.WilliamsHibbs,United States,Teacher
Published Date:28-07-2017
Your Website URL(Optional)
Comment
Programming and Scientific Computing in for Aerospace Engineers 40 35 30 25 20 15 10 5 0 5 0 10 20 30 40 50 60 70 AE Tutorial Programming Python v3.11 Jacco Hoekstra 1. Getting started 1.1 What is programming? Ask a random individual what programming is and you will get a variety of answers. Some love it. Some hate it. Some call it mathematics, others philosophy, and making models in Python is mostly a part of physics. More interestingly, many different opinions exist on how a program should be written. Many experienced programmers tend to believe they see the right solution in a flash, while others say it always has to follow a strict phased design process, starting with thoroughly analyzing the requirements (not my style). It definitely is a skill and I think it’s also an art. It does not require a lot of knowledge, it is a way of thinking and it becomes an intuition after a lot of experience. This also means that learning to program is very different from the learning you do in most other courses. In the beginning, there is a very steep learning curve, but once you have taken this first big step, it will become much easier and basically a lot of fun. But how and when you take that first hurdle is very personal. Of course, you need to achieve the right rate of success over failure, something you can achieve by testing small parts during the development. For me, there aren’t many things that give me more pleasure than to see my program (finally) work. The instant, often visual, feedback makes it a very rewarding activity. And even though at some stage you will also see the right solution method in a flash, at the same time your program will almost certainly not work the first time you run it. A lot of time is spent understanding why it will not work and fixing this. Therefore some people call the art of programming: “solving puzzles created by your own stupidity” While solving these puzzles, you will learn about logic, you will learn to think about thinking. The first step towards a program is always to decompose a problem into smaller steps, into ever smaller building blocks to describe the so-called algorithm. An algorithm is a list of actions and decisions that a computer (or a person) has to go through chronologically to solve a problem. This is often schematically presented in the form of a flow chart. For instance, the algorithm of a thermostat that has to control the room temperature is shown in figure 1.1. Figure 1.1: Flow chart of ‘thermostate’ algorithm. 8Another way to design and represent algorithms is using simplified natural language. Let’s take as an example the algorithm to “find the maximum value of four numbers”. We can detail this algorithm as a number of steps: Let’s call the 4 numbers a,b,c and d if a b then make x equal to a, else make x equal to b if x c then make x equal to c if x d then make x equal to d show result x on screen Going through these steps, the result will always be that the maximum value of the four numbers is shown on the screen. This kind of description in natural language is called “pseudo-code”. This pseudo-code is already very close to how Python looks, as this was one the goals of Python: it should read just as clear as pseudo-code. But before we can look at some real Python code, we need to know what Python is and how you can install it. After that, we will have a look at some simple programs in Python, which you can try out in your freshly installed Python environment. 1.2 What is Python? Python is a general purpose programming language. And even though recently Python was used more in the USA than in Europe, it has been developed by a Dutchman, Guido van Rossum. It all started as a hobby project, which he pursued in his spare time while still employed at the so- called Centrum Wiskunde & Informatica (CWI) in Amsterdam in 1990. Python was named after Monty Python and references to Monty Python in comments and examples are still appreciated. The goals of Python, as Guido has formulated them in a 1999 DARPA proposal, are: - an easy and intuitive language just as powerful as major competitors - open source, so anyone can contribute to its development - code that is as understandable as plain English - suitable for everyday tasks, allowing for short development times Guido van Rossum was employed by Google for years, as this is one of the many companies that use Python. He is currently working for another user of Python: Dropbox. He still is the moderator of the language, or as he is called by the Python community: the “benevolent dictator for life”. A practical advantage of Python is that it is free, and so are all add-ons, which have been developed by the large (academic) Python community. Some have become standards of their own, such as the combination Numpy/Scipy/Matplotlib. These scientific libraries(or modules), in syntax(grammar) heavily inspired by the software package MATLAB, are now the standard libraries for scientific computing in Python. 9There are two current versions: Python 2 and Python 3.(There is also a second and a third number indicating the exact version, but these as less relevant as they are downwards compatible with the other 2.x and 3.x versions. At the time of writing the newest versions were 2.7.3 and 3.2.3) Up to Python 3 all versions were downwards compatible. So all past programs and libraries will still work in a newer Python 2.x versions. However, at some point, Guido van Rossum wanted to correct some issues, which could only be done by breaking the downward compatibility. This started the development of Python 3.0. However, luckily for the Python community, Python 2.x is also still maintained and updated. The majority of the community is still using Python 2. The parallel path offers a gradual, optional transition. Whether Python 3 will actually become the standard is yet unknown. An example of a difference between the two versions is the syntax of the PRINT-statement, which shows a text or a number on the screen during runtime of the program: In Python 2.x: print “Hello world” In Python 3.x: print(“Hello World”) (Another major difference applies to integer division, but we need to know more about data types to understand that) Since still many more modules are available for Python 2.x than for Python 3.x, we use Python 2.x. In this reader and the course, we use Python 2.7 (but any 2.5+ version will work). The libraries that we use: Numpy/Scipy/Matplotlib and Pygame are available for both Python 2.x and Python 3.x for both the 32-bit version(most used) as well as for the 64-bit version (for Windows, for Apple there is only a 32-bits version of Pygame). For the 32-bit Python 2.x the amount of modules available is the largest, so this is the version 95% of the community uses and so will we in this course. At times Python is called a script language. A Python source is interpreted when you run the program. This is very user-friendly: there is no need to compile or link files before you can run it. The cost is, often, some execution speed. In a way, some (milli)seconds of runtime are traded for short development times, which saves days or weeks. Note that Python libraries like Numpy and Scipy use very fast low-level modules, resulting in extremely fast execution times for scientific computing. It beats MATLAB, Fortran and C++ in many instances for these tasks. The same goes for Pygame graphics library, as this is a layer over the very fast SDL library used in many games already. Using an interpreter instead of a compiler, means you need to have Python installed on the computer where you run the Python program. But fortunately there is an add-on, called Py2exe, which avoids this by creating executables, which are self-contained applications. Creating executables is called compiling. These programs can be executed on any computer without having installed Python. How this should be done, is covered by a separate chapter of the reader, called ‘Distributing your Python program’. Using the Inno Setup tool, one can integrate data stored in a program into one setup executable file, which is also covered in this chapter. 1.3 Installing Python 10There are two ways to install Python with the libraries we need: 1. Download a package: a distribution of Python from Python(x,y) or Enthought, which contains all libraries we need (and many more) except Pygame 2. Or do a custom install: Download the components independently (resulting in a smaller, tailored installation) 1.3.1 Using a packaged version of Python There are two common distributions of Python+add-ons: Enthought (free for universities) and Python(x,y) (free for all). Python(x,y) (Windows/Linux) Python(x,y) is recommended for this course for Windows and Linux Users. Go to: http://www.pythonxy.org Then go to ‘Downloads’ and download the version for your operating system (Windows/Linux). This encompasses the 32-bits version of Python 2.7.2.1 + Numpy/Scipy/Matplotlib as well as many other libraries/tools. By default, Pygame is not installed, but you can select it in the Windows installer. When you have installed this without pygame, you can also download and install the Pygame module for Python 2.7 (32 bits) separately. We use this for 2D animated graphics as well as keyboard/mouse input. This you can download from http://pygame.org/download.shtml Current version of Pygame at the writing of this document is 1.9.1. Select the correct version for your OS and your Python version. The full content of Python(x,y) is shown in figure 1.2. 11 Figure 1.2: A full content of Python(x,y). Optional libraries/editors which will be referenced in the course are: Spyder, Py2exe. They are already included in the Python(x,y) distribution. Enthought/Canopy(Windows/Mac/Linux/Sun) The Enthought full distribution is available for Windows, Mac, Linux and Sun Solaris. It is free for universities. Therefore use your university e-mail address when downloading this. There is also a lightweight distribution, which is free for all and which contains the essential libraries (Numpy/Scipy/Matplotlib) for this course except, again, Pygame. Go to : http://www.enthought.com/ Download Enthought version 7.2 which contains Python 2.7. Then add pygame: http://pygame.org/download.shtml Current version of Pygame at the writing of this document is 1.9.1. Select the correct version for your OS and Python version. Enthought has many libraries (see http://www.enthought.com/products/epdlibraries.php ) but not the Spyder editor nor Py2exe. There is also a distribution for Apple (& Windows that is called Anaconda, but this will install 64-bit versions on 64-bit machines. The problem is that there is no 64-bit version of Pygame. 121.3.2 Customized installation of separate packages (Windows/Mac/Linux) If you do not want to use these large distributions, there is always the option to install Python and the modules yourself. Do this in the following order: Download Python 2.7 for your OS from: http://python.org/download/ For scientific computing, now download Numpy+Scipy for Python 2.7 from: http://www.scipy.org/Download For plotting then add Matplotlib for Python 2.7 from: http://matplotlib.sourceforge.net/ (Select ‘Download’ in the text on the right side) For 2D moving graphics, keyboard and mouse input, now download pygame from: http://pygame.org/download.shtml This completes the set-up you need for this course. To start Python select ‘IDLE’ from the Python folder in the Start Menu. Or even better: create a Shortcut to this on your desktop, set the working directory to the folder where you want to keep your Python programs. 1.3.3 Documentation IDLE has an option to Configure IDLE and add items to the Help menu. Here a link to a file or a URL can be added as an item in the Help pull down menu. The Python language documentation is already included. For Scipy and Numpy, downloading the .CHM files (‘chum-files’) of the reference guides onto your hard disk and linking to these files is recommended. They are available for download at: http://docs.scipy.org/doc/ For Matplotlib both an online manual as well as a pdf is available at: http://matplotlib.sourceforge.net/contents.html Also check out the Gallery for examples but most important: with the accompanying source code for plotting with Matplotlib: http://matplotlib.sourceforge.net/gallery.html For Pygame, use the online documentation, with the URL: 13http://www.pygame.org/docs/ Another useful help option is entering ‘python’ in the Google search window followed by what you want to do. Since there is a large Python user community, you will easily find answers to your questions as well as example source codes. 1.3.4 Optional tools A working environment, in which you edit and run a program is called an IDE, which stands for Integrated Development Environment. Which one you use, is very much a matter of taste. In the course we will use as an editor and working environment the IDLE program, because of its simplicity. This is provided with Python and it is easy to use for beginners and advanced programmers. Since it comes with Python, it is hence also available in both distributions. For larger projects or more advanced debugging, Spyder is recommended. Spyder is included in Python(x,y). If you did the customized installation and you want to use Spyder, you first need to download PyQt from riverbanks (when you have the Enthought distribution, you already have this): http://www.riverbankcomputing.co.uk/news Then to download Spyder go to: http://code.google.com/p/spyderlib/ Py2exe can be installed separately and is already included in Python(x,y) . How to install and use this is covered in the chapter on distributing your Python programs. 1.3.5 Configuring and using Python First: Change Working Folder to My Documents\Python In Windows, IDLE will start in the Python program directory (folder) and this will therefore also be your default working directory. This is dangerous because you may overwrite parts of Python when you save your own programs. Therefore make a shortcut on your Desktop in which we change the working folder to a more appropriate one. Right-click in the Start Menu on IDLE, select Copy and then Paste it on to your Desktop. Then right click Properties of this Shortcut and change the working directory to the folder where you want to keep your Python code (e.g. My Documents\Python). Add links to your documentation of the Help menu Start IDLE. Then if you have not already done so, select in the menu of the IDLE window, OptionsConfigure IDLEGeneral to add additional Help sources in the lower part of the property sheet. Download the documentation for Scipy (CHM files) and use the link to the pygame/docs site as well as the Matplotlib gallery. Using IDLE: your first program 14In the IDLE window, named Python Shell, select FileNew Window to create a window in which you can edit your first program. You will see that this window has a different pull-down menu. It contains “Run”, indicating this is a Program window, still called Untitled at first. Enter the following lines in this window: print “Hello World” print “This is my first Python program.” (if you would have chosen to use Python 3.x, you should add brackets around the texts and find another reader on the web, since we will use Python 2.x syntax in all our examples). Now select RunRun Module. You will get a dialog box telling you that you have to Save it and then asking whether it is Ok to Save this, so you click Ok. Then you enter a name for your program like hello.py and save the file. The extension .py is important for Python, the name is only important for you. Then you will see the text being printed by the program which runs in the Python Shell window. After FileNew Window, IDLE shows an editor window (right) next to the Shell window(left) Switching off annoying dialog box “Ok to Save?” By default, IDLE will ask confirmation for Saving the file every time you run it. To have this dialog box only the first time, goto OptionsConfigure IDLEGeneral and Select “No Prompt” in the line: At Start of Run(F5). Now, on a Windows PC, you can run your programs by pressing the function key F5. Now only the first time you run your program, it will prompt you for a locations and filename to save it, the next time it will use the same name automatically. 151.3.6 Spyder: a more advanced IDE Though IDLE is a very useful IDE (Interactive Development Environment), there are some limitations: - With large projects and many files it can become cumbersome to switch between different files - Debugging facilities are limited For this reason often another IDE is used for larger projects. There are many on the web. For scientific purposes the most popular one is Spyder. This comes with python(x,y) and many other distributions. An example of a screenshot of Spyder with some explanation is given below: Spyder screenshot Other features include inspecting data arrays, plotting them and many other advanced debugging tools. Make sure to change the settings of file in the dialog box which will pop up the first time you run the file to allow interaction with the Shell. Then you have similar features to which IDLE allows: checking your variables in the shell after running your program or simply to testing a few lines of code. My advice would be to first keep it simple and use IDLE for the basics. Use the print statement and the shell (to inspect variables) as debugger and occasionally www.pythontutor.com. Then later, for larger or more complex problems switch to Spyder. 161.4 Examples and exploration of the language 1.4.1 Temperature conversion: (PRINT, INPUT statement, variables) In the IDLE window, named Python Shell, select FileNew Window to create a window in which you can edit your program. You will see that this window has a different pull-down menu. It contains “Run”, indicating this is a Program window, still called Untitled at first. Enter the following lines in this window and follow the example literally. If you type 5 (so leave out the decimal point) instead of 5.0 the program might not work. (If you want to make it a bit more interesting, and harder for yourself, you could make a variation on this program. In much the same fashion, you could try to make a saving/debt interest calculator where you enter start amount, interest rate in percentage and number of years. To raise x to the power y, you use xy) Now select RunRun Module. Depending on your settings, you might get a dialog box telling you that you have to Save it and then asking whether it is Ok to Save this, so you click Ok. Then you enter a name for your program like temperature.py and save the file. The extension .py is important for Python, the name is only important for you. Then you will see the text being printed by the program, which runs in the window named “Python Shell”: 17 Now let us have a closer look at this program. It is important to remember that the computer will step through the program line by line. So the first line says: print "Temperature convertor Fahrenheit = Celsius" The computer sees a print statement, which means it will have to output anything that comes after this statement (separated by commas) to the screen. In this case it is a string of characters, marked by a “ at the beginning and the end. Such a string of characters is called a text string or just string. So it put this on the screen. The computer will also automatically add a newline character to jump to the next line for any next print statement (unless you end with a comma to indicate you want to continue on the same line). Then this line is done, so we can go to the next one, which is slightly more complicated: tempf = input("Enter temperature in degrees Fahrenheit: ") This line is a so called assignment statement, indicated by the “=” symbol. In general, it has the following structure: variablename = expression In our example it tells the computer that in the computer’s memory a variable has to be created with the name tempf. To be able to do this, the computer first evaluates the expression on the other side of the “=” sign to see what the type of this variable has to be. It could for example be a floating point value (float type) or a round number (integer type), a serie of characters (string) or a switch (boolean or logical). It then reserves the required amount of bytes, stores the type and the name. If the name already exists, then this old value and type are first to avoid problems later on. 18The computer evaluates the expression. The outcome is stored in memory and can be used later in other expressions by using the variable name. To do this, the computer maintains a table in its memory with the value of a variable, its name and its type. a = 2 For numbers there are two types: integers and floats. Integers are whole numbers, used to count something or as an index in a table. Floats are numbers with a floating point and can be any value. Python looks at the expression to determine the type: 2 = integer type -4 = integer type 34 = integer type 2.0 = float type 0. = float type 1e6 = float type 34. = float type Now let us have a look at the expression, This is not a simple one. The expression in our example has the following structure: functionname ( argument ) Python knows this is a function because of the brackets. In this case, the name of the function is which is used is input( ), one of the standard functions included in the Python language. (Later we will also use functions which we have defined ourselves) Most functions do some calculations and yield a value. Example of these functions are abs(x) for the absolute value (modulus) of x or int(x) which will truncates the float x to returns an integer type. The int( ) function is one of the type conversion functions: int(3.4) = integer with value 3 int(-4.315) = integer with value -4 float(2) = float with value 2. float(0) = float with value 0. But some functions are complete little programs in itself. The input-functions for example does more: it can be called with one argument, which will be printed on the screen, before the user is prompted to enter a value. When the user presses enter, the value is read, the type is determined 19and this is returned as the result by the input function. So in our case, the text Enter temperature in degrees Fahrenheit: is printed and the user enters something (hopefully a number) and this is then stored as an integer or floating point number in a memory location. We call this variable tempf. The next line is again an assignment statement as the computer sees from the equal sign “=”: tempc = (tempf-32.0)5.0/9.0 Here a variable with the name tempc is created. The value is deduced from the result of the expression. Because the numbers in the expression on the left side of the equal sign are spelled like “5.0” and “32.0”, the computer sees we have to use floating point calculations. We could also have left out the zero as long as we use the decimal point, so 5./9. would have been sufficient to indicate we want to use floating point values. If we would leave them out, the result might be an integer value, which means that every intermediate value is truncated (so cut off behind the decimal point) which would mean the result of the expression would be zero, as 5/9 would yield zero as result in integer arithmetic When this expression has been evaluated, a variable of the right type (float) has been created and named tempf, the computer can continue with the next line: print tempf," degrees Fahrenheit is",int(tempc),"degrees Celsius" This line prints four things: a variable value, a text string, an expression which needs to be evaluated and another text string, which are all printed on the same line with each comma a space character is automatically inserted as well. The int function means the result will be truncated (cut off behind the decimal point). Better would have been to use: int(round(tempc)) What do you think the difference would have been? (check section 2.8). Try running the program a few times. See what happens if you enter your name instead of a value. 1.5.2 Example: a,b,c formula solver (IF statement, Math functions) Now create a new window and enter the program below import math print "To solve ax2 + bx + c = 0 :" a = input("Enter the value of a:") 20b = input("Enter the value of b:") c = input("Enter the value of c:") D = b2 - 4.ac x1 = (-b - math.sqrt(D)) / (2.a) x2 = (-b + math.sqrt(D)) / (2.a) print "x1 =",x1 print "x2 =",x2 Run this program and you will see the effect. Some notes about this program: - note how is used to indicate the power function. So 52 will yield 25. (Using 55 is faster by the way.) - the program uses a function called sqrt() This is the square root function. This function is not a standard Python function. It is part of the math module, supplied with Python. Therefore the math module needs to be imported at the beginning of the program. The text math.sqrt() tells Python that the sqrt() function can be found in the imported math module - After you have run the program, you can type D in the shell to see the value of the discriminant. All variables can be checked this way. Also, note the difference between text input and output. The line print is a statement, while input is used as a function returning a value, which is then stored in a variable. The argument of input-function is between the brackets: it’s a prompt text, which will be shown to the user before he enters his input. There is one problem with our program. Many times it will stop with an error because the discriminant D is negative, resulting in an error with the square root function. To solve this, let us try adding some logic to the program, see below. Adapt your program to match this precisely, note the margin jumps (use TAB-key) in the IF statement, which is called indentation. from math import sqrt print "To solve ax2 + bx + c = 0 ," a = input("Enter the value of a:") b = input("Enter the value of b:") c = input("Enter the value of c:") D = b2 - 4.ac if D0: 21 print "This equation has no solutions." else: x1 = (-b - sqrt(D)) / (2.a) x2 = (-b + sqrt(D)) / (2.a) print "x1 =",x1 print "x2 =",x2 Now the program first checks whether D is negative. If so, it will tell you that there are no solutions. - Note the structure and syntax(=grammar) of the if-else statement. A colon ( : ) indicates a block of code will be given, so it acts as a ‘then’. The block is marked by the indented part of the code,. - When it jumps back to the margin of the beginning to indicate the end of this block of code, an ‘else’ follows, again with a colon and an indented block of code. - It also uses a different way to import the sqrt function from the math module. Note the difference in syntax in both the line with the import statement as well as the line where the sqrt function is used. Assignment 1.1: Adapt the program so that it calculates the hypotenuse c of a rectangular triangle for rectangular sides a and b as entered by the user, using Pythagoras formula. Assignment 1.2: Now adapt the program so that it determines the maximum value of given numbers a,b,c and d. Use the algorithm described before. Translate this into Python code using the example above and run it Assignment 1.3: Change the program in a way that it solves a third order polynomials written as: 3 2 x + ax + bx + c = 0 The user enters the values of a, b and c. And the program prints the solutions for x. Find the required formulas by searching for “formulas to solve polynomial functions nth degree”. 1.5.3 Example: using lists and a for-loop Now let us have a look at a program which is slightly more complex. First explore the range function. Go to the Python shell and type the following lines to see how the range function works. range(10) range(1,11) 22 range(2,22,2) range(5,1,-1) What do you notice? If you do not see its logic, try a few values yourself. Some things you probably have noticed: - It produces a list of integers separated by a comma in between square brackets: 2, 3, 4, 5 - the range-function has three arguments start, stop and step The stop is always required, but start en step are optional. - the default start value is zero - the default step value is one - the start value is included in the list - the stop value is not included in the list This result is in fact a new variable type: a list . You can regard this as a table: a = 7, 3, -1, 3 Indexing of the list starts with zero, so a0 will return in the first value (7) and a3 the last one (3). For the next example code we will use the website http://www.pythontutor.com . Go to this site and on the start page cick “Start using..”, clear the source edit window and enter this program in the window (also mind the layout (use tab to move the margin right) a = 9 for i in range(1,11): x = ia print i,"x", a,"=",x print print "Ready." Now click “Visualize execution” and then click “Forward” a few times to see what happens. On the right side of the edit window you can see what happens in the memory of the computer. Next, you see the output window, with the text the user of the program will see: 23 Can you explain what the computer does? Why he jumps back? How he knows which part of the code to repeat and which not? Do you notice what happens to the value of i when it jumps back to the for-statement? What happens when i is equal to 10? This is called a for-loop: i is assigned the first value of the list (in this case the list made by the range function) and after it has completed the indented block of code, it jumps back and assigns the next value of the list until it has reached the end of the list. If there are no more value for i, so after the last value it continues the code and does not jump back, the variable i now has the final value (10, because 11 is not included in the list generated by the range-function. See also the program below, what will this program do? Which integer do you think the len() function returns? lst = 40. , 5. , 13., 1., 5. for i in range(len(lst)): print 2lsti Notice the difference in syntax between calling a function in Python: sqrt(D) len(a) range(1,11) and the use of a list with indices: a0 to get the first element use index zero lsti when i=1, you get the second element, etc. You can see that Python knows whether something is a list or a function based on the type of brackets used See below an example of a table lookup using a for-list: Enthalpy of water at 1 atmosphere 24 Ttab = 0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0 Htab = 0.06,42.1, 84.0, 125.8,167.5,209.3,251.2,293.0, 335.0, 377.0,419.1 n = len(Ttab) Length of list T = input("Enter temperature in degrees Celsius:") for i in range(n-1): if T = Ttabi and T Ttabi+1: print "H is between",Htabi,"and",Htabi+1 Note the two indentations: one for the for-loop, the next for the if statement. A unique feature of Python is that the same list can store different types of variables: b = 2, ”Hello there”, 3.141565 , 2, 10.0, True This assignment of b is a valid list, and it consists of a mix of variable types: floats, integers, a string and a Boolean(logical) You even store lists in a list: c = 2, 3, -1 , 3, 4, 0 , 7, 1, 1 And the result is basically a two dimensional table, as we can see by showing some values of this table in the Python shell (first type the assignment statement above): c0 2, 3, -1 c20 7 The second c20 basically means, from left to right: the third element of c (which is a list)and then the first element of that list. Through with this format it is easy to select a row, but selecting a column is only possible with a for loop. Below there are two ways to go through a two dimensional list to pick a column, What would be advantages of each method? The first method is to have an integer run through a list of integers (so whole numbers) as generated by the range function: 0,1,…..len(people)-1 Remember the end value given in the range function will not be included in the range functions resulting list. These are exactly the indices for the list as this also starts with 0 and ends with its length minus one. Database: one statement can cover more program lines people = "Jan", 18, "Delft", \ 25 "Piet", 20, "Leiden", \ "Kees", 19, "Amsterdam", \ "Klaas",34, "Utrecht", \ "Victor",22,"Leeuwarden" iname = 0 iage = 1 icity = 2 Show first two columsn of table for i in range(len(people)): print peopleiiname,"is",peopleiiage,"years old." The above way will work in most other programming languages as well. A unique feature of python is that a list of any type can be used as the counter (or as we call it: iterator) in the loop. The variable person will get each value from the list people. As people is a list of lists, person will first be the first element from people: "Jan", 18, "Delft". Then, when the block of code that is in the loop has been executed with this value for person, the next value of people will be used: "Piet", 20, "Leiden" and so on, for as long as the list people lasts: Database: see how one statement can cover more program lines (within a list definition, without the backslash is also ok) people = "Jan", 18, "Delft", \ "Piet", 20, "Leiden", \ "Kees", 19, "Amsterdam", \ "Klaas",34, "Utrecht", \ "Victor",22,"Leeuwarden" iname = 0 iage = 1 icity = 2 Show first two columsn of table for person in people: print personiname,"is",personiage,"years old." Lists are often created by appending values at the end of the list, using the append function, which comes with the list-type and has a special syntax (varname.function), similar to how we use functions from a module, which we will later see more often. Such a function, which is called by a dot after the variable name is called a method, in this case of the list object (i.e. the list type). Try this bit of code: debt = rate = 1.03 26

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.