You are on page 1of 30

1: Python programming using simple statements and expressions (exchange the values of two

variables, circulate the values of n variables, distance between two points).

1a. Program 1: Swapping two numbers using


temporary variable

code:
# Python program to swap two variables

num1 = input('Enter First Number: ')


num2 = input('Enter Second Number: ')

print("Value of num1 before swapping: ", num1)


print("Value of num2 before swapping: ", num2)

# swapping two numbers using temporary variable


temp = num1
num1 = num2
num2 = temp

print("Value of num1 after swapping: ", num1)


print("Value of num2 after swapping: ", num2)

output:
Program 2: Swapping two numbers without using
temporary variable
# Python program to swap two variables

num1 = input('Enter First Number: ')


num2 = input('Enter Second Number: ')

print("Value of num1 before swapping: ", num1)


print("Value of num2 before swapping: ", num2)

# swapping two numbers without using temporary variable


num1, num2 = num2, num1

print("Value of num1 after swapping: ", num1)


print("Value of num2 after swapping: ", num2)
output:

1b. circulate the value of n variables

code:

def circulate(A,N):

for(I in range(1,N+1));

B=A[i:]+A[:i]

print(“circulation”,I”,=”,B)
return

output

1c. Aim: Write a program to compute distance between two points taking input from the user
(Pythagorean Theorem).

Description: The Pythagorean theorem is the basis for computing distance between two points. Let
(x1,y1) and (x2,y2) be the co-ordinates of points on xy-plane. From Pythagorean theorem, the distance
between two points is calculated using the formulae:

√[(x₂ - x₁)² + (y₂ - y₁)²]


To find the distance, we need to use the method sqrt(). This method is not accessible directly, so we
need to import math module and then we need to call this method using math static object. To find the
power of a number, we need to use ** operator.

Algorithm:

Input: x1,y1,x2 and y2

Output: Distance between two points.

Step1: Start

Step2: Import math module

Step3: Read the values of x1,y1,x2 and y2

Step4: Calculate the distance using the formulae math.sqrt ( (x2 - x1)**2 + (y2 - y1)**2 ) and store the

result in distance

Step5: Print distance

Step6: Stop

for e.g : let x1 , y1=10,9 and x2 , y2=4,1

then (x2-x1) 2=(10-4)2 = 62 = 36 and (y2-y1) 2= (9- 1)2 = 82 = 64

now 64 + 36 =100 and

100 is square root of 10 sp distance between (10,9) and (4,1) is 10

Code:-

x1=int(input("enter x1 : "))

x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))

y2=int(input("enter y2 : "))

result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)

print("distance between",(x1,x2),"and",(y1,y2),"is : ",result)

Output:

2. 2: Scientific problems using Conditionals and Iterative loops

OBJECTIVES:

a. To checking whether the given number is even number or not.

b. To find the factorial of a number.

c. To print the prime numbers below 100

RESOURCE: Python 3.7.3 PROGRAM

LOGIC: Checking whether the given number is even number or not

1. Read the number.

2. Divide the number by 2

3. Compare the reminder value


4. Display the result

Finding the factorial of a number

1. Read the number.

2. Decrement the value

3. Compare the reminder value

4. Display the result

Print the prime numbers below 100

1. Read the number.

2. Check the number prime or not

3. If yes print the number

4. If not decrement the value

5. Check the number reaches zero.

6. If not goto step2

7. End of the program.

PROCEDURE:

a. Create: Open a new file in Python shell, write a program and save the program with .py extension.

b. Execute : Go to Run -> Run module (F5)

SOURCE CODE:

Checking whether the given number is even number or not

a=int(input())

if(a%2==0):

print("Even")

else: print("Odd")

Input: 4

Output: Even 10

Finding the factorial of a number

a=int(input())

k=1;

for i in range(1,a+1):
k=k*i;

print("The factorial of given number is", k)

input: 5

Output: The factorial of given number is 120

Print the prime numbers below 100

for i in range(2,100):

