Dialog with Python

From XPUB & Lens-Based wiki
Revision as of 14:08, 7 October 2008 by Michael Murtaugh (talk | contribs)

This exercise is slightly modifed from the "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en

  1. based on exercise from "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en

# based on exercise from "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en

Type the following into the Python interpreter and record the results & any questions / comments you have. NB: the ">>>" indicates the "prompt" (what Python prints to ask you to type something to it) -- so you don't type it!

>>> 1 >>> -1 >>> 1- >>> 1 = 2 >>> 1 == 2 >>> 1 != 2 >>> 1 < 2 >>> 1 <=1 >>> 1 > 2 >>> 1 <= 1 >>> 1 > 2 >>> 1 < 3 < 5 >>> 1 < 3 and 3 < 5 >>> 1 < 3 < 5 >>> 1 < 3 and 3 < 2 >>> 1 == 1 == 1 >>> 1 * 2 >>> 1 + 2 >>> 1 / 2 >>> 1 / 2.0 >>> 1 // 2.0

the % sign is the mod operator. It gives the remainder involved.

so, a % b = remainder of a/b such that

if a=6 and b=4 then remainder = 2 if a=4 and b=6 then remainder = 4 this is because 6/4 = is 1 with a remainder of 2 4/6 = is .6 with a remainder of 4

>>> 9 % 2 >>> 10 % 3 >>> int >>> int(2) >>> int(2.0) >>> int(2.1) >>> int(2.9) >>> int("2.0") >>> int("four") >>> long(4) >>> 4 == 4L >>> float(2) >>> float('2') >>> float('2.9') >>> 1/0 >>> 1 + 1.0 >>> 1 + 1L >>> 1.0 + 1L >>> 1<2 and 2<3 >>> 1<2 and 2<3 >>> 1<2 and not (2<3) >>> 1<2 and True >>> 1<2 and False >>> 2 & 4 >>> 2 | 4 >>> ~2 >>> 2 << 1 >>> 2 << 2 >>> abs(4) >>> abs(-4) >>> pow(2,8) >>> 2**8 >>> 2 ** 16 >>> 2 ** 32 >>> 2 ** 31 >>> int ( 2 ** 31-1) >>> int( 2 ** 31-1) + 1 >>> round(1.01) >>> round(1.99) >>> round(1.50) >>> round (1.59) >>> 1/3.0 >>> third = 1/3.0 >>> round(third) >>> round(third, 1) >>> round(third, 2) >>> round(third, 3) >>> round(1234.56, -1) >>> round(1234.56, -2) >>> round(1234.56, -3) >>> type(int) >>> callable(int) >>> int() >>> 0 == int() >>> 0 is int() >>> type(int) >>> type(int()) >>> int(4,3) >>> int('4') >>> int("four") >>> int('z') >>> int('c', 16) >>> int("101", 2) >>> 'hello' >>> "hello" >>> 'bob's your uncle' >>> 'bob\'s your uncle' >>> "bob's your uncle" >>> 'A quote (") mark' >>> bob's your "uncle" >>> 'hello >>> """hello >>> 'hello\nthere\n\n' >>> 'h' in 'hello' >>> 'h' not in 'hello' >>> 'hello'[0] >>> s = 'hello' >>> s[0] = 'j' >>> r'hello' >>> r'hello' is 'hello' >>> r'hello\n' >>> r'hello\n' == 'hello\n' >>> len(r'hello\n') >>> len('hello\n') >>> 2 * 'hello' >>> 2 + 'hello' >>> '2' + 'hello' >>> u'hello' >>> type(u'hello') >>> len('hello') >>> min('hello') >>> max('hello') >>> sorted('hello') >>> 'hello'.startswith('h') >>> 'hello'.startswith('he') >>> 'hello'.startswith('lo') >>> 'hello'.endswith('lo') >>> ' hello '.strip() >>> ' hello '.rstrip() >>> ' hello '.lstrip() >>> sorted('hello') >>> sorted('hello', reverse= True) >>> reversed('hello') >>> list(reversed('hello')) >>> 'hello'.upper() >>> 'HELLO'.isupper() >>> 'hello'.title() >>> 'Hello'.istitle() >>> 'hello world'.title() >>> 'hello world'.title().swapcase() >>> '!' in '.?!'