• Offers
    • RegisterLogin
      • Learn More
    PythonPoint.netPythonPoint.net
    • Offers
    • RegisterLogin
      • Learn More

      Python

      SKILL IS IMPORTANT THAN DEGREE Be skill full.
      • Home
      • Blog
      • Python
      • What are tuples in python

      What are tuples in python

      • Posted by Python Point Team
      • Categories Python
      • Date November 9, 2020
      • Comments 0 comment

      The python tuples are sequences that are used to store a tuple of values of any type. Python tuples are immutable i.e. you cannot change the elements  of a tuple in place; Python will create a fresh tuple when you make changes to an element of a tuple. Tuple is a type of sequence like string and lists but it differs from them in the way that lists are mutable but strings and tuples are immutable.

      Types of Tuples

      The tuple is a standard datatype of Python that can store a sequence of values belonging to any type. The tuples are depicted through parenthesis i.e., round brackets.

      E.g.,

      ()         #tuple with no member, empty tuple

      (1, 2, 3)      #tuple of integers

      (1, 2.5, 3, 4.5, 7.4, 9) #tuple of numbers(integers and floating point)

      (‘a’,’b’,’c’) #tuple of characters

      (‘a’, 1,’b’, 4,’d’) #tuple of mixed value types

      (‘one’, ‘two’, ‘three’) #tuple of strings

      Creating Tuples

      Creating a tuple is similar to list creation, but here we need to put a number of expressions in parentheses. That is, use round brackets to indicate the start and end of the tuple, and separate the items by commas.

      E.g., 

      (2,4,6)

      (‘acf’, ‘deb’)

      (1, 2.5,6.8, 0.2,5)

      Thus, to create a tuple you can write in the form given below:

      T=()

      T=(value, ….)

      This construct is known as a tuple display construct.

      Accessing Tuples

      Tuples are immutable sequences having a progression of elements. Thus, other than editing items, you can do all that you can do with lists. Thus, you can access the tuple elements just like you access a list’s or a string’s elements 

      e.g.,

      Tuple[i] will give you ith index ;

      Tuple[a:b] will give you elements between indexes a to b-1. 

      Etc, etc.

      • Length- Function len(T) returns the number of items(count) in the tuple T.
      • Indexing and Slicing – 

      T[i] returns the item at index i

      T[i:j] returns a new tuple, containing the objects between i and j excluding index j.

      • Membership operators- Both ‘in’ and ‘not in’ operators work in tuples, ‘in’ tells if an element is present in the tuple or not and ‘not in’ does the opposite.
      • Concatenation and replication operators + and * – The + operator adds one tuple to the end of another. The * operators repeats a tuple. 

      Tuple operations

      Joining Tuples– It is very easy just like you perform addition. The + operator, the concatenation operator, when used with two tuples, joins two tuples.

      e.g.,

      tp1=(1,2,3)
      tp2=(4,5,6)
      tp1+tp2
      Output=(1,2,3,4,5,6)
      • Repeating or Replicating Tuples– Like strings and lists you can use * operator to replicate a tuple specified number of times.

      E.g.,

      tp1=(1,3,5)
      tp1*3
      Output=(1,3,5,1,3,5,1,3,5)
      • Slicing the Tuples– Tuples slices are the sub parts of the tuple extracted out.
      Format:- seq=T[start:stop]

      E.g., 

      tp1=(10,12,14,20,22,24,30,32,34)
      seq=tp1[3:6]
      seq
      Output=(20,22,24)
      • Delete tuple– The del statement of python is used to delete elements and objects but as we know tuple is immutable, which means that individual  elements of a tuple cannot be deleted. 

      E.g., 

      t1=(5,6,7,8,9)
      t1
      (5,6,7,8,9)
      del t1
      print(t1)
      NameError: name ‘t1’ is not defined

      Tuple functions and methods

      1. len() – This method returns length of the tuple, i.e., the count of elements in the tuple.

      Syntax:-  

       len(tuple)
      1. max()– This method returns the element from the tuple having maximum value.

      Syntax:- 

      max(tuple)
      1. min()– This method returns the element from the tuple having minimum value.

      Syntax:- 

       min(tuple)
      1. index()- This returns the index of an existing element of a tuple.

      Syntax:-

      tuplename.index(item)
      1. count()- This method returns the count of a number element/object in a given tuple.

      Syntax:- 

       tuplename.count(object)

      • Share:
      author avatar
      Python Point Team

      Previous post

      How to reverse a list in python?
      November 9, 2020

      Next post

      How to convert list to string in Python?
      November 9, 2020

      You may also like

      Introduction to Transfer Learning with Python: A Practical Guide
      31 December, 2022

      Introduction: Definition of transfer learning Overview of how transfer learning works in the context of machine learning Why transfer learning is useful and important Section 1: Transfer learning in Python with Keras In this section, we will explore how to …

      How to Check Type in Python
      31 December, 2022

      In this article, we will learn to check type in Python. The built-in function type() can be used to check the type of data in Python.

      How to make web crawler in python?
      31 December, 2022

      A web crawler is a systematic, well-defined process of extracting specific data about a topic. We have a library named as scrapy which helps in making web crawler. We can install scrapy by the following command:- Now we can easily …

      Subscribe
      Login
      Notify of
      Please login to comment
      0 Discussion
      Inline Feedbacks
      View all comments

      Latest Courses

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      ₹10,000
      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      ₹25,000 ₹2,500

      Latest Posts

      • Introduction to Transfer Learning with Python: A Practical Guide
      • How to Check Type in Python
      • How to make web crawler in python?
      • Why was the language called “python”?
      • Why Python is so Popular
      Contact
      •   support@pythonpoint.com

      We get you the best Python Courses and Blogs aiming to provide skill.

      We Believe Skill is much more important than a Degree

      Company
      • About Us
      • Blog
      • Offers
      • Contact
      Useful Links
      • Courses
      Support
      • Need Support

      © 2020 ALL RIGHTS RESERVED​ PYTHONPOINT.NET

      PythonPoint

      • Terms of Use
      • Refund Policy
      • Privacy Policy

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now

      wpDiscuz