0% found this document useful (0 votes)
7 views4 pages

Python Programming Exercises and Solutions

Python

Uploaded by

mehurvala
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)
7 views4 pages

Python Programming Exercises and Solutions

Python

Uploaded by

mehurvala
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

1. Write a Python program to print the following string in a specific format (see the output).

Sample String : "Twinkle, twinkle, little star, How I wonder what you are! Up above the world so
high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are"
Output :
Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are

2. Write a Python program to find out what version of Python you are using.
3. Write a Python program to display the current date and time.
Sample Output :
Current date and time :
2014-07-05 14:34:14

4. Write a Python program that calculates the area of a circle based on the radius entered by the
user.
Sample Output :
r = 1.1
Area = 3.8013271108436504

5. Write a Python program that accepts the user's first and last name and prints them in reverse order
with a space between them.
6. 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.
Sample data : 3, 5, 7, 23
Output :
List : ['3', ' 5', ' 7', ' 23']
Tuple : ('3', ' 5', ' 7', ' 23')

7. Write a Python program that accepts a filename from the user and prints the extension of the file.
Sample filename : [Link]
Output : java

8. Write a Python program to display the first and last colors from the following list.
color_list = ["Red","Green","White" ,"Black"]
9. Write a Python program to display the examination schedule. (extract the date from
exam_st_date).
exam_st_date = (11, 12, 2014)
Sample Output : The examination will start from : 11 / 12 / 2014

10. Write a Python program that accepts an integer (n) and computes the value of n+nn+nnn.
Sample value of n is 5
Expected Result : 615

11. Write a Python program to print the documents (syntax, description etc.) of Python built-in
function(s).
Sample function : abs()
Expected Result :
abs(number) -> number
Return the absolute value of the argument.

12. Write a Python program that prints the calendar for a given month and year.
Note : Use 'calendar' module.

13. Write a Python program to print the following 'here document'.


Sample string :
a string that you "don't" have to escape
This
is a ....... multi-line
heredoc string --------> example

14. Write a Python program to calculate the number of days between two dates.
Sample dates : (2014, 7, 2), (2014, 7, 11)
Expected output : 9 days

15. Write a Python program to get the volume of a sphere with radius six.

16. 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.
17. Write a Python program to test whether a number is within 100 of 1000 or 2000.
18. Write a Python program to calculate the sum of three given numbers. If the values are equal,
return three times their sum.
19. 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".
20. Write a Python program that returns a string that is n (non-negative integer) copies of a given
string.

21. Write a Python program that determines whether a given number (accepted from the user) is
even or odd, and prints an appropriate message to the user.
22. Write a Python program to count the number 4 in a given list.
23. Write a Python program to get n (non-negative integer) copies of the first 2 characters of a given
string. Return n copies of the whole string if the length is less than 2.
24. Write a Python program to test whether a passed letter is a vowel or not.
25. 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

26. Write a Python program to create a histogram from a given list of integers.

27. Write a Python program that concatenates all elements in a list into a string and returns it.
28. 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
]
29. Write a Python program that prints out all colors from color_list_1 that are not present in
color_list_2.
Test Data :
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])

Expected Output :
{'Black', 'White'}

30. Write a Python program that will accept the base and height of a triangle and compute its area.

You might also like