0% found this document useful (0 votes)
49 views20 pages

Programs STD XI Flow of Control and String

Uploaded by

paarth2404
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)
49 views20 pages

Programs STD XI Flow of Control and String

Uploaded by

paarth2404
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/ 20

Bhavan’s B.P.

Vidya Mandir, Civil lines, Nagpur


Practical record /Program file

Name: Paarth Pandey


Class: XI Sec - B
Python Programs

Conditional & Iterative Statements


1. Program to print multiplication table of given number
CODE:
n=int(input("Enter a No. for the multiplication table : "))
t=int(input("Enter a No. of times to multiply the given no. : "))
for i in range (t):
y=i*n
print(y)
OUTPUT:
Enter a No. for the multiplication table : 5
Enter a No. of times to multiply the given no. : 11
0
5
10
15
20
25
30
35
40
45
50

2. Program to find factorial of a number


CODE:
n=int(input("Enter a No. for the Factorial Of that no. : "))
fact=1
a=1
while a<=n:
fact*=a
a+=1
print("Factorial of the given no. is : ",fact)
OUTPUT:
Enter a No. for the Factorial Of that no. : 3
Factorial of the given no. is : 6

3. Program to check the number is prime.


CODE:
num=int(input("Enter Number : "))
lim=(int(num/2))+1
for i in range (2,lim):
rem=num%i
if rem==0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
OUTPUT:
I. Enter Number : 5
5 is a prime number
II. Enter Number : 2
2 is a prime number
III. Enter Number : 12
12 is not a prime number
4. Program to find :
a. Sum of digits
CODE:
num=int(input("Enter Number : "))
ssum=0
nnum=num
while (nnum!=0):
digit=nnum%10;
nnum=nnum//10
ssum+=digit
print("Sum of digits of number",num,"is : ",ssum)
OUTPUT:
I. Enter Number : 123
sum of digits of number 123 is : 6
II. Enter Number : 123456
sum of digits of number 123456 is : 21

b. Check for Palindrome


CODE:
num=int(input("Enter Number : "))
rev=0
nnum=num
while (nnum !=0):
digit=nnum%10;
nnum=nnum//10
rev=rev*10+digit
if rev==num:
print(num,"is a palindrome number ")
else:
print(num,"is not a palindrome number ")
OUTPUT:
I.Enter Number : 121
121 is a palindrome number
II.Enter Number : 231
231 is not a palindrome number
c. Reverse of numbers
CODE:
num=input("Enter Number : ")
nnum=(num [::-1])
print("The reverse of ",num," is : ",nnum)
OUTPUT:
Enter Number : 123
The reverse of 123 is : 321

d. Check for Armstrong No.


CODE:
num=int(input("Enter Number : "))
ssum=0
temp=num
while (temp > 0):
digit=temp%10
ssum+=digit**3
temp //=10
if (num==ssum):
print(num,"is an Armstrong number ")
else:
print(num,"is not an Armstrong number ")
OUTPUT:
I.Enter Number : 123
123 is not an Armstrong number
II.Enter Number : 407
407 is an Armstrong number

e. Check for Perfect no.


CODE:
num=int(input("Enter Number : "))
ssum=0
for i in range (1,num):
if (num%i==0):
ssum=ssum+i
if (ssum == num):
print(num,"is a perfect number. ")
else:
print(num,"is not a perfect number. ")
OUTPUT:
A. Enter Number : 120
120 is not a perfect number.
B. Enter Number : 6
6 is a perfect number.

5. Program to find odd/even sum of 1 to n natural numbers. Pg:295


CODE:

OUTPUT:

6. Print first n terms of Fibonacci series.


CODE:
n=int(input("Enter till how many Fibonacci numbers are required : "))
n1=0
n2=1
print (n1)
print (n2)
for a in range (1,n-1):
n3=n1+n2
print(n3)
n1,n2=n2,n3
OUTPUT:
Enter till how many Fibonacci numbers are required : 6
0
1
1
2
3
5

