Computer Science, asked by ComputerWaIa, 19 hours ago

For Class 11 & above only.

Predict the output of following Co de fragment .
fruit = { }
f1 = ['Apple', 'Banana', 'apple', 'Banana']
for index in fl:
if index in fruit:
fruit[index] += 1
else:
fruit[index] = 1
print(fruit)
print (len(fruit))

Spams will be reported ASAP.​

Answers

Answered by anindyaadhikari13
12

Corrected Co‎‎de.

fruit = {}

f1 = ['Apple', 'Banana', 'apple', 'Banana']

for index in f1:

   if index in fruit:

       fruit[index] += 1

   else:

       fruit[index] = 1

print(fruit)

print (len(fruit))

Output.

{'Apple': 1, 'Banana': 2, 'apple': 1}

3

Explanation.

We are given with a dictionary named fruit and a list named f1.

After first iteration of loop -

> index = 'Apple'  (The first element of list)

Now, check the condition given -

> index in fruit - False as dictionary is empty.

As condition is false, the statements inside else block will be executed.

> fruit[index] = 1

Dictionary becomes - {'Apple' : 1}

After second iteration of loop -

> index = 'Banana'

Now, check the condition given -

> index in fruit - False, its not present.

As condition is false, the statements inside the else block will be executed.

> fruit[index] = 1

Dictionary becomes - {'Apple' : 1, 'Banana' : 1}

After third iteration of loop -

> index = 'apple'

Now, check the condition given -

> index in fruit = False, not present (Note that Python is case-sensitive)

As condition is false, the statements inside the else block will be executed.

> fruit[index] = 1

Dictionary becomes - {'Apple' : 1, 'Banana' : 1, 'apple' : 1}

After fourth iteration of loop -

> index = 'Banana'

Now, check the condition given -

> index in fruit - True, its present.

As condition is true, the statements inside the if block will be executed.

> fruit[index] += 1

Value of 'key' is not changed but 'value' is incremented by 1.

Dictionary becomes - {'Apple': 1, 'Banana': 2, 'apple': 1}

Loop is now terminated.

Now, the dictionary fruit as well as its length is printed. So, output -

{'Apple': 1, 'Banana': 2, 'apple': 1}

3


anindyaadhikari13: Thanks for the brainliest ^_^
Answered by alaminhussain8011
0

Answer:

tup1=(1,2,3,4,5,”a”,”b”)

tup1=[1:4]

  • Output for the following code will be:
Similar questions