c=0;

for j in range(1,i+1):

if(i%j==0):

c=c+1 if(c<=2):

print(i,end =" "),

Output: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

PRE LAB VIVA QUESTIONS:

a. Define loop process?

b. How many loops are there?

c. Explain the syntax of for loop?

POST LAB VIVA QUESTIONS:

a. What do you understand by loop?

b. Explain the nested control statements?

c. Differentiate between for loop and while loop

d. On which data types only for loop applicable

B) Write a python program to print a number is positive/negative using if-else.

n = float(input("Input a number: "))


if n >= 0:
if n == 0:
print("It is Zero!")
else:
print("Number is Positive number.")
else:
print("Number is Negative number.")
C) Write a python program to find largest number among three numbers.
# Python Program to find Largest of Three Numbers using Elif Statement

a = float(input("Please Enter the First value: "))

b = float(input("Please Enter the First value: "))

c = float(input("Please Enter the First value: "))

if (a > b and a > c):

print("{0} is Greater Than both {1} and {2}". format(a, b, c))

elif (b > a and b > c):

print("{0} is Greater Than both {1} and {2}". format(b, a, c))

elif (c > a and c > b):

print("{0} is Greater Than both {1} and {2}". format(c, a, b))

else:

print("Either any two values or all the three values are equal")

D) Write a python Program to read a number and display corresponding day using if_elif_else?

# Taken day number from user

weekday = int(input("Enter weekday day number (1-7) : "))

if weekday == 1 :

print("\nMonday");
elif weekday == 2 :

print("\nTuesday")

elif(weekday == 3) :

print("\nWednesday")

elif(weekday == 4) :

print("\nThursday")

elif(weekday == 5) :

print("\nFriday")

elif(weekday == 6) :

print("\nSaturday")

elif (weekday == 7) :

print("\nSunday")

else :

print("\nPlease enter weekday number between 1-7.")

3a. 3: Linear search and Binary search

3.a) LINEAR SEARCH


Aim:
To write a Python Program to perform Linear Search
Algorithm:
1.Read n elements into the list
2.Read the element to besearched
3.If alist[pos]==item, then print the position of theitem
4.Else increment the position and repeat step3 until pos reaches
the length of the list
Program:
def search(alist,item):
pos=0
found = False
stop = False
while pos<len(alist) and not found and not stop:
if alist[pos]==item:
found = True
print("element found in position",pos)
else:
if alist[pos]>item:
stop=True
else:
pos=pos+1
return found
a=[]
n=int(input("enter the upper limit"))
print("enter",n,"numbers")
for i in range(0,n):
e=int(input())
a.append(e)
x=int(input("Enter element to search"))
search(a,x)
Sample Output:
enter the upper limit5
enter 5 numbers
4
3
78
34
23
Enter element to search78
element found in position 2
Result:
Thus the Python Program to perform linear search is executed
successfully and the output is verified.

3.b) BINARY SEARCH

Aim: To write a Python Program to perform binary search.

