ENGR/CS 101 CS Session Lecture 2 If possible, each student should have access to a computer with Python 2.7, either on a laptop or on one of the lab computers. If there are not enough computers, two students may share a lab computer. Each should take turns typing. Lecture 2 ENGR/CS 101 Computer Science Session 1 Outline Software life cycle Introduction to Python Lecture 2 ENGR/CS 101 Computer Science Session 2 Software Life Cycle Specification of the problem/task Analysis and design of a solution Implementation of the solution Testing and debugging Maintenance and evolution of the system Obsolescence Lecture 2 ENGR/CS 101 Computer Science Session 3 Specifications How does the user interact with the program? What are the results of the computation? Lecture 2 ENGR/CS 101 Computer Science Session 4 Specification Example Write a program that computes the volume of a right cylinder. It should allow the user to input the base radius and the height of the cylinder, then compute and display the area of the base and the volume of the cylinder. The formula for computing the volume of a right cylinder is: 𝑣𝑜𝑙𝑢𝑚𝑒 = 𝜋 × 𝑟𝑎𝑑𝑖𝑢𝑠 2 × ℎ𝑒𝑖𝑔ℎ𝑡 Lecture 2 ENGR/CS 101 Computer Science Session 5 Analysis and Design Main work of creating applications How will the program accomplish the specifications? Lecture 2 Identify the data being used Identify the algorithm that will compute the result ENGR/CS 101 Computer Science Session 6 Analysis and Design Program = Data + Algorithm Data is the information needed to do the computation. Usually the nouns in a specification. What data do we need for the example specification? Lecture 2 constant 𝜋 radius of base height of cylinder volume of cylinder ENGR/CS 101 Computer Science Session 7 Algorithm Algorithm: a series of well-defined steps that can be followed as a procedure. Example: 1. 2. 3. 4. Lecture 2 Ask the user for a radius and a height Compute the base area of the cylinder Compute the volume of the cylinder Display the base area and volume of cylinder ENGR/CS 101 Computer Science Session 8 Implementation Use a programming language to implement the program. Syntax: What are the legal "sentences" in the language? Semantics: What do the "sentences" mean? Compilers and interpreters enforce syntax. Semantics determine whether the computation is correct. Lecture 2 ENGR/CS 101 Computer Science Session 9 Compiling vs. Interpreting Some languages are compiled with a program called a compiler. Source code file is translated into a machine code file. Examples: C/C++, Java, Pascal, COBOL, Fortran Other languages are interpreted. An interpreter is a program that receives programming language statements and executes them directly. Lecture 2 Examples: (original) BASIC, LISP, Prolog, LOGO ENGR/CS 101 Computer Science Session 10 Introduction to Python On the lab computers, log into Windows (reboot if in Linux). Press the 'Windows' key, start typing 'python', click on 'IDLE (Python GUI) 2.7' IDLE is named for Eric Idle, of Monty Python fame Python is an interpreted language, and IDLE runs it as a shell in a window. Lecture 2 ENGR/CS 101 Computer Science Session 11 Data types Any kind of data item is considered an object in Python. Objects are classified into different kinds called data types. Every object has some specific type. Primitive types are those implemented directly in the language. E.g. integers, floating point numbers Lecture 2 ENGR/CS 101 Computer Science Session 12 Python as a Calculator We can compute numeric expressions directly into the shell at the >>> prompt. >>> 2+2 4 >>> 100-75 25 >>> 7*9 63 An expression is a combination of operators and operands Lecture 2 ENGR/CS 101 Computer Science Session 13 Operators + * / // % ** Lecture 2 : addition : subtraction, negation : multiplication : floating point division : integer division : integer remainder : exponentiation ENGR/CS 101 Computer Science Session 14 Assignment Assignment means to give an object a name. These names are called variables Syntax is: <var> = <expression> The expression is evaluated and the variable refers to the result. >>> pi = 3.14159 >>> radius = 8.0 >>> height = 16 Lecture 2 ENGR/CS 101 Computer Science Session 15 Variable names Must start with a letter or underscore (_) Only start with underscore for system variables Uses only letters, digits Cannot be a reserved word of the language Examples: Lecture 2 pi baseArea cylinderVolume ENGR/CS 101 Computer Science Session 16 Using Variables A variable can be used as an operand. Its value is whatever was assigned to it. Example: compute volume of a cylinder with radius 8 and height 16 >>> baseArea = pi * radius **2 >>> cylinderVolume = baseArea * height >>> baseArea 201.06176 >>> cylinderVolume 3216.98816 Lecture 2 ENGR/CS 101 Computer Science Session 17 Python Programs Computing things directly in the shell works, but we don't really want to type things over and over. To make a Python program, use File -> New File to get an editor window. We type the program statements in this window and save it to a file (File -> Save As) before running it. Note: on a lab machine save your program to a folder on your network drive. Lecture 2 ENGR/CS 101 Computer Science Session 18 Python Programs To run the program, use Run -> Run Module The program will run in the shell window To see the results, type the variable names. Comments start with # to the end of a line. Change the values assigned to radius and height, save the program (Ctrl-S), and run the program again. Lecture 2 ENGR/CS 101 Computer Science Session 19 Keyboard Input Recall in the specifications, we need to ask the user to enter the values of the radius and height instead of assigning them. The input function allows us to do that Syntax: <var> = input (<prompt>) A prompt is a string that tells the user what is expected. Strings are given using quote marks. Example: radius = input ('Enter the radius') Lecture 2 ENGR/CS 101 Computer Science Session 20 Displaying Output The specifications also say that we need to display the results. After all, the user of the program shouldn't have to know the names of our variables to get the answer. The print statement does this. Syntax: print object1, object2, … Example: print 'The base area is', baseArea Lecture 2 ENGR/CS 101 Computer Science Session 21 Running Your Program Later After exiting IDLE, your program will be saved. To run the program later, browse to your program file. Right-click on the file, select 'Edit with IDLE'. Use Run->Run Module to run the program. A shell window will be created. Lecture 2 ENGR/CS 101 Computer Science Session 22 Testing and Debugging Testing should try to cover all possible ways a user might interact with a program A bug is a logic error that causes an incorrect result. Debugging is the process of finding and correcting bugs. A debugger is a program that displays the internals of a program as it runs to aid in debugging. Lecture 2 ENGR/CS 101 Computer Science Session 23 Maintenance/Obsolescence It is estimated that 80% of the cost of a software product is expended after the first release. Maintenance includes fixing bugs, adapting to changing environments, increasing functionality Obsolescence is handling the transition when a software product will no longer be available Lecture 2 ENGR/CS 101 Computer Science Session 24