[go: up one dir, main page]

0% found this document useful (0 votes)
106 views31 pages

Computer Science Project Class Xi

The document contains code snippets for various Python programs related to tuples, patterns, lists, series sums, general programs, strings, and dictionaries. 1) It includes programs to test the data type of a variable, sort tuples alphabetically and by second item, check if an element is in a tuple, and generate a list and tuple from user input. 2) Pattern programs print number and digit pyramids, an inverted pyramid, and a star triangle. 3) List programs find the average and minimum of a list, sort a list, search for an element, and calculate the mean. 4) Series sum programs calculate sums of 12+22+32+..., x^1/

Uploaded by

kunjrastogi2007
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)
106 views31 pages

Computer Science Project Class Xi

The document contains code snippets for various Python programs related to tuples, patterns, lists, series sums, general programs, strings, and dictionaries. 1) It includes programs to test the data type of a variable, sort tuples alphabetically and by second item, check if an element is in a tuple, and generate a list and tuple from user input. 2) Pattern programs print number and digit pyramids, an inverted pyramid, and a star triangle. 3) List programs find the average and minimum of a list, sort a list, search for an element, and calculate the mean. 4) Series sum programs calculate sums of 12+22+32+..., x^1/

Uploaded by

kunjrastogi2007
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/ 31

TUPLE PROGRAMS

1.Write a Python program to test if a variable is a list or tuple


or a set.

x = ('tuple', False, 3.2, 1)


if type(x) is list:
print('x is a list')
elif type(x) is set:
print('x is a set')
elif type(x) is tuple:
print('x is a tuple')
else:
print('Neither a list or a set or a tuple.')
2. Write a Python program to sort a list of tuples
alphabetically

Tuple(tup):
n = len(tup)
for i in range(n):
for j in range(n-i-1):
if tup[j][0] > tup[j + 1][0]:
tup[j], tup[j + 1] = tup[j + 1], tup[j]
return tup
tup = [("Amaruta", 20), ("Zoe", 32), ("Akshay", 25),
("Nilesh", 21), ("C", "D"), ("Penny", 22)]
print(SortTuple(tup))

3. Write a Python program to sort a list of tuples by the


second Item.
Tuple(tup):
lst = len(tup)
for i in range(0, lst):
for j in range(0, lst-i-1):
if (tup[j][1] > tup[j + 1][1]):
temp = tup[j]
tup[j]= tup[j + 1]
tup[j + 1]= temp
return tup
tup =[('for', 2), ('is', 9), ('to', 7),('Go', 5), ('or', 15), ('a', 13)]
print(Sort_Tuple(tup))

4. Write a python program to Check if the given element is


present in tuple or not.
test_tup = (10, 4, 5, 6, 8)
print("The original tuple : " + str(test_tup))
N = int(input("value to be checked:"))
res = False
for ele in test_tup :
if N == ele :
res = True
break
print("Does contain required value ? : " + str(res))

5. Write a Python program which accepts a sequence of


comma-separated numbers from user and generate a list and
a tuple with those numbers.
values = input("Input some comma seprated numbers : ")
list = values.split(",")
tuple = tuple(list)
print('List : ',list)
print('Tuple : ',tuple)

PATTERN PROGRAMS
1.Wap to print number semi-pyramid.
n=9

for n in range(1, n+1):

for m in range(1, n + 1):

print(m, end=’ ’‘)

print()
2.Wap to print the Same Digit Pattern Using Inverted Pyramid
Pattern .
n=7
digit = n
for k in range(n, 0, -1):
for m in range(0, k):
print(digit, end=' ')
print("\r")

3.Wap to Print Numbers Using Inverted Pyramids.

n=7
m=0
for k in range(n, 0, -1):
m += 1
for n in range(1, k + 1):
print(m, end=' ')
print('\r')

4.Wap to Print a Star Equilateral Triangle.

s=9
y = (2 * s)-2
for k in range(0, s):
for m in range(0, y):
print(end=" ")
y = y-1
for m in range(0, k + 1):
print("* ", end=' ')
print(" ")

LIST PROGRAMS

1. WAP to find average of numbers in a List.Program.


A = [6, 22, 7, 66, 88, 99]
Sum = 0
Avg = 0
for x in range(len(A)):
Sum += A[x];
Avg = Sum/len(A);
print(“Average = “, Avg)

2.Wap to print the program.


x=[]
n=eval(input("enter size of a list:"))
for i in range (0,n):
y=int(input("enter no"))
x.append(y)
print(x)
count=0
for i in range(0,n-1):
for j in range(i+1,n):
if x[i] > x[j]:
x[i], x[j] = x[j],x[i]
count=count+1
print("pass",i+1,":",x)
print("Total numbers of operations =",count)
print("Elements in ascending orders are:,\n",x)

3.WAP to print minimum number in a list.

x=[]
n=eval(input("enter size of a list:"))
for i in range(n):
x.append(eval(input("enter"+str(i)+"element:")))
print("numbers in a list are")
print(x)
min=x[0]
for i in range (1,n):
if (x[i]<min):
min=x[i]
print("minimum value in a list =",min)

4. Program to search for an element in given list of


numbers.

lst = eval(input("enter list :"))


