![]() |
Python Tuple Explains Details With Examples |
What is a tuple in Python explain with an example?
Python Tuples: Python tuples are another fundamental data structure and tuples are very similar to Python lists in the sense that tuples.
Important Same Other Languages Data_Types
👉Java Data Types
👉C Data Types
Python tuple is the same to list the difference between the two is that we cannot change the elements of a tuple once it is assigned.
Python List
What is the advantage of using tuples over lists Quizlet?
What is the advantage of using tuples over lists Quizlet?
Tuples are quite similar to lists, both of them are used in similar situations as well. There are certain advantages of implementing a tuple over a list.
Below listed are some points of the main Tuples advantages.
1. We generally use tuples for different data types.
2. Tuple square measure immutable, iterating through tuple is quicker than with list. thus there's a small performance boost.
3. Tuples that contain immutable parts may be used as keys for a lexicon. With the list, this can be possible.
4. If you have got knowledge that does not modify, implementing it as a tuple can guarantee that it remains write-protected.
Create Python Tuples: Python tuple is created by placing all the elements inside parentheses ()separated by comma(,).
Parentheses are optional but are a good practice to write them.
A tuple can have any number of elements and they may be of different data types (integer, floating, list, string ).
What is tuples in Python give example?
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
float_tup=(4.3,8.6,7.4,5.6)
mix_tup=("Icoderweb",8077,35.3)
print("String Tuple:",str_tup)
print("Integer Tuple:",int_tup)
print("Floating Tuple:",float_tup)
print("Mixed Tuple:",mix_tup)
print("Type:",type(str_tup))
*****OUTPUT*****
String Tuple: ('Python', 'Java', 'Programming')
Integer Tuple: (80, 77, 14, 78, 96)
Floating Tuple: (4.3, 8.6, 7.4, 5.6)
Mixed Tuple: ('Icoderweb', 8077, 35.3)
Type: <class 'tuple'>
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
float_tup=(4.3,8.6,7.4,5.6)
mix_tup=("Icoderweb",8077,35.3)
print("String Tuple:",str_tup)
print("Integer Tuple:",int_tup)
print("Floating Tuple:",float_tup)
print("Mixed Tuple:",mix_tup)
print("Type:",type(str_tup))
*****OUTPUT*****
String Tuple: ('Python', 'Java', 'Programming')
Integer Tuple: (80, 77, 14, 78, 96)
Floating Tuple: (4.3, 8.6, 7.4, 5.6)
Mixed Tuple: ('Icoderweb', 8077, 35.3)
Type: <class 'tuple'>
Access values of the tuple using index
Python value tuple can be accessed using index numbers. tuple index number always starting zero.How do you use index function in Python tuple?
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
print("String Tuple:",str_tup[2])
print("Integer Tuple:",int_tup[1])
*****OUTPUT*****
String Tuple: Programming
Integer Tuple: 77
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
print("String Tuple:",str_tup[2])
print("Integer Tuple:",int_tup[1])
*****OUTPUT*****
String Tuple: Programming
Integer Tuple: 77
What is the negative index in the list and tuple in Python?
Python Negative indexes start from the end of the tuple. The negative index always starts with -1.Can we access tuple using index in Python?
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
print("String Tuple:",str_tup[-2])
print("Integer Tuple:",int_tup[-1])
*****OUTPUT*****
String Tuple: Java
Integer Tuple: 96
str_tup=("Python","Java","Programming")
int_tup=(80,77,14,78,96)
print("String Tuple:",str_tup[-2])
print("Integer Tuple:",int_tup[-1])
*****OUTPUT*****
String Tuple: Java
Integer Tuple: 96
str_tup=("Python","Java","Programming")
str_tup[1]="PHP"
print("String Tuple:",str_tup)
*****OUTPUT*****
TypeError: 'tuple' object does not support item assignment
str_tup=("Python","Java","Programming")
str_tup[1]="PHP"
print("String Tuple:",str_tup)
*****OUTPUT*****
TypeError: 'tuple' object does not support item assignment
str_tup=("Python","Java","Programming")
print("Before Update String Tuple:",str_tup)
str_list=list(str_tup)
str_list[1]="PHP"
str_tup=tuple(str_list)
print("After Update String Tuple:",str_tup)
*****OUTPUT*****
Before Update String Tuple: ('Python', 'Java', 'Programming')
After Update String Tuple: ('Python', 'PHP', 'Programming')
str_tup=("Python","Java","Programming")
print("Before Update String Tuple:",str_tup)
str_list=list(str_tup)
str_list[1]="PHP"
str_tup=tuple(str_list)
print("After Update String Tuple:",str_tup)
*****OUTPUT*****
Before Update String Tuple: ('Python', 'Java', 'Programming')
After Update String Tuple: ('Python', 'PHP', 'Programming')
str_tup=("Python","Java","Programming")
print("String Lenght Tuple:",len(str_tup))
*****OUTPUT*****
String Lenght Tuple: 3
str_tup=("Python","Java","Programming")
print("String Lenght Tuple:",len(str_tup))
*****OUTPUT*****
String Lenght Tuple: 3
str_tup=("Python","Java","Programming")
str_tup.append("PHP")
print("String Tuple:",str_tup)
*****OUTPUT*****
TypeError: 'tuple' object does not support item assignment
str_tup=("Python","Java","Programming")
str_tup.append("PHP")
print("String Tuple:",str_tup)
*****OUTPUT*****
TypeError: 'tuple' object does not support item assignment
Delete Elements in Tuple: Python tuple can not delete the item from tuple because it is immutable. Python deletes a tuple completely using the del keyword.
str_tup=("Python","Java","Programming")
print("String Tuple:",str_tup)
del str_tup
print("Delete Tuples Data Success")
*****OUTPUT*****
String Tuple:('Python', 'Java', 'Programming')
Delete Tuples Data Success
NameError: name 'str_tup' is not defined
str_tup=("Python","Java","Programming")
print("String Tuple:",str_tup)
del str_tup
print("Delete Tuples Data Success")
*****OUTPUT*****
String Tuple:('Python', 'Java', 'Programming')
Delete Tuples Data Success
NameError: name 'str_tup' is not defined
str_tup1=("Python","Java","Programming")
str_tup2=("C","C++","HTML")
str_tup3=str_tup1+str_tup2
print("String Join Tuple:",str_tup3)
*****OUTPUT*****
String Join Tuple: ('Python', 'Java', 'Programming', 'C', 'C++', 'HTML')
str_tup1=("Python","Java","Programming")
str_tup2=("C","C++","HTML")
str_tup3=str_tup1+str_tup2
print("String Join Tuple:",str_tup3)
*****OUTPUT*****
String Join Tuple: ('Python', 'Java', 'Programming', 'C', 'C++', 'HTML')