Assignment 2 - Python Programming
Assignment 2 - Python Programming
Conditional
Solution:
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
Solution:
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
li.sort()
print(li[-2])
Solution:
def divisible_by_3_and_5(num):
if num % 3 == 0 and num % 5 == 0:
return True
else:
return False
number = int(input("Enter a number: "))
if divisible_by_3_and_5(number):
print(number, "is divisible by both 3 and 5.")
else:
print(number, "is not divisible by both 3 and 5.")
4. Python program to check if a given year is a leap year or not.
Solution:
year=int(input("Enter the year:"))
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
5. Write a Python program that checks whether a number entered by the user
is positive, negative, or zero.
Solution:
num=int(input("Enter the number:"))
if(num<0):
print("the number is negative!")
elif(num>0):
print("the number is positive!")
else:
print("the number is Zero!!")
Loops
Solution:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
for num in range(2, 101):
if is_prime(num):
print(num)
4. Write a program to take input from the user and print the Fibonacci Series
using a while loop for the given number of elements.
Solution:
num_elements = int(input("Enter the number of elements for the Fibonacci
series: "))
a, b = 0, 1
count = 0
print("Fibonacci Series:")
print()