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

Python Programs 27.02.2025

The document provides various methods for copying lists in Python, including using the copy() method, list() function, and slice operator. It also explains how to join two lists using the + operator, append() method, and extend() method. Additionally, it covers tuples, their creation, handling duplicates, and accessing elements.

Uploaded by

mowis35511
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)
4 views3 pages

Python Programs 27.02.2025

The document provides various methods for copying lists in Python, including using the copy() method, list() function, and slice operator. It also explains how to join two lists using the + operator, append() method, and extend() method. Additionally, it covers tuples, their creation, handling duplicates, and accessing elements.

Uploaded by

mowis35511
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

Copy a List

Program
flist = ["apple", "banana", "cherry"]
slist = flist.copy()
print(slist)
Program
flist = ["apple", "banana", "cherry"]
slist = list(flist)
print(slist)

Use the slice Operator


You can also make a copy of a list by using the : (slice) operator.

Program
flist = ["apple", "banana", "cherry"]
slist = flist[:]
print(slist)

Join Two Lists


Using the + operator.

Program
f1 = ["a", "b", "c"]
f2 = [1, 2, 3]
f3 = f1 + f2
print(f3)

Program
f1 = ["a", "b", "c"]
f2 = [1, 2, 3]
print(f1 + f2)

Using append() method

Program
Append f2 into f1:
f1 = [12, 5.8 , "c"]
f2 = [1, 2, 3]
for x in f2:
f1.append(x)
print(f1)

Using extend() method

Program
f1 = [107, "CIME" , "MCA"]
f2 = [1, 2, 3]
f1.extend(f2)
print(f1)

Tuple
Tuples are used to store multiple items in a single variable.

Create a Tuple:

Program
x = ("apple", "banana", "cherry")
print(x)
Output
('apple', 'banana', 'cherry')

Program
x = tuple((23,56,34,23))
print(x)

Duplicates
x = (12,34,67,98,12,34)
print(x)

Program
x = tuple((107,"CIME","Bhubaneswar"))
print(len(x))

Program
x = ("apple",)
print(type(x))
#NOT a tuple
y = ("apple")
print(type(y))

Program
x1 = ("apple", "banana", "cherry")
x2 = (1, 5, 7, 9, 3)
x3 = (True, False, False)
print(x1)
print(x2)
print(x3)
Program
x1 = ("abc", 34, True, 40, "male")
print(x1)
Program
print(x[-1])
Program
print(x[2:5])
Program
print(x[:4])
Program
print(x[2:])
Program
print(x[-4:-1])

You might also like