0% found this document useful (0 votes)
7 views5 pages

Python Programming Exercises and Solutions

The document contains a series of Python programming tasks, including printing a calendar, calculating factorials using recursion, identifying even-length words in a string, checking for Fibonacci numbers, converting time formats, sorting dictionaries, counting occurrences in a list, explaining lambda expressions, producing a star triangle, and checking for palindromes. Each task includes the corresponding Python code and expected output. The document serves as a programming exercise for learning various Python concepts.

Uploaded by

bgmi131104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Python Programming Exercises and Solutions

The document contains a series of Python programming tasks, including printing a calendar, calculating factorials using recursion, identifying even-length words in a string, checking for Fibonacci numbers, converting time formats, sorting dictionaries, counting occurrences in a list, explaining lambda expressions, producing a star triangle, and checking for palindromes. Each task includes the corresponding Python code and expected output. The document serves as a programming exercise for learning various Python concepts.

Uploaded by

bgmi131104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write a program in python to print the calendar for the month of March
1991.
import calendar
# To take month and year input from the user
yy = int(input("Enter year: "))
mm = int (input ("Enter month: "))
print([Link](yy, mm))
OUTPUT:

[Link] a program to print factorial using recursion.


n = int(input("Enter input number : "))

fact = 1
if n < 0:
print("Factorial does not exist for negative numbers")
elif n == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, n + 1):
fact = fact * i
print("The factorial of",n,"is",fact)
OUTPUT:
[Link] a program to print even length words in a string.
str = input('ENTER THE STRING:')
str1 = [Link]()
print('EVEN LENGTH WORDS ARE:')
for i in str1:
if len(i) % 2 == 0:
print(i)
OUTPUT:

[Link] a program to check a given number is fibonacci series.


n=int(input("Enter the number: "))
c=0
a=1
b=1
if n==0 or n==1:
print("Yes")
else:
while c<n:
c=a+b
b=a
a=c

if c==n:
print("YES")
else:
print("No")
OUTPUT:

[Link] a program to convert time from 12 hours format to 24 hours format.


ip = input("Enter Time in 12 Hour Format (HH:MM:SS AM/PM) : ")

if("AM" in ip and ip[:2] == "12"):

op = ("00" + ip[2:-2])

elif("AM" in ip):

op = ip[2:-2]

if("PM" in ip and ip[:2] == "12"):

op = ip[:-2]

else:

op = str(int(ip[:2]) + 12) + ip[2:-2]

print("24 Hour Format : " + op)

OUTPUT:
[Link] a program to sort the list of dictionaries by values in python using the lamda
function.

list = [{"name": "Nandini", "age": 20},

{"name": "Manjeet", "age": 20},

{"name": "Nikhil", "age": 19}]

print("The list printed sorting by age: ")

print(sorted(list, key=lambda i: i['age']))

OUTPUT:

[Link] a program to count Occurences of an Element in a list.

my_list = [1, 2, 3, 4, 5, 6, 5, 8, 9, 5]

count = my_list.count(5)

print(count)

[Link] Lamda expression.

Python Lambda Functions are anonymous functions means that the function is without a
name. As we already know the def keyword is used to define a normal function in Python.
Similarly, the lambda keyword is used to define an anonymous function in Python.

Syntax: lambda arguments : expression


[Link] a program in python to produce star triangle.

n=5

for i in range(n):

for j in range(i,n):

print(" ",end="")

for j in range(i):

print("*",end="")

for j in range(i+1):

print("*",end="")

print()

OUTPUT:

[Link] a program to check if the sequence is a palindrome.

input_string = 'civic'

rev = input_string[::-1]

if input_string == rev:

print(rev + " is Palindrome")

else:

print(rev + " is not Palindrome")

OUTPUT:

You might also like