- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if variable is tuple in Python
When it is required to check if a variable is a tuple, the 'type' method can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.
The 'type' method checks to see the type of the iterable/value that is passed to it as an argument.
Below is a demonstration of the same −
my_tuple_1 = (7, 8, 0, 3, 45, 3, 2, 22, 4) print ("The tuple is : " ) print(my_tuple_1) my_result = type(my_tuple_1) is tuple print("Is the given variable a tuple ? ") print(my_result)
The tuple is : (7, 8, 0, 3, 45, 3, 2, 22, 4) Is the given variable a tuple ? True
- A tuple is defined, and is displayed on the console.
- The type of the tuple is examined, and the 'is' and 'tuple' operators are used to check if it is a tuple type.
- This result is assigned to a value.
- It is displayed as output on the console.
Kickstart Your Career
Get certified by completing the courseGet Started