#To generate a sequence a_n=1/n
i=1.0 #floating point division
n=input("enter the number of terms in the sequence");
print "a_n=1/n"
print "when n=",n,"a_n is"
for i in range(1,n+1): #iteration till the number of terms specified by the user
a=1.0/i
print "1/",i,",",
print "\n"
for i in range(1,n+1): #iteration till the number of terms specified by the user
a=1.0/i
print a,",",
n=input("Enter the number of terms in the sequence to generate the geometric progression");
i=1
print"the list of terms",
for i in range (n+1):print"b",i,",",
print "begins with",
for i in range (n+1): #iterate for the number of terms given as input
b_n=(-1)**i
print b_n,
print"\n","the list of terms",
for i in range (n+1):print"c",i,",",
print "begins with",
for i in range (n+1): #iterate for the number of terms given as input
c_n=2*(5**i)
print c_n,
print"\n","the list of terms",
for i in range (n+1):print"c",i,",",
print "begins with",
for i in range (n+1): #iterate for the number of terms given as input
d_n=6.0*((1.0/3.0)**i)
print d_n, #prints the fraction values in decimals. Floating point division
n=input("Enter the number terms in the sequence");
s_n=-1+4*n
t_n=7-3*n
i=0
print "The list of terms",
for i in range(n):
print "s",i,",",
print "begins with",
for i in range(n):#generates the sequence for -1*4i
print -1+4*i,
print "\nThe list of terms",
for i in range(n):
print "t",i,",",
print "begins with",
for i in range(n):#generates the sequence for 7-3i
print 7-3*i,
a=[2,0,0,0] #assigning a[0]=2 (Given)
for i in range(1,4):#iteration to run till a[3]
a[i]=a[i-1]+3
print "a[",i,"]",a[i]
a=[3,5,0,0] #assingning a[0],a[1] to the given values
for i in range(2,4): # iterations to find the successive values. If values are to be found for further terms the for loop "stop" has to be modified
a[i]=a[i-1]-a[i-2]
print "a[",i,"]",a[i]
f=[0,1,0,0,0,0,0] #assingning a[0],a[1] to the given values
print "Fibonacci series is"
for i in range(2,7): # iterations to find the successive values. If values are to be found for further terms the for loop "stop" has to be modified
f[i]=f[i-1]+f[i-2]
print "f[",i,"]=f[",i-1,"]+f[",i-2,"]=",f[i]
n=1
result=0
number=input("Enter the number");
for i in range(1,number):
n=n+i*n
print "The factorial of",number,"is",n
#finding the summation of j^2
up=input("Enter the upper limit for the operation j^2");
low=input("Enter the lower limit for the operation j^2");
sum=0
print "The square of terms form 1 to n",
for j in range (low,up+1): #summation. Iteration from lower to upper limit.
print j,"^2+",
j=j**2 #square function is computed as '**'
sum=sum+j
print "=",sum
k=4 #lower limit
sum=0
print "The value for the sequence",
for k in range (4,8+1,1): #8+1 , 8 is the upper limit, in python to make for loop run till the limit equal to upper limit we give a +1.
print "(-1)^",k,"+",
sum=sum+((-1)**k)
print "=",sum
globals()['j']=0
i=0
globals()['s']=0
upj=input("Enter the upper limit for the inner summation");
lowj=input("Enter the lower limit for the inner summation");
upi=input("Enter the upper limit for the outer summation");
lowi=input("Enter the lower limit for the outer summation");
for i in range (lowj,upj+1):
j=j+i
for l in range(lowi,upi+1):
s=s+(j*l)
print s
#To print series 1 once, 2 twice, 3 thrice and so on
a=[]
i=1
for i in range(1,10+1): #for loop to initialise the number
for j in range(1,i+1):#for loop to iterate to make the count
print i,
s=0 #initialise it to zero to store the results
globals()['res']=0 #difining result as global variable since it has to be accessed outside the loop
print "Sum of values of s for all the members of the set {",
for s in range (0,4+1,2): #iterate for terms 0,4,6
print s,
res=res+s
print "} is",res
def getmat(): #function to get the matrix elements
m = int(input('number of rows, m = '))
n = int(input('number of columns, n = '))
matrix = []; columns = []
for i in range(0,m):
matrix.append([])
for j in range(0,n):
matrix[i].append(0)
print ('entry in row: ',i+1,' column: ',j+1)
matrix[i][j] = int(input())
return (matrix)
def matrixADD(m1,m2): #function to add the matrix.
z=[]
for i in range (len(m1)):
tem = []
for j in range (len(m2)):
x=m1[i][j]+m2[i][j]
tem.append(x)
z.append(tem)
return z
mat1=[]
mat2=[]
Z=[]
print "Enter the elements of matrix 1"
mat1=getmat() #function call
print "Enter the elements of matrix 2"
mat2=getmat() #function call
print "Addition of \n matrix 1",mat1,"and \n matrix 2",mat2,"is \n",matrixADD(mat1,mat2) #function call to add
# Program to multiply two matrices using nested loops
# 3x3 matrix
X = [[1,0,4],
[2,1,1],
[3,1,0],
[0,2,2]]
# 3x4 matrix
Y = [[2,4],
[1,1],
[3,0]]
# result is 3x4
result = [[0,0],
[0,0],
[0,0,],
[0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
print "The multiplication of the two matrices AB is"
for r in result:
print(r)
# Program to transpose a matrix using nested loop
# iterate through rows
def mattrans(X,result):
print "The transpose is"
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
def getmat():
row = int(input('number of rows = '))
col = int(input('number of columns = '))
matrix = []; columns = []
for i in range(0,row):
matrix.append([])
for j in range(0,col):
matrix[i].append(0)
print ('entry in row: ',i+1,' column: ',j+1)
matrix[i][j] = int(input())
for c in range(col):
for r in range(row):
result[c][r]=0
mattrans(matrix,result)
print "Enter the elements of the matrix"
getmat()
#mattrans(mat1)