Tuples

NPTEL-NOC IITM
1 Sept 201916:29

Summary

TLDRThis lecture introduces Python tuples, emphasizing their immutability and ordered nature. It covers tuple creation, accessing elements through positive and negative indexing, and slicing. The script also touches on built-in functions like length, minimum, and maximum, and demonstrates tuple concatenation. However, it clarifies that once created, tuples cannot be modified, unlike lists, highlighting the key differences between the two data structures.

Takeaways

  • 📚 A tuple is a data structure in Python that consists of an ordered collection of objects or elements.
  • 🔒 Tuples are immutable, meaning once created, their contents cannot be modified.
  • 📝 To create a tuple, use parentheses and separate elements with commas.
  • 🔍 Indexing in tuples can be done using both positive (starting from 0) and negative (starting from the end) indices.
  • 👀 To access elements in a tuple, use the square bracket notation with the specified index number.
  • ❌ Attempting to modify a tuple, such as changing an element or using list methods like append or remove, will result in an error.
  • 🔑 Slicing in tuples allows for the extraction of a range of elements using index numbers.
  • 📏 The length of a tuple can be found using the `len()` function, which returns the number of elements.
  • 📊 For tuples containing numerical data, the `min()` and `max()` functions can be used to find the smallest and largest values, respectively.
  • 🔗 Tuples can be concatenated, or combined, using the `+` operator to create a new tuple.
  • 🚫 Unlike lists, tuples cannot be modified after creation, emphasizing their use for storing data that should not change.

Q & A

  • What is a tuple in Python?

    -A tuple in Python is an ordered collection of objects or elements, which is immutable, meaning once created, it cannot be modified.

  • How are tuples created in Python?

    -Tuples are created by enclosing the elements separated by commas within parentheses.

  • What is the difference between positive and negative indexing in tuples?

    -Positive indexing starts from 0 at the leftmost element, while negative indexing starts from -1 at the rightmost element, moving leftwards.

  • How can you access a specific element in a tuple?

    -You can access a specific element in a tuple by using the index number with square brackets, e.g., `tuple_name[index_number]`.

  • What is slicing in the context of tuples?

    -Slicing is used to access a subset of elements in a tuple by specifying a range of index numbers.

  • What happens if you try to modify an element in a tuple?

    -Attempting to modify an element in a tuple will result in a TypeError, as tuples are immutable and do not support item assignment.

  • How can you determine the length of a tuple?

    -The length of a tuple can be determined using the `len()` function, which returns the number of elements in the tuple.

  • What built-in functions can be used to find the minimum and maximum values in a tuple containing numeric data?

    -The `min()` and `max()` functions can be used to find the minimum and maximum values in a tuple, respectively.

  • How can you combine two or more tuples in Python?

    -Two or more tuples can be combined by using the plus (+) operator, which concatenates the tuples together.

  • What is the main advantage of using tuples over lists in Python?

    -The main advantage of using tuples over lists is that tuples are immutable, which ensures that the data they contain cannot be changed, providing a level of safety for the data.

  • Can you provide an example of how to create a tuple with employee details?

    -An example of creating a tuple with employee details would be `employee_details = ('P001', 'John', 35, 40000)`, where the tuple contains the employee's ID, name, age, and salary.

Outlines

00:00

📚 Introduction to Tuples in Python

This paragraph introduces the concept of tuples in Python, explaining them as an ordered collection of objects or elements. It highlights the immutability of tuples, meaning once created, they cannot be altered. The paragraph also covers how to create a tuple with an example using employee details, such as ID, name, age, and salary, and demonstrates how to print the tuple. The focus is on the basic understanding of tuples, their creation, and the fundamental operations that can be performed on them.

05:04

🔍 Indexing and Accessing Tuple Components

This section delves into the details of tuple indexing, explaining both positive and negative indexing. It clarifies that positive indexing starts from the left at index 0, while negative indexing starts from the right at -1. The paragraph provides an example of accessing individual elements of a tuple, such as extracting an employee's ID and salary, using their respective index numbers. It also touches on the error that occurs when attempting to access an index out of the tuple's range and introduces the concept of slicing to extract multiple elements from a tuple.

10:04

