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.).

Creation of Tuple

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 :

indexing in tuples

Output :

tuple indexing


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 :

negative indexing in tuple

In the above example,

  • mytup[-1] : accesses last element.

  • mytup[-3] : accesses third last element.

tuple index


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 :

tuple Slicing

Output :

Slicing in tuple

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 :

Membership testing

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,

len() function in python tuples

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,

any() function in python tuples

Back to table


sum()

This method returns the sum of all elements present in the tuple.

For example,

sum() function in python tuples

Back to table


min()

We use min() method to find minimum element of the tuple.

For example,

min() function in python tuples

Back to table


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,

index() function in python tuples

Back to table


max()

We use max() method to find maximum element of the tuple.

For example,

max() function in python tuples

Back to table


sorted()

sorted() method is used to sort the elements of the tuple. It returns a list after sorting.

For example,

sorted() function in python tuples


It is worth noting that the return type is list not a tuple.

Back to table


count()

It counts how many times an element has occurred in a tuple and returns it.

For example,

count() function in python tuples

Back to table


Features of Tuples




  • You can't add elements to a tuple because of their immutable property. There is no append() or extend() method for tuples.

  • You can't remove elements from a tuple also because of the their immutability. Tuples have no remove() or pop() method

  • You can find elements in a tuple since this doesn't change that tuple.

  • You can also use the 'in' operator to check if an element exists in the tuple.

  • You can use list() method to change the value of a tuple


Advantages of Tuples




  • Tuple use less memory whereas list use more memory.

  • We can use tuples in a dictionary as a key but it is not possible with list.

  • We can access elements with an index in both tuple and lists.


Disadvantages of Tuples




  • We cannot add an element to a tuple but we can add an element to a list.

  • We can't remove an element in a tuple but in list we can remove an element.

  • We can't replace an element in a tuple but we can replace in a list.



Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com