• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Python » Tuples in Python
Next →
← Prev

Tuples in Python

By Dinesh Thakur

In this Tutorial we are going to see how to use tuples in Python. Tuples are the most versatile, useful data types in Python. In almost any non-trivial Python program, you will discover them.

We’ll be covering the following topics in this tutorial:

  • What are tuples in python
  • How to creating tuples in python
  • Accessing Values in Tuples
  • Updating Tuples
  • Python tuple with only one element
  • Python Tuple max() Method
  • Python Tuple min() Method
  • Delete Tuple Elements

What are tuples in python

The tuples in python are very similar to lists. The tuple is an orderly and unchangeable collection. Allow duplicate members. It used to store the collection of elements in a single variable. Still, there is a significant difference between them, and that difference is tuples are immutable. Once tuples created, the content in the tuple cannot be changed.

How to creating tuples in python

I’m going to declare a variable called X, and I will assign some values to it. In case of a list, we have seen we used the square brackets in tuples we use these parentheses to store the list of elements. For example

x = (1, 5, 3, 4, 8)

Accessing Values in Tuples

in order to accessing values in tuples you can use

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(x) 

When the above code is executed, it produces the following result

(1, 5, 3, 4, 8)

It’s going to print the tuple. As I said tuples are similar to lists in some ways, you can call a tuple element by index in the tuple, so their index arranges all the tuple elements and when you want to, for example, get the first element here you give the index 0.

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(x[0])

When the above code is executed, it produces the following result

Output: 1

It’s going to give you the value which saved at index 0. let’s get the value which saved at index 4.

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(x[4])
Output: 8

You can see it gives 8, which saved at index 4.

when I try to give an index which is not there, for example, 100.

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(x[100])
Output: IndexError: tuple index out of range

It’s going to give us error which says IndexError: tuple index out of range.

Updating Tuples

As I said tuples are immutable, you cannot change a tuple’s content if they initialized once.

let’s try to test this. I’m going to just asign the value at index zero which is let’s see

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
x[0] = 2
print(x[0])
Output: TypeError: 'tuple' object does not support item assignment

You can see it TypeError: 'tuple' object does not support item assignment. You cannot assign any other value to a tuple element, if once initialized. That also means that you cannot use some of the functions which you used with the list in the tuples, for example, you cannot remove any element, you cannot append any element, you cannot change any element for example

There are few available methods with the tuple and most important is the count method so let’s try to use the count method, and I want to know how many numbers of 8 are there in this tuple.

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(x.count(8))
Output: 1

and it will give me 1 because 8 is only present once in this tuple. If it is present twice in this tuple here, you will see the result. Now to get the tuple’s length, you can use the inbuilt function, which is a len().

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(len(x))
Output: 5

You can save multiple data type values in lists. Let’s see if we can do the same with tuples or not. I’m going to give the parentheses. I will provide the first value as an integer, second value as a string, third value, for example, the decimal value. It’s going to give you the length of the tuples.

y = (1, 'max', 1.6)

and I’m going to get the value of y, and you can see it’s possible to save multiple data type values in our table.

#!/usr/bin/python
y = (1, 'max', 1.6)
print(y)

now I have declared two tuples one is X and other is Y. so let’s print X and Y’s once again very quickly and now what we want to do is we want to use a concatenation operators so as I said tuples are immutable but let me define a third tuple. here and I want to add

Z = x + y

Here I want to assign the value to z. is it possible let’s check. I’m going to see the contents in the Z variable. You can see when you use a plus operator or concatenation operator. It’s going to join these two tuples and create a new tuple with the combination of both these tuples’ values.

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
y = (1, 'max', 1.6)
z = x + y
print(z)
Output: (1, 5, 3, 4, 8, 1, 'max', 1.6)

Python tuple with only one element

Now there is one exciting thing, which you can do with a tuple. which is you can declare a variable and let me define a tuple with only one element, and I’m going to give

#!/usr/bin/python
a = ('hi',) * 5 
print(a)

Here and then after this element. I’m going to provide comma here, so this is important. I have provided this comma, and I haven’t offered the second element in the tuple, and I can use asterisks here, and I can use some value here, so let’s say I have provided 5

('hi', 'hi', 'hi', 'hi', 'hi')

Now you can see the hi string is saved 5 times in a variable in the form of a tuple. you can once again get this value at index two, for example,

print (a[2])

and it’s going to give you the value which stored at index 2 which is hi itself.

Python Tuple max() Method

you can also use inbuilt function called max() here to get the maximum value out of a tuples. We have provided X tuple here and you will be able to see

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(max(x))
Output: 8

It will give us the maximum value stored in that tuples, and you can see here it is the maximum value inside the tuple.

Python Tuple min() Method

We can also get the minimum value using a min() function, and it’s going to give you the minimum value. Which is stored inside the tuple

#!/usr/bin/python
x = (1, 5, 3, 4, 8)
print(min(x))
Output: 1

Delete Tuple Elements

you can use the del() function to delete tuples. so let’s say, I want to delete this tuple, which is Z. I can write the name of the tuple after the del() for example

#!/usr/bin/python
del z

When I try to get the value, it will give me NameError: name 'z' is not defined. To delete the tuple, you can use that del() function.

 

You’ll also like:

  1. What is Python? | Introduction to Python Programming
  2. Python Features | Main Features of Python Programming Language
  3. Python Operators and Operands – Types of Operators in Python
  4. What is for-loop in Python With examples?
  5. Bubble sort in Python
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Python

Python Tutorials

  • Python - Home
  • Python - Features
  • Python - Installation
  • Python - Hello World
  • Python - Operators Types
  • Python - Data Types
  • Python - Variable Type
  • Python - Switch Case
  • Python - Line Structure
  • Python - String Variables
  • Python - Condition Statement
  • Python - if else Statement
  • Python - for-loop
  • Python - while loop
  • Python - Command Line
  • Python - Regular Expression

Python Collections Data Types

  • Python - List
  • Python - Sets
  • Python - Tuples
  • Python - Dictionary

Python Functions

  • Python - Functions
  • Python - String Functions
  • Python - Lambda Function
  • Python - map() Function

Python Object Oriented

  • Python - Oops Concepts
  • Python - File Handling
  • Python - Exception Handling
  • Python - Multithreading
  • Python - File I/O

Python Data Structure

  • Python - Linked List
  • Python - Bubble Sort
  • Python - Selection Sort
  • Python - Linear Search
  • Python - Binary Search

Python Programs

  • Python - Armstrong Number
  • Python - Leap Year Program
  • Python - Fibonacci Series
  • Python - Factorial Program

Other Links

  • Python - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.