📈 Slicing, Length, and Built-in Functions for Tuples

The paragraph discusses the slicing technique for extracting subsets of elements from a tuple, explaining the concept of inclusive and exclusive index numbers in slicing. It illustrates how to use slicing to extract specific elements like an employee's name and age. The paragraph also introduces the use of the 'len' function to determine the number of elements in a tuple and the 'min' and 'max' functions to find the lowest and highest values in a tuple containing numerical data. An example of calculating the minimum and maximum marks in a tuple representing student scores in English is provided.

15:08

🔗 Concatenating Tuples and Their Immutability

This part of the script explores the process of concatenating or combining two or more tuples using the plus operator. It demonstrates how to merge an employee details tuple with another tuple containing education and department information. The paragraph emphasizes the immutability of tuples, stating that once created, their elements cannot be modified, which is a key difference from lists. It concludes with a summary of the main points discussed in the script, including tuple creation, indexing, slicing, built-in functions, concatenation, and the inability to modify tuples after creation.

Mindmap

Keywords

💡Tuples

Tuples are one of the fundamental data structures in Python, characterized by their ordered collection of elements. Unlike lists, tuples are immutable, meaning once created, their contents cannot be altered. In the video, tuples are used to represent employee details such as ID, name, age, and salary, demonstrating their use in storing related data items together.

💡Immutable

The term 'immutable' refers to the property of an object whose state or content cannot be modified after it has been created. In the context of the video, it is used to describe tuples, emphasizing that once a tuple is defined, it cannot be changed, which is a key difference from mutable data structures like lists.

💡Indexing

Indexing is the method used to access elements within a data structure like a tuple by referring to their position. The video explains two types of indexing: positive, which starts from 0, and negative, which counts from the end of the tuple. Indexing is crucial for retrieving specific data elements within a tuple.

💡Slicing

Slicing is a technique used to extract a subset of elements from a tuple by specifying a range of indices. The video illustrates how to use slicing to access multiple elements in a tuple at once, for example, to get the employee's name and age, by specifying the start and end indices of the slice.

💡Concatenation

Concatenation refers to the process of joining two or more tuples together to form a new tuple. The video demonstrates this by showing how to combine an employee's basic details with their education and department details, creating a more comprehensive data structure.

💡Built-in Functions

Built-in functions are pre-defined functions in Python that can be used without the need for importing additional modules. The video mentions functions like 'len' for determining the number of elements in a tuple, 'min' and 'max' for finding the smallest and largest values within a tuple, which are particularly useful for numerical data.

💡Negative Indexing

Negative indexing is a method of accessing elements in a tuple from the end, with the last element being referred to as -1. The video explains how this can be used to access elements in reverse order, which is a convenient way to reference elements without knowing the exact position in a large tuple.

💡Positive Indexing

Positive indexing is the standard method of accessing elements in a tuple from the beginning, with the first element indexed as 0. The video uses positive indexing to explain how to retrieve individual components of a tuple, such as an employee's ID or name.

💡Length

The 'length' of a tuple refers to the total number of elements it contains. The video describes using the 'len' function to determine this, which is essential for understanding the size of the data structure and for performing operations that require knowledge of the tuple's extent.

💡Type Error

A 'type error' in Python occurs when an operation or function is applied to an object of an inappropriate type. In the video, this term is used to illustrate the attempt to modify a tuple, which results in an error because tuples are immutable and do not support item assignment.

💡Employee Details

The term 'employee details' in the video refers to the specific data attributes associated with an employee, such as ID, name, age, and salary. These details are used as an example to demonstrate how tuples can be used to store and manage related pieces of information.

Highlights

Introduction to tuples as an ordered collection of objects or elements in Python.

Explanation of tuple immutability once created.

Demonstration of creating a tuple with employee details.

How to print and display the contents of a tuple.

Introduction to positive and negative indexing in tuples.

Accessing tuple components using index numbers.

Error handling for accessing out-of-range indices in tuples.

Slicing tuples to access multiple elements using range of index numbers.

Using the len function to determine the number of elements in a tuple.

Using min and max functions to find the lowest and highest values in a tuple.

Combining or concatenating two or more tuples using the plus operator.

