Q. Predict the output:
(i)
d = dict ()
d ['left'] = '<'
d ['right'] = '>'
print ('{left} and {right} or {right} and {left}')
(ii)
d = dict ()
d ['left'] = '<'
d ['right' ] = '>'
d ['end'] = ' '
print(d ["left"] and d ['right'] or d ['right'] and d ['left'])
print (d ['left'] and d ['right'] or d ['right'] and d ['left'] )
print( (d['left'] and d [ 'right'] or d ['right'] and d ['left'] ) and d ['end'])
print ("end")
(iii)
text = "abracadabraaabbccrr"
counts = {}
ct = 0
lst = []
for word in text :
if word not in lst :
lst . append(word)
counts[word] = 0
ct = ct + 1
counts [word] = counts [word] + 1
print (counts)
print (lst)
(iv)
list1 = [2, 3, 3, 5, 3, 2, 5, 1, 1]
counts = {}
ct = 0
lst = []
for num in list1 :
if num not in lst :
lst . append(num)
counts[num] = 0
ct = ct + 1
counts[num] = counts[num] + 1
print (counts)
for key in counts.keys() :
counts[key] = key * counts [key]
print (counts)
Answer =
Output: -
(i)
>>>
(ii)
>
end
>>>
(iii)
['a']
{'a': 1, 'b': 1}
['a', 'b']
{'a': 1, 'b': 1, 'r': 1}
['a', 'b', 'r']
{'a': 2, 'b': 1, 'r': 1}
['a', 'b', 'r']
{'a': 2, 'b': 1, 'r': 1, 'c': 1}
['a', 'b', 'r', 'c']
{'a': 3, 'b': 1, 'r': 1, 'c': 1}
['a', 'b', 'r', 'c']
{'a': 3, 'b': 1, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 1, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 2, 'r': 1, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 4, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 6, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 2, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 3, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 1, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 2, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 2, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 3, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
{'a': 7, 'b': 4, 'r': 4, 'c': 3, 'd': 1}
['a', 'b', 'r', 'c', 'd']
>>>
(iv)
{2: 2}
{2: 2, 3: 1}
{2: 4, 3: 3}
{2: 4, 3: 4}
{2: 8, 3: 12}
{2: 8, 3: 12, 5: 1}
{2: 16, 3: 36, 5: 5}
{2: 16, 3: 37, 5: 5}
{2: 32, 3: 111, 5: 25}
{2: 33, 3: 111, 5: 25}
{2: 66, 3: 333, 5: 125}
{2: 66, 3: 333, 5: 126}
{2: 132, 3: 999, 5: 630}
{2: 132, 3: 999, 5: 630, 1: 1}
{2: 264, 3: 2997, 5: 3150, 1: 1}
{2: 264, 3: 2997, 5: 3150, 1: 2}
{2: 528, 3: 8991, 5: 15750, 1: 2}
>>>
Wrong
ReplyDeleteAll are correct, if you have doubt then please copy the code and run it in your IDLE Python.
Deleteon running (ii) its gving syntax error.
ReplyDeleteNo. It will run properly.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )