Saturday, 3 August 2019
Assignment No: 16 Character Conversion UPPER to lower and lower to UPPER case
'''
Assignment No: 16
To copy contents of one file to other. While copying a) all full stops are to be
replaced with commas b) lower case are to be replaced with upper case c) upper case
are to be replaced with lower case.
'''
try:
fin = open("input.txt",'r') # file opening in read mode
fout = open("output.txt",'w') # file is created
for line in fin:
line = line.swapcase() #Replace lower case with upper and upper with lower
line = line.replace(".","*") #Replace full stops with *
fout.write(line)
except:
print("File error occured")
Assignment No: 16
To copy contents of one file to other. While copying a) all full stops are to be
replaced with commas b) lower case are to be replaced with upper case c) upper case
are to be replaced with lower case.
'''
try:
fin = open("input.txt",'r') # file opening in read mode
fout = open("output.txt",'w') # file is created
for line in fin:
line = line.swapcase() #Replace lower case with upper and upper with lower
line = line.replace(".","*") #Replace full stops with *
fout.write(line)
except:
print("File error occured")
Assignment NO: 11 To generate pseudo random numbers
'''
Assignment NO: 11
To generate pseudo random numbers.
'''
import random
try:
start = int(input("Enter start point: "))
end = int(input("Enter end point: "))
num = random.randint(start,end)
print(num)
except:
print("Enter start and end point as integer")
Assignment NO: 11
To generate pseudo random numbers.
'''
import random
try:
start = int(input("Enter start point: "))
end = int(input("Enter end point: "))
num = random.randint(start,end)
print(num)
except:
print("Enter start and end point as integer")
Assignment No: 10 Binary to Decimal Conversion
'''
Assignment No: 10
To input binary number from user and convert it into decimal number.
'''
def binaryToDecimal(binary): # function definition
binary1 = binary
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
print(decimal)
# Driver code
if __name__ == '__main__':
binaryToDecimal(100)
binaryToDecimal(101)
binaryToDecimal(1001)
Assignment No: 10
To input binary number from user and convert it into decimal number.
'''
def binaryToDecimal(binary): # function definition
binary1 = binary
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
print(decimal)
# Driver code
if __name__ == '__main__':
binaryToDecimal(100)
binaryToDecimal(101)
binaryToDecimal(1001)
Assignment No: 9 Printing Reverse No using Function
'''
Assignment No: 9
To accept a number from user and print digits of number in a reverse order using built-in function.
'''
Number = int(input("Please Enter any Number: "))
Reverse = 0
while(Number > 0):
Reminder = Number %10
Reverse = (Reverse *10) + Reminder
Number = Number //10
print("\n Reverse of entered number is = %d" %Reverse)
Assignment No: 9
To accept a number from user and print digits of number in a reverse order using built-in function.
'''
Number = int(input("Please Enter any Number: "))
Reverse = 0
while(Number > 0):
Reminder = Number %10
Reverse = (Reverse *10) + Reminder
Number = Number //10
print("\n Reverse of entered number is = %d" %Reverse)
Assignment No: 6 Simple Calculator
'''
Assignment No: 6
To simulate simple calculator that performs basic tasks such as,
addition,
subtraction,
multiplication and division.
'''
def add(x, y): # function definition of addition
return x + y
def subtract(x, y): # function definition of subtraction
return x - y
def multiply(x, y): # function definition multiplication
return x * y
def divide(x, y): # function definition of divide
return x / y
def main():
while(1):
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4):")
try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
except:
print("Enter numbers as integers")
continue
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
if __name__ == '__main__':
main()
Assignment No: 6
To simulate simple calculator that performs basic tasks such as,
addition,
subtraction,
multiplication and division.
'''
def add(x, y): # function definition of addition
return x + y
def subtract(x, y): # function definition of subtraction
return x - y
def multiply(x, y): # function definition multiplication
return x * y
def divide(x, y): # function definition of divide
return x / y
def main():
while(1):
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4):")
try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
except:
print("Enter numbers as integers")
continue
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
if __name__ == '__main__':
main()
Assignment No: 4 Student Result Calculation
'''
Assignment No: 4
To accept students five courses marks and compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course.
If student scores aggregate greater than 75%, then the grade is distinction.
If aggregate is 60>= and <75 then the grade if first division.
If aggregate is 50>= and <60, then the grade is second division.
If aggregate is 40>= and <50, then the grade is third division.
'''
class Student: # creation of class
def __init__(self): # __init__() function to assign values to object properties
self.marks = []
self.passed = True
def input(self): # input function is defined using the def keyword.
#Self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
try:
for i in range(5):
mark = float(input("Marks: "))
self.marks.append(mark)
if mark < 40:
self.passed = False
except:
print("Enter marks in digits.")
def grade(self): #grade function is defined
if self.passed:
average = sum(self.marks)/5
if average>75:
print("Distinction")
elif average >= 60 and 75> average:
print("First Division")
elif average >= 50 and 60> average:
print("Second Division")
else:
print("Third Division")
else:
print("Failed")
if __name__ == '__main__':
student = Student()
student.input() # function calling
student.grade()
Assignment No: 4
To accept students five courses marks and compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course.
If student scores aggregate greater than 75%, then the grade is distinction.
If aggregate is 60>= and <75 then the grade if first division.
If aggregate is 50>= and <60, then the grade is second division.
If aggregate is 40>= and <50, then the grade is third division.
'''
class Student: # creation of class
def __init__(self): # __init__() function to assign values to object properties
self.marks = []
self.passed = True
def input(self): # input function is defined using the def keyword.
#Self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
try:
for i in range(5):
mark = float(input("Marks: "))
self.marks.append(mark)
if mark < 40:
self.passed = False
except:
print("Enter marks in digits.")
def grade(self): #grade function is defined
if self.passed:
average = sum(self.marks)/5
if average>75:
print("Distinction")
elif average >= 60 and 75> average:
print("First Division")
elif average >= 50 and 60> average:
print("Second Division")
else:
print("Third Division")
else:
print("Failed")
if __name__ == '__main__':
student = Student()
student.input() # function calling
student.grade()
Subscribe to:
Posts (Atom)
Microsoft’s latest Surface Pro X is now available in India
Microsoft Surface Pro X 2020 is now available in India at a starting price of Rs 1,49,999. Here's everything you would like to unders...
-
''' Assignment No: 2 To accept an object mass in kilograms and velocity in meters per second and display its momentum. Momen...
-
''' Assignment No: 1 To calculate salary of an employee given his basic pay (take as input from user). Calculate gross salar...
-
''' Assignment No: 4 To accept students five courses marks and compute his/her result. Student is passing if he/she scores m...