0% found this document useful (0 votes)
9 views

PythonAssignment_DebdutSarkar (1)

The document provides a comprehensive guide on lists and tuples in Python, covering their creation, manipulation, and various operations such as slicing, updating, concatenation, and eliminating duplicates. It includes examples demonstrating how to work with both data structures, including accessing elements, negative indexing, and converting between lists and tuples. The content is structured in a step-by-step manner, making it easy for readers to understand the concepts.

Uploaded by

felixyt428
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PythonAssignment_DebdutSarkar (1)

The document provides a comprehensive guide on lists and tuples in Python, covering their creation, manipulation, and various operations such as slicing, updating, concatenation, and eliminating duplicates. It includes examples demonstrating how to work with both data structures, including accessing elements, negative indexing, and converting between lists and tuples. The content is structured in a step-by-step manner, making it easy for readers to understand the concepts.

Uploaded by

felixyt428
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1 List (How to define a list )

emp = [ "John", 102, "USA"]


Dep1 = [ "CS",10]
Dep2 = [ "IT",11]
HOD_CS = [ 10,"Mr. Holding"]
HOD_IT = [11, "Mr. Bewon"]
print("printing employee data ...")
print(" Name : %s, ID: %d, Country: %s" %(emp[0], emp[1], emp[2]))
print("printing departments ...")
print("Department 1:\nName: %s, ID: %d\n Department 2:\n Name: %s, ID: %s"%( Dep1[0],
Dep2[1], Dep2[0], Dep2[1]))
print("HOD Details ....")
print("CS HOD Name: %s, Id: %d" %(HOD_CS[1], HOD_CS[0]))
print("IT HOD Name: %s, Id: %d" %(HOD_IT[1], HOD_IT[0]))
print(type(emp), type(Dep1), type(Dep2), type(HOD_CS), type(HOD_IT))

2 List (Slicing the list elements )

list = [1,2,3,4,5,6,7]
print(list[0])
print(list[1])
print(list[2])
print(list[3])
# Slicing the elements
print(list[0:6])
# By default, the index value is 0 so its starts from the 0th element and go for index -1.
print(list[:])
print(list[2:5])
print(list[1:6:2])

3 List (Updating list values)

# updating list values


list = [1, 2, 3, 4, 5, 6]
print(list)
# It will assign value to the value to the second index
list[2] = 10
print(list)
# Adding multiple-element
list[1:3] = [89, 78]
print(list)
# It will add value at the end of the list
list[-1] = 25
print(list)

4 List (Concatenation of Lists )

# concatenation of two lists


# declaring the lists
list1 = [12, 14, 16, 18, 20]
list2 = [9, 10, 32, 54, 86]
# concatenation operator +
l = list1 + list2
print(l)

5 List (Length of a list )

# size of the list


# declaring the list
list1 = [12, 14, 16, 18, 20, 23, 27, 39, 40]
# finding length of the list
print(len(list1))

6 List (Add items in a list)

#Declaring the empty list


l =[]
#Number of elements will be entered by the user
n = int(input("Enter the number of elements in the list:"))
# for loop to take the input
for i in range(0,n):
# The input is taken from the user and added to the list as the item
l.append(input("Enter the item:"))
print("printing the list items..")
# traversal loop to print the list items
for i in l:
print(i, end = " ")

