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 (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (892) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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