Professional Documents
Culture Documents
Practical Record Note Ex-7-15
Practical Record Note Ex-7-15
7
Aim:
To write a program to print the Fibonacci series upto ‘n’ terms and also print the sum of
Fibonacci series.
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Get the upper-limit.
STEP 3: Assign res, x as 0 and y as 1.
STEP 4: Using the loop construct from 1 to upper limit-1 add x and y to generate the new
number, print the new number and add with the res to find the sum of the series.
STEP 5: Print the sum of the series.
STEP 6: Stop
Expected input:
Enter the upper limit - 20
Expected output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
10944
Code:
# PROGRAM 7
up_limit = int(input("Enter the count of Fibonacci no : "))
res=x = 0
y = 1
print(x,y,end=" ")
RESULT:
Thus, we have created a program to print the Fibonacci series and its sum.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
Expected input:
8128
Expected output:
Perfect number
9
10
Not palindrome
Code:
# PROGRAM 8
num = int(input("Enter any Number: "))
sum1 = 0
choice = 1
cnt = len(str(num))
eve = odd = 0
while choice != 4:
choice = int(input("Select your choice : \n 1. Find perfect number \
\n 2. Sum of odd and even position numbers \
\n 3. Number is a palindrome \n 4. Quit \
\n Enter your choice: "))
if choice == 1:
for i in range(1, num):
if(num % i == 0):
sum1 = sum1 + i
if (sum1 == num):
print(num, " is a Perfect Number")
else:
print(num, " is not a Perfect Number")
elif choice == 2:
Number = num
while(Number > 0):
Reminder = Number % 10
if (cnt % 2 == 0):
eve = eve + Reminder
else:
odd = odd + Reminder
Number = Number //10
cnt = cnt-1
print("\n Sum of the even position digits of Given Number is " , eve)
print("\n Sum of the odd position digits of Given Number is " , odd)
elif choice == 3:
rNumber = int(str(num)[::-1])
if rNumber == num:
print ("Given number's reverse is ",rNumber , ". So,it is a
palindrome")
else:
print ("Given number's reverse is ",rNumber , ". So, it is not a
palindrome")
RESULT:
Thus, we have created a menu driven program to find perfect number, sum of odd & even
digits and Palindrome or not.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
RESULT:
Thus, we have created a Python program to find the second largest number of the given
list.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
for i in range(0,lstlen):
if lst[i] in lstdict.keys():
lstdict[lst[i]] = lstdict.get(lst[i])+1
else:
lstdict.setdefault(lst[i],1)
Expected input:
[23,45,36,33,83,90]
Expected output:
139
Code:
# PROGRAM 11
lst = eval(input ("Enter a list : "))
sumofno = 0
for no in lst:
if str(no)[-1]=='3':
sumofno += no
if sumofno > 0:
print ("Sum of numbers ending with 3 in the list",sumofno)
else:
print ("No number ending with 3")
RESULT: Thus, we have created a Python program to display the sum of all numbers
which are ending with 3 from a list.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
Expected input:
[-2,1,-3,-15,16,-17,5,-3,-6]
Expected output:
[1, 16, 5, -6, -3, -17, -15, -3, -2]
Code:
# PROGRAM 12
list1= eval (input("Enter a list of numbers : "))
lenl2=len(list1)
list2=[0] * lenl2
lenl2= lenl2-1
j=0
for i in range(lenl2+1):
if list1[i]>0:
list2[j]=list1[i]
j+=1
else:
list2[lenl2]=list1[i]
lenl2-=1
print(list2)
RESULT: Thus, we have created a Python program to shift negative numbers to right and
positive numbers to left.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
Expected input:
BELIEVE you CAN.
Expected output:
EVEILEB uoy .NAC
5
1
Code:
# PROGRAM 13
line_str = input("Enter a line of Text : ")
up_char=s_char=0
words = line_str.split()
RESULT: Thus, we have created a Python program to count and display the upper case
letters, special characters and reverse of each word.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
Enter a line of Text : BELIEVE you CAN. You're done Half-way there.
Reversal of each word is EVEILEB uoy .NAC er'uoY enod yaw-flaH .ereht
Number of Upper case letters in the given string is 5
Number of Special characters in the given string is 4
REPLACE CHARACTERS 14
Aim:
To write a Python program to accept a line of string and a character and find the
occurrence of the given characters, if the character is in uppercase, double it otherwise
replace it with ‘#’ and swap first letter and last letter of each word present in the string.
Also, print each word along with length of each word, number of replacements occurred.
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Read a string and a character and split the words and store as list.
STEP 3: Using the looping construct find the occurrence of the character.
STEP 4: Using the looping construct extract each word, if the character is in uppercase,
double it otherwise replace it with ‘#’ and swap first letter and last letter of the word. Also
calculate the replacements
STEP 5: Print the number of word, length of the word and number of replacement.
STEP 6: Stop
Expected input:
Black bull in Bubble
b
Expected output:
kBlacB 2 6
lul# 2 4
ni 1 2
eBu##lB 4 7
Code:
# PROGRAM 14
for word in words:
replace_char=0
new_word=''
for char in word:
if char.lower() == find_char.lower():
fcount_char+=1
if char.isupper() and char.lower() == find_char.lower():
new_word=new_word + char*2
replace_char+=1
elif char.islower() and char.lower() == find_char.lower():
new_word=new_word + '#'
replace_char+=1
else:
new_word=new_word + char
lst_new_words.append([new_word,replace_char,len(new_word)])
cnt=0
for word_lst in lst_new_words:
word = word_lst[0]
swap_word = word[-1]+word[1:-1]+word[0]
lst_new_words[cnt][0]=swap_word
lst_new_words[cnt][1] = lst_new_words[cnt][1] + 1 # As swaping is a
replacement
cnt +=1
print ("New Word No of Replacement Length of string")
for details in lst_new_words:
print (details[0],'\t\t' , details[1],'\t\t',details[2])
RESULT: Thus, we have created a Python program to replace the text and display the
same.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Read n the number of employees.
STEP 3: Using the looping construct get the salary of employees and store in a list.
STEP 4: Using the max and min function find the minimum and maximum salary.
STEP 5: Print the maximum and minimum salary.
STEP 6: Stop
Expected input:
10
12400,12000,23000,45090,9100,4500,64000,19000,17000,13500
Expected output:
45090
4500
Code:
# PROGRAM 15
no_emp=int(input("Enter the number of Employees : "))
emp_lst = []
for i in range(no_emp):
sal = float(input("Enter the salary : "))
emp_lst.append(sal)
print ("The maximum salary in the employee list is " , max( emp_lst))
print ("The minimum salary in the employee list is " , min( emp_lst))
RESULT: Thus, we have created a Python program to display the maximum and
minimum value in a list
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)