Saturday 23 December 2023

How much do you know about Python tuple?

 


num1 = num2 = (10, 20, 30, 40, 50)

print(isinstance(num1, tuple)) 

The above code  creates a tuple (10, 20, 30, 40, 50) and assigns it to both num1 and num2. Then, it checks if num1 is an instance of the tuple class using the isinstance() function and prints the result.

The correct output of the code will be:

True

This is because both num1 and num2 refer to the same tuple object, and since that object is indeed a tuple, the isinstance() function returns True.


num1 = num2 = (10, 20, 30, 40, 50)

print(num1 is num2)

The above code  checks if num1 and num2 refer to the same object in memory using the is keyword. Since both num1 and num2 are assigned the same tuple (10, 20, 30, 40, 50), which is an immutable object, the result will be True. Here's the correct output:

True

This is because both variables (num1 and num2) point to the same memory location where the tuple is stored.


num1 = num2 = (10, 20, 30, 40, 50)

print(num1 is not num2)

The code checks if num1 and num2 do not refer to the same object in memory using the is not comparison. Since both num1 and num2 are assigned the same tuple (10, 20, 30, 40, 50), the result will be False. Here's the correct output:

False

This is because both variables (num1 and num2) point to the same memory location where the tuple is stored, so the is not comparison returns False.


num1 = num2 = (10, 20, 30, 40, 50)

print(20 in num1)

The code checks if the value 20 is present in the tuple assigned to the variable num1. Since 20 is one of the values in the tuple (10, 20, 30, 40, 50), the result will be True. Here's the correct output:

True

The in keyword is used to check membership, and it returns True if the specified value is found in the sequence (in this case, the tuple num1).


num1 = num2 = (10, 20, 30, 40, 50)

print(30 not in num2)

The code checks if the value 30 is not present in the tuple assigned to the variable num2. Since 30 is one of the values in the tuple (10, 20, 30, 40, 50), the result will be False. Here's the correct output:

False

The not in keyword is used to check if a value is not present in a sequence. In this case, 30 is present in the tuple num2, so the expression evaluates to False.



0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (115) C (77) C# (12) C++ (82) Course (62) Coursera (179) coursewra (1) Cybersecurity (22) data management (11) Data Science (91) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (747) Python Coding Challenge (208) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses