Dialog with Python: Difference between revisions

From XPUB & Lens-Based wiki
(New page: This exercise is slightly modifed from the "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en <code> Type the following into the Python interpreter and record the...)
 
No edit summary
Line 1: Line 1:
This exercise is slightly modifed from the "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en
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


<code>
<code>
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 "&gt;&gt;&gt;" indicates the "prompt" (what Python prints to ask you to type something to it) -- so you don't type it!


>>> 1
&gt;&gt;&gt; 1
>>> -1
&gt;&gt;&gt; -1
>>> 1-
&gt;&gt;&gt; 1-
>>> 1 = 2
&gt;&gt;&gt; 1 = 2
>>> 1 == 2
&gt;&gt;&gt; 1 == 2
>>> 1 != 2
&gt;&gt;&gt; 1 != 2
>>> 1 < 2
&gt;&gt;&gt; 1 < 2
>>> 1 <=1
&gt;&gt;&gt; 1 <=1
>>> 1 > 2
&gt;&gt;&gt; 1 &gt; 2
>>> 1 <= 1
&gt;&gt;&gt; 1 <= 1
>>> 1 > 2
&gt;&gt;&gt; 1 &gt; 2
>>> 1 < 3 < 5
&gt;&gt;&gt; 1 < 3 < 5
>>> 1 < 3 and 3 < 5
&gt;&gt;&gt; 1 < 3 and 3 < 5
>>> 1 < 3 < 5
&gt;&gt;&gt; 1 < 3 < 5
>>> 1 < 3 and 3 < 2
&gt;&gt;&gt; 1 < 3 and 3 < 2
>>> 1 == 1 == 1
&gt;&gt;&gt; 1 == 1 == 1
>>> 1 * 2
&gt;&gt;&gt; 1 * 2
>>> 1 + 2
&gt;&gt;&gt; 1 + 2
>>> 1 / 2
&gt;&gt;&gt; 1 / 2
>>> 1 / 2.0
&gt;&gt;&gt; 1 / 2.0
>>> 1 // 2.0
&gt;&gt;&gt; 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.
Line 36: Line 38:
4/6 = is .6 with a remainder of 4
4/6 = is .6 with a remainder of 4


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

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

  1. 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 '.?!'