Basic Python Programming
**********************************************************
Write Python programs to perform the following:
(SIMPLE ARITHMETIC PROGRAMS)
1. Add two numbers.
2. Find out the area and perimeter of a rectangle.
(IF-ELSE & IF-ELSE-IF LADDER)
3. Input a number and find its absolute value.
4. Compute the telephone bill for Mr. X as per the call rates given below:
Rental = 250
1st 100 calls @Rs. 0.2
Next 100 calls @ Rs. 0.3
Remaining calls @ Rs. 0.5
5. Solve a given quadratic equation. [Without imaginary roots].
(LOOP CONTROL STRUCTURE)
6. Print the following patterns up to n no. of lines:
(a) *
**
***
****
*****
(b) 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
(c)
***
*****
*******
*****
***
*
(e) *******
*****
***
*
***
*****
*******
(FUNCTIONS & RECURSION)
7. Implement simple arithmetic calculator using user defined functions for each operation (addition, subtraction, multiplication,
division, modulus, exponent). You may use a dictionary to print the menu.
8. Input a number n and find its factorial using a user defined function long int fact(int)
9. Input a number and check if it a Krishnamurthy number.
10. Find the sum of first n prime numbers using as user defined function to check for prime. Input the value of n from the user.
(LISTS & TUPLES)
11. Create a tuple with different data types, print the tuple and then add one more item into the tuple and print it again.
12. Create a list containing n tuples and replace last value of all tuples in the list.
Sample list: [(10,20,40), (40,50,60), (70,80,90)]
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]
13. Create a list containing n tuples and remove all empty tuple(s) from the list.
Sample data: [(),(), ('',), ('a', 'b'), ('a', 'b', 'c'), ('d')]
Expected output: [('',), ('a', 'b'), ('a', 'b', 'c'), 'd']
(STRINGS)
14. Input two strings and concatenate them.
15. Input a string and reverse it.
16. Enter a sentence and find number of vowels, consonants, spaces and special characters.
17. Input a string and replace each character by the character two place ahead of it, for e.g., a by c, b by d, z by b
18. Input a word and print it vertically.
19. Input a string and check if it a palindrome or not.
20. Input a string and count the number of words in it.
(CLASSES & OBJECTS)
21. Create a class called Rectangle having two attributes – length and breadth and find the area and perimeter of the rectangle
using two methods – showarea() and showperimeter()
22. Create a class called Complex having two attributes – real and imag. Create two instances of the class and perform addition
of two complex numbers. The class should use the following methods:
show() – that displays the complex numbers in proper format
add() – that adds the two complex instances
sub() – that subtracts one complex instance from another
displaymodulus() – that displays the modulus of the resultant complex number after addition or subtraction
23. Design a class to represent a bank account. Include the following members:
Data members - Name of the depositor, Account number, Type of account, Account balance
Methods – To deposit an amount, To withdraw an amount after checking balance, to display the
name and balance.
Incorporate a constructor to provide the initial values.
(NUMPY ARRAYS – 1D & 2D)
24. Input n random numbers between 0 and 1 and find their sum and average.
25. Input an array of size n and search an item from it. The item should be taken as input from the user and if found, the position
at which it has been found in the array should be displayed, else, display “Sorry, item not found.”
26. Input an array of n random numbers within 100 and find the maximum and minimum among them.
27. Input an array of n integer elements and find the second highest among them.
28. Input an array of n numbers and store the even numbers into an array even and odd numbers into an array odd. Also display
the count of even and odd numbers found.
29. Calculate the mean, median, mode, standard deviation & variance of an array of integers.
30. Input two arrays and find the resultant array after performing their –
(a) Intersection; (b) Union
31. Input an array of n elements in sorted order and perform binary search on it.
32. Input an array of n elements and sort them using –
(a) bubble sort; (b) selection sort; (c) insertion sort
PANDAS & DATA VISUALIZATION
33. Import the titanic dataset into a pandas dataframe and perform the following:
i. List the column names.
ii. List the first five rows for both columns ‘survived’ and ‘age’.
iii. Find the percentage of people who survived and who did not survive.
iv. Find the percentage of women who survived.
v. Find how many children below 5 years of age were on board the ship.
vi. Find the number of children less than 5 years of age who survived.
vii. How many embark points were there?
viii. Find the average age of passengers for each class.
ix. Draw a histogram to represent the number of survivors for each age group (0-10, 11-30, 31-60, >60).
Example:
import pylab as P
df['Age'].hist()
[Link]()
x. Draw a histogram to represent the number of survivors for each sex.
xi. Find the correlation (if any) between passenger class and age.
xii. Draw a scatter plot between passenger age and fare and state your observations (if any).