Programs on Python (AutoRecovered)
Programs on Python (AutoRecovered)
gs=bs+hra+da
6. Write a python program to calculate electricity bill based on following parameters:-
Units Bill/unit
1-150 2.40
For next 151-300 3.00
For next more than 300 3.20
1. Write a python program to create two functions area() and peri() to find area and
perimeter of rectangle.
2. Write a python program to create a function calc(), write code to find summation,
subtraction, multiplication and division and return result in form of list. Now test
calc() function.
3. Write a python program to create a function search(), in search function pass two
parameters first, a list of ten numbers and second, a number to search. If number is
present in list return index of list otherwise return false.
4. Write a python program to create a function named substr(), in substr() function
pass a string and substring. If substring is presented in string then return true or
return false.
5. Write a python program to create a function named rev(), in rev() function pass a
string and this function return reverse string.
6. Write a python program to find factorial of given number using ‘Recursion’.
7. Write a python program to generate Fibonacci series up to n terms using ‘Recursion’.
8. Write a python program to create a module named mycalc. In mycalc module create
four functions add(), sub(), mult() and div() to find summation, subtraction,
multiplication and division respectively. Now import mycalc module and test these
functions.
9. Write a python program to create a module named tempconv. In tempconv module
create two function ctof() and ftoc(). ctof() function converts temperature from
centigrade to Fahrenheit and ftoc() function converts temperature from Fahrenheit
to centigrade. Now import tempconv module and test ctof() and ftoc() functions.
10. Write a python program to create a function random(). This function return random
number from 1-10.
1. Write a python program to create a class with name ‘MyClass’. In MyClass create a
function sayHello(). In sayHello() function pass name of user, and display user name
with Hello, message. Test class ‘MyClass’.
2. Write a python program to create a class with name ‘Employee’. In Employee class
take three instance variables empid, empname and salary. Create two methods in
Employee class first setEmployee() and second display(). setEmployee() method
initialize instance variables empid, empname, salary and display() method display
value of empid, empname and salary. Now test Employee class.
3. Write a python program to create a class with name ‘Rectangle’. In ‘Rectangle’ class
take two instance variables length and breadth. Now create a parameterized
constructor to initialize variables and create a method area() which return area of
rectangle. Test the class ‘Rectangle’.
4. Write a python program to create a class with name ‘Student’. In Student class take
three instance variables rollno, name and fee. Now create a parameterized
constructor to initialize variables and create a method display() which display values
of instance variables. Test the class ‘Student’.
5. Write a python program to create a class with name ‘Bank’. In Bank class take three
instance variables acno, name and balance. Create a parameterized constructor to
initialize instance variables. Now create following methods in Bank class:-
i. deposit() – This method is used to deposit() money in account.
ii. withdraw() – This method is used to withdraw money from account after
checking balance.
iii. enquiry():- This method provide balance enquiry.
Topic – 6 (Inheritance in Python)
1. Write a python program to create a class named Base. In Base class create a method
showBase(), this method display a message ‘This message from base class’. By
inheriting Base class now create a class named Derived. In Derived class create a
method showDerived(), this method display a message ‘This message from derived
class’. Now test Base and Derived class.
2. Write a python program to create a class named Shape. In Shape class create a
method named setValue(), this method initialize side of shape. By inheriting Shape
class create a new class Square. In Square class create a method area(), this method
return area of square. Now by inheriting Shape class create a new class Cube. In
Cube class create a method volume(), this method return volume of cube. Now Test
Square and Cube classes.
3. Write a python program to create a class Employee. In Employee class take two
instance variables empid and empname. In Employee class create two methods
setEmployee() and getEmployee(). setEmployee() method is used to initialize
instance variables empid and empname whereas getEmployee() method is used to
display values of empid and empname. By inheriting Employee class create a new
class Payroll. In Payroll class create three instance variables bs, hra and da. In Payroll
class create two methods setPayroll() and getPayroll(). setPayroll() method is used to
initialize variables bs, hra and da whereas getPayroll() method is used to display
values of bs, hra and da. Now by inheriting Payroll class create a new class Payslip. In
Payslip class create a method named netSalary() which displays net salary with
addition of basic salary, hra and da. Now test Payslip class. This is an example of
multilevel inheritance.
4. In program (3) create a class Loan before the code of Payslip class. In Loan class
create a method setLoan() which is used to initialize amt variable. Now inherit Loan
class in Payslip class and in netSalary() method also print salary on hand as
(bs+hra+da-amt). This is an example of hybrid inheritance.
5. Write a python program to demonstrate super keyword.