0% found this document useful (0 votes)
102 views8 pages

Python QB

This document contains practice questions on basic Python programming concepts like strings, conditional statements, loops, functions, modules and lambda functions. It includes questions to test skills with operators, data types, lists, tuples, dictionaries and more.

Uploaded by

Sameer Najam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
102 views8 pages

Python QB

This document contains practice questions on basic Python programming concepts like strings, conditional statements, loops, functions, modules and lambda functions. It includes questions to test skills with operators, data types, lists, tuples, dictionaries and more.

Uploaded by

Sameer Najam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Python Exercise with Practice Questions

Basic Python program


Q1) Write Python programs to print strings in the given manner:
a) Hello Everyone !!!
b) Hello
World
c) Hello
World
d) ‘ Rohit’ s date of birth is 12\05\1999’

Q2) Write a Python program to calculate the difference between a given


number and 17. If the number is greater than 17, return twice the
absolute difference.
Q3) Declare a string variable called x and assign it the value “Hello”.
Print out the value of x
Q4) Take different data types and print values using print function.
Q5) Take two variable a and b. Assign your first name and last name. Print your Name after
adding your First name and Last name together.
Q6) Declare three variables, consisting of your first name, your last name and Nickname.
Write
a program that prints out your first name, then your nickname in parenthesis and then your
last name.
Example output : George ( woody ) Washington.
Q7) Declare and assign values to suitable variables and print in the following way :
NAME : NIKUNJ BANSAL
ROLL NO : 500069944
DATE OF BIRTH : 13 Oct 1999
ADDRESS : Galgotias
University Greater
Noida Pincode :
123456
Programme : AI & ML
Semester : 2
Use of input statements, operators
1. Declare these variables (x, y and z) as integers. Assign a value of 9 to x, Assign a value
of 7 to y, perform addition, multiplication, division and subtraction on these two
variables and Print out the result.
2. Write a Program where the radius is taken as input to compute the area of a circle.
3. Write a Python program to solve
(x+y)*(x+y) Test data : x = 4 , y = 3
Expected output: 49

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

1. Check whether given number is divisible by 3 and 5 both.


2. Check whether a given number is multiple of five or not.
3. Find the greatest among two numbers. If numbers are equal than print “numbers
are equal”.
4. Find the greatest among three numbers assuming no two values are same.
5. Check whether the quadratic equation has real roots or imaginary roots. Display the roots.
6. Find whether a given year is a leap year or not.
7. Write a program which takes any date as input and display next date of the
calendar e.g.
I/P: day=20 month=9 year=2005
O/P: day=21 month=9 year 2005
8. Print the grade sheet of a student for the given range of cgpa. Scan marks of five
subjects and calculate the percentage.
CGPA=percentage/10
CGPA range:
0 to 3.4 -> F
3.5 to 5.0->C+
5.1 to 6->B
6.1 to 7-> B+
7.1 to 8-> A
8.1 to 9->A+
9.1 to 10-> O (Outstanding)

Sample Gradesheet
Name: Rohit Sharma
Roll Number: R17234512 ROLL NO: 50005673
Sem: 1 Course: B.Tech. CSE AI&ML

Subject name: Marks


PDS: 70
Python: 80
Chemistry: 90
English: 60
Physics: 50
Percentage: 70%
CGPA:7.0
Grade: A

Loops

Q1) Find a factorial of given number.


Q2) Find whether the given number is Armstrong number.
Q3) Print Fibonacci series up to given term.
Q4) Write a program to find if given number is prime number or not.
Q5) Check whether given number is palindrome or not.
Q6) Write a program to print sum of digits.
Q7) Count and print all numbers divisible by 5 or 7 between 1 to 100.
Q8) Convert all lower cases to upper case in a string.
Q9) Print all prime numbers between 1 and 100.
Q10) Print the table for a given number:
5*1=5
5 * 2 = 10………..

String and Sets

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}

Lists, tuples, dictionary

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]

Python Regular Expression


1. Write a Python program to check that a string contains only a certain set
of characters (in this case a-z, A-Z and 0-9).
2. Write a Python program to match a string that contains only upper and
lowercase letters, numbers, and underscores.
3. Write a Python program to search for literal strings within a string.
Sample text : 'The quick brown fox jumps over the lazy dog.'
Searched words : 'fox', 'dog', 'horse'
4. Write a Python program to search for a literal string in a string and also
find the location within the original string where the pattern occurs.
Sample text : 'The quick brown fox jumps over the lazy dog.'
Searched words : 'fox'
5. Write a Python program to convert a date of yyyy-mm-dd format to dd-
mm-yyyy format.
6. Write a Python program to do case-insensitive string replacement.
7. Write a Python program to find all adverbs and their positions in a given
sentence.Sample text : "Clearly, he has no excuse for such behavior."
8. Write a Python program to remove everything except alphanumeric
characters from a string.
9. Write a Python program to remove all whitespaces from a string.
10. Write a Python program to remove multiple spaces from a string.

Python Generators and Yield


1. Write a Python program that creates a generator function that yields
cubes of numbers from 1 to n. Accept n from the user.
2. Write a Python program to implement a generator function that
generates the running average of a sequence of numbers.
3. Write a Python program to create a generator that generates the
square, cube roots of numbers from 1 to n.
4. Write a Python program that creates a generator function that generates
all prime numbers between two given numbers.
5. Write a Python program to implement a generator that yields all
possible combinations of a given list of elements.
Python Itertools
1. Write a Python program to create an iterator from several iterables in a
sequence and display the type and elements of the new iterator.
2. Write a Python program to make an iterator that drops elements from
the iterable as long as the elements are negative; afterwards, it returns
every element.
3. Write a Python program that will select a specified number of colours
from three different colours, and then generate all the combinations with
repetitions.
4. Write a Python program that iterates the integers from 1 to a given
number and prints "Fizz" for multiples of three, prints "Buzz" for
multiples of five, and prints "FizzBuzz" for multiples of both three and
five using the itertools module.
5. Write a Python program to find the shortest distance from a specified
character in a given string. Return the shortest distances through a list
and use an itertools component to solve the problem.

You might also like