Unit 2ExpressionsVariablesTypesString MultiplicationString ConcatenationThe for Loopfor Loop VariationsNested LoopsExerciseExercise SolutionConcatenating RangesExercise Solution 2ConstantsExercise Solution 3Unit 2Expressions and variables; for loopsSpecial thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work.Except where otherwise noted, this work is licensed under:http://creativecommons.org/licenses/by-nc-sa/3.02Expressions•Arithmetic is very similar to JavaOperators: + - * / % (and ** for exponentiation)Precedence: () then ** then * / % then + -Integers vs. real numbers>>> 1 + 12>>> 1 + 3 * 4 - 211>>> 7 / 23>>> 7.0 / 23.5>>> 10 ** 610000003Variables•Declaringno type is written; same syntax as assignment•Operatorsno ++ or -- operators (must manually adjust by 1)Java Pythonint x = 2;x++;System.out.println(x);x = x * 8;System.out.println(x);double d = 3.2;d = d / 2;System.out.println(d);x = 2x = x + 1print(x)x = x * 8print(x)d = 3.2d = d / 2print(d)4Types•Python is looser about types than JavaVariables' types do not need to be declaredVariables can change types as a program is runningValue Java typePython type42 int int3.14 double float"ni!" String str5String Multiplication•Python strings can be multiplied by an integer.The result is many copies of the string concatenated together.>>> "hello" * 3"hellohellohello">>> print(10 * "yo ")yo yo yo yo yo yo yo yo yo yo >>> print(2 * 3 * "4")4444446String Concatenation•Integers and strings cannot be concatenated in Python.Workarounds:str(value) - converts a value into a stringprint(expr, expr) - prints two items on the same line>>> x = 4>>> print("Thou shalt not count to " + x + ".")TypeError: cannot concatenate 'str' and 'int' objects>>> print("Thou shalt not count to " + str(x) + ".")Thou shalt not count to 4.>>> print(x + 1, "is out of the question.")5 is out of the question.7The for Loopfor name in range(max): statementsRepeats for values 0 (inclusive) to max (exclusive)>>> for i in range(5):... print(i)012348for Loop Variationsfor name in range(min, max): statementsfor name in range(min, max, step): statementsCan specify a minimum other than 0, and a step other than 1>>> for i in range(2, 6):... print(i)2345>>> for i in range(15, 0, -5):... print(i)151059Nested Loops•Nested loops are often replaced by string * and +....1...2..3.45Java123456for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (5 - line); j++) { System.out.print("."); } System.out.println(line);}Python12for line in range(1, 6): print((5 - line) * "." + str(line))10Exercise•Rewrite the Mirror lecture program in Python. Its output:#================#| <><> || <>....<> || <>........<> ||<>............<>||<>............<>|| <>........<> || <>....<> || <><> |#================#11Exercise Solutiondef bar(): print "#" + 16 * "=" + "#"def top(): for line in range(1, 5): # split a long line by ending it with \ print "|" + (-2 * line + 8) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 8) * " " + "|"def bottom(): for line in range(4, 0, -1): print "|" + (-2 * line + 8) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 8) * " " + "|"# mainbar()top()bottom()bar()12Concatenating Ranges•Ranges can be concatenated with +Can be used to loop over a disjoint range of numbers>>> range(1, 5) + range(10, 15)[1, 2, 3, 4, 10, 11, 12, 13, 14]>>> for i in range(4) + range(10, 7, -1):... print(i)0123109813Exercise Solution 2def bar(): print "#" + 16 * "=" + "#"def mirror(): for line in range(1, 5) + range(4, 0, -1): print "|" + (-2 * line + 8) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 8) * " " + "|"# mainbar()mirror()bar()14Constants•Python doesn't really have constants.Instead, declare a "global" variable at the top of your code.All methods will be able to use this value.constant.py12345678910111213MAX_VALUE = 3def printTop(): for i in range(MAX_VALUE): for j in range(i): print(j) print()def printBottom(): for i in range(MAX_VALUE, 0, -1): for j in range(i, 0, -1): print(MAX_VALUE) print()15Exercise Solution 3SIZE = 4def bar(): print "#" + 4 * SIZE * "=" + "#"def mirror(): for line in range(1, SIZE + 1) + range(SIZE, 0, -1): print "|" + (-2 * line + 2 * SIZE) * " " + \ "<>" + (4 * line - 4) * "." + "<>" + \ (-2 * line + 2 * SIZE) * " " + "|"#
View Full Document