Programs STD XI Flow of Control and String
Programs STD XI Flow of Control and String
OUTPUT:
8. Sum of series :
a. 1+x+x2+x3-----------xn
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n "))
SUM=0
for i in range(n+1):
sum += x**i
print("The sum of the series is:", sum)
b. 1+1/1!+2/2! ----------
CODE:
OUTPUT:
212 * * 1234
123
32123 *
12
4321234
1
ANS 9 :
I. CODE:
for a in range (7):
for j in range (1,a,+1):
print(j,end=" ")
else:
print()
II. CODE:
n=3
for i in range(0, n):
for j in range(0, n-i-1):
print(" ",end="")
for j in range(0, i+1):
print("* ",end="")
print("")
for i in range(0, n-1):
for j in range(0, i+1):
print(" ",end="")
for j in range(0, n-i-1):
print("* ",end="")
print("")
III. CODE:
n=5
for i in range (n):
p=65
for j in range (n-i):
print( chr(p) ,end=" ")
p+=1
print()
IV. CODE:
n=4
for i in range(0, n):
for j in range(0, i+1):
print("* ",end="")
print("")
V. CODE:
for i in range (65,69):
for j in range(65,69):
print(chr(i),end="")
print()
VI. CODE:
VII. CODE:
VIII. CODE:
IX. CODE:
n=5
for i in range (n):
p=1
for j in range (n-i):
print( p ,end=" ")
p+=1
print()
String Programs:
1. Program to input a string and print its length, 1st and last character
CODE:
n=input("Enter a string : ")
print(n)
print("Length of the given string is : ",len(n))
print("1st Character is : ",n[0])
print("Last Character is : ",n[-1])
OUTPUT:
Enter a string : Hello
Hello
Length of the given string is : 5
1st Character is : H
Last Character is : o
2. Program to input a line of text & count the number of uppercase, lowercase,
numbers, other characters. Pg 351
CODE:
OUTPUT:
Enter a string : Hello AAKASH!120
No. of uppercase letters are : 7
No. of lowercase letters are : 4
No. of numbers letters are : 3
3. Program to input a string substring and find the number of occurrence of the
given substring. Pg 351
CODE:
str1= input("Enter a string : ")
sub=input("Enter a substring to find : ")
print(str1.count(sub))
OUTPUT:
Enter a string : Hello AAKASH!
Enter a substring to find : A
3
4. Program to input a string with multiple words & perform any 5 string
functions/operations (capitalize the first letter of each word., find, replace etc)
CODE:
str1= input("Enter a string with multiple words : ")
sub=input("Enter a substring to find : ")
print("The no of times " , sub , " has come is : ",str1.count(sub))
print()
print("Length of the given string is : ",len(str1))
print()
sub1=input("Enter a substring which you want to replace : ")
sub2=input("Enter a substring which you want instead of the previous one : ")
sub3=str1.replace(sub1,sub2)
print("The replace of the given string is : ",sub3)
print()
print("After the upper function used : ",sub3.upper())
print()
print("After the lower function used : ",sub3.lower())
OUTPUT:
Enter a string with multiple words : Hello, how are you doing?
Enter a substring to find : o
The no of times o has come is : 4
After the upper function used : HELLO, WHAT ARE YOU DOING?
After the lower function used : hello, what are you doing?
print()
print("After the upper function used : ",str2.upper())
print()
print("Length of the given string 1 is : ",len(str1))
print("Length of the given string 2 is : ",len(str2))
print("Thank You ! Have A Good Day Ahead ")
OUTPUT:
Enter a string with multiple words : hello
Enter another string with multiple words : everyone
After Joining Both The Strings : hello everyone
hello
hello
hello
hello
hello
OUTPUT:
OUTPUT:
2) Program to input numbers in a List and find the minimum and maximum no of the
list.
CODE:
l1=input("Input any number : ")
l2=list(l1)
print("The MIN number is : ",min(l2))
print("The MAX number is : ",max(l2))
OUTPUT:
Input any number : 982
The MIN number is : 2
The MAX number is : 9
3) Program to input numbers in a List and search for the given element.
CODE:
l1=input("Input any number : ")
l2=list(l1)
l3=input("The number to search for : ")
if l3 in l2:
print("The number ", l3 ,"is IN the given list of numbers.")
elif l3 not in l2:
print("The number ", l3 ,"is NOT the given list of numbers.")
OUTPUT:
Input any number : 982
The number to search for : 0
The number 0 is NOT the given list of numbers.
4) Program to input numbers in a List & find the frequency of the given element in the
List
CODE:
l1=input("Input any number : ")
l2=list(l1)
print("The list is : ",l2)
l3=input("The number to search for : ")
print("The frequency is : ",l2.count(l3))
OUTPUT:
Input any number : 1221458912
The list is : ['1', '2', '2', '1', '4', '5', '8', '9', '1', '2']
The number to search for : 2
The frequency is : 3
5) Program to input numbers in a List & find the biggest element is present in 1st half
or second half.
CODE:
lst=eval(input("Enter a List : "))
ln=len(lst)
mx=max(lst)
ind=lst.index(mx)
if ind<=(ln/2):
print("The Maximum Element ",mx," lies in the FIRST half")
else:
print("The Maximum Element ",mx," lies in the SECOND half")
OUTPUT:
Enter a List : [10,20,30,50,89,997,120]
The Maximum Element 997 lies in the SECOND half
6) Program update all elements not DIVISIBLE by 5 to ‘0’ in a List.
CODE:
OUTPUT:
7)Input a list of numbers and swap elements at the even location with the elements at
the odd location
CODE:
# Entering 5 element Lsit
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
# printing original list
print("The original list : " + str(mylist))
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(mylist)):
if i % 2:
even_i.append(mylist[i])
else :
odd_i.append(mylist[i])
result = odd_i + even_i
print("Separated odd and even index list: " + str(result))
OUTPUT:
Enter 5 elements for the list:
1
2
3
4
52
The original list : [1, 2, 3, 4, 52]
Separated odd and even index list: [1, 3, 52, 2, 4]
8)Program to input marks of n students in a list and display the highest and lowest
marks
CODE: marks=eval(input('enter list of marks: '))
x=min(marks)
y=max(marks)
print(x)
print(y)
OUTPUT:
10) Program to create dictionary containing names of competition winners as keys and
number of their wins as values.
CODE: n=int(input("How many students? "))
winners ={ }
for a in range(n):
key=input("Name of the student : ")
value=int(input ("Number of competitions won : "))
winners [key]=value
print ("The dictionary now is : ")
print (winners)
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.
OUTPUT:
13) 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:
Display the keys
Display the values
Display the items
Find the length of the dictionary
Check if 7 is present or not
Check if 2 is present or not
Retrieve the value corresponding to the key 9
Delete the item from the dictionary corresponding to the key 9
CODE:
ODD={1:"one",3:"three",5:"five",7:"seven",9:"nine"}
print("The keys are : ",ODD.keys())
print("The values are : ",ODD.values())
print("The items are : ",ODD.items())
print("The length of the dictionary is ",len(ODD))
if ODD.get(7)=="seven":
print("Yes Seven is present in the given dictionary")
else:
print("NO seven is not present in the given dictionary")
if ODD.get(2)=="two":
print("Yes two is present in the given dictionary")
else:
print("NO two is not present in the given dictionary")
OUTPUT:
Solution
ans='y'
while ans=='y':
num=input("enter any number:")
Nname={0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'}
result=' '
for ch in num:
key=int(ch)
value=Nname[key]
result=result+ ' '+value
print("Number name is :",result)
ans=input("Wish to continue y/n :")
Output
-----------------------