0% found this document useful (0 votes)
2 views15 pages

Program Class Xi

The document outlines various programming tasks and exercises involving strings, lists, tuples, and dictionaries. It includes instructions for calculating series sums, identifying number types, manipulating lists, and performing dictionary operations. Each section provides sample code and expected outputs for clarity.
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)
2 views15 pages

Program Class Xi

The document outlines various programming tasks and exercises involving strings, lists, tuples, and dictionaries. It includes instructions for calculating series sums, identifying number types, manipulating lists, and performing dictionary operations. Each section provides sample code and expected outputs for clarity.
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

String Program

1. Write a program to input the value of x and n and print the sum of the following
series:
⮚ 1 + 𝑥 + 𝑥2 + 𝑥3 + 𝑥4 + ⋯ 𝑥n
⮚ 1 − 𝑥 + 𝑥2 − 𝑥3 + 𝑥4 − ⋯ 𝑥n
2. Determine whether a number is a perfect number, an Armstrong number or a
palindrome.
3. Input a number and check if the number is a prime or composite number.
4. Display the terms of a Fibonacci series.
5. Compute the greatest common divisor and least common multiple of two integers.
6. Count and display the number of vowels, consonants, uppercase, lowercase
characters in string.
7. Input a string and determine whether it is a palindrome or not; convert the case of
characters in a string.
8. Find the largest/smallest number in a list/tuple
9. Input a list of numbers and swap elements at the even location with the elements at
the odd location.
10. Input a list/tuple of elements, search for a given element in the list/tuple.
11. Create a dictionary with the roll number, name and marks of n students in a class
and display the names of students who have marks above 75.
12. Write a program to count the number of times a character (passed as argument) occurs in the
given string.
13. Write a program with string which replaces all vowels in the string with '*'.
14. Write a program to input a string from the user and print it in the reverse order without creating
a new string.
15. Write a program using to check if a string is a palindrome or not. (A string is called palindrome if
it reads same backwards as forward. For example, Kanak is a palindrome.)

List Programs

1. Write a menu driven program to perform various list operations, such as:
• Append an element
• Insert an element
• Append a list to the given list
• Modify an existing element
• Delete an existing element from its position
• Delete an existing element with a given value
• Sort the list in ascending order
• Sort the list in descending order
2. Display the list.
3. program to calculate average marks of n students where n is entered by the user.
4. Write a program to find the number of times an element occurs in the list and make a
unique list also.
5. Write a program to read a list of n integers (positive as well as negative). Create two new
lists, one having all positive numbers and the other having all negative numbers from the
given list. Print all three lists.
Tuples Programs
1. This is a program to create a nested tuple to store roll number, name and marks of students.
2. Write a program to swap two numbers without using a temporary variable.
3. Write a program to compute the area and circumference of a circle.
4. Write a program to input n numbers from the user. Store these numbers in a tuple. Print the
maximum and minimum number from this tuple.
5. Consider the following tuples, tuple1 and tuple2:
a. tuple1 = (23,1,45,67,45,9,55,45)
b. tuple2 = (100,200)
c. Find the output of the following statements:
i. print([Link](45))
ii. print([Link](45))
iii. print(tuple1 + tuple2)
iv. print(len(tuple2))
v. print(max(tuple1))
vi. print(min(tuple1))
vii. print(sum(tuple2))
viii. print(sorted(tuple1))
ix. print(tuple1)
6. Consider the following dictionary stateCapital:
i. stateCapital = {
a. "Andhra Pradesh":"Hyderabad",
b. "Bihar":"Patna",
c. "Maharashtra":"Mumbai",
d. "Rajasthan":"Jaipur"
e. }
ii. Find the output of the following statements:
i. print([Link]("Bihar"))
iii. print([Link]())
iv. print([Link]())
v. print([Link]())
vi. print(len(stateCapital))
vii. print("Maharashtra" in stateCapital)
viii. print([Link]("Assam"))
ix. del stateCapital["Rajasthan"]
x. print(stateCapital)

Dictionary Program

