What is Tuple in Python | EP-10 Tuple in Python | How to Create Tuple in Python | Built-in Functions
"""Python Tuple is a collection of objects separated by commas.
In some ways, a tuple is similar to a Python list in terms of
indexing, nested objects, and repetition but the main difference
between both is Python tuple is immutable(if its value cannot be
changed after it has been created.), unlike the Python list
which is mutable."""
#CreatingPythonTuples
"""There are various ways by which you can create a tuple in Python.
They are as follows:
1.Using round brackets
2.With one item
3.Tuple Constructor"""
1. Create Tuples using Round Brackets ()
Tuple = ()
print(Tuple)
Tuple_1 =("a",1,2.5)
print(Tuple_1)
2. Create a Tuple With One Item
"""syntax:
variable : tuple[int | str, ...] = (value1,value2....)
"""
Tuple_3 : tuple[int | str] =( 1,"q")
print(Tuple_3)
#NOTE
"""
In case your generating a tuple with a single element, make sure to add a
comma after the element.Let us see an example of the same.
"""
Tuple_4 = (77)
print(Tuple_4)
print(type(Tuple_4))
NOT a tuple
Tuple_5 = (8,)
print(Tuple_5)
print(type(Tuple_5))
#TupleConstructorinPython
"""To create a tuple with a Tuple constructor, we will pass the elements as
its parameters."""
Tuple_cons_1 =tuple("cybrosys")
print(Tuple_cons_1)
Tuple_cons_2 =tuple(("python","course"))
print(Tuple_cons_2)
#WhatisImmutableinTuples?
"""
Unlike lists, tuples are immutable. This means that once a tuple is created,
you cannot modify its elements or its size. You can't add, remove, or change
elements after the tuple is defined.
1. Ordered and immutable
2. Can hold mixed data types
3. Accessed by index (zero-based)
4. Can contain duplicates
"""
#tuples are indexed
Tuple_7 =(5,7.9,3)
print(Tuple_7[1])
print(Tuple_7[0])
#tuples contain duplicate element
Tuple_8 =(7,7,8,6,3,5,3,3,7)
print(Tuple_8)
adding an element
Tuple_8[1] = 100
print(Tuple_8)
#AccessingValuesinPythonTuples
"""Tuples in Python provide two ways by which we can access the elements of a
tuple.
Using a positive index
Using a negative index """
#Python Access Tuple using a Positive Index
"""
Using square brackets we can get the values from tuples in Python.
"""
positive = (1,2,3,"cybrosys")
print("value in positive[0]",positive[0])
print("value in positive[3]",positive[3])
#Python Access Tuple using a Negative Index
negative = (4,8,"qwert")
print("value in negative[-1]",negative[-1])
print("value in negative[-1]",negative[-3])
#python #pythonprogramming #programming #beginnerpython #tuple #tuples #explained #builtinfunctions #programmingtutorial #learnpython #TupleInPython #PythonTuples #CodingTutorial #PythonTutorials
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys