0% found this document useful (0 votes)
24 views7 pages

Unit 3tuple and Indexing

Uploaded by

roymandesh3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Unit 3tuple and Indexing

Uploaded by

roymandesh3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tuples in Python

 Python Tuple is a collection of objects separated by commas.

 A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main
difference between both is Python tuple is immutable, unlike the Python list which is mutable.

 Creating Python Tuples


There are various ways by which you can create a tuple in Python. They are as follows:
 Using round brackets
 With one item
 Tuple Constructor

 Create Tuples using Round Brackets ()

To create a tuple we will use () operators.


var = (“java", "for", “script")
print(var)

(‘java', 'for', ‘script')


 What is Immutable in Tuples?
Tuples in Python are similar to Python lists but not entirely. Tuples are immutable and ordered and allow
duplicate values. Some Characteristics of Tuples in Python.

 We can find items in a tuple since finding any item does not make changes in the tuple.
 One cannot add items to a tuple once it is created.
 Tuples cannot be appended or extended.
 We cannot remove items from a tuple once it is created.

 A tuple in Python is a collection of ordered, immutable elements. Tuples are similar to lists, but once created,
their values cannot be changed (i.e., they are immutable). Tuples are defined using parentheses ().

 Creating a Tuple

# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple) # Output: (1, 2, 3, 4, 5)
 Accessing Elements :-You can access elements in a tuple using indexing

 print(my_tuple[0]) # Output: 1 (first element)


 print(my_tuple[-1]) # Output: 5 (last element) .

 Immutability:-Once a tuple is created, you cannot change, add, or remove its elements:

 my_tuple[1] = 10 # This will raise an error, as tuples are immutable

 Single Element Tuples:-When creating a tuple with only one element, you must include a comma after the
element; otherwise, it will not be recognized as a tuple:
single_element_tuple = (5,)
print(type(single_element_tuple)) # Output: <class 'tuple'>
 Common Tuple Methods
Tuples have limited built-in methods compared to lists because of their immutability:
•count(value): Returns the number of times a value appears in the tuple.
•index(value): Returns the index of the first occurrence of the value.

my_tuple = (1, 2, 3, 2, 4, 2)
print(my_tuple.count(2)) # Output: 3
print(my_tuple.index(3)) # Output: 2

Why Use Tuples?


•Immutability: When you want to ensure that data cannot be changed, use a
tuple.
•Performance: Tuples are generally faster than lists due to their immutability.
•Keys for Dictionaries: Tuples can be used as keys in dictionaries because they
are hashable.
indexing
 Indexing in Python is a way to access elements from sequences like lists, strings, and tuples. Python uses
zero-based indexing, meaning the first element of a sequence has an index of 0. Here are some common ways
to use indexing:

1. Indexing Lists:- # Define a list


my_list = [10, 20, 30, 40, 50]

# Access elements using index


print(my_list[0]) # Output: 10 (first element)
print(my_list[2]) # Output: 30 (third element)

2. Negative Indexing:-
print(my_list[-1]) # Output: 50 (last element)
print(my_list[-2]) # Output: 40 (second-to-last element)
3. Indexing Strings
Strings can be indexed similarly to lists:

my_string = "Hello"
print(my_string[0]) # Output: 'H'
print(my_string[-1]) # Output: 'o'
4. Slicing
You can also access a range of elements using slicing:

print(my_list[1:4]) # Output: [20, 30, 40] (from index 1 to 3)


print(my_list[:3]) # Output: [10, 20, 30] (from start to index 2)
print(my_list[2:]) # Output: [30, 40, 50] (from index 2 to end)
5. Indexing Tuples: Indexing works the same way for tuples:

my_tuple = (100, 200, 300)


print(my_tuple[1]) # Output: 200

You might also like