Professional Documents
Culture Documents
21EC014 - Assignment 6
21EC014 - Assignment 6
1
EC 21EC014
*
**
***
****
*****
****
***
**
*
ANS: 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('')
2
EC 21EC014
5) Write a Python program that accepts a word from the user and reverses
it.
ANS: word = input("Input a word to reverse: ")
for char in range(len(word) - 1, -1, -1):
print(word[char], end="")
print("\n")
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
ANS: numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple
count_odd = 0 count_even = 0 for x in numbers: if not x
% 2:
count_even+=1
else:
count_odd+=1 print("Number of even
numbers :",count_even) print("Number of odd
numbers :",count_odd) 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'}]
ANS: 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))
3
EC 21EC014
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
ANS: for x in range(6): if (x ==
3 or x==6):
continue
print(x,end=' ')
print("\n")
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
ANS: x,y=0,1
while y<50:
print(y)
x,y = y,x+y
10) Write a
Python
program
that iterates
the integers
from 1 to 50.
For
multiples of
4
EC 21EC014
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
ANS: for fizzbuzz in range(51):
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)
5
EC 21EC014
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.
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). ANS: lines = [] while True:
l = input()
if l:
lines.append(l.upper())
else:
break;
for l in lines:
print(l)
6
EC 21EC014
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
ANS: 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
passwords input
by users.
Validation :
7
EC 21EC014
∙ Maximum length 16
characters. ANS: import re p=
input("Input your password") x =
True while x: if (len(p)<6 or
len(p)>12): break elif not
re.search("[a-z]",p):
break elif not
re.search("[0-9]",p): break
elif not re.search("[A-Z]",p):
break elif not
re.search("[$#@]",p):
break elif
re.search("\s",p):
break
else:
print("Valid
Password") x=False
break
if
x:
print("Not a Valid Password")
s = str(i)
items.append(s) print(
",".join(items))
***
**
**
*****
**
**
**
ANS: result_str=""; for
row in range(0,7): for
column in range(0,7):
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);
9
EC 21EC014
****
**
**
**
**
**
****
ANS: result_str="";
for row in range(0,7):
for column in range(0,7):
if (column == 1 or ((row == 0 or row == 6) and (column > 1 and column <
5)) or (column == 5 and row != 0 and row != 6)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
19) Write a Python program to print the alphabet pattern 'E'. Expected
Output:
*****
*
*
****
*
10
EC 21EC014
*
*****
ANS: result_str=""; for
row in range(0,7): for
column in range(0,7):
if (column == 1 or ((row == 0 or row == 6) and (column > 1 and column <
6)) 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);
20) Write a Python program to print the alphabet pattern 'G'. Expected
Output:
***
**
*
* ***
**
**
***
ANS: result_str=""; for
row in range(0,7): for
column in range(0,7):
if ((column == 1 and row != 0 and row != 6) or ((row == 0 or row == 6)
and column > 1 and column < 5) or (row == 3 and column > 2 and column < 6)
or (column == 5 and row != 0 and row != 2 and row != 6)):
result_str=result_str+"*"
else:
11
EC 21EC014
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
21) Write a Python program to print the alphabet pattern 'L'. Expected
Output:
*
*
*
*
*
*
*****
ANS: result_str=""; for row in range(0,7): for column in
range(0,7): if (column == 1 or (row == 6 and column != 0
and column < 6)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
22) Write a Python program to print the alphabet pattern 'M'. Expected
Output:
**
**
****
***
**
**
12
EC 21EC014
**
ANS: result_str=""; for
row in range(0,7): for
column in range(0,7):
if (column == 1 or column == 5 or (row == 2 and (column == 2 or column
== 4)) or (row == 3 and column == 3)):
result_str=result_str+"* "
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
23) Write a Python program to print the alphabet pattern 'O'. Expected
Output:
***
**
**
**
**
**
***
ANS: result_str=""; for
row in range(0,7): for
column in range(0,7):
if (((column == 1 or column == 5) and row != 0 and row != 6) or ((row ==
0 or row == 6) and column > 1 and column < 5)):
result_str=result_str+"*" else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
13
EC 21EC014
24) Write a Python program to print the alphabet pattern 'P'. Expected
Output:
****
**
**
****
*
*
*
ANS: result_str=""; for
row in range(0,7):
for column in range(0,7):
if (column == 1 or ((row == 0 or row == 3) and column > 0 and column <
5) or ((column == 5 or column == 1) and (row == 1 or row == 2))):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
25) Write a Python program to print the alphabet pattern 'R'. Expected
Output:
****
**
**
14
EC 21EC014
****
**
**
**
ANS: result_str=""; for
row in range(0,7): for
column in range(0,7):
if (column == 1 or ((row == 0 or row == 3) and column > 1 and column <
5) or (column == 5 and row != 0 and row < 3) or (column == row - 1 and row
> 2)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
****
*
*
***
*
*
****
ooooooooooooooooo
ooooooooooooooooo
ooooooooooooooooo
oooo oooo oooo
ooooooooooooooooo
15
EC 21EC014
ooooooooooooooooo
ooooooooooooooooo
oooo
oooo
oooo
ooooooooooooooooo ooooooooooooooooo
ooooooooooooooooo
ANS: result_str=""; for row in range(0,7): for column in range(0,7):
if (((row == 0 or row == 3 or row == 6) and column > 1 and column < 5) or
(column == 1 and (row == 1 or row == 2 or row == 6)) or (column == 5 and
(row == 0 or row == 4 or row == 5))):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
row=15 col=18
result_str="" for i in
range(1,row+1):
if((i<=3)or(i>=7 and i<=9)or(i>=13 and i<=15)):
for j in range(1,col):
result_str=result_str+"o"
result_str=result_str+"\n" elif(i>=4 and i<=6): for
j in range(1,5): result_str=result_str+"o"
result_str=result_str+"\n" else: for j in
range(1,14):
result_str=result_str+" "
for j in range(1,5):
result_str=result_str+"o"
result_str=result_str+"\n"
print(result_str);
16
EC 21EC014
28) Write a Python program to print the alphabet pattern 'U'. Expected
Output:
**
**
**
**
**
**
***
17
EC 21EC014
29) Write a Python program to print the alphabet pattern 'X'. Expected
Output:
**
**
**
*
**
**
**
ANS: result_str=""; for row in range(0,7): for column in
range(0,7): if (((column == 1 or column == 5) and (row > 4 or row
< 2)) or row == column and column > 0 and column < 6 or (column == 2
and row == 4) or (column == 4 and row == 2)):
result_str=result_str+"*"
else:
result_str=result_str+" "
result_str=result_str+"\n" print(result_str);
18
EC 21EC014
*******
*
*
*
*
*
*******
ANS: result_str=""; for row in range(0,7): for column in
range(0,7): if (((row == 0 or row == 6) and column >= 0 and
column <= 6) or row+column==6):
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 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:
Input a dog's age in human years: 15 The
dog's age in dog's years is 73
ANS: h_age = int(input("Input a dog's age in human years: "))
if h_age < 0:
print("Age must be positive
number.") exit() elif h_age <= 2:
d_age = h_age * 10.5 else:
19
EC 21EC014
20
EC 21EC014
if month_name == "February":
print("No. of days: 28/29 days") elif month_name in
("April", "June", "September", "November"):
print("No. of days: 30 days") elif month_name in ("January", "March",
"May", "July", "August", "October", "December"): print("No. of days:
31 day") else:
print("Wrong month name")
34) Write a Python program to sum two integers. However, if the sum is
between 15 and 20 it will return 20.
ANS: def sum(x, y):
sum = x + y if sum in
range(15, 20):
return 20
else:
return sum
print(sum(10, 6))
print(sum(10, 2))
print(sum(10, 12))
21
EC 21EC014
if x == y == z:
print("Equilateral triangle") elif
x==y or y==z or z==x:
print("isosceles triangle") else:
print("Scalene triangle")
22
EC 21EC014
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:
Input the month (e.g. January, February etc.): july Input the day: 31
Season is autumn
ANS: month = input("Input the month (e.g. January, February etc.): ") day
= int(input("Input the day: "))
print("Season is",season)
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
23
EC 21EC014
39) Write a Python program to display the sign of the Chinese Zodiac for the
given year in which you were born. Expected Output:
Input your birth year: 1973
24
EC 21EC014
leap_year = False
27
EC 21EC014
count = 0 sum
= 0.0 number
=1
while number != 0:
number = int(input(""))
sum = sum + number
count += 1
if count == 0:
print("Input some numbers")
else:
print("Average and Sum of the above numbers are: ", sum / (count-1),
sum)
6 x 10 = 60
ANS: n = int(input("Input a number: "))
1
22
333
4444
55555
666666
7777777
88888888
999999999
ANS: for i in range(10):
print(str(i) * i)
29