0% found this document useful (0 votes)
2 views

Python Exercises

The document contains a comprehensive list of Python exercises covering various topics such as basic programming, functions, loops, data structures, and dictionaries. Each exercise presents a specific problem for the user to solve, aimed at enhancing their programming skills. The exercises range from calculating areas and handling strings to working with lists, sets, and dictionaries.

Uploaded by

globallandmark11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Exercises

The document contains a comprehensive list of Python exercises covering various topics such as basic programming, functions, loops, data structures, and dictionaries. Each exercise presents a specific problem for the user to solve, aimed at enhancing their programming skills. The exercises range from calculating areas and handling strings to working with lists, sets, and dictionaries.

Uploaded by

globallandmark11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON EXERCISES

1. Write a Python program that calculates the area of a circle based on the radius entered
by the user.
2. Write a Python program that accepts the user's first and last name and prints them in
reverse order with a space between them.
3. Write a Python program that accepts a sequence of comma-separated numbers from the
user and generates a list and a tuple of those numbers.
4. Write a Python program to display the first and last colors from the following list.
color_list = ["Red","Green","White" ,"Black"]
5. Write a Python program that accepts an integer (n) and computes the value of
n+nn+nnn.
6. Write a Python program to print absolute value on number provided by the user
7. Write a Python program that prints the calendar for a given month and year.
Note : Use 'calendar' module.
8. 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
9. Write a Python program to test whether a number is within 100 of 1000 or 2000
10. Write a Python program to calculate the number of days between two dates.Sample
dates : (2024, 7, 2), (2024, 7, 15)
11. Write a Python program to calculate the sum of three given numbers. If the values are
equal, return three times their sum
12. Write a Python program to get a newly-generated string from a given string where "Is"
has been added to the front. Return the string unchanged if the given string already begins
with "Is".
13. Write a Python program to count the number 4 in a given list.
14. Write a Python program to test whether a passed letter is a vowel or not.
15. Write a Python program that checks whether a specified value is contained within a
group of values.
Test Data :
3 -> [1, 5, 8, 3] : True
-1 -> [1, 5, 8, 3] : False
16. Write a Python program to print all even numbers from a given list of numbers in the
same order and stop printing any after 237 in the sequence.

Sample numbers list :

numbers = [386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958,743, 527]
17. Write a Python program that will accept the base and height of a triangle and compute
its area
18. Write a Python program that computes the greatest common divisor (GCD) of two
positive integers
19. Write a Python program to find the least common multiple (LCM) of two positive
integers
20. Write a Python program to sum three given integers. However, if two values are equal,
the sum will be zero.
21. Write a Python program to sum two given integers. However, if the sum is between 15
and 20 it will return 20.
22. Write a Python program that returns true if the two given integer values are equal or
their sum or difference is 5.
23. Write a Python program to add two objects if both objects are integers
24. Write a Python program that displays your name, age, and address on three different
lines
25. Write a Python program to solve (x + y) * (x + y).
Test Data : x = 4, y = 3

Expected Output : (4 + 3) ^ 2) = 49
26. Write a Python program to compute the future value of a specified principal
amount, rate of interest, and number of years.

Test Data : amt = 10000, int = 3.5, years = 7

Expected Output : 12722.79


27. Write a Python program to calculate the distance between the points (x1, y1) and (x2,
y2).
28. Write a Python program to parse a string to float or integer.
29. Write a Python program to print without a newline or space
30. Write a Python program to sum the first n positive integers
31. Write a Python program to calculate the hypotenuse of a right angled triangle.
32. Write a Python program to convert the distance (in feet) to inches, yards, and miles.
33. Write a Python program to calculate sum of digits of a number.
34. Write a Python program to sort three integers without using conditional statements and
loops

PYTHON FUNCTIONS EXERCISE