1. Create a dictionary ‘ODD’ of odd numbers between 1 and 10, where the key is the decimal
number and the value is the corresponding number in words. Perform the following
operations on this dictionary:
a. Display the keys
b. Display the values
c. Display the items
d. Find the length of the dictionary
e. Check if 7 is present or not
f. Check if 2 is present or not
g. Retrieve the value corresponding to the key 9
h. Delete the item from the dictionary corresponding to the key 9
2. Write a program to enter names of employees and their salaries as input and store them in a
dictionary
🧩 STRING PROGRAMS/Series

1. Series Sum (1 + x + x² + ... + xⁿ)


x = int(input("Enter value of x: "))
n = int(input("Enter value of n: "))
sum_series = 0
for i in range(n+1):
sum_series += x**i
print("Sum of series =", sum_series)

Output:

Enter value of x: 2
Enter value of n: 3
Sum of series = 15

2. Series Sum (1 - x + x² - x³ + ... + xⁿ)


x = int(input("Enter value of x: "))
n = int(input("Enter value of n: "))
sum_series = 0
sign = 1
for i in range(n+1):
sum_series += (x**i) * sign
sign *= -1
print("Sum of series =", sum_series)

Output:

Enter value of x: 2
Enter value of n: 3
Sum of series = -5

3. Perfect, Armstrong, and Palindrome Number


n = int(input("Enter number: "))

# Perfect number
sum_div = 0
for i in range(1, n):
if n % i == 0:
sum_div += i
if sum_div == n:
print("Perfect Number")

# Armstrong number
num = n
sum_arm = 0
while num > 0:
d = num % 10
sum_arm += d**3
num //= 10
if sum_arm == n:
print("Armstrong Number")
# Palindrome
if str(n) == str(n)[::-1]:
print("Palindrome Number")

Output:

Enter number: 153


Armstrong Number

4. Prime or Composite Number


n = int(input("Enter a number: "))
if n > 1:
for i in range(2, n):
if n % i == 0:
print("Composite Number")
break
else:
print("Prime Number")
else:
print("Neither prime nor composite")

Output:

Enter a number: 7
Prime Number

5. Fibonacci Series
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=' ')
a, b = b, a+b

Output:

Enter number of terms: 6


0 1 1 2 3 5

6. GCD and LCM


