0% found this document useful (0 votes)
2 views7 pages

Python Assignment-2

The document provides a series of programming assignments that cover various fundamental programming concepts in Python. These include reading user input, calculating averages, identifying Armstrong numbers, manipulating lists, generating Fibonacci sequences, handling email addresses, and implementing encryption algorithms. Each assignment includes code snippets and specific tasks to be completed.

Uploaded by

kritikav197
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views7 pages

Python Assignment-2

The document provides a series of programming assignments that cover various fundamental programming concepts in Python. These include reading user input, calculating averages, identifying Armstrong numbers, manipulating lists, generating Fibonacci sequences, handling email addresses, and implementing encryption algorithms. Each assignment includes code snippets and specific tasks to be completed.

Uploaded by

kritikav197
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Assignment 1

1. Write a program to read the numbers until -1 is encountered. Find the average of positive
numbers and negative numbers entered by user.

# creating an empty list


list = []

# number of elemetns as input


n = int(input("Enter number of elements : "))

# iterating till the range


for i in range(0, n):
ele = int(input())
if(ele==-1):
break;
else:

list.append(ele) # adding the element

print(list)
sum=0
for i in range(len(list)):
sum=sum+list[i]
print(sum)
n=len(list)
average=sum/n
print(average)

2. WAP to find the given number is an Armstrong number or not.


a=int(input("Enter a number:"))
print(a);
b=a
sum=0
while(a>0):
n=a%10
sum=sum+n*n*n
a=int(a/10)
print(sum)

if(sum==b):
print("number is armstrong")
else:
print("number is not armstrong")
3. WAP to enter a number and then calculate the sum of digits.

a=int(input("Enter a number:"))
print(a);

sum=0
while(a>0):
n=a%10
sum=sum +n
a=int(a/10)
print(sum)

4. WAP to find reverse of a number.

a=int(input("Enter a number:"))
print(a);

while(a>0):
n=a%10
print(n)
a=int(a/10)

5. WAP to print following patterns

a)
1
22
333
4444
55555

for i in range(1 ,6): # i=1 to 6


print()
for j in range(i): #j=1 to 6
print(i,end="")

b)
1
121
12321
1234321
6. WAP to print the Calendar of any given year.
Hint: import calendar and use its functions.
MODULE REFERENCE
https://docs.python.org/3.8/library/calendar
import calendar
year = 2020
# printing the calendar
print(calendar.calendar(year))

7. WAP to create a list of numbers in the range 1 to 10. Then delete all the even numbers from
the list and print the final list.
list=[i for i in range(1,10)]
print(list)

for i in list:
if(i%2 == 0):
list.remove(i)

print(list)

8. Write a program to remove all duplicates from a list.


list=[10,20,30,40,50,50,40,30]
print(list)

res = [i for n, i in enumerate(list) if i not in list[:n]]


print(res)

9. WAP to generate fibonacci sequence and store in a list. Then find the sum of the even-valued
terms.
# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))


list=[]

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
list.append(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
print(list)
sum = 0;
for i in list:
if(list[i]%2 == 0):
sum=sum+list[i]

print(sum)

10. WAP that scans an email address and forms a tuple of user name and domain.
#Program to read email id of n number of students. Store these numbers in a tuple.
#Create two new tuples, one to store only the usernames from the email IDs and second
to store domain names from the email ids.
emails = tuple()
username = tuple()
domainname = tuple()
#Create empty tuple 'emails', 'username' and domain-name
n = int(input("How many email ids you want to enter?: "))
for i in range(0,n):
emid = input("> ")
#It will assign emailid entered by user to tuple 'emails'
emails = emails +(emid,)
#This will split the email id into two parts, username and domain and return a list
spl = emid.split("@")
#assigning returned list first part to username and second part to domain name
username = username + (spl[0],)
domainname = domainname + (spl[1],)

print("\nUsername of tuple are:")


#Printing the list with the email ids
print(username)
print("\nThe domain name in the email ids are:")
#Printing the list with the domain names only
print(domainname)

11. WAP to store a sparse matrix as a dictionary


Output: {(0,3):8, (2,3): 5, (1,0):3, (1,4): 1}
Sparse Matrix
00080
30001
00050
matrix=[[0,0,0,8,0],
[3,0,0,0,1],
[0,0,0,5,0]]
Dict={}
print("Sparse Matrix")
for i in range(len(matrix)):
print("\n")
for j in range(len(matrix[i])):
print(matrix[i][j], end=' ')
if matrix[i][j]!=0:
Dict[i,j]=matrix[i][j]
print("Sparse Matrix can be represented as Dictionary:")
print(Dict)

12. WAP that has a dictionary of names of students and a list of their marks in 4 subjects.
Create another dictionary from this dictionary that has a name of the students with their total
marks. Find the student that have scored highest marks with his/her marks.

n = int(input("Enter number of students: "))

result = {}

for i in range(n):

print("Enter Details of student No.", i+1)

Phys = int(input("Marks: "))

name = input("Name: ")


Chem = int(input("Marks: "))
math = int(input("Marks: "))
bio = int(input("Marks: "))
result[name] = [Chem,math,bio,Phys]

print(result)

13. WAP that combines the lists to a dictionary.

keys = ['red', 'green', 'blue']


values = ['#FF0000','#008000', '#0000FF']
color_dictionary = dict(zip(keys, values))
print(color_dictionary)
14. WAP to encrpt and decrypt message using caesar cipher
encryption function, e(x) = (x+key)% 26
decryption function, e(x) = (x-key)% 26
al = 'abcdefhijklmnopqrstuvwxyz'
message = input("Enter message:")
key = int(input("Enter key :"))
n = len(message)
output= ""
for i in range(n):
ch =message[i]
location =al.find(ch)
new_location =(location+key)%26
output= output + al[new_location]
print(output)

n=len(output)
decrypt=""
for i in range(n):
ch=output[i]
location=al.find(ch)
new_location=(location-key)%26
decrypt = decrypt + al[new_location]
print(decrypt)

15. WAP to implement RSA Algorithm

16: wap to count no of characters in inn the string and store them in dictionary
st = input("Enter a string: ")
dic = {} #creates an empty dictionary
for ch in st:
if ch in dic: #if next character is already in the dictionary
dic[ch] += 1
else:
dic[ch] = 1 #if ch appears for the first time
for key in dic:
print(key,':',dic[key])

You might also like