class: center, middle, inverse # What is a Program? --- ## What is a Program? A **program** is a *sequence of instructions* that specifies how to perform a computation. There are a few basic kinds of instructions: - **input**: Get data from the keyboard, a file, the network, or some other device. - **output**: Display data on the screen, save it in a file, send it over the network, etc. - **math**: Perform basic mathematical operations like addition and multiplication. - **conditional execution**: Check for certain conditions and run the appropriate code. - **repetition**: Perform some action repeatedly. ??? Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no matter how complicated, is made up of instructions that look pretty much like these. So you can think of programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions. --- class: center, middle, inverse # Programming Languages --- ## Formal and Natural Languages Natural languages are the languages people speak, such as English, Spanish, and French. - Not designed by people - Evolved naturally Formal languages are languages that are designed by people for specific applications. - Mathematical notation is particularly good at denoting relationships among numbers and symbols. - Chemists use a formal language to represent the chemical structure of molecules. - Programming languages are formal languages that have been designed to express computations. --- ## Syntax Formal languages tend to have strict syntax rules that govern the structure of statements. - In mathematics the statement `3 + 3 = 6` has correct syntax, but `3+ = 3$6` does not. - In chemistry H2O is a syntactically correct formula, but 2Zz is not. **Tokens** are the basic elements of the language, such as words, numbers, and chemical elements. **Structure** pertains to the way tokens are combined. **Syntax rules** describe the tokens and structure of a language. This is @ well-structured Engli$h sentence with invalid t*kens in it. This sentence all valid tokens has, but invalid structure with. ??? One of the problems with 3+ = 3$6 is that $ is not a legal token in mathematics. Similarly, 2Zz is not legal because there is no element with the abbreviation Zz. The equation 3+ = 3 is illegal because even though + and = are legal tokens, you can’t have one right after the other. Similarly, in a chemical formula the subscript comes after the element name, not before. --- ## Differences between Formal and Natural Languages - Ambiguity: Natural languages are full of ambiguity, which people deal with by using contextual clues and other information. Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context. - Redundancy: In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise. - Literalness: Natural languages are full of idiom and metaphor. If I say, “The penny dropped”, there is probably no penny and nothing dropping (this idiom means that someone understood something after a period of confusion). Formal languages mean exactly what they say. The meaning of a computer program is unambiguous and literal, and can be understood entirely by analysis of the tokens and structure. But, because we all grow up speaking natural languages, it is sometimes hard to adjust to formal languages. ??? Formal languages are more dense than natural languages, so it takes longer to read them. Also, the structure is important, so it is not always best to read from top to bottom, left to right. Instead, learn to parse the program in your head, identifying the tokens and interpreting the structure. Finally, the details matter. Small errors in spelling and punctuation, which you can get away with in natural languages, can make a big difference in a formal language. --- class: center, middle, inverse # The Python Programming Language --- ## Why Python? --- ## Python Versions There are two versions of Python, called Python 2 and Python 3. They are very similar, so if you learn one, it is easy to switch to the other. This course uses Python 3. When you start the Python interpreter, you should see output like this: ```bash Python 3.4.0 (default, Jun 19 2015, 14:20:21) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` The first three lines contain information about the interpreter and the operating system it’s running on, so it might be different for you. But you should check that the version number, which is 3.4.0 in this example, begins with 3, which indicates that you are running Python 3. --- ## Hello, World! Try this Python program: ``` >>> print('Hello, World!') ``` This is an example of a print statement, although it doesn’t actually print anything on paper. It displays a result on the screen. In this case, the result is the words `Hello, World!` The quotation marks in the program mark the beginning and end of the text to be displayed; they don’t appear in the result. The parentheses indicate that print is a function. --- ## Arithmetic Operators Python provides operators, which are special symbols that represent computations like addition and multiplication. The operators +, -, *, and / perform addition, subtraction, multiplication, and division: ```python >>> 40 + 2 42 >>> 43 - 1 42 >>> 6 * 7 42 >>> 84 / 2 42.0 ``` You might wonder why the result is 42.0 instead of 42. We'll get to that. Finally, the operator ** performs exponentiation; that is, it raises a number to a power: ```python >>> 6**2 + 6 42 ``` --- ## Values and Types A **value** is one of the basic things a program works with, like a letter or a number. Some values we have seen so far are 2, 42.0, and 'Hello, World!'. These values belong to different types: 2 is an integer, 42.0 is a floating-point number, and 'Hello, World!' is a string, so-called because the letters it contains are strung together. If you are not sure what type a value has, the interpreter can tell you: ```python >>> type(2)
>>> type(42.0)
>>> type('Hello, World!')
``` What about values like '2' and '42.0'? They look like numbers, but they are in quotation marks like strings. ```python >>> type('2')
>>> type('42.0')
``` They’re strings. --- ## Debugging Programmers make mistakes. Programming errors are called **bugs** and the process of tracking them down is called **debugging**. Programming, and especially debugging, sometimes brings out strong emotions. If you are struggling with a difficult bug, you might feel angry, despondent, or embarrassed. I like to think of a bug as a puzzle to solve. The Python interpreter will often give you messages that hint at what the problem could be. A difficult puzzle is frustrating when you are stuck while trying to solve it, but oh so satisfying when you finally figure it out! --- class: center, middle, inverse # Vocabulary --- ## Vocabulary - **problem solving**: The process of formulating a problem, finding a solution, and expressing it. - **high-level language**: A programming language like Python that is designed to be easy for humans to read and write. - **low-level language**: A programming language that is designed to be easy for a computer to run; also called “machine language” or “assembly language”. - **interpreter**: A program that reads another program and executes it - **prompt**: Characters displayed by the interpreter to indicate that it is ready to take input from the user. - **program**: A set of instructions that specifies a computation. - **print statement**: An instruction that causes the Python interpreter to display a value on the screen. - **operator**: A special symbol that represents a simple computation like addition, multiplication, or string concatenation. - **value**: One of the basic units of data, like a number or string, that a program manipulates. --- ## Vocabulary - **type**: A category of values. The types we have seen so far are integers (type int), floatingpoint numbers (type float), and strings (type str). - **integer**: A type that represents whole numbers. - **floating-point**: A type that represents numbers with fractional parts. - **string**: A type that represents sequences of characters. - **natural language**: Any one of the languages that people speak that evolved naturally. - **formal language**: Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are formal languages. - **token**: One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language. - **syntax**: The rules that govern the structure of a program. - **parse**: To examine a program and analyze the syntactic structure. - **bug**: An error in a program. - **debugging**: The process of finding and correcting bugs.