You are on page 1of 6

EC 1 21EC027

EC262 : PYTHON PROGRAMMING


Assignment – 5

1) Repeating something over and over until a particular condition is


satisfied is called Loop.

2) An index loop repeates for a number of times that is determined by a


numeric value. It is also known as a FOR loop.

3) A piece of code that lacks a functional exit so that it repeats indefinitely.

4) A list is Mutable, changeable, and ordered sequence of elements.

5) A “While loop” is used to repeat a specific block of code an unknown


number of times, until a condition is satisfied.

6) A FOR loop is used for iterating over a sequence. That is, either a list, a
tuple, a dictionary, a set, or a string.

7) If a body exists inside the body of another loop, is called Nested loop.

8) False

9) False

10) print("The first 10 natural numbers are:")


for i in range(1, 11):
print (i)

11) for even_no in range(2, 20, 2):


print(even_no, end = " ")

12) sum = 0
print("Enter the value of n:")

1
EC 1 21EC027

a = int(input()) print("Enter " + str(a) + "Numbers: ")


for i in range(a):
num = int(input())
sum = sum + num
print("Sum of " + str(a) + "Numbers = " + str(sum))

13) n = int(input("Enter the value: "))


sum = 0
for i in range(1, n+1, 2):
sum+=i
print(sum)

14) n = int(input("Multiplication table: " ))


for i in range(1, 10+1):
print("{} * {}". format(i, n, i*n))

15) a = [1, 2 , 3, 4, 5]
for i in range(len(a)):
print a[i]

16) num = 12434364532


print(len(str(num)))

17) def isPalindrome(function):


if(function == function[::-1]):
return "The function is a Palindrome."
else:
return "The function is not a Palindrome."
function = input("Enter value:")
print("isPalindrome(string)")

2
EC 1 21EC027

18) the_input = input("Enter string: ")


reversed = the_input[::-1]
print(reversed)

19) num = int(input("Enter a no: "))


sum = 0
n1 = len(str(num))
temp = num
while temp > 0:
digit = temp % 10
sum += digit**n1
temp //= 10
if num == sum:
print(num, "is an armstrong no")
else:
print(num, "is not an armstrong no")

20) numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)


count_odd = 0
count_even = 0
for i in numbers:
if not i % 2:
count_even +=1
else:
count_odd +=1
print("Number of even numbers: ", count_even)
print("Number of odd numbers: ", count_odd)

21) x=0
y=1
print(x)
print(y)
for x in range(1, 9):
z=x+y

3
EC 1 21EC027

print(z)
x,y = y,x

22) num = 7
factorial = 1
if num < 0:
print("Fac does not exists for negative no:")
elif num == 0:
print("The fac of 0 is 1")
else:
for i in range(1, num+1):
factorial = factorial*i
print("The fac of", num, "is", factorial)

23) alpha,string=0,"Geeks1234"
for i in string:
if (i.isalpha()):
alpha+=1
print("Number of Digit is", len(string)-alpha)
print("Number of Alphabets is", alpha)

24) given_range = 25
for i in range(given_range+1):
if i % 4 == 0 and i % 5 == 0:
print("fizzbuzz")
continue
if i % 4 == 0 and i%5!=0:
print("fizz")
continue
if i % 5 == 0 and i % 4!= 0:
print("buzz")
else:
print(i)

4
EC 1 21EC027

25) import re password = "S@k$hi_P@nchal27"


flag = 0
while True:
if (len(password)<=8):
flag = -1
break
elif not re.search("[a-z]", password):
flag = -1
break
elif not re.search("[A-Z]", password):
flag = -1
break
elif not re.search("[0-9]", password):
flag = -1
break
elif not re.search("[_@$]" , password):
flag = -1
break
elif re.search("\s" , password):
flag = -1
break
else:
flag = 0
print("Valid Password")
break
if flag == -1:
print("Not a Valid Password ")

26) print("List of months: January, February, March, April, May, June,


July, August, September, October, November, December")
month_name = input("Input the name of Month: ")
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"):
5
EC 1 21EC027

print("No. of days: 31 day")


else:
print("Wrong month name")

27) alpha,string=0,"Patel1234"
for i in string:
if (i.isalpha()):
alpha+=1
print("Number of Digit is", len(string)-alpha)
print("Number of Alphabets is", alpha)

28) list1 = [10, -21, 4, -45, 66, -93, 1]


pos_count, neg_count = 0, 0
for num in list1:
if num >= 0:
pos_count += 1
else:
neg_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list: ", neg_count)

29) nums = [43, 20, 53, 12, 53, 5, 3, 2]


even = []
odd = []
for i in nums:
if(i % 2 == 0):
even.append(i)
else:
odd.append(i)
print("Even List: ",even)
print("Odd List: ",odd)

You might also like