Python Programming Practice Exercises
Basic Programs
- Write a program to print a simple welcome message as “Welcome to Python Programming”
- Write a program to store any integer value in a variable and display its data type and value on the screen
- Write a program to store float value in a variable, display its data type and value and then change the value of variable to any string and again display its data type and value.
- Write a program to accept a number from the user and display what he has entered.
- Write a program to accept the name of the user and display welcome message to him.
- Write a program to accept any two numbers from the user and display their sum, difference, product
- Write a program to accept any numerator and denominator for division operation from the user and display the quotient and remainder after division.
- Write a program to accept length and breadth of a rectangle and display the area and perimeter of the rectangle
- Write a program to accept radius of the circle and display area of the circle
- Write a program to accept principle, rate of interest and tenure and calculate simple interest
- Write a program to accept three numbers from the user and display their average.
- Write a program to exchange the existing values of two variables with/without using third variable
Control Statements
Selection Statements Programs
- Write a program to accept any two numbers from the user and display which is big
- Write a program to accept any number from the user and display whether it is positive or negative
- Write a program to accept any number from the user and display whether it is even or add
- Write a program to accept 1,2,3 or 4 only from the user and display their textual form on the screen. For example: for user input 1 the program should display ONE
- Write a program to accept any three numbers from the user and display which is big
- Write a program to accept marks of a student and print the grade based on the given criteria:
0-35 – Fail
36 – 50 – C
51 – 70 – B
71 – 90 – A
91 – 100 – A+
for any other inputted marks, your program should print Invalid Marks - Write a program to accept any two numbers and display a menu for all arithmetic operations and perform arithmetic operation based on the choice of user.
Loop Statements Programs
- Write a program to print first 10 natural numbers
- Write a program to print first ‘n’ natural numbers where n is supplied by the user
- Write a program to print even natural numbers upto user given number
- Write a program to print sum of all entered input numbers by the user
- Write a program to display any math table upto required steps asked by the user.
Jump Statements Programs
- Write a program to print first 50 natural numbers except 5 multiples
- Write a program to check whether the given number is prime number or not
Functions
- Write a program to define a function to display message as “hi” when it is called.
- Write function definitions to calculate average of three given numbers using four types of function definitions.
- Define a function to accept multiple arguments
- Define a function using recursion to calculate factorial of given number.
Classes and Objects
Fill Code in the Blanks
#Practice 1
class __________:
def __init__(_____________):
self.name = name
self.age = age
p1 = Person(“Ram”, 42)
print(__________) #prints name in p1
print(__________) #prints age in p1
#Practice 2
class Person:
def __init__(self, fname, lname):
___________= fname
___________ = lname
def ______________:
print(self.firstname, self.lastname)
x = _____________________
x.printname()
- Write a program to define a class called student and store his personal details like and name, age, height and weight. Create two objects out of the class and initialise their values by yourself as developer.
Create a method inside ‘Student’ class created in exercise no.1 and define functions to accept user input and display the data. Create an object and get user input for the data members of the object.
- Create a class named ‘Rectangle’ with a method named ‘Area’ which returns the area and length and breadth passed as parameters to its constructor.
- Create class ‘Employee’ and define its constructor with three variables name, year of joining, salary and address. Also define methods to accept user input and display output