You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: 100+ Python challenging programming exercises.txt
+255-8
Original file line number
Diff line number
Diff line change
@@ -766,6 +766,8 @@ def printValue(n):
766
766
printValue(3)
767
767
768
768
#----------------------------------------#
769
+
2.10
770
+
769
771
Question:
770
772
Define a function that can receive two integral numbers in string form and compute their sum and then print it in console.
771
773
@@ -781,6 +783,9 @@ printValue("3","4") #7
781
783
782
784
783
785
#----------------------------------------#
786
+
2.10
787
+
788
+
784
789
Question:
785
790
Define a function that can accept two strings as input and concatenate them and then print it in console.
786
791
@@ -795,6 +800,9 @@ def printValue(s1,s2):
795
800
printValue("3","4") #34
796
801
797
802
#----------------------------------------#
803
+
2.10
804
+
805
+
798
806
Question:
799
807
Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.
800
808
@@ -820,6 +828,8 @@ printValue("one","three")
820
828
821
829
822
830
#----------------------------------------#
831
+
2.10
832
+
823
833
Question:
824
834
Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".
825
835
@@ -839,22 +849,259 @@ checkValue(7)
839
849
840
850
841
851
#----------------------------------------#
852
+
2.10
853
+
842
854
Question:
843
-
Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".
855
+
Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.
844
856
845
857
Hints:
846
858
847
-
Use % operator to check if a number is even or odd.
859
+
Use dict[key]=value pattern to put entry into a dictionary.
860
+
Use ** operator to get power of a number.
848
861
849
862
Solution
850
-
def checkValue(n):
851
-
if n%2 == 0:
852
-
print "It is an even number"
853
-
else:
854
-
print "It is an odd number"
863
+
def printDict():
864
+
d=dict()
865
+
d[1]=1
866
+
d[2]=2**2
867
+
d[3]=3**2
868
+
print d
855
869
856
870
857
-
checkValue(7)
871
+
printDict()
872
+
873
+
874
+
875
+
876
+
877
+
#----------------------------------------#
878
+
2.10
879
+
880
+
Question:
881
+
Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.
882
+
883
+
Hints:
884
+
885
+
Use dict[key]=value pattern to put entry into a dictionary.
886
+
Use ** operator to get power of a number.
887
+
Use range() for loops.
888
+
889
+
Solution
890
+
def printDict():
891
+
d=dict()
892
+
for i in range(1,21):
893
+
d[i]=i**2
894
+
print d
895
+
896
+
897
+
printDict()
898
+
899
+
900
+
#----------------------------------------#
901
+
2.10
902
+
903
+
Question:
904
+
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.
905
+
906
+
Hints:
907
+
908
+
Use dict[key]=value pattern to put entry into a dictionary.
909
+
Use ** operator to get power of a number.
910
+
Use range() for loops.
911
+
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
912
+
913
+
Solution
914
+
def printDict():
915
+
d=dict()
916
+
for i in range(1,21):
917
+
d[i]=i**2
918
+
for (k,v) in d.items():
919
+
print v
920
+
921
+
922
+
printDict()
923
+
924
+
#----------------------------------------#
925
+
2.10
926
+
927
+
Question:
928
+
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.
929
+
930
+
Hints:
931
+
932
+
Use dict[key]=value pattern to put entry into a dictionary.
933
+
Use ** operator to get power of a number.
934
+
Use range() for loops.
935
+
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
936
+
937
+
Solution
938
+
def printDict():
939
+
d=dict()
940
+
for i in range(1,21):
941
+
d[i]=i**2
942
+
for k in d.keys():
943
+
print k
944
+
945
+
946
+
printDict()
947
+
948
+
949
+
#----------------------------------------#
950
+
2.10
951
+
952
+
Question:
953
+
Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).
954
+
955
+
Hints:
956
+
957
+
Use ** operator to get power of a number.
958
+
Use range() for loops.
959
+
Use list.append() to add values into a list.
960
+
961
+
Solution
962
+
def printList():
963
+
li=list()
964
+
for i in range(1,21):
965
+
li.append(i**2)
966
+
print li
967
+
968
+
969
+
printList()
970
+
971
+
#----------------------------------------#
972
+
2.10
973
+
974
+
Question:
975
+
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.
976
+
977
+
Hints:
978
+
979
+
Use ** operator to get power of a number.
980
+
Use range() for loops.
981
+
Use list.append() to add values into a list.
982
+
Use [n1:n2] to slice a list
983
+
984
+
Solution
985
+
def printList():
986
+
li=list()
987
+
for i in range(1,21):
988
+
li.append(i**2)
989
+
print li[:5]
990
+
991
+
992
+
printList()
993
+
994
+
995
+
#----------------------------------------#
996
+
2.10
997
+
998
+
Question:
999
+
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.
1000
+
1001
+
Hints:
1002
+
1003
+
Use ** operator to get power of a number.
1004
+
Use range() for loops.
1005
+
Use list.append() to add values into a list.
1006
+
Use [n1:n2] to slice a list
1007
+
1008
+
Solution
1009
+
def printList():
1010
+
li=list()
1011
+
for i in range(1,21):
1012
+
li.append(i**2)
1013
+
print li[-5:]
1014
+
1015
+
1016
+
printList()
1017
+
1018
+
1019
+
#----------------------------------------#
1020
+
2.10
1021
+
1022
+
Question:
1023
+
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.
1024
+
1025
+
Hints:
1026
+
1027
+
Use ** operator to get power of a number.
1028
+
Use range() for loops.
1029
+
Use list.append() to add values into a list.
1030
+
Use [n1:n2] to slice a list
1031
+
1032
+
Solution
1033
+
def printList():
1034
+
li=list()
1035
+
for i in range(1,21):
1036
+
li.append(i**2)
1037
+
print li[5:]
1038
+
1039
+
1040
+
printList()
1041
+
1042
+
1043
+
#----------------------------------------#
1044
+
2.10
1045
+
1046
+
Question:
1047
+
Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included).
1048
+
1049
+
Hints:
1050
+
1051
+
Use ** operator to get power of a number.
1052
+
Use range() for loops.
1053
+
Use list.append() to add values into a list.
1054
+
Use tuple() to get a tuple from a list.
1055
+
1056
+
Solution
1057
+
def printTuple():
1058
+
li=list()
1059
+
for i in range(1,21):
1060
+
li.append(i**2)
1061
+
print tuple(li)
1062
+
1063
+
printTuple()
1064
+
1065
+
1066
+
1067
+
#----------------------------------------#
1068
+
2.10
1069
+
1070
+
Question:
1071
+
With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line.
1072
+
1073
+
Hints:
1074
+
1075
+
Use [n1:n2] notation to get a slice from a tuple.
1076
+
1077
+
Solution
1078
+
tp=(1,2,3,4,5,6,7,8,9,10)
1079
+
tp1=tp[:5]
1080
+
tp2=tp[5:]
1081
+
print tp1
1082
+
print tp2
1083
+
1084
+
1085
+
#----------------------------------------#
1086
+
2.10
1087
+
1088
+
Question:
1089
+
Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10).
0 commit comments