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

Python Excercises

Uploaded by

Mud Pie Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Excercises

Uploaded by

Mud Pie Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PYTHON EXCERICSES

Use the internet and provided cheat sheets to do the following problems:
Write python program to:

1. Compute the perimeter and area of the circle with radius =10
2. Swap the contents of two variables a, b with given values, namely
12, 39
3. Find the largest values among two given variables a, b with given
values, namely 34, 78
4. Test whether a number is divisible by 5 or 6 or both
e.g. 150 is divisible by 5 and 6, 15 is divisible by 5, 12 is divisible
by 6 and 41 is neither divisible by 5 nor 6.
5. Calculate number of days between two dates
(Hint: >>> from datetime import date then use function
date(yy,mm,dd) to create a datetime value).
6. Calculate the number of living days from your birthday.
(Hint: use date.today() to get current date)
7. Print a table of squares and cubes as shown on the right:
(Hint: print (‘{0:2d {1:3d} ‘.format(x,y) will display x,y like a string with the width of 2
and 3 characters respectively)
8. Test whether a letter is a vowel or not
(hint: make use of tenary operator expression
value_when_true if condition else value_when_false )
9. Print a line of asterisk (*) characters:
10. Print a rectangular of asterisk (*) character of m rows and n columns

11. Print a triangle of asterisk (*) character of height = h


12. Print a triangle of numbers of height = h
13. Print a table of numbers from 0 to 99 as shown on the right

14. Print the table of multiplication


15. Print out the number of digits of an integer value
16. Print out the greatest common divisor (gcd) of two integer namely 45 and 135.
17. Print out the sum all the items in a list.
18. Print out the largest values and its position within a list.
19. Remove duplicates from a list and print out the remaining list.
20. Remove even numbers from a list and print out the remaining list.
Write all the above problems in script and run these scripts. Define functions for problems 2, 3,
8, 16, 17, 18
21. Define function swap(x,y) for problem 2 then call swap to exchange values of two
integers
22. Define function max(x,y) for problem 3 to find the largest values of two integers then use
max to find the largest values among three integers.
23. Define function IsVowel(c) to return true if c is a vowel, return false otherwise.
24. Define function gcd(a,b) to find the greatest common divisor of two integers a,b.
25. Define function sum_of_list() to find the sum of a list of integers.
26. Define function max_in_list() to find the largest values among a list of integers.

You might also like