Algorithm:
1.Read the search element
2.Find the middle element in the sorted list
3.Compare the search element with the middle element
a. if both are matching, print elementfound
b. else then check if the search element is smaller or larger
than the middle element
4.If the search element is smaller than the middle element, then
repeat steps 2 and 3 for the left sublist of the middle element
5. If the search element is larger than the middle element, then
repeat steps 2 and 3 for the right sublist of the middle element
6.Repeat the process until the search element if found in the list
7. If element is not found, loop terminates
Program:
def bsearch(alist,item):
first = 0
last =len(alist)-1
found = False
mid=int((first+last)/2)
while first<=last and not found:
if alist[mid]==item:
found = True
print("element found in position",mid)
else:
if item<alist[mid]:
last = mid-1
else:
first = mid+mid-1
mid=int((first+last)/2)
if found == False:
print("Element not found")
return found
a=[]
n=int(input("Enter upper limit:"))
print("Enter",n,"numbers")
for i in range(0,n):
e=int(input())
a.append(e)
x=int(input("Enter element to search"))
bsearch(a,x)
Sample Output:
Enter upper limit:10
Enter 10 numbers
12
13
14
15
16
23
26
28
31
35
Enter element to search31
element found in position 8
Enter upper limit:5
Enter 5 numbers
11
17
21
27
32
Enter element to search31
Element not found
Result:
Thus the Python Program to perform binary search is executed
successfully and the output is verified.
4. 4: Selection sort, Insertion sort
4.a) SELECTION SORT
Aim:
To write a Python Program to perform selection sort.
Algorithm:
1.Create a function named selectionsort
2.Initialise pos=0
3.If alist[location]>alist[pos] then perform the following till i+1,
4.Set pos=location
5.Swap alist[i] and alist[pos]
6. Print the sorted list
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos = 0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos=location
temp = alist[i]
alist[i]=alist[pos]
alist[pos]=temp
alist = []
n=int(input("enter the upper limit"))
print("enter",n,"numbers")
for i in range(0,n):
x=int(input())
alist.append(x)
selectionSort(alist)
print(“The Sorted list are”)
print(alist)
Sample Output:
enter the upper limit5
enter 5 numbers
14
12
35
48
95
The sorted list are
[12, 14, 35, 48, 95]
Result: Thus the Python Program to perform selection sort is
successfully executed and the output is verified.

4. b ) INSERTION SORT
Aim: To write a Python Program to perform insertion sort.
Algorithm:
1. Create a function named insertionsort
2. Initialise currentvalue=alist[index] and position=index
3. while position>0 and alist[position-1]>currentvalue, perform the
following till len(alist)
4.alist[position]=alist[position-1]
5. position =position-1
6. alist[position]=currentvalue
7. Print the sortedlist
Program:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = []
n=int(input("enter the upper limit"))
print("enter",n,"numbers")
for i in range(0,n):
x=int(input())
alist.append(x)
insertionSort(alist)
print("The Sorted list are")
print(alist)
Sample Output:
enter the upper limit10
enter 10 numbers
34
26
10
32
78
91
105
36
59
47
The Sorted list are
[10, 26, 32, 34, 36, 47, 59, 78, 91, 105]
Result:
Thus the Python program to perform insertion sort is successfully
execute and the output is verified
5: Merge sort, Quick Sort

5a. MERGE SORT


Aim: To write a Python Program to perform Merge sort.
Algorithm:
1.Create a function named mergesort
2.Find the mid of the list
3.Assign lefthalf = alist[:mid] and righthalf = alist[mid:]
4.Initialise i=j=k=0
5. while i < len(lefthalf) and j < len(righthalf), perform the following
if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] Increment I else
alist[k]=righthalf[j] Increment j Increment k
6.while i < len(lefthalf),perform the following
alist[k]=lefthalf[i]Increment i Increment k
7.while j < len(righthalf), perform the following
8.alist[k]=righthalf[j] Increment j Increment k
9. Print the sorted list
Program:
#function for merging two sub-arrays
def merge(left, right, Array):
i=0
j=0
k=0
while (i<len(left) and j<len(right)):
if(left[i]<right[j]):
Array[k] = left[i]
i = i+1
else:
Array[k] = right[j]
j = j+1
k = k+1
while(i<len(left)):
Array[k] = left[i]
i = i+1
k = k+1
while(j<len(right)):
Array[k] = right[j]
j = j+1
k = k+1
#function for dividing and calling merge function
def mergesort(Array):
n = len(Array)
if(n<2):
return
mid = n//2
left = []
right = []
for i in range(mid):
number = Array[i]
left.append(number)
for i in range(mid,n):
number = Array[i]
right.append(number)
mergesort(left)
mergesort(right)
merge(left,right,Array)
#calling mergesort
Array = []
n=int(input("enter the upper limit"))
print("enter",n,"numbers")
for i in range(0,n):
x=int(input())
Array.append(x)
mergesort(Array)
print("The Sorted list are")
print(Array)
Sample Output:
enter the upper limit10
enter 10 numbers
16
11
76
23
10
9
4
98
26
76
The Sorted list are
[4, 9, 10, 11, 16, 23, 26, 76, 76, 98]
Result:
Thus the Python program to perform merge sort is successfully
executed and the output is verified.

