-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 1
More file actions
1196 lines (1040 loc) · 38.8 KB
/
Assignment 1
File metadata and controls
1196 lines (1040 loc) · 38.8 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
Interactive Programming Through Python
Assignment-1
QUESTION: 5
Open the Python IDLE and execute the following commands. Observe the output.
1)10 + 15
2)print("Hello World")
3)45 - 34
4)8 * 2
5)print("Rahul's age is", 45)
6)print("I have", 10, "mangoes and", 12, "bananas")
Solution:5
1) 25
2) Hello World
3) 11
4) 16
5) Rahul's age is 45
6) I have 10 mangoes and 12 bananas
QUESTION: 6
Open Python IDLE and execute the following commands. Observe the output.
1.emp_number = 1233
2.print("Employee Number:", emp_number)
3.emp_salary = 16745.50
4.emp_name = "Jerry Squaris"
5.print("Employee Salary and Name:", emp_salary, emp_name)
6.emp_salary = 23450.34
7.print("Updated Employee Salary:", emp_salary)
Solution:6
2. 1233
4.Employee Salary and Name: 16745.5 Jerry Squaris
6.Updated Employee Salary: 23450.34
QUESTION: 7
Execute the following Python statements in IDLE and observe the output:
•customer_id = 101
•type(customer_id)
•customer_name = "John"
•type(customer_name)
•bill_amount = 675.45
•type(bill_amount )
•x = 5.3 + 0.9j
•type(x)
•print(customer_id, customer_name, bill_amount)
•print(x.real)
•print(x.imag + 3)
•Flag = True
•type(Flag)
•y = "False"
Solution:7
1. <class 'int'>
2. <class 'str'>
3. <class 'float'>
4. <class 'complex'>
5. 101 John 675.45
6. 5.3
7. 3.9
8. <class 'bool'>
QUESTION: 8
In a retail application, shopkeeper wants to keep a track of following details of a customer. Sample values are provided.
•bill_id = 101
•customer_id = 1001
•customer_name = "Rahul"
•if_minor = False
•bill_amount = 2000.50
Write a python program to store the details and display them
Solution:8
bill_id: 101
customer_id: 1001
customer_name: Rahul
if_minor: False
bill_amount: 2000.5
QUESTION: 9
Execute the following commands and observe the usage of different types of commenting styles.
i = 10
# creates an integer variable. This is a single line comment.
print("i =", i)
# prints 10
'''
Below code creates a Boolean variable in Python
(This is a multiple line comment)
'''
s = True
print("s =", s)
#prints True, Here, s is a Boolean variable with value True
"""
Below code assigns string data to variable 's'. Data type of variable can change during execution,
Hence, Python supports Dynamic Semantics.
(This is multi-line comment used for documentation)
"""
s = 24
print("s =", s)
#prints 24, Here, s is changed to integer data type with value 24
QUESTION: 10
Write a Python program for the following requirements:
•Prompt the user to input two numbers num1 and num2
•Increment num1 by 4 and num2 by 6
•Find and print the sum of new values of num1 and num2
Hint – Use type casting for converting the input into an integer.
Solution:10
num1 = int(input("enter value of num1:"))
num2 = int(input("enter value of num1:"))
num1 += 4
num2 += 6
sum = (num1) + (num2)
print("The sum of num1 and num2 is:",sum)
QUESTION: 11
1) Consider two variables 'a' and 'b' in Python such that a = 4 and b = 5. Swap the values of 'a' and 'b' without using a temporary variable. Print the values of 'a' and 'b' before and after swapping.
2)Consider the scenario of processing marks of a student in ABC Training Institute. John, the student of fifth grade takes exams in three different subjects. Create three variables to store the marks obtained by John in three subjects. Find and display the average marks scored by John.
Now change the marks in one of the subjects and observe the output. Did the value of average change?
3) Given the value of radius of a circle, write a Python program to calculate the area and perimeter of the circle. Display both the values.
4) The finance department of a company wants to compute the monthly pay of its employees. Monthly pay shouldbe calculated as mentioned in the formula below. Display all the employee details.
Monthly Pay = Number of hours worked in a week * Pay rate per hour * No. of weeks in a month
•The number of hours worked by the employee in a week should be considered as 40
•Pay rate per hour should be considered as Rs.400
•Number of weeks in a month should be considered as 4
Write a Python program to implement the above real world problem.
Solution(11.1)
a = 4
b = 5
print('The value of a before swapping:',a)
print('The value of b before swapping:',b)
a,b = b,a
print('The value of a after swapping:',a)
print('The value of b after swapping:',b)
2)
sub1=int(input("Enter marks of the first subject 1: "))
sub2=int(input("Enter marks of the second subject 2: "))
sub3=int(input("Enter marks of the third subject 3: "))
avg = (sub1+sub2+sub3)/3
print("the average is:",avg)
sub2=int(input("Enter new marks of sub2: "))
avg = (sub1+sub2+sub3)/3
print("the new average is:",avg)
3)
pi = 3.14
radius = float(input(' Enter the radius of a circle: '))
area = pi * radius * radius
perimeter = 2 * pi * radius
print(" Area Of a Circle:",area)
print(" perimeter Of a Circle:",perimeter)
4)
number_of_hours = 40
Pay_rate = 400
Number_of_weeks = 4
Monthly_Pay = number_of_hours * Pay_rate * Number_of_weeks
print("number of hours:",number_of_hours)
print("payrate:",Pay_rate)
print("no. of weeks:",Number_of_weeks)
print("monthly pay:",Monthly_Pay)
QUESTION: 12Identify the sections of the given program where the coding standards are not followed and correct them.
itemNo=1005
unitprice = 250
quantity = 2
amount=quantity*unitprice
print("Item No:", itemNo)
print("Bill Amount:", amount)
Solution:12
QUESTION: 13
Create a new file in Python IDLE as "triangle.py"
•Write a Python program to calculate and print the area of the triangle. Prompt the user to input the values for base and height of the triangle.
•Execute the program(use 'Run Module' under 'Run' tab) and observe the output.
•Close the file, open it again and execute it once more with different values. Observe the output.
Hint – Use type casting for converting the input into an integer.
Area of a triangle = ½ * base * height
Solution:13
base = int(input("the base is : "))
height = int(input("the height is : "))
Area = 1/2 * base * height
print("the area is:",Area)
QUESTION: 14
1)Consider the scenario of retail store management again. The store provides discount for all bill amounts based on the criteria below:
Write a Python program to find the net bill amount after discount. Observe the output with different values of bill amount. Assume that bill amount will be always greater than zero.
2) Extend the above program to validate the customer id. Customer ids in the range of 101 and 1000 (both inclusive) should only be considered valid.
Note: Display appropriate error messages wherever applicable
Solution:14
bill_id =111
customer_id=35
bill_amount=400.0
discounted_bill_amount=0.0
print("Bill Id:",bill_id)
print("Customer Id:",customer_id)
print("Bill Amount:",bill_amount)
if bill_amount>500:
discounted_bill_amount=bill_amount - (bill_amount*2/100)
else:
discounted_bill_amount=bill_amount-(bill_amount*1/100)
print("Discounted Bill Amount:",discounted_bill_amount)
2)
bill_id =111
customer_id=35
bill_amount=400.0
discounted_bill_amount=0.0
print("Bill Id:",bill_id)
print("Customer Id:",customer_id)
print("Bill Amount:",bill_amount)
if ((customer_id > 100) and (customer_id <=1000)) is True:
if bill_amount >= 500:
discounted_bill_amount = bill_amount-bill_amount *10/100
print("Discounted Bill Amount:",iscounted_bill_amount)
else:
print("No Discount")
else:
print("Invalid Customer id,customer id must between 101 and 1000")
QUESTION: 15
Implement the following in Python:
a. Display all even numbers between 50 and 80 (both inclusive) using "for" loop.
b.Add natural numbers up to n where n is taken as an input from user. Print the sum.
c.Prompt the user to enter a number. Print whether the number is prime or not.
d. Print Fibonacci series till nth term where n is taken as an input from user.
Solution:a
x=0
for x in range(50,81):
if x%2==0:
print(x)
Solution:b.
x=int(input('Enter the value for the range:'))
sum=0
while x>0:
sum=sum+x
x-=1
print('The sum of natural numbers is:',sum)
Solution:c
n=int(input('enter a number'))
if n>1:
for i in range(2,n):
if(n%i)==0:
print(n,"not a Prime number")
break
else:
print(n,"prime number")
else:
print(n,"not a prime number")
Solution:d
#Print Fibonacci series till nth term where n is taken as an input from user.
n=int(input("Enter the value of n:"))
n1 = 0
n2 = 1
count = 0
if n<= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":",n1)
else:
print("Fibonacci sequence upto",n,":")
while count < n:
print(n1,end=' , ')
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
QUESTION: 16
Create four string variables a, b, c, d to store the following values and display them:
•My city is Mexico
•Raghu is my friend's brother
•My favorite programming language is "Python"
•Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. It's design philosophy emphasizes code readability, and it's syntax allows programmers to express concepts in fewer
lines of code than possible in languages such as "C++" or "Java".
Solution:16
a = "My city is mexico"
b = "Raghu is my friend's brother"
c = "My favorite programming language is 'Python' "
d = "Python is a widely used high-level, general-purpose, interpreted,dynamic programming language.\
Its design philosophy emphasizes code readability \
it syntax allows programmers to express concepts in fewer lines of code than possible in languages\
such as 'C++' or 'Java'"
print(a)
print(b)
print(c)
print(d)
QUESTION: 17
•Accept a string as an input from the user. Check if the accepted string is palindrome or not.
•If the string is palindrome, print "String is palindrome", otherwise print "String is not palindrome".
•Also print the actual and the reversed strings.
Note – Ignore the case of characters.
Hint – A palindrome string remains the same if the characters of the string are reversed.
Solution:17
string = input("enter a string:")
temp=string
reverse= string[::-1]
if temp==reverse:
print("string is a palindrome")
else:
print("string is not a palindrome")
print("Actual string is:",temp)
print("Reverse string is:",reverse)
QUESTION: 18
Accept two strings 'string1' and 'string2' as an input from the user. Generate a resultant string, such that it is a concatenated string of all upper case alphabets from both the strings in the order they appear. Print the actual and the resultant strings.
Note: Each character should be checked if it is a upper case alphabet and then it should be concatenated to the resultant string.
Sample Input:
string1: I Like C
string2: Mary Likes Python
Output: ILCMLP
Solution:18
string1 = input("Enter a string: ")
string2 = input("Enter another string: ")
result = ""
for i in string1:
if (i.isupper()):
result = result + i
for i in string2:
if (i.isupper()):
result = result + i
print("String1: ",string1)
print("String2: ",string2)
print(result)
QUESTION: 19
Given a string containing both upper and lower case alphabets. Write a Python program to count the number of occurrences of each alphabet (case insensitive) and display the same.
Sample Input: ABaBCbGc
Sample Output:
2A
3B
2C
1G
Solution:19
x=input("enter string")
x=x.upper()
t={}
count=0
t=set(x)
for i in t:
for j in s:
if(i==j):
count=count+1
print(count,i)
count=0
QUESTION: 20
Write a Python program to accept a string 'accepted_string'. Generate a resultant string
'resultant_string' such that 'resultant_string' should contain all characters at the even position
of 'accepted_string'(ignoring blank spaces).
Display 'resultant_string' in reverse order.
accepted_string: An apple a day keeps the doctor away
resultant_string: Aapedyepteotrwy
expected_output: ywrtoetpeydepaA
Hint: String starts from index 0, hence the starting character is at even position.
Solution:20
str1="An apple a day keeps the doctor away"
str2=str1.replace(" ","")
a=len(str2)
print(str2)
str3=str2[0:a:2]
print(str3)
str4=str3[::-1]
print(str4)
QUESTION: 22
The "Variety Retail Store" sells different varieties of Furniture to the customers. The list of furniture available with its respective cost is given below:
The furniture and its corresponding cost should be stored as a list. A customer can order any furniture in any quantity (the name and quantity of the furniture will be provided). If the required furniture is available in the furniture list(given above) and quantity to be purchased is greater than zero, then bill amount should be calculated.
In case of invalid values for furniture required by the customer and quantity to be purchased, display appropriate error message and consider bill amount to be 0. Initialize required furniture and quantity with different values and test the results.
Write a Python program to calculate and display the bill amount to be paid by the customer based on the furniture bought and quantity purchased.
Hint – Create two different lists for 'Furniture' and 'Cost'. Indices of two lists should be matched to retrieve the cost of a particular furniture.
Solution:22
furniture=['Sofa_set','Dining_table','T.V.Stand','Cupboard']
cost=[20000,8500,4599,13920]
item=input()
if item in furniture:
quantity=int(input("enter quantity"))
if quantity>0:
cost_index=furniture.index(item)
cost_item=cost[cost_index]
total_cost=cost_item*quantity
print(total_cost)
else:
print("Invalid purchase")
QUESTION: 23
Consider the list of courses opted by a Student "John" and available electives at ABC Training Institute:
courses = ("Python Programming", "RDBMS", "Web Technology", "Software Engg.")
electives = ("Business Intelligence", "Big Data Analytics")
Write a Python Program to satisfy business requirements mentioned below:
1.List the number of courses opted by John.
2.List all the courses opted by John.
3.John is also interested in elective courses mentioned above. Print the updated tuple including electives.
Solution:23
t = ()
courses = ("Python Programming", "RDBMS", "Web Technology", "Software Engg.")
electives = ("Business Intelligence", "Big Data Analytics")
print("enter 1 for Python Programing\nenter 2 for RDBMS\nenter 3 for Web Technology\nenter 4 Software Engg. ")
count=0
while True:
count=count+1
course=input("enter the course :")
if course == '1':
t += ('Python Programming',)
elif course=='2':
t += ('RDBMS',)
elif course=='3':
t += ('Web Technology',)
elif course=='4':
t += ('Soft. Eng.',)
y=input("do you want to add more course :")
if not(y.lower()=="yes"):
break
x=input("are you interested in electives :")
if x.lower()=="yes":
total_course=count+1
elective=input("select 1 for Business Intillengence\nselect 2 for Big Data Analytics")
if elective=='1':
t+=('Business Intillengence',)
if elective=='2':
t+=('Big Data Analytics',)
total_course=count+1
print("no of course opt by john is :",total_course)
print("the courses are opted by jhon are :",t)
QUESTION: 24
Given below is a dictionary 'customer_details' representing customer details from a Retail Application. Customer Id
is the key and Customer Name is the value.
customer_details = { 1001 : "John", 1004 : "Jill", 1005: "Joe", 1003 : "Jack" }
Write Python code to perform the operations mentioned below:
a)Print details of customers.
b)Print number of customers.
c)Print customer names in ascending order.
d)Delete the details of customer with customer id = 1005 and print updated dictionary.
e)Update the name of customer with customer id = 1003 to "Mary" and print updated dictionary.
f)Check whether details of customer with customer id = 1002 exists in the dictionary.
Solution:24
customer_details = { 1001 : "John", 1004 : "Jill", 1005: "Joe", 1003 : "Jack" }
for k,v in customer_details.items():
print("details of",v,"is",k)
print(len(customer_details.values()))
for i in sorted(customer_details.values()):
print(i)
del customer_details[1005]
print(customer_details)
customer_details[1003]='Marry'
print(customer_details)
QUESTION: 25
Consider a scenario from ABC Training Institute. The given table shows the marks scored by students of grade XI in Python Programming course.
Write a Python program to meet the requirements mentioned below:
a. Display the name and marks for every student.
b. Display the top two scorers for the course.
c. Display class average of this course.
Hint- Implement the solution using a dictionary.
Solution:25
details={"john":86.5,"jack":91.2,"jill":84.5,"harry":72.1,"joe":80.5}
for k,v in details.items():
print(k,"has marks",v)
top1=max(details.values())
print(top1)
for x in details.values():
if top1 not in details.values():
if max<x:
max=x
print("top2 of the class is",max)
avg=(sum(details.values()))/len(details.values())
print(avg)
QUESTION: 26
Consider the scenario from "Variety Retail Store" discussed in 'List' section. The list of furniture available with its respective cost is given below:
A customer can order any furniture in any quantity. If the required furniture is available in the furniture list(given above) and quantity to be purchased is greater than zero, then bill amount should be calculated. In case of invalid values for furniture required by the customer and quantity to be purchased, display appropriate error message and consider bill amount to be 0. Initialize required furniture and quantity with different values and test the results.
Calculate and display the bill amount to be paid by the customer based on the furniture bought and quantity purchased. Implement the given scenario using:
1)List of tuples
2)Dictionary
Solution:26
furniture=['Sofa set','Dining Table','T.V Stand','Cupboard']
cost=[20000,8500,4599,13920]
furname=input("Enter name of furniture you want to buy\t")
quantity=int(input("Enter quantity\t"))
if(furname in furniture and quantity>0):
index1=furniture.index(furname)
amt=cost[index1]
price= quantity*amt
print("Bill amount to be paid is Rs.",price)
else:
if(quantity==0 or quantity<0):
print(" Bill amount to be paid is 0")
if(furname not in furniture):
print("Invalid demand of furniture,this item is not in stock,Enter a valid item and try again")
print("Bill amount to be paid is zero")
QUESTION: 27
Consider a scenario from ABC Training Institute. Given below are two Sets representing the names of students enrolled for a particular course:
java_course = {"John", "Jack", "Jill", "Joe"}
python_course = {"Jake", "John", "Eric", "Jill"}
Write a Python program to list the number of students enrolled for:
1)Python course
2)Java course only
3)Python course only
4)Both Java and Python courses
5)Either Java or Python courses but not both
6)Either Java or Python courses
Solution:27
java_course = {"John", "Jack", "Jill", "Joe"}
python_course = {"Jake", "John", "Eric", "Jill"}
print("Students enroll in python course",python_course)
print("Students enroll in java course only",java_course.difference(python_course))
print("Students enroll in python course only",python_course.difference(java_course))
print("Enrolled inn both python and java",java_course.intersection(python_course))
print("Enrolled in either java or python but not both",java_course.symmetric_difference(python_course))
print("Enrolled in either java or python courses",java_course.union(python_course))
QUESTION: 28
Using functions, re-write and execute Python program to:
1.Add natural numbers upto n where n is taken as an input from user.
2.Print Fibonacci series till nth term (Take input from user).
Note: You have implemented these programs using loops earlier.
Solution:1)
n=int(input("enter value of n"))
i=0
sum=0
while i<=n:
sum=sum+i
i=i+1
print("sum of n natural numbers is",sum)
Solution:2)
n=int(input("Enter the value of n:"))
def Fibonacci(n):
n1 = 0
n2 = 1
count = 0
if n<= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":",n1)
else:
print("Fibonacci sequence upto",n,":")
while count < n:
print(n1,end=' ,')
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
print(Fibonacci(n))
QUESTION: 29
At an airport, a traveler is allowed entry into the flight only if he clears the following checks:
1.Baggage Check
2.Immigration Check
3.Security Check
The logic for the check methods are given below:
check_baggage (baggage_weight)
•.returns True if baggage_weightis greater than or equal to 0 and less than or equal to 40. Otherwise returns False.
check_immigration (expiry_year)
•.returns True if expiry_yearis greater than or equal to 2001 and less than or equal to 2025. Otherwise returns False.
check_security(noc_status)
•.returns True if noc_statusis 'valid' or 'VALID', for all other values return False.
traveler()
•.Initialize the traveler Id and traveler name and invoke the functions check_baggage(), check_immigration() and check_security() by passing required arguments.
•.Refer the table below for values of arguments.
•.If all values of check_baggage(), check_immigration() and check_security() are true, display traveler_id and traveler_name
display "Allow Traveler to fly!" Otherwise,display traveler_id and traveler_name
display "Detain Traveler for Re-checking!“
Invoke the traveler() function. Modify the values of different variables in traveler() function and observe the output.
Solution:29
def traveller():
traveler_name="Sahil Gupta"
traveler_id=500678
print("Traveler name is",traveler_name)
print("Traveler id is",traveler_id)
def check_baggage(baggage_weight):
a="true"
b="false"
if(baggage_weight>=0 and baggage_weight<=40):
return a
else:
return b
def check_immigration (expiry_year):
a="true"
b="false"
if(expiry_year>=2001 and expiry_year<=2025):
return a
else:
return b
def check_security(noc_status):
a="true"
b="false"
if(noc_status== 'valid' or noc_status=='VALID'):
return a
else:
return b
def traveller():
traveler_name="Jim"
traveler_id=1001
x=check_baggage(35)
y=check_immigration(2019)
z=check_security("valid")
if(x=="true" and y=="true" and z=="true"):
print("Traveler name is",traveler_name)
print("Traveler id is",traveler_id)
print("Allow traveller to Fly !!!")
else:
print("Traveler name is",traveler_name)
print("Traveler id is",traveler_id)
print("Detain Traveler for Rechecking")
traveller()
QUESTION: 30
Consider the pseudo code for generating Fibonacci series using Recursion:
FIBO (number)
1. if (number = 0) then
2. return (0)
3. else if (number = 1) then
4. return (1)
5. else
6. return FIBO(number - 1) + FIBO(number - 2)
7. end if
Write a program in Python to implement the same using Recursion and execute it in Eclipse. Print appropriate error message if the user enters negative number as input.
Solution:30
def FIBO (number):
if(number<0):
return (print("number is not valid"))
if (number==0):
return (0)
elif (number==1):
return (1)
else:
return FIBO(number - 1) + FIBO(number - 2)
print(FIBO(int(input("enter number"))))
QUESTION: 31
Write a Python program to implement the following (Use Recursion):
1.Print first 'n' multiples of 3, where 'n' is taken as an input from the user. The multiples should be printed from first to last.
2.Reverse a string. Print the original and reversed string.
3.Check if the given string is palindrome. If yes, print "String is palindrome" otherwise print "String is not palindrome".
Solution:1)
n=int(input("enter value of n:"))
def mult(n):
if n == 1:
return 3
else:
return mult(n-1) + 3
for i in range(1,n+1):
print(mult(i))
Solution:2
string = input("enter a string:")
def reverse(string):
if len(string) == 0:
return
temp = string[0]
reverse(string[1:])
print(temp, end='')
print("Original string is:",string)
reverse(string)
Solution:3
string=input("enter any string:")
def isPalRec(string,first,last) :
if (first==last):
return True
if (string[first] != string[last]) :
return False
if (first < last+ 1) :
return isPalRec(string, first + 1, last - 1);
return True
def isPalindrome(string) :
n = len(string)
if (n == 0) :
return True
return isPalRec(string, 0, n - 1);
if (isPalindrome(string)) :
print ("String is Palindrome")
else :
print ("String is not palindrome")
QUESTION: 32
Write a Python program to:
1.read a file.
2.add backslash (\) before every double quote in the file contents.
3.write it to another file in the same folder.
4.print the contents of both the files.
For example:
If the first file is 'TestFile1.txt' with text as:
Jack said, "Hello Pune".
The output of the file 'TestFile2.txt' should be:
Jack said,\"Hello Pune\".
Solution:32
f=open("TestFile1.txt","w")
f.write(input(""))
f.close()
with open("TestFile1.txt","r")as f1:
data=f1.read()
with open("TestFile2.txt","w")as f2:
for l in f1:
f2.write(l)
print(f2.write(data.replace('\"', '\\\"')))
f2.close()
f3=open("TestFile2.txt","r")
print(f3.read())
f3.close()
QUESTION: 33
Consider a file 'courses.txt' in D Drive with the following details:
Write a program to read the file and store the courses in Python variables as a:
1)Dictionary ( Sample - {0: 'Java', 1: 'Python', 2:'Javascript' 3: 'PHP'} )
2)List ( Sample - ['Java', 'Python', 'Javascript', 'PHP'] )
Solution:1)
f=open("F:\python\courses.txt","w")
a={0:'Java',1:'Python',2:'Javascript',3:'PHP'}
f.write(str(a))
b=['Java','Python','Javascript','PHP']
f.write(str(b))
f.close()
f1=open("F:\python\courses.txt","r")
print(f1.read())
f1.close()
Solution:2)
f=open("F:\python\courses.txt","w")
b=['Java','Python','Javascript','PHP']
f.write(str(b))
f.close()
f1=open("F:\python\courses.txt","r")
print(f1.read())
f1.close()
QUESTION: 34
Consider a file 'student_details.txt' in D Drive with the details of students in ABC institute – student id and name:
Write a program to read the file and store the student records in Python variable as:
1)List of lists
2)List of dictionaries
Solution:1)
f=open("F:\python\student_details.txt","w")
b=[["ABC",1, "Sarika"], ["ABC",2, "Mahi"], ["ABC",3, "Raavi"], ["ABC",4, "Ravi"]]
f.write(str(b))
f.close()
f1=open("F:\python\student_details.txt","r")
print(f1.read())
f1.close()
Solution:2)
f=open("F:\python\student_details.txt","w")
a=[{1:"Sarika"}, {2:"Mahi"}, {3:"Raavi"}, {4:"Ravi"}]
f.write(str(a))
f.close()
f1=open("F:\python\student_details.txt","r")
print(f1.read())
f1.close()
QUESTION: 35
Consider a file 'rhyme.txt' in D Drive with following text:
Write a Python program to count the words in the file using a dictionary (use space as a delimiter). Find unique words and the count of their occurrences(ignoring case). Write the output in another file "words.txt" at the same location.
Solution:35
f=open("rhyme.txt","w")
f.write(input(""))
f.close()
f1=open("rhyme.txt","r")
print(f1.read())
f1.close()
with open("rhyme.txt","r") as f:
words=f.read()
wordfreq={}
for word in words.replace(',', ' ').split():
wordfreq[word] = wordfreq.setdefault(word, 0) + 1
print(wordfreq)
st = set(open('rhyme.txt').read().split())
print(st)
print(len(st))
f2=open("words.txt","w")
f2.write(str(st))
f2.close()
QUESTION: 37
You have already created a Python program to implement the following in file handling section:
1.read a file.
2.add backslash (\) before every double quote in the file contents.
3.write it to another file in the same folder.
4.print the contents of both the files.
Modify your code to implement Exception handling. Print appropriate error messages wherever applicable.
Solution:37
try:
with open("TestFile1.txt","r")as f1:
data=f1.read()
with open("TestFile2.txt","w")as f2:
for l in f1:
f2.write(l)
except IOError:
print ("Error: can\'t find file or read data")
print(f2.write(data.replace('\"', '\\\"')))
else:
print("Written content in the file successfully")
f2.close()
f3 =open("TestFile2.txt","r")
print(f3.read())
f3.close()
QUESTION: 39
Refer to the following Question which you have already executed in Functions section. Modify your code to implement Exception Handling and display appropriate error message wherever applicable.
At an airport, a traveler is allowed entry into the flight only if he clears the following checks:
1.Baggage Check
2.Immigration Check
3.Security Check
The logic for the check methods are given below:
check_baggage (baggage_weight)
•returns True if baggage_weightis greater than or equal to 0 and less than or equal to 40. Otherwise returns False.
check_immigration (expiry_year)
•returns True if expiry_yearis greater than or equal to 2001 and less than or equal to 2025. Otherwise returns False.
check_security(noc_status)
•returns True if noc_statusis 'valid' or 'VALID', for all other values return False.
traveler()
•Initialize the traveler Id and traveler name and invoke the functions check_baggage(), check_immigration() and check_security() by passing required arguments.
•Refer the table below for values of arguments.
•If all values of check_baggage(), check_immigration() and check_security() are true, display traveler_id and traveler_name
display "Allow Traveler to fly!"
Otherwise,
display traveler_id and traveler_name
display "Detain Traveler for Re-checking!“
Invoke the traveler() function. Modify the values of different variables in traveler() function and observe the output
Solution:39
def traveller():
traveler_name=input("Enter traveler name ")
traveler_id=int(input("Enter traveler id "))
print("Traveler name is",traveler_name)
print("Traveler id is",traveler_id)
def check_baggage(baggage_weight):
try :
a="true"
b="false"
if(baggage_weight>=0 and baggage_weight<=40):
return a
else:
return b
except ValueError:
print("Value Error")
else:
print("Over weight , pay extra charge")
def check_immigration (expiry_year):
a="true"
b="false"
if(expiry_year>=2001 and expiry_year<=2025):
return a
else:
return b
def check_security(noc_status):
a="true"
b="false"
if(noc_status== 'valid' or noc_status=='VALID'):
return a
else:
return b
def traveller():
traveler_name="Jim"
traveler_id=1001
x=check_baggage(50)
y=check_immigration(2019)
z=check_security("valid")
if(x=="true" and y=="true" and z=="true"):
print("Traveler name is",traveler_name)
print("Traveler id is",traveler_id)
print("Allow traveller to Fly !!!")
else:
print("Traveler name is",traveler_name)
print("Traveler id is",traveler_id)
print("Detain Traveler for Rechecking")
traveller()
QUESTION: 41
•Write a Python program to randomly print any of the below numbers:
100,200,300,400,500,600,700,800,900,1000
Execute the program 10 times and verify if the number generated in every output is one out of the numbers given in the list above.
•Write a Python program to print a random odd numbers between 10 and 50.
Solution:41
1.
import random
l=[100,200,300,400,500,600,700,800,900,1000]
for i in range(10):
a=(random.choice([100,200,300,400,500,600,700,800,900,1000]))
if(a in l):
print(a)
print("Number is from the given list")
2.
import random
random_number =random.randrange(11,50,2)
print(random_number )
QUESTION: 42
Write a Python program for rolling a dice on clicking enter key. The program should run infinitely until user enters 'q'.
Solution:
import random
while(1):
a=input("Enter any string")
if(a=='q'):
break
else:
print(random.randint(1,7))
QUESTION: 43
If area of one wall of a cubical wooden box is 16 units, write a Python program to display the volume of the box.
Note:
Area of a cube with side 'a' is 'a**2'.
Volume of the cube can be computed as 'a**3'.
Solution:
a=16**(1/2)
volume=a**3
print("Volume of cube is",volume)
QUESTION: 44
The ABC Institute offers vocational courses to students in multiple areas e.g. theatre, classical singing, traditional dance forms, Bollywood dance, literature and so on. A student can enroll for zero to all courses.
Write a Python function that takes the number of courses as an input and returns the total number of different course combinations, a student can opt for. (Make use of functions available in math module)
Solution: