Recitation 1b (Nov 1, 2011)
Welcome (Fall 2011/2012) » Recitation Logs (Fall 2011/2012) » Recitation 1b (Nov 1, 2011)
Table of Contents
Code examples
Example 1
print('Speak, friend, and enter.') password = input('password: ') if password == 'mellon': print('A great doorway swings open.') print('Welcome to Moria!') else: print('Nothing happens.') print('Bye.')
Download (right click -> save link as "ex1.py")
Example 2
name = 'Lee' # a brave volunteer #for letter in name: # print('Give me', str.upper(letter)) for letter in str.upper(name): #print('Give me', letter) print('Give me "' + letter + '"') print('What did we get?', name)
Download (right click -> save link as "ex2.py")
Example 3
sentence = input('Please write something: ') words = str.split(sentence) lengths = [len(w) for w in words] maxlen = max(lengths) print('The longest word has', maxlen, 'letters.') pos = list.index(lengths, maxlen) # first occurence print('Here it is:', words[pos])
Download (right click -> save link as "ex3.py")
Example 4
maxiter = 10 n = input('Please enter a number: ') # keep n as a string throughout the program #TODO: verify that n represents a number for k in range(maxiter): if n == n[::-1]: print('This took', k, 'iterations.') break #n = int(n) + int(n[::-1]) n = str(int(n) + int(n[::-1])) print(n) else: print('Could not reach a palindrome :(')
Download (right click -> save link as "ex4.py")
Example 5
def my_title(s): words = str.split(s, ' ') twords = [] for w in words: #tw = str.upper(w[0]) + str.lower(w[1:]) tw = str.upper(w[:1]) + str.lower(w[1:]) list.append(twords, tw) #In one line: ## twords = [str.upper(w[:1]) + str.lower(w[1:]) \ ## for w in words] return str.join(' ', twords)
Download (right click -> save link as "ex5.py")
Interpreter session log
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 100*2 + 5
205
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: sumsum
Nothing happens.
Bye.
>>> x = 100
>>> x + 5
105
>>> x = 'Hello'
>>> x + ' World'
'Hello World'
>>> 1 == 2
False
>>> 3 > 0
True
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: mellon
A great doorway swings open.
Welcome to Moria!
Bye.
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: mellon
A great doorway swings open.
Welcome to Moria!
Bye.
>>> if 12 > 4:
print('Hi')
if 10 == 10:
print('Yes')
print('Bye')
Hi
Yes
Bye
>>> print(8)
8
>>> print(True)
True
>>> 'one'
'one'
>>> "two"
'two'
>>> "Don't Panic!"
"Don't Panic!"
>>> ================================ RESTART ================================
>>>
Give me L
Give me e
Give me e
What did we get? Lee
>>> ================================ RESTART ================================
>>>
Give me L
Give me E
Give me E
What did we get? Lee
>>> ================================ RESTART ================================
>>>
Give me L
Give me E
Give me E
What did we get? Lee
>>> print(100, 'Hi', False)
100 Hi False
>>> ================================ RESTART ================================
>>>
Give me "L"
Give me "E"
Give me "E"
What did we get? Lee
>>> ['Hi', 100, print]
['Hi', 100, <built-in function print>]
>>> type('Hi')
<class 'str'>
>>> type(100)
<class 'int'>
>>> type(print)
<class 'builtin_function_or_method'>
>>> type(['Hi', 100, print])
<class 'list'>
>>> ['Hi', 100, print, [5,6]]
['Hi', 100, <built-in function print>, [5, 6]]
>>> ================================ RESTART ================================
>>>
Please write something: this is a test
>>>
>>> sentence
'this is a test'
>>> words
['this', 'is', 'a', 'test']
>>> len
<built-in function len>
>>> len('Hello')
5
>>> len([1,2,3])
3
>>> ================================ RESTART ================================
>>>
Please write something: this is a test
>>> lengths
[4, 2, 1, 4]
>>> ================================ RESTART ================================
>>>
Please write something: my hovercraft is full of eels
>>> lengths
[2, 10, 2, 4, 2, 4]
>>> maxlen
10
>>> pos
1
>>> blabla + 6
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
blabla + 6
NameError: name 'blabla' is not defined
>>> ================================ RESTART ================================
>>>
Please write something: this is a test
The longest word has 4 letters.
Traceback (most recent call last):
File "C:/Documents and Settings/ranihod/Desktop/rec1_ex3.py", line 6, in <module>
blabla + 6
NameError: name 'blabla' is not defined
>>> ================================ RESTART ================================
>>>
Please write something: do not buy this record, it is scratched
The longest word has 9 letters.
Here it is: scratched
>>> ================================ RESTART ================================
>>>
Please write something: this is a test
The longest word has 4 letters.
Here it is: this
>>> list.index([1,2,3,4], 100)
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
list.index([1,2,3,4], 100)
ValueError: 100 is not in list
>>> [x**2 + 3 for x in [1,10,14,7]]
[4, 103, 199, 52]
>>> [10 for x in [1,10,14,7]]
[10, 10, 10, 10]
>>> x = 'my hovercraft is full of eels'
>>> x[9]
'r'
>>> x[8:14]
'craft '
>>> x[:10]
'my hovercr'
>>> x[10:]
'aft is full of eels'
>>> x[1:10:2]
'yhvrr'
>>> x[::2]
'm oecati ulo es'
>>> x[::-1]
'slee fo lluf si tfarcrevoh ym'
>>> 162+261
423
>>> 423+324
747
>>> x[::-2]
'se olu itaceo m'
>>> range(8)
range(0, 8)
>>> print(list(range(8)))
[0, 1, 2, 3, 4, 5, 6, 7]
>>> n = '1234'
>>> n + n[::-1]
'12344321'
>>> ================================ RESTART ================================
>>>
Please enter a number: 1234
5555
Traceback (most recent call last):
File "C:/Documents and Settings/ranihod/Desktop/rec1_ex4.py", line 9, in <module>
if n == n[::-1]:
TypeError: 'int' object is not subscriptable
>>> 100[3]
Traceback (most recent call last):
File "<pyshell#55>", line 1, in <module>
100[3]
TypeError: 'int' object is not subscriptable
>>> ================================ RESTART ================================
>>>
Please enter a number: 127
848
This took 1 iterations.
1696
8657
16225
68486
This took 5 iterations.
136972
416603
723217
1435544
5890885
>>> ================================ RESTART ================================
>>>
Please enter a number: 127
848
This took 1 iterations.
This took 2 iterations.
This took 3 iterations.
This took 4 iterations.
This took 5 iterations.
This took 6 iterations.
This took 7 iterations.
This took 8 iterations.
This took 9 iterations.
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
747
This took 2 iterations.
>>> ================================ RESTART ================================
>>>
Please enter a number: 9875
15664
62315
113641
259952
This took 4 iterations.
>>> ================================ RESTART ================================
>>>
Please enter a number: 10677
88278
175566
841137
1572285
7395036
13700973
51601704
92312319
183633648
1029970029
>>> ================================ RESTART ================================
>>>
Please enter a number: 10677
88278
175566
841137
1572285
7395036
13700973
51601704
92312319
183633648
1029970029
10230769230
13527472431
26954944962
53899890924
96809790759
192519581628
1018705496919
10215650575020
12273156226221
24535421363442
48971733816984
97933567534968
184877144068947
934737585847428
1759486171584867
9444337888434438
17788686775768887
96675444544457658
182350889088915327
905870770076968608
1712740440154047117
8830144950594519288
17659299901188929676
85352288012188225347
159704576133276450705
666759248464951858656
1323617407929794816322
3559802387226841979553
7119593873454674069106
13139198637998458028223
45421284127972147221354
90733558255944295433808
171567017500899580967517
887336103498905291732688
1773573296008799593366476
8520207255986806517120247
15940424412073702044140505
66444568432810723486545456
131899136865512546973089922
361879516510728115605088053
>>> ================================ RESTART ================================
>>>
Please enter a number: 10677
88278
175566
841137
1572285
7395036
13700973
51601704
92312319
183633648
1029970029
Could not reach a palindrome :(
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
747
This took 2 iterations.
Could not reach a palindrome :(
>>> k
2
>>> ================================ RESTART ================================
>>>
Please enter a number: 10677
88278
175566
841137
1572285
7395036
13700973
51601704
92312319
183633648
1029970029
Could not reach a palindrome :(
>>> k
9
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
747
This took 2 iterations.
>>> ================================ RESTART ================================
>>>
Please enter a number: 10677
88278
175566
841137
1572285
7395036
13700973
51601704
92312319
183633648
1029970029
Could not reach a palindrome :(
>>> str.title("bond, JAMES bond.")
'Bond, James Bond.'
>>> str.title("bond, JAMES $$bond.")
'Bond, James $$Bond.'
>>> ================================ RESTART ================================
>>>
>>> my_title('Hi')
'Hi!'
>>> str.join('+', ['hi', 'bye', 'bla'])
'hi+bye+bla'
>>> str.join('$$$', ['hi', 'bye', 'bla'])
'hi$$$bye$$$bla'
>>> ================================ RESTART ================================
>>>
>>> my_title('bond, JAMES bonD.')
'Bond, James Bond.'
>>> my_title('mind THE gap')
'Mind The Gap'
>>> str.split('mind the gap')
['mind', 'the', 'gap']
>>> str.split('mind the gap', ' ')
['mind', 'the', '', '', 'gap']
>>> ================================ RESTART ================================
>>>
>>> my_title('bond, JAMES bonD.')
'Bond, James Bond.'
>>> my_title('mind THE gap')
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
my_title('mind THE gap')
File "C:/Documents and Settings/ranihod/Desktop/rec1_ex5.py", line 5, in my_title
tw = str.upper(w[0]) + str.lower(w[1:])
IndexError: string index out of range
>>> 'hello'[100]
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
'hello'[100]
IndexError: string index out of range
>>> 'hello'[]
SyntaxError: invalid syntax
>>> 'hello'[:4]
'hell'
>>> 'hello'[0:4]
'hell'
>>> 'hello'[:100]
'hello'
>>> ''[:1]
''
>>> ''[0]
Traceback (most recent call last):
File "<pyshell#75>", line 1, in <module>
''[0]
IndexError: string index out of range
>>> ================================ RESTART ================================
>>>
>>> my_title('mind THE gap')
'Mind The Gap'
>>> 'hello'[:100]
'hello'
>>> x = 40593246
>>> func(x, ...)
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
func(x, ...)
NameError: name 'func' is not defined
>>>
>>>
>>>
>>> 'hello'[1:]
'ello'
>>> 'hello'[100:]
''
>>> ================================ RESTART ================================
>>>
>>> my_title('mind THE gap')
'Mind The Gap'
>>> str.title("bond, JAMES bond.")
'Bond, James Bond.'
>>>