IMPORTANT SERIES PYTHON PROGRAM
1. #Write a short program to print the following series :
#(i) 1 4 7 10 .......... 40.
#(ii) 1 -4 7 -10 .......... -40
print("First Series:")
for i in range(1, 41, 3) :
print(i, end = ' ')
print("\nSecond Series:")
x=1
for i in range(1, 41, 3) :
print(i * x, end = ' ')
x *= -1
2. #Write Python programs to sum the given sequences:
#2/9 - 5/13 + 8/17 ...... (print 7 terms)
n = 2 #numerator initial value
d = 9 #denominator initial value
m = 1 #to add/subtract alternate terms
sum = 0
for i in range(7) :
t=n/d
sum += t * m
n += 3
d += 4
m *= -1
print("Sum =", sum)
3. Write Python programs to sum the given sequences:
1^2 + 3^2 + 5^2 + ..... + n^2 (Input n)
n = int(input("Enter the value of n: "))
i=1
sum = 0
while i <= n :
sum += i ** 2
i += 2
print("Sum =", sum)
4. Write a Python program to sum the sequence:
1 + 1/1! + 1/2! + 1/3! + ..... + 1/n! (Input n)
n = int(input("Enter the value of n: "))
sum = 0
for i in range(n + 1) :
fact = 1
for j in range(1, i) :
fact *= j
term = 1 / fact
sum += term
print("Sum =", sum)
5. Write programs to find the sum of the following series:
x - x^2/2! + x^3/3! - x^4/4! + x^5/5! - x^6/6! (Input x)
x = int(input("Enter the value of x: "))
sum = 0
m=1
for i in range(1, 7) :
fact = 1
for j in range(1, i+1) :
fact *= j
term = x ** i / fact
sum += term * m
m = m * -1
print("Sum =", sum)
6. Write programs to find the sum of the following series:
#x + x^2/2 + x^3/3 + ...... + x^n/n (Input x and n both)
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n + 1) :
term = x ** i / i
sum += term
print("Sum =", sum)
7. Write programs to print the following shapes:
# *
#* *
#* * *
#* *
# *
n = 3 # number of rows
# upper half
for i in range(n) :
for j in range(n, i+1, -1) :
print(' ', end = '')
for k in range(i+1) :
print('*', end = ' ')
print()
# lower half
for i in range(n-1) :
for j in range(i + 1) :
print(' ', end = '')
for k in range(n-1, i, -1) :
print('*', end = ' ')
print()
8. #Write programs to print the following shapes:
#*
#* *
#* * *
#* *
#*
n = 3 # number of rows
# upper half
for i in range(n) :
for k in range(i+1) :
print('*', end = ' ')
print()
# lower half
for i in range(n-1) :
for k in range(n-1, i, -1) :
print('*', end = ' ')
print()
9. '''Write programs to print the following shapes:
* *
* *
* *
*'''
n = 3 # number of rows
# upper half
for i in range(1, n+1) :
# for loop for initial spaces
for j in range(n, i, -1) :
print(' ', end = '')
#while loop for * and spaces
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
print('*', end = '')
else :
print(' ', end = '')
x += 1
print()
# lower half
for i in range(n-1, 0, -1) :
# for loop for initial spaces
for j in range(n, i, -1) :
print(' ', end = '')
#while loop for * and spaces
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
print('*', end = '')
else :
print(' ', end = '')
x += 1
print()
'''Write programs to print the following shapes:
**
* *
* *
* *
**
*'''
n = 4 # number of row
#upper half
for i in range(1, n+1) :
#while loop for * and spaces
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
print('*', end = '')
else :
print(' ', end = '')
x += 1
print()
#lower half
for i in range(n-1, 0, -1) :
#while loop for * and spaces
x=1
while x < 2 * i :
if x == 1 or x == 2 * i - 1 :
print('*', end = '')
else :
print(' ', end = '')
x += 1
print()
'''Write programs using nested loops to produce the following patterns:
A
AB
ABC
ABCD
ABCDE
A B C D E F'''
n=6
for i in range(n) :
t = 65
for j in range(i + 1) :
print(chr(t), end = ' ')
t += 1
print()
'''Write programs using nested loops to produce the following patterns:
BB
CCC
DDDD
E E E E E'''
n=5
t = 65
for i in range(n) :
for j in range(i + 1) :
print(chr(t), end = ' ')
t += 1
print()
'''Write programs using nested loops to produce the following patterns:
22
444
6666
8 8 8 8 8'''
for i in range(0, 10, 2):
for j in range(0, i + 1, 2) :
print(i, end = ' ')
print()
'''Write programs using nested loops to produce the following patterns:
44
666
8 8 8 8'''
for i in range(2, 10, 2) :
for j in range(2, i + 1, 2) :
print(i, end = ' ')
print()