Dialog with Python: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
This exercise is slightly modifed from the "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en
The exercise can be found here:
[[media:PythonDialog.txt]]


# based on exercise from "python crash course" http://groups.google.com/group/pythoncrashclass?hl=en
This exercise is slightly a modifed exercise from this [http://groups.google.com/group/pythoncrashclass?hl=en "python crash course"].
 
<code>
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!
 
&gt;&gt;&gt; 1
&gt;&gt;&gt; -1
&gt;&gt;&gt; 1-
&gt;&gt;&gt; 1 = 2
&gt;&gt;&gt; 1 == 2
&gt;&gt;&gt; 1 != 2
&gt;&gt;&gt; 1 < 2
&gt;&gt;&gt; 1 <=1
&gt;&gt;&gt; 1 &gt; 2
&gt;&gt;&gt; 1 <= 1
&gt;&gt;&gt; 1 &gt; 2
&gt;&gt;&gt; 1 < 3 < 5
&gt;&gt;&gt; 1 < 3 and 3 < 5
&gt;&gt;&gt; 1 < 3 < 5
&gt;&gt;&gt; 1 < 3 and 3 < 2
&gt;&gt;&gt; 1 == 1 == 1
&gt;&gt;&gt; 1 * 2
&gt;&gt;&gt; 1 + 2
&gt;&gt;&gt; 1 / 2
&gt;&gt;&gt; 1 / 2.0
&gt;&gt;&gt; 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
 
&gt;&gt;&gt; 9 % 2
&gt;&gt;&gt; 10 % 3
&gt;&gt;&gt; int
&gt;&gt;&gt; int(2)
&gt;&gt;&gt; int(2.0)
&gt;&gt;&gt; int(2.1)
&gt;&gt;&gt; int(2.9)
&gt;&gt;&gt; int("2.0")
&gt;&gt;&gt; int("four")
&gt;&gt;&gt; long(4)
&gt;&gt;&gt; 4 == 4L
&gt;&gt;&gt; float(2)
&gt;&gt;&gt; float('2')
&gt;&gt;&gt; float('2.9')
&gt;&gt;&gt; 1/0
&gt;&gt;&gt; 1 + 1.0
&gt;&gt;&gt; 1 + 1L
&gt;&gt;&gt; 1.0 + 1L
&gt;&gt;&gt; 1<2 and 2<3
&gt;&gt;&gt; 1<2 and 2<3
&gt;&gt;&gt; 1<2 and not (2<3)
&gt;&gt;&gt; 1<2 and True
&gt;&gt;&gt; 1<2 and False
&gt;&gt;&gt; 2 & 4
&gt;&gt;&gt; 2 | 4
&gt;&gt;&gt; ~2
&gt;&gt;&gt; 2 << 1
&gt;&gt;&gt; 2 << 2
&gt;&gt;&gt; abs(4)
&gt;&gt;&gt; abs(-4)
&gt;&gt;&gt; pow(2,8)
&gt;&gt;&gt; 2**8
&gt;&gt;&gt; 2 ** 16
&gt;&gt;&gt; 2 ** 32
&gt;&gt;&gt; 2 ** 31
&gt;&gt;&gt; int ( 2 ** 31-1)
&gt;&gt;&gt; int( 2 ** 31-1) + 1
&gt;&gt;&gt; round(1.01)
&gt;&gt;&gt; round(1.99)
&gt;&gt;&gt; round(1.50)
&gt;&gt;&gt; round (1.59)
&gt;&gt;&gt; 1/3.0
&gt;&gt;&gt; third = 1/3.0
&gt;&gt;&gt; round(third)
&gt;&gt;&gt; round(third, 1)
&gt;&gt;&gt; round(third, 2)
&gt;&gt;&gt; round(third, 3)
&gt;&gt;&gt; round(1234.56, -1)
&gt;&gt;&gt; round(1234.56, -2)
&gt;&gt;&gt; round(1234.56, -3)
&gt;&gt;&gt; type(int)
&gt;&gt;&gt; callable(int)
&gt;&gt;&gt; int()
&gt;&gt;&gt; 0 == int()
&gt;&gt;&gt; 0 is int()
&gt;&gt;&gt; type(int)
&gt;&gt;&gt; type(int())
&gt;&gt;&gt; int(4,3)
&gt;&gt;&gt; int('4')
&gt;&gt;&gt; int("four")
&gt;&gt;&gt; int('z')
&gt;&gt;&gt; int('c', 16)
&gt;&gt;&gt; int("101", 2)
&gt;&gt;&gt; 'hello'
&gt;&gt;&gt; "hello"
&gt;&gt;&gt; 'bob's your uncle'
&gt;&gt;&gt; 'bob\'s your uncle'
&gt;&gt;&gt; "bob's your uncle"
&gt;&gt;&gt; 'A quote (") mark'
&gt;&gt;&gt; '''bob's your "uncle"'''
&gt;&gt;&gt; 'hello
&gt;&gt;&gt; """hello
&gt;&gt;&gt; 'hello\nthere\n\n'
&gt;&gt;&gt; 'h' in 'hello'
&gt;&gt;&gt; 'h' not in 'hello'
&gt;&gt;&gt; 'hello'[0]
&gt;&gt;&gt; s = 'hello'
&gt;&gt;&gt; s[0] = 'j'
&gt;&gt;&gt; r'hello'
&gt;&gt;&gt; r'hello' is 'hello'
&gt;&gt;&gt; r'hello\n'
&gt;&gt;&gt; r'hello\n' == 'hello\n'
&gt;&gt;&gt; len(r'hello\n')
&gt;&gt;&gt; len('hello\n')
&gt;&gt;&gt; 2 * 'hello'
&gt;&gt;&gt; 2 + 'hello'
&gt;&gt;&gt; '2' + 'hello'
&gt;&gt;&gt; u'hello'
&gt;&gt;&gt; type(u'hello')
&gt;&gt;&gt; len('hello')
&gt;&gt;&gt; min('hello')
&gt;&gt;&gt; max('hello')
&gt;&gt;&gt; sorted('hello')
&gt;&gt;&gt; 'hello'.startswith('h')
&gt;&gt;&gt; 'hello'.startswith('he')
&gt;&gt;&gt; 'hello'.startswith('lo')
&gt;&gt;&gt; 'hello'.endswith('lo')
&gt;&gt;&gt; '  hello  '.strip()
&gt;&gt;&gt; '  hello  '.rstrip()
&gt;&gt;&gt; '  hello  '.lstrip()
&gt;&gt;&gt; sorted('hello')
&gt;&gt;&gt; sorted('hello', reverse= True)
&gt;&gt;&gt; reversed('hello')
&gt;&gt;&gt; list(reversed('hello'))
&gt;&gt;&gt; 'hello'.upper()
&gt;&gt;&gt; 'HELLO'.isupper()
&gt;&gt;&gt; 'hello'.title()
&gt;&gt;&gt; 'Hello'.istitle()
&gt;&gt;&gt; 'hello world'.title()
&gt;&gt;&gt; 'hello world'.title().swapcase()
&gt;&gt;&gt; '!' in '.?!'
</code>

Latest revision as of 15:57, 8 October 2008

The exercise can be found here: media:PythonDialog.txt

This exercise is slightly a modifed exercise from this "python crash course".