7. Print first n terms of Mersene series.


CODE:
num=int(input("Enter till where you want to know the Mersenne Series : "))
print("First ten mersenne Numbers are : ")
for a in range (1,num+1):
number=2**a-1
print(number)
print()
OUTPUT:
Enter till where you want to know the Mersenne Series : 6
First ten mersenne Numbers are :
1
3
7
15
31
63

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:

9. Program to print the following pattern


1 * ABCDE
12 * * ABCD
123 * * * ABC
1234 * * AB
12345 * A
* AAAAA
* * BBBBB
* * * CCCCC
* * * DDDDD
1 * 12345

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:

n= input("Enter a string : ")


u=0
l=0
cch=0
s=0
for ch in (n):
if ch >='A' and ch<='Z':
u+=1
if ch >='a' and ch<='z':
l+=1
if ch >='0' and ch<='9':
cch+=1
print("No. of uppercase letters are : ",u)
print("No. of lowercase letters are : ",l)
print("No. of numbers letters are : ",cch)

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

Length of the given string is : 25

Enter a substring which you want to replace : how


Enter a substring which you want instead of the previous one : what
The replace of the given string is : Hello, what are you doing?

After the upper function used : HELLO, WHAT ARE YOU DOING?

After the lower function used : hello, what are you doing?

5. Program to input 2 string and perform the following operations


1) join then
2) print string1 5 times
3) Convert string2 to upper case
4) find the length of string1 and string2
CODE:
str1= input("Enter a string with multiple words : ")
str2= input("Enter another string with multiple words : ")
print("After Joining Both The Strings : ", " ".join([str1,str2]))
print()
for i in range (5):
print (str1)

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

After the upper function used : EVERYONE

Length of the given string 1 is : 5


Length of the given string 2 is : 8
Thank You ! Have A Good Day Ahead

6 . Menu based string program to


1)input string
2)Length
3)Reverse
4)Alternate characters of string
CODE:

OUTPUT:

LIST, TUPLES & Dictionary


1) Program to input numbers in a List and exchange first half with the second half of
the list. Eg 123456 Exchange : 456123
CODE:

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: enter list of marks: [1,2,3,4]


1
4

9) Create a Tuple to store


the squares of integers from 1 to 25 .
first 5 numbers of Fibbonaci Series.
first 5 numbers and find its mean.
CODE:

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)

OUTPUT: ========================= RESTART: C:/Users/User/f5.py


=========================
How many students? 3
Name of the student : a
Number of competitions won : 5
The dictionary now is :
Name of the student : b
Number of competitions won : 6
The dictionary now is :
Name of the student : c
Number of competitions won : 7
The dictionary now is :
{'a': 5, 'b': 6, 'c': 7}

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.

CODE: no_of_std = int(input("Enter number of students: "))


result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is/are",(result[student][0]))

OUTPUT: Enter number of students: 1


Enter Details of student No. 1
Roll No: 10
Student Name: a
Marks: 80
{10: ['a', 80]}
Student's name who get more than 75 marks is/are a

12) Create a dictionary containing details of workers(name, age, salary)& display in


record format. Update the salary by 2000.
CODE:

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")

print("The value corresponding to the key 9 is : ",ODD.get(9))


print(ODD.pop(9))
print(ODD)
OUTPUT:
The keys are : dict_keys([1, 3, 5, 7, 9])
The values are : dict_values(['one', 'three', 'five', 'seven', 'nine'])
The items are : dict_items([(1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')])
The length of the dictionary is 5
Yes Seven is present in the given dictionary
NO two is not present in the given dictionary
The value corresponding to the key 9 is : nine
nine
{1: 'one', 3: 'three', 5: 'five', 7: 'seven'}
14) Write a program using dictionary to convert number into corresponding number in
words. Eg : 123 OUTPUT: one two three
CODE:

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

-----------------------

You might also like