Python programs
1)
#Using intersection()
list1=[10,20,30,40,50,'geeks']
list2=[10,20,'geeks']
if(set(list1).intersection(set(list2))==set(list2)):
print("Sublist exist")
else:
print("Sublist not exist")
#Getting i/p from user
l1=list(input(""))
print("List1:",l1)
l2=list(input(""))
print("List2:",l2)
flag=False
for i in range(len(l1)-len(l2)+1):
if l1[i:i+len(l2)]==l2:
flag=True
break
print("Is sublist present in list....",flag)
#Using for loop
list1=[10,20,30,40,50]
list2=[10,20]
list=False
for i in range(0,len(list1)):
j=0
while((i+j)<len(list1) and j<len(list2) and list1[i+j] == list2[j]):
j+=1
if j==len(list2):
list=True
break
if(list):
print("Sublist exist")
else:
print("Sublist not exist")
#Using subset()
list1=[10,20,30,40,50]
list2=[10,20]
print(set(list2).issubset(set(list1)))
OUTPUT:
Sublist exist
123456
List1: ['1', '2', '3', '4', '5', '6']
123
List2: ['1', '2', '3']
Is sublist present in list.... True
Sublist exist
True
2)
def dict(dictionary):
values = list(dictionary.values())
average = sum(values) / len(values)
new_dict = {key: average for key in dictionary}
return new_dict
dict1 = {'a': 10, 'b': 20, 'c': 30}
new_dict= dict(dict1)
print("Dictionary:",dict1)
print("Values Replaced by Average:", new_dict)
OUTPUT:
Dictionary: {'a': 10, 'b': 20, 'c': 30}
Values Replaced by Average: {'a': 20.0, 'b': 20.0, 'c': 20.0}
3)
t1=(1,4,5,6,11,30,40,56)
t2=('Python','java',)
print("Tuple elements:",t1+t2)
print("Length:",len(t1))
print("3rd element:",t1[3])
print("Value at t2[-1]:",t2[-1])
print("Repetition:",t2*3)
print("Slicing:",t1[2:5])
print("Slicing:",t1[1:])
print("Slicing:",t1[:4])
print("Slicing:",t1[2:5:2])
print("Slicing:",t1[::-2])
l1=[23,34,45]
print("List1:",tuple(l1))
OUTPUT:
Tuple elements: (1, 4, 5, 6, 11, 30, 40, 56, 'Python', 'java')
Length: 8
3rd element: 6
Value at t2[-1]: java
Repetition: ('Python', 'java', 'Python', 'java', 'Python', 'java')
Slicing: (5, 6, 11)
Slicing: (4, 5, 6, 11, 30, 40, 56)
Slicing: (1, 4, 5, 6)
Slicing: (5, 11)
Slicing: (56, 30, 6, 4)
List1: (23, 34, 45)
4)
import numpy as np
array = np.arange(8)
print("Original array")
print(array)
print("Power of 3 for every element-wise:")
print(np.power(array, 3))
a1= np.arange(5)
a2 = np.arange(0, 10, 2)
print("Array1:",a1)
print("Array2:", a2)
print("Power of array1 to array2",a1**a2)
OUTPUT:
Original array
[0 1 2 3 4 5 6 7]
Power of 3 for every element-wise:
[ 0 1 8 27 64 125 216 343]
Array1: [0 1 2 3 4]
Array2: [0 2 4 6 8]
Power of array1 to array2 [ 1 1 16 729 65536]
5)
def ispangram(str):
alphabet = "abcdefghijklmnopqrstuvwxyz"
for char in alphabet:
if char not in str.lower():
return False
return True
string = input(“Enter a string:”)
if(ispangram(string) == True):
print("This is pangram")
else:
print("This is not pangram")
OUTPUT:
Enter a string:qwertyuiopasdfghjklzxcvbnm
This is pangram
6)
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold, cross_val_score
X, y = datasets.load_iris(return_X_y=True)
clf = DecisionTreeClassifier(random_state=42)
k_folds = KFold(n_splits = 5)
scores = cross_val_score(clf, X, y, cv = k_folds)
print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print("Number of CV Scores used in Average: ", len(scores))
Output:
Cross Validation Scores: [1. 1. 0.83333333 0.93333333 0.8 ]
Average CV Score: 0.9133333333333333
Number of CV Scores used in Average: 5
7)
import csv
with open('sample1.csv') as sam:
file=csv.reader(sam)
for row in file:
print(row)
sample.csv
ROLL.no Name Department
23MCA001 Abi MCA
23MCA002 Amudha MCA
23MCA003 Aruljothi MCA
23MCA004 Divya MCA
23MCA005 Duruv MCA
23MCA006 Kalai MCA
23MCA007 Mano MCA
23MCA008 Pavithra MCA
23MCA009 Priya MCA
23MCA010 Saranya MCA
OUTPUT:
['ROLL.no', 'Name', 'Department']
['23MCA001', 'Abi', 'MCA']
['23MCA002', 'Amudha', 'MCA']
['23MCA003', 'Aruljothi', 'MCA']
['23MCA004', 'Divya', 'MCA']
['23MCA005', 'Duruv', 'MCA']
['23MCA006', 'Kalai', 'MCA']
['23MCA007', 'Mano', 'MCA']
['23MCA008', 'Pavithra', 'MCA']
['23MCA009', 'Priya', 'MCA']
['23MCA010', 'Saranya', 'MCA']