0% found this document useful (0 votes)
30 views20 pages

Python List Imca241

The document contains 12 programming problems related to Python. The problems cover topics like perfect numbers, Fibonacci series, palindrome numbers, list methods, factorials, and printing patterns. For each problem, the expected input, output and code are provided.
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)
30 views20 pages

Python List Imca241

The document contains 12 programming problems related to Python. The problems cover topics like perfect numbers, Fibonacci series, palindrome numbers, list methods, factorials, and printing patterns. For each problem, the expected input, output and code are provided.
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
You are on page 1/ 20

PYTHON LAB LIST-IMCA 241

1.Write a program to identify a Perfect number.

num=int(input("Enter the number: "))


sum_v=0
for i in range(1,num):
if (num%i==0):
sum_v=sum_v+i
if(sum_v==num):
print("The entered number is a perfect number")
else:
print("The entered number is not a perfect number")

OUTPUT

2. Write a program to print the first n elements of the Fibonacci series.

lim=int(input("Enter the LIMIT ="))

n1=0
n2=1
print "Fibonnaci Series :"
print(n1)
print(n2)
for i in range(1,lim):

n3=n1+n2
n1=n2
n2=n3
print(n3)
OUTPUT

3. Write a program to reverse a number and find the sum of digits in the given number.

num=int(input("Enter the number ="))


s=0
rev=0
while( num > 0 ):

rem=num%10
s=s+rem
rev=rev*10+rem
num=num//10

print "\nReversed Number =",rev


print "\nSum of Digits=",s

OUTPUT
4. Write a program to check whether a number is a palindrome or not.

num=int(input("Enter a number ="))


s=0
rev=0
temp=num
while( num > 0 ):

rem=num%10
s=s+rem
rev=rev*10+rem
num=num//10

if(rev == temp ):
print "The number is Palindrome Number!"
else :
print "The number is not a Palindrome Number!"

OUTPUT

5. Write a program printing the Sum of squares of first n natural numbers

n=int(input("Enter a Number ="))


s=0

for i in range(1,n+1):
s=s+(i*i)

print "\n The Sum of squares =",s


OUTPUT

6. Write a program to read a year (4 digit integer) and tell whether the given
year is/was a leap year.

year = int(input("Enter a Year : "))

if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):


print("The Year is a leap Year!")
else:
print("The Year is not a leap Year !")
7 . write a program to perform all the list methods

mylist=["Apple", "Grape", "Kiwi"]


print"\n
---------------------------------------------------------------------------------------------------------------------
--------------"

print "\n The List one =",mylist

mylist.append("Pear")
print "\n The list after append() method =",mylist

mylist.extend(["Cherry","Strawberry",10,20,30])
print "\n The list after extend() method =",mylist

index_of_kiwi=mylist.index("Kiwi")
print "\n Index of kiwi using index() method =",index_of_kiwi

mylist.insert(3,"Orange")
print"\n The list after insert() method =",mylist

mylist.pop(2)
print "\n The list after popping index 2 using pop() method =",mylist

mylist.sort()
print"\n The list after sorting using sort() method=",mylist

mylist.reverse()
print"\n The list after reversing using reverse() method=",mylist

mylist.remove("Orange")
print"\n The list after removing orange using remove() method=",mylist

print "\n
---------------------------------------------------------------------------------------------------------------------
--------------"

mylist2=[10,20,30,40,50]
print "\n The list two =",mylist2
sum_of_list=sum(mylist2)
print"\n calcuting the sum using sum() method=",sum_of_list

max_num=max(mylist2)
print"\n calcuting the largest number in the list using max() method=",max_num

min_num=min(mylist2)
print"\n calcuting the smallest number in the list using max() method=",min_num

del mylist2[4]
print "\n After deleting element in the 4th index usind del() method=",mylist2

length=len(mylist2)
print "\n The length of the list=",length

OUTPUT
8. Write a program to find factorial

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

OUTPUT

9 . Python program to :

A. Interchange first and last elements in a list


B. To swap two elements in a list
print "--------- List --------"
mylist=[]
n=int(input("Enter no: of elements in list1="))
print "enter the elements ="
for i in range(1,n):
ele=int(input())
mylist.append(ele)
print(mylist)

while True:

