44 Exercises on Python Conditional and Loop Statements
44 Exercises on Python Conditional and Loop Statements
Statements
1. Write a Python program to find those numbers which are divisible by 7 and
multiples of 5, between 1500 and 2700 (both included).
*
**
***
****
*****
****
***
**
*
5. Write a Python program that accepts a word from the user and reverses it.
6. Write a Python program to count the number of even and odd numbers in a
series of numbers
Sample numbers : numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
Expected Output :
Number of even numbers : 5
Number of odd numbers : 4
7. Write a Python program that prints each item and its corresponding type
from the following list.
Sample List : datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12],
{"class":'V', "section":'A'}]
8. Write a Python program that prints all the numbers from 0 to 6 except 3 and
6.
Note : Use 'continue' statement.
Expected Output : 0 1 2 4 5
9. Write a Python program to get the Fibonacci series between 0 and 50.
Note : The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Every next number is found by adding up the two numbers before it.
Expected Output : 1 1 2 3 5 8 13 21 34
10. Write a Python program that iterates the integers from 1 to 50. For
multiples of three print "Fizz" instead of the number and for multiples of five
print "Buzz". For numbers that are multiples of three and five, print "FizzBuzz".
Sample Output :
fizzbuzz
1
2
fizz
4
buzz
11. Write a Python program that takes two digits m (row) and n (column) as
input and generates a two-dimensional array. The element value in the i-th row
and j-th column of the array should be i*j.
Note :
i = 0,1.., m-1
j = 0,1, n-1.
12. Write a Python program that accepts a sequence of lines (blank line to
terminate) as input and prints the lines as output (all characters in lower case).
14. Write a Python program that accepts a string and calculates the number of
digits and letters.
Sample Data : Python 3.2
Expected Output :
Letters 6
Digits 2
15. Write a Python program to check the validity of passwords input by users.
Validation :
16. Write a Python program to find numbers between 100 and 400 (both
included) where each digit of a number is an even number. The numbers
obtained should be printed in a comma-separated sequence.
***
* *
* *
*****
* *
* *
* *
18. Write a Python program to print the alphabet pattern 'D'.
Expected Output:
****
* *
* *
* *
* *
* *
****
19. Write a Python program to print the alphabet pattern 'E'.
Expected Output:
*****
*
*
****
*
*
*****
20. Write a Python program to print the alphabet pattern 'G'.
Expected Output:
***
* *
*
* ***
* *
* *
***
21. Write a Python program to print the alphabet pattern 'L'.
Expected Output:
*
*
*
*
*
*
*****
22. Write a Python program to print the alphabet pattern 'M'.
Expected Output:
* *
* *
** **
* * *
* *
* *
* *
23. Write a Python program to print the alphabet pattern 'O'.
Expected Output:
***
* *
* *
* *
* *
* *
***
24. Write a Python program to print the alphabet pattern 'P'.
Expected Output:
****
* *
* *
****
*
*
*
25. Write a Python program to print the alphabet pattern 'R'.
Expected Output:
****
* *
* *
****
**
* *
* *
26. Write a Python program to print the following patterns.
Expected Output:
****
*
*
***
*
*
****
ooooooooooooooooo
ooooooooooooooooo
ooooooooooooooooo
oooo
oooo
oooo
ooooooooooooooooo
ooooooooooooooooo
ooooooooooooooooo
oooo
oooo
oooo
ooooooooooooooooo
ooooooooooooooooo
ooooooooooooooooo
27. Write a Python program to print the alphabet pattern 'T'.
Expected Output:
*****
*
*
*
*
*
*
28. Write a Python program to print the alphabet pattern 'U'.
Expected Output:
* *
* *
* *
* *
* *
* *
***
29. Write a Python program to print the alphabet pattern 'X'.
Expected Output:
* *
* *
**
*
**
* *
* *
30. Write a Python program to print the alphabet pattern 'Z'.
Expected Output:
*******
*
*
*
*
*
*******
31. Write a Python program to calculate a dog's age in dog years.
Note: For the first two years, a dog year is equal to 10.5 human years. After
that, each dog year equals 4 human years.
Expected Output:
List of months: January, February, March, April, May, June, July, August
, September, October, November, December
Input the name of Month: February
No. of days: 28/29 days
34. Write a Python program to sum two integers. However, if the sum is
between 15 and 20 it will return 20.
35. Write a Python program that checks whether a string represents an integer
or not.
Expected Output:
37. Write a Python program that reads two integers representing a month and
day and prints the season for that month and day.
Expected Output:
38. Write a Python program to display the astrological sign for a given date of
birth.
Expected Output:
Input birthday: 15
Input month of birth (e.g. march, july etc): may
Your Astrological sign is : Taurus
39. Write a Python program to display the sign of the Chinese Zodiac for the
given year in which you were born.
Expected Output:
41. Write a Python program to get the next day of a given date.
Expected Output:
42. Write a Python program to calculate the sum and average of n integer
numbers (input from the user). Input 0 to finish.
43. Write a Python program to create the multiplication table (from 1 to 10) of
a number.
Expected Output:
Input a number: 6
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
44. Write a Python program to construct the following pattern, using a nested
loop number.
Expected Output:
1
22
333
4444
55555
666666
7777777
88888888
999999999