Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

REWARD POINT ACTIVITY FOR PYTHON

NIGHT CLASS

BANNARI AMMAN INSTITUTE OF TECHNOLOGY

ELECTRONICS AND COMMUNICATION ENGINEERING

SUBMITTED BY,

GIRIDARAN T P

(191EC140)
Reward Points Activity

Python Night Class Evaluation Task

1. Write a Python program to remove duplicates from a list. 

a = [10,20,30,20,10,50,60,40,80,50,40]

dup_items = set()

uniq_items = []

for x in a:

if x not in dup_items:

uniq_items.append(x)

dup_items.add(x)

print(dup_items)
2. Write a Python program to print a specified list after removing the 0th, 4th
and 5th elements. 
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']

color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']

color = [x for (i,x) in enumerate(color) if i not in (0,4,5)]

print(color)

3. Write a Python program to generate all permutations of a list in Python.

import itertools

print(list(itertools.permutations([1,2,3])))
4. Write a python program to check whether two lists are circularly identical. 

list1 = [10, 10, 0, 0, 10]

list2 = [10, 10, 10, 0, 0]

list3 = [1, 10, 10, 0, 0]

print('Compare list1 and list2')

print(' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2)))

print('Compare list1 and list3')

print(' '.join(map(str, list3)) in ' '.join(map(str, list1 * 2)))


5. Write a Python program to find the second smallest number in a list. 

def second_smallest(numbers):

if (len(numbers)<2):

return

if ((len(numbers)==2) and (numbers[0] == numbers[1]) ):

return

dup_items = set()

uniq_items = []

for x in numbers:

if x not in dup_items:

uniq_items.append(x)
dup_items.add(x)

uniq_items.sort()

return uniq_items[1]

print(second_smallest([1, 2, -8, -2, 0, -2]))

print(second_smallest([1, 1, 0, 0, 2, -2, -2]))

print(second_smallest([1, 1, 1, 0, 0, 0, 2, -2, -2]))

print(second_smallest([2,2]))

print(second_smallest([2]))

6. Write a Python program to find thedef second_smallest(numbers)

if (len(numbers)<2):
return

if ((len(numbers)==2) and (numbers[0] == numbers[1]) ):

return

dup_items = set()

uniq_items = []

for x in numbers:

if x not in dup_items:

uniq_items.append(x)

dup_items.add(x)

uniq_items.sort()

return uniq_items[1]

print(second_smallest([1, 2, -8, -2, 0, -2]))

print(second_smallest([1, 1, 0, 0, 2, -2, -2]))

print(second_smallest([1, 1, 1, 0, 0, 0, 2, -2, -2]))

print(second_smallest([2,2]))

print(second_smallest([2]))
 

7. Write a Python script to sort (ascending and descending) a dictionary by


value

import operator

d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

print('Original dictionary : ',d)

sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1)))

print('Dictionary in ascending order by value : ',sorted_d)

sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1),reverse=True))

print('Dictionary in descending order by value : ',sorted_d)


8. Write a Python script to add a key to a dictionary.
Sample Dictionary : {0: 10, 1: 20}
Expected Result : {0: 10, 1: 20, 2: 30}

d = {0:10, 1:20}

print(d)

d.update({2:30})
print(d)
9.  Write a Python program to combine values in python list of dictionaries. 
Sample data: [{'item': 'item1', 'amount': 400}, {'item': 'item2', 'amount': 300},
{'item': 'item1', 'amount': 750}]
Expected Output: Counter({'item1': 1150, 'item2': 300})

from collections import Counter

item_list = [{'item': 'item1', 'amount': 400}, {'item': 'item2', 'amount': 300}, {'item':
'item1', 'amount': 750}]

result = Counter()

for d in item_list:

result[d['item']] += d['amount']
print(result)

10.Write a Python program to convert a list to a tuple. 

listx = [5, 10, 7, 4, 15, 3]

print(listx)

#use the tuple() function built-in Python, passing as parameter the list

tuplex = tuple(listx)

print(tuplex)
11. Write a Python program to get the 4th element and 4th element from last
of a tuple.

tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")

print(tuplex)

#Get item (4th element)of the tuple by index

item = tuplex[3]

print(item)

#Get item (4th element from last)by index negative

item1 = tuplex[-4]

print(item1)
12.  Write a Python program to find the index of an item of a tuple. 

tuplex = tuple("index tuple")

print(tuplex)

index = tuplex.index("p")

print(index)

index = tuplex.index("p", 5)

print(index)

index = tuplex.index("e", 3, 6)

print(index)

index = tuplex.index("y")
13. Write a Python program to replace last value of tuples in a list. 
Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]

l = [(10, 20, 40), (40, 50, 60), (70, 80, 90)]

print([t[:-1] + (100,) for t in l])


14. Write a Python program to display first 5 rows from COVID-19 dataset.
Also print the dataset information and check the missing values.

import pandas as pd

covid_data=
pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-
19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-17-2020.csv')

print(covid_data)

print("\nDataset information:")

print(covid_data.info())

print("\nMissing data information:")

print(covid_data.isna().sum())
15. Write a Python program to get the Chinese province wise cases of
confirmed, deaths and recovered cases of Novel Coronavirus (COVID-19).

import pandas as pd

covid_data=
pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-
19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-17-2020.csv')

c_data = covid_data[covid_data['Country/Region']=='China']

c_data = c_data[['Province/State', 'Confirmed', 'Deaths', 'Recovered']]

result = c_data.sort_values(by='Confirmed', ascending=False)

result = result.reset_index(drop=True)

print(result)
16. Write a Python program to list countries with all cases of Novel
Coronavirus (COVID-19) recovered.

import pandas as pd
covid_data=
pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-
19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-17-2020.csv')
data = covid_data.groupby('Country/Region')
['Confirmed','Deaths','Recovered'].sum().reset_index()
result = data[data['Confirmed']==data['Recovered']]
result = result[['Country/Region','Confirmed','Recovered']]
result = result.sort_values('Confirmed', ascending=False)
result = result[result['Confirmed']>0]
result = result.reset_index(drop=True)
print(result)

You might also like