Friday 10 November 2017

TCS aspire Python Assignments

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() 

20 comments:

  1. what type of questions will be asked in python assessment ?

    ReplyDelete
    Replies
    1. Just basics of python like this http://tcstechlounge.blogspot.in/2017/11/tcs-aspire-python-questions-answers.html

      Delete
  2. #!/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()


    its running but not showing the output

    ReplyDelete
    Replies
    1. It should be,

      class Items:
      def __init__(self,price):
      self.price = price

      def CalculateSalePrice(self):
      pass

      class Perishable(Items):
      def __init__(self,price):
      self.price = price

      def CalculateSalePrice(self):
      print('The price is:',(self.price + 500))

      class NonPerishable(Items):
      def __init__(self,price):
      self.price = price

      def CalculateSalePrice(self):
      print('The price is:',(self.price+(self.price * 0.1)))

      obj1 = Perishable(1000)
      obj2 = NonPerishable(1000)
      obj1.CalculateSalePrice()
      obj2.CalculateSalePrice()

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. this information is unique really thank you for sharing this information
    python training in Hyderabad the best career

    ReplyDelete
  5. the solution for the 8th problem is not working. does anyone have one that does?

    ReplyDelete
  6. Open a txt file in the python directory and add some text. the name of the txt file is your input. then the below code does the work.
    n=raw_input("Enter the name of the file")
    f1=open(n,"r+")
    f2=open("tha20.txt","w+")
    f2.write(f1.read())
    f2.close()
    f1.close()

    ReplyDelete
  7. Thank you for your information.it is very nice article.
    Python Training in Pune


    ReplyDelete
  8. I really enjoyed reading your blog. I really appreciate your information which you shared with us. Best Python Online Training || Learn Python Course

    ReplyDelete
  9. Such an interesting and informative piece of guidance imparted by you. I am glad to discover this information here and I am sure that this might be beneficial for us.
    Web delopment company in India

    ReplyDelete
  10. Admiring the time and effort you put into your blog and detailed information you offer!.. python training institute in pune

    ReplyDelete
  11. Thanks for sharing this informative content , Great work
    Read this PSM vs CSM blog from Leanpitch to get a better conclusion : PSM vs CSM

    ReplyDelete
  12. Trainingicon is offering Python training in Delhi NCR. We are a training institute in Delhi and Noida.We provide industrial training in programming like Python, PHP, Web designing, R Programming etc so if any body is looking to get trained into any skills, just let us know.following is the link to get enrilled into python batch
    Python Training in Noida

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete