Sunday, 21 June 2020

18 - Python MCQ-1


What is the output of the following?

x=123

for i in x:

  print(i)

 

(i)                        1  2  3

(ii)                   123

(iii)              Error



x=123

hai yaha x ek integer type ka variable hai and for each loop is tarah ka aise objects par kaam karta hai jisme iteration ho means 2 se jyada values ho jaise list, tuple etc.

so x ke liye ham for i in x wala loop nahi chala sakte isme error hai

so ans error hoga

 

isko agar ham

x=(123,)

karke likh de to ab ye tuple ban gya hai tuple me 1 element likhne ke liye uske baad me comma laga do, tuple ko ya to small bracket me likhte hai ya bina bracket ke but kam se kam 1 comma jarur hona chahiye

so

x=123,

ya

x=(123,)

likhne se ye tuple ban jayega jisme 1 element hoga

fir ans (ii) wala hoga 123

 

or agar ham yaha

x=[123] likh de to ye list ban jayega and list ko bade bracket me likhte hai

list par bhi iteration ka loop chal jayega

so ans tab bhi (ii) wala yani 123 aayega 

No comments:

Post a Comment