PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • What is Tuples?
  • Memory Allocation of Tuple items:
  • Indexing and Slicing in Tuple:
  • Properties of a Tuple:
  • Functions used in Tuple :
  • Tuple Operations:
  • iteration in tuples:
  • Empty Tuple:
  • To create a tuple with Single item:
  • Tuple Methods:
  • 1.count() :
  • 2.index() :
  • tuple() Constructor :
  1. Python For Data Analytics
  2. 1.Python
  3. 1.Python Documents

11.Tuples In Python

What is Tuples?

Tuples are used to store multiple items in a single variable. It is also an iterable.

In more simpler terms, Tuple is a collection of items enclosed between Parantheses ().

Properties : Ordered , immutable(unchangeable) , Allow Duplicate Values.

For Example:

In [1]:

name = ('Mark','Bill','Elon')
print(name)

Memory Allocation of Tuple items:

As we know that Tuples is a sequence of items, hence Each item of Tuple gets store in different memory block, similar to list.

Indexing and Slicing in Tuple:

Indexing and Slicing in Tuple is also similar to list. For Example:

In [2]:

#indexing in Tuple:
name = ('Mark','Bill','Elon')
name[1]

In [3]:

name = ('Mark','Bill','Elon')
name[-1]

In [4]:

#Slicing in Tuple:
name = ('Mark','Bill','Elon')
name[0:2]

Properties of a Tuple:

1.Tuples are Ordered:

When You define a Tuple , its items are stored and assigned to specific index number. It means Tuple items are ordered . To Explain it better, Here is an Example:

In [7]:

vartuple = ("console","flare","python","data")
print(vartuple)
('console', 'flare', 'python', 'data')

1.Tuples are Immutable/UnChangeable:

We cannot change the items in Tuple once defined.

In [8]:

vartuple = ("console","flare","python","data")
vartuple[3] = "data science"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-452a68df3a1c> in <module>
      1 vartuple = ("console","flare","python","data")
----> 2 vartuple[3] = "data science"

TypeError: 'tuple' object does not support item assignment

3.Tuples allow duplicate values:

Tuple allows duplicate values, it can have as many duplicate values. For Example:

In [9]:

vartuple = ("console","flare","python","data","data")
print(vartuple)
('console', 'flare', 'python', 'data', 'data')

Functions used in Tuple :

len() function:

len() function is used to count items in a Tuple. For Example:

In [10]:

vartuple = ("console","flare","python","data")
len(vartuple)

max() function:

max() function is used to return largest value or item in a Tuple ,For Example:

In [11]:

vartuple = [23,45,12,76,83]
max(vartuple)

In [12]:

vartuple = [23,45,12,76,83]
min(vartuple)

max() and min() function does not work in mixed Tuples with different category of data types , Just like Lists.

Tuple Operations:

how to Concatenate two Tuples +++:

vartuple = (1,2,3,4,5,6,7,8,9)
anothertuple = ('a','b','c','d')
combined_tuple = vartuple + anothertuple
print(combined_tuple)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd')

Repetition of two Tuples ∗∗∗:

var_tuple = (1,2,3,4,5,6,7,8,9)
rep_tuple = var_tuple * 2
print(rep_tuple)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Membership operator in Tuples:

var_tuple = [1,2,3,4,5,6,7,8,9]
present = 1 in var_tuple
print(present)
True

iteration in tuples:

for loop is used to iterate over the Tuple items. For example:

var_tuple = (1,2,3,4,5,6,7,8,9)

for i in var_tuple:
    print(i)
1
2
3
4
5
6
7
8
9

Empty Tuple:

We can create an empty tuple with the help of parantheses ().

empty_tuple = ()
print(empty_tuple)
()

To create a tuple with Single item:

In tuple , You can not simply create a tuple with single item. For example:

var = (5)
type(var)
int

Now , here var is not a tuple , it is an integer because python treats parantheses here as Operator. So What is the Solution?

var = (5,)
type(var)
tuple

So , We can create a tuple with single item by using comma like above.

Tuple Methods:

Since We cannot change values in Tuple once we have defined tuple. So there are only two methods in Tuple.

1.count() :

count method is used to count occurence of elements in Tuples.

var = (1,2,3,4,4,4,4,4)
x = var.count(4)
print(x)
5

2.index() :

index method is used to find position of element in tuple.

var = ("console","flare","python","data")
position = var.index("data")
print(position)
3

tuple() Constructor :

tuple constructor is used to convert an iterable to tuple. We can also use to create an empty tuple with it.

varlist = [1,2,3,4]
vartuple = tuple(varlist)
print(vartuple)
(1, 2, 3, 4)
var = "Console"
var = tuple(var)
print(var)
('C', 'o', 'n', 's', 'o', 'l', 'e')
#To Create an empty tuple.
var = tuple()
print(var)
()
Previous10.Sets In PythonNext12.Dictionary In Python

Last updated 11 months ago