length= len(lst)
element = int(input("enter element to be searched for :"))
for i in range(0, length-1) :
if element == lst[i] :
print(element, "found at index", i)
break
else :
print(element, "not found in given list")

5. Program to calculate mean of a given list of numbers.

lst = eval(input("enter list :"))


length= len(lst)
mean = sum = 0
for i in range(0, length-1) :
sum += lst[i]
mean = sum/length
print("given list is :", lst)
print("the mean of the given list is :", mean)
SUM OF SERIES PROGRAMS

1.WAP to calculate Sum of Series 1²+2²+3²+….+n².

number = int(input("Please Enter any Positive Number : "))


total = 0
total = (number * (number + 1) * (2 * number + 1)) / 6
for i in range(1, number + 1):
if(i != number):
print("%d^2 + " %i, end = ' ')
else:
print("{0}^2 = {1}".format(i, total))

2.Python Program to calculate Sum of Series 1²+2²+3²+….+n².

number = int(input("Please Enter any Positive Number : "))


total = 0
total = (number * (number + 1) * (2 * number + 1)) / 6
print("The Sum of Series upto {0} = {1}".format(number, total))
3.WAP to input the value of x and n and print the sum of
series.

term=1
x=int(input("enter value of x:"))
n=int(input("enter the power of (n):"))
s=x
sign =+1
for a in range(2,n+1):
f=1
for i in range(1,a+1):
f*=i
term + ((x**a)*sign)/f
s+=term
sign*=-1
print ("sum of first ",n,"terms:",s)
4.Write a Python program to find the sum of the series:- X -
X**2/2! + X**3/3! - X**4/4! + X**5/5! - X**6/6!.

x = int(input("enter the value for x:"))


j = int(input("enter The value for j:"))
Sum = 0
Fact = 1
Sign = 1
for i in range(1,x+1):
for i in range(1,j+1):
Fact = Fact*1
Sum += (Sign*(x**i)/Fact*j)
print(Sum)
GENERAL PROGRAMS

1.WAP to check wether a name is palindrome or not.

n=""
a=input("enter name")
l=len(a)-1
for i in range (l,-1,-1):
n+=a[i]
if a==n:
print("its a palendrome number",a)
else:
print("its not a palendrome number",a)
2.WAP to count upper and lower letters in a name.

u=l=0
a=input("enter name ")
for i in a:
if i.isupper():
u+=1
elif i.islower():
l+=1
print("upper",u)
print("lower",l)
3.WAP to calculate grade of student .

a=int(input("enter marks of english"))


b=int(input("enter marks of hindi"))
c=int(input("enter marks of maths"))
d=int(input("enter marks of accounts"))
e=int(input("enter marks of bussiness"))
A=(a+b+c+d+e)/500*100
if A>85:
print("A")
elif A<85 and A>=75:
print("B")
elif A<75 and A>=50:
print("C")
elif A<50 and A>=33:
print("D")
elif A<33:
print("FAIL")
STRING PROGRAMS

1.WAP that reads and then prints a string that capitalizes


every other letter in the string.

string = input("enter a string :")


length = len(string)
print("original string :", string)
string2 = ' '
for a in range(0, length, 2) :
string2 += string[a]
if a < (length-1) :
string2 += string[a+1].upper()
print("Alternatively capitalized string :", string2)
2.WAP to input a formula with some brackets and checks, and
prints out if the formula has the same number of opening and
closing parentheses.'

str = input("Enter a formula: ")


count = 0
for ch in str :
if ch == '(' :
count += 1
elif ch == ')' :
count -= 1
if count == 0 :
print("Formula has same number of opening and closing
parentheses")
else :
print("Formula has unequal number of opening and closing
parentheses")

3.WAP that inputs a line of text and prints out the count of
vowels in it.
str = input("Enter a string: ")
count = 0
for ch in str :
lch = ch.lower()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
count += 1
print("Vowel Count =", count)

5. WAP which replaces all vowels in the string with *.


str = input("Enter the string: ")
newStr = ""
for ch in str :
lch = ch.lower()
if lch == 'a' \
or lch == 'e' \
or lch == 'i' \
or lch == 'o' \
or lch == 'u' :
newStr += '*'
else :
newStr += ch
print(newStr)

DICTIONARY PROGRAMS

1.Program to update elements in a dictionary.

person = {
"name": "John",
"age": 28,
"city": "New York"
}
person["age"] = 30
print(person)

2.Program to add elements in a dictionary.

person = {"name": "BHARAT","age": 69,"city": "DUBAI"}

person["profession"] = "delievery boy"

print(person)

3.WAP to make basic calculator.

print("Basic Calculator")
num1 = float(input("Enter First Number: "))
num2 = float(input("Enter Second Number: "))
operator = input("Enter Operator: ")
if operator == '+':
print(num1 + num2)
elif operator == '-':
print(num1 - num2)
elif operator == '*':
print(num1 * num2)
elif operator == '/':
print(num1 / num2)
else:
print("Invalid Operator")

4.WAP to do addition in dictionary.

num_dict = {'num1': 5, 'num2': 7}

result = num_dict['num1'] + num_dict['num2']

print("The result is:", result)


5.WAP in dictionary to do multiplication .

num_dict = {'num1': 10, 'num2': 6}

result = num_dict['num1'] * num_dict['num2']

print("The result is:", result)

You might also like