NATIONAL ENGINEERING COLLEGE, K.R.
NAGAR
(An Autonomous Institution – Affiliated to Anna University, Chennai)
Department of Artificial Intelligence and Data Science
B.E-Mech-Sixth Semester (2019R)
19CS04N-Programming in Python Laboratory
1. Write a Python program that takes an integer input from the user and computes the
prime factorization of the integer. The program should return a list of prime factors,
where each factor appears as many times as it divides the integer.
2. Write a program that asks the user to enter two strings of the same length. The
program should then check to see if the strings are of the same length. If they are
not, the program should print an appropriate message and exit. If they are of the
same length, the program should alternate the characters of the two strings.
3. Write a program that asks the user for a weight in kilograms and converts it to
pounds. There are 2.2 pounds in a kilogram.
4. Write a Python program using the fundamental operations of plotting and
visualizing data using the Matplotlib library. The program should demonstrate how
to create different types of plots, customize them, and apply various visualization
techniques to effectively represent data.
5. Write a simple Python program that opens a Tkinter window with a title My First
Window and a specified size of 400x300 pixels. Create a Tkinter window with a title
and a size, and then display the window.
6. Write a Python program that adds a button to a Tkinter window. When the button
is clicked, a message should be printed in the console. Create a Tkinter window with
a button that, when clicked, prints Button Clicked to the console.
7. Create a basic calculator using Tkinter with buttons for digits (0-9) and basic
operations (+, -, *, /). Display the result in an entry widget. Design a calculator
interface with buttons for digits and operations, and calculate the result when the
button is clicked.
8. Write a program that opens a Tkinter window with a button. When the button is
clicked, a new window (popup) opens with a label saying Hello, World! Create a
button that, when clicked, opens a new top- level window with a label.
9. Write a program with a Tkinter window that acts as a timer. Start the timer by
pressing a button, and stop it by pressing another button. Display the elapsed time
on a label. Use a button to start the timer and another button to stop it, while
displaying the time elapsed in a label.
10. Use a for loop to print a triangle like the one below. Allow the user to specify how
high the triangle should be.
**
***
****
11. Write a Python program to get a string made of the first 2 and last 2 characters of a
given string. If the string length is less than 2, return the empty string instead.
12. Write a Python program that uses recursion to determine whether a given string is a
palindrome or symmetrical.
13. Write a Python program to create a lap timer that records the time for each lap,
displays the total time, and allows the timer to be started, stopped, and reset.
14. Python program to check whether the given integer is a multiple of both 5 and 7.
15. Python program to find the roots of a quadratic equation.
16. Write a Python program to find how many times each day of the week occurs in a given
year.
17. Write a Python program to count the number of characters, words, spaces, and lines in a
text file.
18. Write a Python program to count the number of occurrences of a specific key-value pair
in a text file.
19. Write a Python program to find the line numbers in a text file where a given word
appears.
20. Write a program that asks the user for their name and how many times to print it. The
program should print out the user's name the specified number of times.
21. Write a program that asks the user to enter two strings of the same length. The
program should then check to see if the strings are of the same length. If they are
not, the program should print an appropriate message and exit. If they are of the
same length, the program should alternate the character of the two strings.
22. Write a program that asks the user to enter a word and prints out whether that word
contains any vowels.
23. Generate a random number between 1 and 10. Ask the user to guess the number
and print a message based on whether they get it right or not.
24. To write a Python program to accept a positive integer N and print its square
root with precision up to 5 decimal places.
25. To write a Python program that calculates the area of a circle using a function
defined in a separate module. Import this function into your main program and use
it to calculate the area of a circle with radius 5 units.
26. To create a Python module that contains functions for reversing a string, counting
the
occurrences of a specific character, and checking if a string is a palindrome.
Demonstrate the usage of each function in your main program.
27. The program must accept N integers as the input. The value of N is always a multiple of
3. The program must divide the given N integers into 3 equal parts. Write a program to
print the integers in the first part then the program must print the integers last print
finally, the program must print the integers in the middle part.
28. To develop a program that accepts an integer matrix of size R*C as input and
replaces the last integer with 10. The program must then print the modified matrix
following specific formatting conditions. Each row is separated by a comma and
enclosed in parentheses.
29. Given a square matrix with n rows and n columns. Write a program to rotate this
matrix such that each element is shifted by one place in a clockwise manner.
30. Write a Python code to find the first non-repeating character in a string.
31. To write a Python program that takes two strings as input and determines if they
are anagrams of each other using dictionaries to count character frequencies.
32. To write a Python program to read the current line from a given CSV file. Use
[Link].
33. Write a python program using fundamental operations of array creation,
Manipulation using NumPy packages in Python.
34. Write a python program using fundamental operations of indexing, slicing using
NumPy packages in Python.
35. Write a python program using fundamental operations of mathematical
computations, and data handling using NumPy packages in Python.
36. Write a python program using SciPy library for optimization and integration operations.
37. Write a python program using SciPy library for linear algebra operations in Python.
38. Write a function called primes that is given a number n and returns a list
of the first n primes. Let the default value of n be 100.
39. Write a program that reads a list of temperatures from a file called [Link],
converts those temperatures to Fahrenheit, and writes the results to a file called
[Link].
40. Write a program that opens a file dialog that allows you to select a text file. The program
then displays the contents of the file in a textbox.
41. write a python program to read dictionary data and delete any given key entry in
the dictionary.
42. A website requires the users to input username and password to register. Construct
a program to check the validity of password input by users. Following are the
criteria for checking the password:
1. At least 1 letter between [a-z], 1 number between [0-9], 1 letter between [A-Z],
and 1 character from [$#@].
Minimum and Maximum length of transaction password: 6 and 12
Your program should accept a sequence of comma separated passwords and will
check them according to the above criteria. Passwords that match the criteria are to
be printed, each separated by a comma.
43. Construct a program which accepts a sequence of words separated by whitespace
as file input. Print the words composed of digits only.
44. Change all the numbers in the file to text. Construct a program for the same.
Example: Given 2 integer numbers, return their product only if the product is equal
to or lower than 10. And the result should be: Given two integer numbers, return
their product only if the product is equal to or lower than one zero.
45. Construct a program to read [Link] dataset, remove last column and save it in
an array. Save the last column to another array. Plot the first two columns.
46. Print the pattern using the for loop :
1
1
2
1 2 3
1 2 3 4
1 2 3 4 5
47. Write a script named [Link]. This script should prompt the user for the names
of two text files. The contents of the first file should be input and written to the
second file.
48. Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order.
49. Using a numpy module create array and check the following:
1. Reshape 3X4 array to 2X2X3 array
2. Sequence of integers from 0 to 30 with steps of 5
3. Flatten array
4. Constant value array of complex type
50. Write a python code to set background color and pic and draw a circle using tkinter
module.
51. Write a python code to set background color and pic and draw a square and fill the color
using tkinter module.
52. Create a package mathpack with sub-packages algebra and geometry. In algebra, create
[Link] with a function solve_linear(a, b) In geometry, create [Link] with a function
area(radius) Write a main script that uses both.
53. Create a simple text editor using Tkinter. Include a menu bar with options: New, Open,
Save, and Exit. Implement file dialogs to open and save files. Allow the user to edit text
in a Text widget.
54. Create a canvas where a ball moves and bounces off the window edges. Use the after()
method for smooth animation. Allow the user to start and stop the animation with buttons.
55. Write a Python program that simulates two moving shapes on a 2D plane and
checks for collisions between them. You can use a graphical library Tkinter to create
and animate the shapes.
56. Create a tuple and perform the following methods
a. add items
b. len
c. Check for item in tuple
d. Access items
57. Write a python program to filter only even numbers from a given list without using
filter function.
58. Write a function nearly_ equal to test whether two strings are nearly equal. Two
strings a and b are nearly equal when a can be generated by a single mutation on b.
a. Write a function dups to find all duplicates in the list.
b. Write a function unique to find all the unique elements of a list.
59. Write a program that asks the user to enter a length in feet. The program should then give
the user the option to convert from feet into inches, yards, miles, millimeters, centimeters,
meters, or kilometers. Say if the user enters a 1, then the program converts to inches; if
they enter a 2, then the program converts to yards, etc.
60. Design a Python script using the Tkinter graphics library to construct a bar chart
representing the grades obtained by N students read from a file categorising them into
distinction, first class, second class, third class and failed.
61. Write a function called isphonenumber() to recognize a pattern 415-555-4242 without
using regular expression and also write the code to recognize the same pattern using
regular expression.
62. Write a python program to create a ZIP file of a particular folder which contains
several files inside it.
63. Write a program to draw the Line Plot and Bar chart for the following data.
Elapsed time (s) 0 1 2 3 4 5 6
Speed(m/s) 0 3 7 12 20 30 45.6
64. Create a custom exception InvalidAgeError. Write a program that takes the user's
age and raises this exception if the age is less than 18.
65. Write a program that takes two numbers from the user and divides them. Handle the
ZeroDivisionError and ValueError.
66. Write a Python program that calculates income tax based on the following rules:
a. No tax if income is ₹2,50,000 or below.
b. 5% tax on the amount above ₹2,50,000 up to ₹5,00,000.
c. 20% tax on the amount above ₹5,00,000 up to ₹10,00,000.
d. 30% tax on the amount above ₹10,00,000.
e. Display the total tax payable.
67. Write a Python program to accept the lengths of three sides of a triangle. First, check
whether the sides can form a triangle. If yes, determine if it is:
a. Equilateral (all sides equal)
b. Isosceles (any two sides equal)
c. Scalene (no sides equal)
68. Write a program where a global variable score = 0 is modified inside a function
update_score (points) using the global keyword. Call the function multiple times and print
the final score.
69. Create a global variable rate = 5. Define a function calculate_interest(principal) that:
a. Uses the global rate without modifying it
b. Calculates and returns interest = (principal * rate) / 100
c. Does not use the global keyword
70. Write a recursive function decimal_to_binary (n) to convert a number to binary format.