Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53) [GCC 4.0.1 (Apple Computer, Inc. build 5363)] Type "help", "copyright", "credits" or "license" for more information. >>> # and - true if both are true; false, otherwise >>> x = 5 >>> y = 44 >>> x == 5 and y == 44 True >>> x == 5 and y == 440 False >>> x == 50 and y == 44 False >>> #or >>> >>> tall = True >>> short = False >>> woman = True >>> man = False >>> tall or short True >>> man or woman True >>> tall or woman True >>> short or man # only if both are False is the result false False >>> # we can combine these as many times as we want >>> (False or True) and (False or True) True >>> (False and False) or (False or True) True >>> #Boolean is stroed internally as 0 and True as 1, with interesting effects >>> if 9.23345: ... print 'yes' ... yes >>> # for fun what does the following do? NOTE: Terrible style! >>> num = 6 >>> if num == 10 or 20 or 30: ... print 'yeah' ... else: ... print 'no way' ... yeah >>> # num is 6; 6 is not zero, so True >>> # 10 or 20 or 30 is True or True or True >>> # So, the test is True == True, which is True >>> # But don't program like this! >>> >>> #lazy evaluation: python does not evaluate something it doesn't need to >>> # x and y : if x is False, we know the answer is False. No need to evaluate y >>> # x or y: if x is True, we know the answer is True. No need to evaluate y >>> # To see this, note that 4/0 would give an error >>> 4/0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> True or 4/0 True >>> # The example above shows Python did not evaluation 4/0 >>> 4/0 or True Traceback (most recent call last): File "", line 1, in ZeroDivisionError: integer division or modulo by zero >>> # Now, let's move onto strings >>> 'cs401' 'cs401' >>> "cs401" # use double or single quotes 'cs401' >>> "cs401' # but you can't mix them Traceback (most recent call last): File "", line 1, in EOL while scanning single-quoted string: , line 1, pos 32 >>> # + concatenates 2 strings - glues them together >>> 'cs' + '0007' 'cs0007' >>> 'cs' + '401' 'cs401' >>> 'cs' + 401 # error! the operands need to be the same type Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects >>> 'cs' + str(401) # change the int to a str, and things are fine 'cs401' >>> int(25.4) 25 >>> float('45') # python will make sense of this if it can. 45.0 >>> # It first went from str to int; then from int to float. >>> # * is also defined for strings >>> "Tigers are " + "fun" * 4 'Tigers are funfunfunfun' >>> # another type of boolean expression: uses the 'in' keyword >>> 's' in 'pragmatics' True >>> #True, because the char 's' is in the string 'pragmatics' >>> # Now, write the code in [string_funs.py] >>>