Dialog with Python: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
# 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 | ||
# 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! | |||
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- | >>> -1 | ||
>>> 1 = 2 | >>> 1- | ||
>>> 1 == 2 | >>> 1 = 2 | ||
>>> 1 != 2 | >>> 1 == 2 | ||
>>> 1 < 2 | >>> 1 != 2 | ||
>>> 1 <=1 | >>> 1 < 2 | ||
>>> 1 > 2 | >>> 1 <=1 | ||
>>> 1 <= 1 | >>> 1 > 2 | ||
>>> 1 > 2 | >>> 1 <= 1 | ||
>>> 1 < 3 < 5 | >>> 1 > 2 | ||
>>> 1 < 3 and 3 < 5 | >>> 1 < 3 < 5 | ||
>>> 1 < 3 < 5 | >>> 1 < 3 and 3 < 5 | ||
>>> 1 < 3 and 3 < 2 | >>> 1 < 3 < 5 | ||
>>> 1 == 1 == 1 | >>> 1 < 3 and 3 < 2 | ||
>>> 1 * 2 | >>> 1 == 1 == 1 | ||
>>> 1 + 2 | >>> 1 * 2 | ||
>>> 1 / 2 | >>> 1 + 2 | ||
>>> 1 / 2.0 | >>> 1 / 2 | ||
>>> 1 // 2.0 | >>> 1 / 2.0 | ||
>>> 1 // 2.0 | |||
the % sign is the mod operator. It gives the remainder involved. | |||
the % sign is the mod operator. It gives the remainder involved. | |||
so, a % b = remainder of a/b such that | |||
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 | if a=6 and b=4 then remainder = 2 | ||
this is because | if a=4 and b=6 then remainder = 4 | ||
6/4 = is 1 with a remainder of 2 | this is because | ||
4/6 = is .6 with a remainder of 4 | 6/4 = is 1 with a remainder of 2 | ||
4/6 = is .6 with a remainder of 4 | |||
>>> 9 % 2 | |||
>>> 10 % 3 | >>> 9 % 2 | ||
>>> int | >>> 10 % 3 | ||
>>> int(2) | >>> int | ||
>>> int(2.0) | >>> int(2) | ||
>>> int(2.1) | >>> int(2.0) | ||
>>> int(2.9) | >>> int(2.1) | ||
>>> int("2.0") | >>> int(2.9) | ||
>>> int("four") | >>> int("2.0") | ||
>>> long(4) | >>> int("four") | ||
>>> 4 == 4L | >>> long(4) | ||
>>> float(2) | >>> 4 == 4L | ||
>>> float('2') | >>> float(2) | ||
>>> float('2.9') | >>> float('2') | ||
>>> 1/0 | >>> float('2.9') | ||
>>> 1 + 1.0 | >>> 1/0 | ||
>>> 1 + 1L | >>> 1 + 1.0 | ||
>>> 1.0 + 1L | >>> 1 + 1L | ||
>>> 1<2 and 2<3 | >>> 1.0 + 1L | ||
>>> 1<2 and 2<3 | >>> 1<2 and 2<3 | ||
>>> 1<2 and not (2<3) | >>> 1<2 and 2<3 | ||
>>> 1<2 and True | >>> 1<2 and not (2<3) | ||
>>> 1<2 and False | >>> 1<2 and True | ||
>>> 2 & 4 | >>> 1<2 and False | ||
>>> 2 | 4 | >>> 2 & 4 | ||
>>> ~2 | >>> 2 | 4 | ||
>>> 2 << 1 | >>> ~2 | ||
>>> 2 << 2 | >>> 2 << 1 | ||
>>> abs(4) | >>> 2 << 2 | ||
>>> abs(-4) | >>> abs(4) | ||
>>> pow(2,8) | >>> abs(-4) | ||
>>> 2**8 | >>> pow(2,8) | ||
>>> 2 ** 16 | >>> 2**8 | ||
>>> 2 ** 32 | >>> 2 ** 16 | ||
>>> 2 ** 31 | >>> 2 ** 32 | ||
>>> int ( 2 ** 31-1) | >>> 2 ** 31 | ||
>>> int( 2 ** 31-1) + 1 | >>> int ( 2 ** 31-1) | ||
>>> round(1.01) | >>> int( 2 ** 31-1) + 1 | ||
>>> round(1.99) | >>> round(1.01) | ||
>>> round(1.50) | >>> round(1.99) | ||
>>> round (1.59) | >>> round(1.50) | ||
>>> 1/3.0 | >>> round (1.59) | ||
>>> third = 1/3.0 | >>> 1/3.0 | ||
>>> round(third) | >>> third = 1/3.0 | ||
>>> round(third, 1) | >>> round(third) | ||
>>> round(third, 2) | >>> round(third, 1) | ||
>>> round(third, 3) | >>> round(third, 2) | ||
>>> round(1234.56, -1) | >>> round(third, 3) | ||
>>> round(1234.56, -2) | >>> round(1234.56, -1) | ||
>>> round(1234.56, -3) | >>> round(1234.56, -2) | ||
>>> type(int) | >>> round(1234.56, -3) | ||
>>> callable(int) | >>> type(int) | ||
>>> int() | >>> callable(int) | ||
>>> 0 == int() | >>> int() | ||
>>> 0 is int() | >>> 0 == int() | ||
>>> type(int) | >>> 0 is int() | ||
>>> type(int()) | >>> type(int) | ||
>>> int(4,3) | >>> type(int()) | ||
>>> int('4') | >>> int(4,3) | ||
>>> int("four") | >>> int('4') | ||
>>> int('z') | >>> int("four") | ||
>>> int('c', 16) | >>> int('z') | ||
>>> int("101", 2) | >>> int('c', 16) | ||
>>> 'hello' | >>> int("101", 2) | ||
>>> "hello" | >>> 'hello' | ||
>>> 'bob's your uncle' | >>> "hello" | ||
>>> 'bob\'s your uncle' | >>> 'bob's your uncle' | ||
>>> "bob's your uncle" | >>> 'bob\'s your uncle' | ||
>>> 'A quote (") mark' | >>> "bob's your uncle" | ||
>>> '''bob's your "uncle"''' | >>> 'A quote (") mark' | ||
>>> 'hello | >>> '''bob's your "uncle"''' | ||
>>> """hello | >>> 'hello | ||
>>> 'hello\nthere\n\n' | >>> """hello | ||
>>> 'h' in 'hello' | >>> 'hello\nthere\n\n' | ||
>>> 'h' not in 'hello' | >>> 'h' in 'hello' | ||
>>> 'hello'[0] | >>> 'h' not in 'hello' | ||
>>> s = 'hello' | >>> 'hello'[0] | ||
>>> s[0] = 'j' | >>> s = 'hello' | ||
>>> r'hello' | >>> s[0] = 'j' | ||
>>> r'hello' is 'hello' | >>> r'hello' | ||
>>> r'hello\n' | >>> r'hello' is 'hello' | ||
>>> r'hello\n' == 'hello\n' | >>> r'hello\n' | ||
>>> len(r'hello\n') | >>> r'hello\n' == 'hello\n' | ||
>>> len('hello\n') | >>> len(r'hello\n') | ||
>>> 2 * 'hello' | >>> len('hello\n') | ||
>>> 2 + 'hello' | >>> 2 * 'hello' | ||
>>> '2' + 'hello' | >>> 2 + 'hello' | ||
>>> u'hello' | >>> '2' + 'hello' | ||
>>> type(u'hello') | >>> u'hello' | ||
>>> len('hello') | >>> type(u'hello') | ||
>>> min('hello') | >>> len('hello') | ||
>>> max('hello') | >>> min('hello') | ||
>>> sorted('hello') | >>> max('hello') | ||
>>> 'hello'.startswith('h') | >>> sorted('hello') | ||
>>> 'hello'.startswith('he') | >>> 'hello'.startswith('h') | ||
>>> 'hello'.startswith('lo') | >>> 'hello'.startswith('he') | ||
>>> 'hello'.endswith('lo') | >>> 'hello'.startswith('lo') | ||
>>> ' hello '.strip() | >>> 'hello'.endswith('lo') | ||
>>> ' hello '.rstrip() | >>> ' hello '.strip() | ||
>>> ' hello '.lstrip() | >>> ' hello '.rstrip() | ||
>>> sorted('hello') | >>> ' hello '.lstrip() | ||
>>> sorted('hello', reverse= True) | >>> sorted('hello') | ||
>>> reversed('hello') | >>> sorted('hello', reverse= True) | ||
>>> list(reversed('hello')) | >>> reversed('hello') | ||
>>> 'hello'.upper() | >>> list(reversed('hello')) | ||
>>> 'HELLO'.isupper() | >>> 'hello'.upper() | ||
>>> 'hello'.title() | >>> 'HELLO'.isupper() | ||
>>> 'Hello'.istitle() | >>> 'hello'.title() | ||
>>> 'hello world'.title() | >>> 'Hello'.istitle() | ||
>>> 'hello world'.title().swapcase() | >>> 'hello world'.title() | ||
>>> '!' in '.?!' | >>> 'hello world'.title().swapcase() | ||
>>> '!' in '.?!' |
Revision as of 13:08, 7 October 2008
This exercise is slightly modifed from the "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
# 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 '.?!'