I. Installing and Running the Python Interpreter A. Interpreters and Compilers ------------------------------------------ WHAT IS A PROGRAMMING LANGUAGE? def: a *language* is a set of strings def: a *programming language* is a language that is capable of expressing all computations (i.e., is Turing-complete) ------------------------------------------ ------------------------------------------ IMPLEMENTATIONS OF PROGRAMMING LANGUAGES Programs in a programming language are (generally) written in *source files*, which are files containing the text of a program. 2 general strategies: Compilers (C): translate source files into machine code, then the machine directly interprets that Interpreters (Python): read and execute the source program Intermediate approaches (Java): translate source to intermediate form (byte code) and execute that ------------------------------------------ ------------------------------------------ INTERPRETERS VS. COMPILERS (source program) -----------> output interpreter (Source program) | | compiler | v [exectuable] ----------> output machine ADVANTAGES OF COMPILERS/INTERPRETERS compilers (C) interpreters (Python) code runs ~10 times faster ------------------------------------------ B. Downloading the Python Interpreter for your own machine ------------------------------------------ USING PYTHON ON YOUR OWN MACHINE See https://www.python.org/downloads/ Follow the directions Run IDLE exit using exit(0) or quit() ------------------------------------------ 1. Windows ------------------------------------------ WINDOWS INSTALLATION DETAILS When running the installer, click on the option to add Python to your PATH Select the "Python 3.7" Start Menu folder, then click on IDLE Pin IDLE to the taskbar For command line use, run python3 ------------------------------------------ 2. Macs ------------------------------------------ MAC INSTALLATION DETAILS After installation, use the finder: Find applications/Python 3.7 directory In that directory run IDLE (double click on it) Pin IDLE to the dock ------------------------------------------ C. Using Python from Eustis ------------------------------------------ YOU CAN USE PYTHON AT UCF Use the eustis.eecs.ucf.edu system login: your nid password: your nid password Use a ssh client like putty (putty.org or http://www.chiark.greenend.org.uk/ ~sgtatham/putty/download.html) Run the command python3 at the shell's prompt ------------------------------------------ II. Modeling with Programs A. What programs do ------------------------------------------ WHAT A PROGRAM DOES How does a program control an airplane (or a car, etc.)? environment environment | ^ v [---------] | sensors and --> [ Program ] ---> actuators input devices [---------] and output devices ------------------------------------------ What are some examples of sensors or input devices? What are some examples of actuators or output devices? 1. The OS helps abstract away devices ------------------------------------------ FROM THE POINT OF VIEW OF THE PROGRAM With the help of the Operating System (OS): [---------] input data --> [ Program ] ---> output data [---------] ------------------------------------------ 2. Why we can model programs using simple inputs and outputs ------------------------------------------ SOPHISTICATED DEVICE CONTROL ISN'T NEEDED An old application: Artillery range finding Problem: what angle to aim an artillery piece at to hit a certain target (at a given distance)? Solution: Some person tells the operator where to shoot (the environment) Some types into the computer the distance (input). The program does a calculation (using physics) The program prints out the angle (output). The operator adjusts the angle (actuator). ------------------------------------------ B. The conclusion about modeling ------------------------------------------ THE CONCLUSION ABOUT MODELING Abstractly every computer program is "blind" but models the real world environment's change to state environment (user's question) (user's answer) | ^ | encoding | interpretation v | representation -->[Program]--> output as input representation ------------------------------------------ III. Python Numeric Types and Expressions A. Numeric Types 1. in Python ------------------------------------------ PYTHON NUMERIC TYPE HIERARCHY numbers.Number numbers.Integral int values: 0, 1, -1, 2, -2, 3, -3, ... operators: +, -, *, **, /, //, %, <<, >>, &, |, ^, ~ bool values: False, True operators: &, and, |, or, ^, ~, if else float (numbers.Real) values: 0.0, 3.14159, 0.2718e1, .112, 1e10, 5e-6 operators: +, -, *, **, /, //, % complex (numbers.Complex) values: 3.14j, 10j, 3.14+5.2j operators: +, -, *, **, /, //, % ------------------------------------------ 2. in C ------------------------------------------ NUMERIC TYPES IN C Several kinds of ints Signed (both positive and negative): short or short int int long or long int Unsigned (only positive): unsigned short or unsigned short int unsigned or unsigned int unsigned long or unsigned long int The number of bits varies by compiler but generally: short ~ 16 bits up to 32,767 int ~ 32 bits up to 2**31 ~ 2 billion long ~ 64 bits up to 2**63 ~ 10**18 int is usually most efficient for time ------------------------------------------ How are integers different in Python? ------------------------------------------ FLOATING POINT TYPES IN C 2 kinds of floating point types in C: float ~ 32 bits double ~ 64 bits (like floats in Python) ------------------------------------------ 3. Python numbers and operators ------------------------------------------ EXAMPLES OF NUMBERS AND OPERATORS >>> -1 >>> 42 >>> 2.0e5 >>> 3.0e-4 >>> 0.2718e1 >>> 3.14159 * 1.0j >>> 7 + 3 >>> 7 - 3 >>> 2 * 3 >>> 8 * 5 ------------------------------------------ So what does * do? B. Number representations and operators 1. integer operators ------------------------------------------ MORE INTEGER OPERATORS >>> 7 / 3 >>> 7 // 3 >>> -7 / 3 >>> -7 // 3 >>> 3 // 2 >>> 3223 // 100 ------------------------------------------ What is the difference between / and //? ------------------------------------------ INTEGER MODULO OPERATOR (%) >>> 3 % 2 >>> (3//2)*2 + (3%2) >>> -3 // 2 >>> -3 % 2 >>> (-3 // 2)*2 + (-3 % 2) ------------------------------------------ Why is -3 // 2 equal to -2 and not -1? a. power operator ------------------------------------------ POWER OPERATOR >>> 2**3 >>> 5**2 >>> 5**3 >>> 10**3 >>> -3**2 >>> -3**3 >>> 1e3 ------------------------------------------ What should Python do if we raise a number to a negative power? b. integer bitwise operators ------------------------------------------ INTEGER REPRESENTATION Integers are (conceptually) represented by (infinite) bitstrings in 2's complement notation. 0 is represented by 000...000 1 is represented by 000...001 2 is represented by 000...010 3 is represented by 000...011 4 is represented by 000...100 -1 is represented by 111...111 -2 is represented by 111...110 -3 is represented by 111...100 The advantage of 2's complement notation is that the numbers can be treated as unsigned numbers by hardware ------------------------------------------ What's the pattern for converting the binary representation to decimal? ------------------------------------------ BITWISE OPERATORS ON INTEGERS ~ is the inversion of the bits of its argument >>> ~0 >>> ~1 >>> ~(-1) >>> ~2 >>> ~(-2) >>> ~3 >>> ~(~3) >>> ~(-4) ------------------------------------------ What does ~ do? ------------------------------------------ THE & OPERATOR >>> 1 & 1 >>> 1 & -1 >>> 3 & 4 >>> 7 & 2 >>> 2 & 3 >>> 3 & 4 ------------------------------------------ What does & do? ------------------------------------------ >>> 1 | 1 >>> 1 | -1 >>> 2 | 3 >>> 2 | 16 >>> 3 | 4 ------------------------------------------ Why is 3|4 equal to the result shown? 2. floating point a. representation ------------------------------------------ FLOATING POINT NUMBERS Approximations of real numbers Represented (in IEEE 754 format) by: a sign bit (1 means negative) a binary exponent (11 bits representing -1022 to 1023) a mantissa (a 52 bit binary fraction, stored in 51 bits representing a fraction between 0.5 and 1.0) the meaning of S E M is (-1)**(S) * 2**(E) * M Can represent +/- 2**-1022 to +/- 2-(2**(-52))*2**1023 or about -1.0e308 to 1.0e308 >>> 1.0e308 >>> 1.0e309 >>> -1.0e309 >>> -1.0e308 >>> 1.0e-323 >>> 1.0e-324 >>> -1.0e-323 >>> -1.0e-324 ------------------------------------------ What numbers cannot be represented as floats? ------------------------------------------ FLOATING POINT NUMBERS APPROXIMATE REALS They are not exact! >>> 1/9.232 * 9.232 >>> 1e-5 + 1e5 >>> 1e-9 + 1e9 >>> 1e-27 + 1e27 >>> 1 - 3*(4/3 - 1) >>> 0.1 + 0.1 + 0.1 ------------------------------------------ How many real numbers are there between 0 and 10**(308.25)? How many floats are there in that range? ------------------------------------------ FLOATING POINT OPERATORS >>> 5.3 + 4.2 >>> 1.125 * 10.0 >>> 1.2345 / 10.0 >>> 12.5 // 10 ------------------------------------------ What does // do for floats? C. errors ------------------------------------------ NUMERIC ERRORS >>> #Divison by zero: >>> 5 / 0 # error! >>> 1.0 / 0.0 # error! >>> 5 // 0 # error! >>> #Overflow: >>> 10.0**323 # error! >>> # However: >>> 10 ** 323 ------------------------------------------ What's the difference in the last 2? D. Modeling with numbers ------------------------------------------ SOME QUESTIONS FOR YOU Calculate using Python: 1. How many steps do you need to take to walk 0.25 miles? 2. How many gallons are in a teaspoon? 3. How many acres is a baseball diamond? 4. How many acres is a soccer field? ------------------------------------------ IV. Friday Problems with Python Numeric Types and Expressions A. A few left over points 1. floating point numbers are approximations ------------------------------------------ FLOATING POINT 'e' (or 'E') NOTATION >>> 0.2718e1 >>> 3.0e-4 ------------------------------------------ What does that e mean following the number? ------------------------------------------ FLOATING POINT NUMBERS ARE APPROXIMATIONS NOT EXACT! They are not exact! >>> 1/9.232 * 9.232 >>> 1e-5 + 1e5 >>> 1e-9 + 1e9 >>> 1e-27 + 1e27 >>> 1 - 3*(4/3 - 1) >>> 0.1 + 0.1 + 0.1 ------------------------------------------ For each of these, what should the mathematical answer be? How many real numbers are there between 0 and 10**(308.25)? How many floats are there in that range? B. Problems ------------------------------------------ SOME PROBLEMS FOR YOU Define functions to solve these problems: 1. How long would it take for you to drive a distance of D miles at a speed of V miles/hour? ------------------------------------------ ------------------------------------------ MORE PROBLEMS 2. What is the energy (in Joules) needed to heat a given volume of water a given number of degrees Celsius? ------------------------------------------ ------------------------------------------ MORE PROBLEMS 3. Given the dimensions of a TV screen in inches, what is the diagonal measure of that screen? ------------------------------------------ ------------------------------------------ MORE PROBLEMS 4. Given an age, in years, return the number of miles that a person that age has traveled around the sun. ------------------------------------------ ------------------------------------------ MORE PROBLEMS 5. If you could stand on the surface of Pluto, what would a scale you brought with you say you weighed? ------------------------------------------