Attempt to modify a tuple component results in a TypeError due to immutability.

Summary of tuple creation with commas and parentheses.

Summary of tuple indexing including positive and negative indexing.

Highlight of tuple slicing to extract multiple elements.

Mention of built-in functions for tuple length, minimum, and maximum values.

Explanation of tuple concatenation using the plus operator.

Final note on the inability to modify tuple components post-creation.

Transcripts

play00:14

Welcome to the lecture. In this lecture we will see, what is mean

play00:20

by tuples, how to create tuples, indexing, how to access the components of the tuple.

play00:26

We will also see slicing and some of the built in functions available in tuples. We will

play00:32

also see how to combine multiple tuples that is two or more tuples, we will also see how

play00:38

to modify components of the tuples. Let us get started. We will see what is mean

play00:43

by tuples. Tuples is one of the data structure available in python. It consists of an ordered

play00:51

collection of objects or elements. Some of the operations on tuples which are similar

play00:55

to list as well so, you can access the components of the lists. So, if you wanted to do slicing

play01:00

operations as well so, you can do it on the tuples.

play01:03

So, the tuples values are in separated by the commas and enclosed between the parentheses.

play01:10

The tuples are immutable. So, if you have created a tuple, you will not be able to modify

play01:16

the tuple later. So, let us see how to create a tuple. So,

play01:22

I want to told you create a tuple with employee id, employee name, age and salary details.

play01:30

So, this basically forms our employee details. So, the tuple has to be created using a parenthesis

play01:38

separated by commas. So, basically what I will do is I will create

play01:43

a tuple employee underscore details which contains the id p 0 0 1 which is the string

play01:49

is basically an id John is an employee name 35 is a numeric data type which is corresponds

play01:57

to age 40000 which corresponds to the salary details. So, I wanted to create a tuples using

play02:03

these details. So, let us say if I wanted to print the tuple

play02:07

employee underscore details. So, you have to give the print of employee underscore details.

play02:14

So, whatever you have created as a tuples so, it will be displayed with those details.

play02:19

So, we will have id we will have a employee name, will have a age and as well as the salary

play02:25

details. Next we will look at the indexing in python.

play02:29

Since a tuple is an ordered collection of elements so, each items can be accessed individually.

play02:35

So, if you wanted to slice out the some of the elements as well. So, you can do it in

play02:40

the tuple. So, we have the employee underscore details

play02:44

which has a tuple with id employee name, age and salary. So, let us see the index number.

play02:52

So, we have two types of indexing. So, that is one is positive indexing and the other

play02:58

one is negative indexing. So, the positive indexing, it always starts from left to right

play03:03

and negative indexing it starts from right to left for the positive indexing the index

play03:10

number, it always starts from 0. So, the id as corresponds to the index number

play03:15

0, employee name John; it corresponds to the index number 1 age which is 35 corresponds

play03:23

to the index number 2, salary details which corresponds to the index number 3. So, this

play03:30

is the positive indexing. Next negative indexing, it always starts from right to left so, our

play03:40

40000 which is a salary detail, its starts with the index number of minus 1, age it corresponds

play03:48

to the index number of minus 2, employee name it corresponds to the index number of minus

play03:54

3, id will corresponds to the index number of minus 4. So, this is called as a negative

play04:00

indexing. Let us say if I wanted to access some of the

play04:04

components of the tuple, let us see how to do that to access the components. We need

play04:10

to use the single slicing operator which is a square brackets. So, basically the syntax

play04:16

is you have to give the tuple name followed by the in the single slicing operator, we

play04:22

have to specify the index number. Let us say if I wanted to extract id from the employee

play04:29

details already, we saw that for id, it corresponds to the index number of 0. So, for positive

play04:36

indexing for negative indexing, it corresponds to the index number of minus 4. So, I wanted

play04:42

to extract id from the employee details. So, employee details is a tuple. So, if we give

play04:50

employee details followed by the index number so, id corresponds to the index number of

play04:56

0 right. So, if you give that you will be able to access the id from the tuple employee

play05:03

underscore details. Let us say if I wanted to extract salary from

play05:09

the employee details. So, salary corresponds to the index number of 3 right. So, if we

play05:15