5b. quick sort


# Quick sort in Python

# function to find the partition position


def partition(array, low, high):

# choose the rightmost element as pivot


pivot = array[high]

# pointer for greater element


i = low - 1

# traverse through all elements


# compare each element with pivot
for j in range(low, high):
if array[j] <= pivot:
# if element smaller than pivot is found
# swap it with the greater element pointed by i
i = i + 1

# swapping element at i with element at j


(array[i], array[j]) = (array[j], array[i])

# swap the pivot element with the greater element specified by i


(array[i + 1], array[high]) = (array[high], array[i + 1])

# return the position from where partition is done


return i + 1

# function to perform quicksort


def quickSort(array, low, high):
if low < high:

# find pivot element such that


# element smaller than pivot are on the left
# element greater than pivot are on the right
pi = partition(array, low, high)

# recursive call on the left of pivot


quickSort(array, low, pi - 1)

# recursive call on the right of pivot


quickSort(array, pi + 1, high)

data = [8, 7, 2, 1, 0, 9, 6]
print("Unsorted Array")
print(data)

size = len(data)

quickSort(data, 0, size - 1)

print('Sorted Array in Ascending Order:')


print(data)

6: Implementing applications using Lists, Tuples.

1. Create a Python List

Defining a List in Python is easy. You just need to provide name of the list and initialize it with
values. Following is an example of a List in Python :

>>> myList = ["The", "earth", "revolves", "around", "sun"]

>>> myList

['The', 'earth', 'revolves', 'around', 'sun']

So you can see that a list named ‘myList’ was created with some elements.

Lists in python are zero indexed. This means, you can access individual elements in a list just as
you would do in an array. Here is an example :

>>> myList[0]

'The'

>>> myList[4]
'sun'

Lists cannot be accessed beyond the rightmost index boundary. Here is an example :

2. Add Elements to a List

One can use the method insert, append and extend to add elements to a List.

The insert method expects an index and the value to be inserted. Here is an example of insert :

>>> myList.insert(0,"Yes")

>>> myList

['Yes', 'The', 'earth', 'revolves', 'around', 'sun']

So we see the value ‘yes’ was inserted at the index 0 in the list and all the other elements were
shifted accordingly.

The append method can take one or more elements as input and appends them into the list.
Here is an example :

>>> myList.append(["a", "true"])

>>> myList

['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true']]

So, the two values were appended towards the end of the list. Note that whether we add one
element or multiple elements to the list, if we have used append then they will be added as a
single element only. This can be proven if we try to check the length of myList now :

>>> len(myList)

So we see that though we added two elements but they are counted as a single element (or a
sub-list) in myList.

The other method that can be used to add elements to list is extend. Like append, it also expects
one or more values as input. But, unlike append, all the elements are added as individual
elements. Here is an example :

>>> myList.extend(["statement", "for", "sure"])

>>> myList

['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']

>>> len(myList)

10

So we see that each element got added as a separate element towards the end of the list.
3. Slice Elements from a List

Python also allows slicing of the lists. You can access a part of complete list by using index range.
There are various ways through which this can be done. Here are some examples :

If it is required to access a sub-list from index 1 to index 3 then it can be done in following way:

>>> myList[1:4]

['The', 'earth', 'revolves']

Note that the index 4 was passed instead of 3 because if we pass index range x:y then values up
to index y-1 are printed.

Now, if it is required to access first ‘n’ elements of the list, then there is no need to provide
index ‘0’, only the value of ‘n’ is required. Here is an example :

>>> myList[:4]

['Yes', 'The', 'earth', 'revolves']

So we see that first three elements were accessed.

Similarly, if last ‘n’ elements are required to be accessed then only starting index is required.
Here is an example :

