0% found this document useful (0 votes)
20 views3 pages

FALLSEM2023-24 BCSE101E ELA VL2023240107115 2023-10-24 Reference-Material-I

The document discusses tuples in Python. It defines a tuple as a collection that is ordered and unchangeable. It demonstrates how to create tuples with different data types, access tuple items, check if an item exists, add and remove items, unpack tuples, loop through tuples, join tuples, and multiply the contents of a tuple. Key tuple operations include creating, accessing and modifying tuples as well as common iterable methods like len(), indexing, slicing and looping.

Uploaded by

Avishi Oj
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)
20 views3 pages

FALLSEM2023-24 BCSE101E ELA VL2023240107115 2023-10-24 Reference-Material-I

The document discusses tuples in Python. It defines a tuple as a collection that is ordered and unchangeable. It demonstrates how to create tuples with different data types, access tuple items, check if an item exists, add and remove items, unpack tuples, loop through tuples, join tuples, and multiply the contents of a tuple. Key tuple operations include creating, accessing and modifying tuples as well as common iterable methods like len(), indexing, slicing and looping.

Uploaded by

Avishi Oj
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/ 3

File: /home/devendra/work/Python-2022/Programs/30Tuples.

text Page 1 of 3

# A tuple is a collection which is ordered and unchangeable.

##mytuple = ("apple", "banana", "cherry")


##print(mytuple)

# Tuples allow duplicate values:

##thistuple = ("apple", "banana", "cherry", "apple", "cherry")


##print(thistuple)

# Print the number of items in the tuple:

##thistuple = ("apple", "banana", "cherry")


##print(len(thistuple))

# String, int and boolean data types:

##tuple1 = ("apple", "banana", "cherry")


##tuple2 = (1, 5, 7, 9, 3)
##tuple3 = (True, False, False)

# A tuple with strings, integers and boolean values:

##tuple1 = ("abc", 34, True, 40, "male")


##print(type(tuple1))

# It is also possible to use the tuple() constructor to make a tuple.

##thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets


##print(thistuple)

# Access Tuple Items

##thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")


##print(thistuple[1])
##print(thistuple[-1])
##print(thistuple[2:5])
##print(thistuple[:4])
##print(thistuple[2:])
##print(thistuple[-4:-1])

# Check if Item Exists


##thistuple = ("apple", "banana", "cherry")
##if "apple" in thistuple:
## print("Yes, 'apple' is in the fruits tuple")

# Once a tuple is created, you cannot change its values.


# Convert the tuple into a list to be able to change it:

##x = ("apple", "banana", "cherry")


##y = list(x)
##y[1] = "kiwi"
##x = tuple(y)
##
##print(x)

# Add Items

##thistuple = ("apple", "banana", "cherry")


##y = list(thistuple)
##y.append("orange")
##thistuple = tuple(y)

# Remove Items
File: /home/devendra/work/Python-2022/Programs/30Tuples.text Page 2 of 3

##thistuple = ("apple", "banana", "cherry")


##y = list(thistuple)
##y.remove("apple")
##thistuple = tuple(y)

# The del keyword can delete the tuple completely:

##thistuple = ("apple", "banana", "cherry")


##del thistuple
##print(thistuple) #this will raise an error because the tuple no longer exists

# Packing a tuple:

##fruits = ("apple", "banana", "cherry")

# But, in Python, we are also allowed to extract the values


# back into variables. This is called "unpacking":

##fruits = ("apple", "banana", "cherry")


##
##(green, yellow, red) = fruits
##
##print(green)
##print(yellow)
##print(red)

# If the number of variables is less than the number of values,


# you can add an * to the variable name and the values will be
# assigned to the variable as a list:

##fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")


##
##(green, yellow, *red) = fruits
##
##print(green)
##print(yellow)
##print(red)

# another ex

##fruits = ("apple", "mango", "papaya", "pineapple", "cherry")


##
##(green, *tropic, red) = fruits
##
##print(green)
##print(tropic)
##print(red)

# Loop Through a Tuple

##thistuple = ("apple", "banana", "cherry")


##for x in thistuple:
## print(x)

##thistuple = ("apple", "banana", "cherry")


##for i in range(len(thistuple)):
## print(thistuple[i])

##thistuple = ("apple", "banana", "cherry")


##i = 0
##while i < len(thistuple):
## print(thistuple[i])
File: /home/devendra/work/Python-2022/Programs/30Tuples.text Page 3 of 3

## i = i + 1

# To join two or more tuples you can use the + operator:

##tuple1 = ("a", "b" , "c")


##tuple2 = (1, 2, 3)
##
##tuple3 = tuple1 + tuple2
##print(tuple3)

# If you want to multiply the content of a tuple a given number of times, you can
use the * operator:

##fruits = ("apple", "banana", "cherry")


##mytuple = fruits * 2
##
##print(mytuple)

You might also like