0% found this document useful (0 votes)
4 views7 pages

Assignment 2 - Python Programming

The document contains a series of Python programming assignments focusing on conditional statements and loops. It includes solutions for checking palindromes, finding the second largest number in a list, checking divisibility, determining leap years, and identifying positive/negative numbers. Additionally, it covers generating prime numbers, computing factorials, and printing Fibonacci series using loops.

Uploaded by

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

Assignment 2 - Python Programming

The document contains a series of Python programming assignments focusing on conditional statements and loops. It includes solutions for checking palindromes, finding the second largest number in a list, checking divisibility, determining leap years, and identifying positive/negative numbers. Additionally, it covers generating prime numbers, computing factorials, and printing Fibonacci series using loops.

Uploaded by

minatoax.gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Assignment-2

Conditional

1. Write a Python program that checks if a string is a palindrome, ignoring


spaces, punctuation, and case sensitivity.

Solution:
def isPalindrome(s):
return s == s[::-1]

# Driver code
s = "malayalam"
ans = isPalindrome(s)

if ans:
print("Yes")
else:
print("No")

2. Write a Python program to find the second largest number in a list.

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])

3.Write a Python program to check whether a number is divisible by both 3


and 5.

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

1.complete all example program given in ppt for loops

Solution: Paste code and screenshot of output


2.Write a program using range and for loop to print prime numbers between
2 and 100.

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)

3.Write a program to compute the factorial of a number given by the user.


Use condition statements and looping construct only.
Solution:
num = int(input("Enter a number: "))
factorial=1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

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:")

while count < num_elements:


print(a, end=" ")
a, b = b, a + b
count += 1

print()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy