file: 01.2.typesExprsSimplification.txt author: Bob Muller Lecture Notes CS1101 Computer Science 1 Week 1, Meeting 2 Topics: 1. What is a Programming Language? 2. Basic Types, Literals, Operators and Expressions 3. Simplification (aka Evaluation or Reduction) and Values 1. What is a Programming Language? For the purposes of this course, we can understand a language as a system for expressing ideas. A programming language is a language designed to express ideas related to computation and information. At the present time, most programming languages, including Python, are text-based: the program code in the language is written out in text (much like these notes) and is then processed by the computer to carry out the expressed computation. In Python, this processing is typically carried out in by an -interpreter- which 1. prompts the user for and -reads- a Python expression 2. -evaluates- the expression to find its value (if it has one) 3. -prints- the value For example, a session with Python might look like: >>> 3.14 * 2 6.28 >>> Since these 3 steps are done over and over in a -loop-, the setup is often called a REPL. ---------------- 2. Basic Types, Literals, Operators and Expressions In computing, -types- can be understood as sets of "things" (for now) that are the subject of the computation. All programming languages have a number of basic types such as int and float that are built-in to the language. Most programming languages have ways to define new types. We'll get to that subject later in the semester. Virtually all programming languages have basic types int and float together with built-in operators (e.g., +, -, *, /, %) that work on values of type int and float. >>> # Anything to the right of a pound sign is a comment to the human >>> # reader, Python just ignores it. The int Type >>> 2 2 # 2 is a -literal- (aka -constant-) expression # of type int. Every literal is a -value-. >>> 5 * 2 10 # 10 is the -value- of the expression 5 * 2. CONVENTION: Operators should almost always have spaces on either side! So "5 / 2" is good, "5/2" is less good, and both "5 /2" and "5/ 2" are bad. >>> 5 / 2 2 # integer division, value is an integer >>> 2 / 0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> >>> 5 % 2 # mod or integer remainder, of 5 / 2 1 >>> 2 ** 3 # aka 2^3 8 The float Type >>> 3.14 3.14 >>> 2.0 * 3.14 6.28 >>> .3 + 2. 2.3 >>> 314e-2 3.14 The built-in operators are interpreted using the familiar PEMDAS order for the operators: Euclid's Common Notion #1: Things that are equal to the same thing are equal to each other. 2.0 + 3.14 * 4.0 = 2.0 + 12.56 = 14.56 Since 2.0 + 3.14 * 4.0 is equal to 2.0 + 12.56, and 2.0 + 12.56 is equal to 14.56, we can conclude that 2.0 + 3.14 * 4.0 is equal to 14.56. Simplification Computation is a process of -simplification-. 2.0 + 3.14 * 4.0 -> 2.0 + 12.56 -> 14.56 Since the literal 14.56 cannot be simplified, we call it a -value-. The standard mathematical order of evaluation can be altered with parenthesization: >>> (2.0 + 3.14) * 4.0 20.560000000000002 # NB: 15 decimal places of information >>> 5.0 / 2.0 2.5 >>> 5.0 % 2.0 1.0 # Mod produces a float Mixing Types --- it isn't a great idea but most programming languages try to do something sensible. They generally try to -preserve information- by converting literals of the less informative type to the more informative one. >>> 5 / 2.0 2.5 # Div sees int on one side, float on the # the other, it first converts 5 to 5.0 # before dividing. As we have seen, Python's arithmetic operators are flexible about the types of their operands. (We'll see the same sort of flexibility with some of the built-in functions below.) The Python documentation sometimes uses the psuedo-type "number" to suggest this flexibility with types. Strings >>> 'hello world' 'hello world' >>> "hello world" # Hmm, lets stick with 'hello world' 'hello world' >>> 'hello' + 'world' # For strings, the plus operator concatenates helloworld Boolean (or Logical) Values There are just two values of this type, the constants True and False. >>> True # Like all constants, they are their own values. True >>> False False >>> false Traceback (most recent call last): File "", line 1, in NameError: name 'false' is not defined Although Python can do something reasonable when integers are mixed with floats, surprising things can sometimes happen when values of other types are mixed. >>> 'hello ' * 3 'hello hello hello' >>> True + 4 5 # Ouch! Don't do this! >>> 2.0 / False Traceback (most recent call last): File "", line 1, in ZeroDivisionError: float division by zero >>> Summary: - Python's modus operandi is to simplify -expressions-. Expressions are mostly familiar, being built-up from constants of base type, operators and parenthesis. - Expressions can be evaluated by simplifying them to their values if they have one. - A -value- is an expression that cannot be further simplified. So for now, the containment is: +--------------------------+ | Expressions | | +----------------------+ | | | Values | | | | +------------------+ | | | | | Literals | | | | | +------------------+ | | | +----------------------+ | +--------------------------+ Every literal is a value and every value is an expression. There are expressions that are not values and (as we will see) there are values that are not literals. Later on we will alter this diagram a bit.