## Fundamentals of Programming I: Statements and Expressions. # Program code in a language such a Python consists of *statements* or # *commands*. A statement/command is a directive for the computer to perform # some action. In order to form statements, however, we also need *expressions* # or *values*. For example: print "hello" # This line is a statement that does something obvious. Here, "hello" is # an expression. The difference between statements and expressions is # analogous to the difference between sentences and nouns in natural # language. In the context of this analogy *print* would be the verb. x = 3+1 # assigns 4 to memory location associated with x, prints nothing # This is another statement: it instructs the system to calculate the # value 4 and *assign* it to a variable named x; in other words to store # 4 into a certain memory location it will associate with x. In this # statement the expressions are x and 3+1. Note that 3+1 by itself is # not a statement. It does calculate 4, but 4 is also not a statement: # we need to put this *value* into something else in order to make it a # statement. Please note that this statement prints nothing, even in the # interactive interpreter. ## Expressions/values come in many different forms or *types*. Understanding # the exact type of an expression is very important. Furthermore, each # type of expression can be divided into primitive and compound expressions. # I list the principal types that you need to know, along with examples: ## 0. Integer expressions: 3, 3+4, 3-2*5, etc. Here, 3 is primitive while # the rest are compound. A particularly important kind of integer expression # is formed with the % operator (called the mod operator). 5%2 returns the # remainder after dividing 5 by 2. The expression 5/2 returns 2, the quotient # without the remainder after dividing 5 by 2. ## 1. Floating point expressions: 3.14, 2.0, etc.. # These are numbers with decimal points, and are often called "doubles". # Note carefully that 2.0 is not the same as 2 (why?). Similarly, # the compound expression 1.0/2.0 is not the same as 1/2. 1/2 is an # integer value (0), while 1.0/2.0 is 0.5. # To convert a float into an int, use the following kinds of expressions: print 1/int(2.0) print 1/float(2) # Here, int(2.0) is an operation that converts a float to an int. int(2.0) # has the same value as the integer 2, and analogously for float(2). print int(3.9) # prints 3 - the decimal part is discarded. print int(3.9+0.5) # prints 4 - always add .5 if you want to round off. ## 2. String expressions: "hello world", "12", "faks3r3**(()", "" # Strings are just sequences of zero or more characters that are taken # as-is. Thus do not confuse "12" with the integer 12. "12" is just a # sequence of two characters. If you type "12"+3, do not expect 15! # However, there are also operations to convert strings to and from other # types. Try to generalize the operations demonstrated as follows: print "12"+str(3) print int("12")+3 # as you can see, when + is used as an operator between two strings, it # *concatenates* them. # One particularly important string is the empty string "". Do not confuse # the empty string with " ", which is a string that consists of one character: # the space character. # In Python, sometimes strings are displayed using single quotes: 'abc'. # What if you want the character " in a string? do this: print "say \"hello\" to him" # OK that was easy, but what if you want to include the \ character as well? print "who needs \\ in a string anyway" # \ is called the "escape character." ### 3. Variables. # variables are important kinds of expressions. The type of a variable is # the same as the type of value that it's assigned to. x = "abc" # x is now of type string print x x = 14 # x is now of type int print x+1 # x was first assigned to a string, and therefore can be used as a string # expression until it is assigned to something of different type. ## 4. Boolean Expressions. # This is a fundamentally important type of values in any programming # language, though a little harder to understand than numbers and strings. # A boolean value is either True or False (capitalized T and F). # All compound boolean expressions eventually evaluate to one of these # two values. Here are some sample boolean expressions and what they # evaluate to: 3<4 # True 4>4 # False 4>=4 # True: >= means greater than or equal to 3+1 == 4 # True: note that "==" means "boolean equality", which is not # the same as "=", which is used to form an assignment statement. "abc" != "ABC" # True: Python is case sensitive, and != means "not equal" # Compound boolean expressions are formed as follows 3<4 and 3>0 # True because both are true 3<4 or 3>4 # True or means either or BOTH is true. 1<2 or 2<3 # True not(1<2) # False: because 1<2 is True. not inverts the boolean value. # As another example, assume that x and y are two integer variables, then # the boolean expression x != y is equivalent to not(x==y), which is also # equivalent to (x0 and x<=10). ### When do you need spaces? Expressions such as x<=0 and x <= 0 are the # same. But do not use x < = 0: <= is one symbol. Usually symbols are # divided into "alphanumeric" and non-alphanumeric. An alphanumeric term # starts with a letter (upper or lower case), and is followed by zero or # more letters and digits. For example, x2ab3d is alphanumeric, and can # be used as the name of a variable. Non-alphanumeric symbols include # ==, =, <=, %, etc. A number like 24 is also non-alphanumeric because # it does not start with a letter. In general, use spaces between different # alphanumeric symbols. For example, what's wrong with 010 ? Spaces # are needed to separate the alphanumeric symbols 'x', 'or', and 'y'. So # this should be written as 010. However, there does not need to # be spaces between alphanumeric and non-alphanumeric symbols: 0