E2-Loops & Ifs
E2-Loops & Ifs
1. Write a Python program to find those numbers which are divisible by 7 and
multiple of 5, between 1500 and 2700 (both included).
nl=[]
2. Write
for a Python
x in program to convert temperatures to and from celsius,
range(1500,
fahrenheit.
2700): [if
Formula : c/5 = f-32/9 [ where c = temperature in celsius
(x%7==0)
and and (x%5==0): in fahrenheit ]
f = temperature
Expectednl.append(str(
Output :
x)) print
60°C is 140 in
Fahrenheit 45°F is 7
in Celsius
if i_convention.upper() == "C":
result = int(round((9 * degree) / 5 +
32)) o_convention = "Fahrenheit"
elif i_convention.upper() == "F":
result = int(round((degree - 32) * 5 /
9)) o_convention = "Celsius"
else:
print("Input proper
convention.") quit()
print("The temperature in", o_convention, "is", result, "degrees.")
import random
target_num, guess_num = random.randint(1,
10), 0 while target_num != guess_num:
guess_num = int(input('Guess a number between 1 and 10 until you
get it right : '))
print('Well guessed!')
Python conditional statements and loops
4. Write a Python program to construct the following pattern, using a nested for
loop.
*
**
***
****
*****
****
***
**
*
n=5;
for i in range(n):
for j in
range(i):
print ('* ',
end="") print('')
for i in range(n,0,-
1): for j in
range(i):
print('* ',
end="") print('')
5. Write a Python program that accepts a word from the user and reverse it.
6. Write a Python program to count the number of even and odd numbers from
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
datalist = [1452, 11.23, 1+2j, True, 'w3resource', (0, -1), [5, 12],
{"class":'V',
"section":'A'}] for item
in datalist:
print ("Type of ",item, " is ", type(item))
for x in range(6):
if (x == 3 or
x==6): continue
print(x,end=' ')
print("\n")
while y<50:
print(y)
x,y =
y,x+y
10. Write a Python program which iterates the integers from 1 to 50. For
multiples of three print "Fizz" instead of the number and for the multiples of
five print "Buzz". For numbers which are multiples of both three and five print
"FizzBuzz".
Sample Output :
fizzbuzz
1
2
fiz
z4
buzz
Python conditional statements and loops
for fizzbuzz in range(50):
if fizzbuzz % 3 == 0 and fizzbuzz % 5
== 0: print("fizzbuzz")
continue
elif fizzbuzz % 3 ==
0: print("fizz")
continue
elif fizzbuzz % 5 ==
0: print("buzz")
continue
print(fizzbuzz)
11. Write a Python program which 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.
print(multi_list)
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).
lines = []
while
True:
l =
input() if
l:
lines.append(l.upper(
)) else:
break;
Python conditional statements and loops
for l in
lines:
print(l)
Python conditional statements and loops
13. Write a Python program which accepts a sequence of comma
separated 4 digit binary numbers as its input and print the numbers that
are divisible by 5 in a comma separated sequence.
Sample Data : 0100,0011,1010,1001,1100,1001
Expected Output : 1010
items = []
num = [x for x in
input().split(',')] for p in num:
x = int(p,
2) if not x
%5:
items.append(
p)
print(','.join(items
))
14. Write a Python program that accepts a string and calculate the number
of digits and letters.
Sample Data : Python
3.2 Expected Output :
Letters 6
Digits 2
s = input("Input a
string") d=l=0
for c in s:
if
c.isdigit(
): d=d+1
elif
c.isalpha():
l=l+1
else:
pass
print("Letters", l)
print("Digits", d)
15. Write a Python program to check the validity of password input
by users. Validation :
if x:
print("Not a Valid Password")
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.
items = []
for i in range(100,
401): s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])
%2==0): items.append(s)
print( ",".join(items))
***
* *
* *
*****
* *
* *
* *
result_str="";
for row in range(0,7):
for column in range(0,7):
Python conditional statements and loops
if (((column == 1 or column == 5) and row != 0) or ((row == 0 or
row
== 3) and (column > 1 and column <
5))):
result_str=result_str+"*"
else:
result_str=result_str+
" " result_str=result_str+"\
n"
print(result_str);
31. Write a Python program to calculate a dog's age in dog's 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:
if h_age < 0:
print("Age must be positive
number.") exit()
elif h_age <= 2:
d_age = h_age *
10.5 else:
d_age = 21 + (h_age - 2)*4
print(sum(10, 6))
print(sum(10, 2))
print(sum(10, 12))
Python conditional statements and loops
35. Write a Python program to check a string represent an integer or not.
Expected Output:
if x == y == z:
print("Equilateral
triangle")
elif x != y != z:
print("Scalene triangle")
else:
Python conditional statements and loops
print("isosceles triangle")
42. Write a Python program to calculate the sum and average of n integer
numbers (input from the user). Input 0 to finish.
count = 0
sum = 0.0
number = 1
while number != 0:
number =
int(input("")) sum
= sum + number
count += 1
Python conditional statements and loops
if count == 0:
print("Input some
numbers") else:
print("Average and Sum of the above numbers are: ", sum /
(count-1), sum)
43. Write a Python program to create the multiplication table (from 1 to 10) of a
number.
Expected Output:
1
22
333
4444
55555
666666
7777777
88888888
999999999
for i in range(10):
print(str(i) * i)