CMIS 102 syntax, identifiers, variables, comments, sequence, type(). We start learning some of the copious detail that all programming languages have. Entering (typing), editing, and running a program in IDLE: --make a New File, or Open an existing .py file --type/edit Save (ctrl-s) --Run | Run Module or F5 Loop: "figure out error, try to fix it". It's because programming is hard. It's not easy to create exactly correct detailed instructions of an abstract data-processing procedure in a formal language. "Syntax" is grammar, the rules that specify the allowable forms that a program and its parts may have. The interpreter enforces the syntax rules 100%. You can't successfully run a program that has syntax errors. Every program has to be syntactically perfect. There are lots of syntax rules and it seems difficult to learn them at first but syntax is the easy part of programming, just like it's the easy part of human language. It's doing something with the language that is hard: creating a program that does what it's supposed to do correctly (or writing a novel, a research paper, a song, a recipe, a contract in human language). "Identifiers" are the names of variables that you create. The rules for a valid identifier are easy: they consist of letters, digits and _ but cannot start with a digit. These rules are insisted upon by the interpreter, so you can't disobey them. Also, Python is case-sensitive (upper and lowercase letters are different) but don't have identifiers that differ only in their case as that is error-prone. You should be vaguely familiar with the list of "keywords" (reserved words) which are the built-in vocabulary of Python, so that you understand why the interpreter says it's an error to try use int, or while, or in, etc. as an identifier. IDLE shows keywords in orange: False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield best_score bestscore bestScore 3 different names. Underscore is best style. if If IF 3 different identifiers. Only if is a keyword, avoid the others. Interpreter knows nothing about English or the world. It only knows what you tell it to do, it doesn't know your purpose or your intention. IDLE purple words are the names of built-in functions and shouldn't be used for your own identifiers. (your use will override the meaning of these functions.) It's important to choose names that are meaningful and that indicate purpose, so that human readers, including you, of the program can understand it more easily. The computer is oblivious of any meaning of a name; it knows nothing about the world or human language, it has no common sense, it's a mindless machine; it has no idea what your intention or purpose is, it only knows what you've told it to do. The computer will carry out ("execute") your program. Always use names that are as meaningful as possible to aid human understanding of the program. Program as communication between human and computer and between humans, and mostly to yourself. One of the things identifiers are used for is to give names to "variables". A variable stores/holds a datum, a piece of information. Data that is input to the program is held in variables. A variable can also be used to store the value of an evaluated expression so that the expression does not need to be evaluated/calculated more than once. The "type" of data is a very important concept in computing. We'll only be using the simple types (the others you learn in 141 and 242). "int" is the basic integer number type. int is short for integer. Many of the keywords in Python are abbreviations (saves typing, or card punching in the really old days). ints are for integers. Numbers with decimal point (real numbers in math) are called floating-point numbers in computing because of how they're stored in the machine. Python has the "float" type (remember, saves typing). So cool numbers like 3.141592 and 2.718281828 are floats. And so is 5.0. 5 is the int five. There are these two kinds of numbers in Python because the machine, specifically the CPU which does all the computation, has separate circuitry for integers and floating point values. Also because integers and real numbers are used for different purposes in the world, the former for counting things, the latter for measuring. And math makes the distinction, too. Everyone knows computers are used for calculating, therefore they have numbers, therefore a programming language needs a way to store and manipulate numbers, hence int and float. But computers are also used for textual manipulation. Letters and punctuation characters need to be represented. Names of people, places, things, events. Descriptions, IDs etc. Python has the "str" (short for string) type for this purpose. To specify a string value in a program, the string is surrounded by a pair of apostrophes (known as single quotes to the computing cognoscenti), or a pair of quotation marks (known as double quotes). 'Alfred', '123 Main Street', and "yabba dabba doo" are strings. 'A' is a different string from 'a'. '5' is the string with the five digit, it is not the int 5 nor the float 5.0. Now we've got identifiers and data types. The time has come to join them together by creating variables of specific types. A variable can be thought of as a little box with a name. It's actually a location in memory. The box can contain ONE value. To both create the variable and get a value into the variable, make a statement that has an identifier on the left side, a =, then an integer value on the right side: my_var = 5 This variable comes into existence at this point in the execution of the program. Its value is 5. The type of the datum is int. A variable's value can vary, that's why it's called a variable. If you have a variable you can assign a value to it. The = does not mean "equal". That was math, this is computer science. = is an action: it takes whatever is on the right side of it and makes that the value of the variable on its left side. (A left-pointing arrow would be a better symbol for assignment, but such a character still does not exist on keyboards.) my_var = 23 Whatever value my_var has before is gone, obliterated, lost, its new value after the execution of this statement is 23. All data in a program is stored in variables. This is the data that will be processed by the program, which is what the computer does. All the data in all the computers in all the world is stored in variables right now. x = y = z = 0 #assign a value to many variables in one statement print(x,y,z) x = x + 1 If x's value before the statement is 5, then the right side of the = is evaluated, it's 6 and that value is assigned to the variable x (sort of just coincidental that it's used on the right side). x's value after the statement is 6. *************************************** COMMENTS Python has two ways to indicate comments. Comments are to explain non-obvious or complicated stuff, assumptions. Notes, commentary, annotations to yourself and others. They are ignored by the interpreter; as if they don't exist. 1. #blabityblabblab-to-the-end-of-line 2. """ start of a multiline comment. Actually a multiline string, but if not assigned to a variable can be used as a commenting technique """ *************************************** SEQUENCE Statements are executed in the sequence in which they appear in the program (this will be modified when we learn selection and iteration). x = 2 y = 3 z = x + y #z is 5 print("z:",z) x = 100 #does not change value of z print("z:",z) *************************************** TYPE() The type() function returns the data type of its argument. print(type(123)) print(type(34.12)) print(type("hello")) a = 234 print(type(a)) a = 345.123 #can change the type of data of a variable, but rarely do print(type(a)) a = "asdfasdf" print(type(a)) Useful when dealing with more complex types.