Calculate the sum and average of n integer numbers



Python QuestionsCalculate the sum and average of n integer numbersInput?some?integers?to?calculate?their?sum?and?average.?Input?0?to?exit.121050Average?of?the?above?numbers?are:?9.0Sum?of?the?above?numbers?are:?27.0Input?some?integers?to?calculate?their?sum?and?average.?Input?0?to?exit.34560Average?of?the?above?numbers?are:?4.5Sum?of?the?above?numbers?are:?18.0Ans:print("Input?some?integers?to?calculate?their?sum?and?average.?Input?0?to?exit.")count?=?0sum?=?0.0number?=?1?#?dummy?value,?will?get?replaced?in?future#?give?a?number?and?press?enter,?and?give?number?as?zero?to?exit?and?get?the?outputwhile?number?!=?0:????number?=?int(input(""))????sum?=?sum?+?number????count?+=?1if?count?==?0:????print("Input?some?numbers,?run?again.")else:????print("Average?of?the?above?numbers?are:",?sum/(count-1))????print("Sum?of?the?above?numbers?are:",?sum)Program to get Fizz, Buzz and FizzBuzzExplanation:A?a?Python?program?which?iterates?the?integers?from?1?to?50.?For?multiples?of?three?print?"Fizz"?instead?of?the?number?and?for?the?multiples?of?five?print?"Buzz".?For?numbers?which?are?multiples?of?both?three?and?five?print?"FizzBuzz".divisible?by?3?=?fizzdivisible?by?5?=?buzzdivisible?by?3?and?5?=?fizzbuzzOutput:Enter?a?number2012fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz1617fizz19buzzAns:num?=?int(input("Enter?a?number"))for?fizzbuzz?in?range(1,num+1):????if?fizzbuzz?%?3?==?0?and?fizzbuzz?%?5?==?0:????????print("fizzbuzz")????????continue????elif?fizzbuzz?%?3?==?0:????????print("fizz")????????continue????elif?fizzbuzz?%?5?==?0:????????print("buzz")????????continue????print(fizzbuzz)Get the largest & Smallest number from a listINPUT:[1,?2,?-8,?0]OUTPUT:Maximum?number?in?the?given?list?=?2Minimum?number?in?the?given?list?=?-8INPUT:[1,22,-999,999,2,545,7,456,8,3]OUTPUTMaximum?number?in?the?given?list?=?999Minimum?number?in?the?given?list?=?-999Ans:def?max_num_in_list(?mylist?):????max?=?mylist[?0?]????for?a?in?mylist:????????if?a?>?max:????????????max?=?a????return?max??def?min_num_in_list(?mylist?):????max?=?mylist[?0?]????for?a?in?mylist:????????if?a?<?max:????????????max?=?a????return?max??print("Maximum?number?in?the?given?list?=",?max_num_in_list([1,?2,?-8,?0]))print("Minimum?number?in?the?given?list?=",?min_num_in_list([1,?2,?-8,?0]))Takes two lists and returns True if they have at least one common member or False if they have no Common MemberAns:def?common_data(list1,?list2):??result?=?False??for?x?in?list1:????for?y?in?list2:??????if?x?==?y:????????result?=?True????????return?result??return?resultprint(common_data([1,2,3,4,5],?[5,6,7,8,9]))print(common_data([1,2,3,4,5],?[6,7,8,9]))Remove duplicates from a listINPUT:[1,1,1,1,2,2,2,2,3,3,3,3]OUTPUT:[1,?2,?3]INPUT:[10,20,30,20,10,50,60,40,80,50,40]OUTPUT:[40,?10,?80,?50,?20,?60,?30]Ans:a?=?[10,20,30,20,10,50,60,40,80,50,40]newlist?=?list(set(a))print(newlist)Hint : converting set to list removes the duplicate values.Generate and print a dictionary that contains a number in the form {x: x*x}Input:Input?a?number?=?6Output:{1:?1,?2:?4,?3:?9,?4:?16,?5:?25,?6:?36}Input:Input?a?number?=?2Output:{1:?1,?2:?4}Ans:n?=?int(input("Input?a?number?=?"))d?=?dict()???#?creates?a?empty?dictionary,?because?a?=?{}?will?create?a?empty?setfor?x?in?range(1,n+1):????d[x]=x*xprint(d)?Fibonacci?series?using?functioninput:?10output:?0?1?1?2?3?5?8?13?21?34input:?5output:?0?1?1?2?3Ans:def?fibonacci(n):??a?=?0??b?=?1??print(a,?end='?')??print(b,?end='?')??for?i?in?range(2,n):????c?=?a?+?b????a?=?b????b?=?c????print(c,?end='?')fibonacci(10)Sum?of?all?integers/float?in?a?list?using?functioninput:?[1,2,3,4,5]output:?15input:?[2,2,2,2,5,5]output:?18Ans:def?sum_list(mylist):??total?=?0??for?i?in?mylist:????total?+=?i??return?totalx?=?[2,2,2,2,5,5]print(sum_list(x))Product?of?all?integers/float?in?a?list?using?functioninput:?[1,2,3,4,5]output:?120input:?[2,2,2,2,5,5]output:?400Ans:def?product_list(mylist):??total?=?1??for?i?in?mylist:????total?*=?i??return?totalx?=?[1,2,3,4,5,6,7,8]print(product_list(x))Check?whether?a?string?is?palindrome?or?not?using?functioninput?:?madamoutput?:?yes?it?is?a?palindromeinput:?nandoutput?:?no?it?is?not?a?palindromeAns:def?check_palindrome(mystring):??if?mystring?==?mystring[::-1]:?#?refer?practice?question?1?document????print("yes?it?is?a?palindrome")??else:????print("no?it?is?not?a?palindrome")check_palindrome("2madam2")Program to calculate the sum of three given numbers, if the values are equal then return thrice of their sum.Ans:def?sum_thrice(x,?y,?z):??sum?=?x?+?y?+?z??if?x?==?y?==?z:????sum?=?sum?*?3??return?sumprint(sum_thrice(1,?2,?3))print(sum_thrice(3,?3,?3))Fibonacci series with ConstraintsExplanationDisplay?sequence?of?Fibonacci?series?until?the?number?given?by?the?user?and? count?total?even?numbers?and?odd?numbers?in?that?series?except?zero.Fibonacci?series?should?start?with?1.Total?count?of?even?numbers?should?be?displayed?in?first?row?and?odd?numbers?should?be?displayed?in?next?row.Number?given?by?user?for?fibonacci?series?should?ve?greater?than?5?and?less ?than?or?equal?to?20,?otherwise?display?"INVALID?INPUT"If?number?given?by?user?is?space,?character?or?empty?display?"INVALID?INPUT"The?text?message?displayed?should?be?in?exact?format?as?it?is?case?sensitive.In?below?example?2?is?for?count?of?even?numbers?that?is?(2,8)?and?4?is?count?of?odd?numbers?that?is?(1,1,3,5).Input:6Output:[1,1,2,3,5,8](fibonacci series)2(count of even numbers in fibonacci series)4(count of odd numbers in fibonacci series)Input:10Output:[1,1,2,3,5,8,13,21,34,55]37Ans:def?myfun(x):????a?=?1????b?=?1????mylist?=?[1,1]????for?i?in?range(2,x):????????c?=?a?+?b????????a?=?b????????b?=?c????????mylist.append(c)????print(mylist)????even?=?[]????odd?=?[]????for?each?in?mylist:????????if?each%2==0:????????????even.append(each)????????else:????????????odd.append(each)????print(len(even))????print(len(odd),end='')????????????n?=?input()if?n==''?or?n=='?'?or?n.isalpha():????print("INVALID?INPUT",end='')elif?int(n)>5?and?int(n)<=20:????myfun(int(n))else:????print("INVALID?INPUT",end='')Create a histogram from a given list of integers.Ans:def?histogram(?mychar,?items?):??for?n?in?items:????output?=?''????times?=?n????while(?times?>?0?):??????output?+=?mychar??????times?=?times?-?1????print(output)histogram('@', [2,?3,?6,?5])Sum of digitsinput:?1234output:?10Explanation:?1+2+3+4?=?10input:?55555output:?25Explanation:?5+5+5+5+5?=?25Ansdef?myfun(num):??total?=?0??while?num:????digit?=?num%10????total?+=?digit????num?//=?10????return?total??print(myfun(55555))Sum of digits to single numberinput:?1234output:?1Explanation:?1+2+3+4?=?10;?1+0?=?1input:?991output:?7Explanation:?9+9+1?=?19;?1+9?=?10;?1+0?=?1Ans:def?myfun(num):??total?=?0??while?num:????digit?=?num%10????total?+=?digit????num?//=?10????if?total>=10:????return?myfun(total)??else:????return?total??print(myfun(991))Function that takes a sequence of numbers and determines whether all are different from each otherAns:def?test_distinct(data):??if?len(data)?==?len(set(data)):????return?True??else:????return?False;print(test_distinct([1,5,7,9]))print(test_distinct([2,4,5,5,7,9]))Reverse the digits of an integerInput:234Output:432Input:-234Output:-432Input:10000Output:1Ans:def?reverse_integer(x):??sign?=?-1?if?x?<?0?else?1??x?*=?sign??#?Remove?leading?zero?in?the?reversed?integer??while?x:????if?x?%?10?==?0:??????x?/=?10??????x?=?int(x)????else:??????break??#?string?manipulation??x?=?str(x)??lst?=?list(x)??#?list('234')?returns?['2',?'3',?'4']??lst.reverse()??x?=?"".join(lst)??x?=?int(x)??return?sign*x????print(reverse_integer(234))print(reverse_integer(-234))print(reverse_integer(10000))Reverse the digits of a given number and add it to the originalAns:def?rev_number(number):????numberinstring?=?str(number)????reversednumber?=?int(numberinstring[::-1])????number?+=?reversednumber????return?number?print(rev_number(121))print(rev_number(1234))print(rev_number(1473))Write a Python program to replace a string "Python" with "Java" and "Java" with "Python" in a given string. (ignore case)input:?Hello?Java?I?know?Python.?PYTHON?is?Easier?than?JAVAoutput:?hello?python?i?know?java.?java?is?easier?than?pythoninput:?java?java?python?python?javaoutput:?python?python?java?java?pythonAns:def?interchange(mystr):??newstr?=?mystr.lower()??newstr?=?newstr.replace('python',?'##$##')??#?encoded?string??newstr?=?newstr.replace('java',?'python')??newstr?=?newstr.replace('##$##',?'java')??return?newstr??print(interchange("java?java?python?python?java"))Print all prime numbers in given range (HARD)Prime?numbers?between?0?and?100?are:2?3?5?7?11?13?17?19?23?29?31?37?41?43?47?53?59?61?67?71?73?79?83?89?97Prime?numbers?between?100?and?200?are:101?103?107?109?113?127?131?137?139?149?151?157?163?167?173?179?181?191?193?197?199Ans:lower?=?100upper?=?200print(f"Prime?numbers?between?{lower}?and?{upper}?are:")for?num?in?range(lower,?upper?+?1):????#?all?prime?numbers?are?greater?than?1????if?num?>?1:????????for?i?in?range(2,?num):????????????if?(num?%?i)?==?0:????????????????break????????else:????????????print(num,?end='?') ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download