Understanding Tuples in Python: An Introduction for Beginners

Learn Code
0

 

 



  • Tuples are used to store multiple items in a single variable.  
  • Tuple is one of 4 built-in data types in Python used to store collections of data, 
  • the other 3 are List, Set, and Dictionary, all with different qualities and usage.
  • A tuple is a collection that is ordered and unchangeable.
  • Tuples are written with round brackets.

creating tuple:

mytuple = ("Joel", "Joanna", "Robin","Prince")
print(mytuple) 

output: 

('"Joel"', 'Joanna', 'Robin', 'Prince')

code :

 mytuple = ('"Joel"', "Joanna", "Robin",40,"Prince")

 print(mytuple) 

 print(mytuple[-1]) 

 print(mytuple[1:4])

Output:

('"Joel"', 'Joanna', 'Robin', 40, 'Prince')
Prince
('Joanna', 'Robin', 40)

Code Explanation:

This code creates a tuple named mytuple with five elements: "Joel", "Joanna", "Robin", 40, and "Prince".
The first line initializes mytuple with the five elements, which are enclosed in parentheses and separated by commas.
The second line uses the print function to output the entire tuple to the console. This will print the contents of the tuple as it was initialized, which are "Joel", "Joanna", "Robin", 40, and "Prince". The output will be enclosed in parentheses and separated by commas,

Code:

mytuple1 = ("Joel", "Joanna","Joanna", "Robin","Prince")
mytuple2 = (85, 78,66,56,95)
mytuple3 = (True, False,True,True ,False)
print(mytuple1)
print(mytuple2)
print(mytuple3)

Output: 

('Joel', 'Joanna', 'Joanna', 'Robin', 'Prince')    

(85, 78, 66, 56, 95)                                                                                 
(True, False, True, True, False)  
 

Code Explanation:

This code initializes three tuples, mytuple1, mytuple2, and mytuple3, each containing different types of elements.

The first tuple, mytuple1, contains five string elements: "Joel", "Joanna", "Joanna", "Robin", and "Prince".

The second tuple, mytuple2, contains five integer elements: 85, 78, 66, 56, and 95.

The third tuple, mytuple3, contains five boolean elements: True, False, True, True, and False.

The fourth line uses the print function to output the contents of mytuple1 to the console.

code: 

print(len(mytuble)

Output:

5

code explanation:  

The code "print(len(mytuple))" is used to output the length of the mytuple tuple. The len() function is a built-in Python function that returns the number of items in an object, such as a tuple. 

len keyword is used to printing the length of the tuple or object.

code:

one=("joel",)
one1=("joel")
print(one)
print(one1)
print(type(one))
print(type(one1))

Output: 

('joel',)
joel
<class 'tuple'>
<class 'str'>

Code explanation:

In this code, two tuples named one and one1 are initialized, each with one element.

The first tuple one is initialized with a single string element "joel". Note that the string is enclosed in parentheses and followed by a comma. This is because in Python, a tuple with a single element needs to have a comma after the element to distinguish it from a string enclosed in parentheses.

The second tuple one1 is initialized with a single string element "joel", but without a comma. Since it doesn't have a comma, this is not a tuple, but a string.

When you run this code and print the values of one and one1 using the print() function.

Code:

mytuple4 = ("Joel", 87,"Joanna", True,"Prince") 

print(mytuple4)

Output:

('Joel', 87, 'Joanna', True, 'Prince')

Code:

print(mytuple4[3])
print(mytuple4[1:4])

Output: 

True
(87, 'Joanna', True)

code:

mytuple4 = ("Joel", 87,"Joanna", True,"Prince")
if "Joel" in mytuple4:
   print("Yes, 'Joel' is in the tuple")

mytuple4 = ("Joel", 87,"Joanna", True,"Prince")                                                                                y = list(mytuple4)
y.append("Jovan")
mytuple4 = tuple(y)
print(y)
print(type(y))

Output:

['Joel', 87, 'Joanna', True, 'Prince', 'Jovan'] 

<class 'list'>
Code:

mytuple4 = ("Joel", 87,"Joanna", True,"Prince")   

y = ("Jovan",)                                                                             
mytuple4 += y
print(mytuple4) 

Output:

('Joel', 87, 'Joanna', True, 'Prince', 'Jovan')

Code:

mytuple4 = ("Joel", 87,"Joanna", True,"Prince")

y = list(mytuple4)
y.remove("Joel") 

print(mytuple4)
print(y)
Output:
 

('Joel', 87, 'Joanna', True, 'Prince')
[87, 'Joanna', True, 'Prince']

Code:

mytuple4 = ("Joel", 87,"Joanna", True,"Prince")
for x in mytuple4:
  print(x)
  print(type(x)) 

Output:
Joel
<class 'str'>
87
<class 'int'>
Joanna
<class 'str'>
True
<class 'bool'>
Prince
<class 'str'> 

Code:

mytuple1 = ("Joel", "Joanna","Joanna", "Robin","Prince")    

mytuple2 = (85, 78,66,56,95)                                                     
mytuple5= mytuple1 + mytuple2
print(mytuple5)
print(type(mytuple5))

Output:

('Joel', 'Joanna', 'Joanna', 'Robin', 'Prince', 85, 78, 66, 56, 95)
<class 'tuple'>

 




 




 

 




 


Tags

Post a Comment

0Comments
Post a Comment (0)