Class 11 CBSE - Python
Python is designed to understand the user’s responsibility, the protocol, and the ethical issues related to the field of computing.
Python in class 11 for cbse is very handy when it comes to practice on computers but for the beginers there might be some errors and problems such as exception. But it's always advisable to practice program on computer than writing it on paper because it cherishes your practical skills , knowledge and typing speed and you'll come to know new things and if you want to be a software developer someday then its a must call for you.
Tuple
Tuple in Python
A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas we can change the elements of a list.
Creating a Tuple
A tuple is created by placing all the items (elements) inside parentheses (), separated by commas.
A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).
In the above example, we have created different types of tuples and stored different data items inside them.
Accessing Tuple Elements
Like a list, each element of a tuple is represented by index numbers (0, 1, ...) where the first element is at index 0.
Indexing :
In Python, indexing starts from 0. We can access an item in a tuple through []. So, a tuple having 5 elements will have index from 0 to 4.
The index must be an integer. We can't use float or other types, this will result in "TypeError".
Nested tuples are accessed using nested indexing.
Let us see the Example below :
Output :
Negative Indexing :
Python also allows negative indexing for tuple . The index -1 refers to the last item, -2 to the second last item and so on.
Let us see the Example below :
In the above example,
- mytup[-1] : accesses last element.
- mytup[-3] : accesses third last element.
Slicing :
In Python we can access a section of items from the tuple through slicing using [:] operator. It is an operation in which we slice a particular range of items from that tuple. A slice of a tuple is basically its sub-tuple.
Let us see the Example below :
Output :
Here,
- my_tuple[1:4] returns a tuple with items from index 1 to index 3.
- my_tuple[:-1] returns a tuple with items from index -7 to index -2.
- my_tuple[4:] returns a tuple with items from index 4 to the end.
- my_tuple[:] returns all tuple items
Note : When we slice tuple, the start index is included but the end index is excluded (i.e. including start index to (end index-1)).
Membership Testing :
Membership Testing is an operation to check if an item exists in the tuple or not using 'in' keyword. For example,
Let us see the Example below :
Here,
- 'C' is not present in lang, 'C' in lang evaluates to False.
- 'Python' is present in lang, 'Python' in lang evaluates to True.
len() Function
In Python, we use the len() method to find the number of elements present in a tuple.
For Example,
Python Tuple Methods
Python has many useful tuple methods that makes it really easy to work with tuple.
Built-in Methods of Python Tuples
any()
The any() method returns true if tuple is having atleast one item otherwise returns false.
For example,
index()
The index() method returns the index of the specified element in the tuple.
Its syntax is :
tuple.index(element, start_index, end_index)
The index() method can take one to three parameters:
- element - the item to scan
- start_index (optional) - start scanning the element from the start_index
- end_index (optional) - stop scanning the element at the end_index
For example,
sorted()
sorted() method is used to sort the elements of the tuple. It returns a list after sorting.
For example,
It is worth noting that the return type is list not a tuple.
count()
It counts how many times an element has occurred in a tuple and returns it.
For example,
The any() method returns true if tuple is having atleast one item otherwise returns false.
For example,
The index() method returns the index of the specified element in the tuple.
Its syntax is :
tuple.index(element, start_index, end_index)
The index() method can take one to three parameters:
- element - the item to scan
- start_index (optional) - start scanning the element from the start_index
- end_index (optional) - stop scanning the element at the end_index
sorted() method is used to sort the elements of the tuple. It returns a list after sorting.
For example,
It is worth noting that the return type is list not a tuple.
It counts how many times an element has occurred in a tuple and returns it.
For example,