7 List (Create a program to eliminate the List's duplicate items.)

list1 = [1,2,2,3,55,98,65,65,13,29]
# Declare an empty list that will store unique values
list2 = []
for i in list1:
if i not in list2:
list2.append(i)
print(list2)

8 Tuple (Creating a tuple)

# Python program to show how to create a tuple


# Creating an empty tuple
emptyTuple = ()
print("Empty tuple: ", emptyTuple)

# Creating a tuple having integers


integerTuple = (3, 6, 7, 10, 16, 23)
print("Tuple with integers: ", integerTuple)

# Creating a tuple having objects of different data types


mixedTuple = (6, "Javatpoint", 14.3)
print("Tuple with different data types: ", mixedTuple)

# Creating a nested tuple


nestedTuple = ("Javatpoint", {4: 5, 6: 2, 8:2}, (5, 3, 5, 6))
print("A nested tuple: ", nestedTuple)
9 Tuple (Accessing Tuples )

# Python program to show how to access tuple elements

# Creating a tuple having six elements


sampleTuple = ("Apple", "Mango", "Banana", "Orange", "Guava", "Berries")

# accessing the elements of a tuple using indexing


print("First Element of the Given Tuple:", sampleTuple[0])
print("Second Element of the Given Tuple:", sampleTuple[1])
print("Third Element of the Given Tuple:", sampleTuple[2])
print("Forth Element of the Given Tuple:", sampleTuple[3])
print("Fifth Element of the Given Tuple:", sampleTuple[4])
print("Sixth Element of the Given Tuple:", sampleTuple[5])

# trying to access element index more than the length of a tuple


try:
print("Seventh Element of the Given Tuple:", sampleTuple[6])
except Exception as e:
print(e)

# trying to access elements through the index of floating data type


try:
print("Accessing Second Element of the Given Tuple using floating-point index value:",
sampleTuple[1.0])
except Exception as e:
print(e)

# Creating a nested tuple


nestedTuple = ("Fruits", [4, 6, 2, 6], (6, 2, 6, 7))

# Accessing the index of a nested tuple


print(nestedTuple[0][3])
print(nestedTuple[1][1])
10 Tuples (Negative Indexing)

# Python program to show how negative indexing works in Python tuples

# Creating a tuple
sampleTuple = ("Apple", "Mango", "Banana", "Orange", "Guava", "Berries")

# Printing elements using negative indices


print("Element at -1 index: ", sampleTuple[-1])
print("Element at -2 index: ", sampleTuple[-2])
print("Element at -3 index: ", sampleTuple[-3])
print("Element at -4 index: ", sampleTuple[-4])
print("Element at -5 index: ", sampleTuple[-5])
print("Element at -6 index: ", sampleTuple[-6])

# Printing the range of elements using negative indices


print("Elements between -6 and -1 are: ", sampleTuple[-6:-1])
11 Tuples (Slicing in tuples )

# Python program to show how slicing works in Python tuples

# Creating a tuple
sampleTuple = ("Apple", "Mango", "Banana", "Orange", "Guava", "Berries")

# Using slicing to access elements of the tuple


print("Elements between indices 1 and 5: ", sampleTuple[1:5])

# Using negative indexing in slicing


print("Elements between indices 0 and -3: ", sampleTuple[:-3])

# Printing the entire tuple by using the default start and end values
print("Entire tuple: ", sampleTuple[:])

12 Tuples (Deleting Tuple elements )

# Python program to show how to delete elements of a Python tuple

# Creating a tuple
sampleTuple = ("Apple", "Mango", "Banana", "Orange", "Guava", "Berries")
# printing the entire tuple for reference
print("Given Tuple:", sampleTuple)

# Deleting a particular element of the tuple using the del keyword


try:
del sampleTuple[3]
print(sampleTuple)
except Exception as e:
print(e)

# Deleting the variable from the global space of the program using the del keyword
del sampleTuple

# Trying accessing the tuple after deleting it


try:
print(sampleTuple)
except Exception as e:
print(e)

13 Tuples (Changing Tuple Elements)

# Python program to demonstrate the approach of changing the element in the tuple

# creating a tuple
fruits_tuple = ("mango", "orange", "banana", "apple", "papaya")

# printing the tuple before update


print("Before Changing the Element in Tuple...")
print("Tuple =", fruits_tuple)

# converting the tuple into the list


fruits_list = list(fruits_tuple)

# changing the element of the list


fruits_list[2] = "grapes"
print("Converting", fruits_tuple[2], "=>", fruits_list[2])

# converting the list back into the tuple


fruits_tuple = tuple(fruits_list)

# printing the tuple after update


print("After Changing the Element in Tuple...")
print("Tuple =", fruits_tuple)

14 Tuples (Convert tuple to list and adding element to it )

fruits_tuple = ("mango", "orange", "banana", "apple", "papaya")

# printing the tuple before update


print("Before Adding a New Element in Tuple...")
print("Original Tuple =", fruits_tuple)

# converting the tuple into the list


fruits_list = list(fruits_tuple)

# changing the element of the list


fruits_list.append("blueberry")
print("Adding New Element -> 'blueberry'")

# converting the list back into the tuple


fruits_tuple = tuple(fruits_list)

# printing the tuple after update


print("After Adding a New Element in Tuple...")
print("Updated Tuple =", fruits_tuple)

You might also like