3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming
3-2 Data types (1) Programs input, store, manipulate, and output values or data. Values are classified according to their types and the operations that can be performed on them. E.g.: –It makes sense to subtract numbers but not booleans or strings. –It makes sense to concatenate strings but not subtract them.
3-3 Data types (2) Basic data types in Python: –integer numbers –floating-point numbers –booleans –strings … integers … floats False True booleans ‘’‘$’‘Hi’‘apple’ … strings values
3-4 Data types (3) Composite data types in Python: –tuples (§6) –lists (§7) –dictionaries (§9).
3-5 Integer numbers The integer numbers are positive and negative whole numbers: …, –3, –2, –1, 0, +1, +2, +3, …
3-6 Integer operators Integer operators: - y negation of y x + y sum of x and y x - y difference of x and y x * y product of x and y x // y quotient when x is divided by y x % y remainder when x is divided by y x ** y x raised to the power of y
3-7 Example: integer arithmetic This function uses integer remainders: def gcd (m, n): # Return the greatest common divisor of m and n. p = m q = n r = p % q # remainder on dividing p by q while r != 0: # i.e., p is not a multiple of q p = q q = r r = p % q return q
3-8 Floating-point numbers The floating-point numbers are positive and negative real numbers. Floating-point numbers are represented approximately in a computer (unlike integers, booleans, etc.).
3-9 Floating-point operators Floating-point operators: - y negation of y x + y sum of x and y x - y difference of x and y x * y product of x and y x / y division of x by y x ** y x raised to the power of y
3-10 Example: floating-point arithmetic (1) This function uses floating-point arithmetic: def square_root (x): # Return the square root of the positive number x. r = 1.0 while abs(x/r**2 – 1) > : r = 0.5 * (r + x/r) return r This function assumes that its argument is positive. –What will happen if its argument is negative?
3-11 Example: floating-point arithmetic (2) Tracing the function call square_root(2.0) : Enter the function: Test “ abs(…) > ”: Execute “ r = 1.0 ”: 2.0 x r 1.0 yields True Execute “ r = 0.5*(r+x/r) ”: Test “ abs(…) > ”: yields True Execute “ r = 0.5*(r+x/r) ”: Test “ abs(…) > ”: yields True
3-12 Example: floating-point arithmetic (3) Tracing the function call (continued): Execute “ r = 0.5*(r+x/r) ”: rx Test “ abs(…) > ”: Execute “ return r ”: yields False returns
3-13 Floating-point approximation Floating-point numbers are represented in the form: ± m x 2 ±e where the mantissa m is a binary fraction (½ ≤ m < 1) and the exponent e is a small binary integer. Most real numbers (including all irrational numbers) can only be approximated in a computer. It follows that floating-point computations are only approximate – beware!
3-14 Example: floating-point approximation Consider the expression: – 1.0 On my computer, this expression yields ! The problem is that the number 0.2, although it can be written exactly as a decimal fraction, cannot be represented exactly as a binary fraction.
3-15 Boolean values and operators The boolean values are False and True. Boolean operators: not y negation of y (i.e., True iff y is False) x and y conjunction of x and y (i.e., True iff both x and y are True) x or y disjunction of x and y (i.e., True iff either x or y is True)
3-16 Comparison operators Comparison operators: x == y True iff x is equal to y x != y True iff x is unequal to y x < y True iff x is less than y x <= y True iff x is less than or equal to y x > y True iff x is greater than y x >= y True iff x is greater than or equal to y Comparison chaining: x < y < z True iff x is less than y and y is less than z etc.
3-17 Example: booleans Using boolean and comparison operations: def in_range (n, p, q): # Return True iff n is in the range p … q. return (p <= n and n <= q) Alternatively, using comparison chaining: def in_range (n, p, q): # Return True iff n is in the range p … q. return (p <= n <= q) Function call: d = input('Enter integer in range 0-9: ') if not in_range(d, 0, 9): print 'Invalid integer'
3-18 String values A string is a sequence of characters. The length of a string is the number of characters in it. The empty string has length 0 (i.e., it consists of no characters at all). The string values are character sequences of any length.
3-19 String operators String operators: s + t concatenation of strings s and t n * s concatenation of n copies of s s * n ditto
3-20 String comparison operators String comparison operators: s == t True iff s is equal to t s != t True iff s is unequal to t s < t True iff s is lexicographically less than t s <= t True iff s is lexicographically less than or equal to t s > t True iff s is lexicographically greater than t s >= t True iff s is lexicographically greater than or equal to t Comparison chaining: as before.
3-21 Example: string operations Program: place = 'Paris' season = 'spring' title = place + ' in ' + 2*'the ' + season print title Output: Paris in the the spring
3-22 Example: string comparisons (1) Program: word1 = raw_input('Enter a word: ') word2 = raw_input('Enter another word: ') if word1 > word2: # Swap word1 and word2 … word1, word2 = word2, word1 print 'Words in lexicographic order:' print word1 print word2
3-23 Example: string comparisons (2) Program’s output and input: Enter a word: mouse Enter another word: elephant Words in lexicographic order: elephant mouse