TCS Python Assignments | 2018

1. Write a Python program to find whether a given number (accept from the user) is prime or not,print out an appropriate message to the user.?

n=int(input('Enter Number'))
if n<0:
    print("Given Number is Negative")
elif n==2:
    print(n,"is prime")
else:
    for i in range(2,n):
        if (n%i)==0:
            print(n,"is not prime")
            break
    else:

        print(n,"is prime number")


2. Write a Python program which accept the radius of a circle from the user and compute the area.

from math import pi
r = float(input ("Enter the Radius of circle: "))
print ("The area of the circle is" + str(r) + " is: " + str(pi * r**2))

3. Write a Python program to print all even numbers from a given list of numbers. The list is terminated by -99999.

list= [1, 2, 3, 4, 5, 6, 7, 8, 9, -99999]
print("Even Numbers are")
for x in list: 
        if not x % 2: 
             print(x,end=' ') 

4. Write a Python program to find the occurrence of a given number in a given list of numbers.

list = [1,2,3,1,4,1,2,5,1,2,3,1]
print("Number Of occurrence of given number is",list.count(1))

5. Write a Python program that accept a word from the user and reverse it.

word = input("Give A Word to Reverse: ")
 for char in range(len(word) - 1, -1, -1):
  print(word[char], end="")

6. Write a Python program that accept a number and prints the reverse of it.

n = int(input("Please Give A Number: "))
R = 0
while(n > 0):
    Reminder = n %10
    R = (R *10) + Reminder
    n = n //10
print("Reverse of Given Number is = %d" %R)

7. Write a Python program that accept a number and finds the summation of the digits in the number.

Number = int(input("Please Give A Number: "))
Sum = 0
while(Number > 0):
    Reminder = Number % 10
    Sum = Sum + Reminder
    Number = Number //10
print("Sum of the digits of Given Number = %d" %Sum)

8. Write a Python program to copy the content of a given file to another. Take the file names as input from user.

filename=input("Enter Filename ,")
to_readfile=open("ex.py", "r")
try:
      reading_file=to_readfile.read()

      writefile=open(filename, "w")
      print("Succefully Copied afile")
      try:
           writefile.write(reading_file)
      finally:
           writefile.close()

finally:
      to_readfile.close()

9. Define a class student with attributes roll number, name and score and methods to find grade. Grade is ‘A’ if score >=80, grade is ‘B’ if score >=60 and score<80, grade is ‘C’ if score >=50 and score<60 otherwise ‘F’.

class Student_Grade:
    def __init__(self, RollNo,Name,Score):
        self.RollNo = RollNo
        self.Name = Name
        self.Score = Score
    def grade(self):
        print("Roll No Of the Student is:",self.RollNo)
        print("Name Of the Student is:",self.Name)
        if(self.Score>=80 and self.Score<=100):
            print("Your Grade is A")
        elif(self.Score>=50 and self.Score<=60):
            print("Your Grade is B")
        else:
            print("Your Grade is F")
        return None
while True:
            print("Enter Roll Number, Name, Score of the Student: ")
            rollnumber = input()
            name = input()
            score = int(input())
            r=Student_Grade(rollnumber,name,score)
            r.grade()
            break

10. Define a class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle and one more method which will check whether the area is greater than a given value or not.

from math import pi
class Circle:
    def __init__(self,r):
        self.r = r
    def area(self):
        return pi * self.r * self.r
    def perimeter(self):
        return 2 * pi * self.r
    def check(self,area):
        if area > self.r:
            print("Area is Greater than Given Ridus")
r=Circle(5)
a=r.area()
print("Circle area is:",a)
print("Perimetere is:",r.perimeter())
r.check(a)

11. Define a class Shape with its sub class Square . The Square has an initialize function which takes as argument a length. Both classes have a function findArea which can print the area of the shape where Shape's area is 0 by default.

class Shape:
    def area(side):
        print("Area form Shape class:",side * side)
class Square(Shape):
        def __init__(self,side):
            self.side = side
        def area(self,area=0):
            print("Area from Square class:",self.side * self.side)
            Shape.area(self.side)
Square(5).area()

12. Define a class Item and its two child classes: Perishable and NonPerishable. All classes have attribute price a method "calculateSalePrice" which can print "price + (price * .1) " for nonperishable class and "price + 500" for perishable class.

class Pershable:
    def calculateSalePrice(price):
        print("Pershable Price :",price+500)
class NonPershable(Pershable):
      def __init__(self,price):
        self.price = price
      def calculateSalePrice(self):
          print("Nonpershable Price :",self.price + (self.price * .1))
          Pershable.calculateSalePrice(self.price)
x=NonPershable(100)
x.calculateSalePrice()


#!/usr/bin/python
# python program for items and its two child classes perishable and non perishable
class Items(object):
        def __init__(self,price):
               self.price = price
       
        def CalculateSalePrice(self):
               pass

class Perishable(Items):
        def __init__(self,price):
               self.price = price
       
        def CalculateSalePrice(price):
               print('The price is:',(price+500))

class NonPerishable(Items):
        def __init__(self,price):
               self.price = price
       
        def CalculateSalePrice(price):
               print('The price is:',(price+(price * 0.1)))

i = Items(10)
i.CalculateSalePrice()
i.CalculateSalePrice() 

No comments:

Post a Comment