COMPSCI 1MD3 Lecture Notes - Lecture 4: Reserved Word, Modulo Operation
Document Summary
In python, variables are the names of objects. Variables can contain uppercase and lowercase letters, digits, and underscores. Variables must not start with a digit and must not be a reserved word such as def, if, while, return. Python uses indentation for bracketing of nested statements. Variables used within a function are local to the function def factorial(n): i = 0 f = 1 while i < n: i = i+1 f = f*(i+1) return f. All the expressions on the right are evaluated before the values are simultaneously assigned to variables on the left: x, y = y, x (this swaps the values for x and y) Caution: if you try to do this one at a time, it will not work: x = y y = x (both y and x will be the initial value of x) Any function that needs to be iterated uses a counter variable and an accumulator variable.