-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_70_Programs.py
More file actions
1132 lines (924 loc) · 25.7 KB
/
Copy pathPython_70_Programs.py
File metadata and controls
1132 lines (924 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#1.Display float number with 2 decimal places using print()
val = float(input("Enter any number: "))
format_float = "{:.2f}".format(val)
print(format_float)
Enter any number: 4.3
4.30
#2.Takes two integer numbers and return their product.
val1=2
val2=4
def prod(x,y):
return x*y
print(prod(val1,val2))
8
#3.Write a Python program to get the volume of a sphere with radius 10.
pi = 3.1415926535897931
r= 10
V= 4.0/3.0*pi* r**3
print('The volume of the sphere is: ',V)
The volume of the sphere is: 4188.790204786391
#4.Accept two numbers from the user and multiply them together.
val1=int(input("enter no. : "))
val2=int(input("enter no. : "))
def prod(x,y):
return x*y
print(prod(val1,val2))
enter no. : 3
enter no. : 3
9
#5.Write a Python program that accepts an integer (a) and computes the value of a+aa+aaa.
a=int(input("enter any integer no. :"))
print(a+(a*a)+(a*a*a))
enter any integer no. :2
14
#6.Write a Python program to calculate the length of a string
string="ram shyam"
print(len(string))
13
#7.Write a Python program to parse a string to Float & Integer
a = "1500"
b = int(a)
c = float(a)
print(type(b))
print(b)
print(type(c))
print(c)
<class 'int'>
1500
<class 'float'>
1500.0
#8.Given two integer numbers return their sum. If the sum is greater than 100, then return their product.
val1=4
val2=97
def prod(x,y):
return x*y
def sum(x,y):
return x+y
sum=sum(val1,val2)
if(sum>100):
print("product :",prod(val1,val2))
else:
print("sum :",sum)
sum :101
#9.Write a Python program to calculate the sum of three given numbers, if the values are not - equal then return four times of their sum
a=5
b=0
c=5
def sum(x,y,z):
return x+y+z
sum=sum(a,b,c)
if(a==b and a==c):
print("sum :",sum)
else:
print("sum :",sum*4)
sum : 100
#10.A shop will give discount of 50% if the cost of purchased quantity is more than 10000. Ask user for quantity suppose, one unit will cost 100. Judge and print total cost for user.
quantity=int(input("enter quantity of item :"))
price=quantity*100
if(price>10000):
print("total price is :",(price/100)*50)
else:
print("total price is :",price)
enter quantity of item :101
total price is : 5050.0
#11.To check whether a number is divisible by 8 and 12 or not.
a=int(input("enter any no. :"))
if(a%8==0 and a%12==0):
print("{0} number is divisible by 8 and 12".format(a))
else:
print("{0} number is not divisible by 8 and 12".format(a))
enter any no. :48
24 number is divisible by 8 and 12
#12.To check whether a given number is even or odd.
num = int(input("Enter a number to check even or odd:- "))
if (num % 2) == 0:
print("{0} is Even number".format(num))
else:
print("{0} is Odd number".format(num))
Enter a number to check even or odd:- 4
4 is Even number
#13.Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:
'''Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F'''
phy=int(input("enter physics marks :"))
chem=int(input("enter chemistry marks :"))
bio=int(input("enter biology marks :"))
math=int(input("enter marhematics marks :"))
comp=int(input("enter computer marks :"))
per = (phy + chem + bio + math + comp) / 5.0
print("Percentage = ", per)
if(per >= 90):
print("Grade A");
elif(per >= 80):
print("Grade B");
elif(per >= 70):
print("Grade C");
elif(per >= 60):
print("Grade D");
elif(per >= 40):
print("Grade E");
else:
print("Grade F")
enter physics marks :60
enter chemistry marks :30
enter biology marks :25
enter marhematics marks :54
enter computer marks :64
Percentage = 46.6
Grade E
#14.Take input of age of 3 people by user and determine oldest and youngest among them.
person1 = int(input("Enter age of person 1 : "))
person2 = int(input("Enter age of person 2 : "))
person3 = int(input("Enter age of person 3 : "))
# check oldest
if person1 > person2 and person1 > person3:
print("Person 1 is oldest")
elif person2 > person1 and person2 > person3:
print("Person 2 is oldest")
elif person3 > person1 and person3 > person2:
print("Person 3 is oldest")
# check youngest
if person1 < person2 and person1 < person3:
print("Person 1 is youngest")
elif person2 < person1 and person2 < person3:
print("Person 2 is youngest")
elif person3 < person1 and person3 < person2:
print("Person 3 is youngest")
Enter age of person 1 : 2
Enter age of person 2 : 50
Enter age of person 3 : 43
Person 2 is oldest
Person 1 is youngest
'''15.Write a program to input electricity unit charges and calculate total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill'''
unit = int(input("enter units of electricity : "))
if(unit<=50):
bill=unit*0.50
totalbill=bill+(bill/100)*20
print("electricity bill is ",totalbill)
elif(unit<=150):
bill=unit*0.75
totalbill=bill+(bill/100)*20
print("electricity bill is ",totalbill)
elif(unit<=250):
bill=unit*1.20
totalbill=bill+(bill/100)*20
print("electricity bill is ",totalbill)
elif(unit>250):
bill=unit*1.50
totalbill=bill+(bill/100)*20
print("electricity bill is ",totalbill)
enter units of electricity : 200
electricity bill is 288.0
# 16. A student will not be allowed to sit in exam if his/her attendence is less than 80%.
# Take following input from user
# Total Number of classes held
# Total Number of classes attended. And print percentage of class attended. Is student is allowed to sit in exam or not.
a=int(input("Number of classes held:"))
b=int(input("Number of classes attended:"))
percentage=b/a*100
if percentage>=80:
print("The student is allowed to sit in the exam hall")
else:
print("The student is not allowed to sit in the exam hall")
Number of classes held:10
Number of classes attended:9
The student is allowed to sit in the exam hall
#17. Calculate income tax for the given income by adhering to the below rules
# Taxable Income Rate (in %)
# First Rs.10,0000 0
# Next Rs. 10,0000 10
# The remaining 20
income=int(input("enter income :"))
if (income <= 100000):
tax = 0
elif (income <= 200000):
tax = (income*10)/100
else :
tax = (income *20)/100
print("you owe", tax, "Rupees in tax!")
enter income :500000
you owe 100000.0 Rupees in tax!
#18. Program to find digital sum of a given Number
# ex: n=123 Digital sum----->1+2+3=6
def getsum(n):
sum=0
num = str(n)
for i in num:
sum = sum + int(i)
return sum
n=int(input("Enter the number:"))
print(getsum(n))
Enter the number:123
6
#19. Program to find the digital product of a given number
# ex: n=43 Digital product ----->4*3=12
def getProduct(n):
product = 1
num = str(n)
for i in num:
product = product * int(i)
return product
n=int(input("Enter the number:"))
print(getProduct(n))
Enter the number:43
12
# 20.Find the sum of the series 3 +33 + 333 + 3333 + .. n terms
n=int(input("Enter the range of number:"))
sum=0
p=3
for i in range(1,n+1):
sum += p
p=(p*10)+3
print("The sum of the series = ",sum)
Enter the range of number:3
The sum of the series = 369
#21.Print the following pattern
# *
# * *
# * * *
# * * * *
# * * * * *
# * * * *
# * * *
# * *
# *
rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print("\r")
for i in range(rows, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print("\r")
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
#22. Program to reverse a given Number. ex: n=123 Reversed no is 321
n = 4562
rev = 0
while(n > 0):
a = n % 10
rev = rev * 10 + a
n = n // 10
print(rev)
2654
#23.Program to check whether a given number is a palindrome or not
def reverseDigits(num) :
rev_num = 0;
while (num > 0) :
rev_num = rev_num * 10 + num % 10
num = num // 10
return rev_num
def isPalindrome(n) :
rev_n = reverseDigits(n);
if (rev_n == n) :
return 1
else :
return 0
n = 121
if isPalindrome(n) == 1 :
print("Is", n, "a Palindrome number? ->", True)
else :
print("Is", n, "a Palindrome number? ->", False)
Is 121 a Palindrome number? -> True
#24.Program to check whether a given number is an Armstrong number or not.
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Enter a number: 407
407 is an Armstrong number
#25.Program to find factorial of a given number.
fact=1
n=5
while(n!=0):
fact=fact*n
n=n-1
print("fact",fact)
fact 120
#26)Program to find whether a given number is a strong number or not.
# e.g. n=145
# 1!+4!+5!==145
def factorial(d):
if(d==1 or d==0):
return 1
return d*factorial(d-1)
def isStrong(n):
num=n
sm=0
while(n>0):
digit=n%10
sm=sm+factorial(digit)
n=n//10
if(sm==num):
return True
else:
return False
print("Input a number")
a=int(input())
print(isStrong(a))
Input a number
145
True
#27. Program to find whether a given number is a unique number. For example, 20, 56, 9863, 145, etc. are the unique numbers
#while 33, 121, 900, 1010, etc. are not unique numbers
visited = [0,0,0,0,0,0,0,0,0,0];
num=20
while (num):
if visited[num % 10] == 1:
break;
visited[num % 10] = 1;
num = (int)(num / 10);
if num == 0:
print("no. is unique");
else:
print("no. is not unique")
no. is not unique
#28)Program to find whether a given number is a perfect number or not.
def perfect_number(n):
sum = 0
for x in range(1, n):
if n % x == 0:
sum += x
return sum == n
print(perfect_number(6))
True
#29)Program to find whether a given number is a prime number or not.
num = 11
if num > 1:
for i in range(2, int(num/2)+1):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
11 is a prime number
#30)Print downward Half-Pyramid Pattern with Star (asterisk)
#* * * * *
#* * * *
#* * *
#* *
#*
rows = 5
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print(" ")
* * * * *
* * * *
* * *
* *
*
# 31)Print following pattern:
# 1
# 22
# 333
# 4444
# 55555
# 666666
# 7777777
# 88888888
# 999999999
for i in range(10):
print(str(i) * i)
1
22
333
4444
55555
666666
7777777
88888888
999999999
# 32)Print following pattern:
# 1
# 12
# 123
# 1234
# 12345
# 123456
# 1234567
# 12345678
# 123456789'''
for i in range(1,9+1):
for j in range(1, i+1):
print(j, end="")
print()
1
12
123
1234
12345
123456
1234567
12345678
123456789
#33)Print following pattern:
#A
#BB
#CCC
#DDDD
#EEEEE
#FFFFFF
#GGGGGGG
#HHHHHHHH'''
for i in range(8):
print((chr(65+i)+"")*(i+1))
A
BB
CCC
DDDD
EEEEE
FFFFFF
GGGGGGG
HHHHHHHH
#34)Write a Python program to calculate the length of a given string.
str = "shruti"
print(len(str))
6
#35.Write a Python program to get a single string from two given strings, separated by a space and swap the first characters of each string.
#Sample String : 'abc', 'xyz'
#Expected Result : 'xbc ayz'
str1 = input("Please Enter First String : ")
str2 =input("Please Enter Second String : ")
x=str1[0:1]
str1=str1.replace(str1[0:1],str2[0:1])
str2=str2.replace(str2[0:1],x)
print("Your first string has become :- ",str1)
print("Your second string has become :- ",str2)
Please Enter First String : shruti
Please Enter Second String : tiwari
Your first string has become :- thruti
Your second string has become :- siwari
#36. Write a Python program to add 'polis' at the end of a given string (length should be at least 3).
# If the given string already ends with 'polis' then add 'polisCS' instead.
# If the string length of the given string is less than 3, leave it unchanged.
# Sample String : 'abc'
# Expected Result : 'abcpolisCS'
# Sample String : 'Acropolis'
# Expected Result : 'AcropolisCS'
def add_string(str1):
length = len(str1)
if length > 2:
if str1[-5:] == 'polis':
str1 += 'CSIT'
else:
str1 += 'polis'
return str1
print(add_string('ab'))
print(add_string('abc'))
print(add_string('Acropolis'))
ab
abcpolis
AcropolisCSIT
#37)Write a Python program to count the number of characters (character frequency) in a string.
# Sample String : google.com'
#Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
return dict
print(char_frequency('google.com'))
{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}
#38. Write a Python program to change a given string to a new string where the second and last chars have been exchanged.
def change_sring(str1):
return str1[0:1]+str1[-1:] + str1[2:-1] + str1[1:2]
print(change_sring('abcd'))
print(change_sring('12345'))
adcb
15342
#39)Write a Python program to remove the characters which have even index values of a given string.
def even_values_string(str):
result = ""
for i in range(len(str)):
if i % 2!= 0:
result = result + str[i]
return result
print(even_values_string('abcdef'))
print(even_values_string('python'))
bdf
yhn
#40 .Write a Python program that takes input from the user and displays that input back in upper and lower cases.
str1=input("enter string :")
print("in upper :" ,str1.upper())
print("in lower :" ,str1.lower())
enter string :SHyam
in upper : SHYAM
in lower : shyam
#41)Write a Python program to count occurrences of a substring in a string.
str1 = 'school collage school'
print()
print(str1.count("school"))
print()
2
#42)Write a Python program to lowercase first n characters in a string.
str1 = 'ACROPOLIS'
print(str1[:4].lower() + str1[4:])
acroPOLIS
#43)Write a Python program to remove spaces from a given string.
def remove_spaces(str1):
str1 = str1.replace(' ','')
return str1
print(remove_spaces("A c r o p o l i s"))
print(remove_spaces("S H Y A M "))
Acropolis
SHYAM
#44)Write a Python program to move spaces to the front of a given string.
def move_Spaces_front(str1):
noSpaces_char = [ch for ch in str1 if ch!=' ']
spaces_char = len(str1) - len(noSpaces_char)
result = ' '*spaces_char
result = '"'+result + ''.join(noSpaces_char)+'"'
return(result)
print(move_Spaces_front("Acropolis Collage "))
print(move_Spaces_front("CSIT Branch "))
" AcropolisCollage"
" CSITBranch"
#45)Write a Python program to find the maximum occurring character in a given string.
def get_max_occuring_char(str1):
ASCII_SIZE = 256
ctr = [0] * ASCII_SIZE
max = -1
ch = ''
for i in str1:
ctr[ord(i)]+=1;
for i in str1:
if max < ctr[ord(i)]:
max = ctr[ord(i)]
ch = i
return ch
print(get_max_occuring_char("Acropolis"))
print(get_max_occuring_char("Shruti"))
o
S
#46)Write a Python program to capitalize first and last letters of each word of a given string.
def capitalize_first_last_letters(str1):
str1 = result = str1.title()
result = ""
for word in str1.split():
result += word[:-1] + word[-1].upper() + " "
return result[:-1]
print(capitalize_first_last_letters("acropolis"))
print(capitalize_first_last_letters("Shyam"))
AcropoliS
Shryam
#47)Write a Python program to compute sum of digits of a given string.
def sum_digits_string(str1):
sum_digit = 0
for x in str1:
if x.isdigit() == True:
z = int(x)
sum_digit = sum_digit + z
return sum_digit
print(sum_digits_string("Acropolis596CI"))
print(sum_digits_string("CS35IT"))
20
8
#48)Write a Python function that takes a list of words and returns the length of the longest one.
def find_longest_word(words_list):
word_len = []
for n in words_list:
word_len.append((len(n), n))
word_len.sort()
return word_len[-1][0], word_len[-1][1]
result = find_longest_word(["CS", "ACROPOLIS", "CSIT"])
print("\nLongest word: ",result[1])
print("Length of the longest word: ",result[0])
Longest word: ACROPOLIS
Length of the longest word: 9
#49.Write a function x(n) for computing an element in the sequence xn=n^2+1. Call the function for n=4 and write out the result.
def x(n):
return(n**2 + 1)
print(x(4))
17
#50. Take the following Python code that stores a string: ‘str = 'Y-tatata-acropolis: 0.8475'. Use find and string slicing to extract the portion
#of the string after the colon character and then use the float function to convert the extracted string into a floating point number.
string = 'Y-tatata-acropolis: 0.8475'
col_pos = string.find(':') # Finds the colon character
number = string[col_pos + 1:] # Extracts portion after colon
confidence = float(number) # Converts to floating point number
print(confidence)
0.8475
#51. Write a function that returns the middle value among three integers. (Hint: make use of min() and max()). Write code to test this function with different inputs.
def middleOfThree(a, b, c) :
if a > b :
if (b > c):
return b
elif (a > c) :
return c
else :
return a
else:
if (a > c) :
return a
elif (b > c) :
return c
else :
return b
a = 20
b = 45
c = 40
print( middleOfThree(a, b, c) )
40
#52.Write a function that computes the area of a rectangle. Then, write a second function that calls this function three times to compute the surface area of a rectangular solid.
def secondfun():
for i in range(3):
print(area(i+1,i+3))
def area(h,w):
area=h*w
return area
height=2
width=3
print(area(height,width))
secondfun()
6
3
8
15
#53.Create an outer function that will accept three parameters, a, b and c. Create an inner function inside an outer function that will calculate the
#addition of a, b and c. At last, an outer function will add 5 into addition and return it
def num1(x,y,z):
def num2(a,b,c):
return a+b+c
outer=num2(x,y,z)+5
return outer
print(num1(5,4,5))
19
#54.Write a program to create a recursive function to calculate the product of numbers from 10 to 100.
def recur(n):
if n == 10:
return n
else:
return n*recur(n-1)
print("the product of numbers from 10 to 100 : " "is", recur(100))
the product of numbers from 10 to 100 : is 257182031095525112107857249934597388918419224714455526533820998388496472644482792132224051962512451185663850090463028434334174412800000000000000000000000
#55.Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number : "))
print(factorial(n))
Input a number : 5
120
#56.Write a Python function to display all the multiples of 7 & 9 within the range 100 to 500.
nl=[]
for x in range(100, 501):
if (x%7==0) and (x%9==0):
nl.append(str(x))
print (','.join(nl))
126,189,252,315,378,441
#57.Write a Python function to check whether the given integer is a prime number or not.
num = 27
flag = False
def prime(n):
flag1=False
if n > 1:
for i in range(2, n):
if (n % i) == 0:
flag1 = True
break
return flag1
flag=prime(num)
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
27 is not a prime number
#58. Write a Python function that checks whether a passed interger is armstrong or not.
n = int(input("Enter a number: "))
def armstrong(num):
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
return sum
summ=armstrong(n)
if n == summ:
print(n,"is an Armstrong number")
else:
print(n,"is not an Armstrong number")
Enter a number: 407
407 is an Armstrong number
#59. Program to return a function from another function.
def B():
print("Inside the method B.")
def A():
print("Inside the method A.")
return B
returned_function = A()
returned_function()
Inside the method A.
Inside the method B.
#60. First, def a function, start_process, that takes one argument p. Then, if the start_process function receives an p equal to "Yes",
# it should return "Start Process" Alternatively, elif p is equal to "No", then the function should return "Start Process Aborted".
# Finally, if start_process gets anything other than those inputs, the function should return "Sorry for the input".
def start_process(p):
if p=="Yes":
return "Start Process"
elif p=="No":
return "Start Process Aborted"
else:
return "Sorry for the input"
command= input("Enter a command: ")
print(start_process(command))
Enter a command: Yes
Start Process
#61. First, def a function called calculate_distance, with one argument (choose any argument name you like).
# If the type of the argument is either int or float, the function should return the absolute value of the function input.
# Otherwise, the function should return "No value". Check if it works calling the function with 9.6 and "what?".
def calculate_distance(argument):
if type(argument) == int or type(argument) == float:
return abs(argument)
else:
print("No value")
print("the absolute value : " ,calculate_distance(33.20))
calculate_distance("what?")
the absolute value : 33.2
No value
#62.Python program to display the sum of input (n) numbers using a list.
list1 = [11, 5, 17, 18, 23]
total=0
for i in range(0, len(list1)):
total = total + list1[i]
print("Sum of all elements in given list: ", total)
Sum of all elements in given list: 74
#63)Python program to insert a number to given position in a list.
print("Enter 10 Elements of List: ")
nums = []
for i in range(10):
nums.insert(i, input())
print("Enter an Element to Insert at End: ")
elem = input()
nums.append(elem)
print("\nThe New List is: ")
print(nums)
Enter 10 Elements of List:
2
3
4
5
6
1
8
6
9
5
Enter an Element to Insert at End:
4
The New List is:
['2', '3', '4', '5', '6', '1', '8', '6', '9', '5', '4']
#64. Write the program that prompts the user for a list of numbers and prints out the maximum and minimum of the
# numbers at the end when the user enters “done”. Write the program to store the numbers the user enters in a list and
# use the max() and min() functions to compute the maximum and minimum numbers after the loop completes.
my_list = []
while True:
number = 0.0
input_number = input('Enter a number: ')
if input_number == 'done':
break
try:
number = float(input_number)
except ValueError:
print('Invalid input')
quit()