35. Write a program to create function func1() to accept a variable length of arguments and
print their value.
36. Write a program to create function calculation() such that it can accept two variables
and calculate addition and subtraction. Also, it must return both addition and
subtraction in a single return call.
37. Write a program to create a function show_employee() using the following conditions.
38. It should accept the employee’s name and salary and display both.
39. If the salary is missing in the function call then assign default value 9000 to salary
40. Create an inner function to calculate the addition in the following way
41. Create an outer function that will accept two parameters, a and b
42. Create an inner function inside an outer function that will calculate the addition
of a and b
43. At last, an outer function will add 5 into addition and return it
44. Write a program to create a recursive function to calculate the sum of numbers from
0 to 10. A recursive function is a function that calls itself again and again.
45. Generate a Python list of all the even numbers between 4 to 30
46. Write python function to find the largest item from a given list

PYTHON IF ELSE, FOR LOOP, AND RANGE() EXERCISES


47. Print First 10 natural numbers using while loop
48. Write a program to print the following number pattern using a loop.

49.
50. Write a program to accept a number from a user and calculate the sum of all numbers
from 1 to a given number. For example, if the user entered 10 the output should
be 55 (1+2+3+4+5+6+7+8+9+10)
51. Write a program to print multiplication table of a given number
52. Write a program to display only those numbers from a list that satisfy the following
conditions

 The number must be divisible by five


 If the number is greater than 150, then skip it and move to
the next number
 If the number is greater than 500, then stop the loop
53. Write a program to count the total number of digits in a number using a while loop.
For example, the number is 75869, so the output should be 5.
54. Print list in reverse order using a loop
55. Display numbers from -10 to -1 using for loop
56. Use else block to display a message “Done” after successful execution of for loop.
57. Write a program to display all prime numbers within a range

Note: A Prime Number is a number that cannot be made by


multiplying other whole numbers. A prime number is a natural
number greater than 1 that is not a product of two smaller natural
numbers

Examples:

6 is not a prime mumber because it can be made by 2×3 = 6

37 is a prime number because no other whole numbers multiply


together to make it.

58. Write a program to use the loop to find the factorial of a given number.
59. Write python program to reverse a given integer number
60. Use a loop to display elements from a given list present at odd index positions
61. Calculate the cube of all numbers from 1 to a given number
62. Write a program to calculate the sum of series up to n term. For example, if n =5 the
series will become 2 + 22 + 222 + 2222 + 22222 = 24690

PYTHON DATA STRUCTURE EXERCISE

63. Write a program to remove the item present at index 4 and add it to the 2nd position
and at the end of the list.
64. Slice list into 3 equal chunks and reverse each chunk

65. Write a program to iterate a given list and count the occurrence of each element and
create a dictionary to show the count of each element.
66. Create a Python set such that it shows the element from both lists in a pair
67. Find the intersection (common) of two sets and remove those elements from the first
set

68. Checks if one set is a subset or superset of another set. If found, delete all elements
from that set

69.
70. Iterate a given list and check if a given element exists as a key’s value in a dictionary.
If not, delete it from the list

71. Get all values from the dictionary and add them to a list but don’t add duplicates
speed = {'jan': 47, 'feb': 52, 'march': 47, 'April': 44, 'May': 52, 'June': 53, 'july': 54, 'Aug':
44, 'Sept': 54}
72. Remove duplicates from a list and create a tuple and find the minimum and maximum
number

PYTHON DICTIONARY EXERCISE

73. Write a Python program to convert them into a dictionary in a way that item from list1
is the key and item from list2 is the value

74. Write python program to Merge two Python dictionaries into one
75. Write a Python program to create a new dictionary by extracting the mentioned keys
from the below dictionary.

76. Write a python program to delete a list of keys from a dictionary

77. Write a Python program to check if value 200 exists in the following dictionary.

78. Write a program to rename a key city to a location in the following dictionary.
79. Write a python program to get the key of a minimum value from the following
dictionary
80. Write a Python program to change Brad’s salary to 8500 in the following dictionary.

You might also like