General Computer Science for Engineers CISC 106 Lecture 03 Dr. John Cavazos Computer and Information Sciences 09/07/2010
Machine Language to Assembly Computers only understand machine language Machine language is difficult to write Assembly language uses short words that are known as mnemonics Assembler is used to translate an assembly language program to machine language Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Do you know of any other high-level computer programming languages? High-Level Languages Assembly language is referred to as a low-level language High-level languages allow you to create powerful and complex programs without knowing how the CPU works, using words that are easy to understand. For example: Ada, BASIC, Python, C++, Ruby, Visual Basic Do you know of any other high-level computer programming languages? Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Compilers and Interpreters The statements written in a high-level language are called source code or simply code Source code is translated to machine language using a compiler or an interpreter Syntax error is a mistake that the compiler / interpreter can catch: Misspelled word Missing punctuation character Incorrect use of an operator Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Compilers Compiler is a program that translates a high-level language program into a separate machine language program Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Interpreter An interpreter is a program that both translates and executes the instructions in a high-level language program Python language uses an interpreter Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Python Introduction Python programs can be saved in files Python interpreter : Can run programs Can execute individual statements typed at prompt IDLE is a development environment Can be used to write, execute, and test Python programs Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Python Interpreter A program that can read Python programming statements and execute them is the Python interpreter Python interpreter has two modes: Interactive mode waits for a statement from the keyboard and executes it Script mode reads the contents of a file (Python program or Python script) and interprets each statement in the file Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Python Interpreter (cont’d) Interpreter Mode Invoke Python interpreter >>> is prompt indicating interpreter is waiting for a Python statement >>> print ‘Python programming is fun!’ Python programming is fun! >>> Statements typed in interactive mode are not saved as a program [ENTER] Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Python Interpreter (cont’d) Script Mode Use a text editor to create a file containing the Python statements Save the file with a .py extension Run the program: > python test.py [ENTER] Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
What is a function? inputs output
Designing Programs Use Design Recipes 5 Steps TAs will make sure you follow steps!
Design Recipe : Step 1 : Docstring Read the problem. Inputs : What kinds of data does the function consume? Output : What kind of data does it produce? Write a short purpose statement. List all required input parameters (use meaningful names). Provide data kinds for each parameter, including the return value.
Design Recipe : Step 1 (example) Write a function that computes the area of a rectangle, given the width and height.
Design Recipe : Step 2 : Function Header Write the function header, using the information from the contract. def area(width, height): inputs keyword function name
Design Recipe : Step 3 : Testing Make up examples of inputs. What should your program produce for those inputs? Write these as unit tests using assertEqual. assertEqual(area(0,0), 0) assertEqual(area(10,10), 100)
Design Recipe : Step 4 : Function Body Now write your function body. Look closely at your examples, and try to see the pattern. How can the inputs be turned into the desired outputs? def area(width, height): … return width * height
Design Recipe : Step 5 : Put it all Together! Now put it all together into one Python file,
Extra Slides
Atomic Data - Numbers : floats, integers Can use “type” to find out the data type of something. type(5) # int type (5.0) # float Strings Some examples : “hello” and ‘hello’ type(“hello”) # str “hello” + “world” # plus symbol concatenates two strings
Atomic Data (cont’d) Booleans True False True and False True or False type(True) # bool
Atomic Data (cont’d) # Can mix and match numbers >>> 3 + 5.0 8.0 # Cannot mix numbers and strings “hello” + 3 # Builtin convert between types str(100) int(“5”)
Programming, Computing, Functions Programming = writing “arithmetic” expressions Computing = finding the value of an expression Functions = programs that take inputs and compute outputs
Simple Function What is the expression being computed? X= 1 2 3 4 5 Y= 9 16 ??
Simple Function The python function to compute this table. def compute_square(x) : """ returns the square of the input value x that is passed in x -- int return – int return x * x X= 1 2 3 4 5 Y= 9 16 ??