print "\n"
print "----MENU----"
print "1.Interchage First and Last Element"
print "2.Swap two numbers"
print "3.Exit"
choice=int(input("choose an option ="))
if choice == 1 :
print "The List before Interchange=",mylist
mylist[0],mylist[-1]=mylist[-1],mylist[0]
print "The List after Interchange =",mylist
elif choice == 2 :
index1=int(input("Enter the first element to be swapped ="))
index2=int(input("Enter the second element to be swapped ="))
sw1=mylist.index(index1)
sw2=mylist.index(index2)
print "List before Swap =",mylist
mylist[sw1],mylist[sw2]=mylist[sw2],mylist[sw1]
print "List after Swap =",mylist
elif choice == 3:
break
else:
print "invalid choice!"

OUTPUT
10. Write a Python program to :

A. To reverse a List
B. Sort the list alphabetically

mylist=[]
mylist2=[]
while True:
print "-------------------------- MENU -------------------------------"
print "1.Reverse"
print "2.Sort"
print "3.Exit"
choice=int(input("Enter a choice :"))
if choice == 1:
print"--------------------------- LIST ----------------------------------"
n=int(input("Enter no: of elements in the list :"))
print "Enter the elements :"
for i in range(0,n):
ele=int(input())
mylist.append(ele)
print "Original List : ",mylist
mylist.reverse()
print "List After Reversing :",mylist
elif choice == 2:
print"--------------------------- LIST ----------------------------------"
n=int(input("Enter no: of elements in the list :"))
print "Enter the elements :"
for i in range(0,n):
ele=raw_input()
mylist2.append(ele)
print "Original List : ",mylist2
mylist2.sort()
print "List After sorting :",mylist2
elif choice == 3:
break
else:
print "invalid choice!"
OUTPUT
11. Write a Python program to
A. To check if element exists in list
B. Print odd numbers in a list
C. Print positive numbers in a list

mylist=[]
mylist2=[]
mylist3=[]
while True:

print "-------------------------- MENU -------------------------------"

print "1.To check if element exists in list"


print "2.Print odd numbers in a list"
print "3.Print positive numbers in a list"
print "4.Exit"

choice=int(input("Enter a choice :"))

if choice == 1:
print"--------------------------- LIST ----------------------------------"
n=int(input("Enter no: of elements in the list :"))
print "Enter the elements :"

for i in range(0,n):
ele=int(input())
mylist.append(ele)

print "Original List : ",mylist


element=int(input("Enter the element to be serached :"))

for i in range(0,n):
if element == mylist[i]:
print "Element exits in the list at the index:",mylist.index(element)
break
else:
print "Element not found!"

elif choice == 2:
print"--------------------------- LIST ----------------------------------"
n=int(input("Enter no: of elements in the list :"))
print "Enter the elements :"

for i in range(0,n):
ele=int(input())
mylist2.append(ele)

print "Original List : ",mylist2

print "Odd elements are :"


for i in range(0,n):
if mylist2[i] % 2 != 0:
print mylist2[i]

elif choice == 3:
print"--------------------------- LIST ----------------------------------"
n=int(input("Enter no: of elements in the list :"))
print "Enter the elements :"

for i in range(0,n):
ele=int(input())
mylist3.append(ele)

print "Original List : ",mylist3

print "Positive elements are :"


for i in range(0,n):
if mylist3[i] > 0:
print mylist3[i]

elif choice == 4:
break

else:
print "invalid choice!"
OUTPUT
12. Using control structures and loops write a program to print these patterns :
pattern1
01

024

0369

0 4 8 12 16

0 5 10 15 20 25

0 6 12 18 24 30 36

pattern2
*****
****
***
**
*
*
**
***
****
*****
pattern 3
****************
*******__*******
******____******
*****______*****
****________****
***__________***
**____________**
*______________*
print "----------------------- PATTERNS -----------------------------"
n=int(input("Enter the no: of rows = "))

print "\n"
print "------------------------ PATTERN 1 ------------------------------"
for i in range(n):
num = 0
for j in range(i + 1):
print num ,
num += i
print

print "------------------------ PATTERN 2 ------------------------------"

for i in range(n-1):
for j in range(i):
print ' ',
for k in range(2*(n-i)-1):
print '*',
print
for i in range(n):
for j in range(n-i-1):
print ' ',
for k in range(2*i+1):
print '*',
print

print "------------------------ PATTERN 3 ------------------------------"


print "*" * n

i = (n // 2) - 1
j=2

while i != 0:
while j <= (n - 2):
print "*" * i, "_" * j, "*" * i
i=i-1
j=j+2

print('\n')
OUTPUT

You might also like