>>> myList[4:]

['around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']

So we see that all the elements beginning from index 4 till end of the list were displayed.

If neither starting index, nor the ending index is provided then complete list is displayed. Here is
an example :

>>> myList[:]

['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']

4. Search the Lists and find Elements

Lists can easily be searched for values using the index method that expects a value that is to be
searched. The output is the index at which the value is kept.

Here is an example :

Here we try to search the list for value ‘revolves’.

>>> myList.index("revolves")

So we see that the corresponding index was displayed in the output.

If some value is not found then an error is displayed. Here is an example :


>>> myList.index("a")

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: 'a' is not in list

Here is how you can search a sub list :

>>> myList.index(["a", "true"])

If it is desired to just whether an element is present in a list, it can be done in following way :

>>> "sun" in myList

True

So we see that ‘True’ was returned as ‘sun’ was present in the list.

5. Delete Elements from the List

Python provides the remove method through which we can delete elements from a list. It
expects the value which is required to be deleted.

Here are some examples :

>>> myList

['Yes', 'The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']

>>> myList.remove("Yes")

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'statement', 'for', 'sure']

>>> myList.remove("statement")

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', ['a', 'true'], 'for', 'sure']

Here we see that remove can be used to easily delete elements in list. Here is how a sub list can
be deleted :

>>> myList.remove(["a", "true"])

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure']

So we see that the sub list was deleted from the original list.
If it is required to access the last element and then to delete it, this can be done through pop
method.

>>> myList.pop()

'sure'

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', 'for']

So we see that the value was displayed and deleted simultaneously.

6. Python List Operators

Python allows to use mathematical operators like +, * etc to be used with lists.

Here are some examples :

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', 'for']

>>> myList = myList + ["sure"]

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure']

>>> myList += ["."]

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.']

>>> myList *= 2

>>> myList

['The', 'earth', 'revolves', 'around', 'sun', 'for', 'sure', '.', 'The', 'earth', 'revolves', 'around', 'sun',
'for', 'sure', '.']

So we see that through + operator elements could be added to the list and through * operator
we can add the complete list repeatedly towards the end.

# Python code to demonstrate the working of

# del and pop()

# initializing list

lis = [2, 1, 3, 5, 4, 3, 8]

# using del to delete elements from pos. 2 to 5

# deletes 3,5,4
del lis[2 : 5]

# displaying list after deleting

print ("List elements after deleting are : ",end="")

for i in range(0, len(lis)):

print(lis[i], end=" ")

print("\r")

# using pop() to delete element at pos 2

# deletes 3

lis.pop(2)

# displaying list after popping

print ("List elements after popping are : ", end="")

for i in range(0, len(lis)):

print(lis[i], end=" ")

Output:

List elements after deleting are : 2 1 3 8

List elements after popping are : 2 1 8

3. insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It
takes 2 arguments, position and element to be added at respective position.

4. remove() :- This function is used to delete the first occurrence of number mentioned in its
arguments.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of

# insert() and remove()

# initializing list

lis = [2, 1, 3, 5, 3, 8]

# using insert() to insert 4 at 3rd pos


lis.insert(3, 4)

# displaying list after inserting

print("List elements after inserting 4 are : ", end="")

for i in range(0, len(lis)):

print(lis[i], end=" ")

print("\r")

# using remove() to remove first occurrence of 3

# removes 3 at pos 2

lis.remove(3)

# displaying list after removing

print ("List elements after removing are : ", end="")

for i in range(0, len(lis)):

print(lis[i], end=" ")

Output:

List elements after inserting 4 are : 2 1 3 4 5 3 8

List elements after removing are : 2 1 4 5 3 8

5. sort() :- This function sorts the list in increasing order

6. reverse() :- This function reverses the elements of list.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of

# sort() and reverse()

# initializing list

lis = [2, 1, 3, 5, 3, 8]

# using sort() to sort the list

lis.sort()

# displaying list after sorting


print ("List elements after sorting are : ", end="")

for i in range(0, len(lis)):

print(lis[i], end=" ")

print("\r")

# using reverse() to reverse the list

lis.reverse()

# displaying list after reversing

print ("List elements after reversing are : ", end="")

for i in range(0, len(lis)):

print(lis[i], end=" ")

Output:

List elements after sorting are : 1 2 3 3 5 8

List elements after reversing are : 8 5 3 3 2 1

7. extend(b) :- This function is used to extend the list with the elements present in another list.
This function takes another list as its argument.

8. clear() :- This function is used to erase all the elements of list. After this operation, list
becomes empty.

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate the working of

# extend() and clear()

# initializing list 1

lis1 = [2, 1, 3, 5]

# initializing list 1

lis2 = [6, 4, 3]

# using extend() to add elements of lis2 in lis1

lis1.extend(lis2)
# displaying list after sorting

print ("List elements after extending are : ", end="")

for i in range(0, len(lis1)):

print(lis1[i], end=" ")

print ("\r")

# using clear() to delete all lis1 contents

lis1.clear()

# displaying list after clearing

print ("List elements after clearing are : ", end="")

for i in range(0, len(lis1)):

print(lis1[i], end=" ")

Output:

List elements after extending are : 2 1 3 5 6 4 3

List elements after clearing are :

Tuple

A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round
brackets.

Example

Create a Tuple:

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

print(thistuple)

Access Tuple Items

You can access tuple items by referring to the index number, inside square brackets:

Example

Return the item in position 1:

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

print(thistuple[1])

Change Tuple Values


Once a tuple is created, you cannot change its values. Tuples are unchangeable.

Loop Through a Tuple

You can loop through the tuple items by using a for loop.

Example

Iterate through the items and print the values:

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

for x in thistuple:

print(x)

Check if Item Exists

To determine if a specified item is present in a tuple use the in keyword:

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

if "apple" in thistuple:

print("Yes, 'apple' is in the fruits tuple")

Tuple Length

To determine how many items a tuple has, use the len() method:

Example

Print the number of items in the tuple:

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

print(len(thistuple))

Add Items

Once a tuple is created, you cannot add items to it. Tuples are unchangeable.

Example

You cannot add items to a tuple:

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

thistuple[3] = "orange" # This will raise an error

print(thistuple)

Remove Items

Note: You cannot remove items in a tuple.

Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:
Example

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

The tuple() Constructor

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

Example

Using the tuple() method to make a tuple:

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

print(thistuple)

Tuple Methods

Python has two built-in methods that you can use on tuples.

Method Description

count() Returns the number of times a specified value occurs in a tuple

index() Searches the tuple for a specified value and returns the position of where it was found

Source code:

# creating tuples with college names..

colleges = ("SIIET","BHARAT","GNIT", "AVN")

print("the lists in colleges tuple is",colleges)

print("we can\'t add or remove new elements in a tuple")

print("length of the tuple colleges is:",len(colleges))

# checking whether 'SIIET' is present in the tuple or not if "SIIET" in colleges:

print("Yes, 'SIIET' is in the colleges tuple")

PROGRAM-1 Date:

Aim: A) Create a list and perform the following methods 1) insert() 2) remove() 3) append() 4) len() 5)
pop() 6) clear()

Program:

a=[1,3,5,6,7,[3,4,5],"hello"]
print(a)

a.insert(3,20)

print(a)

a.remove(7)

print(a)

a.append("hi")

print(a)

len(a)

print(a)

a.pop()

print(a)

a.pop(6)

print(a)

a.clear()

print(a)

Output:

Exercise Questions:

B) Create a dictionary and apply the following methods

1) Print the dictionary items

2) access items

3) use get()

4)change values

5) use len()

C) Create a tuple and perform the following methods

1) Add items

2) len()

3) check for item in tuple

4)Access iems

Viva Questions:
1. Define list?

2. List out the methods of list?

3. What is list indexing and slicing with an example?

You might also like