{"id":51,"date":"2019-01-15T23:23:34","date_gmt":"2019-01-15T23:23:34","guid":{"rendered":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/chapter\/writing-simple-programs\/"},"modified":"2019-01-28T19:07:05","modified_gmt":"2019-01-28T19:07:05","slug":"writing-simple-programs","status":"publish","type":"chapter","link":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/chapter\/writing-simple-programs\/","title":{"raw":"UNIT 2: Writing Simple Programs","rendered":"UNIT 2: Writing Simple Programs"},"content":{"raw":"<h1>Learning Objectives<\/h1>\r\n<ul>\r\n \t<li>Explain the dependencies between hardware and software<\/li>\r\n \t<li>Describe the form and the function of computer programming languages<\/li>\r\n \t<li>Create, modify, and explain computer programs following the input\/process\/output pattern.<\/li>\r\n \t<li>Form valid Python identifiers and expressions.<\/li>\r\n \t<li>Write Python statements to output information to the screen, assign values to variables, and accept information from the keyboard.<\/li>\r\n \t<li>Read and write programs that process numerical data and the Python math module.<\/li>\r\n \t<li>Read and write programs that process textual data using built-in functions and methods.<\/li>\r\n<\/ul>\r\n<h1>Computer Hardware Architecture<\/h1>\r\nBefore we start learning a programming language to give instructions to computers to develop software, we need to learn about how computers are built. If you were to take apart your computer or cell phone and look deep inside, you would find the following parts:\r\n\r\n[caption id=\"\" align=\"aligncenter\" width=\"255\"]<img class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232300\/image12.png\" alt=\"image\" width=\"255\" height=\"152\" \/> Figure 10: Computer Hardware Architecture[\/caption]\r\n<p class=\"import-Normal\">The high-level definitions of these parts are as follows:<\/p>\r\n\r\n<ul>\r\n \t<li class=\"import-Normal\">The <em>Central Processing Unit<\/em> (or CPU) is the part of the computer that is built to be obsessed with \"what is next?\" If your computer is rated at 3.0 Gigahertz, it means that the CPU will ask \"What next?\" three billion times per second.<\/li>\r\n \t<li class=\"import-Normal\">The <em style=\"text-align: initial;font-size: 1em\">Main Memory<\/em><span style=\"text-align: initial;font-size: 1em\"> is used to store information that the CPU needs in a hurry. The main memory is nearly as fast as the CPU. But the information stored in the main memory vanishes when the computer is turned off.<\/span><\/li>\r\n \t<li class=\"import-Normal\">The <em style=\"text-align: initial;font-size: 1em\">Secondary Memory<\/em><span style=\"text-align: initial;font-size: 1em\"> is also used to store information, but it is much slower than the main memory. The advantage of the secondary memory is that it can store information even when there is no power to the computer. Examples of secondary memory are disk drives or flash memory (typically found in USB sticks and portable music players).<\/span><\/li>\r\n \t<li class=\"import-Normal\">The <em style=\"text-align: initial;font-size: 1em\">Input and Output Devices<\/em><span style=\"text-align: initial;font-size: 1em\"> are simply our screen, keyboard, mouse, microphone, speaker, touchpad, etc. They are all of the ways we interact with the computer.<\/span><\/li>\r\n \t<li class=\"import-Normal\">These days, most computers also have a <em style=\"text-align: initial;font-size: 1em\">Network Connection<\/em><span style=\"text-align: initial;font-size: 1em\"> to retrieve information over a network. We can think of the network as a very slow place to store and retrieve data that might not always be \"up\". So in a sense, the network is a slower and at times unreliable form of <\/span><em style=\"text-align: initial;font-size: 1em\">Secondary Memory<\/em><span style=\"text-align: initial;font-size: 1em\">.<\/span><\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">While most of the detail of how these components work is best left to computer builders, it helps to have some terminology so we can talk about these different parts as we write our programs.<\/p>\r\n<p class=\"import-Normal\">As a programmer, your job is to use and orchestrate each of these resources to solve the problem that you need to solve and analyze the data you get from the solution. As a programmer you will mostly be \"talking\" to the CPU and telling it what to do next. Sometimes you will tell the CPU to use the main memory, secondary memory, network, or the input\/output devices.<\/p>\r\n\r\n<h1>Digital Computing: It\u2019s All about 0\u2019s and 1\u2019s<\/h1>\r\n<p class=\"import-Normal\">It is essential that computer hardware be reliable and error free. If the hardware gives incorrect results, then any program run on that hardware is unreliable. The key to developing reliable systems is to keep the design as simple as possible[footnote]Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p9.[\/footnote]. In digital computing, all information is represented as a series of digits or electronic symbols which are either \"on\" or \"off\" (similar to a light switch). These patterns of electronic symbols are best represented as a sequence of zeroes and ones, digits from the binary (base 2) number system.<\/p>\r\n\r\n\r\n[caption id=\"\" align=\"aligncenter\" width=\"344\"]<img class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232303\/image13.png\" alt=\"image\" width=\"344\" height=\"146\" \/> Figure 11: Digital Representation[\/caption]\r\n\r\nThe term bit stands for binary digit. Therefore, every bit has the value 0 or 1. A byte is a group of bits operated on as a single unit in a computer system, usually consisting of eight bits. Although values represented in base 2 are significantly longer than those represented in base 10, binary representation is used in digital computing because of the resulting simplicity of hardware design[footnote]Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p10[\/footnote]. For example, the decimal number <code>485<\/code> is represented in binary as <code>111100101<\/code>.\r\n<h1>Operating Systems\u2014Bridging Software and Hardware<\/h1>\r\n<img class=\" wp-image-145 alignright\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232306\/image14-1.png\" alt=\"\" width=\"125\" height=\"185\" \/>\r\n<p class=\"import-Normal\">An operating system is software that has the job of managing and interacting with the hardware resources of a computer. Because an operating system is intrinsic to the operation a computer, it is referred to as system software.<\/p>\r\n<p class=\"import-Normal\">An operating system acts as the \u201cmiddle man\u201d between the hardware and executing application programs (see Figure 12). For example, it controls the allocation of memory for the various programs that may be executing on a computer. Operating systems also provide a particular user interface. Thus, it is the operating system installed on a given computer that determines the \u201clook and feel\u201d of the user interface and how the user interacts with the system, and not the particular model computer. [footnote]Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, pp.11-12.[\/footnote]<\/p>\r\n\r\n<h1>Software Development Tools<\/h1>\r\n<p class=\"import-Normal\">It can be very cumbersome, error prone, and time consuming to converse with a computer using only zeros and ones. Numerical machine code (computer code using only zeros and ones) does exist but is rarely used by programmers. For that reason most people program using a \"higher-level\" programming language which use words and symbols that are easier for humans to manage than binary sequences. Tools exist that automatically convert a higher-level description of what is to be done into the required lower-level, machine code. Higher-level programming languages like Python allow programmers to express solutions to programming problems in terms that are much closer to a natural language like English. Some examples of the more popular of the hundreds of higher-level programming languages that have been devised over the past 60 years include FORTRAN, COBOL, Lisp, Haskell, C++, Perl, C , Java, and C#. Most programmers today, especially those concerned with high-level applications, usually do not worry about the details of underlying hardware platform and its machine language.<\/p>\r\n\r\n<div class=\"textbox\">Sample numerical machine code:\r\n<code>001010001110100100101010000001111<\/code>\r\n<code>11100110000011101010010101101101<\/code><\/div>\r\n<p class=\"import-Normal\">Fortunately, higher-level programming languages provide a relatively simple structure with very strict rules for forming statements, called the programming language syntax, which can express a solution to any problem that can be solved by a computer.<\/p>\r\n<p class=\"import-Normal\">Consider the following program fragment written in the Python programming language:<\/p>\r\n<code>subtotal = 25<\/code>\r\n<code>tax = 3<\/code>\r\n<code>total = subtotal + tax<\/code>\r\n<p class=\"import-Normal\">While these three lines (three statements) do constitute a proper Python program, they are more likely a small piece of a larger program. The lines of text in this program fragment look similar to expressions in algebra. We see no sequence of binary digits. Three words,<code> subtotal, tax, and total<\/code>, called variables, represent information. In programming, a variable represents a value stored in the computer\u2019s memory. Instead of some cryptic binary instructions meant only for the CPU, we see familiar-looking mathematical operators (= and +). Since this program is expressed in the Python language, not machine language, no computer processor (CPU) can execute the program directly. A program called an <strong>interpreter<\/strong> translates the Python code into machine code when a user runs the program. The higher-level language code is called the source code. The corresponding machine language code is called the target code. The interpreter translates the source code into the target machine language.<\/p>\r\n<p class=\"import-Normal\">The beauty of higher-level languages is this: the same Python source code can execute on different target platforms. The target platform must have a Python interpreter available, but multiple Python interpreters are available for all the major computing platforms. The human programmer therefore is free to think about writing the solution to the problem in Python, not in a specific machine language.<\/p>\r\n<p class=\"import-Normal\">Programmers have a variety of tools available to enhance the software development process. Some common tools include:<\/p>\r\n\r\n<ul>\r\n \t<li><strong>Editors<\/strong>. An editor allows the programmer to enter the program source code and save it to files. Most programming editors increase programmer productivity by using colors to highlight language features. The syntax of a language refers to the way pieces of the language are arranged to make well-formed \u201csentences.\u201d To illustrate, the sentence<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\" style=\"margin-left: 108pt\"><em>The tall boy runs quickly to the door.<\/em><\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">uses proper English syntax. By comparison, the sentence<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 108pt\"><em>Boy the tall runs door to quickly the.<\/em><\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">is not correct syntactically. It uses the same words as the original sentence, but their arrangement does not follow the rules of English.<\/p>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">Similarly, programming languages have strict syntax rules that programmers must follow to create well-formed programs. Only well-formed programs are acceptable for translation into executable machine code. Some syntax-aware editors can use colors or other special annotations to alert programmers of syntax errors during the editing process.<\/p>\r\n\r\n<ul>\r\n \t<li><strong>Compilers<\/strong>. A compiler translates the source code to target code. The target code may be the machine language for a particular platform or embedded device. The target code could be another source language; for example, the earliest C++ compiler translated C++ into C, another higher-level language. The resulting C++ code was then processed by a C++ compiler to produce an executable program. (C++ compilers today translate C++ directly into machine language.) Compilers translate the contents of a source file and produce a file containing all the target code. Popular compiled languages include C, C++ , Java, C#.<\/li>\r\n \t<li><strong>Interpreters<\/strong>. An interpreter is like a compiler, in that it translates higher-level source code into target code (usually machine language). It works differently, however. While a compiler produces an executable program that may run many times with no additional translation needed, an interpreter translates source code statements into machine language each time a user runs the program. A compiled program does not need to be recompiled to run, but an interpreted program must be reinterpreted each time it executes. The interpreter in essence reads the code one line at a time. In general, compiled programs execute more quickly than interpreted programs because the translation activity occurs only once. Interpreted programs, on the other hand, can run as is on any platform with an appropriate interpreter; they do not need to be recompiled to run on a different platform. Python, for example, is used mainly as an interpreted language, but compilers for it are available. Interpreted languages are better suited for dynamic, explorative development which many people feel is ideal for beginning programmers. Popular scripting languages include Python, Ruby, Perl, and, for web browsers, Javascript.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">The Python interpreter is written in a high-level language called \"C\". You can look at the actual source code for the Python interpreter by going to <a class=\"rId60\"><span class=\"import-Hyperlink\">www.python.org<\/span><\/a> and working your way to their source code. So Python is a program itself and it is compiled into machine code. When you install Python on your computer, you copy a machine-code copy of the translated Python program onto your system. In Windows, the executable machine code for Python itself is likely in a file with a name like:<\/p>\r\n<code>C:\\Python35\\python.exe<\/code>\r\n<ul>\r\n \t<li><strong>Debuggers<\/strong>. A debugger allows a programmer to more easily trace a program\u2019s execution in order to locate and correct errors in the program\u2019s implementation. With a debugger, a developer can simultaneously run a program and see which line in the source code is responsible for the program\u2019s current actions. The programmer can watch the values of variables and other program elements to see if their values change as expected. Debuggers are valuable for locating errors (also called bugs) and repairing programs that contain errors. (See the Debugging Section in this Unit for more information about programming errors.)<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\"><strong>COMPILER<\/strong><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\" style=\"text-align: center\"><strong>INTERPRETER<\/strong><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">Compiler takes an entire program as input. It works on the complete program at once.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">Interpreter takes a single statement at a time as input. It works line by line.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">It generates Intermediate Object code (machine code).<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">It doesn\u2019t generate Intermediate code (machine code).<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">It executes Conditional control statements faster than Interpreter. It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">It executes Conditional control statements much slower than Compiler. In general, the overall execution time is slower.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">More memory required (since Object Code is generated).<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">Memory requirement is less, hence more memory efficient. It doesn't generate intermediate Object Code.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">Compiled program doesn't need to be compiled every time.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">Every time higher level program is converted into lower level program.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">Errors are displayed after entire program is checked. Hence debugging is comparatively hard.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">Errors are displayed for every instruction interpreted (if any). Continues translating the program until the first error is met, in which case it stops. Hence debugging is easier.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\r\n<p class=\"import-Normal\">Programming languages that use compilers are COBOL, C, C++ .<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\r\n<p class=\"import-Normal\">Programming languages that use interpreter are Visual Basic Script, Ruby, Perl.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p style=\"text-align: center\"><small>Table 1: Compiler Vs Interpreter<\/small><span class=\"import-FootnoteReference\">[footnote]Based on information from: https:\/\/www.softwaretestingmaterial.com\/compiler-vs-interpreter\/[\/footnote]<\/span><\/p>\r\n<p class=\"import-Normal\">Many developers use integrated development environments (IDEs). An IDE includes editors, debuggers, and other programming aids in one comprehensive program. Python IDEs include Wingware, PyCharm,, and IDLE.<\/p>\r\n<p class=\"import-Normal\">Despite the wide variety of tools (and tool vendors\u2019 claims), the programming process for all but trivial programs is not automatic. Good tools are valuable and certainly increase the productivity of developers, but they cannot write software. There are no substitutes for sound logical thinking, creativity, common sense, and, of course, programming experience.<\/p>\r\n\r\n<h1>Learning Programming with Python<\/h1>\r\n<p class=\"import-Normal\">Guido van Rossum created the Python programming language in the late 1980s. He named the language after the BBC show \"Monty Python's Flying Circus\". In contrast to other popular languages such as C, C++ , Java, and C#, Python strives to provide a simple but powerful syntax.<\/p>\r\n<p class=\"import-Normal\">Python is used for software development at companies and organizations such as Google, Yahoo, Facebook, CERN, Industrial Light and Magic, and NASA. It is especially relevant in developing information science applications such as ForecastWatch.com which uses Python to help meteorologists, online travel sites, airline reservation systems, university student record systems, air traffic control systems among many others[footnote]www.python.org\/about\/success\/[\/footnote]. Experienced programmers can accomplish great things with Python, but Python\u2019s beauty is that it is accessible to beginning programmers and allows them to tackle interesting problems more quickly than many other, more complex languages that have a steeper learning curve.<\/p>\r\n<p class=\"import-Normal\">Python has an extensive Standard Library which is a collection of built-in modules, each providing specific functionality beyond what is included in the \u201ccore\u201d part of Python. (For example, the math module provides additional mathematical functions. The random module provides the ability to generate random numbers)[footnote]Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p23[\/footnote]. Additionally the Standard Library can help you do various things involving regular expressions, documentation generation, databases, web browsers, CGI, FTP, email, XML, HTML, WAV files, cryptography, GUI (graphical user interfaces), among other things.<\/p>\r\n<p class=\"import-Normal\">More information about Python, including links to download the latest version for Microsoft Windows, Mac OS X, and Linux, can be found in Appendix A of this book and also at <a class=\"rId61\" href=\"http:\/\/www.python.org\"><span class=\"import-Hyperlink\">http:\/\/www.python.org<\/span><\/a> .<\/p>\r\n<p class=\"import-Normal\">In late 2008, Python 3.0 was released. Commonly called Python 3, the current version of Python, VERSION 3.0, is incompatible with earlier versions of the language. Many existing books and online resources cover Python 2, but more Python 3 resources now are becoming widely available. The code in this book is based on Python 3.<\/p>\r\n<p class=\"import-Normal\">This book does not attempt to cover all the facets of the Python programming language. The focus here is on introducing programming techniques and developing good habits and skills. To that end, this approach avoids some of the more obscure features of Python and concentrates on the programming basics that easily transfer directly to other programming languages.<\/p>\r\n<p class=\"import-Normal\">The goal is to turn you into a person who is skilled in the art of programming. In the end you will be a programmer - perhaps not a professional programmer, but at least you will have the skills to look at a data\/information analysis problem and develop a program to solve the problem.<\/p>\r\n<p class=\"import-Normal\">In a sense, you need two skills to be a programmer:<\/p>\r\n\r\n<ul>\r\n \t<li>First, you need to know the programming language (Python) - you need to know the vocabulary and the grammar (the syntax). You need to be able to spell the words in this new language properly and know how to construct well-formed \"sentences\" in this new language.<\/li>\r\n \t<li>Second, you need to \"tell a story\". In writing a story, you combine words and sentences to convey an idea to the reader. There is a skill and art in constructing the story, and skill in story writing is improved by doing some writing (practice) and getting some feedback. In programming, our program is the \"story\" and the problem you are trying to solve is the \"idea\".<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Once you learn one programming language such as Python, you will find it much easier to learn a second programming language such as JavaScript or C++ . Other programming languages have very different vocabulary and grammar (syntax) but the problem-solving skills will be the same across all programming languages.<\/p>\r\n<p class=\"import-Normal\">You will learn the \"vocabulary\" and \"sentences\" (the syntax) of Python pretty quickly. It will take longer for you to be able to write a coherent program to solve a new problem. We learn programming much like we learn writing. We start by reading and explaining programs, then we write simple programs, and then we write increasingly complex programs over time. At some point you \"get your muse\" and see the patterns on your own and can see more naturally how to take a problem and write a program that solves that computational problem. And once you get to that point, programming becomes a very pleasant and creative process.<\/p>\r\n<p class=\"import-Normal\">We start with the vocabulary and structure of Python programs. Be patient as the simple examples remind you of when you started reading for the first time.<\/p>\r\n\r\n<h1>Writing a Python Program<\/h1>\r\n<p class=\"import-Normal\">The text that makes up a Python program has a particular structure. The syntax must be correct, or the interpreter will generate error messages and not execute the program. This section introduces Python by providing a simple example program.<\/p>\r\n<p class=\"import-Normal\">A program consists of one or more <strong>statements<\/strong>. A statement is an instruction that the interpreter executes.<\/p>\r\n<p class=\"import-Normal\">The following statement invokes the print function to display a message:<\/p>\r\n<code>print(\"This is a simple Python program\")<\/code>\r\n<p class=\"import-Normal\">We can use the statement in a program. Figure 13 (<code>simple.py<\/code>) is an example of a very simple Python program that does something:<\/p>\r\n\r\n\r\n[caption id=\"attachment_118\" align=\"aligncenter\" width=\"349\"]<img class=\"wp-image-118 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232309\/image15-1.png\" alt=\"\" width=\"349\" height=\"129\" \/> Figure 13: Listing of simple.py[\/caption]\r\n<p class=\"import-Normal\">IDLE is Python\u2019s Integrated Development and Learning Environment (IDE) and is included as part of the Python Standard Library which is distributed with Python 3 (See Appendix A). IDLE is the standard Python development environment. Its name is an acronym of \"<strong>I<\/strong>ntegrated <strong>D<\/strong>eve<strong>L<\/strong>opment <strong>E<\/strong>nvironment\". It works well on both Unix and Windows platforms.<\/p>\r\n<p class=\"import-Normal\">IDLE has a <strong>Python <\/strong><strong>S<\/strong><strong>hell window<\/strong>, which gives you access to the Python interactive mode. It also has a file editor that lets you create and edit existing Python source files. The file editor was used to write the <code>simple.py<\/code> program.<\/p>\r\n<p class=\"import-Normal\">The way you launch IDLE depends on your operating system and how it was installed. Figure 13 shows a screenshot of IDLE running on a Windows 8.1 computer. The IDE consists of a simple menu bar at the top. Other Python IDEs are similar in appearance.<\/p>\r\n<p class=\"import-Normal\">To begin entering our program, we just type in the Python statements. To start a new program, choose the <strong>New<\/strong><strong> File<\/strong> item from the <strong>File<\/strong> menu. This action produces a new editor pane for a file named <code>Untitled<\/code> as shown in Figure 14 below.<\/p>\r\n\r\n\r\n[caption id=\"attachment_119\" align=\"aligncenter\" width=\"348\"]<img class=\"wp-image-119 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232311\/image16-1.png\" alt=\"\" width=\"348\" height=\"111\" \/> Figure 14: New File Editor Window[\/caption]\r\n<p class=\"import-Normal\">We now are ready to type in the code that constitutes the program.<\/p>\r\n<code>print(\"This is a simple Python program\")<\/code>\r\n<p class=\"import-Normal\">Next we will save the file. The menu sequence <strong>File<\/strong><strong>Save<\/strong> or <strong>File<\/strong> <strong>Save As<\/strong>, produces the dialog box shown in Figure 15 that allows us to select a folder and filename for our program. You should be sure all Python programs are saved with a <code>.py<\/code> extension. If \u201cSave as Type: Python files\u201d is available there is no need to add the <code>.py<\/code> extension as it will automatically be saved as a <code>.py<\/code> file (see Figure 15).<\/p>\r\n\r\n\r\n[caption id=\"attachment_120\" align=\"aligncenter\" width=\"417\"]<img class=\"wp-image-120 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232314\/image17-1.png\" alt=\"\" width=\"417\" height=\"230\" \/> Figure 15: Save Python File, Option 1[\/caption]\r\n\r\nIf you are using a different text editor select \u201cSave as Type: All files\u201d and add the <code>.py<\/code> extension (see Figure 16).\r\n\r\n[caption id=\"attachment_121\" align=\"aligncenter\" width=\"401\"]<img class=\"wp-image-121 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232317\/image18-1.png\" alt=\"\" width=\"401\" height=\"222\" \/> Figure 16: Save Python File, Option 2[\/caption]\r\n\r\nWe can run the program by selecting the menu sequence <strong>Run --&gt; <\/strong><strong>Run Module<\/strong> or use the shortcut key <strong>F5<\/strong>. A new window labeled <strong>Python Shell<\/strong> will display the program\u2019s output. Figure 17 shows the results of running the program.\r\n\r\n[caption id=\"attachment_122\" align=\"aligncenter\" width=\"425\"]<img class=\"wp-image-122 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232320\/image19-1.png\" alt=\"\" width=\"425\" height=\"160\" \/> Figure 17: Program simple.py Output[\/caption]\r\n<p class=\"import-Normal\">This program contains one Python statement. A statement is a command that the interpreter executes. This statement prints the message <code>This is a simple Python program in the Python Shell window<\/code>. A statement is the fundamental unit of execution in a Python program. Statements may be grouped into larger chunks called blocks, and blocks can make up more complex statements (e.g. the selection structure or the iterative structure we saw in the last Unit). The statement <code>print(\"This is a simple Python program\")<\/code> makes use of a built in function named <code>print<\/code>. Python has a variety of different kinds of statements that we can use to build programs, and the sections that follow explore these various kinds of statements.<\/p>\r\n\r\n<div class=\"textbox\"><strong>Note to Reader:<\/strong> In the context of programming, a function is a named sequence of statements that performs a computation. The name of the function here is <code>print.<\/code> The expression in parentheses is called the <strong>argument<\/strong> of the function. The result, for this function, is the string of characters in quotes (i.e. the \u2018message\u2019) of the <strong>argument<\/strong>.It is common to say that a function \u201ctakes\u201d an argument and \u201creturns\u201d a result. The result is called the return value.<\/div>\r\n<p class=\"import-Normal\">When you type a statement on the command line in the Shell window and select the Enter key, Python executes it. Statements alone don\u2019t produce any result.<\/p>\r\n\r\n<h1>The Python Interactive Shell<\/h1>\r\n<p class=\"import-Normal\">We created the program in Figure 13 (simple.py) and submitted it to the Python interpreter for execution. We can interact with the interpreter directly, typing in Python statements and expressions for immediate execution. As we saw in Figure 17, the IDLE window labeled <em>Python Shell<\/em> is where the executing program directs its output. We also can type commands into the Python Shell window, and the interpreter will attempt to execute them. Figure 18 shows how the interpreter responds when we enter the program statement directly into the Shell. The interpreter prompts the user for input with three greater-than symbols (&gt;&gt;&gt;). This means the user typed in the text on the line prefixed with &gt;&gt;&gt;. Any lines without the &gt;&gt;&gt; prefix represent the interpreter\u2019s output, or feedback, to the user. We will find Python\u2019s interactive interpreter invaluable for experimenting with various language constructs.<\/p>\r\n\r\n\r\n[caption id=\"attachment_123\" align=\"aligncenter\" width=\"455\"]<img class=\"wp-image-123 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232322\/image20-1.png\" alt=\"\" width=\"455\" height=\"156\" \/> Figure 18: Executing Individual Commands in the Python Shell[\/caption]\r\n\r\nWe can discover many things about Python without ever writing a complete program. We can execute the interactive Python interpreter directly from the command line in the Python Shell. The interpreter prompt (&gt;&gt;&gt;) prefixes all user input in the interactive Shell. Lines that do not begin with the &gt;&gt;&gt; prompt represent the interpreter\u2019s response.If you try to enter each line one at a time into the interactive Shell, the program\u2019s output will be intermingled with the statements you type. In this case the best approach is to type the program into an editor, save the code you type to a file, and then execute the program. Most of the time we use an editor to enter and run our Python programs. The interactive interpreter is most useful for experimenting with small snippets of Python code.\r\n<div class=\"textbox\">\r\n\r\n<strong>Note to Reader<\/strong>:\r\n\r\nThe print() function always ends with an invisible \"new line\" character ( \\n ) so that repeated calls to print will all print on a separate line each. To prevent this newline character from being printed, you can specify that it should end with a blank:\r\n<code>print('a', end='')\r\nprint('b', end='')<\/code>\r\nOutput is:\r\n<code>ab<\/code>\r\nOr you can end with a space:\r\n<code>print('a', end=' ')\r\nprint('b', end=' ')print('c')<\/code>\r\nOutput is:\r\n<code>a b<\/code>\r\nOr you can end with a space:\r\n<code>print('a', end=' ')\r\nprint('b', end=' ')\r\nprint('c')<\/code>\r\nOutput is:\r\n<code>a b c<\/code>\r\n\r\n<\/div>\r\n<h1>Debugging<\/h1>\r\n[caption id=\"\" align=\"alignright\" width=\"298\"]<img class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232324\/image21.jpg\" alt=\"image\" width=\"298\" height=\"234\" \/> Figure 19: First Computer Bug (Image \u00a9Courtesy of the Naval Surface Warfare Center, Dahlgren, VA., 1988. NHHC Collection)[\/caption]\r\n<p class=\"import-Normal\">Programming is a complex process, and because it is done by human beings, it often leads to errors. Programming errors are called bugs and the process of tracking them down and correcting them is called debugging.<\/p>\r\n<p class=\"import-Normal\">The story behind this term dates back to September 9, 1947, when Harvard\u2019s Mark II Aiken Relay computer was malfunctioning. After rooting through the massive machine to find the cause of the problem, Admiral Grace Hopper, who worked in the Navy\u2019s engineering program at Harvard, found the bug. It was an actual insect. The incident is recorded in Hopper\u2019s logbook alongside the offending moth, taped to the logbook page: \u201c15:45 Relay #70 Panel F (moth) in relay. First actual case of bug being found.\u201d[footnote]Jacobson, Molly McBride. \"Grace Hopper's Bug.\" Atlas Obscura, https:\/\/www.atlasobscura.com\/places\/grace-hoppers-bug . Accessed January 9, 2018.[\/footnote]<\/p>\r\n<p class=\"import-Normal\">Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors. It is useful to distinguish between them in order to track them down more quickly.<\/p>\r\n\r\n<h3>Syntax errors<\/h3>\r\nPython can only execute a program if the program is syntactically correct; otherwise, the process fails and returns an error message. Syntax refers to the structure of a program and the rules about that structure. For example, in English, a sentence must begin with a capital letter and end with a period.\r\n<div class=\"textbox\"><strong>this sentence contains a syntax error.<\/strong>\r\n<strong>So does this one<\/strong><\/div>\r\n<p class=\"import-Normal\">For most readers, a few syntax errors are not a significant problem, which is why we can read the poetry of E. E. Cummings without problems. Python is not so forgiving. If there is a single syntax error anywhere in your program, Python will display an error message and quit, and you will not be able to run your program. Early on, you will probably spend a lot of time tracking down syntax errors. As you gain experience, though, you will make fewer errors and find them faster.<\/p>\r\n\r\n<h3>Runtime errors<\/h3>\r\n<p class=\"import-Normal\">The second type of error is a runtime error, so called because the error does not appear until you run the program. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.<\/p>\r\n<p class=\"import-Normal\">Runtime errors are rare in the simple programs you will see in the first Units, so it might be a while before you encounter one.<\/p>\r\n\r\n<h3>Semantic errors<\/h3>\r\n<p class=\"import-Normal\">The third type of error is the semantic error. If there is a semantic error in your program, it will run successfully, in the sense that the computer will not generate any error messages and quit, but it will not do the right thing. It will do something else. Specifically, it will do what you told it to do.<\/p>\r\n<p class=\"import-Normal\">The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing. The test cases we generated in UNIT #1 helps programmers fix semantic errors.<\/p>\r\n\r\n<h3>Experimental debugging<\/h3>\r\n<p class=\"import-Normal\">One of the most important skills you will acquire is debugging. Although it can be frustrating, debugging is one of the most intellectually rich, challenging, and interesting parts of programming.<\/p>\r\n<p class=\"import-Normal\">In some ways, debugging is like detective work. You are confronted with clues, and you have to infer the processes and events that led to the results you see.<\/p>\r\n<p class=\"import-Normal\">Debugging is also like an experimental science. Once you have an idea what is going wrong, you modify your program and try again. If your hypothesis was correct, then you can predict the result of the modification, and you take a step closer to a working program. If your hypothesis was wrong, you have to come up with a new one.<\/p>\r\n<p class=\"import-Normal\">For some people, programming and debugging are the same thing. That is, programming is the process of gradually debugging a program until it does what you want. The idea is that you should start with a program that does something and make small modifications, debugging them as you go, so that you always have a working program.<\/p>\r\n\r\n<h1>The Basics of Python Programming<\/h1>\r\n<p class=\"import-Normal\">Just printing a single sentence is not enough, is it? You want to do more than that - you want to take some input, manipulate it and get something out of it. We can achieve this in Python using constants and variables, and we'll learn some other concepts as well in this section.<\/p>\r\n\r\n<h3>Comments<\/h3>\r\n<p class=\"import-Normal\">Comments are any text to the right of the <strong># <\/strong>symbol and is mainly useful as notes for the reader of the program.<\/p>\r\n<p class=\"import-Normal\">For example:<\/p>\r\n\r\n<div class=\"textbox\" style=\"text-align: left\">\r\n\r\n<code>print('hello world') # Note that print is a function<\/code>\r\n<p style=\"text-align: center\"><strong>OR<\/strong><\/p>\r\n<code># Note that print is a function<\/code>\r\n<code>print('hello world')<\/code>\r\n\r\n<\/div>\r\n<p class=\"import-Normal\">Use as many useful comments as you can in your program to:<\/p>\r\n\r\n<ul>\r\n \t<li>explain assumptions<\/li>\r\n \t<li>explain important decisions<\/li>\r\n \t<li>explain important details<\/li>\r\n \t<li>explain problems you're trying to solve<\/li>\r\n \t<li>explain problems you're trying to overcome in your program, etc.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Code tells you <strong>how<\/strong>, comments should tell you <strong>why<\/strong>.<\/p>\r\n<p class=\"import-Normal\">This is useful for readers of your program so that they can easily understand what the program is doing. Consider the programmer, newly hired, who has been assigned the job of maintaining a 2000 line-of-code program. Without comments the codes can be very difficult to understand let alone maintain.<\/p>\r\n\r\n<h3>Literal Constants<\/h3>\r\n<p class=\"import-Normal\">An example of a literal constant is a number like <code>5<\/code> , <code>1.23<\/code> , or a string like <code>'This is a string'<\/code> or <code>\"It's a string!\"<\/code> (String literals must be in quotes).<\/p>\r\n<p class=\"import-Normal\">It is called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.<\/p>\r\n\r\n<h3>Numbers<\/h3>\r\n<p class=\"import-Normal\">Numbers are mainly of two types - integers and floats. An example of an integer is 2 which is just a whole number. Examples of floating point numbers (or floats for short) are <code>3.23<\/code> and <code>7845.322222<\/code>.<\/p>\r\n\r\n<h3>Strings<\/h3>\r\n<p class=\"import-Normal\">A string is a sequence of characters. Strings can be a single character, a single word or a bunch of words. You will be using strings in many Python programs that you write. Note the following:<\/p>\r\n<p class=\"import-Normal\">Single Quote: You can specify (define) strings using single quotes such as <code>'Quote me on this'<\/code> . All white space i.e. spaces and tabs, within the quotes, are preserved as-is.<\/p>\r\n<p class=\"import-Normal\">Double Quotes: Strings in double quotes work exactly the same way as strings in single quotes. An example is <code>\"What's your name?\"<\/code> .There is no difference in using single quotes or double quotes , just be sure to use a matching set.<\/p>\r\n<p class=\"import-Normal\">Triple Quotes: You can specify multi-line strings using triple quotes - ( \"\"\" or ''' ). You can use single quotes and double quotes freely within the triple quotes. An example is:<\/p>\r\n\r\n<div class=\"textbox\"><code>'''This is a multi-line string. This is the first line.\r\nThis is the second line.\r\n\"What's your name?,\" I asked.\r\nHe said \"Bond, James Bond.\"\r\n'''<\/code><\/div>\r\n<p class=\"import-Normal\">Strings are immutable. This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn't. We will see why this is not a limitation in the various programs that we see later on.<\/p>\r\n\r\n<h3>Variables<\/h3>\r\n<p class=\"import-Normal\">Using just literal constants can soon become boring - we need some way of storing any information and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.<\/p>\r\n<p class=\"import-Normal\">One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.<\/p>\r\n<code>&gt;&gt;&gt; message = \"What is today\u2019s date?\"\r\n&gt;&gt;&gt; n = 17\r\n&gt;&gt;&gt; pi = 3.14159<\/code>\r\n<p class=\"import-Normal\">The <strong>assignment statement<\/strong> gives a value to a variable:<\/p>\r\n<p class=\"import-Normal\">This example makes three assignments. The first assigns the string value <code>\"What is today\u2019s date?\"<\/code> to a variable named message. The second assigns the integer 17 to n, and the third assigns the floating-point number 3.14159 to a variable called <code>pi.<\/code><\/p>\r\n<p class=\"import-Normal\">The <strong>assignment <\/strong><strong>symbol<\/strong>, =, should not be confused with equals, which uses the symbol ==. The assignment statement binds a name, on the left-hand side of the operator, to a value, on the right-hand side. This is why you will get an error if you enter:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; 17 = n\r\nFile \"&lt;interactive input&gt;\", line 1\r\nSyntaxError: can't assign to literal<\/code><\/div>\r\n<div class=\"textbox\">Tip: When reading or writing code, say to yourself \u201cn is assigned 17\u201d or \u201cn gets the value 17\u201d. Don\u2019t say \u201cn equals 17\u201d.<\/div>\r\n<p class=\"import-Normal\">A common way to represent variables on paper is to write the name with an arrow pointing to the variable\u2019s value. This kind of diagram is called a state snapshot because it shows what state each of the variables is in at a particular instant in time. (Think of it as the variable\u2019s state of mind). The following diagram shows the result of executing the assignment statements:<\/p>\r\n\r\n<div class=\"textbox shaded\">message\u00a0\u2192 \"What is today\u2019s date?\"\r\nn \u2192 17\r\npi \u2192 3.14159<\/div>\r\n<div>If you ask the interpreter to evaluate a variable in the Python Shell, it will produce the value that is currently linked to the variable:<\/div>\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; message\r\n\u2018What is today\u2019s date?\u2019\r\n&gt;&gt;&gt; n\r\n17\r\n&gt;&gt;&gt; pi\r\n3.14159<\/code><\/div>\r\n<p class=\"import-Normal\">We use variables in a program to \u201cremember\u201d things, perhaps the current score at the football game. But variables are variable. This means they can change over time, just like the scoreboard at a football game. You can assign a value to a variable, and later assign a different value to the same variable. (This is different from mathematics. In mathematics, if you give `x` the value 3, it cannot change to link to a different value half-way through your calculations!). For example:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; day = \"Thursday\"\r\n&gt;&gt;&gt; day\r\n'Thursday'\r\n&gt;&gt;&gt; day = \"Friday\"\r\n&gt;&gt;&gt; day\r\n'Friday'\r\n&gt;&gt;&gt; day = 21\r\n&gt;&gt;&gt; day\r\n21<\/code><\/div>\r\n<p class=\"import-Normal\">You\u2019ll notice we changed the value of day three times, and on the third assignment we even made it refer to a value that was of a different data type.<\/p>\r\n<p class=\"import-Normal\">A great deal of programming is about having the computer remember things, e.g. The number of missed calls on your phone, and then arranging to update or change the variable when you miss another call.<\/p>\r\n\r\n<div class=\"textbox\">\r\n\r\n<strong>Note to Reader:<\/strong>\r\n\r\nThere are two common variable genres that are used in computer programming. They are so regularly used that they have special names.\r\n\r\n<strong>accumulator<\/strong>: A variable used in a loop to add up or accumulate a result.\r\n\r\n<strong>counter<\/strong>: A variable used to count something, usually initialized to zero and then incremented.\r\n\r\n<\/div>\r\n<h3>Identifier Naming<\/h3>\r\n<p class=\"import-Normal\">Variables are examples of identifiers. Identifiers are names given to identify something. There are some rules you have to follow for naming identifiers:<\/p>\r\n\r\n<ul>\r\n \t<li>The first character of the identifier must be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore ( _ ).<\/li>\r\n \t<li>The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores ( _ ) or digits (0-9).<\/li>\r\n \t<li>Identifier names are case-sensitive. For example, myname and myName are not the same. Note the lowercase n in the former and the uppercase N in the latter.<\/li>\r\n \t<li>Examples of valid identifier names are <code>i<\/code> , <code>name_2_3<\/code> . Examples of invalid identifier names are <code>2things<\/code> , <code>this is spaced out<\/code> , <code>my-name<\/code> and <code>&gt;a1b2_c3<\/code><\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Python keywords define the language\u2019s syntax rules and structure, and they cannot be used as variable names.<\/p>\r\n<p class=\"import-Normal\">Python has thirty-something keywords (and every now and again improvements to Python introduce or eliminate one or two):<\/p>\r\n\r\n<table style=\"height: 184px\">\r\n<tbody>\r\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\r\n<p class=\"import-Normal\"><code>and<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\r\n<p class=\"import-Normal\"><code>as<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\r\n<p class=\"import-Normal\"><code>assert<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\r\n<p class=\"import-Normal\"><code>break<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\r\n<p class=\"import-Normal\"><code>class<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\r\n<p class=\"import-Normal\"><code>continue<\/code><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\r\n<p class=\"import-Normal\"><code>def<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\r\n<p class=\"import-Normal\"><code>del<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\r\n<p class=\"import-Normal\"><code>elif<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\r\n<p class=\"import-Normal\"><code>else<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\r\n<p class=\"import-Normal\"><code>except<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\r\n<p class=\"import-Normal\"><code>exec<\/code><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\r\n<p class=\"import-Normal\"><code>finally<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\r\n<p class=\"import-Normal\"><code>for<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\r\n<p class=\"import-Normal\"><code>from<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\r\n<p class=\"import-Normal\"><code>global<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\r\n<p class=\"import-Normal\"><code>if<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\r\n<p class=\"import-Normal\"><code>import<\/code><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\r\n<p class=\"import-Normal\"><code>in<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\r\n<p class=\"import-Normal\"><code>is<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\r\n<p class=\"import-Normal\"><code>lambda<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\r\n<p class=\"import-Normal\"><code>nonlocal<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\r\n<p class=\"import-Normal\"><code>not<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\r\n<p class=\"import-Normal\"><code>or<\/code><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\r\n<p class=\"import-Normal\"><code>pass<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\r\n<p class=\"import-Normal\"><code>raise<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\r\n<p class=\"import-Normal\"><code>return<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\r\n<p class=\"import-Normal\"><code>try<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\r\n<p class=\"import-Normal\"><code>while<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\r\n<p class=\"import-Normal\"><code>with<\/code><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\r\n<p class=\"import-Normal\"><code>yield<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\r\n<p class=\"import-Normal\"><code>True<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\r\n<p class=\"import-Normal\"><code>False<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\r\n<p class=\"import-Normal\"><code>None<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\r\n<p class=\"import-Normal\"><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\r\n<p class=\"import-Normal\"><\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<small>Table 2: Python Keywords<\/small>\r\n<p class=\"import-Normal\">You might want to keep this table handy. If the interpreter complains about one of your variable names and you don\u2019t know why, see if it is in this table.<\/p>\r\n<p class=\"import-Normal\">Programmers generally choose names for their variables that are meaningful to the human readers of the program \u2014 they help the programmer document, or remember, what the variable is used for.<\/p>\r\n\r\n<div class=\"textbox\"><strong>Note to Reader<\/strong>:\r\nBeginners sometimes confuse \u201cmeaningful to the human readers\u201d with \u201cmeaningful to the computer\u201d. So they\u2019ll wrongly think that because they\u2019ve called some variable <code>average<\/code> or <code>pi<\/code>, it will somehow magically calculate an average, or magically know that the variable pi should have a value like 3.14159. No! The computer doesn\u2019t understand what you intend the variable to mean.So you\u2019ll find some textbooks or tutorials that deliberately don\u2019t choose meaningful names when they teach beginners \u2014 not because we don\u2019t think it is a good habit, but because we\u2019re trying to reinforce the message that you \u2014 the programmer \u2014 must write the program code to calculate the average, and you must write an assignment statement to give the variable <code>pi<\/code> the value you want it to have.<\/div>\r\n<h3>Indentation<\/h3>\r\n<p class=\"import-Normal\">Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.<\/p>\r\n<p class=\"import-Normal\">This means that statements which go together must have the same indentation. Each such set of statements is called a block. We will see examples of how blocks are important in later sections and Units.<\/p>\r\n<p class=\"import-Normal\">One thing you should remember is that wrong indentation can give rise to errors. For example:<\/p>\r\n\r\n<div class=\"textbox\"><code>i = 5\r\n# Error below! Notice a single space at the start of the line print('Value is', i)\r\nprint('I repeat, the value is', i)<\/code><\/div>\r\n<p class=\"import-Normal\">When you run this, you get the following error:<\/p>\r\n\r\n<div class=\"textbox\"><code>File \"whitespace.py\", line 3\r\nprint('Value is', i)<\/code>\r\n^\r\n<code style=\"color: #ff0000\">IndentationError: unexpected indent<\/code><\/div>\r\n<p class=\"import-Normal\">Notice that there is a single space at the beginning of the second line. The error indicated by Python tells us that the syntax of the program is invalid i.e. the program was not properly written. What this means to you is that you cannot arbitrarily start new blocks of statements (except for the default main block which you have been using all along, of course). Cases where you can use new blocks (such as the iteration control structure) will be detailed in later Units.<\/p>\r\n\r\n<div class=\"textbox\"><strong>How to indent:<\/strong> Use four spaces for indentation. This is the official Python language recommendation. Good editors (including IDLE) will automatically do this for you. Make sure you use a consistent number of spaces for indentation, otherwise your program will not run or will have unexpected behavior.<\/div>\r\n<h1>Example: Using Variables and Literal Constants<\/h1>\r\n<p class=\"import-Normal\">Practice: Type, save and run the following program,var.py, using the Python editor.<\/p>\r\n\r\n<div class=\"textbox\"><code># Filename : var.py\r\ni = 5\r\nprint(i)\r\ni = i + 1\r\nprint(i)\r\ns = '''This is a multi-line string.\r\nThis is the second line.'''\r\nprint(s)<\/code><\/div>\r\nOutput:\r\n<div class=\"textbox\"><code>5\r\n6\r\nThis is a multi-line string.\r\nThis is the second line.<\/code><\/div>\r\n<p class=\"import-Normal\">Let us examine how this program works.<\/p>\r\n\r\n<table>\r\n<tbody>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">Python Statement<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">Explanation<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>i = 5<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">First, we assign the literal constant value 5 to the variable i using the assignment operator ( = ). This line is called a statement because it states that something should be done and in this case, we connect the variable name i to the value 5.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>print(i)<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Next, we print the value of i using the print statement which, unsurprisingly, just prints the value of the variable to the screen<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>i = i + 1<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Here we add 1 to the value stored in i and store it back into i.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>print(i)<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">We then print it and expectedly, we get the value 6 .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>s = '''This is a multi-line string.<br style=\"clear: both\" \/>This is the second line.'''<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Here we assign the literal string constant to the variable s.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\"><code>print(s)<\/code><\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">We then print it.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h1>Operators and Expressions<\/h1>\r\n<p class=\"import-Normal\">Most statements (logical lines) that you write will contain expressions. A simple example of an expression is <code>2+3<\/code>. An expression can be broken down into operators and operands.<\/p>\r\n<p class=\"import-Normal\">Operators are functionality that do something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data is called operands. In this case, <code>2<\/code> and<code> 3<\/code> are the operands.<\/p>\r\n<p class=\"import-Normal\">When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.<\/p>\r\n\r\n<h1>Operators<\/h1>\r\n<p class=\"import-Normal\">We will briefly take a look at the operators and their usage.<\/p>\r\n<p class=\"import-Normal\">Note that you can evaluate the expressions given in the examples using the interpreter interactively. For example, to test the expression 2+3 , use the interactive Python interpreter prompt in the Shell window:<\/p>\r\n\r\n<div class=\"textbox\" style=\"padding: 0pt 5.4pt 0pt 12pt\">\r\n<p class=\"import-Normal\"><code>&gt;&gt;&gt; 2 + 3\r\n5<\/code>\r\n<code>&gt;&gt; 3 * 5\r\n<\/code><code>15\r\n<\/code><code>&gt;&gt;&gt;<\/code>\r\n\r\n<\/div>\r\n<p class=\"import-Normal\">Here is a quick overview of the available operators:<\/p>\r\n\r\n<table>\r\n<tbody>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">+ (plus)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Adds two objects<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">3 + 5 gives 8 . <br style=\"clear: both\" \/>'a' + 'b' gives 'ab' .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">- (minus)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Gives the subtraction of one number from the other; if the first operand is absent it is assumed to be zero.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">-5.2 gives a negative number <br style=\"clear: both\" \/>50 - 24 gives 26 .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">* (multiply)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Gives the multiplication of the two numbers or returns the string repeated that many times.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">2 * 3 gives 6 .<br style=\"clear: both\" \/>'la' * 3 gives 'lalala' .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">** (power)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Returns x to the power of y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">\/ (divide)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Divide x by y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">13 \/ 3 gives 4.333333333333333<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">\/\/ (divide and floor)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Divide x by y and round the answer down to the nearest whole number<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">13 \/\/ 3 gives 4<br style=\"clear: both\" \/>-13 \/\/ 3 gives -5<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">% (modulo)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Returns the remainder of the division<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">13 % 3 gives 1 .<br style=\"clear: both\" \/>-25.5 % 2.25 gives 1.5 .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">&lt; (less than)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Returns whether x is less than y. All comparison operators return True or False . <br style=\"clear: both\" \/>Note the capitalization of these names.<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">5 &lt; 3 gives False <br style=\"clear: both\" \/>3 &lt; 5 gives True .<br style=\"clear: both\" \/>Comparisons can be chained arbitrarily: <br style=\"clear: both\" \/>3 &lt; 5 &lt; 7 gives True .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">&gt; (greater than)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Returns whether x is greater than y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">5 &gt; 3 returns True <br style=\"clear: both\" \/>If both operands are numbers, they are first converted to a common type. Otherwise, it always returns False .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">&lt;= (less than or equal to)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Returns whether x is less than or equal to y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = 3;<br style=\"clear: both\" \/>y = 6; <br style=\"clear: both\" \/>x &lt;= y returns True<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">&gt;= (greater than or equal to)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Returns whether x is greater than or equal to y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = 4; <br style=\"clear: both\" \/>y = 3; <br style=\"clear: both\" \/>x &gt;= 3 returns True<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">== (equal to)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Compares if the objects are equal<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = 2; y = 2; x == y returns True<br style=\"clear: both\" \/>x = 'str'; y = 'stR'; x == y returns False<br style=\"clear: both\" \/>x = 'str'; y = 'str'; x == y returns True<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">!= (not equal to)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">Compares if the objects are not equal<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = 2; <br style=\"clear: both\" \/>y = 3; <br style=\"clear: both\" \/>x != y returns True<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">not (boolean NOT)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">If x is True , it returns False . If x is False , it returns True .<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = True; <br style=\"clear: both\" \/>not x returns False .<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">and (boolean AND)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x and y returns False if x is False , else it returns evaluation of y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = False; y = True; <br style=\"clear: both\" \/>x and y returns False since x is False. <br style=\"clear: both\" \/>In this case, Python will not evaluate y since it knows that the left hand side of the 'and' expression is False which implies that the whole expression will be False irrespective of the other values. <br style=\"clear: both\" \/>This is called short-circuit evaluation.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">or (boolean OR)<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">If x is True , it returns True, else it returns evaluation of y<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">x = True; y = False; <br style=\"clear: both\" \/>x or y returns True . <br style=\"clear: both\" \/>Short-circuit evaluation applies here as well.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p style=\"text-align: center\"><small><em>Table 3: Python Operators<\/em><\/small><\/p>\r\n<p class=\"import-Normal\">Some of the comparison operators work on strings. For example the + operator (plus) works with strings, but it is not addition in the mathematical sense. Instead it performs <strong>concatenation<\/strong>, which means joining the strings by linking them end to end.<\/p>\r\n<p class=\"import-Normal\">The * operator also works on strings; it performs repetition. For example,<code>'Fun'*3<\/code> is\u00a0<code>'FunFunFun'<\/code>. One of the operands has to be a string; the other has to be an integer.<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; first = 10\r\n&gt;&gt;&gt; second = 15\r\n&gt;&gt;&gt; print(first + second)\r\n25\r\n&gt;&gt;&gt; first = '100'\r\n&gt;&gt;&gt; second = '150'\r\n&gt;&gt;&gt; print(first + second)\r\n100150<\/code><\/div>\r\nPython does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters, so: the word, Zebra, comes before apple. A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison.\r\n<h1>Practice with Operators &amp; Expressions<\/h1>\r\n<h3 class=\"import-Normal\"><span class=\"import-Heading3Char\">Practice #1<\/span><\/h3>\r\n<p class=\"import-Normal\">Use the Python IDLE Shell to calculate:<\/p>\r\n\r\n<ol>\r\n \t<li>6 + 4*10<\/li>\r\n \t<li>(6 + 4)*10 (Compare this to the previous expression, and note that Python uses parentheses just like you would in normal math to determine order of operations!)<\/li>\r\n \t<li>23.0 to the 5th power<\/li>\r\n \t<li>Positive root of the following equation: 34*x^2 + 68*x \u2013 510<br style=\"clear: both\" \/>Recall:<br style=\"clear: both\" \/>a*x^2+b*x+c<br style=\"clear: both\" \/>x1 = ( - b + sqrt ( b*b - 4*a*c ) ) \/ ( 2*a)<\/li>\r\n<\/ol>\r\n<h3 class=\"import-Normal\"><span class=\"import-Heading3Char\">Practic<\/span><span class=\"import-Heading3Char\">e #2<\/span><\/h3>\r\n<p class=\"import-Normal\">So now let us convert 645 minutes into hours. Use IDLE\u2019s Python Shell to type in the following:<\/p>\r\n<code>&gt;&gt;&gt; minutes = 645\r\n&gt;&gt;&gt; hours = minutes \/ 60\r\n&gt;&gt;&gt; hours<\/code>\r\n<p class=\"import-Normal\">Oops! The output gives us 10.75 which is not what we expected. In Python 3, the division operator \/ always yields a floating point result. What we might have wanted to know was how many whole hours there are, and how many minutes remain. Python gives us two different flavors of the division operator. The second, called floor division uses the token \/\/. Its result is always a whole number \u2014 and if it has to adjust the number it always moves it to the left on the number line. So 6 \/\/ 4 yields 1, but -6 \/\/ 4 might surprise you!<\/p>\r\n\r\n<h3><span class=\"import-Heading3Char\">Practice<\/span><span class=\"import-Heading3Char\">#3<\/span><\/h3>\r\nTry this:\r\n<div class=\"textbox\"><code>\u00a0&gt;&gt;&gt; 7 \/ 4\r\n1.75\r\n&gt;&gt;&gt; 7 \/\/ 4\r\n1\r\n&gt;&gt;&gt; minutes = 645\r\n&gt;&gt;&gt; hours = minutes \/\/ 60\r\n&gt;&gt;&gt; hours\r\n10\u00a0<\/code><\/div>\r\n<p class=\"import-Normal\">Take care that you choose the correct flavor of the division operator. If you\u2019re working with expressions where you need floating point values, use the division operator that does the division accurately.<\/p>\r\n\r\n<h1>Evaluation Order<\/h1>\r\n<p class=\"import-Normal\">If you had an expression such as <code>2 + 3 * 4<\/code> , is the addition done first or the multiplication?<\/p>\r\n<p class=\"import-Normal\">Recall from Algebra PEMDAS (parenthesis, exponents, multiplication, division, addition, subtraction).This tells us that the multiplication should be done first and that the multiplication operator has higher precedence than the addition operator.<\/p>\r\n<p class=\"import-Normal\">It is far better to use parentheses to group operators and operands appropriately in order to explicitly specify the precedence. This makes the program more readable. For example, <code>2 + (3 * 4)<\/code> is definitely easier to read than<code> 2 + 3 * 4<\/code> which requires knowledge of the operator precedence\u2019s.<\/p>\r\n<p class=\"import-Normal\">There is an additional advantage to using parentheses - it helps us to change the order of evaluation. For example, if you want addition to be evaluated before multiplication in an expression, then you can write something like <code>(2 + 3) * 4<\/code> .<\/p>\r\n\r\n<h3 class=\"import-Normal\"><span class=\"import-Heading3Char\">Practic<\/span><span class=\"import-Heading3Char\">e #4 <\/span><\/h3>\r\n<p class=\"import-Normal\">Use the Python editor to enter the following code (save as <code>expression.py<\/code> ):<\/p>\r\n\r\n<div class=\"textbox\"><code># given the length and width calculate the area and the<\/code>\r\n<code># perimeter of a rectangle<\/code>\r\n<code>length = 5<\/code>\r\n<code>width = 2<\/code>\r\n<code>area = length * width<\/code>\r\n<code>print('Area is', area)<\/code>\r\n<code>print('Perimeter is', 2 * (length + width))<\/code><\/div>\r\n<p class=\"import-Normal\">Output:<\/p>\r\n\r\n<div class=\"aligncenter wp-caption\"><img class=\"aligncenter wp-caption wp-image-124\" style=\"width: 395px\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232327\/image22-1-1.png\" alt=\"\" width=\"395\" height=\"201\" \/><\/div>\r\n<div class=\"unit-2:-writing-simple-programs.\">\r\n<div class=\"aligncenter wp-caption\"><\/div>\r\n<p class=\"import-Normal\">Let us examine how this program works.<\/p>\r\n\r\n<table>\r\n<tbody>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">Python Statement<\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\" style=\"text-align: center\">Explanation<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>length = 5<\/code><\/p>\r\n<p class=\"import-Normal\"><code>width = 2<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">The <code>length<\/code> and <code>width<\/code> of the rectangle are stored in variables by the same name. They are each assigned an integer value. We use these to calculate the <code>area<\/code> and <code>perimeter<\/code> of the rectangle with the help of expressions.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>area = length * width<\/code><\/p>\r\n<p class=\"import-Normal\"><code>print('Area is', area)<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">We store the result of (assign) the expression <code>length * width<\/code> in the variable <code>area<\/code> and then print it using the <code>print<\/code> function.<\/p>\r\n<\/td>\r\n<\/tr>\r\n<tr class=\"TableGrid-R\">\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\"><code>print('Perimeter is', 2 * (length + width))<\/code><\/p>\r\n<\/td>\r\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\r\n<p class=\"import-Normal\">In this print statement, we directly use the value of the expression<code> 2 * (length + width)<\/code> in the print function. <br style=\"clear: both\" \/>Also, notice how Python prints the output in a readable format even though we have not specified a space between <code>'Area is'<\/code> and the variable area (by adding a comma Python knows to separate the output as individual \u2018words\u2019, like you would in a sentence).<\/p>\r\n<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<h1>Input\/Process\/Output Pattern<\/h1>\r\n<p class=\"import-Normal\">Recall from our previous Unit the example input-process-output diagram for the algorithm we called find_max.<\/p>\r\n\r\n\r\n[caption id=\"attachment_341\" align=\"aligncenter\" width=\"300\"]<img class=\"wp-image-341 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232329\/input-process-output-300x105-1.png\" alt=\"\" width=\"300\" height=\"105\" \/> Figure 20: Input-Process-Output for Finding the Largest Number[\/caption]\r\n<p class=\"import-Normal\">We can represent any solution to a computational problem using this pattern of identifying the input (the data we are given) and then the general process that must be completed to get the desired output.<\/p>\r\n<p class=\"import-Normal\">We have used Python statements invoking the print function to display a string of characters (i.e. the \"message\").<\/p>\r\n\r\n<div class=\"textbox\"><code>print(\"This is a simple Python program\")<\/code><\/div>\r\n<p class=\"import-Normal\">For starters, the built-in function print() will be used to print the output for our programs.<\/p>\r\n\r\n<div class=\"textbox\"><code>name = input(\"Please enter your name: \")<\/code><\/div>\r\n<p class=\"import-Normal\">There is also a built-in function in Python for getting input from the user:<\/p>\r\n<p class=\"import-Normal\">A sample run of this script in IDLE\u2019s Python Shell would pop up a window like this:<\/p>\r\n\r\n\r\n[caption id=\"attachment_125\" align=\"aligncenter\" width=\"537\"]<img class=\"wp-image-125 \" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232331\/image23-1.png\" alt=\"\" width=\"537\" height=\"195\" \/> Figure 21: Using the Built-in Function input()[\/caption]\r\n<p class=\"import-Normal\">The user of the program can enter the name and click OK (the <em>Enter<\/em> key), and when this happens the text that has been entered is returned from the <code>input<\/code> function, and in this case assigned to the variable <code>name<\/code>.<\/p>\r\n<p class=\"import-Normal\">Even if you asked the user to enter their age, you would get back a string like <code>\"17\"<\/code>. It would be your job, as the programmer, to convert that string into an integer or a float value.<\/p>\r\n\r\n<h1>Type Converter Functions<\/h1>\r\n<p class=\"import-Normal\">Here we\u2019ll look at three more built-in Python functions, <code>int()<\/code>, <code>float()<\/code> and <code>str()<\/code>, which will (attempt to) convert their arguments into data types int, float and str respectively. We call these type converter functions.<\/p>\r\n<p class=\"import-Normal\">The int function can take a floating point number or a string, and turn it into an int. For floating point numbers, it discards the decimal portion of the number \u2014 a process we call truncation towards zero on the number line. For example:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; int(3.14)<\/code>\r\n3\r\n<code>&gt;&gt;&gt; int(3.9999)\u00a0 \u00a0 \u00a0# This doesn't round to the closest int!<\/code>\r\n3\r\n<code>&gt;&gt;&gt; int(3.0)<\/code>\r\n3\r\n<code>&gt;&gt;&gt; int(-3.999)\u00a0 \u00a0 \u00a0# Note that the result is closer to zero<\/code>\r\n<code>-3<\/code>\r\n<code>&gt;&gt;&gt; int(minutes \/ 60)<\/code>\r\n<code>10<\/code>\r\n<code>&gt;&gt;&gt; int(\"2345\")\u00a0 \u00a0 \u00a0# Parse a string to produce an int<\/code>\r\n<code>2345<\/code>\r\n<code>&gt;&gt;&gt; int(17)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0# It even works if arg is already an int<\/code>\r\n<code>17<\/code>\r\n<code>&gt;&gt;&gt; int(\"23 bottles\")<\/code><\/div>\r\n<p class=\"import-NormalWeb\">This last type conversion case doesn\u2019t look like a number \u2014 what do we expect?<\/p>\r\n\r\n<div class=\"textbox\"><code><code style=\"color: #ff0000\">Traceback (most recent call last):<\/code>\r\n<code style=\"color: #ff0000\">File \"&lt;interactive input&gt;\", line 1, in &lt;module&gt;&lt;\/code\r\n<code style=\"color: #ff0000\">ValueError: invalid literal for int() with base 10: '23 bottles'<\/code><\/code><\/code><\/div>\r\n<p class=\"import-NormalWeb\">The type converter <span class=\"import-n\">float<span class=\"import-n\">()<\/span> can turn an integer, a float, or a syntactically legal string into a float:<\/span><\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; float(17)<\/code>\r\n<code>17.0<\/code>\r\n<code>&gt;&gt;&gt; float(\"123.45\")<\/code>\r\n<code>123.45<\/code><\/div>\r\n<p class=\"import-NormalWeb\">The type converter <span class=\"import-pre\">str<\/span><span class=\"import-pre\">()<\/span> turns its argument into a string:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; str(17)<\/code>\r\n<code>'17'<\/code>\r\n<code>&gt;&gt;&gt; str(123.45)<\/code>\r\n<code>'123.45'<\/code><\/div>\r\n<p class=\"import-Normal\">If you are not sure what class a value falls into (i.e. unsure whether a value is an integer, a float or a string), Python has a built-in function called type which can tell you.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-Normal\"><code>type('hello')<\/code>\r\n<code>&lt;class 'str'&gt;<\/code>\r\n<code>&gt;&gt;&gt; type(29)<\/code>\r\n<code>&lt;class 'int'&gt;<\/code>\r\n<code>&gt;&gt;&gt; num = 89.32<\/code>\r\n<code>&gt;&gt;&gt; type(num)<\/code>\r\n<code>&lt;class 'float'&gt;<\/code><\/p>\r\n\r\n<\/div>\r\n<h1>Python\u2019s Standard Library<\/h1>\r\n<p class=\"import-Normal\">As mentioned in Unit #1, Python has an extensive Standard Library which is a collection of built-in modules, each providing specific functionality beyond what is included in the \u201ccore\u201d part of Python. A Python <em>module<\/em> is simply a file that contains Python code. The name of the file dictates the name of the module; for example, a file named math.py contains the functions available from the standard math module. We will explore this module (math) here.<\/p>\r\n\r\n<h3>Math Module and Math Functions<\/h3>\r\n<p class=\"import-Normal\">Python has a math module that provides most of the familiar mathematical functions. Before we can use the module, we have to import it:<\/p>\r\n<p class=\"import-Normal\">&gt;&gt;&gt; import math<\/p>\r\n<p class=\"import-Normal\">This statement creates a module object named math. The following is a partial list of the functions which are provided by this module.<\/p>\r\n\r\n<ul>\r\n \t<li><code>math.trunc(x)<\/code>: Returns the float value x truncated to an integer value.<\/li>\r\n \t<li><code>math.sqrt(x)<\/code>: Returns the square root of x.<\/li>\r\n \t<li><code>math.pow(x, y)<\/code>: Returns x raised to the power y.<\/li>\r\n \t<li><code>math.degrees(x)<\/code>: Converts angle x from radians to degrees.<\/li>\r\n \t<li><code>math.radians(x)<\/code>: Converts angle x from degrees to radians.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Many math operations depend on special constants also provided by the math module.<\/p>\r\n\r\n<ul>\r\n \t<li><code>math.pi<\/code>: The mathematical constant <code>\u03c0 = 3.141592<\/code>\u2026.<\/li>\r\n \t<li><code>math.e<\/code>: The mathematical constant <code>e = 2.718281<\/code>\u2026.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">Some examples of using the math module functions (note: these functions require the library name followed by a period followed by the function name):<\/p>\r\n\r\n<div class=\"textbox\"><code>import math<\/code>\r\n<code>math.exp(5)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # returns 148.4131591025766<\/code>\r\n<code>math.e**5\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # returns 148.4131591025765<\/code>\r\n<code>math.sqrt(144)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# returns 12.0<\/code>\r\n<code>math.pow(12.5, 2.8)\u00a0 \u00a0 \u00a0 \u00a0 # returns 1178.5500657314767<\/code>\r\n<code>math.pow(144, 0.5)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0# returns 12.0<\/code>\r\n<code>math.trunc(1.001)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # returns 1math.trunc(1.999) # returns 1<\/code>\r\n<code>12*math.pi**2\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #returns 18.4352528130723<\/code><\/div>\r\n<p class=\"import-Normal\">Additional useful mathematical built-in functions besides <code>float()<\/code> and <code>int()<\/code> include:<\/p>\r\n\r\n<ul>\r\n \t<li><code>abs(x)<\/code>: Returns the absolute value of the number x..<\/li>\r\n \t<li><code>round( x [, n] )<\/code>: returns x rounded to n digits from the decimal point (n is optional). If n is omitted it returns the nearest integer to its input.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">More examples of math functions (no need to import the <code>math<\/code> module with these functions):<\/p>\r\n\r\n<div class=\"textbox\"><code>round(80.23456, 2)<\/code> # returns 80.23\r\n<code>round(100.000056, 3)<\/code> # returns 100.0\r\n<code>abs(-45)<\/code> # returns 45\r\n<code>abs(100.12)<\/code> # returns 100.12<\/div>\r\n<h1>More on Strings<\/h1>\r\n<p class=\"import-Normal\">We have seen how to print strings and how to get a string as input from a user. We also saw how to \u2018add\u2019 strings (concatenate) and \u2018multiply\u2019 strings.<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; word1='fun'<\/code>\r\n<code>&gt;&gt;&gt; word2='times'<\/code>\r\n<code>&gt;&gt;&gt; word1 word2<\/code>\r\n<code>'funtimes'<\/code>\r\n<code>&gt;&gt;&gt; word1*4'<\/code>\r\n<code>funfunfunfun'<\/code><\/div>\r\n<p class=\"import-Normal\">Processing of data includes the manipulation of strings (which is data) to produce something (information) which is meaningful. For example we might be presented with a file of thousands of passwords in use at a privately owned company and we would like to determine which of these passwords are secure and those that are not secure.<\/p>\r\n<p class=\"import-Normal\">Recall that a string is a merely sequence of characters. To determine if an individual password was secure or not we might want to look at the length of the password and the individual characters in the password, looking for characters such as uppercase, numeric, special characters, etc.<\/p>\r\n<p class=\"import-Normal\">Strings are actually a type of sequence; a sequence of individual characters. The indexing operator (Python uses square brackets to enclose the index) selects a single character substring from a string:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; pw = \"abc123\"<\/code>\r\n<code>&gt;&gt;&gt; char1 = pw[1]<\/code>\r\n<code>&gt;&gt;&gt; print(char1)<\/code>\r\n<code>b<\/code><\/div>\r\n<p class=\"import-Normal\">The expression <code>pw[1]<\/code> selects character number 1 from <code>pw<\/code>, and creates a new string containing just this one character. The variable <code>char1<\/code> refers to the result. When we display <code>char1<\/code>, we get the second character in the string <code>pw<\/code>, the letter \u201cb\u201d. Computer scientists always start counting from zero. The letter at subscript position zero of <code>\"abc123\"<\/code> is a. So at position <code>[1]<\/code> we have the letter b.<\/p>\r\n<p class=\"import-Normal\">If we want to access the zero-eth letter of a string, we just place 0, or any expression that evaluates to 0, in between the brackets:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; pw = \"abc123\"<\/code>\r\n<code>&gt;&gt;&gt; char1 = pw[0]<\/code>\r\n<code>&gt;&gt;&gt; print(char1)<\/code>\r\n<code>a<\/code><\/div>\r\n<p class=\"import-Normal\">The expression in brackets is called an index. An index specifies a member of an ordered collection, in this case the collection of characters in the string. The index indicates which one you want, hence the name. It can be any integer expression.<\/p>\r\n<p class=\"import-Normal\">Note that indexing returns a string \u2014 Python has no special type for a single character. It is just a string of length 1.<\/p>\r\n<p class=\"import-Normal\">The string method<code> len()<\/code>, when applied to a string, returns the number of characters in a string:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; pw = \"abc123\"<\/code>\r\n<code>&gt;&gt;&gt; len(pw)<\/code>\r\n<code>6<\/code><\/div>\r\n<p class=\"import-Normal\">At some point, you may need to break a large string (i.g a paragraph) down into smaller chunks, or strings. This is the opposite of concatenation which merges or combines strings into one.<\/p>\r\n<p class=\"import-Normal\">To do this, you use the<code> split()<\/code> method. What it does is split or breakup a string and add the data to a list of separate \u2018words\u2019 using a defined separator.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-Normal\"><code>&gt;&gt;&gt; sentence = \"Python is an interpreted high-level programming language for general-purpose programming.\"<\/code>\r\n<code>&gt;&gt;&gt; sentence.split()<\/code>\r\n<code>['Python', 'is', 'an', 'interpreted', 'high-level', 'programming', 'language', 'for', 'general-purpose', 'programming.']<\/code>\r\n<code>&gt;&gt;&gt; len(sentence.split())<\/code>\r\n<code>10<\/code><\/p>\r\n\r\n<\/div>\r\n<p class=\"import-Normal\">If no separator is defined when you call upon the function, whitespace will be used by default (as seen above). In simpler terms, the separator is a defined character that will be placed between each variable. For example:<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; numbers = \"122,35,09,97,56\"<\/code>\r\n<code>&gt;&gt;&gt; numbers.split(\",\")<\/code>\r\n<code>['122', '35', '09', '97', '56']<\/code>\r\n<code>&gt;&gt;&gt; len(numbers.split(\",\"))<\/code>\r\n<code>5<\/code><\/div>\r\n<p class=\"import-Normal\">The string method <code>lower()<\/code> converts all lowercase characters in a string into uppercase characters and returns it.<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; title=\"The Earth, My Butt, and Other Big Round Things\"<\/code>\r\n<code>&gt;&gt;&gt; title.lower()'the earth, my butt, and other big round things'<\/code>\r\n<code>&gt;&gt;&gt;<\/code><\/div>\r\n<p class=\"import-Normal\">Similarly, The string method <code>upper()<\/code> converts all uppercase characters in a string into lowercase characters and returns it.<\/p>\r\n\r\n<div class=\"textbox\"><code>&gt;&gt;&gt; title=\"Where the wild things are\"<\/code>\r\n<code>&gt;&gt;&gt; title.upper()<\/code>\r\n<code>'WHERE THE WILD THINGS ARE'<\/code>\r\n<code>&gt;&gt;&gt;<\/code><\/div>\r\n<h1>Unit Summary<\/h1>\r\n<ul>\r\n \t<li>Explain the dependencies between hardware and software<\/li>\r\n \t<li>Describe the form and the function of computer programming languages<\/li>\r\n \t<li>Create, modify, and explain computer programs following the input\/process\/output pattern.<\/li>\r\n \t<li>Form valid Python identifiers and expressions.<\/li>\r\n \t<li>Write Python statements to output information to the screen, assign values to variables, and accept information from the keyboard.<\/li>\r\n \t<li>Read and write programs that process numerical data and the Python math module.<\/li>\r\n \t<li>Read and write programs that process textual data using built-in functions and methods.<\/li>\r\n<\/ul>\r\n<p class=\"import-Normal\">From now on, we will assume that you have Python installed on your system. You should now be able to write, save and run Python programs at ease.<\/p>\r\n<p class=\"import-Normal\">Now that you are a Python user, let us learn some more Python concepts.<\/p>\r\n<p class=\"import-Normal\">We have seen how to use operators, operands and expressions - these are the basic building blocks of any program.<\/p>\r\n<p class=\"import-Normal\">Next, we will see how to make use of these in our programs using statements.<\/p>\r\n<p class=\"import-Normal\">We have seen how to use the three control flow statements - if , while and for along with their associated break and continue statements. These are some of the most commonly used parts of Python and hence, becoming comfortable with them is essential.<\/p>\r\n\r\n<h1>Practice Problems<\/h1>\r\n<ol>\r\n \t<li>What is the result of each of the following:&gt;&gt;&gt; \"Python\"[1]\r\na.&gt;&gt;&gt; \"Strings are sequences of characters.\"[5]\r\nb.&gt;&gt;&gt; len(\"wonderful\")\r\nc.&gt;&gt;&gt; \"Mystery\"[:4]\r\nd.&gt;&gt;&gt; \"p\" in \"Pineapple\"\r\ne.&gt;&gt;&gt; \"apple\" in \"Pineapple\"\r\nf.&gt;&gt;&gt; \"pear\" not in \"Pineapple\"\r\ng.&gt;&gt;&gt; \"apple\" &gt; \"pineapple\"\r\nh.&gt;&gt;&gt; \"pineapple\" &lt; \"Peach\"<\/li>\r\n \t<li>Take the sentence: <code>All work and no play makes Jack a dull boy<\/code><span style=\"font-size: 1em\">. Store each word in a separate variable, then print out the sentence on one line using the <\/span><code>print<\/code><span style=\"font-size: 1em\"> function.<\/span><\/li>\r\n \t<li>Add parenthesis to the expression <code style=\"text-align: initial\">6 * 1 - 2<\/code><span style=\"font-size: 1rem;text-align: initial\"> to change its value from <\/span><code style=\"text-align: initial\">4<\/code><span style=\"font-size: 1rem;text-align: initial\"> to <\/span><code style=\"text-align: initial\">-6<\/code><span style=\"font-size: 1rem;text-align: initial\">.<\/span><\/li>\r\n \t<li>The formula for computing the final amount if one is earning compound interest is given on Wikipedia as this formula for compound interest:<img class=\"size-full wp-image-343 aligncenter\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232333\/image24-1.png\" alt=\"\" width=\"396\" height=\"166\" \/>\r\nWrite a Python program that assigns the principal amount of $10000 to variable <code>P<\/code>, assign to n the value 12, and assign to <code>r<\/code><span style=\"text-align: initial;font-size: 1em\"><span style=\"text-align: initial;font-size: 1em\"> the interest rate of 8%. Then have the program prompt the user for the number of years<code> t<\/code> that the money will be compounded for. Calculate and print the final amount after <code>t<\/code> years.<\/span><\/span><\/li>\r\n \t<li>Evaluate the following numerical expressions on paper, then use the Python IDLE Shell to check your results:a.&gt;&gt;&gt; 5 % 2\r\nb.&gt;&gt;&gt; 9 % 5\r\nc.&gt;&gt;&gt; 15 % 12\r\nd.&gt;&gt;&gt; 12 % 15\r\ne.&gt;&gt;&gt; 6 % 6\r\nf.&gt;&gt;&gt; 0 % 7\r\ng.&gt;&gt;&gt; 7 % 0<\/li>\r\n \t<li>You look at the clock and it is exactly 2 pm. You set an alarm to go off in 51 hours. At what time does the alarm go off? (Hint: you could count on your fingers, but this is not what we\u2019re after. If you are tempted to count on your fingers, change the 51 to 5100.)Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and ask for the number of hours to wait. Your program should output (print) what the time will be on the clock when the alarm goes off.<\/li>\r\n \t<li>Write a program <code>find_hypot<\/code> which, given the length of two sides of a right-angled triangle, returns the length of the hypotenuse. (Hint: x ** 0.5 will return the square root.)<\/li>\r\n \t<li>Practice using the Python IDLE Shell as a calculator:\r\n<ol type=\"a\">\r\n \t<li>Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?<\/li>\r\n \t<li>If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?<\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li>Enter the following statement, exactly, into the interactive Shell:<code>printt<span style=\"text-align: initial;font-size: 1em\">('What time is it?')<\/span><\/code>Is this a syntax error or a logic error?<\/li>\r\n \t<li>Suppose that the math module of the Python Standard Library were<span style=\"text-align: initial;font-size: 1em\"> imported. Write the Python statements to calculate the square root of four and print the answer.<\/span><\/li>\r\n \t<li>What is the value of variables <code>num1<\/code> and <code>num2<\/code> after the following Python statements are executed?\r\n<ol type=\"a\">\r\n \t<li style=\"list-style-type: none\"><code>num = 0<\/code><\/li>\r\n \t<li style=\"list-style-type: none\"><code>new = 5<\/code><\/li>\r\n \t<li style=\"list-style-type: none\"><code>num1 = num + new * 2<\/code><\/li>\r\n \t<li style=\"list-style-type: none\"><code>num2 = num +\u00a0 new * 2<\/code><\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li>What is wrong with the following statement that attempts to assign the value ten to variable x?\r\n<ol>\r\n \t<li style=\"list-style-type: none\"><code>10 = x<\/code><\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li>Classify each of the following as either a legal or illegal Python identifier:a. fred\r\nb .if\r\nc. 2x\r\nd.-4\r\ne. sum_total\r\nf. sumTotal\r\ng. sumtotal\r\nh. While\r\ni. x2\r\nj. Private\r\nk. public\r\nl. $16\r\nm. xTwo\r\nn. 10%\r\no. a27834<\/li>\r\n \t<li>How is the value <strong>2.45 x\u00a0<\/strong>10<sup>-5\u00a0<\/sup>expressed as a Python literal?<\/li>\r\n \t<li>Given the following assignment:x = 2: Indicate what each of the following Python statements would print.\r\na. print(\"x\")\r\nb. print('x')\r\nc. print(x)\r\nd. print(\"x + 1\")\r\ne. print('x' + 1)\r\nf.\u00a0 print(x + 1)<\/li>\r\n \t<li>Given the following assignments:\r\ni1 = 2\r\ni2 = 5\r\ni3 = -3\r\nd1 = 2.0\r\nd2 = 5.0\r\nd3 = -0.5<\/li>\r\n \t<li>Evaluate each of the following Python expressions.\r\n<ol>\r\n \t<li>\u00a0i1 + i2<\/li>\r\n \t<li>i1 \/ i2<\/li>\r\n \t<li>i1 \/\/ i2<\/li>\r\n \t<li>i2 \/ i1<\/li>\r\n \t<li>i1 * i3<\/li>\r\n \t<li>d1 + d2<\/li>\r\n \t<li>d1 \/ d2<\/li>\r\n \t<li>d2 \/ d1<\/li>\r\n \t<li>d3 * d1<\/li>\r\n \t<li>d1 + i2<\/li>\r\n \t<li>i1 \/ d2<\/li>\r\n \t<li>d2 \/ i1<\/li>\r\n \t<li>i2 \/ d1<\/li>\r\n \t<li>i1\/i2*d1<\/li>\r\n<\/ol>\r\n<\/li>\r\n \t<li>What is printed by the following statement:\r\n#print(5\/3)<\/li>\r\n \t<li>Consider the following program which contains some errors. You may assume that the comments within the program accurately describe the program\u2019s intended behavior.# Get two numbers from the user\r\nn1 = float(input()) # first number\r\nn2 = float(input()) # second number\r\n# Compute sum of the two numbers\r\nprint(n1 + n2) # third number\r\n# Compute average of the two numbers\r\nprint(n1+ n2\/2) # fourth number\r\n# Assign some variables\r\nd1 = d2 = 0 # fifth number\r\n# Compute a quotient\r\nprint(n1\/d1) # sixth number\r\n# Compute a product\r\nn1*n2 = d1 # seventh number\r\n# Print result\r\nprint(d1) # eighth numberFor each line listed in the comments, indicate whether or not a syntax error, run-time error, or semantic error is present. Not all lines contain an error.<\/li>\r\n \t<li>What is printed by the following code fragment?x1 = 2\r\nx2 = 2\r\nx1 += 1\r\nx2 -= 1\r\nprint(x1)\r\nprint(x2)<\/li>\r\n \t<li>Given the following definitions:\r\nx = 3\r\ny = 5\r\nz = 7evaluate the following Boolean expressions:\r\nx == 3\r\nx &lt; y\r\nx &gt;= y\r\nx &lt;= y\r\nx != y - 2\r\nx &lt; 10\r\nx &gt;= 0 and x &lt; 10\r\nx &lt; 0 and x &lt; 10\r\nx &gt;= 0 and x &lt; 2\r\nx &lt; 0 or x &lt; 10\r\nx &gt; 0 or x &lt; 10\r\nx &lt; 0 or x &gt; 10<\/li>\r\n \t<li>Given the following definitions:x =3\r\ny = 3\r\nb1 = true\r\nb2 = false\r\nx == 3\r\ny &lt; 3\r\nevaluate the following Boolean expressions:<\/li>\r\n<\/ol>\r\n<\/div>\r\n<ol type=\"a\">\r\n \t<li>b2<\/li>\r\n \t<li>not b1<\/li>\r\n \t<li>not b2<\/li>\r\n \t<li>b1 and b2<\/li>\r\n \t<li>b1 or b2<\/li>\r\n \t<li>x<\/li>\r\n \t<li>y<\/li>\r\n \t<li>x or y<\/li>\r\n \t<li>not y<\/li>\r\n<\/ol>","rendered":"<h1>Learning Objectives<\/h1>\n<ul>\n<li>Explain the dependencies between hardware and software<\/li>\n<li>Describe the form and the function of computer programming languages<\/li>\n<li>Create, modify, and explain computer programs following the input\/process\/output pattern.<\/li>\n<li>Form valid Python identifiers and expressions.<\/li>\n<li>Write Python statements to output information to the screen, assign values to variables, and accept information from the keyboard.<\/li>\n<li>Read and write programs that process numerical data and the Python math module.<\/li>\n<li>Read and write programs that process textual data using built-in functions and methods.<\/li>\n<\/ul>\n<h1>Computer Hardware Architecture<\/h1>\n<p>Before we start learning a programming language to give instructions to computers to develop software, we need to learn about how computers are built. If you were to take apart your computer or cell phone and look deep inside, you would find the following parts:<\/p>\n<div style=\"width: 265px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232300\/image12.png\" alt=\"image\" width=\"255\" height=\"152\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 10: Computer Hardware Architecture<\/p>\n<\/div>\n<p class=\"import-Normal\">The high-level definitions of these parts are as follows:<\/p>\n<ul>\n<li class=\"import-Normal\">The <em>Central Processing Unit<\/em> (or CPU) is the part of the computer that is built to be obsessed with &#8220;what is next?&#8221; If your computer is rated at 3.0 Gigahertz, it means that the CPU will ask &#8220;What next?&#8221; three billion times per second.<\/li>\n<li class=\"import-Normal\">The <em style=\"text-align: initial;font-size: 1em\">Main Memory<\/em><span style=\"text-align: initial;font-size: 1em\"> is used to store information that the CPU needs in a hurry. The main memory is nearly as fast as the CPU. But the information stored in the main memory vanishes when the computer is turned off.<\/span><\/li>\n<li class=\"import-Normal\">The <em style=\"text-align: initial;font-size: 1em\">Secondary Memory<\/em><span style=\"text-align: initial;font-size: 1em\"> is also used to store information, but it is much slower than the main memory. The advantage of the secondary memory is that it can store information even when there is no power to the computer. Examples of secondary memory are disk drives or flash memory (typically found in USB sticks and portable music players).<\/span><\/li>\n<li class=\"import-Normal\">The <em style=\"text-align: initial;font-size: 1em\">Input and Output Devices<\/em><span style=\"text-align: initial;font-size: 1em\"> are simply our screen, keyboard, mouse, microphone, speaker, touchpad, etc. They are all of the ways we interact with the computer.<\/span><\/li>\n<li class=\"import-Normal\">These days, most computers also have a <em style=\"text-align: initial;font-size: 1em\">Network Connection<\/em><span style=\"text-align: initial;font-size: 1em\"> to retrieve information over a network. We can think of the network as a very slow place to store and retrieve data that might not always be &#8220;up&#8221;. So in a sense, the network is a slower and at times unreliable form of <\/span><em style=\"text-align: initial;font-size: 1em\">Secondary Memory<\/em><span style=\"text-align: initial;font-size: 1em\">.<\/span><\/li>\n<\/ul>\n<p class=\"import-Normal\">While most of the detail of how these components work is best left to computer builders, it helps to have some terminology so we can talk about these different parts as we write our programs.<\/p>\n<p class=\"import-Normal\">As a programmer, your job is to use and orchestrate each of these resources to solve the problem that you need to solve and analyze the data you get from the solution. As a programmer you will mostly be &#8220;talking&#8221; to the CPU and telling it what to do next. Sometimes you will tell the CPU to use the main memory, secondary memory, network, or the input\/output devices.<\/p>\n<h1>Digital Computing: It\u2019s All about 0\u2019s and 1\u2019s<\/h1>\n<p class=\"import-Normal\">It is essential that computer hardware be reliable and error free. If the hardware gives incorrect results, then any program run on that hardware is unreliable. The key to developing reliable systems is to keep the design as simple as possible<a class=\"footnote\" title=\"Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p9.\" id=\"return-footnote-51-1\" href=\"#footnote-51-1\" aria-label=\"Footnote 1\"><sup class=\"footnote\">[1]<\/sup><\/a>. In digital computing, all information is represented as a series of digits or electronic symbols which are either &#8220;on&#8221; or &#8220;off&#8221; (similar to a light switch). These patterns of electronic symbols are best represented as a sequence of zeroes and ones, digits from the binary (base 2) number system.<\/p>\n<div style=\"width: 354px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232303\/image13.png\" alt=\"image\" width=\"344\" height=\"146\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 11: Digital Representation<\/p>\n<\/div>\n<p>The term bit stands for binary digit. Therefore, every bit has the value 0 or 1. A byte is a group of bits operated on as a single unit in a computer system, usually consisting of eight bits. Although values represented in base 2 are significantly longer than those represented in base 10, binary representation is used in digital computing because of the resulting simplicity of hardware design<a class=\"footnote\" title=\"Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p10\" id=\"return-footnote-51-2\" href=\"#footnote-51-2\" aria-label=\"Footnote 2\"><sup class=\"footnote\">[2]<\/sup><\/a>. For example, the decimal number <code>485<\/code> is represented in binary as <code>111100101<\/code>.<\/p>\n<h1>Operating Systems\u2014Bridging Software and Hardware<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-145 alignright\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232306\/image14-1.png\" alt=\"\" width=\"125\" height=\"185\" \/><\/p>\n<p class=\"import-Normal\">An operating system is software that has the job of managing and interacting with the hardware resources of a computer. Because an operating system is intrinsic to the operation a computer, it is referred to as system software.<\/p>\n<p class=\"import-Normal\">An operating system acts as the \u201cmiddle man\u201d between the hardware and executing application programs (see Figure 12). For example, it controls the allocation of memory for the various programs that may be executing on a computer. Operating systems also provide a particular user interface. Thus, it is the operating system installed on a given computer that determines the \u201clook and feel\u201d of the user interface and how the user interacts with the system, and not the particular model computer. <a class=\"footnote\" title=\"Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, pp.11-12.\" id=\"return-footnote-51-3\" href=\"#footnote-51-3\" aria-label=\"Footnote 3\"><sup class=\"footnote\">[3]<\/sup><\/a><\/p>\n<h1>Software Development Tools<\/h1>\n<p class=\"import-Normal\">It can be very cumbersome, error prone, and time consuming to converse with a computer using only zeros and ones. Numerical machine code (computer code using only zeros and ones) does exist but is rarely used by programmers. For that reason most people program using a &#8220;higher-level&#8221; programming language which use words and symbols that are easier for humans to manage than binary sequences. Tools exist that automatically convert a higher-level description of what is to be done into the required lower-level, machine code. Higher-level programming languages like Python allow programmers to express solutions to programming problems in terms that are much closer to a natural language like English. Some examples of the more popular of the hundreds of higher-level programming languages that have been devised over the past 60 years include FORTRAN, COBOL, Lisp, Haskell, C++, Perl, C , Java, and C#. Most programmers today, especially those concerned with high-level applications, usually do not worry about the details of underlying hardware platform and its machine language.<\/p>\n<div class=\"textbox\">Sample numerical machine code:<br \/>\n<code>001010001110100100101010000001111<\/code><br \/>\n<code>11100110000011101010010101101101<\/code><\/div>\n<p class=\"import-Normal\">Fortunately, higher-level programming languages provide a relatively simple structure with very strict rules for forming statements, called the programming language syntax, which can express a solution to any problem that can be solved by a computer.<\/p>\n<p class=\"import-Normal\">Consider the following program fragment written in the Python programming language:<\/p>\n<p><code>subtotal = 25<\/code><br \/>\n<code>tax = 3<\/code><br \/>\n<code>total = subtotal + tax<\/code><\/p>\n<p class=\"import-Normal\">While these three lines (three statements) do constitute a proper Python program, they are more likely a small piece of a larger program. The lines of text in this program fragment look similar to expressions in algebra. We see no sequence of binary digits. Three words,<code> subtotal, tax, and total<\/code>, called variables, represent information. In programming, a variable represents a value stored in the computer\u2019s memory. Instead of some cryptic binary instructions meant only for the CPU, we see familiar-looking mathematical operators (= and +). Since this program is expressed in the Python language, not machine language, no computer processor (CPU) can execute the program directly. A program called an <strong>interpreter<\/strong> translates the Python code into machine code when a user runs the program. The higher-level language code is called the source code. The corresponding machine language code is called the target code. The interpreter translates the source code into the target machine language.<\/p>\n<p class=\"import-Normal\">The beauty of higher-level languages is this: the same Python source code can execute on different target platforms. The target platform must have a Python interpreter available, but multiple Python interpreters are available for all the major computing platforms. The human programmer therefore is free to think about writing the solution to the problem in Python, not in a specific machine language.<\/p>\n<p class=\"import-Normal\">Programmers have a variety of tools available to enhance the software development process. Some common tools include:<\/p>\n<ul>\n<li><strong>Editors<\/strong>. An editor allows the programmer to enter the program source code and save it to files. Most programming editors increase programmer productivity by using colors to highlight language features. The syntax of a language refers to the way pieces of the language are arranged to make well-formed \u201csentences.\u201d To illustrate, the sentence<\/li>\n<\/ul>\n<p class=\"import-Normal\" style=\"margin-left: 108pt\"><em>The tall boy runs quickly to the door.<\/em><\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">uses proper English syntax. By comparison, the sentence<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 108pt\"><em>Boy the tall runs door to quickly the.<\/em><\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">is not correct syntactically. It uses the same words as the original sentence, but their arrangement does not follow the rules of English.<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">Similarly, programming languages have strict syntax rules that programmers must follow to create well-formed programs. Only well-formed programs are acceptable for translation into executable machine code. Some syntax-aware editors can use colors or other special annotations to alert programmers of syntax errors during the editing process.<\/p>\n<ul>\n<li><strong>Compilers<\/strong>. A compiler translates the source code to target code. The target code may be the machine language for a particular platform or embedded device. The target code could be another source language; for example, the earliest C++ compiler translated C++ into C, another higher-level language. The resulting C++ code was then processed by a C++ compiler to produce an executable program. (C++ compilers today translate C++ directly into machine language.) Compilers translate the contents of a source file and produce a file containing all the target code. Popular compiled languages include C, C++ , Java, C#.<\/li>\n<li><strong>Interpreters<\/strong>. An interpreter is like a compiler, in that it translates higher-level source code into target code (usually machine language). It works differently, however. While a compiler produces an executable program that may run many times with no additional translation needed, an interpreter translates source code statements into machine language each time a user runs the program. A compiled program does not need to be recompiled to run, but an interpreted program must be reinterpreted each time it executes. The interpreter in essence reads the code one line at a time. In general, compiled programs execute more quickly than interpreted programs because the translation activity occurs only once. Interpreted programs, on the other hand, can run as is on any platform with an appropriate interpreter; they do not need to be recompiled to run on a different platform. Python, for example, is used mainly as an interpreted language, but compilers for it are available. Interpreted languages are better suited for dynamic, explorative development which many people feel is ideal for beginning programmers. Popular scripting languages include Python, Ruby, Perl, and, for web browsers, Javascript.<\/li>\n<\/ul>\n<p class=\"import-Normal\" style=\"margin-left: 36pt\">The Python interpreter is written in a high-level language called &#8220;C&#8221;. You can look at the actual source code for the Python interpreter by going to <a class=\"rId60\"><span class=\"import-Hyperlink\">www.python.org<\/span><\/a> and working your way to their source code. So Python is a program itself and it is compiled into machine code. When you install Python on your computer, you copy a machine-code copy of the translated Python program onto your system. In Windows, the executable machine code for Python itself is likely in a file with a name like:<\/p>\n<p><code>C:\\Python35\\python.exe<\/code><\/p>\n<ul>\n<li><strong>Debuggers<\/strong>. A debugger allows a programmer to more easily trace a program\u2019s execution in order to locate and correct errors in the program\u2019s implementation. With a debugger, a developer can simultaneously run a program and see which line in the source code is responsible for the program\u2019s current actions. The programmer can watch the values of variables and other program elements to see if their values change as expected. Debuggers are valuable for locating errors (also called bugs) and repairing programs that contain errors. (See the Debugging Section in this Unit for more information about programming errors.)<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\" style=\"text-align: center\"><strong>COMPILER<\/strong><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\" style=\"text-align: center\"><strong>INTERPRETER<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">Compiler takes an entire program as input. It works on the complete program at once.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">Interpreter takes a single statement at a time as input. It works line by line.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">It generates Intermediate Object code (machine code).<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">It doesn\u2019t generate Intermediate code (machine code).<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">It executes Conditional control statements faster than Interpreter. It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">It executes Conditional control statements much slower than Compiler. In general, the overall execution time is slower.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">More memory required (since Object Code is generated).<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">Memory requirement is less, hence more memory efficient. It doesn&#8217;t generate intermediate Object Code.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">Compiled program doesn&#8217;t need to be compiled every time.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">Every time higher level program is converted into lower level program.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">Errors are displayed after entire program is checked. Hence debugging is comparatively hard.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">Errors are displayed for every instruction interpreted (if any). Continues translating the program until the first error is met, in which case it stops. Hence debugging is easier.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 488px\">\n<p class=\"import-Normal\">Programming languages that use compilers are COBOL, C, C++ .<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;width: 476px\">\n<p class=\"import-Normal\">Programming languages that use interpreter are Visual Basic Script, Ruby, Perl.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: center\"><small>Table 1: Compiler Vs Interpreter<\/small><span class=\"import-FootnoteReference\"><a class=\"footnote\" title=\"Based on information from: https:\/\/www.softwaretestingmaterial.com\/compiler-vs-interpreter\/\" id=\"return-footnote-51-4\" href=\"#footnote-51-4\" aria-label=\"Footnote 4\"><sup class=\"footnote\">[4]<\/sup><\/a><\/span><\/p>\n<p class=\"import-Normal\">Many developers use integrated development environments (IDEs). An IDE includes editors, debuggers, and other programming aids in one comprehensive program. Python IDEs include Wingware, PyCharm,, and IDLE.<\/p>\n<p class=\"import-Normal\">Despite the wide variety of tools (and tool vendors\u2019 claims), the programming process for all but trivial programs is not automatic. Good tools are valuable and certainly increase the productivity of developers, but they cannot write software. There are no substitutes for sound logical thinking, creativity, common sense, and, of course, programming experience.<\/p>\n<h1>Learning Programming with Python<\/h1>\n<p class=\"import-Normal\">Guido van Rossum created the Python programming language in the late 1980s. He named the language after the BBC show &#8220;Monty Python&#8217;s Flying Circus&#8221;. In contrast to other popular languages such as C, C++ , Java, and C#, Python strives to provide a simple but powerful syntax.<\/p>\n<p class=\"import-Normal\">Python is used for software development at companies and organizations such as Google, Yahoo, Facebook, CERN, Industrial Light and Magic, and NASA. It is especially relevant in developing information science applications such as ForecastWatch.com which uses Python to help meteorologists, online travel sites, airline reservation systems, university student record systems, air traffic control systems among many others<a class=\"footnote\" title=\"www.python.org\/about\/success\/\" id=\"return-footnote-51-5\" href=\"#footnote-51-5\" aria-label=\"Footnote 5\"><sup class=\"footnote\">[5]<\/sup><\/a>. Experienced programmers can accomplish great things with Python, but Python\u2019s beauty is that it is accessible to beginning programmers and allows them to tackle interesting problems more quickly than many other, more complex languages that have a steeper learning curve.<\/p>\n<p class=\"import-Normal\">Python has an extensive Standard Library which is a collection of built-in modules, each providing specific functionality beyond what is included in the \u201ccore\u201d part of Python. (For example, the math module provides additional mathematical functions. The random module provides the ability to generate random numbers)<a class=\"footnote\" title=\"Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p23\" id=\"return-footnote-51-6\" href=\"#footnote-51-6\" aria-label=\"Footnote 6\"><sup class=\"footnote\">[6]<\/sup><\/a>. Additionally the Standard Library can help you do various things involving regular expressions, documentation generation, databases, web browsers, CGI, FTP, email, XML, HTML, WAV files, cryptography, GUI (graphical user interfaces), among other things.<\/p>\n<p class=\"import-Normal\">More information about Python, including links to download the latest version for Microsoft Windows, Mac OS X, and Linux, can be found in Appendix A of this book and also at <a class=\"rId61\" href=\"http:\/\/www.python.org\"><span class=\"import-Hyperlink\">http:\/\/www.python.org<\/span><\/a> .<\/p>\n<p class=\"import-Normal\">In late 2008, Python 3.0 was released. Commonly called Python 3, the current version of Python, VERSION 3.0, is incompatible with earlier versions of the language. Many existing books and online resources cover Python 2, but more Python 3 resources now are becoming widely available. The code in this book is based on Python 3.<\/p>\n<p class=\"import-Normal\">This book does not attempt to cover all the facets of the Python programming language. The focus here is on introducing programming techniques and developing good habits and skills. To that end, this approach avoids some of the more obscure features of Python and concentrates on the programming basics that easily transfer directly to other programming languages.<\/p>\n<p class=\"import-Normal\">The goal is to turn you into a person who is skilled in the art of programming. In the end you will be a programmer &#8211; perhaps not a professional programmer, but at least you will have the skills to look at a data\/information analysis problem and develop a program to solve the problem.<\/p>\n<p class=\"import-Normal\">In a sense, you need two skills to be a programmer:<\/p>\n<ul>\n<li>First, you need to know the programming language (Python) &#8211; you need to know the vocabulary and the grammar (the syntax). You need to be able to spell the words in this new language properly and know how to construct well-formed &#8220;sentences&#8221; in this new language.<\/li>\n<li>Second, you need to &#8220;tell a story&#8221;. In writing a story, you combine words and sentences to convey an idea to the reader. There is a skill and art in constructing the story, and skill in story writing is improved by doing some writing (practice) and getting some feedback. In programming, our program is the &#8220;story&#8221; and the problem you are trying to solve is the &#8220;idea&#8221;.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Once you learn one programming language such as Python, you will find it much easier to learn a second programming language such as JavaScript or C++ . Other programming languages have very different vocabulary and grammar (syntax) but the problem-solving skills will be the same across all programming languages.<\/p>\n<p class=\"import-Normal\">You will learn the &#8220;vocabulary&#8221; and &#8220;sentences&#8221; (the syntax) of Python pretty quickly. It will take longer for you to be able to write a coherent program to solve a new problem. We learn programming much like we learn writing. We start by reading and explaining programs, then we write simple programs, and then we write increasingly complex programs over time. At some point you &#8220;get your muse&#8221; and see the patterns on your own and can see more naturally how to take a problem and write a program that solves that computational problem. And once you get to that point, programming becomes a very pleasant and creative process.<\/p>\n<p class=\"import-Normal\">We start with the vocabulary and structure of Python programs. Be patient as the simple examples remind you of when you started reading for the first time.<\/p>\n<h1>Writing a Python Program<\/h1>\n<p class=\"import-Normal\">The text that makes up a Python program has a particular structure. The syntax must be correct, or the interpreter will generate error messages and not execute the program. This section introduces Python by providing a simple example program.<\/p>\n<p class=\"import-Normal\">A program consists of one or more <strong>statements<\/strong>. A statement is an instruction that the interpreter executes.<\/p>\n<p class=\"import-Normal\">The following statement invokes the print function to display a message:<\/p>\n<p><code>print(\"This is a simple Python program\")<\/code><\/p>\n<p class=\"import-Normal\">We can use the statement in a program. Figure 13 (<code>simple.py<\/code>) is an example of a very simple Python program that does something:<\/p>\n<div id=\"attachment_118\" style=\"width: 359px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-118\" class=\"wp-image-118\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232309\/image15-1.png\" alt=\"\" width=\"349\" height=\"129\" \/><\/p>\n<p id=\"caption-attachment-118\" class=\"wp-caption-text\">Figure 13: Listing of simple.py<\/p>\n<\/div>\n<p class=\"import-Normal\">IDLE is Python\u2019s Integrated Development and Learning Environment (IDE) and is included as part of the Python Standard Library which is distributed with Python 3 (See Appendix A). IDLE is the standard Python development environment. Its name is an acronym of &#8220;<strong>I<\/strong>ntegrated <strong>D<\/strong>eve<strong>L<\/strong>opment <strong>E<\/strong>nvironment&#8221;. It works well on both Unix and Windows platforms.<\/p>\n<p class=\"import-Normal\">IDLE has a <strong>Python <\/strong><strong>S<\/strong><strong>hell window<\/strong>, which gives you access to the Python interactive mode. It also has a file editor that lets you create and edit existing Python source files. The file editor was used to write the <code>simple.py<\/code> program.<\/p>\n<p class=\"import-Normal\">The way you launch IDLE depends on your operating system and how it was installed. Figure 13 shows a screenshot of IDLE running on a Windows 8.1 computer. The IDE consists of a simple menu bar at the top. Other Python IDEs are similar in appearance.<\/p>\n<p class=\"import-Normal\">To begin entering our program, we just type in the Python statements. To start a new program, choose the <strong>New<\/strong><strong> File<\/strong> item from the <strong>File<\/strong> menu. This action produces a new editor pane for a file named <code>Untitled<\/code> as shown in Figure 14 below.<\/p>\n<div id=\"attachment_119\" style=\"width: 358px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-119\" class=\"wp-image-119\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232311\/image16-1.png\" alt=\"\" width=\"348\" height=\"111\" \/><\/p>\n<p id=\"caption-attachment-119\" class=\"wp-caption-text\">Figure 14: New File Editor Window<\/p>\n<\/div>\n<p class=\"import-Normal\">We now are ready to type in the code that constitutes the program.<\/p>\n<p><code>print(\"This is a simple Python program\")<\/code><\/p>\n<p class=\"import-Normal\">Next we will save the file. The menu sequence <strong>File<\/strong><strong>Save<\/strong> or <strong>File<\/strong> <strong>Save As<\/strong>, produces the dialog box shown in Figure 15 that allows us to select a folder and filename for our program. You should be sure all Python programs are saved with a <code>.py<\/code> extension. If \u201cSave as Type: Python files\u201d is available there is no need to add the <code>.py<\/code> extension as it will automatically be saved as a <code>.py<\/code> file (see Figure 15).<\/p>\n<div id=\"attachment_120\" style=\"width: 427px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-120\" class=\"wp-image-120\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232314\/image17-1.png\" alt=\"\" width=\"417\" height=\"230\" \/><\/p>\n<p id=\"caption-attachment-120\" class=\"wp-caption-text\">Figure 15: Save Python File, Option 1<\/p>\n<\/div>\n<p>If you are using a different text editor select \u201cSave as Type: All files\u201d and add the <code>.py<\/code> extension (see Figure 16).<\/p>\n<div id=\"attachment_121\" style=\"width: 411px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-121\" class=\"wp-image-121\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232317\/image18-1.png\" alt=\"\" width=\"401\" height=\"222\" \/><\/p>\n<p id=\"caption-attachment-121\" class=\"wp-caption-text\">Figure 16: Save Python File, Option 2<\/p>\n<\/div>\n<p>We can run the program by selecting the menu sequence <strong>Run &#8211;&gt; <\/strong><strong>Run Module<\/strong> or use the shortcut key <strong>F5<\/strong>. A new window labeled <strong>Python Shell<\/strong> will display the program\u2019s output. Figure 17 shows the results of running the program.<\/p>\n<div id=\"attachment_122\" style=\"width: 435px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-122\" class=\"wp-image-122\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232320\/image19-1.png\" alt=\"\" width=\"425\" height=\"160\" \/><\/p>\n<p id=\"caption-attachment-122\" class=\"wp-caption-text\">Figure 17: Program simple.py Output<\/p>\n<\/div>\n<p class=\"import-Normal\">This program contains one Python statement. A statement is a command that the interpreter executes. This statement prints the message <code>This is a simple Python program in the Python Shell window<\/code>. A statement is the fundamental unit of execution in a Python program. Statements may be grouped into larger chunks called blocks, and blocks can make up more complex statements (e.g. the selection structure or the iterative structure we saw in the last Unit). The statement <code>print(\"This is a simple Python program\")<\/code> makes use of a built in function named <code>print<\/code>. Python has a variety of different kinds of statements that we can use to build programs, and the sections that follow explore these various kinds of statements.<\/p>\n<div class=\"textbox\"><strong>Note to Reader:<\/strong> In the context of programming, a function is a named sequence of statements that performs a computation. The name of the function here is <code>print.<\/code> The expression in parentheses is called the <strong>argument<\/strong> of the function. The result, for this function, is the string of characters in quotes (i.e. the \u2018message\u2019) of the <strong>argument<\/strong>.It is common to say that a function \u201ctakes\u201d an argument and \u201creturns\u201d a result. The result is called the return value.<\/div>\n<p class=\"import-Normal\">When you type a statement on the command line in the Shell window and select the Enter key, Python executes it. Statements alone don\u2019t produce any result.<\/p>\n<h1>The Python Interactive Shell<\/h1>\n<p class=\"import-Normal\">We created the program in Figure 13 (simple.py) and submitted it to the Python interpreter for execution. We can interact with the interpreter directly, typing in Python statements and expressions for immediate execution. As we saw in Figure 17, the IDLE window labeled <em>Python Shell<\/em> is where the executing program directs its output. We also can type commands into the Python Shell window, and the interpreter will attempt to execute them. Figure 18 shows how the interpreter responds when we enter the program statement directly into the Shell. The interpreter prompts the user for input with three greater-than symbols (&gt;&gt;&gt;). This means the user typed in the text on the line prefixed with &gt;&gt;&gt;. Any lines without the &gt;&gt;&gt; prefix represent the interpreter\u2019s output, or feedback, to the user. We will find Python\u2019s interactive interpreter invaluable for experimenting with various language constructs.<\/p>\n<div id=\"attachment_123\" style=\"width: 465px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-123\" class=\"wp-image-123\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232322\/image20-1.png\" alt=\"\" width=\"455\" height=\"156\" \/><\/p>\n<p id=\"caption-attachment-123\" class=\"wp-caption-text\">Figure 18: Executing Individual Commands in the Python Shell<\/p>\n<\/div>\n<p>We can discover many things about Python without ever writing a complete program. We can execute the interactive Python interpreter directly from the command line in the Python Shell. The interpreter prompt (&gt;&gt;&gt;) prefixes all user input in the interactive Shell. Lines that do not begin with the &gt;&gt;&gt; prompt represent the interpreter\u2019s response.If you try to enter each line one at a time into the interactive Shell, the program\u2019s output will be intermingled with the statements you type. In this case the best approach is to type the program into an editor, save the code you type to a file, and then execute the program. Most of the time we use an editor to enter and run our Python programs. The interactive interpreter is most useful for experimenting with small snippets of Python code.<\/p>\n<div class=\"textbox\">\n<p><strong>Note to Reader<\/strong>:<\/p>\n<p>The print() function always ends with an invisible &#8220;new line&#8221; character ( \\n ) so that repeated calls to print will all print on a separate line each. To prevent this newline character from being printed, you can specify that it should end with a blank:<br \/>\n<code>print('a', end='')<br \/>\nprint('b', end='')<\/code><br \/>\nOutput is:<br \/>\n<code>ab<\/code><br \/>\nOr you can end with a space:<br \/>\n<code>print('a', end=' ')<br \/>\nprint('b', end=' ')print('c')<\/code><br \/>\nOutput is:<br \/>\n<code>a b<\/code><br \/>\nOr you can end with a space:<br \/>\n<code>print('a', end=' ')<br \/>\nprint('b', end=' ')<br \/>\nprint('c')<\/code><br \/>\nOutput is:<br \/>\n<code>a b c<\/code><\/p>\n<\/div>\n<h1>Debugging<\/h1>\n<div style=\"width: 308px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232324\/image21.jpg\" alt=\"image\" width=\"298\" height=\"234\" \/><\/p>\n<p class=\"wp-caption-text\">Figure 19: First Computer Bug (Image \u00a9Courtesy of the Naval Surface Warfare Center, Dahlgren, VA., 1988. NHHC Collection)<\/p>\n<\/div>\n<p class=\"import-Normal\">Programming is a complex process, and because it is done by human beings, it often leads to errors. Programming errors are called bugs and the process of tracking them down and correcting them is called debugging.<\/p>\n<p class=\"import-Normal\">The story behind this term dates back to September 9, 1947, when Harvard\u2019s Mark II Aiken Relay computer was malfunctioning. After rooting through the massive machine to find the cause of the problem, Admiral Grace Hopper, who worked in the Navy\u2019s engineering program at Harvard, found the bug. It was an actual insect. The incident is recorded in Hopper\u2019s logbook alongside the offending moth, taped to the logbook page: \u201c15:45 Relay #70 Panel F (moth) in relay. First actual case of bug being found.\u201d<a class=\"footnote\" title=\"Jacobson, Molly McBride. &quot;Grace Hopper's Bug.&quot; Atlas Obscura, https:\/\/www.atlasobscura.com\/places\/grace-hoppers-bug . Accessed January 9, 2018.\" id=\"return-footnote-51-7\" href=\"#footnote-51-7\" aria-label=\"Footnote 7\"><sup class=\"footnote\">[7]<\/sup><\/a><\/p>\n<p class=\"import-Normal\">Three kinds of errors can occur in a program: syntax errors, runtime errors, and semantic errors. It is useful to distinguish between them in order to track them down more quickly.<\/p>\n<h3>Syntax errors<\/h3>\n<p>Python can only execute a program if the program is syntactically correct; otherwise, the process fails and returns an error message. Syntax refers to the structure of a program and the rules about that structure. For example, in English, a sentence must begin with a capital letter and end with a period.<\/p>\n<div class=\"textbox\"><strong>this sentence contains a syntax error.<\/strong><br \/>\n<strong>So does this one<\/strong><\/div>\n<p class=\"import-Normal\">For most readers, a few syntax errors are not a significant problem, which is why we can read the poetry of E. E. Cummings without problems. Python is not so forgiving. If there is a single syntax error anywhere in your program, Python will display an error message and quit, and you will not be able to run your program. Early on, you will probably spend a lot of time tracking down syntax errors. As you gain experience, though, you will make fewer errors and find them faster.<\/p>\n<h3>Runtime errors<\/h3>\n<p class=\"import-Normal\">The second type of error is a runtime error, so called because the error does not appear until you run the program. These errors are also called exceptions because they usually indicate that something exceptional (and bad) has happened.<\/p>\n<p class=\"import-Normal\">Runtime errors are rare in the simple programs you will see in the first Units, so it might be a while before you encounter one.<\/p>\n<h3>Semantic errors<\/h3>\n<p class=\"import-Normal\">The third type of error is the semantic error. If there is a semantic error in your program, it will run successfully, in the sense that the computer will not generate any error messages and quit, but it will not do the right thing. It will do something else. Specifically, it will do what you told it to do.<\/p>\n<p class=\"import-Normal\">The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong. Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing. The test cases we generated in UNIT #1 helps programmers fix semantic errors.<\/p>\n<h3>Experimental debugging<\/h3>\n<p class=\"import-Normal\">One of the most important skills you will acquire is debugging. Although it can be frustrating, debugging is one of the most intellectually rich, challenging, and interesting parts of programming.<\/p>\n<p class=\"import-Normal\">In some ways, debugging is like detective work. You are confronted with clues, and you have to infer the processes and events that led to the results you see.<\/p>\n<p class=\"import-Normal\">Debugging is also like an experimental science. Once you have an idea what is going wrong, you modify your program and try again. If your hypothesis was correct, then you can predict the result of the modification, and you take a step closer to a working program. If your hypothesis was wrong, you have to come up with a new one.<\/p>\n<p class=\"import-Normal\">For some people, programming and debugging are the same thing. That is, programming is the process of gradually debugging a program until it does what you want. The idea is that you should start with a program that does something and make small modifications, debugging them as you go, so that you always have a working program.<\/p>\n<h1>The Basics of Python Programming<\/h1>\n<p class=\"import-Normal\">Just printing a single sentence is not enough, is it? You want to do more than that &#8211; you want to take some input, manipulate it and get something out of it. We can achieve this in Python using constants and variables, and we&#8217;ll learn some other concepts as well in this section.<\/p>\n<h3>Comments<\/h3>\n<p class=\"import-Normal\">Comments are any text to the right of the <strong># <\/strong>symbol and is mainly useful as notes for the reader of the program.<\/p>\n<p class=\"import-Normal\">For example:<\/p>\n<div class=\"textbox\" style=\"text-align: left\">\n<p><code>print('hello world') # Note that print is a function<\/code><\/p>\n<p style=\"text-align: center\"><strong>OR<\/strong><\/p>\n<p><code># Note that print is a function<\/code><br \/>\n<code>print('hello world')<\/code><\/p>\n<\/div>\n<p class=\"import-Normal\">Use as many useful comments as you can in your program to:<\/p>\n<ul>\n<li>explain assumptions<\/li>\n<li>explain important decisions<\/li>\n<li>explain important details<\/li>\n<li>explain problems you&#8217;re trying to solve<\/li>\n<li>explain problems you&#8217;re trying to overcome in your program, etc.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Code tells you <strong>how<\/strong>, comments should tell you <strong>why<\/strong>.<\/p>\n<p class=\"import-Normal\">This is useful for readers of your program so that they can easily understand what the program is doing. Consider the programmer, newly hired, who has been assigned the job of maintaining a 2000 line-of-code program. Without comments the codes can be very difficult to understand let alone maintain.<\/p>\n<h3>Literal Constants<\/h3>\n<p class=\"import-Normal\">An example of a literal constant is a number like <code>5<\/code> , <code>1.23<\/code> , or a string like <code>'This is a string'<\/code> or <code>\"It's a string!\"<\/code> (String literals must be in quotes).<\/p>\n<p class=\"import-Normal\">It is called a literal because it is literal &#8211; you use its value literally. The number 2 always represents itself and nothing else &#8211; it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.<\/p>\n<h3>Numbers<\/h3>\n<p class=\"import-Normal\">Numbers are mainly of two types &#8211; integers and floats. An example of an integer is 2 which is just a whole number. Examples of floating point numbers (or floats for short) are <code>3.23<\/code> and <code>7845.322222<\/code>.<\/p>\n<h3>Strings<\/h3>\n<p class=\"import-Normal\">A string is a sequence of characters. Strings can be a single character, a single word or a bunch of words. You will be using strings in many Python programs that you write. Note the following:<\/p>\n<p class=\"import-Normal\">Single Quote: You can specify (define) strings using single quotes such as <code>'Quote me on this'<\/code> . All white space i.e. spaces and tabs, within the quotes, are preserved as-is.<\/p>\n<p class=\"import-Normal\">Double Quotes: Strings in double quotes work exactly the same way as strings in single quotes. An example is <code>\"What's your name?\"<\/code> .There is no difference in using single quotes or double quotes , just be sure to use a matching set.<\/p>\n<p class=\"import-Normal\">Triple Quotes: You can specify multi-line strings using triple quotes &#8211; ( &#8220;&#8221;&#8221; or &#8221;&#8217; ). You can use single quotes and double quotes freely within the triple quotes. An example is:<\/p>\n<div class=\"textbox\"><code>'''This is a multi-line string. This is the first line.<br \/>\nThis is the second line.<br \/>\n\"What's your name?,\" I asked.<br \/>\nHe said \"Bond, James Bond.\"<br \/>\n'''<\/code><\/div>\n<p class=\"import-Normal\">Strings are immutable. This means that once you have created a string, you cannot change it. Although this might seem like a bad thing, it really isn&#8217;t. We will see why this is not a limitation in the various programs that we see later on.<\/p>\n<h3>Variables<\/h3>\n<p class=\"import-Normal\">Using just literal constants can soon become boring &#8211; we need some way of storing any information and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies &#8211; their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer&#8217;s memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.<\/p>\n<p class=\"import-Normal\">One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.<\/p>\n<p><code>&gt;&gt;&gt; message = \"What is today\u2019s date?\"<br \/>\n&gt;&gt;&gt; n = 17<br \/>\n&gt;&gt;&gt; pi = 3.14159<\/code><\/p>\n<p class=\"import-Normal\">The <strong>assignment statement<\/strong> gives a value to a variable:<\/p>\n<p class=\"import-Normal\">This example makes three assignments. The first assigns the string value <code>\"What is today\u2019s date?\"<\/code> to a variable named message. The second assigns the integer 17 to n, and the third assigns the floating-point number 3.14159 to a variable called <code>pi.<\/code><\/p>\n<p class=\"import-Normal\">The <strong>assignment <\/strong><strong>symbol<\/strong>, =, should not be confused with equals, which uses the symbol ==. The assignment statement binds a name, on the left-hand side of the operator, to a value, on the right-hand side. This is why you will get an error if you enter:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; 17 = n<br \/>\nFile \"&lt;interactive input&gt;\", line 1<br \/>\nSyntaxError: can't assign to literal<\/code><\/div>\n<div class=\"textbox\">Tip: When reading or writing code, say to yourself \u201cn is assigned 17\u201d or \u201cn gets the value 17\u201d. Don\u2019t say \u201cn equals 17\u201d.<\/div>\n<p class=\"import-Normal\">A common way to represent variables on paper is to write the name with an arrow pointing to the variable\u2019s value. This kind of diagram is called a state snapshot because it shows what state each of the variables is in at a particular instant in time. (Think of it as the variable\u2019s state of mind). The following diagram shows the result of executing the assignment statements:<\/p>\n<div class=\"textbox shaded\">message\u00a0\u2192 &#8220;What is today\u2019s date?&#8221;<br \/>\nn \u2192 17<br \/>\npi \u2192 3.14159<\/div>\n<div>If you ask the interpreter to evaluate a variable in the Python Shell, it will produce the value that is currently linked to the variable:<\/div>\n<div class=\"textbox\"><code>&gt;&gt;&gt; message<br \/>\n\u2018What is today\u2019s date?\u2019<br \/>\n&gt;&gt;&gt; n<br \/>\n17<br \/>\n&gt;&gt;&gt; pi<br \/>\n3.14159<\/code><\/div>\n<p class=\"import-Normal\">We use variables in a program to \u201cremember\u201d things, perhaps the current score at the football game. But variables are variable. This means they can change over time, just like the scoreboard at a football game. You can assign a value to a variable, and later assign a different value to the same variable. (This is different from mathematics. In mathematics, if you give `x` the value 3, it cannot change to link to a different value half-way through your calculations!). For example:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; day = \"Thursday\"<br \/>\n&gt;&gt;&gt; day<br \/>\n'Thursday'<br \/>\n&gt;&gt;&gt; day = \"Friday\"<br \/>\n&gt;&gt;&gt; day<br \/>\n'Friday'<br \/>\n&gt;&gt;&gt; day = 21<br \/>\n&gt;&gt;&gt; day<br \/>\n21<\/code><\/div>\n<p class=\"import-Normal\">You\u2019ll notice we changed the value of day three times, and on the third assignment we even made it refer to a value that was of a different data type.<\/p>\n<p class=\"import-Normal\">A great deal of programming is about having the computer remember things, e.g. The number of missed calls on your phone, and then arranging to update or change the variable when you miss another call.<\/p>\n<div class=\"textbox\">\n<p><strong>Note to Reader:<\/strong><\/p>\n<p>There are two common variable genres that are used in computer programming. They are so regularly used that they have special names.<\/p>\n<p><strong>accumulator<\/strong>: A variable used in a loop to add up or accumulate a result.<\/p>\n<p><strong>counter<\/strong>: A variable used to count something, usually initialized to zero and then incremented.<\/p>\n<\/div>\n<h3>Identifier Naming<\/h3>\n<p class=\"import-Normal\">Variables are examples of identifiers. Identifiers are names given to identify something. There are some rules you have to follow for naming identifiers:<\/p>\n<ul>\n<li>The first character of the identifier must be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore ( _ ).<\/li>\n<li>The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores ( _ ) or digits (0-9).<\/li>\n<li>Identifier names are case-sensitive. For example, myname and myName are not the same. Note the lowercase n in the former and the uppercase N in the latter.<\/li>\n<li>Examples of valid identifier names are <code>i<\/code> , <code>name_2_3<\/code> . Examples of invalid identifier names are <code>2things<\/code> , <code>this is spaced out<\/code> , <code>my-name<\/code> and <code>&gt;a1b2_c3<\/code><\/li>\n<\/ul>\n<p class=\"import-Normal\">Python keywords define the language\u2019s syntax rules and structure, and they cannot be used as variable names.<\/p>\n<p class=\"import-Normal\">Python has thirty-something keywords (and every now and again improvements to Python introduce or eliminate one or two):<\/p>\n<table style=\"height: 184px\">\n<tbody>\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\n<p class=\"import-Normal\"><code>and<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\n<p class=\"import-Normal\"><code>as<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\n<p class=\"import-Normal\"><code>assert<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\n<p class=\"import-Normal\"><code>break<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\n<p class=\"import-Normal\"><code>class<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\n<p class=\"import-Normal\"><code>continue<\/code><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\n<p class=\"import-Normal\"><code>def<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\n<p class=\"import-Normal\"><code>del<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\n<p class=\"import-Normal\"><code>elif<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\n<p class=\"import-Normal\"><code>else<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\n<p class=\"import-Normal\"><code>except<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\n<p class=\"import-Normal\"><code>exec<\/code><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\n<p class=\"import-Normal\"><code>finally<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\n<p class=\"import-Normal\"><code>for<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\n<p class=\"import-Normal\"><code>from<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\n<p class=\"import-Normal\"><code>global<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\n<p class=\"import-Normal\"><code>if<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\n<p class=\"import-Normal\"><code>import<\/code><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\n<p class=\"import-Normal\"><code>in<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\n<p class=\"import-Normal\"><code>is<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\n<p class=\"import-Normal\"><code>lambda<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\n<p class=\"import-Normal\"><code>nonlocal<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\n<p class=\"import-Normal\"><code>not<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\n<p class=\"import-Normal\"><code>or<\/code><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\n<p class=\"import-Normal\"><code>pass<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\n<p class=\"import-Normal\"><code>raise<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\n<p class=\"import-Normal\"><code>return<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\n<p class=\"import-Normal\"><code>try<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\n<p class=\"import-Normal\"><code>while<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\n<p class=\"import-Normal\"><code>with<\/code><\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\" style=\"height: 29px\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 40.625px\">\n<p class=\"import-Normal\"><code>yield<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 32.625px\">\n<p class=\"import-Normal\"><code>True<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 49.625px\">\n<p class=\"import-Normal\"><code>False<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 57.625px\">\n<p class=\"import-Normal\"><code>None<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 45.625px\">\n<p class=\"import-Normal\">\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt;border: 0.5pt solid windowtext;height: 29px;width: 59.625px\">\n<p class=\"import-Normal\">\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><small>Table 2: Python Keywords<\/small><\/p>\n<p class=\"import-Normal\">You might want to keep this table handy. If the interpreter complains about one of your variable names and you don\u2019t know why, see if it is in this table.<\/p>\n<p class=\"import-Normal\">Programmers generally choose names for their variables that are meaningful to the human readers of the program \u2014 they help the programmer document, or remember, what the variable is used for.<\/p>\n<div class=\"textbox\"><strong>Note to Reader<\/strong>:<br \/>\nBeginners sometimes confuse \u201cmeaningful to the human readers\u201d with \u201cmeaningful to the computer\u201d. So they\u2019ll wrongly think that because they\u2019ve called some variable <code>average<\/code> or <code>pi<\/code>, it will somehow magically calculate an average, or magically know that the variable pi should have a value like 3.14159. No! The computer doesn\u2019t understand what you intend the variable to mean.So you\u2019ll find some textbooks or tutorials that deliberately don\u2019t choose meaningful names when they teach beginners \u2014 not because we don\u2019t think it is a good habit, but because we\u2019re trying to reinforce the message that you \u2014 the programmer \u2014 must write the program code to calculate the average, and you must write an assignment statement to give the variable <code>pi<\/code> the value you want it to have.<\/div>\n<h3>Indentation<\/h3>\n<p class=\"import-Normal\">Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements.<\/p>\n<p class=\"import-Normal\">This means that statements which go together must have the same indentation. Each such set of statements is called a block. We will see examples of how blocks are important in later sections and Units.<\/p>\n<p class=\"import-Normal\">One thing you should remember is that wrong indentation can give rise to errors. For example:<\/p>\n<div class=\"textbox\"><code>i = 5<br \/>\n# Error below! Notice a single space at the start of the line print('Value is', i)<br \/>\nprint('I repeat, the value is', i)<\/code><\/div>\n<p class=\"import-Normal\">When you run this, you get the following error:<\/p>\n<div class=\"textbox\"><code>File \"whitespace.py\", line 3<br \/>\nprint('Value is', i)<\/code><br \/>\n^<br \/>\n<code style=\"color: #ff0000\">IndentationError: unexpected indent<\/code><\/div>\n<p class=\"import-Normal\">Notice that there is a single space at the beginning of the second line. The error indicated by Python tells us that the syntax of the program is invalid i.e. the program was not properly written. What this means to you is that you cannot arbitrarily start new blocks of statements (except for the default main block which you have been using all along, of course). Cases where you can use new blocks (such as the iteration control structure) will be detailed in later Units.<\/p>\n<div class=\"textbox\"><strong>How to indent:<\/strong> Use four spaces for indentation. This is the official Python language recommendation. Good editors (including IDLE) will automatically do this for you. Make sure you use a consistent number of spaces for indentation, otherwise your program will not run or will have unexpected behavior.<\/div>\n<h1>Example: Using Variables and Literal Constants<\/h1>\n<p class=\"import-Normal\">Practice: Type, save and run the following program,var.py, using the Python editor.<\/p>\n<div class=\"textbox\"><code># Filename : var.py<br \/>\ni = 5<br \/>\nprint(i)<br \/>\ni = i + 1<br \/>\nprint(i)<br \/>\ns = '''This is a multi-line string.<br \/>\nThis is the second line.'''<br \/>\nprint(s)<\/code><\/div>\n<p>Output:<\/p>\n<div class=\"textbox\"><code>5<br \/>\n6<br \/>\nThis is a multi-line string.<br \/>\nThis is the second line.<\/code><\/div>\n<p class=\"import-Normal\">Let us examine how this program works.<\/p>\n<table>\n<tbody>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\" style=\"text-align: center\">Python Statement<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\" style=\"text-align: center\">Explanation<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>i = 5<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">First, we assign the literal constant value 5 to the variable i using the assignment operator ( = ). This line is called a statement because it states that something should be done and in this case, we connect the variable name i to the value 5.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>print(i)<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Next, we print the value of i using the print statement which, unsurprisingly, just prints the value of the variable to the screen<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>i = i + 1<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Here we add 1 to the value stored in i and store it back into i.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>print(i)<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">We then print it and expectedly, we get the value 6 .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>s = '''This is a multi-line string.<br style=\"clear: both\" \/>This is the second line.'''<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Here we assign the literal string constant to the variable s.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\"><code>print(s)<\/code><\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">We then print it.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Operators and Expressions<\/h1>\n<p class=\"import-Normal\">Most statements (logical lines) that you write will contain expressions. A simple example of an expression is <code>2+3<\/code>. An expression can be broken down into operators and operands.<\/p>\n<p class=\"import-Normal\">Operators are functionality that do something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data is called operands. In this case, <code>2<\/code> and<code> 3<\/code> are the operands.<\/p>\n<p class=\"import-Normal\">When a variable name appears in the place of an operand, it is replaced with its value before the operation is performed.<\/p>\n<h1>Operators<\/h1>\n<p class=\"import-Normal\">We will briefly take a look at the operators and their usage.<\/p>\n<p class=\"import-Normal\">Note that you can evaluate the expressions given in the examples using the interpreter interactively. For example, to test the expression 2+3 , use the interactive Python interpreter prompt in the Shell window:<\/p>\n<div class=\"textbox\" style=\"padding: 0pt 5.4pt 0pt 12pt\">\n<p class=\"import-Normal\"><code>&gt;&gt;&gt; 2 + 3<br \/>\n5<\/code><br \/>\n<code>&gt;&gt; 3 * 5<br \/>\n<\/code><code>15<br \/>\n<\/code><code>&gt;&gt;&gt;<\/code><\/p>\n<\/div>\n<p class=\"import-Normal\">Here is a quick overview of the available operators:<\/p>\n<table>\n<tbody>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">+ (plus)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Adds two objects<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">3 + 5 gives 8 . <br style=\"clear: both\" \/>&#8216;a&#8217; + &#8216;b&#8217; gives &#8216;ab&#8217; .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">&#8211; (minus)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Gives the subtraction of one number from the other; if the first operand is absent it is assumed to be zero.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">-5.2 gives a negative number <br style=\"clear: both\" \/>50 &#8211; 24 gives 26 .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">* (multiply)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Gives the multiplication of the two numbers or returns the string repeated that many times.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">2 * 3 gives 6 .<br style=\"clear: both\" \/>&#8216;la&#8217; * 3 gives &#8216;lalala&#8217; .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">** (power)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Returns x to the power of y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">\/ (divide)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Divide x by y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">13 \/ 3 gives 4.333333333333333<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">\/\/ (divide and floor)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Divide x by y and round the answer down to the nearest whole number<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">13 \/\/ 3 gives 4<br style=\"clear: both\" \/>-13 \/\/ 3 gives -5<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">% (modulo)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Returns the remainder of the division<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">13 % 3 gives 1 .<br style=\"clear: both\" \/>-25.5 % 2.25 gives 1.5 .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">&lt; (less than)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Returns whether x is less than y. All comparison operators return True or False . <br style=\"clear: both\" \/>Note the capitalization of these names.<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">5 &lt; 3 gives False <br style=\"clear: both\" \/>3 &lt; 5 gives True .<br style=\"clear: both\" \/>Comparisons can be chained arbitrarily: <br style=\"clear: both\" \/>3 &lt; 5 &lt; 7 gives True .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">&gt; (greater than)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Returns whether x is greater than y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">5 &gt; 3 returns True <br style=\"clear: both\" \/>If both operands are numbers, they are first converted to a common type. Otherwise, it always returns False .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">&lt;= (less than or equal to)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Returns whether x is less than or equal to y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = 3;<br style=\"clear: both\" \/>y = 6; <br style=\"clear: both\" \/>x &lt;= y returns True<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">&gt;= (greater than or equal to)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Returns whether x is greater than or equal to y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = 4; <br style=\"clear: both\" \/>y = 3; <br style=\"clear: both\" \/>x &gt;= 3 returns True<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">== (equal to)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Compares if the objects are equal<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = 2; y = 2; x == y returns True<br style=\"clear: both\" \/>x = &#8216;str&#8217;; y = &#8216;stR&#8217;; x == y returns False<br style=\"clear: both\" \/>x = &#8216;str&#8217;; y = &#8216;str&#8217;; x == y returns True<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">!= (not equal to)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">Compares if the objects are not equal<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = 2; <br style=\"clear: both\" \/>y = 3; <br style=\"clear: both\" \/>x != y returns True<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">not (boolean NOT)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">If x is True , it returns False . If x is False , it returns True .<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = True; <br style=\"clear: both\" \/>not x returns False .<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">and (boolean AND)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x and y returns False if x is False , else it returns evaluation of y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = False; y = True; <br style=\"clear: both\" \/>x and y returns False since x is False. <br style=\"clear: both\" \/>In this case, Python will not evaluate y since it knows that the left hand side of the &#8216;and&#8217; expression is False which implies that the whole expression will be False irrespective of the other values. <br style=\"clear: both\" \/>This is called short-circuit evaluation.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">or (boolean OR)<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">If x is True , it returns True, else it returns evaluation of y<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">x = True; y = False; <br style=\"clear: both\" \/>x or y returns True . <br style=\"clear: both\" \/>Short-circuit evaluation applies here as well.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: center\"><small><em>Table 3: Python Operators<\/em><\/small><\/p>\n<p class=\"import-Normal\">Some of the comparison operators work on strings. For example the + operator (plus) works with strings, but it is not addition in the mathematical sense. Instead it performs <strong>concatenation<\/strong>, which means joining the strings by linking them end to end.<\/p>\n<p class=\"import-Normal\">The * operator also works on strings; it performs repetition. For example,<code>'Fun'*3<\/code> is\u00a0<code>'FunFunFun'<\/code>. One of the operands has to be a string; the other has to be an integer.<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; first = 10<br \/>\n&gt;&gt;&gt; second = 15<br \/>\n&gt;&gt;&gt; print(first + second)<br \/>\n25<br \/>\n&gt;&gt;&gt; first = '100'<br \/>\n&gt;&gt;&gt; second = '150'<br \/>\n&gt;&gt;&gt; print(first + second)<br \/>\n100150<\/code><\/div>\n<p>Python does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters, so: the word, Zebra, comes before apple. A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison.<\/p>\n<h1>Practice with Operators &amp; Expressions<\/h1>\n<h3 class=\"import-Normal\"><span class=\"import-Heading3Char\">Practice #1<\/span><\/h3>\n<p class=\"import-Normal\">Use the Python IDLE Shell to calculate:<\/p>\n<ol>\n<li>6 + 4*10<\/li>\n<li>(6 + 4)*10 (Compare this to the previous expression, and note that Python uses parentheses just like you would in normal math to determine order of operations!)<\/li>\n<li>23.0 to the 5th power<\/li>\n<li>Positive root of the following equation: 34*x^2 + 68*x \u2013 510<br style=\"clear: both\" \/>Recall:<br style=\"clear: both\" \/>a*x^2+b*x+c<br style=\"clear: both\" \/>x1 = ( &#8211; b + sqrt ( b*b &#8211; 4*a*c ) ) \/ ( 2*a)<\/li>\n<\/ol>\n<h3 class=\"import-Normal\"><span class=\"import-Heading3Char\">Practic<\/span><span class=\"import-Heading3Char\">e #2<\/span><\/h3>\n<p class=\"import-Normal\">So now let us convert 645 minutes into hours. Use IDLE\u2019s Python Shell to type in the following:<\/p>\n<p><code>&gt;&gt;&gt; minutes = 645<br \/>\n&gt;&gt;&gt; hours = minutes \/ 60<br \/>\n&gt;&gt;&gt; hours<\/code><\/p>\n<p class=\"import-Normal\">Oops! The output gives us 10.75 which is not what we expected. In Python 3, the division operator \/ always yields a floating point result. What we might have wanted to know was how many whole hours there are, and how many minutes remain. Python gives us two different flavors of the division operator. The second, called floor division uses the token \/\/. Its result is always a whole number \u2014 and if it has to adjust the number it always moves it to the left on the number line. So 6 \/\/ 4 yields 1, but -6 \/\/ 4 might surprise you!<\/p>\n<h3><span class=\"import-Heading3Char\">Practice<\/span><span class=\"import-Heading3Char\">#3<\/span><\/h3>\n<p>Try this:<\/p>\n<div class=\"textbox\"><code>\u00a0&gt;&gt;&gt; 7 \/ 4<br \/>\n1.75<br \/>\n&gt;&gt;&gt; 7 \/\/ 4<br \/>\n1<br \/>\n&gt;&gt;&gt; minutes = 645<br \/>\n&gt;&gt;&gt; hours = minutes \/\/ 60<br \/>\n&gt;&gt;&gt; hours<br \/>\n10\u00a0<\/code><\/div>\n<p class=\"import-Normal\">Take care that you choose the correct flavor of the division operator. If you\u2019re working with expressions where you need floating point values, use the division operator that does the division accurately.<\/p>\n<h1>Evaluation Order<\/h1>\n<p class=\"import-Normal\">If you had an expression such as <code>2 + 3 * 4<\/code> , is the addition done first or the multiplication?<\/p>\n<p class=\"import-Normal\">Recall from Algebra PEMDAS (parenthesis, exponents, multiplication, division, addition, subtraction).This tells us that the multiplication should be done first and that the multiplication operator has higher precedence than the addition operator.<\/p>\n<p class=\"import-Normal\">It is far better to use parentheses to group operators and operands appropriately in order to explicitly specify the precedence. This makes the program more readable. For example, <code>2 + (3 * 4)<\/code> is definitely easier to read than<code> 2 + 3 * 4<\/code> which requires knowledge of the operator precedence\u2019s.<\/p>\n<p class=\"import-Normal\">There is an additional advantage to using parentheses &#8211; it helps us to change the order of evaluation. For example, if you want addition to be evaluated before multiplication in an expression, then you can write something like <code>(2 + 3) * 4<\/code> .<\/p>\n<h3 class=\"import-Normal\"><span class=\"import-Heading3Char\">Practic<\/span><span class=\"import-Heading3Char\">e #4 <\/span><\/h3>\n<p class=\"import-Normal\">Use the Python editor to enter the following code (save as <code>expression.py<\/code> ):<\/p>\n<div class=\"textbox\"><code># given the length and width calculate the area and the<\/code><br \/>\n<code># perimeter of a rectangle<\/code><br \/>\n<code>length = 5<\/code><br \/>\n<code>width = 2<\/code><br \/>\n<code>area = length * width<\/code><br \/>\n<code>print('Area is', area)<\/code><br \/>\n<code>print('Perimeter is', 2 * (length + width))<\/code><\/div>\n<p class=\"import-Normal\">Output:<\/p>\n<div class=\"aligncenter wp-caption\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-caption wp-image-124\" style=\"width: 395px\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232327\/image22-1-1.png\" alt=\"\" width=\"395\" height=\"201\" \/><\/div>\n<div class=\"unit-2:-writing-simple-programs.\">\n<div class=\"aligncenter wp-caption\"><\/div>\n<p class=\"import-Normal\">Let us examine how this program works.<\/p>\n<table>\n<tbody>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\" style=\"text-align: center\">Python Statement<\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\" style=\"text-align: center\">Explanation<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>length = 5<\/code><\/p>\n<p class=\"import-Normal\"><code>width = 2<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">The <code>length<\/code> and <code>width<\/code> of the rectangle are stored in variables by the same name. They are each assigned an integer value. We use these to calculate the <code>area<\/code> and <code>perimeter<\/code> of the rectangle with the help of expressions.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>area = length * width<\/code><\/p>\n<p class=\"import-Normal\"><code>print('Area is', area)<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">We store the result of (assign) the expression <code>length * width<\/code> in the variable <code>area<\/code> and then print it using the <code>print<\/code> function.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"TableGrid-R\">\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\"><code>print('Perimeter is', 2 * (length + width))<\/code><\/p>\n<\/td>\n<td class=\"TableGrid-C\" style=\"vertical-align: middle;padding: 0pt 5.4pt 0pt 5.4pt;border: solid windowtext 0.5pt\">\n<p class=\"import-Normal\">In this print statement, we directly use the value of the expression<code> 2 * (length + width)<\/code> in the print function. <br style=\"clear: both\" \/>Also, notice how Python prints the output in a readable format even though we have not specified a space between <code>'Area is'<\/code> and the variable area (by adding a comma Python knows to separate the output as individual \u2018words\u2019, like you would in a sentence).<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>Input\/Process\/Output Pattern<\/h1>\n<p class=\"import-Normal\">Recall from our previous Unit the example input-process-output diagram for the algorithm we called find_max.<\/p>\n<div id=\"attachment_341\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-341\" class=\"wp-image-341 size-full\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232329\/input-process-output-300x105-1.png\" alt=\"\" width=\"300\" height=\"105\" \/><\/p>\n<p id=\"caption-attachment-341\" class=\"wp-caption-text\">Figure 20: Input-Process-Output for Finding the Largest Number<\/p>\n<\/div>\n<p class=\"import-Normal\">We can represent any solution to a computational problem using this pattern of identifying the input (the data we are given) and then the general process that must be completed to get the desired output.<\/p>\n<p class=\"import-Normal\">We have used Python statements invoking the print function to display a string of characters (i.e. the &#8220;message&#8221;).<\/p>\n<div class=\"textbox\"><code>print(\"This is a simple Python program\")<\/code><\/div>\n<p class=\"import-Normal\">For starters, the built-in function print() will be used to print the output for our programs.<\/p>\n<div class=\"textbox\"><code>name = input(\"Please enter your name: \")<\/code><\/div>\n<p class=\"import-Normal\">There is also a built-in function in Python for getting input from the user:<\/p>\n<p class=\"import-Normal\">A sample run of this script in IDLE\u2019s Python Shell would pop up a window like this:<\/p>\n<div id=\"attachment_125\" style=\"width: 547px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-125\" class=\"wp-image-125\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232331\/image23-1.png\" alt=\"\" width=\"537\" height=\"195\" \/><\/p>\n<p id=\"caption-attachment-125\" class=\"wp-caption-text\">Figure 21: Using the Built-in Function input()<\/p>\n<\/div>\n<p class=\"import-Normal\">The user of the program can enter the name and click OK (the <em>Enter<\/em> key), and when this happens the text that has been entered is returned from the <code>input<\/code> function, and in this case assigned to the variable <code>name<\/code>.<\/p>\n<p class=\"import-Normal\">Even if you asked the user to enter their age, you would get back a string like <code>\"17\"<\/code>. It would be your job, as the programmer, to convert that string into an integer or a float value.<\/p>\n<h1>Type Converter Functions<\/h1>\n<p class=\"import-Normal\">Here we\u2019ll look at three more built-in Python functions, <code>int()<\/code>, <code>float()<\/code> and <code>str()<\/code>, which will (attempt to) convert their arguments into data types int, float and str respectively. We call these type converter functions.<\/p>\n<p class=\"import-Normal\">The int function can take a floating point number or a string, and turn it into an int. For floating point numbers, it discards the decimal portion of the number \u2014 a process we call truncation towards zero on the number line. For example:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; int(3.14)<\/code><br \/>\n3<br \/>\n<code>&gt;&gt;&gt; int(3.9999)\u00a0 \u00a0 \u00a0# This doesn't round to the closest int!<\/code><br \/>\n3<br \/>\n<code>&gt;&gt;&gt; int(3.0)<\/code><br \/>\n3<br \/>\n<code>&gt;&gt;&gt; int(-3.999)\u00a0 \u00a0 \u00a0# Note that the result is closer to zero<\/code><br \/>\n<code>-3<\/code><br \/>\n<code>&gt;&gt;&gt; int(minutes \/ 60)<\/code><br \/>\n<code>10<\/code><br \/>\n<code>&gt;&gt;&gt; int(\"2345\")\u00a0 \u00a0 \u00a0# Parse a string to produce an int<\/code><br \/>\n<code>2345<\/code><br \/>\n<code>&gt;&gt;&gt; int(17)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0# It even works if arg is already an int<\/code><br \/>\n<code>17<\/code><br \/>\n<code>&gt;&gt;&gt; int(\"23 bottles\")<\/code><\/div>\n<p class=\"import-NormalWeb\">This last type conversion case doesn\u2019t look like a number \u2014 what do we expect?<\/p>\n<div class=\"textbox\"><code><code style=\"color: #ff0000\">Traceback (most recent call last):<\/code><br \/>\n<code style=\"color: #ff0000\">File \"&lt;interactive input&gt;\", line 1, in &lt;module&gt;&lt;\/code<br \/>\n<code style=\"color: #ff0000\">ValueError: invalid literal for int() with base 10: '23 bottles'<\/code><\/code><\/code><\/div>\n<p class=\"import-NormalWeb\">The type converter <span class=\"import-n\">float<span class=\"import-n\">()<\/span> can turn an integer, a float, or a syntactically legal string into a float:<\/span><\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; float(17)<\/code><br \/>\n<code>17.0<\/code><br \/>\n<code>&gt;&gt;&gt; float(\"123.45\")<\/code><br \/>\n<code>123.45<\/code><\/div>\n<p class=\"import-NormalWeb\">The type converter <span class=\"import-pre\">str<\/span><span class=\"import-pre\">()<\/span> turns its argument into a string:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; str(17)<\/code><br \/>\n<code>'17'<\/code><br \/>\n<code>&gt;&gt;&gt; str(123.45)<\/code><br \/>\n<code>'123.45'<\/code><\/div>\n<p class=\"import-Normal\">If you are not sure what class a value falls into (i.e. unsure whether a value is an integer, a float or a string), Python has a built-in function called type which can tell you.<\/p>\n<div class=\"textbox\">\n<p class=\"import-Normal\"><code>type('hello')<\/code><br \/>\n<code>&lt;class 'str'&gt;<\/code><br \/>\n<code>&gt;&gt;&gt; type(29)<\/code><br \/>\n<code>&lt;class 'int'&gt;<\/code><br \/>\n<code>&gt;&gt;&gt; num = 89.32<\/code><br \/>\n<code>&gt;&gt;&gt; type(num)<\/code><br \/>\n<code>&lt;class 'float'&gt;<\/code><\/p>\n<\/div>\n<h1>Python\u2019s Standard Library<\/h1>\n<p class=\"import-Normal\">As mentioned in Unit #1, Python has an extensive Standard Library which is a collection of built-in modules, each providing specific functionality beyond what is included in the \u201ccore\u201d part of Python. A Python <em>module<\/em> is simply a file that contains Python code. The name of the file dictates the name of the module; for example, a file named math.py contains the functions available from the standard math module. We will explore this module (math) here.<\/p>\n<h3>Math Module and Math Functions<\/h3>\n<p class=\"import-Normal\">Python has a math module that provides most of the familiar mathematical functions. Before we can use the module, we have to import it:<\/p>\n<p class=\"import-Normal\">&gt;&gt;&gt; import math<\/p>\n<p class=\"import-Normal\">This statement creates a module object named math. The following is a partial list of the functions which are provided by this module.<\/p>\n<ul>\n<li><code>math.trunc(x)<\/code>: Returns the float value x truncated to an integer value.<\/li>\n<li><code>math.sqrt(x)<\/code>: Returns the square root of x.<\/li>\n<li><code>math.pow(x, y)<\/code>: Returns x raised to the power y.<\/li>\n<li><code>math.degrees(x)<\/code>: Converts angle x from radians to degrees.<\/li>\n<li><code>math.radians(x)<\/code>: Converts angle x from degrees to radians.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Many math operations depend on special constants also provided by the math module.<\/p>\n<ul>\n<li><code>math.pi<\/code>: The mathematical constant <code>\u03c0 = 3.141592<\/code>\u2026.<\/li>\n<li><code>math.e<\/code>: The mathematical constant <code>e = 2.718281<\/code>\u2026.<\/li>\n<\/ul>\n<p class=\"import-Normal\">Some examples of using the math module functions (note: these functions require the library name followed by a period followed by the function name):<\/p>\n<div class=\"textbox\"><code>import math<\/code><br \/>\n<code>math.exp(5)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # returns 148.4131591025766<\/code><br \/>\n<code>math.e**5\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # returns 148.4131591025765<\/code><br \/>\n<code>math.sqrt(144)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# returns 12.0<\/code><br \/>\n<code>math.pow(12.5, 2.8)\u00a0 \u00a0 \u00a0 \u00a0 # returns 1178.5500657314767<\/code><br \/>\n<code>math.pow(144, 0.5)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0# returns 12.0<\/code><br \/>\n<code>math.trunc(1.001)\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # returns 1math.trunc(1.999) # returns 1<\/code><br \/>\n<code>12*math.pi**2\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #returns 18.4352528130723<\/code><\/div>\n<p class=\"import-Normal\">Additional useful mathematical built-in functions besides <code>float()<\/code> and <code>int()<\/code> include:<\/p>\n<ul>\n<li><code>abs(x)<\/code>: Returns the absolute value of the number x..<\/li>\n<li><code>round( x [, n] )<\/code>: returns x rounded to n digits from the decimal point (n is optional). If n is omitted it returns the nearest integer to its input.<\/li>\n<\/ul>\n<p class=\"import-Normal\">More examples of math functions (no need to import the <code>math<\/code> module with these functions):<\/p>\n<div class=\"textbox\"><code>round(80.23456, 2)<\/code> # returns 80.23<br \/>\n<code>round(100.000056, 3)<\/code> # returns 100.0<br \/>\n<code>abs(-45)<\/code> # returns 45<br \/>\n<code>abs(100.12)<\/code> # returns 100.12<\/div>\n<h1>More on Strings<\/h1>\n<p class=\"import-Normal\">We have seen how to print strings and how to get a string as input from a user. We also saw how to \u2018add\u2019 strings (concatenate) and \u2018multiply\u2019 strings.<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; word1='fun'<\/code><br \/>\n<code>&gt;&gt;&gt; word2='times'<\/code><br \/>\n<code>&gt;&gt;&gt; word1 word2<\/code><br \/>\n<code>'funtimes'<\/code><br \/>\n<code>&gt;&gt;&gt; word1*4'<\/code><br \/>\n<code>funfunfunfun'<\/code><\/div>\n<p class=\"import-Normal\">Processing of data includes the manipulation of strings (which is data) to produce something (information) which is meaningful. For example we might be presented with a file of thousands of passwords in use at a privately owned company and we would like to determine which of these passwords are secure and those that are not secure.<\/p>\n<p class=\"import-Normal\">Recall that a string is a merely sequence of characters. To determine if an individual password was secure or not we might want to look at the length of the password and the individual characters in the password, looking for characters such as uppercase, numeric, special characters, etc.<\/p>\n<p class=\"import-Normal\">Strings are actually a type of sequence; a sequence of individual characters. The indexing operator (Python uses square brackets to enclose the index) selects a single character substring from a string:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; pw = \"abc123\"<\/code><br \/>\n<code>&gt;&gt;&gt; char1 = pw[1]<\/code><br \/>\n<code>&gt;&gt;&gt; print(char1)<\/code><br \/>\n<code>b<\/code><\/div>\n<p class=\"import-Normal\">The expression <code>pw[1]<\/code> selects character number 1 from <code>pw<\/code>, and creates a new string containing just this one character. The variable <code>char1<\/code> refers to the result. When we display <code>char1<\/code>, we get the second character in the string <code>pw<\/code>, the letter \u201cb\u201d. Computer scientists always start counting from zero. The letter at subscript position zero of <code>\"abc123\"<\/code> is a. So at position <code>[1]<\/code> we have the letter b.<\/p>\n<p class=\"import-Normal\">If we want to access the zero-eth letter of a string, we just place 0, or any expression that evaluates to 0, in between the brackets:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; pw = \"abc123\"<\/code><br \/>\n<code>&gt;&gt;&gt; char1 = pw[0]<\/code><br \/>\n<code>&gt;&gt;&gt; print(char1)<\/code><br \/>\n<code>a<\/code><\/div>\n<p class=\"import-Normal\">The expression in brackets is called an index. An index specifies a member of an ordered collection, in this case the collection of characters in the string. The index indicates which one you want, hence the name. It can be any integer expression.<\/p>\n<p class=\"import-Normal\">Note that indexing returns a string \u2014 Python has no special type for a single character. It is just a string of length 1.<\/p>\n<p class=\"import-Normal\">The string method<code> len()<\/code>, when applied to a string, returns the number of characters in a string:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; pw = \"abc123\"<\/code><br \/>\n<code>&gt;&gt;&gt; len(pw)<\/code><br \/>\n<code>6<\/code><\/div>\n<p class=\"import-Normal\">At some point, you may need to break a large string (i.g a paragraph) down into smaller chunks, or strings. This is the opposite of concatenation which merges or combines strings into one.<\/p>\n<p class=\"import-Normal\">To do this, you use the<code> split()<\/code> method. What it does is split or breakup a string and add the data to a list of separate \u2018words\u2019 using a defined separator.<\/p>\n<div class=\"textbox\">\n<p class=\"import-Normal\"><code>&gt;&gt;&gt; sentence = \"Python is an interpreted high-level programming language for general-purpose programming.\"<\/code><br \/>\n<code>&gt;&gt;&gt; sentence.split()<\/code><br \/>\n<code>['Python', 'is', 'an', 'interpreted', 'high-level', 'programming', 'language', 'for', 'general-purpose', 'programming.']<\/code><br \/>\n<code>&gt;&gt;&gt; len(sentence.split())<\/code><br \/>\n<code>10<\/code><\/p>\n<\/div>\n<p class=\"import-Normal\">If no separator is defined when you call upon the function, whitespace will be used by default (as seen above). In simpler terms, the separator is a defined character that will be placed between each variable. For example:<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; numbers = \"122,35,09,97,56\"<\/code><br \/>\n<code>&gt;&gt;&gt; numbers.split(\",\")<\/code><br \/>\n<code>['122', '35', '09', '97', '56']<\/code><br \/>\n<code>&gt;&gt;&gt; len(numbers.split(\",\"))<\/code><br \/>\n<code>5<\/code><\/div>\n<p class=\"import-Normal\">The string method <code>lower()<\/code> converts all lowercase characters in a string into uppercase characters and returns it.<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; title=\"The Earth, My Butt, and Other Big Round Things\"<\/code><br \/>\n<code>&gt;&gt;&gt; title.lower()'the earth, my butt, and other big round things'<\/code><br \/>\n<code>&gt;&gt;&gt;<\/code><\/div>\n<p class=\"import-Normal\">Similarly, The string method <code>upper()<\/code> converts all uppercase characters in a string into lowercase characters and returns it.<\/p>\n<div class=\"textbox\"><code>&gt;&gt;&gt; title=\"Where the wild things are\"<\/code><br \/>\n<code>&gt;&gt;&gt; title.upper()<\/code><br \/>\n<code>'WHERE THE WILD THINGS ARE'<\/code><br \/>\n<code>&gt;&gt;&gt;<\/code><\/div>\n<h1>Unit Summary<\/h1>\n<ul>\n<li>Explain the dependencies between hardware and software<\/li>\n<li>Describe the form and the function of computer programming languages<\/li>\n<li>Create, modify, and explain computer programs following the input\/process\/output pattern.<\/li>\n<li>Form valid Python identifiers and expressions.<\/li>\n<li>Write Python statements to output information to the screen, assign values to variables, and accept information from the keyboard.<\/li>\n<li>Read and write programs that process numerical data and the Python math module.<\/li>\n<li>Read and write programs that process textual data using built-in functions and methods.<\/li>\n<\/ul>\n<p class=\"import-Normal\">From now on, we will assume that you have Python installed on your system. You should now be able to write, save and run Python programs at ease.<\/p>\n<p class=\"import-Normal\">Now that you are a Python user, let us learn some more Python concepts.<\/p>\n<p class=\"import-Normal\">We have seen how to use operators, operands and expressions &#8211; these are the basic building blocks of any program.<\/p>\n<p class=\"import-Normal\">Next, we will see how to make use of these in our programs using statements.<\/p>\n<p class=\"import-Normal\">We have seen how to use the three control flow statements &#8211; if , while and for along with their associated break and continue statements. These are some of the most commonly used parts of Python and hence, becoming comfortable with them is essential.<\/p>\n<h1>Practice Problems<\/h1>\n<ol>\n<li>What is the result of each of the following:&gt;&gt;&gt; &#8220;Python&#8221;[1]<br \/>\na.&gt;&gt;&gt; &#8220;Strings are sequences of characters.&#8221;[5]<br \/>\nb.&gt;&gt;&gt; len(&#8220;wonderful&#8221;)<br \/>\nc.&gt;&gt;&gt; &#8220;Mystery&#8221;[:4]<br \/>\nd.&gt;&gt;&gt; &#8220;p&#8221; in &#8220;Pineapple&#8221;<br \/>\ne.&gt;&gt;&gt; &#8220;apple&#8221; in &#8220;Pineapple&#8221;<br \/>\nf.&gt;&gt;&gt; &#8220;pear&#8221; not in &#8220;Pineapple&#8221;<br \/>\ng.&gt;&gt;&gt; &#8220;apple&#8221; &gt; &#8220;pineapple&#8221;<br \/>\nh.&gt;&gt;&gt; &#8220;pineapple&#8221; &lt; &#8220;Peach&#8221;<\/li>\n<li>Take the sentence: <code>All work and no play makes Jack a dull boy<\/code><span style=\"font-size: 1em\">. Store each word in a separate variable, then print out the sentence on one line using the <\/span><code>print<\/code><span style=\"font-size: 1em\"> function.<\/span><\/li>\n<li>Add parenthesis to the expression <code style=\"text-align: initial\">6 * 1 - 2<\/code><span style=\"font-size: 1rem;text-align: initial\"> to change its value from <\/span><code style=\"text-align: initial\">4<\/code><span style=\"font-size: 1rem;text-align: initial\"> to <\/span><code style=\"text-align: initial\">-6<\/code><span style=\"font-size: 1rem;text-align: initial\">.<\/span><\/li>\n<li>The formula for computing the final amount if one is earning compound interest is given on Wikipedia as this formula for compound interest:<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-343 aligncenter\" src=\"https:\/\/s3-us-west-2.amazonaws.com\/courses-images\/wp-content\/uploads\/sites\/3976\/2019\/01\/15232333\/image24-1.png\" alt=\"\" width=\"396\" height=\"166\" \/><br \/>\nWrite a Python program that assigns the principal amount of $10000 to variable <code>P<\/code>, assign to n the value 12, and assign to <code>r<\/code><span style=\"text-align: initial;font-size: 1em\"><span style=\"text-align: initial;font-size: 1em\"> the interest rate of 8%. Then have the program prompt the user for the number of years<code> t<\/code> that the money will be compounded for. Calculate and print the final amount after <code>t<\/code> years.<\/span><\/span><\/li>\n<li>Evaluate the following numerical expressions on paper, then use the Python IDLE Shell to check your results:a.&gt;&gt;&gt; 5 % 2<br \/>\nb.&gt;&gt;&gt; 9 % 5<br \/>\nc.&gt;&gt;&gt; 15 % 12<br \/>\nd.&gt;&gt;&gt; 12 % 15<br \/>\ne.&gt;&gt;&gt; 6 % 6<br \/>\nf.&gt;&gt;&gt; 0 % 7<br \/>\ng.&gt;&gt;&gt; 7 % 0<\/li>\n<li>You look at the clock and it is exactly 2 pm. You set an alarm to go off in 51 hours. At what time does the alarm go off? (Hint: you could count on your fingers, but this is not what we\u2019re after. If you are tempted to count on your fingers, change the 51 to 5100.)Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and ask for the number of hours to wait. Your program should output (print) what the time will be on the clock when the alarm goes off.<\/li>\n<li>Write a program <code>find_hypot<\/code> which, given the length of two sides of a right-angled triangle, returns the length of the hypotenuse. (Hint: x ** 0.5 will return the square root.)<\/li>\n<li>Practice using the Python IDLE Shell as a calculator:\n<ol type=\"a\">\n<li>Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?<\/li>\n<li>If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?<\/li>\n<\/ol>\n<\/li>\n<li>Enter the following statement, exactly, into the interactive Shell:<code>printt<span style=\"text-align: initial;font-size: 1em\">('What time is it?')<\/span><\/code>Is this a syntax error or a logic error?<\/li>\n<li>Suppose that the math module of the Python Standard Library were<span style=\"text-align: initial;font-size: 1em\"> imported. Write the Python statements to calculate the square root of four and print the answer.<\/span><\/li>\n<li>What is the value of variables <code>num1<\/code> and <code>num2<\/code> after the following Python statements are executed?\n<ol type=\"a\">\n<li style=\"list-style-type: none\"><code>num = 0<\/code><\/li>\n<li style=\"list-style-type: none\"><code>new = 5<\/code><\/li>\n<li style=\"list-style-type: none\"><code>num1 = num + new * 2<\/code><\/li>\n<li style=\"list-style-type: none\"><code>num2 = num +\u00a0 new * 2<\/code><\/li>\n<\/ol>\n<\/li>\n<li>What is wrong with the following statement that attempts to assign the value ten to variable x?\n<ol>\n<li style=\"list-style-type: none\"><code>10 = x<\/code><\/li>\n<\/ol>\n<\/li>\n<li>Classify each of the following as either a legal or illegal Python identifier:a. fred<br \/>\nb .if<br \/>\nc. 2x<br \/>\nd.-4<br \/>\ne. sum_total<br \/>\nf. sumTotal<br \/>\ng. sumtotal<br \/>\nh. While<br \/>\ni. x2<br \/>\nj. Private<br \/>\nk. public<br \/>\nl. $16<br \/>\nm. xTwo<br \/>\nn. 10%<br \/>\no. a27834<\/li>\n<li>How is the value <strong>2.45 x\u00a0<\/strong>10<sup>-5\u00a0<\/sup>expressed as a Python literal?<\/li>\n<li>Given the following assignment:x = 2: Indicate what each of the following Python statements would print.<br \/>\na. print(&#8220;x&#8221;)<br \/>\nb. print(&#8216;x&#8217;)<br \/>\nc. print(x)<br \/>\nd. print(&#8220;x + 1&#8221;)<br \/>\ne. print(&#8216;x&#8217; + 1)<br \/>\nf.\u00a0 print(x + 1)<\/li>\n<li>Given the following assignments:<br \/>\ni1 = 2<br \/>\ni2 = 5<br \/>\ni3 = -3<br \/>\nd1 = 2.0<br \/>\nd2 = 5.0<br \/>\nd3 = -0.5<\/li>\n<li>Evaluate each of the following Python expressions.\n<ol>\n<li>\u00a0i1 + i2<\/li>\n<li>i1 \/ i2<\/li>\n<li>i1 \/\/ i2<\/li>\n<li>i2 \/ i1<\/li>\n<li>i1 * i3<\/li>\n<li>d1 + d2<\/li>\n<li>d1 \/ d2<\/li>\n<li>d2 \/ d1<\/li>\n<li>d3 * d1<\/li>\n<li>d1 + i2<\/li>\n<li>i1 \/ d2<\/li>\n<li>d2 \/ i1<\/li>\n<li>i2 \/ d1<\/li>\n<li>i1\/i2*d1<\/li>\n<\/ol>\n<\/li>\n<li>What is printed by the following statement:<br \/>\n#print(5\/3)<\/li>\n<li>Consider the following program which contains some errors. You may assume that the comments within the program accurately describe the program\u2019s intended behavior.# Get two numbers from the user<br \/>\nn1 = float(input()) # first number<br \/>\nn2 = float(input()) # second number<br \/>\n# Compute sum of the two numbers<br \/>\nprint(n1 + n2) # third number<br \/>\n# Compute average of the two numbers<br \/>\nprint(n1+ n2\/2) # fourth number<br \/>\n# Assign some variables<br \/>\nd1 = d2 = 0 # fifth number<br \/>\n# Compute a quotient<br \/>\nprint(n1\/d1) # sixth number<br \/>\n# Compute a product<br \/>\nn1*n2 = d1 # seventh number<br \/>\n# Print result<br \/>\nprint(d1) # eighth numberFor each line listed in the comments, indicate whether or not a syntax error, run-time error, or semantic error is present. Not all lines contain an error.<\/li>\n<li>What is printed by the following code fragment?x1 = 2<br \/>\nx2 = 2<br \/>\nx1 += 1<br \/>\nx2 -= 1<br \/>\nprint(x1)<br \/>\nprint(x2)<\/li>\n<li>Given the following definitions:<br \/>\nx = 3<br \/>\ny = 5<br \/>\nz = 7evaluate the following Boolean expressions:<br \/>\nx == 3<br \/>\nx &lt; y<br \/>\nx &gt;= y<br \/>\nx &lt;= y<br \/>\nx != y &#8211; 2<br \/>\nx &lt; 10<br \/>\nx &gt;= 0 and x &lt; 10<br \/>\nx &lt; 0 and x &lt; 10<br \/>\nx &gt;= 0 and x &lt; 2<br \/>\nx &lt; 0 or x &lt; 10<br \/>\nx &gt; 0 or x &lt; 10<br \/>\nx &lt; 0 or x &gt; 10<\/li>\n<li>Given the following definitions:x =3<br \/>\ny = 3<br \/>\nb1 = true<br \/>\nb2 = false<br \/>\nx == 3<br \/>\ny &lt; 3<br \/>\nevaluate the following Boolean expressions:<\/li>\n<\/ol>\n<\/div>\n<ol type=\"a\">\n<li>b2<\/li>\n<li>not b1<\/li>\n<li>not b2<\/li>\n<li>b1 and b2<\/li>\n<li>b1 or b2<\/li>\n<li>x<\/li>\n<li>y<\/li>\n<li>x or y<\/li>\n<li>not y<\/li>\n<\/ol>\n<hr class=\"before-footnotes clear\" \/><div class=\"footnotes\"><ol><li id=\"footnote-51-1\">Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p9. <a href=\"#return-footnote-51-1\" class=\"return-footnote\" aria-label=\"Return to footnote 1\">&crarr;<\/a><\/li><li id=\"footnote-51-2\">Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p10 <a href=\"#return-footnote-51-2\" class=\"return-footnote\" aria-label=\"Return to footnote 2\">&crarr;<\/a><\/li><li id=\"footnote-51-3\">Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, pp.11-12. <a href=\"#return-footnote-51-3\" class=\"return-footnote\" aria-label=\"Return to footnote 3\">&crarr;<\/a><\/li><li id=\"footnote-51-4\">Based on information from: https:\/\/www.softwaretestingmaterial.com\/compiler-vs-interpreter\/ <a href=\"#return-footnote-51-4\" class=\"return-footnote\" aria-label=\"Return to footnote 4\">&crarr;<\/a><\/li><li id=\"footnote-51-5\">www.python.org\/about\/success\/ <a href=\"#return-footnote-51-5\" class=\"return-footnote\" aria-label=\"Return to footnote 5\">&crarr;<\/a><\/li><li id=\"footnote-51-6\">Dierbach, Charles. Introduction to Computer Science Using Python: A Computational Problem-solving Focus. Wiley Publishing, 2012, p23 <a href=\"#return-footnote-51-6\" class=\"return-footnote\" aria-label=\"Return to footnote 6\">&crarr;<\/a><\/li><li id=\"footnote-51-7\">Jacobson, Molly McBride. \"Grace Hopper's Bug.\" Atlas Obscura, https:\/\/www.atlasobscura.com\/places\/grace-hoppers-bug . Accessed January 9, 2018. <a href=\"#return-footnote-51-7\" class=\"return-footnote\" aria-label=\"Return to footnote 7\">&crarr;<\/a><\/li><\/ol><\/div>","protected":false},"author":23485,"menu_order":1,"template":"","meta":{"_candela_citation":"[]","CANDELA_OUTCOMES_GUID":"","pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[48],"contributor":[],"license":[],"class_list":["post-51","chapter","type-chapter","status-publish","hentry","chapter-type-numberless"],"part":3,"_links":{"self":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/users\/23485"}],"version-history":[{"count":2,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/51\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/51\/revisions\/98"}],"part":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapters\/51\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/pressbooks\/v2\/chapter-type?post=51"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/contributor?post=51"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/courses.lumenlearning.com\/suny-albany-programmingforproblemsolving-v2\/wp-json\/wp\/v2\/license?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}