import math
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("GCD =", [Link](a, b))
print("LCM =", (a*b)//[Link](a, b))

Output:

Enter first number: 12


Enter second number: 18
GCD = 6
LCM = 36
7. Count Vowels, Consonants, Uppercase, Lowercase
s = input("Enter string: ")
v = c = u = l = 0
for ch in s:
if [Link]():
u += 1
if [Link]():
l += 1
if [Link]() in 'aeiou':
v += 1
elif [Link]():
c += 1
print("Vowels:", v, "Consonants:", c, "Uppercase:", u, "Lowercase:", l)

Output:

Enter string: HelloWorld


Vowels: 3 Consonants: 7 Uppercase: 2 Lowercase: 8

8. String Palindrome & Case Conversion


s = input("Enter string: ")
if [Link]() == s[::-1].lower():
print("Palindrome String")
print("Case Converted String:", [Link]())

Output:

Enter string: Kanak


Palindrome String
Case Converted String: kANAK

9. Largest and Smallest in List/Tuple


nums = eval(input("Enter list/tuple: "))
print("Largest:", max(nums))
print("Smallest:", min(nums))

Output:

Enter list: [3,5,9,2]


Largest: 9
Smallest: 2

10. Swap Even & Odd Location Elements


lst = eval(input("Enter list: "))
for i in range(0, len(lst)-1, 2):
lst[i], lst[i+1] = lst[i+1], lst[i]
print("Swapped List:", lst)
Output:

Enter list: [10,20,30,40]


Swapped List: [20,10,40,30]

11. Dictionary – Marks Above 75


n = int(input("Enter number of students: "))
students = {}
for i in range(n):
r = int(input("Roll no: "))
name = input("Name: ")
marks = int(input("Marks: "))
students[r] = [name, marks]
print("Students scoring above 75:")
for r, data in [Link]():
if data[1] > 75:
print(data[0])

Output:

Enter number of students: 3


Roll no: 1 Name: Ravi Marks: 90
Roll no: 2 Name: Aman Marks: 72
Roll no: 3 Name: Neha Marks: 88
Students scoring above 75:
Ravi
Neha

12. Count Character Occurrences


s = input("Enter string: ")
ch = input("Enter character: ")
print("Count =", [Link](ch))

Output:

Enter string: banana


Enter character: a
Count = 3

13. Replace All Vowels with ‘*’


s = input("Enter string: ")
for v in 'aeiouAEIOU':
s = [Link](v, '*')
print("Modified String:", s)

Output:

Enter string: Hello


Modified String: H*ll*
14. Reverse String (Without New String)
s = list(input("Enter string: "))
[Link]()
print("Reversed String:", ''.join(s))

Output:

Enter string: Python


Reversed String: nohtyP

15. Check Palindrome (String Method)


s = input("Enter string: ")
if [Link]() == s[::-1].lower():
print("Palindrome")
else:
print("Not Palindrome")

Output:

Enter string: level


Palindrome
🧩 LIST PROGRAMS

3. Menu Driven List Operations


lst = []
while True:
print(“\[Link] [Link] [Link] [Link](by position)”)
print(“[Link](by value) [Link] Asc [Link] Desc [Link] [Link]”)
ch = int(input(“Enter your choice: “))

if ch == 1:
ele = input(“Enter element to append: “)
[Link](ele)
elif ch == 2:
pos = int(input(“Enter position: “))
ele = input(“Enter element: “)
[Link](pos, ele)
elif ch == 3:
pos = int(input(“Enter position to modify: “))
new = input(“Enter new value: “)
lst[pos] = new
elif ch == 4:
pos = int(input(“Enter position to delete: “))
del lst[pos]
elif ch == 5:
ele = input(“Enter element to delete: “)
[Link](ele)
elif ch == 6:
[Link]()
elif ch == 7:
[Link](reverse=True)
elif ch == 8:
print(“List =”, lst)
elif ch == 9:
break
else:
print(“Invalid choice!”)

Output:

[Link] [Link] …
Enter your choice: 1
Enter element to append: 10
List = [10]

4. Display the List


lst = eval(input(“Enter a list: “))
print(“The List is:”, lst)

Output:

Enter a list: [1,2,3]


The List is: [1, 2, 3]
3. Average Marks of n Students
n = int(input("Enter number of students: "))
marks = []
for i in range(n):
m = float(input("Enter marks: "))
[Link](m)
avg = sum(marks) / n
print("Average marks =", avg)

Output:

Enter number of students: 3


Enter marks: 60
Enter marks: 70
Enter marks: 80
Average marks = 70.0

4. Frequency of Element and Unique List


lst = eval(input("Enter list: "))
ele = eval(input("Enter element to count: "))
print("Count of", ele, "=", [Link](ele))
unique = list(set(lst))
print("Unique List =", unique)

Output:

Enter list: [1,2,2,3]


Enter element to count: 2
Count of 2 = 2
Unique List = [1, 2, 3]

5. Separate Positive and Negative Numbers


lst = eval(input("Enter list of numbers: "))
pos = []
neg = []
for i in lst:
if i >= 0:
[Link](i)
else:
[Link](i)
print("All numbers:", lst)
print("Positive numbers:", pos)
print("Negative numbers:", neg)

Output:

Enter list: [1,-2,3,-4]


Positive numbers: [1, 3]
Negative numbers: [-2, -4]
🧩 TUPLE PROGRAMS

1. Nested Tuple for Student Data


n = int(input("Enter number of students: "))
students = ()
for i in range(n):
r = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = int(input("Enter marks: "))
students += ((r, name, marks),)
print("Student Tuple:", students)

Output:

Enter number of students: 2


(1, 'Ravi', 90), (2, 'Neha', 85)

2. Swap Two Numbers Without Temporary Variable


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping: a =", a, "b =", b)

Output:

Enter first number: 5


Enter second number: 10
After swapping: a = 10 b = 5

3. Area and Circumference of Circle


import math
r = float(input("Enter radius: "))
area = [Link] * r**2
circumference = 2 * [Link] * r
print("Area =", area)
print("Circumference =", circumference)

Output:

Enter radius: 7
Area = 153.938
Circumference = 43.982
4. Tuple Max and Min
nums = tuple(map(int, input("Enter numbers separated by space: ").split()))
print("Tuple =", nums)
print("Maximum =", max(nums))
print("Minimum =", min(nums))

Output:

Enter numbers: 3 7 2 9
Maximum = 9
Minimum = 2

5. Tuple Operations
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)

