Recitation 1a (Oct 31, 2011)
Welcome (Fall 2011/2012) » Recitation Logs (Fall 2011/2012) » Recitation 1a (Oct 31, 2011)
Table of Contents
Code examples
Example 1
print('Speak, friend, and enter.') password = input('password: ') # this is a comment 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 = 'Simon' #for letter in name: # print('Give me', str.upper(letter)) for letter in str.upper(name): 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 = 50 n = input('Please enter a number: ') # keep n as a string throughout the program # TODO: verify n is actually a decimal 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) 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) 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.
>>> print('Hello')
Hello
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: sumsum
Nothing happens.
Bye!
>>> print(password)
sumsum
>>> x=4
>>> x==4
True
>>> x==10
False
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: sumsum
Bye!
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: mellon
Bye!
>>> ================================ RESTART ================================
>>>
Speak, friend, and enter.
password: mellon
A great doorway swings open.
Welcome to Moria!
Bye!
>>> if 10>5:
if 5==5:
print('Yes!')
Yes!
>>> if 10>5:
if 5==5:
print('Yes!')
Yes!
>>> if 10>5:
print('Hi')
if 5==5:
print('Yes!')
print('Bye')
Hi
Yes!
Bye
>>> ================================ RESTART ================================
>>>
Give me S
Give me i
Give me m
Give me o
Give me n
What did we get? Simon
>>> letter
'n'
>>> for digit in 178:
print(digit)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
for digit in 178:
TypeError: 'int' object is not iterable
>>> ================================ RESTART ================================
>>>
Give me S
Give me i
Give me m
Give me o
Give me n
What did we get? Simon
Traceback (most recent call last):
File "C:/Documents and Settings/ranihod/Desktop/rec1_ex2.py", line 6, in <module>
dfhdshdsh
NameError: name 'dfhdshdsh' is not defined
>>> ================================ RESTART ================================
>>>
Give me S
Give me i
Give me m
Give me o
Give me n
Give me 1
Give me 7
Give me 8
Give me *
Give me $
Give me $
Give me $
What did we get? Simon178*$$$
>>> 178
178
>>> '178'
'178'
>>> type(178)
<class 'int'>
>>> type('178')
<class 'str'>
>>> ================================ RESTART ================================
>>>
Give me S
Give me I
Give me M
Give me O
Give me N
What did we get? Simon
>>> ================================ RESTART ================================
>>>
Give me S
Give me I
Give me M
Give me O
Give me N
What did we get? Simon
>>> letter
'N'
>>> letter='z'
>>> print(letter)
z
>>> print('Hi', 100, True)
Hi 100 True
>>> print('Hi', 100 ,True)
Hi 100 True
>>> print('Hi', 100
, True)
Hi 100 True
>>> print("Don't Panic!")
Don't Panic!
>>> input()
Don't Panic!
"Don't Panic!"
>>> x=input()
sfgfsg
>>> x
'sfgfsg'
>>> input()
Homer: Maybe, just once, someone will call me "Sir" without adding "You're making a scene."
''
>>> input()
Homer: Maybe, just once, someone will call me "Sir" without adding "You're making a scene."
'Homer: Maybe, just once, someone will call me "Sir" without adding "You\'re making a scene."'
>>> 'one
SyntaxError: EOL while scanning string literal
>>> 'one
SyntaxError: EOL while scanning string literal
>>> 'one\ntwo'
'one\ntwo'
>>> print('one\ntwo')
one
two
>>> ================================ RESTART ================================
>>>
Please write something: this is a test
>>> words
['this', 'is', 'a', 'test']
>>> [1, 'test', False, [4,3]]
[1, 'test', False, [4, 3]]
>>> type([1,False])
<class 'list'>
>>> len('hello')
5
>>> len([1,False])
2
>>> [len(w) for w in words]
[4, 2, 1, 4]
>>> ================================ RESTART ================================
>>>
Please write something: my hovercraft is full of eels
The longest word has 10 letters.
>>> list.index([30,10,50], 50)
2
>>> list.index([30,10,50], 70)
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
list.index([30,10,50], 70)
ValueError: 70 is not in list
>>> ================================ RESTART ================================
>>>
Please write something: my hovercraft is full of eels
The longest word has 10 letters.
Here it is: hovercraft
>>> pos
1
>>> 162+261
423
>>> 423+324
747
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10)
range(0, 10)
>>> 'hello'[2]
'l'
>>> 'hello'[2:]
'llo'
>>> 'hello'[2:4]
'll'
>>> 'hello'[1:4:2]
'el'
>>> 'hello'[0:4:2]
'hl'
>>> 'hello'[::-1]
'olleh'
>>> n = 162
>>> n + n[::-1]
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
n + n[::-1]
TypeError: 'int' object is not subscriptable
>>> n = '162'
>>> n+n[::-1]
'162261'
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
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
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
747
This took 2 iterations.
1494
6435
11781
30492
59895
This took 7 iterations.
119790
217701
325413
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
747
This took 2 iterations.
>>> ================================ RESTART ================================
>>>
Please enter a number: 162
423
747
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: 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
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
Could not reach a palindrome :(
>>> ================================ RESTART ================================
>>>
Please enter a number: 103
404
This took 1 iterations.
Could not reach a palindrome :(
>>> k
1
>>> ================================ 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
Could not reach a palindrome :(
>>> k
49
>>> ================================ 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
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
Could not reach a palindrome :(
>>> str.title('bond, JAMES bond.')
'Bond, James Bond.'
>>> ================================ RESTART ================================
>>>
>>> my_title('Hi')
'Hi!'
>>> ================================ RESTART ================================
>>>
>>> my_title('hello WORLD')
'Hello World'
>>> my_title('mind the gap')
'Mind The Gap'
>>> ================================ RESTART ================================
>>>
>>> my_title('hello WORLD')
'Hello World'
>>> my_title('mind the gap')
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
my_title('mind the gap')
File "C:/Documents and Settings/ranihod/Desktop/rec1_ex5.py", line 7, in my_title
tw = str.upper(w[0]) + str.lower(w[1:])
IndexError: string index out of range
>>> str.split('mind the gap', ' ')
['mind', 'the', '', '', '', '', '', 'gap']
>>> ================================ RESTART ================================
>>>
>>> my_title('hello WORLD')
'Hello World'
>>> my_title('mind the gap')
'Mind The Gap'
>>> str.split('332154+246546+432635463+432642', '+')
['332154', '246546', '432635463', '432642']
>>> "hello"[100:]
''
>>> "hello"[:3]
'hel'
>>>