Which of the following statements is true?,
Classified in Computers
Written at on English with a size of 10.36 KB.
infile.Read(1)
'T'
>>>
infile.Read(5)
'he 3 '
>>>
infile.Readline()
'lines in this file end with the new line
character.\n'
>>>
infile.Read()
'\nThere is a blank line above this line.\n'
>>>
infile.Close()
outfile = open('test.Txt', 'w')
>>>
outfile.Write('T')
1
>>>
outfile.Write('his is the first line.')
22
>>>
outfile.Write(' Still the first line...\n')
25
>>>
outfile.Write('Now we are in the second line.\n')
31
>>>
outfile.Write('Non string value like '+str(5)+' must be converted first.\n')
49
>>>
outfile.Write('Non string value like {} must be converted first.\n'.Format(5))
49
>>> outfile.Close()
if temp > 86:
print('It is hot!')
print('Be sure to drink liquids.')
else:
print('It is not hot.')
print('Bring a jacket.')
print('Goodbye.')
BMI(weight, height):
'prints BMI report
bmi = weight*703/height**2
if bmi <>
print('Underweight')
elif bmi <>
print('Normal')
else: # bmi >= 25
print('Overweight')
>>> n = 10
>>> for i in range(n):
print(i, end=' ')
0 1 2 3 4 5 6 7 8 9
>>> for i in range(7, 100, 17):
print(i, end=' ')
7 24 41 58 75 92
>>> for i in range(len('world')):
print(i, end=' ')
0 1 2 3 4
>>>Link = 'http://www.Main.Com/smith/index.Html'
>>>Link[:4]
'http'
>>>Link[:4].Upper()
'http'
>>>Link.Find('smith')
20
>>>Link[20:25]
'smith'
>>>Link[20:25].Capitalize()
'smith'
>>>Link.Replace('smith','ferreira')
'http://www.Main.Com/ferreira/index.Html'
>>>Link
'http://www.Main.Com/smith/index.Html'
>>>New =Link.Replace('smith','ferreira')
>>>New
'http://www.Main.Com/ferreira/index.Html'
>>>Link.Count('/')
4
>>>Link.Split('/')
['http:','','www.Main.Com','smith','index.Html']
Table =Str.Maketrans(':,-'3*' ')
>>>Event.Transl8(table)
'tuesdayFeb292012335Pm'
Event.Transl8(table).Split()
['tuesday','feb','29','2012','3','35','pm']
Print(prodCostWght2talSep=';')
Morels1390.569.5
Print(prodCostWght2talSep=':::')
Morels:::139:::0.5:::69.5
Print('{},{} {},{} @ {}:{}:{}'.4mat(weekdayMonth,
DayYearHourMinute2nd))
WednesdayMarch102012 @11:45:33
>>> lst = ['Alan Turing', 'Ken Thompson', 'Vint Cerf']
>>> for name in lst:
fl = name.Split()
print(fl[0], fl[1])
Alan Turing
Ken Thompson
Vint Cerf
>>>
for name in lst:
fl = name.Split()
print('{:5} {:10}'.Format(fl[0], fl[1]))
Alan Turing
Ken Thompson
Vint Cerf
for animal in pets:
print(animal, end=' ')
cat dog fish bird
def checkSorted(lst):
'return True if sequence lst is increasing, False otherwise'
for i in range(len(lst)):
# compare lst[i] with lst[i+1]
lst)-1):
i = 0, 1, 2, ..., len(lst)-2
if lst[i] <=>=>
# correctly ordered, continue on
else:
# incorrectly ordered, return false
> lst[i+1]:
return False
# all adjacent
def arithmetic(lst):
if len(lst) < 2:="" #="" a="" sequence="" of="" length="">< 2="" is="">
return True
# check that the difference between successive numbers is
# equal to the difference between the first two numbers
diff = lst[1] - lst[0]
for i in range(1, len(lst)-1):
if lst[i+1] - lst[i] != diff:
return False
return True
>>> lst = [3, 2, 7, 1, 9]
>>> res = 0
>>> for num in lst:
res = res + num
>>> res
22
>>> lst = [3, 2, 7, 1, 9]
>>> res = 1
>>> for num in lst:
res *= num
def acronym(phrase):
'return the acronym of the input string phrase'
# split phrase into a list of words
words = phrase.Split()
# accumulate first character, as an uppercase, of every word
res = ''
for w in words:
res = res + w[0].Upper()
return res
def divisors(n):
'return the list of divisors of n'
res = [] # accumulator initialized to an empty list
for i in range(1, n+1):
if n % i == 0: # if i is a divisor of n
res.Append(i) # accumulate i
return res
>>> n = 5
>>> nested2(n)
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
print(item, end=' ')
print()
>>> print2D(table)
3 5 7 9
0 2 1 6
3 8 3
i=7
while i <=>=>
i += 7
>>> i
42
def greater(lst):
# iterate through list lst and
# compare each number with 0
# Which loop pattern shoud we use?
for i in range(len(lst)):
# using counter loop pattern
if lst[i] <>
# number at index i is first
# negative number, so return i
return i
# if for loop
def before0(table):
for row in table:
for num in row:
if num == 0:
break
print(num, end=' )
print()
>>> before0(table)
2 3
4 5 6
def ignore0(table):
for row in table:
for num in row:
if num == 0:
continue
print(num, end=' )
print()
>>> ignore0(table)
2 3 6
3 4 5
4 5 6
User--?Defined
indexes
and
dic5onaries
>>> employee[987654321]
['Yu', 'Tsun']
>>> employee[864209753]
['Anna', 'Karenina']
>>> employee[100010010]
['Hans', 'Castorp']
Goal:a container of employee records indexed by employee SS# Problems:the range ofSS#s is huge SS# sare not really integers Solu)on:the dic)onaryclass dict
>>> employee = {'864-20-9753': ['Anna', 'Karenina'],'987-65-4321': ['Yu', 'Tsun'],'100-01-0010': ['Hans', 'Castorp']}
>>>employee['987-65-4321']
['Yu', 'Tsun']
>>> employee['864-20-9753']
['Anna', 'Karenina']
'864-20-9753'
['Anna', 'Karenina']
'987-65-4321'
['Yu', 'Tsun']
'100-01-0010'
['Hans', 'Castorp']
A key can be used as an index to access the corresponding value
>>> days['Mo']
1
>>> days['Th'] = 5
>>> days
{'Mo': 1, 'Tu': 2, 'Th': 5, 'W': 3}
>>> days['Th'] = 4
>>> days
{'Mo': 1, 'Tu': 2, 'Th': 4, 'W': 3}
>>> 'Fr' in days
False
>>> len(days)
4
{'Mo': 1, 'Tu': 2, 'Th': 4, 'W': 3}
>>> days.Pop('Tu')
2
>>> days
{'Mo': 1, 'Th': 4, 'W': 3}
>>> days2 = {'Tu':2, 'Fr':5}
>>> days.Update(days2)
>>> days
{'Fr': 5, 'W': 3, 'Th': 4, 'Mo': 1,
'Tu': 2}
>>> days.Items()
dict_items([('Fr', 5), ('W', 3), ('Th',
4), ('Mo', 1), ('Tu', 2)])
>>> days.Keys()
dict_keys(['Fr', 'W', 'Th', 'Mo', 'Tu'])
>>> >>> vals = days.Values()
>>> vals
dict_values([5, 3, 4, 1, 2])
for val in vals:
print(val, end=' ')
5 3 4 1 2
def complete(abbreviation):
'returns day of the week corresponding to abbreviation'
days = {'Mo': 'Monday', 'Tu':'Tuesday', 'We': 'Wednesday',
'Th': 'Thursday', 'Fr': 'Friday', 'Sa': 'Saturday',
'Su':'Sunday'}
return days[abbreviation]
def frequency(itemList):
'returns frequency of items in itemList'
counters = {}
for item in itemList:
if item in counters: # increment item counter
counters[item] += 1
else: # create item counter
counters[item] = 1
return counters