Python Assignment-2
Python Assignment-2
1. Write a program to read the numbers until -1 is encountered. Find the average of positive
numbers and negative numbers entered by user.
print(list)
sum=0
for i in range(len(list)):
sum=sum+list[i]
print(sum)
n=len(list)
average=sum/n
print(average)
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)
a=int(input("Enter a number:"))
print(a);
while(a>0):
n=a%10
print(n)
a=int(a/10)
a)
1
22
333
4444
55555
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)
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
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],)
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.
result = {}
for i in range(n):
print(result)
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)
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])