Python QB
Python QB
4. Write a program to compute the length of the hypotenuse (c) of a right triangle using
Pythagoras theorem.
5. Write a program to find simple interest.
6. Write a program to find area of triangle when length of sides are given.
7. Write a program to convert given seconds into hours, minutes and remaining seconds.
8. Write a program to swap two numbers without taking additional variable.
9. Write a program to find sum of first n natural numbers.
10. Write a program to print truth table for bitwise operators( & , | and ^ operators)
11. Write a program to find left shift and right shift values of a given number.
12. Using membership operator find whether a given number is in sequence
(10,20,56,78,89)
13. Using membership operator find whether a given character is in a string.
Conditional Statements
Sample Gradesheet
Name: Rohit Sharma
Roll Number: R17234512 ROLL NO: 50005673
Sem: 1 Course: B.Tech. CSE AI&ML
Loops
1. Write a program to count and display the number of capital letters in a given string.
2. Count total number of vowels in a given string.
3. Input a sentence and print words in separate lines.
4. WAP to enter a string and a substring. You have to print the number of times that the
substring occurs in the given string. String traversal will take place from left to right,
not from right to left.
Sample Input
ABCDCD
C CDC
Sample
Output 2
5. Given a string containing both upper and lower case alphabets. Write a Python program
to count the number of occurrences of each alphabet (case insensitive) and display the
same. Sample Input
ABaBCbG
c Sample
Output 2A
3B
2C
1G
6. Program to count number of unique words in a given sentence using sets.
7. Create 2 sets s1 and s2 of n fruits each by taking input from user and find:
a) Fruits which are in both sets s1 and s2
b) Fruits only in s1 but not in s2
c) Count of all fruits from s1 and s2
8. Take two sets and apply various set operations on
them : S1 = {Red ,yellow, orange , blue }
S2 = {violet, blue , purple}
1. Scan n values in range 0-3 and print the number of times each value has occurred.
2. Create a tuple to store n numeric values and find average of all values.
3. WAP to input a list of scores for N students in a list data type. Find the score of the
runner-up and print the output.
Sample
Input N =
5
Scores= 2 3 6
6 5 Sample
output
5
Note: Given list is [2, 3, 6, 6, 5]. The maximum score is 6, second maximum is 5. Hence,
we print 5 as the runner-up score.
4. Create a dictionary of n persons where key is name and value is city.
a) Display all names
b) Display all city names
c) Display student name and city of all students.
d) Count number of students in each city.
5. Store details of n movies in a dictionary by taking input from the user. Each movie must
store details like name, year, director name, production cost, collection made (earning) &
perform the following :-
a) print all movie details
b) display name of movies released before 2015
c) print movies that made a profit.
d) print movies directed by a particular director.
Functions
1. Write a Python function to find the maximum and minimum numbers from a
sequence of numbers. (Note: Do not use built-in functions.)
2. Write a Python function that takes a positive integer and returns the sum of the cube
of all the positive integers smaller than the specified number.
3. Write a Python function to print 1 to n using recursion. (Note: Do not use loop)
4. Write a lambda function which gives tuple of max and min from a
list. Sample input: [10, 6, 8, 90, 12, 56]
Sample output: (90,6)
5. Write functions to explain mentioned concepts:
a. Keyword argument
b. Default argument
c. Variable length argument
Module - random
1. Write a Python program to generate a random color hex, a random
alphabetical string, random value between two integers (inclusive) and
a random multiple of 7 between 0 and 70. Use random.randint()
2. Write a Python program to generate a random integer between 0 and 6
- excluding 6, random integer between 5 and 10 - excluding 10, random
integer between 0 and 10, with a step of 3 and random date between
two dates. Use random.randrange()
3. Write a Python program to create a list of random integers and
randomly select multiple items from the said list. Use random.sample()
Module – types
1. Write a Python program to check if a function is a user-defined function
or not. Use types.FunctionType, types.LambdaType()
2. Write a Python program to check if a given value is a method of a user-
defined class. Use types.MethodType()
3. Write a Python program to check if a given function is a generator or
not. Use types.GeneratorType()
Module – decimal
1. Write a Python program to construct a Decimal from a float and a
Decimal from a string. Also represent the decimal value as a tuple. Use
decimal.Decimal
2. Write a Python program to round a decimal value to the nearest multiple
of 0.10, unless already an exact multiple of 0.05. Use decimal.Decimal
3. Write a Python program that can be configured to round to the nearest -
with ties going towards 0 and ties going away from 0. Use
decimal.ROUND_HALF_DOWN, decimal.ROUND_HALF_UP
Python Lambda
1. Write a Python program to create a lambda function that adds 15 to a
given number passed in as an argument, also create a lambda function
that multiplies argument x with argument y and prints the result.
Sample Output:
25
48
2. Write a Python program to create a function that takes one argument,
and that argument will be multiplied with an unknown given number.
Sample Output:
Double the number of 15 = 30
Triple the number of 15 = 45
Quadruple the number of 15 = 60
Quintuple the number 15 = 75
3. Write a Python program to sort a list of tuples using Lambda.
Original list of tuples:
[('English', 88), ('Science', 90), ('Maths', 97), ('Social sciences', 82)]
Sorting the List of Tuples:
[('Social sciences', 82), ('English', 88), ('Science', 90), ('Maths', 97)]
4. Write a Python program to sort a list of dictionaries using Lambda.
Original list of dictionaries :
[{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model':
'2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]
Sorting the List of dictionaries :
[{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Samsung',
'model': 7, 'color': 'Blue'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}]
5. Write a Python program to create Fibonacci series up to n using
Lambda.
Fibonacci series upto 2:
[0, 1]
Fibonacci series upto 5:
[0, 1, 1, 2, 3]
Fibonacci series upto 6:
[0, 1, 1, 2, 3, 5]
Fibonacci series upto 9:
[0, 1, 1, 2, 3, 5, 8, 13, 21]