give the tuple name followed by the index number, you will be able to extract salary

play05:21

from the employee details tuples so, which basically gives you the value of 40000.

play05:27

So, you can also use the negative indexing value as well. So, we have the index number

play05:33

starting from 0 till 3. So, if I give index number greater than 3, it will be the out

play05:40

of range. So, let us say if I wanted to access the fifth index number print employee details

play05:48

5. So, it will throws you an error of tuple index out of range which means so, we have

play05:56

the index number its starting from 0 till 3. So, you do not have the value of index

play06:01

number 5 which says tuple index out of range. Next look at slicing.

play06:07

So, slicing is basically used to access the set of elements. If you have wanted to access

play06:13

the multiple elements from the tuple so, you will use the range of index numbers which

play06:19

is x is to y let us see what is x and what is y. So, x is basically the index number

play06:25

where the slice should just starts. So, this is inclusive. So, this number will be included

play06:32

in the tuple. So, y it is the index number where the slices should end. So, if you give

play06:40

the value of 2 so, that index number will be excluded from the tuple. So, y is the index

play06:48

number where the slice should ends. So, if you give the index number of 2, tuple will

play06:54

be getting output of the range till index number of 1.

play06:59

So, the elements are extracted from x to y minus y minus 1. So, the x index number will

play07:04

be included for y it is exclusive. So, it will be y minus 1.

play07:09

Let us say if I wanted to extract two elements from the employee underscore details tuple.

play07:15

So, I wanted to extract name as well as age. So, if you give underscore details so, the

play07:23

name corresponds to the index number of 1 right; employee name it corresponds to the

play07:27

index number of 1, age it corresponds to the index number of 2, salary details it corresponds

play07:33

to the index number of 3. So, if you give 1 is to 3 so, what it does

play07:40

is so, 1 will be included. So, the index number will be included 3 will not be included. So,

play07:45

it will be 3 minus 1 so, it will be 2. So, we will be getting an output of John and 35

play07:52

so, which corresponds to the employee name and age. So, only elements with the index

play07:59

number of 1 and 2 will be printed 3 will not be printed in this case.

play08:04

Let us say if I wanted to extract id, employee name, age from the employee underscore details.

play08:12

So, we have four elements right a id, employee name, age and salary details, but I wanted

play08:21

to extract only the 3 elements from the employee underscore details. So, if we give print of

play08:26

employee underscore details within the square bracket colon is to 3 so, it will be able

play08:32

to print id, the employer name and age values. So, the id will be having p 0 0 1 John and

play08:42

the age value which is 35. So, the all the elements will be printed except the corresponding

play08:49

corresponding to index number 3. So, it will be 3 minus 1 so, the elements will be printed

play08:54

till the index number 2. So, let us says if I wanted to calculate the

play08:58

length of a tuple. So, if you have more elements in the tuple when you are created so, let

play09:04

us say I wanted to check the length of the tuple. So, for command is l e n so, it basically

play09:12

returns the length of the tuples. So, which means a number of elements in the tuple. The

play09:16

syntax is l e n followed by the tuple name which we have declared earlier.

play09:23

So, in our case our tuple name is employee underscore details. So, if you give length

play09:29

of employee underscore details so, it returns you an value of 4 which means so, you have

play09:37

four elements in the tuples. So, basically we have id, employee name, age and the salary

play09:44

details. Next we will let us see how to calculate the

play09:48

minimum and maximum from the tuple. So, if you have a quantitative data with integer

play09:53

or numeric value. So, if you wanted to calculate the minimum and maximum so, you can use the

play10:00

built in functions which are available in python.

play10:03

So, min is a built in function. So, if you wanted to calculate the lowest value in the

play10:10

tuples so, you can use the min function. So, if I wanted to calculate the highest value

play10:16

in the tuple so, we can use the max function. So, syntax for min is min followed by the

play10:23

tuple name. So, now, I will create a tuple of marks secured by students in English subject.

play10:31

So, now I am creating a tuple called English which has sets of values, 56 85 96 75 and

play10:40

12. So, while creating a tuple the element should be separated by commas. So, I wanted

play10:48

to calculate the minimum value for this marks secured by the students in English. So, if

play10:53

