Posts

Showing posts from February, 2019

Extra Questions

Q3 d. Given a list of integers L, write code to calculate and display the sum of all even numbers in the list. Also display how many numbers in the list ends with 3. user_input=int(input('Please enter length of the list: ')) numList=[] sum=0 count=0 print('\n') for x in range(user_input): numList.append(str(input('enter list elements: '))) if (int(numList[x])%2)==0: sum=int(numList[x])+sum if numList[x][len(numList[x])-1]=='3': count=count+1 print('\nSum of all the even integers: ',sum) print('Numbers ending in 3 : ',count) Q3 e. Write a python code to generate and print a dictionary that contains a number (between 1 and n) in the form (x:x*x) my_Dictionary={} index=int(input('enter a number betwen 1 and n: ')) for x in range(1,index): key=x value=x*x my_Dictionary[key]=value print(my_Dictionary)

Dictionary (Python)

DICTIONARY 1. Repeatedly ask the user to enter a team name and how many games the team has won and how many they lost. Store this information in a dictionary where the keys are the team names and the values are the lists of the form [wins, losses]. (a) Using the dictionary created above, allow the user to enter a team name and print out the teams winning percentage. (b) Using the dictionary, create a list whose entries are the number of wins of each team. (c) Using the dictionary, create a list of all those teams that have winning records. team_dict ={} winningList=[] winningTeam=[] numofTeams=int(input("Insert Number of Teams: ")) for x in range(numofTeams): teamName=input('Team Name: ') winLossList=[] wins = int(input('Wins: ')) winningList.append(wins) losses=int(input('Losses: ')) if(wins>losses): winningTeam.append(teamName) winLossList.append(wins) winLossList.append(losses) team_dict[teamName] = winLossList print(...

Tuple (Python)

TUPLE 1. Write a Python program that creates a tuple storing first 9 terms of Fibonacci series. fib=(0,) n=int(input('Please enter a number ')) a,b=0,1 for i in range(n): a, b = b, a+b fib=fib+(a,) print(fib) 2. (a) Write a program that receives the index and returns the corresponding value. fib=(0,) n=int(input('Please enter a number ')) a,b=0,1 for i in range(n): a, b = b, a+b fib=fib+(a,) print(fib) readInput=int(input("Enter an index ")) print("Element at index ",readInput," is : ",fib[readInput]) (b) Write a program that receives a Fibonacci term and returns a number telling which term it is for instance, if you pass 3, it returns 5, telling it is 5th term; for 8, it returns 7. fib=(0,) n=int(input('Please enter a number ')) a,b=0,1 for i in range(n): a, b = b, a+b fib=fib+(a,) print(fib) readInput=int(input("Enter a fibonacci term: ")) print(fib.index(readInput)+1) 3. Write a pr...

List (Python)

LIST 1. Write a program that reverses an array of integers (in place). lst = [10, 11, 12, 13, 14, 15] print(lst) lst.reverse() print("Reversing the list") print(lst) 2. Write a program that inputs two lists and creates a third,  that contain all elements of the first followed by all elements of the second. ListOne=['H','e','l','l','o'] ListTwo=['W','o','r','l','d'] ListThree=[] print(ListOne) print(ListTwo) ListOne.extend(ListTwo) ListThree.extend(ListOne) print(ListThree) 3. Ask the user to enter a list containing numbers between 1 and 12. Then replace all of the entries in the list that are  greater than 10 with 10. mylist=[] user_input=int(input('Please enter a number: ')) for i in range(user_input): numbers = int(input('Please enter some values between 1 and 12: ')) mylist.append(numbers) print(mylist) for i in mylist: if i>10: mylist[mylist.index(i)]=10 pr...