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

Lab Python Program

The document discusses operations on tuples and lists in Python. It contains examples of creating, accessing, updating, deleting tuples using indexing, slicing, functions etc. It also provides examples of common list operations like appending, inserting, removing and iterating over list elements. Basic programs demonstrate applications like finding max/min/sum of tuple elements, checking country membership in a list, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Lab Python Program

The document discusses operations on tuples and lists in Python. It contains examples of creating, accessing, updating, deleting tuples using indexing, slicing, functions etc. It also provides examples of common list operations like appending, inserting, removing and iterating over list elements. Basic programs demonstrate applications like finding max/min/sum of tuple elements, checking country membership in a list, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON PROGRAMS (28.11.

2023)
EXERCISE NO: 02A: OPERATIONS IN TUPLE:
1. CREATING TUPLE:

MyTup1 = (23, 56, 89, 'A', 'E', 'I', "Tamil")


print(MyTup1)
MyTup2 = (11, 34, 78, 'B', 'C', 'P', "COMPUTER SCIENCE")
print(MyTup2)

2. CREATING TUPLE USING TUPLE( ) FUNCTION:

MyTup3 = tuple( [23, 45, 90] )


print(MyTup3)
print(type(MyTup3))

3. CREATING SINGLE ELEMENT TUPLE:

MyTup4 = (10)
print (type(MyTup4))
MyTup5 = (10,)
print (type(MyTup5))

4. ACCESSING VALUES IN TUPLE USING INDEX VALUES:

Tup1 = (12, 78, 91, "Tamil”, “Telugu", 3.14, 69.48)

print(Tup1)

print(Tup1[2:5])
print(Tup1[:5])

print(Tup1[4:])

print(Tup1[:])

5. UPDATING AND DELETING A TUPLES:

Tup1 = (2,4,6,8,10)
Tup2 = (1,3,5,7,9)
Tup3 = Tup1 + Tup2
print(Tup3) # JOINING TWO TUPLES
del Tup3 # DELETING THE TUPLE
print (Tup3) # SINCE IT IS DELETED TUPLE BECOMES UN-
DEFINED

6. TUPLE ASSIGNMENT:

(x, y, z, p) = (2**2, 5/3+4, 15%2, 34>65)


print(x,y,z,p)

(a, b, c) = (34, 90, 76)


print(a,b,c)

7. RETURNING MULTIPLE VALUES IN TUPLE:

def Min_Max(n):
a = max(n)
b = min(n)
return(a, b)
Num = (12, 65, 84, 1, 18, 85, 99)
(Max_Num, Min_Num) = Min_Max(Num)
print("Maximum value = ", Max_Num)
print("Minimum value = ", Min_Num)

8. NESTED TUPLES:

Toppers = (("Vinodini", "XII-F", 98.7), ("Soundarya", "XII-H",


97.5),
("Tharani", "XII-F", 95.3), ("Saisri", "XII-G", 93.8))
for i in Toppers:
print(i)

BASIC PROGRAMS:

1. PROGRAM TO SWAP TWO VALUES USING TUPLE


ASSIGNMENT:

a = int(input("Enter value of A: "))


b = int(input("Enter value of B: "))
print("Value of A = ", a, "\n Value of B = ", b)
(a, b) = (b, a)
print("Value of A = ", a, "\n Value of B = ", b)

2. PROGRAM USING A FUNCTION THAT RETURNS THE


AREA AND CIRCUMFERENCE OF A CIRCLE WHOSE
RADIUS IS PASSED AS AN ARGUMENT. TWO VALUES
USING TUPLE ASSIGNMENT:

pi = 3.14
def Circle(r):
return (pi*r*r, 2*pi*r)
radius = float(input("Enter the Radius: "))
(area, circum) = Circle(radius)
print ("Area of the circle = ", area)
print ("Circumference of the circle = ", circum)

3. PROGRAM TO FIND MAXIMUM ELEMENT IN TUPLE:

# Creating tuples
Tuple = ( 1, 3, 4, 2, 5, 6 )

res = max(Tuple)
print('Maximum of Tuple is', res)

4. SUM OF TUPLE ELEMENTS:

test_tup = (7, 8, 9, 1, 10, 7)


print("The original tuple is : " + str(test_tup))
res = 0
for i in test_tup:
res += i
print("The summation of tuple elements are : " + str(res))

EXERCISE NO: 02B: OPERATIONS ON LISTS:


1. CREATING AND ACCESSING A LIST AND PRINTING ITS
LENGTH:

Marks = [10, 23, 41, 75]


i=0
while i < 4:
print (Marks[i])
i=i+1
print("Length of the given list is ",len(Marks))

2. PROGRAM TO CHANGE RANGE OF VALUES IN A LIST:

MyList = [1, 3, 5, 7, 9]
print ("List Odd numbers... ")
for x in MyList:
print (x)
MyList[0:5] = 2,4,6,8,10
print ("List Even numbers... ")
for y in MyList:
print (y)

3. ADDING MORE ELEMENTS TO LIST:

Mylist=[34, 45, 48,60,73]


Mylist.append(90)
print(Mylist)
Mylist.extend([71, 32, 29])
print(Mylist)

MyList=[34,98,47,'Kannan', 'Gowrisankar', 'Lenin',


'Sreenivasan' ]
print(MyList)
MyList.insert(3, 12)
print(MyList)
4. DELETING ELEMENTS FROM LIST:

MyList=[12,89,34,'Kannan', 'Gowrisankar', 'Lenin']


print(MyList)

MyList.remove(89) #REMOVES A SINGLE ELEMENT


print(MyList)

MyList.pop(1) #REMOVES A ELEMENT BY INDEX


print(MyList)

MyList.clear( ) #REMOVES ALL ELMENTS LEAVING THE


LIST
print(MyList)

del MyList
print(MyList)

BASIC PROGRAMS:
1. GIVEN COUNTRY IS PART OF BRICS OR NOT:

country=["India", "Russia", "Srilanka", "China", "Brazil"]


is_member = input("Enter the name of the country: ")
if is_member in country:
print(is_member, " is the member of BRICS")
else:
print(is_member, " is not a member of BRICS")

You might also like