you find out so, the minimum value will be 12. So, we will use the built in function

play10:59

as well. So, if you give min of English so, you will be getting an output of 12 max of

play11:07

English so, which returns you an values of 96.

play11:10

Let us look at how to combine the 2 tuples or more tuples. So, 2 tuples can be concatenated

play11:19

by using the operations which are available in python. So, if we have tuple one plus tuple

play11:26

2, we can combine the 2 tuples. So, we can also combined the more tuples as well. So,

play11:33

we already created a tuple with a employee details. Now I am going to create an another

play11:41

tuple with employee education and department details. So, we already had employee underscore

play11:46

details which as a id, age, employee name and salary details. So, now, I am creating

play11:52

an employee underscore details 2 which as the education, M.com and the department details

play12:02

he belongs to just Accounts. Now, we will see how to combine the 2 tuples.

play12:09

This is our tuple one and we have created employee details 2 which is our tuple two.

play12:15

So, I am going to combined the tuple one and tuple 2 together. So, we can also store it

play12:21

in a some other variable name or else if you wanted to print it as well you can print the

play12:27

2 tuples by using the plus symbol. So, I wanted to print the updated tuple.

play12:32

So, if you give print employee underscore details plus employee underscore details 2.

play12:37

So, you can see the combined 2 tuples; those now we will have id, employee name, age, salary

play12:48

details and as well as a education and as well as a department which he belongs to.

play12:55

Next we will see how to modify the components of a tuple. So, the tuples are different from

play12:59

list in the sense, he will not be able to modified the tuples once created. So, in this

play13:08

we saw how to add or remove the elements from the lists right, but in the tuple we will

play13:14

not be able to add or remove from the tuples using as well as a index numbers or as well

play13:20

as the built in functions. So, append del remove those functions will not be working

play13:25

in this tuple. So, in this case, now I wanted to change the

play13:30

id. So, already we had corresponding to the index number 0 we had p 0 0 1. But now I wanted

play13:40

to change the id value. So, now, I am replacing with p 0 0 2 corresponds to the index number

play13:47

of 0, but it throws me an error of type error tuple object does not support item assignment.

play13:53

Once the tuple has been created will not be able to modify which means you will not be

play14:01

able to add or remove elements from the tuple. So, let us summarize. So, we saw how to create

play14:09

tuples values inside the tuples are separated by commas and enclosed by the parenthesis.

play14:14

We saw the indexing. So, there are 2 types of indexing one is positive indexing and the

play14:21

other one is negative indexing. So, positive indexing it always starts from 0 to n minus

play14:26

1. So, the negative indexing, it starts from the rightmost to the leftmost. We also saw

play14:34

how to access the components from a tuple so, you need to specify the index numbers

play14:40

in the tuples. So, if I wanted to extract age from the tuples, you have to specify the

play14:46

index number. We also saw how to extract 2 or more elements

play14:50

from the tuple using the slicing. So, let us say if I wanted to extract 2 elements from

play14:56

the tuple, you need to specify the x and y values. So, x is the index number will be

play15:02

included in the tuple, y the index number will not included. So, it will be elements

play15:07

will be extracted from x to y minus 1 we also saw some of the built in functions which are

play15:15

available in python which is the length its written the length of the tuple which means

play15:19

the number of elements in the tuple. We also saw the minimum function and maximum function.

play15:25

So, these can be used for the quantitative data.

play15:29

We also saw concatenation of a tuple. So, how to combine the 2 tuples? So, you can also

play15:34

use it plus and you are going to if you wanted to multiple the tuples as well you can use

play15:39

the multiple function as well. So, we also saw concatenation of a tuple how to combine

play15:44

2 or more tuples. So, you can use the plus operator . We also saw the modifying components

play15:52

in the tuple, we will not be able to modify the components using the index number or as

play15:58

well as the built in functions as well. The major advantage is so, once you have created,

play16:04

the tuples will not be able to modify .

Rate This

5.0 / 5 (0 votes)

関連タグ
Python TuplesData StructuresImmutabilityIndexingSlicingTuple CreationAccess ElementsConcatenationBuilt-in FunctionsProgramming Tutorial
英語で要約が必要ですか?