Python coding challenge - Day 8 | Output Prediction MCQ

   What Is the Output of This python Code?

day 8 python coding challenge


Question:

x = [1, 2, 3]

print(x == x.copy( ))

print(x is x.copy( ))


Options :

A)

True

True

B)

False

False

C)

True

False

D) Error


Correct answer :

C)

True

False

Explanation:

Step 1 - Creating the List

x = [1, 2, 3]

A list named x is created containing three elements

Step - using copy ( )

x.copy()

The copy( ) method creates a new list object with
the same elements.

So now we have :

x → [1, 2, 3] copy → [1, 2, 3]

Both lists contains the same values but they are different
objects in memory.


Step 3- using ==

x == x.copy()

The == operator checks value equality.

both lists contain the same elements:

[1, 2, 3] == [1, 2, 3]

So the result is :

True

Step 4 - using is

x is x.copy()

The is operator checks whether both variables refer to the same
object in memory.

Here:

x → original list x.copy() → new list

They are different objects so the result is :

False


Final output :

True False

key points :

  • == checks value equality
  • is checks object identity (memory loaction)
  • copy( ) creates a new list object
  • Two lists can have same values but different memory references


Practice daily python coding challenges to improve your programming and logical

thinking skills.

Happy coding ❤️




No comments:

Post a Comment