print([Link](45))
print([Link](45))
print(tuple1 + tuple2)
print(len(tuple2))
print(max(tuple1))
print(min(tuple1))
print(sum(tuple2))
print(sorted(tuple1))
print(tuple1)

Output:

1
3
(23,1,45,67,45,9,55,45,100,200)
2
67
1
300
[1,9,23,45,45,45,55,67]
(23,1,45,67,45,9,55,45)
🧩 DICTIONARY PROGRAMS

1. Create Dictionary ‘ODD’ and Perform Operations


ODD = {1: "One", 3: "Three", 5: "Five", 7: "Seven", 9: "Nine"}

print("Keys:", [Link]())
print("Values:", [Link]())
print("Items:", [Link]())
print("Length:", len(ODD))
print("Is 7 present?", 7 in ODD)
print("Is 2 present?", 2 in ODD)
print("Value for key 9:", [Link](9))
del ODD[9]
print("After deleting key 9:", ODD)

Output:

Keys: dict_keys([1,3,5,7,9])
Values: dict_values(['One','Three','Five','Seven','Nine'])
Items:
dict_items([(1,'One'),(3,'Three'),(5,'Five'),(7,'Seven'),(9,'Nine')])
Length: 5
Is 7 present? True
Is 2 present? False
Value for key 9: Nine
After deleting key 9: {1:'One',3:'Three',5:'Five',7:'Seven'}

2. Employee Name and Salary Dictionary


n = int(input("Enter number of employees: "))
employee = {}
for i in range(n):
name = input("Enter employee name: ")
salary = float(input("Enter salary: "))
employee[name] = salary

print("Employee Details:")
for k, v in [Link]():
print(k, ":", v)

Output:

Enter number of employees: 2


Ravi 35000
Neha 40000
Employee Details:
Ravi : 35000
Neha : 40000
🧩 ADDITIONAL PROGRAMS

1. Check if Dictionary d1 is Contained in Dictionary d2


d1 = eval(input("Enter first dictionary (d1): "))
d2 = eval(input("Enter second dictionary (d2): "))

for key in d1:


if key not in d2 or d1[key] != d2[key]:
print("d1 is NOT contained in d2")
break
else:
print("d1 is contained in d2")

Output:

Enter first dictionary (d1): {'a':1, 'b':2}


Enter second dictionary (d2): {'a':1, 'b':2, 'c':3}
d1 is contained in d2

2. Find All Prime Numbers Between 2 and 20000 (Using for–else loop)
print("Prime numbers between 2 and 20000 are:")

for num in range(2, 20001):


for i in range(2, int(num**0.5) + 1):
if num % i == 0:
break
else:
print(num, end=' ')

Output:

Prime numbers between 2 and 20000 are:


2 3 5 7 11 13 17 19 ... 19997 19999
🧩 FOR–ELSE and WHILE–ELSE LOOP PROGRAMS

Understanding the Concept

In Python, both for and while loops can have an optional else block.

• The else part executes only when the loop completes normally —
i.e., it does not end by a break statement.
• If a break is used and the loop stops early, the else part is skipped.

🔹 1. FOR–ELSE LOOP EXAMPLE

Program: Check if a Number is Prime


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

for i in range(2, int(num**0.5) + 1):


if num % i == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")

Explanation:

• The loop checks every divisor of num.


• If any divisor divides it evenly, break executes → else skipped.
• If no divisor is found → else executes → number is prime.

Output:

Enter a number: 13
13 is a prime number

Output (for non-prime):

Enter a number: 12
12 is not a prime number

“else runs only if loop is not broken.”


🧩 2. While–Else Program (With break)
i = 1
while i <= 5:
if i == 3:
print("Breaking the loop at", i)
break
print("Number:", i)
i += 1
else:
print("Loop completed successfully")

Output:
Number: 1
Number: 2
Breaking the loop at 3

Explanation:

• When i becomes 3, break stops the loop immediately.


• Because the loop ended with break,
the else part does not execute.

You might also like