Uploaded by Rohit Anand

Python answers (2)

advertisement
1.
Write a program that prompts a user to enter his first name and last name and then displays
message “welcome to firstname lastname.”
Answer:
Code:
fname=input("Enter the first name:")
lname=input("Enter the last name:")
print("welcome to",fname,lname)
Output:
Enter the first name:George
Enter the last name:Bailey
welcome to George Bailey
2.
Write a python program to enter the number between 1 to 7 and display the corresponding day of
the number, if the number is not valid then displays the message “Enter the correct number
between 1 to 7”.
Answer:
Code:
n=int(input("Enter a number(1-7):"))
if(n==1):
print("Sunday")
elif(n==2):
print("Monnday")
elif(n==3):
print("Tuesday")
elif(n==4):
print("Wednesday")
elif(n==5):
print("Thursday")
elif(n==6):
print("Friday")
elif(n==7):
print("Saturday")
else:
print("Invalid number, Enter the correct number between 1 to 7")
Output:
Enter a number(1-7):5
Thursday
1.
Write a Python program that defines a list of countries that are a member of BRICS. Check whether
a country is a member of BRICS or not
Answer:
Code:
BRICS=["Brazil","Russia","India","China","Sri Lanka"]
country=input("Enter the name of the country:")
if country in BRICS:
print(country,"is the member of BRICS")
else:
print(country,"is not the member of BRICS")
Output:
Enter the name of the country:India
India is the member of BRICS
2.
Write a python program that creates a list of 10 random integers. Then create two list – odd list and
event list that has all odd and even values in the list respectively
Answer:
Code:
def split(mix):
even_list=[]
odd_list=[]
for i in mix:
if(i%2==0):
even_list.append(i)
else:
odd_list.append(i)
print("Even:",even_list)
print("Odd:",odd_list)
list=[2, 5, 13, 17, 51, 62, 73, 84, 95]
split(list)
Output:
Even: [2, 62, 84]
Odd: [5, 13, 17, 51, 73, 95]
3.
Write a Python program to check whether the entered input is number or a character and if it is
number check it is odd or even.
Answer:
Code:
n=input("Enter an input:")
if(n.isdigit()):
if(int(n)%2==0):
print(n,"is even number")
else:
print(n,"is odd number")
else:
print("Its a character")
Output:
Enter an input:1
1 is odd number
1.
Given three integers between 1 and 11, if there sum is less than or equal to 21, return their sum, if
their sum exceeds 21 and there is a number 11, reduce the total sum by 10, finally, if the sum
exceeds 21 return “BUST”.
Answer:
Code:
def check(a,b,c):
if sum((a,b,c))<=21:
return sum((a,b,c))
elif sum((a,b,c))>21 and 11 in (a,b,c):
return sum((a,b,c))-10
else:
return 'BUST'
f=int(input("Enter the first number:"))
s=int(input("Enter the second number:"))
t=int(input("Enter the third number:"))
print(check(f,s,t))
Output:
Enter the first number:10
Enter the second number:2
Enter the third number:3
15
2.
Write a Python program to perform the following operation on file
a.Read
b. Write
c.append
Answer:
Code:
rs=input("Enter the file name for reading:")
ws=input("Enter the file name for writing:")
ap=input("Enter the file name for appending:")
f1=open(rs,"r")
f2=open(ws,"w")
f3=open(ap,"w+")
str1=f1.read()
print(str1)
f2.write("Hello")
f3.write(str1)
f3.seek(0)
wr=f3.read()
print(wr)
f1.close()
f2.close()
f3.close()
Output:
Enter the file name for reading:New file
Enter the file name for writing:File
Enter the file name for appending:File 1
Welcome To python programming
Welcome To python programming
1.
Write a program with class “Employee” that keeps a track of the number of employee is an
organization and also store their name, designation, and salary details
Answer:
Code:
class Employee:
__name=""
__designation=""
__salary=0
def setData(self):
self.__name = input("Enter Name\t:")
self.__designation = input("Enter Designation:")
self.__salary = int(input("Enter Salary\t:"))
def showData(self):
print("Name\t:", self.__name)
print("Designation:", self.__designation)
print("Salary\t:", self.__salary)
emp=Employee()
emp.setData()
emp.showData()
Output:
Enter Name:Rohit
Enter Designation:Manager
Enter Salary:100000
Name: Rohit
Designation: Manager
Salary: 100000
2.
Write a program that accepts file name as an input from the user. Open the file and count the
number of times a character appears in the file.
Answer:
Code:
count=0
fl=input("Enter the file name for reading:")
F=open(fl,"r")
txt=F.read()
for ch in txt:
count+=1
print("Number of character is",count)
Output:
Enter the file name for reading:New File
Number of character is 39
Download