Python List Imca241
Python List Imca241
OUTPUT
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.
rem=num%10
s=s+rem
rev=rev*10+rem
num=num//10
OUTPUT
4. Write a program to check whether a number is a palindrome or not.
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
for i in range(1,n+1):
s=s+(i*i)
6. Write a program to read a year (4 digit integer) and tell whether the given
year is/was a leap year.
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:
elif num == 0:
else:
factorial = factorial*i
OUTPUT
9 . Python program to :
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:
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)
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)
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)
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
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
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